Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / components / metrics / serialization / serialization_utils_unittest.cc
blob083735ccb5c4d5d65c3e5f38a412c4828d9b7eb2
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/serialization/serialization_utils.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "components/metrics/serialization/metric_sample.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace metrics {
15 namespace {
17 class SerializationUtilsTest : public testing::Test {
18 protected:
19 SerializationUtilsTest() {
20 bool success = temporary_dir.CreateUniqueTempDir();
21 if (success) {
22 base::FilePath dir_path = temporary_dir.path();
23 filename = dir_path.value() + "chromeossampletest";
24 filepath = base::FilePath(filename);
28 void SetUp() override { base::DeleteFile(filepath, false); }
30 void TestSerialization(MetricSample* sample) {
31 std::string serialized(sample->ToString());
32 ASSERT_EQ('\0', serialized[serialized.length() - 1]);
33 scoped_ptr<MetricSample> deserialized =
34 SerializationUtils::ParseSample(serialized);
35 ASSERT_TRUE(deserialized);
36 EXPECT_TRUE(sample->IsEqual(*deserialized.get()));
39 std::string filename;
40 base::ScopedTempDir temporary_dir;
41 base::FilePath filepath;
44 TEST_F(SerializationUtilsTest, CrashSerializeTest) {
45 TestSerialization(MetricSample::CrashSample("test").get());
48 TEST_F(SerializationUtilsTest, HistogramSerializeTest) {
49 TestSerialization(
50 MetricSample::HistogramSample("myhist", 13, 1, 100, 10).get());
53 TEST_F(SerializationUtilsTest, LinearSerializeTest) {
54 TestSerialization(
55 MetricSample::LinearHistogramSample("linearhist", 12, 30).get());
58 TEST_F(SerializationUtilsTest, SparseSerializeTest) {
59 TestSerialization(MetricSample::SparseHistogramSample("mysparse", 30).get());
62 TEST_F(SerializationUtilsTest, UserActionSerializeTest) {
63 TestSerialization(MetricSample::UserActionSample("myaction").get());
66 TEST_F(SerializationUtilsTest, IllegalNameAreFilteredTest) {
67 scoped_ptr<MetricSample> sample1 =
68 MetricSample::SparseHistogramSample("no space", 10);
69 scoped_ptr<MetricSample> sample2 = MetricSample::LinearHistogramSample(
70 base::StringPrintf("here%cbhe", '\0'), 1, 3);
72 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample1.get(), filename));
73 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample2.get(), filename));
74 int64 size = 0;
76 ASSERT_TRUE(!PathExists(filepath) || base::GetFileSize(filepath, &size));
78 EXPECT_EQ(0, size);
81 TEST_F(SerializationUtilsTest, BadInputIsCaughtTest) {
82 std::string input(
83 base::StringPrintf("sparsehistogram%cname foo%c", '\0', '\0'));
84 EXPECT_EQ(NULL, MetricSample::ParseSparseHistogram(input).get());
87 TEST_F(SerializationUtilsTest, MessageSeparatedByZero) {
88 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash");
90 SerializationUtils::WriteMetricToFile(*crash.get(), filename);
91 int64 size = 0;
92 ASSERT_TRUE(base::GetFileSize(filepath, &size));
93 // 4 bytes for the size
94 // 5 bytes for crash
95 // 7 bytes for mycrash
96 // 2 bytes for the \0
97 // -> total of 18
98 EXPECT_EQ(size, 18);
101 TEST_F(SerializationUtilsTest, MessagesTooLongAreDiscardedTest) {
102 // Creates a message that is bigger than the maximum allowed size.
103 // As we are adding extra character (crash, \0s, etc), if the name is
104 // kMessageMaxLength long, it will be too long.
105 std::string name(SerializationUtils::kMessageMaxLength, 'c');
107 scoped_ptr<MetricSample> crash = MetricSample::CrashSample(name);
108 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*crash.get(), filename));
109 int64 size = 0;
110 ASSERT_TRUE(base::GetFileSize(filepath, &size));
111 EXPECT_EQ(0, size);
114 TEST_F(SerializationUtilsTest, ReadLongMessageTest) {
115 base::File test_file(filepath,
116 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND);
117 std::string message(SerializationUtils::kMessageMaxLength + 1, 'c');
119 int32 message_size = message.length() + sizeof(int32);
120 test_file.WriteAtCurrentPos(reinterpret_cast<const char*>(&message_size),
121 sizeof(message_size));
122 test_file.WriteAtCurrentPos(message.c_str(), message.length());
123 test_file.Close();
125 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("test");
126 SerializationUtils::WriteMetricToFile(*crash.get(), filename);
128 ScopedVector<MetricSample> samples;
129 SerializationUtils::ReadAndTruncateMetricsFromFile(filename, &samples);
130 ASSERT_EQ(size_t(1), samples.size());
131 ASSERT_TRUE(samples[0] != NULL);
132 EXPECT_TRUE(crash->IsEqual(*samples[0]));
135 TEST_F(SerializationUtilsTest, WriteReadTest) {
136 scoped_ptr<MetricSample> hist =
137 MetricSample::HistogramSample("myhist", 1, 2, 3, 4);
138 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash");
139 scoped_ptr<MetricSample> lhist =
140 MetricSample::LinearHistogramSample("linear", 1, 10);
141 scoped_ptr<MetricSample> shist =
142 MetricSample::SparseHistogramSample("mysparse", 30);
143 scoped_ptr<MetricSample> action = MetricSample::UserActionSample("myaction");
145 SerializationUtils::WriteMetricToFile(*hist.get(), filename);
146 SerializationUtils::WriteMetricToFile(*crash.get(), filename);
147 SerializationUtils::WriteMetricToFile(*lhist.get(), filename);
148 SerializationUtils::WriteMetricToFile(*shist.get(), filename);
149 SerializationUtils::WriteMetricToFile(*action.get(), filename);
150 ScopedVector<MetricSample> vect;
151 SerializationUtils::ReadAndTruncateMetricsFromFile(filename, &vect);
152 ASSERT_EQ(vect.size(), size_t(5));
153 for (int i = 0; i < 5; i++) {
154 ASSERT_TRUE(vect[0] != NULL);
156 EXPECT_TRUE(hist->IsEqual(*vect[0]));
157 EXPECT_TRUE(crash->IsEqual(*vect[1]));
158 EXPECT_TRUE(lhist->IsEqual(*vect[2]));
159 EXPECT_TRUE(shist->IsEqual(*vect[3]));
160 EXPECT_TRUE(action->IsEqual(*vect[4]));
162 int64 size = 0;
163 ASSERT_TRUE(base::GetFileSize(filepath, &size));
164 ASSERT_EQ(0, size);
167 } // namespace
168 } // namespace metrics