Index: components/metrics/export/metrics_utils_unittest.cc |
diff --git a/components/metrics/export/metrics_utils_unittest.cc b/components/metrics/export/metrics_utils_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2fd203bf87158d2c0f21bb319f47951ebf8a1e25 |
--- /dev/null |
+++ b/components/metrics/export/metrics_utils_unittest.cc |
@@ -0,0 +1,215 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/metrics/export/metrics_utils.h" |
+ |
+#include "base/file_util.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/logging.h" |
+#include "base/strings/stringprintf.h" |
+#include "components/metrics/export/crash_sample.h" |
+#include "components/metrics/export/histogram_sample.h" |
+#include "components/metrics/export/linearhistogram_sample.h" |
+#include "components/metrics/export/metric_sample.h" |
+#include "components/metrics/export/sparsehistogram_sample.h" |
+#include "components/metrics/export/useraction_sample.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace metrics { |
+namespace { |
+ |
+void AreEqHistogram(HistogramSample* a, HistogramSample* b) { |
+ EXPECT_EQ(a->name(), b->name()); |
+ EXPECT_EQ(a->sample(), b->sample()); |
+ EXPECT_EQ(a->min(), b->min()); |
+ EXPECT_EQ(a->max(), b->max()); |
+ EXPECT_EQ(a->nbucket(), b->nbucket()); |
+} |
+ |
+void AreEqCrash(CrashSample* a, CrashSample* b) { |
+ EXPECT_EQ(a->name(), b->name()); |
+} |
+ |
+void AreEqLinear(LinearHistogramSample* a, LinearHistogramSample* b) { |
+ EXPECT_EQ(a->name(), b->name()); |
+ EXPECT_EQ(a->sample(), b->sample()); |
+ EXPECT_EQ(a->max(), b->max()); |
+} |
+ |
+void AreEqSparse(SparseHistogramSample* a, SparseHistogramSample* b) { |
+ EXPECT_EQ(a->name(), b->name()); |
+ EXPECT_EQ(a->sample(), b->sample()); |
+} |
+ |
+void AreEqAction(UserActionSample* a, UserActionSample* b) { |
+ EXPECT_EQ(a->name(), b->name()); |
+} |
+ |
+void AreEq(MetricSample* a, MetricSample* b) { |
+ ASSERT_TRUE(a); |
+ ASSERT_TRUE(b); |
+ MetricSample::SampleType type = a->type(); |
+ ASSERT_EQ(a->type(), b->type()); |
+ if (type == MetricSample::CRASH) { |
+ AreEqCrash(reinterpret_cast<CrashSample*>(a), |
+ reinterpret_cast<CrashSample*>(b)); |
+ } else if (type == MetricSample::HISTOGRAM) { |
+ AreEqHistogram(reinterpret_cast<HistogramSample*>(a), |
+ reinterpret_cast<HistogramSample*>(b)); |
+ } else if (type == MetricSample::SPARSE_HISTOGRAM) { |
+ AreEqSparse(reinterpret_cast<SparseHistogramSample*>(a), |
+ reinterpret_cast<SparseHistogramSample*>(b)); |
+ } else if (type == MetricSample::LINEAR_HISTOGRAM) { |
+ AreEqLinear(reinterpret_cast<LinearHistogramSample*>(a), |
+ reinterpret_cast<LinearHistogramSample*>(b)); |
+ } else if (type == MetricSample::USER_ACTION) { |
+ AreEqAction(reinterpret_cast<UserActionSample*>(a), |
+ reinterpret_cast<UserActionSample*>(b)); |
+ } else { |
+ DLOG(ERROR) << "could not recognize type"; |
+ FAIL(); |
+ } |
+} |
+ |
+class MetricsUtilsChromeOSTest : public testing::Test { |
+ protected: |
+ static const int buff_size = 1024; |
+ |
+ MetricsUtilsChromeOSTest() { |
+ bool success = temporary_dir.CreateUniqueTempDir(); |
+ if (success) { |
+ base::FilePath dir_path = temporary_dir.path(); |
+ filename = dir_path.value() + "chromeossampletest"; |
+ filepath = base::FilePath(filename); |
+ } |
+ } |
Alexei Svitkine (slow)
2014/05/01 15:57:09
Add empty line after this.
|
+ virtual void SetUp() { |
+ base::DeleteFile(filepath, false); |
Alexei Svitkine (slow)
2014/05/01 15:57:09
What's this call for?
bsimonnet
2014/05/01 20:34:21
To make sure the file is not here and therefore no
|
+ buffer.reset(new char[buff_size]); |
+ } |
+ |
+ void TestSerialization(MetricSample* sample) { |
+ std::string serialized(sample->ToString()); |
+ ASSERT_EQ('\0', serialized[serialized.length() - 1]); |
+ AreEq(sample, MetricsUtils::ReadSample(serialized).get()); |
+ } |
+ |
+ scoped_ptr<char[]> buffer; |
Alexei Svitkine (slow)
2014/05/01 15:57:09
Is this used somewhere?
bsimonnet
2014/05/01 20:34:21
no I forgot to delete it.
|
+ std::string filename; |
+ base::ScopedTempDir temporary_dir; |
+ base::FilePath filepath; |
+}; |
+ |
+TEST_F(MetricsUtilsChromeOSTest, CrashSerializeTest) { |
+ CrashSample crash("test"); |
+ TestSerialization(&crash); |
+} |
+ |
+TEST_F(MetricsUtilsChromeOSTest, HistogramSerializeTest) { |
+ HistogramSample hist("myhist", 13, 1, 100, 10); |
+ TestSerialization(&hist); |
+} |
+ |
+TEST_F(MetricsUtilsChromeOSTest, LinearSerializeTest) { |
+ LinearHistogramSample lhist("linearhist", 12, 30); |
+ TestSerialization(&lhist); |
+} |
+ |
+TEST_F(MetricsUtilsChromeOSTest, SparseSerializeTest) { |
+ SparseHistogramSample shist("mysparse", 30); |
+ TestSerialization(&shist); |
+} |
+ |
+TEST_F(MetricsUtilsChromeOSTest, UserActionSerializeTest) { |
+ TestSerialization(new UserActionSample("myaction")); |
+} |
+ |
+TEST_F(MetricsUtilsChromeOSTest, IllegalNameAreFilteredTest) { |
+ SparseHistogramSample sample1("no space", 10); |
+ LinearHistogramSample sample2(base::StringPrintf("here%cbhe", '\0'), 1, 3); |
+ EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample1, filename)); |
+ EXPECT_FALSE(MetricsUtils::WriteMetricToFile(sample2, filename)); |
+ int64 size = 0; |
+ |
+ ASSERT_TRUE(!PathExists(filepath) || base::GetFileSize(filepath, &size)); |
+ |
+ EXPECT_EQ(0, size); |
+} |
+ |
+TEST_F(MetricsUtilsChromeOSTest, BadInputIsCaughtTest) { |
+ std::string input( |
+ base::StringPrintf("sparsehistogram%cname foo%c", '\0', '\0')); |
+ EXPECT_EQ(NULL, SparseHistogramSample::ReadSparseHistogram(input).get()); |
+} |
+ |
+TEST_F(MetricsUtilsChromeOSTest, MessageSeparatedByZero) { |
+ CrashSample crash("crash"); |
+ |
+ MetricsUtils::WriteMetricToFile(crash, filename); |
+ int64 size = 0; |
+ ASSERT_TRUE(base::GetFileSize(filepath, &size)); |
+ EXPECT_EQ(size, 16); |
+} |
+ |
+TEST_F(MetricsUtilsChromeOSTest, MessagesTooLongAreDiscardedTest) { |
+ // Creates a message that is bigger than the maximum allowed size. |
+ // As we are adding extra character (crash, \0s, etc), if the name is |
+ // kMessageMaxLength long, it will be too long. |
+ std::string name(MetricsUtils::kMessageMaxLength, 'c'); |
+ |
+ CrashSample crash(name); |
+ EXPECT_FALSE(MetricsUtils::WriteMetricToFile(crash, filename)); |
+ int64 size = 0; |
+ ASSERT_TRUE(base::GetFileSize(filepath, &size)); |
+ EXPECT_EQ(0, size); |
+} |
+ |
+TEST_F(MetricsUtilsChromeOSTest, ReadLongMessageTest) { |
+ base::File test_file(filepath, |
+ base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); |
+ std::string message(MetricsUtils::kMessageMaxLength + 1, 'c'); |
+ |
+ int32_t message_size = message.length() + sizeof(int32_t); |
+ test_file.WriteAtCurrentPos(reinterpret_cast<const char*>(&message_size), |
+ sizeof(message_size)); |
+ test_file.WriteAtCurrentPos(message.c_str(), message.length()); |
+ test_file.Close(); |
+ |
+ CrashSample crash("test"); |
+ MetricsUtils::WriteMetricToFile(crash, filename); |
+ |
+ ScopedVector<MetricSample> samples; |
+ MetricsUtils::ReadAndTruncateMetricsFromFile(filename, &samples); |
+ ASSERT_EQ(size_t(1), samples.size()); |
+ AreEq(&crash, samples[0]); |
+} |
+ |
+TEST_F(MetricsUtilsChromeOSTest, WriteReadTest) { |
+ HistogramSample hist("myhist", 1, 2, 3, 4); |
+ CrashSample crash("crash"); |
+ LinearHistogramSample lhist("linear", 1, 10); |
+ SparseHistogramSample shist("mysparse", 30); |
+ UserActionSample action("myaction"); |
+ |
+ MetricsUtils::WriteMetricToFile(hist, filename); |
+ MetricsUtils::WriteMetricToFile(crash, filename); |
+ MetricsUtils::WriteMetricToFile(lhist, filename); |
+ MetricsUtils::WriteMetricToFile(shist, filename); |
+ MetricsUtils::WriteMetricToFile(action, filename); |
+ ScopedVector<MetricSample> vect; |
+ MetricsUtils::ReadAndTruncateMetricsFromFile(filename, &vect); |
+ ASSERT_EQ(vect.size(), size_t(5)); |
+ AreEq(&hist, vect[0]); |
+ AreEq(&crash, vect[1]); |
+ AreEq(&lhist, vect[2]); |
+ AreEq(&shist, vect[3]); |
+ AreEq(&action, vect[4]); |
+ |
+ int64 size = 0; |
+ ASSERT_TRUE(base::GetFileSize(filepath, &size)); |
+ ASSERT_EQ(0, size); |
+} |
+ |
+} // namespace |
+} // namespace metrics |