OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 // Test of crash class |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/metrics/chromeos_metrics.h" |
| 10 #include "base/metrics/crash_sample.h" |
| 11 #include "base/metrics/histogram_sample.h" |
| 12 #include "base/metrics/linearhistogram_sample.h" |
| 13 #include "base/metrics/metric_sample.h" |
| 14 #include "base/metrics/sparsehistogram_sample.h" |
| 15 #include "base/metrics/useraction_sample.h" |
| 16 #include "base/strings/stringprintf.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace base { |
| 20 namespace { |
| 21 |
| 22 void AreEqHistogram(HistogramSample* a, HistogramSample* b) { |
| 23 ASSERT_EQ(a->name(), b->name()); |
| 24 ASSERT_EQ(a->sample(), b->sample()); |
| 25 ASSERT_EQ(a->min(), b->min()); |
| 26 ASSERT_EQ(a->max(), b->max()); |
| 27 ASSERT_EQ(a->nbucket(), b->nbucket()); |
| 28 } |
| 29 |
| 30 void AreEqCrash(CrashSample* a, CrashSample* b) { |
| 31 ASSERT_EQ(a->name(), b->name()); |
| 32 } |
| 33 |
| 34 void AreEqLinear(LinearHistogramSample* a, LinearHistogramSample* b) { |
| 35 ASSERT_EQ(a->name(), b->name()); |
| 36 ASSERT_EQ(a->sample(), b->sample()); |
| 37 ASSERT_EQ(a->max(), b->max()); |
| 38 } |
| 39 |
| 40 void AreEqSparse(SparseHistogramSample* a, SparseHistogramSample* b) { |
| 41 ASSERT_EQ(a->name(), b->name()); |
| 42 ASSERT_EQ(a->sample(), b->sample()); |
| 43 } |
| 44 |
| 45 void AreEqAction(UserActionSample* a, UserActionSample* b) { |
| 46 ASSERT_EQ(a->name(), b->name()); |
| 47 } |
| 48 |
| 49 void AreEq(MetricSample* a, MetricSample* b) { |
| 50 ASSERT_TRUE(a); |
| 51 ASSERT_TRUE(b); |
| 52 MetricSample::SampleType type = a->type(); |
| 53 ASSERT_EQ(a->type(), b->type()); |
| 54 if (type == MetricSample::CRASH) { |
| 55 AreEqCrash(reinterpret_cast<CrashSample*>(a), |
| 56 reinterpret_cast<CrashSample*>(b)); |
| 57 } else if (type == MetricSample::HISTOGRAM) { |
| 58 AreEqHistogram(reinterpret_cast<HistogramSample*>(a), |
| 59 reinterpret_cast<HistogramSample*>(b)); |
| 60 } else if (type == MetricSample::SPARSE_HISTOGRAM) { |
| 61 AreEqSparse(reinterpret_cast<SparseHistogramSample*>(a), |
| 62 reinterpret_cast<SparseHistogramSample*>(b)); |
| 63 } else if (type == MetricSample::LINEAR_HISTOGRAM) { |
| 64 AreEqLinear(reinterpret_cast<LinearHistogramSample*>(a), |
| 65 reinterpret_cast<LinearHistogramSample*>(b)); |
| 66 } else if (type == MetricSample::USER_ACTION) { |
| 67 AreEqAction(reinterpret_cast<UserActionSample*>(a), |
| 68 reinterpret_cast<UserActionSample*>(b)); |
| 69 } else { |
| 70 DLOG(ERROR) << "could not recognize type"; |
| 71 FAIL(); |
| 72 } |
| 73 } |
| 74 |
| 75 class ChromeOSMetricTest : public testing::Test { |
| 76 protected: |
| 77 static const int buff_size = 1024; |
| 78 char* buffer; |
| 79 std::string filename; |
| 80 FilePath filepath; |
| 81 ChromeOSMetricTest() { |
| 82 filename = "/tmp/chromeossampletest"; |
| 83 filepath = FilePath(filename); |
| 84 } |
| 85 virtual void SetUp() { |
| 86 DeleteFile(filepath, false); |
| 87 buffer = new char[buff_size]; |
| 88 } |
| 89 |
| 90 void TestSerialization(MetricSample* sample) { |
| 91 string serialized(sample->toString()); |
| 92 ASSERT_EQ('\0', serialized[serialized.length() - 1]); |
| 93 AreEq(sample, ChromeOSMetrics::ReadSample(serialized)); |
| 94 } |
| 95 }; |
| 96 |
| 97 TEST_F(ChromeOSMetricTest, CrashSerializeTest) { |
| 98 CrashSample crash("test"); |
| 99 TestSerialization(&crash); |
| 100 } |
| 101 |
| 102 TEST_F(ChromeOSMetricTest, HistogramSerializeTest) { |
| 103 HistogramSample hist("myhist", 13, 1, 100, 10); |
| 104 TestSerialization(&hist); |
| 105 } |
| 106 |
| 107 TEST_F(ChromeOSMetricTest, LinearSerializeTest) { |
| 108 LinearHistogramSample lhist("linearhist", 12, 30); |
| 109 TestSerialization(&lhist); |
| 110 } |
| 111 |
| 112 TEST_F(ChromeOSMetricTest, SparseSerializeTest) { |
| 113 SparseHistogramSample shist("mysparse", 30); |
| 114 TestSerialization(&shist); |
| 115 } |
| 116 |
| 117 TEST_F(ChromeOSMetricTest, UserActionSerializeTest) { |
| 118 TestSerialization(new UserActionSample("myaction")); |
| 119 } |
| 120 |
| 121 TEST_F(ChromeOSMetricTest, IllegalNameAreFilteredTest) { |
| 122 SparseHistogramSample sample1("no space", 10); |
| 123 LinearHistogramSample sample2(StringPrintf("here%cbhe", '\0'), 1, 3); |
| 124 EXPECT_FALSE(ChromeOSMetrics::WriteMetricToFile(sample1, filename)); |
| 125 EXPECT_FALSE(ChromeOSMetrics::WriteMetricToFile(sample2, filename)); |
| 126 int64 size = GetFileSize(filepath, &size); |
| 127 |
| 128 EXPECT_EQ(0, size); |
| 129 } |
| 130 |
| 131 TEST_F(ChromeOSMetricTest, BadInputIsCaughtTest) { |
| 132 string input(StringPrintf("sparsehistogram%cname foo%c", '\0', '\0')); |
| 133 EXPECT_EQ(NULL, SparseHistogramSample::ReadSparseHistogram(input)); |
| 134 } |
| 135 |
| 136 TEST_F(ChromeOSMetricTest, MessageSeparatedByZero) { |
| 137 CrashSample crash("crash"); |
| 138 |
| 139 ChromeOSMetrics::WriteMetricToFile(crash, filename); |
| 140 int64 size; |
| 141 GetFileSize(filepath, &size); |
| 142 ASSERT_EQ(size, 16); |
| 143 } |
| 144 |
| 145 TEST_F(ChromeOSMetricTest, WriteReadTest) { |
| 146 HistogramSample hist("myhist", 1, 2, 3, 4); |
| 147 CrashSample crash("crash"); |
| 148 LinearHistogramSample lhist("linear", 1, 10); |
| 149 SparseHistogramSample shist("mysparse", 30); |
| 150 UserActionSample action("myaction"); |
| 151 |
| 152 ChromeOSMetrics::WriteMetricToFile(hist, filename); |
| 153 ChromeOSMetrics::WriteMetricToFile(crash, filename); |
| 154 ChromeOSMetrics::WriteMetricToFile(lhist, filename); |
| 155 ChromeOSMetrics::WriteMetricToFile(shist, filename); |
| 156 ChromeOSMetrics::WriteMetricToFile(action, filename); |
| 157 ScopedVector<MetricSample> vect; |
| 158 ChromeOSMetrics::ReadAndTruncateMetricsFromFile(filename, &vect); |
| 159 ASSERT_EQ(vect.size(), size_t(5)); |
| 160 AreEq(&hist, vect[0]); |
| 161 AreEq(&crash, vect[1]); |
| 162 AreEq(&lhist, vect[2]); |
| 163 AreEq(&shist, vect[3]); |
| 164 AreEq(&action, vect[4]); |
| 165 |
| 166 int64 size; |
| 167 GetFileSize(filepath, &size); |
| 168 ASSERT_EQ(0, size); |
| 169 } |
| 170 } // namespace |
| 171 } // namespace base |
OLD | NEW |