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/serialization_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 SerializationUtilsChromeOSTest : public testing::Test { |
| 18 protected: |
| 19 SerializationUtilsChromeOSTest() { |
| 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() OVERRIDE { |
| 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 scoped_ptr<MetricSample> deserialized = |
| 36 SerializationUtils::ParseSample(serialized); |
| 37 ASSERT_TRUE(deserialized); |
| 38 EXPECT_TRUE(sample->IsEqual(*deserialized.get())); |
| 39 } |
| 40 |
| 41 std::string filename; |
| 42 base::ScopedTempDir temporary_dir; |
| 43 base::FilePath filepath; |
| 44 }; |
| 45 |
| 46 TEST_F(SerializationUtilsChromeOSTest, CrashSerializeTest) { |
| 47 TestSerialization(MetricSample::CrashSample("test").get()); |
| 48 } |
| 49 |
| 50 TEST_F(SerializationUtilsChromeOSTest, HistogramSerializeTest) { |
| 51 TestSerialization( |
| 52 MetricSample::HistogramSample("myhist", 13, 1, 100, 10).get()); |
| 53 } |
| 54 |
| 55 TEST_F(SerializationUtilsChromeOSTest, LinearSerializeTest) { |
| 56 TestSerialization( |
| 57 MetricSample::LinearHistogramSample("linearhist", 12, 30).get()); |
| 58 } |
| 59 |
| 60 TEST_F(SerializationUtilsChromeOSTest, SparseSerializeTest) { |
| 61 TestSerialization(MetricSample::SparseHistogramSample("mysparse", 30).get()); |
| 62 } |
| 63 |
| 64 TEST_F(SerializationUtilsChromeOSTest, UserActionSerializeTest) { |
| 65 TestSerialization(MetricSample::UserActionSample("myaction").get()); |
| 66 } |
| 67 |
| 68 TEST_F(SerializationUtilsChromeOSTest, 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); |
| 73 |
| 74 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample1.get(), filename)); |
| 75 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample2.get(), filename)); |
| 76 int64 size = 0; |
| 77 |
| 78 ASSERT_TRUE(!PathExists(filepath) || base::GetFileSize(filepath, &size)); |
| 79 |
| 80 EXPECT_EQ(0, size); |
| 81 } |
| 82 |
| 83 TEST_F(SerializationUtilsChromeOSTest, BadInputIsCaughtTest) { |
| 84 std::string input( |
| 85 base::StringPrintf("sparsehistogram%cname foo%c", '\0', '\0')); |
| 86 EXPECT_EQ(NULL, MetricSample::ParseSparseHistogram(input).get()); |
| 87 } |
| 88 |
| 89 TEST_F(SerializationUtilsChromeOSTest, MessageSeparatedByZero) { |
| 90 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash"); |
| 91 |
| 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); |
| 101 } |
| 102 |
| 103 TEST_F(SerializationUtilsChromeOSTest, 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'); |
| 108 |
| 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); |
| 114 } |
| 115 |
| 116 TEST_F(SerializationUtilsChromeOSTest, 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'); |
| 120 |
| 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(); |
| 126 |
| 127 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("test"); |
| 128 SerializationUtils::WriteMetricToFile(*crash.get(), filename); |
| 129 |
| 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])); |
| 135 } |
| 136 |
| 137 TEST_F(SerializationUtilsChromeOSTest, 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"); |
| 146 |
| 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); |
| 157 } |
| 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])); |
| 163 |
| 164 int64 size = 0; |
| 165 ASSERT_TRUE(base::GetFileSize(filepath, &size)); |
| 166 ASSERT_EQ(0, size); |
| 167 } |
| 168 |
| 169 } // namespace |
| 170 } // namespace metrics |
OLD | NEW |