| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS 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 <cstring> | 5 #include <cstring> |
| 6 | 6 |
| 7 #include <base/file_util.h> | 7 #include <base/file_util.h> |
| 8 #include <gtest/gtest.h> | 8 #include <gtest/gtest.h> |
| 9 | 9 |
| 10 #include "c_metrics_library.h" | 10 #include "c_metrics_library.h" |
| 11 #include "metrics_library.h" | 11 #include "metrics_library.h" |
| 12 | 12 |
| 13 static const FilePath kTestUMAEventsFile("test-uma-events"); | 13 static const FilePath kTestUMAEventsFile("test-uma-events"); |
| 14 | |
| 15 static const char kTestConsent[] = "test-consent"; | 14 static const char kTestConsent[] = "test-consent"; |
| 15 static const char kTestMounts[] = "test-mounts"; |
| 16 | 16 |
| 17 static void SetMetricsEnabled(bool enabled) { | 17 static void SetMetricsEnabled(bool enabled) { |
| 18 if (enabled) | 18 if (enabled) |
| 19 ASSERT_EQ(1, file_util::WriteFile(FilePath(kTestConsent) , "0", 1)); | 19 ASSERT_EQ(1, file_util::WriteFile(FilePath(kTestConsent) , "0", 1)); |
| 20 else | 20 else |
| 21 file_util::Delete(FilePath(kTestConsent), false); | 21 file_util::Delete(FilePath(kTestConsent), false); |
| 22 } | 22 } |
| 23 | 23 |
| 24 class MetricsLibraryTest : public testing::Test { | 24 class MetricsLibraryTest : public testing::Test { |
| 25 protected: | 25 protected: |
| 26 virtual void SetUp() { | 26 virtual void SetUp() { |
| 27 EXPECT_EQ(NULL, lib_.uma_events_file_); | 27 EXPECT_EQ(NULL, lib_.uma_events_file_); |
| 28 lib_.Init(); | 28 lib_.Init(); |
| 29 EXPECT_TRUE(NULL != lib_.uma_events_file_); | 29 EXPECT_TRUE(NULL != lib_.uma_events_file_); |
| 30 lib_.uma_events_file_ = kTestUMAEventsFile.value().c_str(); | 30 lib_.uma_events_file_ = kTestUMAEventsFile.value().c_str(); |
| 31 SetMetricsEnabled(true); | 31 SetMetricsEnabled(true); |
| 32 // Defeat metrics enabled caching between tests. | 32 // Defeat metrics enabled caching between tests. |
| 33 lib_.cached_enabled_time_ = 0; | 33 lib_.cached_enabled_time_ = 0; |
| 34 lib_.consent_file_ = kTestConsent; | 34 lib_.consent_file_ = kTestConsent; |
| 35 } | 35 } |
| 36 | 36 |
| 37 virtual void TearDown() { | 37 virtual void TearDown() { |
| 38 file_util::Delete(FilePath(kTestConsent), false); |
| 39 file_util::Delete(FilePath(kTestMounts), false); |
| 38 file_util::Delete(kTestUMAEventsFile, false); | 40 file_util::Delete(kTestUMAEventsFile, false); |
| 39 } | 41 } |
| 40 | 42 |
| 41 void VerifyEnabledCacheHit(bool to_value); | 43 void VerifyEnabledCacheHit(bool to_value); |
| 42 void VerifyEnabledCacheEviction(bool to_value); | 44 void VerifyEnabledCacheEviction(bool to_value); |
| 43 | 45 |
| 44 MetricsLibrary lib_; | 46 MetricsLibrary lib_; |
| 45 }; | 47 }; |
| 46 | 48 |
| 49 TEST_F(MetricsLibraryTest, IsDeviceMounted) { |
| 50 static const char kTestContents[] = |
| 51 "0123456789abcde 0123456789abcde\nguestfs foo bar\n"; |
| 52 char buffer[1024]; |
| 53 int block_sizes[] = { 1, 2, 3, 4, 5, 6, 8, 12, 14, 16, 32, 1024 }; |
| 54 bool result; |
| 55 EXPECT_FALSE(lib_.IsDeviceMounted("guestfs", |
| 56 "nonexistent", |
| 57 buffer, |
| 58 1, |
| 59 &result)); |
| 60 ASSERT_TRUE(file_util::WriteFile(FilePath(kTestMounts), |
| 61 kTestContents, |
| 62 strlen(kTestContents))); |
| 63 EXPECT_FALSE(lib_.IsDeviceMounted("guestfs", |
| 64 kTestMounts, |
| 65 buffer, |
| 66 0, |
| 67 &result)); |
| 68 for (size_t i = 0; i < arraysize(block_sizes); ++i) { |
| 69 EXPECT_TRUE(lib_.IsDeviceMounted("0123456789abcde", |
| 70 kTestMounts, |
| 71 buffer, |
| 72 block_sizes[i], |
| 73 &result)); |
| 74 EXPECT_TRUE(result); |
| 75 EXPECT_TRUE(lib_.IsDeviceMounted("guestfs", |
| 76 kTestMounts, |
| 77 buffer, |
| 78 block_sizes[i], |
| 79 &result)); |
| 80 EXPECT_TRUE(result); |
| 81 EXPECT_TRUE(lib_.IsDeviceMounted("0123456", |
| 82 kTestMounts, |
| 83 buffer, |
| 84 block_sizes[i], |
| 85 &result)); |
| 86 EXPECT_FALSE(result); |
| 87 EXPECT_TRUE(lib_.IsDeviceMounted("9abcde", |
| 88 kTestMounts, |
| 89 buffer, |
| 90 block_sizes[i], |
| 91 &result)); |
| 92 EXPECT_FALSE(result); |
| 93 EXPECT_TRUE(lib_.IsDeviceMounted("foo", |
| 94 kTestMounts, |
| 95 buffer, |
| 96 block_sizes[i], |
| 97 &result)); |
| 98 EXPECT_FALSE(result); |
| 99 EXPECT_TRUE(lib_.IsDeviceMounted("bar", |
| 100 kTestMounts, |
| 101 buffer, |
| 102 block_sizes[i], |
| 103 &result)); |
| 104 EXPECT_FALSE(result); |
| 105 } |
| 106 } |
| 107 |
| 47 TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) { | 108 TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) { |
| 48 SetMetricsEnabled(false); | 109 SetMetricsEnabled(false); |
| 49 EXPECT_FALSE(lib_.AreMetricsEnabled()); | 110 EXPECT_FALSE(lib_.AreMetricsEnabled()); |
| 50 } | 111 } |
| 51 | 112 |
| 52 TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) { | 113 TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) { |
| 53 EXPECT_TRUE(lib_.AreMetricsEnabled()); | 114 EXPECT_TRUE(lib_.AreMetricsEnabled()); |
| 54 } | 115 } |
| 55 | 116 |
| 56 void MetricsLibraryTest::VerifyEnabledCacheHit(bool to_value) { | 117 void MetricsLibraryTest::VerifyEnabledCacheHit(bool to_value) { |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 | 265 |
| 205 char exp[kLen]; | 266 char exp[kLen]; |
| 206 sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0); | 267 sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0); |
| 207 EXPECT_EQ(0, memcmp(exp, buf, kLen)); | 268 EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 208 } | 269 } |
| 209 | 270 |
| 210 int main(int argc, char** argv) { | 271 int main(int argc, char** argv) { |
| 211 testing::InitGoogleTest(&argc, argv); | 272 testing::InitGoogleTest(&argc, argv); |
| 212 return RUN_ALL_TESTS(); | 273 return RUN_ALL_TESTS(); |
| 213 } | 274 } |
| OLD | NEW |