Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / metrics / serialization / serialization_utils_unittest.cc
blob5a080aea771fa2fe1571f3c5bf1f5740372285d3
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/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 virtual void SetUp() OVERRIDE {
29 base::DeleteFile(filepath, false);
32 void TestSerialization(MetricSample* sample) {
33 std::string serialized(sample->ToString());
34 ASSERT_EQ('\0', serialized[serialized.length() - 1]);
35 scoped_ptr<MetricSample> deserialized =
36 SerializationUtils::ParseSample(serialized);
37 ASSERT_TRUE(deserialized);
38 EXPECT_TRUE(sample->IsEqual(*deserialized.get()));
41 std::string filename;
42 base::ScopedTempDir temporary_dir;
43 base::FilePath filepath;
46 TEST_F(SerializationUtilsTest, CrashSerializeTest) {
47 TestSerialization(MetricSample::CrashSample("test").get());
50 TEST_F(SerializationUtilsTest, HistogramSerializeTest) {
51 TestSerialization(
52 MetricSample::HistogramSample("myhist", 13, 1, 100, 10).get());
55 TEST_F(SerializationUtilsTest, LinearSerializeTest) {
56 TestSerialization(
57 MetricSample::LinearHistogramSample("linearhist", 12, 30).get());
60 TEST_F(SerializationUtilsTest, SparseSerializeTest) {
61 TestSerialization(MetricSample::SparseHistogramSample("mysparse", 30).get());
64 TEST_F(SerializationUtilsTest, UserActionSerializeTest) {
65 TestSerialization(MetricSample::UserActionSample("myaction").get());
68 TEST_F(SerializationUtilsTest, IllegalNameAreFilteredTest) {
69 scoped_ptr<MetricSample> sample1 =
70 MetricSample::SparseHistogramSample("no space", 10);
71 scoped_ptr<MetricSample> sample2 = MetricSample::LinearHistogramSample(
72 base::StringPrintf("here%cbhe", '\0'), 1, 3);
74 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample1.get(), filename));
75 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample2.get(), filename));
76 int64 size = 0;
78 ASSERT_TRUE(!PathExists(filepath) || base::GetFileSize(filepath, &size));
80 EXPECT_EQ(0, size);
83 TEST_F(SerializationUtilsTest, BadInputIsCaughtTest) {
84 std::string input(
85 base::StringPrintf("sparsehistogram%cname foo%c", '\0', '\0'));
86 EXPECT_EQ(NULL, MetricSample::ParseSparseHistogram(input).get());
89 TEST_F(SerializationUtilsTest, MessageSeparatedByZero) {
90 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash");
92 SerializationUtils::WriteMetricToFile(*crash.get(), filename);
93 int64 size = 0;
94 ASSERT_TRUE(base::GetFileSize(filepath, &size));
95 // 4 bytes for the size
96 // 5 bytes for crash
97 // 7 bytes for mycrash
98 // 2 bytes for the \0
99 // -> total of 18
100 EXPECT_EQ(size, 18);
103 TEST_F(SerializationUtilsTest, MessagesTooLongAreDiscardedTest) {
104 // Creates a message that is bigger than the maximum allowed size.
105 // As we are adding extra character (crash, \0s, etc), if the name is
106 // kMessageMaxLength long, it will be too long.
107 std::string name(SerializationUtils::kMessageMaxLength, 'c');
109 scoped_ptr<MetricSample> crash = MetricSample::CrashSample(name);
110 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*crash.get(), filename));
111 int64 size = 0;
112 ASSERT_TRUE(base::GetFileSize(filepath, &size));
113 EXPECT_EQ(0, size);
116 TEST_F(SerializationUtilsTest, ReadLongMessageTest) {
117 base::File test_file(filepath,
118 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND);
119 std::string message(SerializationUtils::kMessageMaxLength + 1, 'c');
121 int32 message_size = message.length() + sizeof(int32);
122 test_file.WriteAtCurrentPos(reinterpret_cast<const char*>(&message_size),
123 sizeof(message_size));
124 test_file.WriteAtCurrentPos(message.c_str(), message.length());
125 test_file.Close();
127 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("test");
128 SerializationUtils::WriteMetricToFile(*crash.get(), filename);
130 ScopedVector<MetricSample> samples;
131 SerializationUtils::ReadAndTruncateMetricsFromFile(filename, &samples);
132 ASSERT_EQ(size_t(1), samples.size());
133 ASSERT_TRUE(samples[0] != NULL);
134 EXPECT_TRUE(crash->IsEqual(*samples[0]));
137 TEST_F(SerializationUtilsTest, WriteReadTest) {
138 scoped_ptr<MetricSample> hist =
139 MetricSample::HistogramSample("myhist", 1, 2, 3, 4);
140 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash");
141 scoped_ptr<MetricSample> lhist =
142 MetricSample::LinearHistogramSample("linear", 1, 10);
143 scoped_ptr<MetricSample> shist =
144 MetricSample::SparseHistogramSample("mysparse", 30);
145 scoped_ptr<MetricSample> action = MetricSample::UserActionSample("myaction");
147 SerializationUtils::WriteMetricToFile(*hist.get(), filename);
148 SerializationUtils::WriteMetricToFile(*crash.get(), filename);
149 SerializationUtils::WriteMetricToFile(*lhist.get(), filename);
150 SerializationUtils::WriteMetricToFile(*shist.get(), filename);
151 SerializationUtils::WriteMetricToFile(*action.get(), filename);
152 ScopedVector<MetricSample> vect;
153 SerializationUtils::ReadAndTruncateMetricsFromFile(filename, &vect);
154 ASSERT_EQ(vect.size(), size_t(5));
155 for (int i = 0; i < 5; i++) {
156 ASSERT_TRUE(vect[0] != NULL);
158 EXPECT_TRUE(hist->IsEqual(*vect[0]));
159 EXPECT_TRUE(crash->IsEqual(*vect[1]));
160 EXPECT_TRUE(lhist->IsEqual(*vect[2]));
161 EXPECT_TRUE(shist->IsEqual(*vect[3]));
162 EXPECT_TRUE(action->IsEqual(*vect[4]));
164 int64 size = 0;
165 ASSERT_TRUE(base::GetFileSize(filepath, &size));
166 ASSERT_EQ(0, size);
169 } // namespace
170 } // namespace metrics