| 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/serialization/serialization_utils.h" | 5 #include "components/metrics/serialization/serialization_utils.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 12 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 11 #include "components/metrics/serialization/metric_sample.h" | 14 #include "components/metrics/serialization/metric_sample.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 16 |
| 14 namespace metrics { | 17 namespace metrics { |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 } | 67 } |
| 65 | 68 |
| 66 TEST_F(SerializationUtilsTest, IllegalNameAreFilteredTest) { | 69 TEST_F(SerializationUtilsTest, IllegalNameAreFilteredTest) { |
| 67 scoped_ptr<MetricSample> sample1 = | 70 scoped_ptr<MetricSample> sample1 = |
| 68 MetricSample::SparseHistogramSample("no space", 10); | 71 MetricSample::SparseHistogramSample("no space", 10); |
| 69 scoped_ptr<MetricSample> sample2 = MetricSample::LinearHistogramSample( | 72 scoped_ptr<MetricSample> sample2 = MetricSample::LinearHistogramSample( |
| 70 base::StringPrintf("here%cbhe", '\0'), 1, 3); | 73 base::StringPrintf("here%cbhe", '\0'), 1, 3); |
| 71 | 74 |
| 72 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample1.get(), filename)); | 75 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample1.get(), filename)); |
| 73 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample2.get(), filename)); | 76 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*sample2.get(), filename)); |
| 74 int64 size = 0; | 77 int64_t size = 0; |
| 75 | 78 |
| 76 ASSERT_TRUE(!PathExists(filepath) || base::GetFileSize(filepath, &size)); | 79 ASSERT_TRUE(!PathExists(filepath) || base::GetFileSize(filepath, &size)); |
| 77 | 80 |
| 78 EXPECT_EQ(0, size); | 81 EXPECT_EQ(0, size); |
| 79 } | 82 } |
| 80 | 83 |
| 81 TEST_F(SerializationUtilsTest, BadInputIsCaughtTest) { | 84 TEST_F(SerializationUtilsTest, BadInputIsCaughtTest) { |
| 82 std::string input( | 85 std::string input( |
| 83 base::StringPrintf("sparsehistogram%cname foo%c", '\0', '\0')); | 86 base::StringPrintf("sparsehistogram%cname foo%c", '\0', '\0')); |
| 84 EXPECT_EQ(NULL, MetricSample::ParseSparseHistogram(input).get()); | 87 EXPECT_EQ(NULL, MetricSample::ParseSparseHistogram(input).get()); |
| 85 } | 88 } |
| 86 | 89 |
| 87 TEST_F(SerializationUtilsTest, MessageSeparatedByZero) { | 90 TEST_F(SerializationUtilsTest, MessageSeparatedByZero) { |
| 88 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash"); | 91 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("mycrash"); |
| 89 | 92 |
| 90 SerializationUtils::WriteMetricToFile(*crash.get(), filename); | 93 SerializationUtils::WriteMetricToFile(*crash.get(), filename); |
| 91 int64 size = 0; | 94 int64_t size = 0; |
| 92 ASSERT_TRUE(base::GetFileSize(filepath, &size)); | 95 ASSERT_TRUE(base::GetFileSize(filepath, &size)); |
| 93 // 4 bytes for the size | 96 // 4 bytes for the size |
| 94 // 5 bytes for crash | 97 // 5 bytes for crash |
| 95 // 7 bytes for mycrash | 98 // 7 bytes for mycrash |
| 96 // 2 bytes for the \0 | 99 // 2 bytes for the \0 |
| 97 // -> total of 18 | 100 // -> total of 18 |
| 98 EXPECT_EQ(size, 18); | 101 EXPECT_EQ(size, 18); |
| 99 } | 102 } |
| 100 | 103 |
| 101 TEST_F(SerializationUtilsTest, MessagesTooLongAreDiscardedTest) { | 104 TEST_F(SerializationUtilsTest, MessagesTooLongAreDiscardedTest) { |
| 102 // Creates a message that is bigger than the maximum allowed size. | 105 // 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 | 106 // As we are adding extra character (crash, \0s, etc), if the name is |
| 104 // kMessageMaxLength long, it will be too long. | 107 // kMessageMaxLength long, it will be too long. |
| 105 std::string name(SerializationUtils::kMessageMaxLength, 'c'); | 108 std::string name(SerializationUtils::kMessageMaxLength, 'c'); |
| 106 | 109 |
| 107 scoped_ptr<MetricSample> crash = MetricSample::CrashSample(name); | 110 scoped_ptr<MetricSample> crash = MetricSample::CrashSample(name); |
| 108 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*crash.get(), filename)); | 111 EXPECT_FALSE(SerializationUtils::WriteMetricToFile(*crash.get(), filename)); |
| 109 int64 size = 0; | 112 int64_t size = 0; |
| 110 ASSERT_TRUE(base::GetFileSize(filepath, &size)); | 113 ASSERT_TRUE(base::GetFileSize(filepath, &size)); |
| 111 EXPECT_EQ(0, size); | 114 EXPECT_EQ(0, size); |
| 112 } | 115 } |
| 113 | 116 |
| 114 TEST_F(SerializationUtilsTest, ReadLongMessageTest) { | 117 TEST_F(SerializationUtilsTest, ReadLongMessageTest) { |
| 115 base::File test_file(filepath, | 118 base::File test_file(filepath, |
| 116 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); | 119 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); |
| 117 std::string message(SerializationUtils::kMessageMaxLength + 1, 'c'); | 120 std::string message(SerializationUtils::kMessageMaxLength + 1, 'c'); |
| 118 | 121 |
| 119 int32 message_size = message.length() + sizeof(int32); | 122 int32_t message_size = message.length() + sizeof(int32_t); |
| 120 test_file.WriteAtCurrentPos(reinterpret_cast<const char*>(&message_size), | 123 test_file.WriteAtCurrentPos(reinterpret_cast<const char*>(&message_size), |
| 121 sizeof(message_size)); | 124 sizeof(message_size)); |
| 122 test_file.WriteAtCurrentPos(message.c_str(), message.length()); | 125 test_file.WriteAtCurrentPos(message.c_str(), message.length()); |
| 123 test_file.Close(); | 126 test_file.Close(); |
| 124 | 127 |
| 125 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("test"); | 128 scoped_ptr<MetricSample> crash = MetricSample::CrashSample("test"); |
| 126 SerializationUtils::WriteMetricToFile(*crash.get(), filename); | 129 SerializationUtils::WriteMetricToFile(*crash.get(), filename); |
| 127 | 130 |
| 128 ScopedVector<MetricSample> samples; | 131 ScopedVector<MetricSample> samples; |
| 129 SerializationUtils::ReadAndTruncateMetricsFromFile(filename, &samples); | 132 SerializationUtils::ReadAndTruncateMetricsFromFile(filename, &samples); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 152 ASSERT_EQ(vect.size(), size_t(5)); | 155 ASSERT_EQ(vect.size(), size_t(5)); |
| 153 for (int i = 0; i < 5; i++) { | 156 for (int i = 0; i < 5; i++) { |
| 154 ASSERT_TRUE(vect[0] != NULL); | 157 ASSERT_TRUE(vect[0] != NULL); |
| 155 } | 158 } |
| 156 EXPECT_TRUE(hist->IsEqual(*vect[0])); | 159 EXPECT_TRUE(hist->IsEqual(*vect[0])); |
| 157 EXPECT_TRUE(crash->IsEqual(*vect[1])); | 160 EXPECT_TRUE(crash->IsEqual(*vect[1])); |
| 158 EXPECT_TRUE(lhist->IsEqual(*vect[2])); | 161 EXPECT_TRUE(lhist->IsEqual(*vect[2])); |
| 159 EXPECT_TRUE(shist->IsEqual(*vect[3])); | 162 EXPECT_TRUE(shist->IsEqual(*vect[3])); |
| 160 EXPECT_TRUE(action->IsEqual(*vect[4])); | 163 EXPECT_TRUE(action->IsEqual(*vect[4])); |
| 161 | 164 |
| 162 int64 size = 0; | 165 int64_t size = 0; |
| 163 ASSERT_TRUE(base::GetFileSize(filepath, &size)); | 166 ASSERT_TRUE(base::GetFileSize(filepath, &size)); |
| 164 ASSERT_EQ(0, size); | 167 ASSERT_EQ(0, size); |
| 165 } | 168 } |
| 166 | 169 |
| 167 } // namespace | 170 } // namespace |
| 168 } // namespace metrics | 171 } // namespace metrics |
| OLD | NEW |