OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "components/metrics/chromeos/metrics_utils.h" | |
6 | |
7 #include "base/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/chromeos/metric_sample.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace metrics { | |
15 namespace { | |
16 | |
17 class MetricsUtilsChromeOSTest : public testing::Test { | |
18 protected: | |
19 MetricsUtilsChromeOSTest() { | |
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); | |
25 } | |
26 } | |
27 | |
28 virtual void SetUp() { | |
Alexei Svitkine (slow)
2014/05/13 19:52:42
OVERRIDE
bsimonnet
2014/05/13 21:28:00
Done.
| |
29 base::DeleteFile(filepath, false); | |
30 } | |
31 | |
32 void TestSerialization(MetricSample* sample) { | |
33 std::string serialized(sample->ToString()); | |
34 ASSERT_EQ('\0', serialized[serialized.length() - 1]); | |
35 EXPECT_TRUE( | |
36 sample->IsEqual(MetricsUtils::ReadSample(serialized).release())); | |
Alexei Svitkine (slow)
2014/05/13 19:52:42
I think you want .get() instead of .release() here
bsimonnet
2014/05/13 21:28:00
Done.
| |
37 } | |
38 | |
39 std::string filename; | |
40 base::ScopedTempDir temporary_dir; | |
41 base::FilePath filepath; | |
42 }; | |
43 | |
44 TEST_F(MetricsUtilsChromeOSTest, CrashSerializeTest) { | |
45 TestSerialization(MetricSample::CrashSample("test").release()); | |
46 } | |
47 | |
48 TEST_F(MetricsUtilsChromeOSTest, HistogramSerializeTest) { | |
49 TestSerialization( | |
50 MetricSample::HistogramSample("myhist", 13, 1, 100, 10).release()); | |
51 } | |
52 | |
53 TEST_F(MetricsUtilsChromeOSTest, LinearSerializeTest) { | |
54 TestSerialization( | |
55 MetricSample::LinearHistogramSample("linearhist", 12, 30).release()); | |
56 } | |
57 | |
58 TEST_F(MetricsUtilsChromeOSTest, SparseSerializeTest) { | |
59 TestSerialization( | |
60 MetricSample::SparseHistogramSample("mysparse", 30).release()); | |
61 } | |
62 | |
63 TEST_F(MetricsUtilsChromeOSTest, UserActionSerializeTest) { | |
64 TestSerialization(MetricSample::UserActionSample("myaction").release()); | |
65 } | |
66 | |
67 TEST_F(MetricsUtilsChromeOSTest, IllegalNameAreFilteredTest) { | |
68 scoped_ptr<MetricSample> sample1 = | |
69 MetricSample::SparseHistogramSample("no space", 10); | |
70 scoped_ptr<MetricSample> sample2 = MetricSample::LinearHistogramSample( | |
71 base::StringPrintf("here%cbhe", '\0'), 1, 3); | |
72 | |
73 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample1.get(), filename)); | |
74 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample2.get(), filename)); | |
75 int64 size = 0; | |
76 | |
77 ASSERT_TRUE(!PathExists(filepath) || base::GetFileSize(filepath, &size)); | |
78 | |
79 EXPECT_EQ(0, size); | |
80 } | |
81 | |
82 TEST_F(MetricsUtilsChromeOSTest, BadInputIsCaughtTest) { | |
83 std::string input( | |
84 base::StringPrintf("sparsehistogram%cname foo%c", '\0', '\0')); | |
85 EXPECT_EQ(NULL, MetricSample::ReadSparseHistogram(input).release()); | |
86 } | |
87 | |
88 TEST_F(MetricsUtilsChromeOSTest, MessageSeparatedByZero) { | |
89 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash"); | |
90 | |
91 MetricsUtils::WriteMetricToFile(crash.get(), filename); | |
92 int64 size = 0; | |
93 ASSERT_TRUE(base::GetFileSize(filepath, &size)); | |
94 // 4 bytes for the size | |
95 // 5 bytes for crash | |
96 // 7 bytes for mycrash | |
97 // 2 bytes for the \0 | |
98 // -> total of 18 | |
99 EXPECT_EQ(size, 18); | |
100 } | |
101 | |
102 TEST_F(MetricsUtilsChromeOSTest, MessagesTooLongAreDiscardedTest) { | |
103 // Creates a message that is bigger than the maximum allowed size. | |
104 // As we are adding extra character (crash, \0s, etc), if the name is | |
105 // kMessageMaxLength long, it will be too long. | |
106 std::string name(MetricsUtils::kMessageMaxLength, 'c'); | |
107 | |
108 scoped_ptr<MetricSample> crash = MetricSample::CrashSample(name); | |
109 EXPECT_FALSE(MetricsUtils::WriteMetricToFile(crash.get(), filename)); | |
110 int64 size = 0; | |
111 ASSERT_TRUE(base::GetFileSize(filepath, &size)); | |
112 EXPECT_EQ(0, size); | |
113 } | |
114 | |
115 TEST_F(MetricsUtilsChromeOSTest, ReadLongMessageTest) { | |
116 base::File test_file(filepath, | |
117 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); | |
118 std::string message(MetricsUtils::kMessageMaxLength + 1, 'c'); | |
119 | |
120 int32_t message_size = message.length() + sizeof(int32_t); | |
Alexei Svitkine (slow)
2014/05/13 19:52:42
Nit: Prefer int32 over int32_t types for new code.
bsimonnet
2014/05/13 21:28:00
Done.
| |
121 test_file.WriteAtCurrentPos(reinterpret_cast<const char*>(&message_size), | |
122 sizeof(message_size)); | |
123 test_file.WriteAtCurrentPos(message.c_str(), message.length()); | |
124 test_file.Close(); | |
125 | |
126 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("test"); | |
127 MetricsUtils::WriteMetricToFile(crash.get(), filename); | |
128 | |
129 ScopedVector<MetricSample> samples; | |
130 MetricsUtils::ReadAndTruncateMetricsFromFile(filename, &samples); | |
131 ASSERT_EQ(size_t(1), samples.size()); | |
132 EXPECT_TRUE(crash->IsEqual(samples[0])); | |
133 } | |
134 | |
135 TEST_F(MetricsUtilsChromeOSTest, 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"); | |
144 | |
145 MetricsUtils::WriteMetricToFile(hist.get(), filename); | |
146 MetricsUtils::WriteMetricToFile(crash.get(), filename); | |
147 MetricsUtils::WriteMetricToFile(lhist.get(), filename); | |
148 MetricsUtils::WriteMetricToFile(shist.get(), filename); | |
149 MetricsUtils::WriteMetricToFile(action.get(), filename); | |
150 ScopedVector<MetricSample> vect; | |
151 MetricsUtils::ReadAndTruncateMetricsFromFile(filename, &vect); | |
152 ASSERT_EQ(vect.size(), size_t(5)); | |
153 EXPECT_TRUE(hist->IsEqual(vect[0])); | |
154 EXPECT_TRUE(crash->IsEqual(vect[1])); | |
155 EXPECT_TRUE(lhist->IsEqual(vect[2])); | |
156 EXPECT_TRUE(shist->IsEqual(vect[3])); | |
157 EXPECT_TRUE(action->IsEqual(vect[4])); | |
158 | |
159 int64 size = 0; | |
160 ASSERT_TRUE(base::GetFileSize(filepath, &size)); | |
161 ASSERT_EQ(0, size); | |
162 } | |
163 | |
164 } // namespace | |
165 } // namespace metrics | |
OLD | NEW |