| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/metrics/chromeos/serialization_utils.h" | 5 #include "components/metrics/serialization/serialization_utils.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "components/metrics/chromeos/metric_sample.h" | 11 #include "components/metrics/serialization/metric_sample.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace metrics { | 14 namespace metrics { |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 class SerializationUtilsChromeOSTest : public testing::Test { | 17 class SerializationUtilsTest : public testing::Test { |
| 18 protected: | 18 protected: |
| 19 SerializationUtilsChromeOSTest() { | 19 SerializationUtilsTest() { |
| 20 bool success = temporary_dir.CreateUniqueTempDir(); | 20 bool success = temporary_dir.CreateUniqueTempDir(); |
| 21 if (success) { | 21 if (success) { |
| 22 base::FilePath dir_path = temporary_dir.path(); | 22 base::FilePath dir_path = temporary_dir.path(); |
| 23 filename = dir_path.value() + "chromeossampletest"; | 23 filename = dir_path.value() + "chromeossampletest"; |
| 24 filepath = base::FilePath(filename); | 24 filepath = base::FilePath(filename); |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 | 27 |
| 28 virtual void SetUp() OVERRIDE { | 28 virtual void SetUp() OVERRIDE { |
| 29 base::DeleteFile(filepath, false); | 29 base::DeleteFile(filepath, false); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void TestSerialization(MetricSample* sample) { | 32 void TestSerialization(MetricSample* sample) { |
| 33 std::string serialized(sample->ToString()); | 33 std::string serialized(sample->ToString()); |
| 34 ASSERT_EQ('\0', serialized[serialized.length() - 1]); | 34 ASSERT_EQ('\0', serialized[serialized.length() - 1]); |
| 35 scoped_ptr<MetricSample> deserialized = | 35 scoped_ptr<MetricSample> deserialized = |
| 36 SerializationUtils::ParseSample(serialized); | 36 SerializationUtils::ParseSample(serialized); |
| 37 ASSERT_TRUE(deserialized); | 37 ASSERT_TRUE(deserialized); |
| 38 EXPECT_TRUE(sample->IsEqual(*deserialized.get())); | 38 EXPECT_TRUE(sample->IsEqual(*deserialized.get())); |
| 39 } | 39 } |
| 40 | 40 |
| 41 std::string filename; | 41 std::string filename; |
| 42 base::ScopedTempDir temporary_dir; | 42 base::ScopedTempDir temporary_dir; |
| 43 base::FilePath filepath; | 43 base::FilePath filepath; |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 TEST_F(SerializationUtilsChromeOSTest, CrashSerializeTest) { | 46 TEST_F(SerializationUtilsTest, CrashSerializeTest) { |
| 47 TestSerialization(MetricSample::CrashSample("test").get()); | 47 TestSerialization(MetricSample::CrashSample("test").get()); |
| 48 } | 48 } |
| 49 | 49 |
| 50 TEST_F(SerializationUtilsChromeOSTest, HistogramSerializeTest) { | 50 TEST_F(SerializationUtilsTest, HistogramSerializeTest) { |
| 51 TestSerialization( | 51 TestSerialization( |
| 52 MetricSample::HistogramSample("myhist", 13, 1, 100, 10).get()); | 52 MetricSample::HistogramSample("myhist", 13, 1, 100, 10).get()); |
| 53 } | 53 } |
| 54 | 54 |
| 55 TEST_F(SerializationUtilsChromeOSTest, LinearSerializeTest) { | 55 TEST_F(SerializationUtilsTest, LinearSerializeTest) { |
| 56 TestSerialization( | 56 TestSerialization( |
| 57 MetricSample::LinearHistogramSample("linearhist", 12, 30).get()); | 57 MetricSample::LinearHistogramSample("linearhist", 12, 30).get()); |
| 58 } | 58 } |
| 59 | 59 |
| 60 TEST_F(SerializationUtilsChromeOSTest, SparseSerializeTest) { | 60 TEST_F(SerializationUtilsTest, SparseSerializeTest) { |
| 61 TestSerialization(MetricSample::SparseHistogramSample("mysparse", 30).get()); | 61 TestSerialization(MetricSample::SparseHistogramSample("mysparse", 30).get()); |
| 62 } | 62 } |
| 63 | 63 |
| 64 TEST_F(SerializationUtilsChromeOSTest, UserActionSerializeTest) { | 64 TEST_F(SerializationUtilsTest, UserActionSerializeTest) { |
| 65 TestSerialization(MetricSample::UserActionSample("myaction").get()); | 65 TestSerialization(MetricSample::UserActionSample("myaction").get()); |
| 66 } | 66 } |
| 67 | 67 |
| 68 TEST_F(SerializationUtilsChromeOSTest, IllegalNameAreFilteredTest) { | 68 TEST_F(SerializationUtilsTest, IllegalNameAreFilteredTest) { |
| 69 scoped_ptr<MetricSample> sample1 = | 69 scoped_ptr<MetricSample> sample1 = |
| 70 MetricSample::SparseHistogramSample("no space", 10); | 70 MetricSample::SparseHistogramSample("no space", 10); |
| 71 scoped_ptr<MetricSample> sample2 = MetricSample::LinearHistogramSample( | 71 scoped_ptr<MetricSample> sample2 = MetricSample::LinearHistogramSample( |
| 72 base::StringPrintf("here%cbhe", '\0'), 1, 3); | 72 base::StringPrintf("here%cbhe", '\0'), 1, 3); |
| 73 | 73 |
| 74 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample1.get(), filename)); | 74 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample1.get(), filename)); |
| 75 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample2.get(), filename)); | 75 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample2.get(), filename)); |
| 76 int64 size = 0; | 76 int64 size = 0; |
| 77 | 77 |
| 78 ASSERT_TRUE(!PathExists(filepath) || base::GetFileSize(filepath, &size)); | 78 ASSERT_TRUE(!PathExists(filepath) || base::GetFileSize(filepath, &size)); |
| 79 | 79 |
| 80 EXPECT_EQ(0, size); | 80 EXPECT_EQ(0, size); |
| 81 } | 81 } |
| 82 | 82 |
| 83 TEST_F(SerializationUtilsChromeOSTest, BadInputIsCaughtTest) { | 83 TEST_F(SerializationUtilsTest, BadInputIsCaughtTest) { |
| 84 std::string input( | 84 std::string input( |
| 85 base::StringPrintf("sparsehistogram%cname foo%c", '\0', '\0')); | 85 base::StringPrintf("sparsehistogram%cname foo%c", '\0', '\0')); |
| 86 EXPECT_EQ(NULL, MetricSample::ParseSparseHistogram(input).get()); | 86 EXPECT_EQ(NULL, MetricSample::ParseSparseHistogram(input).get()); |
| 87 } | 87 } |
| 88 | 88 |
| 89 TEST_F(SerializationUtilsChromeOSTest, MessageSeparatedByZero) { | 89 TEST_F(SerializationUtilsTest, MessageSeparatedByZero) { |
| 90 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash"); | 90 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash"); |
| 91 | 91 |
| 92 SerializationUtils::WriteMetricToFile(*crash.get(), filename); | 92 SerializationUtils::WriteMetricToFile(*crash.get(), filename); |
| 93 int64 size = 0; | 93 int64 size = 0; |
| 94 ASSERT_TRUE(base::GetFileSize(filepath, &size)); | 94 ASSERT_TRUE(base::GetFileSize(filepath, &size)); |
| 95 // 4 bytes for the size | 95 // 4 bytes for the size |
| 96 // 5 bytes for crash | 96 // 5 bytes for crash |
| 97 // 7 bytes for mycrash | 97 // 7 bytes for mycrash |
| 98 // 2 bytes for the \0 | 98 // 2 bytes for the \0 |
| 99 // -> total of 18 | 99 // -> total of 18 |
| 100 EXPECT_EQ(size, 18); | 100 EXPECT_EQ(size, 18); |
| 101 } | 101 } |
| 102 | 102 |
| 103 TEST_F(SerializationUtilsChromeOSTest, MessagesTooLongAreDiscardedTest) { | 103 TEST_F(SerializationUtilsTest, MessagesTooLongAreDiscardedTest) { |
| 104 // Creates a message that is bigger than the maximum allowed size. | 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 | 105 // As we are adding extra character (crash, \0s, etc), if the name is |
| 106 // kMessageMaxLength long, it will be too long. | 106 // kMessageMaxLength long, it will be too long. |
| 107 std::string name(SerializationUtils::kMessageMaxLength, 'c'); | 107 std::string name(SerializationUtils::kMessageMaxLength, 'c'); |
| 108 | 108 |
| 109 scoped_ptr<MetricSample> crash = MetricSample::CrashSample(name); | 109 scoped_ptr<MetricSample> crash = MetricSample::CrashSample(name); |
| 110 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*crash.get(), filename)); | 110 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*crash.get(), filename)); |
| 111 int64 size = 0; | 111 int64 size = 0; |
| 112 ASSERT_TRUE(base::GetFileSize(filepath, &size)); | 112 ASSERT_TRUE(base::GetFileSize(filepath, &size)); |
| 113 EXPECT_EQ(0, size); | 113 EXPECT_EQ(0, size); |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST_F(SerializationUtilsChromeOSTest, ReadLongMessageTest) { | 116 TEST_F(SerializationUtilsTest, ReadLongMessageTest) { |
| 117 base::File test_file(filepath, | 117 base::File test_file(filepath, |
| 118 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); | 118 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); |
| 119 std::string message(SerializationUtils::kMessageMaxLength + 1, 'c'); | 119 std::string message(SerializationUtils::kMessageMaxLength + 1, 'c'); |
| 120 | 120 |
| 121 int32 message_size = message.length() + sizeof(int32); | 121 int32 message_size = message.length() + sizeof(int32); |
| 122 test_file.WriteAtCurrentPos(reinterpret_cast<const char*>(&message_size), | 122 test_file.WriteAtCurrentPos(reinterpret_cast<const char*>(&message_size), |
| 123 sizeof(message_size)); | 123 sizeof(message_size)); |
| 124 test_file.WriteAtCurrentPos(message.c_str(), message.length()); | 124 test_file.WriteAtCurrentPos(message.c_str(), message.length()); |
| 125 test_file.Close(); | 125 test_file.Close(); |
| 126 | 126 |
| 127 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("test"); | 127 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("test"); |
| 128 SerializationUtils::WriteMetricToFile(*crash.get(), filename); | 128 SerializationUtils::WriteMetricToFile(*crash.get(), filename); |
| 129 | 129 |
| 130 ScopedVector<MetricSample> samples; | 130 ScopedVector<MetricSample> samples; |
| 131 SerializationUtils::ReadAndTruncateMetricsFromFile(filename, &samples); | 131 SerializationUtils::ReadAndTruncateMetricsFromFile(filename, &samples); |
| 132 ASSERT_EQ(size_t(1), samples.size()); | 132 ASSERT_EQ(size_t(1), samples.size()); |
| 133 ASSERT_TRUE(samples[0] != NULL); | 133 ASSERT_TRUE(samples[0] != NULL); |
| 134 EXPECT_TRUE(crash->IsEqual(*samples[0])); | 134 EXPECT_TRUE(crash->IsEqual(*samples[0])); |
| 135 } | 135 } |
| 136 | 136 |
| 137 TEST_F(SerializationUtilsChromeOSTest, WriteReadTest) { | 137 TEST_F(SerializationUtilsTest, WriteReadTest) { |
| 138 scoped_ptr<MetricSample> hist = | 138 scoped_ptr<MetricSample> hist = |
| 139 MetricSample::HistogramSample("myhist", 1, 2, 3, 4); | 139 MetricSample::HistogramSample("myhist", 1, 2, 3, 4); |
| 140 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash"); | 140 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash"); |
| 141 scoped_ptr<MetricSample> lhist = | 141 scoped_ptr<MetricSample> lhist = |
| 142 MetricSample::LinearHistogramSample("linear", 1, 10); | 142 MetricSample::LinearHistogramSample("linear", 1, 10); |
| 143 scoped_ptr<MetricSample> shist = | 143 scoped_ptr<MetricSample> shist = |
| 144 MetricSample::SparseHistogramSample("mysparse", 30); | 144 MetricSample::SparseHistogramSample("mysparse", 30); |
| 145 scoped_ptr<MetricSample> action = MetricSample::UserActionSample("myaction"); | 145 scoped_ptr<MetricSample> action = MetricSample::UserActionSample("myaction"); |
| 146 | 146 |
| 147 SerializationUtils::WriteMetricToFile(*hist.get(), filename); | 147 SerializationUtils::WriteMetricToFile(*hist.get(), filename); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 161 EXPECT_TRUE(shist->IsEqual(*vect[3])); | 161 EXPECT_TRUE(shist->IsEqual(*vect[3])); |
| 162 EXPECT_TRUE(action->IsEqual(*vect[4])); | 162 EXPECT_TRUE(action->IsEqual(*vect[4])); |
| 163 | 163 |
| 164 int64 size = 0; | 164 int64 size = 0; |
| 165 ASSERT_TRUE(base::GetFileSize(filepath, &size)); | 165 ASSERT_TRUE(base::GetFileSize(filepath, &size)); |
| 166 ASSERT_EQ(0, size); | 166 ASSERT_EQ(0, size); |
| 167 } | 167 } |
| 168 | 168 |
| 169 } // namespace | 169 } // namespace |
| 170 } // namespace metrics | 170 } // namespace metrics |
| OLD | NEW |