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 | |
| 7 | #include <base/file_util.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 10 | #include "c_metrics_library.h" |
| 11 | #include "metrics_library.h" |
| 12 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 13 | static const FilePath kTestUMAEventsFile("test-uma-events"); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 14 | static const char kTestConsent[] = "test-consent"; |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame^] | 15 | static const char kTestMounts[] = "test-mounts"; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 16 | |
| 17 | static void SetMetricsEnabled(bool enabled) { |
| 18 | if (enabled) |
| 19 | ASSERT_EQ(1, file_util::WriteFile(FilePath(kTestConsent) , "0", 1)); |
| 20 | else |
| 21 | file_util::Delete(FilePath(kTestConsent), false); |
| 22 | } |
| 23 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 24 | class MetricsLibraryTest : public testing::Test { |
| 25 | protected: |
| 26 | virtual void SetUp() { |
| 27 | EXPECT_EQ(NULL, lib_.uma_events_file_); |
| 28 | lib_.Init(); |
| 29 | EXPECT_TRUE(NULL != lib_.uma_events_file_); |
| 30 | lib_.uma_events_file_ = kTestUMAEventsFile.value().c_str(); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 31 | SetMetricsEnabled(true); |
| 32 | // Defeat metrics enabled caching between tests. |
| 33 | lib_.cached_enabled_time_ = 0; |
| 34 | lib_.consent_file_ = kTestConsent; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | virtual void TearDown() { |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame^] | 38 | file_util::Delete(FilePath(kTestConsent), false); |
| 39 | file_util::Delete(FilePath(kTestMounts), false); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 40 | file_util::Delete(kTestUMAEventsFile, false); |
| 41 | } |
| 42 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 43 | void VerifyEnabledCacheHit(bool to_value); |
| 44 | void VerifyEnabledCacheEviction(bool to_value); |
| 45 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 46 | MetricsLibrary lib_; |
| 47 | }; |
| 48 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame^] | 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 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 108 | TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) { |
| 109 | SetMetricsEnabled(false); |
| 110 | EXPECT_FALSE(lib_.AreMetricsEnabled()); |
| 111 | } |
| 112 | |
| 113 | TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) { |
| 114 | EXPECT_TRUE(lib_.AreMetricsEnabled()); |
| 115 | } |
| 116 | |
| 117 | void MetricsLibraryTest::VerifyEnabledCacheHit(bool to_value) { |
| 118 | // We might step from one second to the next one time, but not 100 |
| 119 | // times in a row. |
| 120 | for (int i = 0; i < 100; ++i) { |
| 121 | lib_.cached_enabled_time_ = 0; |
| 122 | SetMetricsEnabled(!to_value); |
| 123 | ASSERT_EQ(!to_value, lib_.AreMetricsEnabled()); |
| 124 | SetMetricsEnabled(to_value); |
| 125 | if (lib_.AreMetricsEnabled() == !to_value) |
| 126 | return; |
| 127 | } |
| 128 | ADD_FAILURE() << "Did not see evidence of caching"; |
| 129 | } |
| 130 | |
| 131 | void MetricsLibraryTest::VerifyEnabledCacheEviction(bool to_value) { |
| 132 | lib_.cached_enabled_time_ = 0; |
| 133 | SetMetricsEnabled(!to_value); |
| 134 | ASSERT_EQ(!to_value, lib_.AreMetricsEnabled()); |
| 135 | SetMetricsEnabled(to_value); |
| 136 | ASSERT_LT(abs(time(NULL) - lib_.cached_enabled_time_), 5); |
| 137 | // Sleep one second (or cheat to be faster). |
| 138 | --lib_.cached_enabled_time_; |
| 139 | ASSERT_EQ(to_value, lib_.AreMetricsEnabled()); |
| 140 | } |
| 141 | |
| 142 | TEST_F(MetricsLibraryTest, AreMetricsEnabledCaching) { |
| 143 | VerifyEnabledCacheHit(false); |
| 144 | VerifyEnabledCacheHit(true); |
| 145 | VerifyEnabledCacheEviction(false); |
| 146 | VerifyEnabledCacheEviction(true); |
| 147 | } |
| 148 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 149 | TEST_F(MetricsLibraryTest, FormatChromeMessage) { |
| 150 | char buf[7]; |
| 151 | const int kLen = 6; |
| 152 | EXPECT_EQ(kLen, lib_.FormatChromeMessage(7, buf, "%d", 1)); |
| 153 | |
| 154 | char exp[kLen]; |
| 155 | sprintf(exp, "%c%c%c%c1", kLen, 0, 0, 0); |
| 156 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 157 | } |
| 158 | |
| 159 | TEST_F(MetricsLibraryTest, FormatChromeMessageTooLong) { |
| 160 | char buf[7]; |
| 161 | EXPECT_EQ(-1, lib_.FormatChromeMessage(7, buf, "test")); |
| 162 | } |
| 163 | |
| 164 | TEST_F(MetricsLibraryTest, SendEnumToUMA) { |
| 165 | char buf[100]; |
| 166 | const int kLen = 40; |
| 167 | EXPECT_TRUE(lib_.SendEnumToUMA("Test.EnumMetric", 1, 3)); |
| 168 | EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
| 169 | |
| 170 | char exp[kLen]; |
| 171 | sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3", |
| 172 | kLen, 0, 0, 0, 0); |
| 173 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 174 | } |
| 175 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 176 | TEST_F(MetricsLibraryTest, SendEnumToUMANotEnabled) { |
| 177 | SetMetricsEnabled(false); |
| 178 | EXPECT_TRUE(lib_.SendEnumToUMA("Test.EnumMetric", 1, 3)); |
| 179 | EXPECT_FALSE(file_util::PathExists(kTestUMAEventsFile)); |
| 180 | } |
| 181 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 182 | TEST_F(MetricsLibraryTest, SendMessageToChrome) { |
| 183 | EXPECT_TRUE(lib_.SendMessageToChrome(4, "test")); |
| 184 | EXPECT_TRUE(lib_.SendMessageToChrome(7, "content")); |
| 185 | std::string uma_events; |
| 186 | EXPECT_TRUE(file_util::ReadFileToString(kTestUMAEventsFile, &uma_events)); |
| 187 | EXPECT_EQ("testcontent", uma_events); |
| 188 | } |
| 189 | |
| 190 | TEST_F(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation) { |
| 191 | // Checks that the library doesn't die badly if the file can't be |
| 192 | // created. |
| 193 | static const char kDoesNotExistFile[] = "/does/not/exist"; |
| 194 | lib_.uma_events_file_ = kDoesNotExistFile; |
| 195 | static const char kDummyMessage[] = "Dummy Message"; |
| 196 | EXPECT_FALSE(lib_.SendMessageToChrome(strlen(kDummyMessage), kDummyMessage)); |
| 197 | file_util::Delete(FilePath(kDoesNotExistFile), false); |
| 198 | } |
| 199 | |
| 200 | TEST_F(MetricsLibraryTest, SendToUMA) { |
| 201 | char buf[100]; |
| 202 | const int kLen = 37; |
| 203 | EXPECT_TRUE(lib_.SendToUMA("Test.Metric", 2, 1, 100, 50)); |
| 204 | EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
| 205 | |
| 206 | char exp[kLen]; |
| 207 | sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0); |
| 208 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 209 | } |
| 210 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 211 | TEST_F(MetricsLibraryTest, SendToUMANotEnabled) { |
| 212 | SetMetricsEnabled(false); |
| 213 | EXPECT_TRUE(lib_.SendToUMA("Test.Metric", 2, 1, 100, 50)); |
| 214 | EXPECT_FALSE(file_util::PathExists(kTestUMAEventsFile)); |
| 215 | } |
| 216 | |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 217 | class CMetricsLibraryTest : public testing::Test { |
| 218 | protected: |
| 219 | virtual void SetUp() { |
| 220 | lib_ = CMetricsLibraryNew(); |
| 221 | MetricsLibrary& ml = *reinterpret_cast<MetricsLibrary*>(lib_); |
| 222 | EXPECT_EQ(NULL, ml.uma_events_file_); |
| 223 | CMetricsLibraryInit(lib_); |
| 224 | EXPECT_TRUE(NULL != ml.uma_events_file_); |
| 225 | ml.uma_events_file_ = kTestUMAEventsFile.value().c_str(); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 226 | SetMetricsEnabled(true); |
| 227 | reinterpret_cast<MetricsLibrary*>(lib_)->cached_enabled_time_ = 0; |
| 228 | reinterpret_cast<MetricsLibrary*>(lib_)->consent_file_ = kTestConsent; |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | virtual void TearDown() { |
| 232 | CMetricsLibraryDelete(lib_); |
| 233 | file_util::Delete(kTestUMAEventsFile, false); |
| 234 | } |
| 235 | |
| 236 | CMetricsLibrary lib_; |
| 237 | }; |
| 238 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 239 | TEST_F(CMetricsLibraryTest, AreMetricsEnabledFalse) { |
| 240 | SetMetricsEnabled(false); |
| 241 | EXPECT_FALSE(CMetricsLibraryAreMetricsEnabled(lib_)); |
| 242 | } |
| 243 | |
| 244 | TEST_F(CMetricsLibraryTest, AreMetricsEnabledTrue) { |
| 245 | EXPECT_TRUE(CMetricsLibraryAreMetricsEnabled(lib_)); |
| 246 | } |
| 247 | |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 248 | TEST_F(CMetricsLibraryTest, SendEnumToUMA) { |
| 249 | char buf[100]; |
| 250 | const int kLen = 40; |
| 251 | EXPECT_TRUE(CMetricsLibrarySendEnumToUMA(lib_, "Test.EnumMetric", 1, 3)); |
| 252 | EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
| 253 | |
| 254 | char exp[kLen]; |
| 255 | sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3", |
| 256 | kLen, 0, 0, 0, 0); |
| 257 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 258 | } |
| 259 | |
| 260 | TEST_F(CMetricsLibraryTest, SendToUMA) { |
| 261 | char buf[100]; |
| 262 | const int kLen = 37; |
| 263 | EXPECT_TRUE(CMetricsLibrarySendToUMA(lib_, "Test.Metric", 2, 1, 100, 50)); |
| 264 | EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100)); |
| 265 | |
| 266 | char exp[kLen]; |
| 267 | sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0); |
| 268 | EXPECT_EQ(0, memcmp(exp, buf, kLen)); |
| 269 | } |
| 270 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 271 | int main(int argc, char** argv) { |
| 272 | testing::InitGoogleTest(&argc, argv); |
| 273 | return RUN_ALL_TESTS(); |
| 274 | } |