Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 5 | #include <cstring> |
| 6 | |
Ben Chan | 51bf92a | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 7 | #include <base/files/file_util.h> |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 8 | #include <gmock/gmock.h> |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 10 | #include <policy/mock_device_policy.h> |
| 11 | #include <policy/libpolicy.h> |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 12 | |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 13 | #include "metrics/c_metrics_library.h" |
| 14 | #include "metrics/metrics_library.h" |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 15 | |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 16 | using base::FilePath; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 17 | using ::testing::_; |
| 18 | using ::testing::Return; |
| 19 | using ::testing::AnyNumber; |
| 20 | |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 21 | static const FilePath kTestUMAEventsFile("test-uma-events"); |
| 22 | static const char kTestMounts[] = "test-mounts"; |
| 23 | |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 24 | ACTION_P(SetMetricsPolicy, enabled) { |
| 25 | *arg0 = enabled; |
| 26 | return true; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 29 | class MetricsLibraryTest : public testing::Test { |
| 30 | protected: |
| 31 | virtual void SetUp() { |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 32 | EXPECT_TRUE(lib_.uma_events_file_.empty()); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 33 | lib_.Init(); |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 34 | EXPECT_FALSE(lib_.uma_events_file_.empty()); |
| 35 | lib_.uma_events_file_ = kTestUMAEventsFile.value(); |
Luigi Semenzato | a5db220 | 2014-05-23 15:06:24 -0700 | [diff] [blame] | 36 | EXPECT_EQ(0, WriteFile(kTestUMAEventsFile, "", 0)); |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 37 | device_policy_ = new policy::MockDevicePolicy(); |
| 38 | EXPECT_CALL(*device_policy_, LoadPolicy()) |
| 39 | .Times(AnyNumber()) |
| 40 | .WillRepeatedly(Return(true)); |
| 41 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 42 | .Times(AnyNumber()) |
| 43 | .WillRepeatedly(SetMetricsPolicy(true)); |
| 44 | provider_ = new policy::PolicyProvider(device_policy_); |
| 45 | lib_.SetPolicyProvider(provider_); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 46 | // Defeat metrics enabled caching between tests. |
| 47 | lib_.cached_enabled_time_ = 0; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | virtual void TearDown() { |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 51 | base::DeleteFile(FilePath(kTestMounts), false); |
| 52 | base::DeleteFile(kTestUMAEventsFile, false); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 55 | void VerifyEnabledCacheHit(bool to_value); |
| 56 | void VerifyEnabledCacheEviction(bool to_value); |
| 57 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 58 | MetricsLibrary lib_; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 59 | policy::MockDevicePolicy* device_policy_; |
| 60 | policy::PolicyProvider* provider_; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 63 | TEST_F(MetricsLibraryTest, IsDeviceMounted) { |
| 64 | static const char kTestContents[] = |
| 65 | "0123456789abcde 0123456789abcde\nguestfs foo bar\n"; |
| 66 | char buffer[1024]; |
| 67 | int block_sizes[] = { 1, 2, 3, 4, 5, 6, 8, 12, 14, 16, 32, 1024 }; |
| 68 | bool result; |
| 69 | EXPECT_FALSE(lib_.IsDeviceMounted("guestfs", |
| 70 | "nonexistent", |
| 71 | buffer, |
| 72 | 1, |
| 73 | &result)); |
Ben Chan | 8c3cd41 | 2014-05-19 20:30:51 -0700 | [diff] [blame] | 74 | ASSERT_TRUE(base::WriteFile(base::FilePath(kTestMounts), |
| 75 | kTestContents, |
| 76 | strlen(kTestContents))); |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 77 | EXPECT_FALSE(lib_.IsDeviceMounted("guestfs", |
| 78 | kTestMounts, |
| 79 | buffer, |
| 80 | 0, |
| 81 | &result)); |
| 82 | for (size_t i = 0; i < arraysize(block_sizes); ++i) { |
| 83 | EXPECT_TRUE(lib_.IsDeviceMounted("0123456789abcde", |
| 84 | kTestMounts, |
| 85 | buffer, |
| 86 | block_sizes[i], |
| 87 | &result)); |
| 88 | EXPECT_TRUE(result); |
| 89 | EXPECT_TRUE(lib_.IsDeviceMounted("guestfs", |
| 90 | kTestMounts, |
| 91 | buffer, |
| 92 | block_sizes[i], |
| 93 | &result)); |
| 94 | EXPECT_TRUE(result); |
| 95 | EXPECT_TRUE(lib_.IsDeviceMounted("0123456", |
| 96 | kTestMounts, |
| 97 | buffer, |
| 98 | block_sizes[i], |
| 99 | &result)); |
| 100 | EXPECT_FALSE(result); |
| 101 | EXPECT_TRUE(lib_.IsDeviceMounted("9abcde", |
| 102 | kTestMounts, |
| 103 | buffer, |
| 104 | block_sizes[i], |
| 105 | &result)); |
| 106 | EXPECT_FALSE(result); |
| 107 | EXPECT_TRUE(lib_.IsDeviceMounted("foo", |
| 108 | kTestMounts, |
| 109 | buffer, |
| 110 | block_sizes[i], |
| 111 | &result)); |
| 112 | EXPECT_FALSE(result); |
| 113 | EXPECT_TRUE(lib_.IsDeviceMounted("bar", |
| 114 | kTestMounts, |
| 115 | buffer, |
| 116 | block_sizes[i], |
| 117 | &result)); |
| 118 | EXPECT_FALSE(result); |
| 119 | } |
| 120 | } |
| 121 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 122 | TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) { |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 123 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 124 | .WillOnce(SetMetricsPolicy(false)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 125 | EXPECT_FALSE(lib_.AreMetricsEnabled()); |
| 126 | } |
| 127 | |
| 128 | TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) { |
| 129 | EXPECT_TRUE(lib_.AreMetricsEnabled()); |
| 130 | } |
| 131 | |
| 132 | void MetricsLibraryTest::VerifyEnabledCacheHit(bool to_value) { |
| 133 | // We might step from one second to the next one time, but not 100 |
| 134 | // times in a row. |
| 135 | for (int i = 0; i < 100; ++i) { |
| 136 | lib_.cached_enabled_time_ = 0; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 137 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 138 | .WillOnce(SetMetricsPolicy(!to_value)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 139 | ASSERT_EQ(!to_value, lib_.AreMetricsEnabled()); |
Julian Pastarmov | 0e5debf | 2011-08-04 11:15:13 +0200 | [diff] [blame] | 140 | ON_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 141 | .WillByDefault(SetMetricsPolicy(to_value)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 142 | if (lib_.AreMetricsEnabled() == !to_value) |
| 143 | return; |
| 144 | } |
| 145 | ADD_FAILURE() << "Did not see evidence of caching"; |
| 146 | } |
| 147 | |
| 148 | void MetricsLibraryTest::VerifyEnabledCacheEviction(bool to_value) { |
| 149 | lib_.cached_enabled_time_ = 0; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 150 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 151 | .WillOnce(SetMetricsPolicy(!to_value)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 152 | ASSERT_EQ(!to_value, lib_.AreMetricsEnabled()); |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 153 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 154 | .WillOnce(SetMetricsPolicy(to_value)); |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 155 | ASSERT_LT(abs(static_cast<int>(time(nullptr) - lib_.cached_enabled_time_)), |
| 156 | 5); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 157 | // Sleep one second (or cheat to be faster). |
| 158 | --lib_.cached_enabled_time_; |
| 159 | ASSERT_EQ(to_value, lib_.AreMetricsEnabled()); |
| 160 | } |
| 161 | |
| 162 | TEST_F(MetricsLibraryTest, AreMetricsEnabledCaching) { |
| 163 | VerifyEnabledCacheHit(false); |
| 164 | VerifyEnabledCacheHit(true); |
| 165 | VerifyEnabledCacheEviction(false); |
| 166 | VerifyEnabledCacheEviction(true); |
| 167 | } |
| 168 | |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 169 | class CMetricsLibraryTest : public testing::Test { |
| 170 | protected: |
| 171 | virtual void SetUp() { |
| 172 | lib_ = CMetricsLibraryNew(); |
| 173 | MetricsLibrary& ml = *reinterpret_cast<MetricsLibrary*>(lib_); |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 174 | EXPECT_TRUE(ml.uma_events_file_.empty()); |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 175 | CMetricsLibraryInit(lib_); |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 176 | EXPECT_FALSE(ml.uma_events_file_.empty()); |
| 177 | ml.uma_events_file_ = kTestUMAEventsFile.value(); |
Luigi Semenzato | a5db220 | 2014-05-23 15:06:24 -0700 | [diff] [blame] | 178 | EXPECT_EQ(0, WriteFile(kTestUMAEventsFile, "", 0)); |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 179 | device_policy_ = new policy::MockDevicePolicy(); |
| 180 | EXPECT_CALL(*device_policy_, LoadPolicy()) |
| 181 | .Times(AnyNumber()) |
| 182 | .WillRepeatedly(Return(true)); |
| 183 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 184 | .Times(AnyNumber()) |
| 185 | .WillRepeatedly(SetMetricsPolicy(true)); |
| 186 | provider_ = new policy::PolicyProvider(device_policy_); |
| 187 | ml.SetPolicyProvider(provider_); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 188 | reinterpret_cast<MetricsLibrary*>(lib_)->cached_enabled_time_ = 0; |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | virtual void TearDown() { |
| 192 | CMetricsLibraryDelete(lib_); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 193 | base::DeleteFile(kTestUMAEventsFile, false); |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | CMetricsLibrary lib_; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 197 | policy::MockDevicePolicy* device_policy_; |
| 198 | policy::PolicyProvider* provider_; |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 201 | TEST_F(CMetricsLibraryTest, AreMetricsEnabledFalse) { |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 202 | EXPECT_CALL(*device_policy_, GetMetricsEnabled(_)) |
| 203 | .WillOnce(SetMetricsPolicy(false)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 204 | EXPECT_FALSE(CMetricsLibraryAreMetricsEnabled(lib_)); |
| 205 | } |
| 206 | |
| 207 | TEST_F(CMetricsLibraryTest, AreMetricsEnabledTrue) { |
| 208 | EXPECT_TRUE(CMetricsLibraryAreMetricsEnabled(lib_)); |
| 209 | } |
| 210 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 211 | int main(int argc, char** argv) { |
| 212 | testing::InitGoogleTest(&argc, argv); |
| 213 | return RUN_ALL_TESTS(); |
| 214 | } |