blob: a748ed5c31be46018a41ae56dd8cd6774c69418f [file] [log] [blame]
Darin Petkov11b8eb32010-05-18 11:00:59 -07001// 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 Petkov11b8eb32010-05-18 11:00:59 -07005#include <cstring>
6
7#include <base/file_util.h>
Ken Mixterb2f17092011-07-22 14:59:51 -07008#include <gmock/gmock.h>
Darin Petkov11b8eb32010-05-18 11:00:59 -07009#include <gtest/gtest.h>
Ken Mixterb2f17092011-07-22 14:59:51 -070010#include <policy/mock_device_policy.h>
11#include <policy/libpolicy.h>
Darin Petkov11b8eb32010-05-18 11:00:59 -070012
Sam Leffler10b301d2010-06-17 14:22:43 -070013#include "c_metrics_library.h"
14#include "metrics_library.h"
15
Ben Chan2e6543d2014-02-05 23:26:25 -080016using base::FilePath;
Ken Mixterb2f17092011-07-22 14:59:51 -070017using ::testing::_;
18using ::testing::Return;
19using ::testing::AnyNumber;
20
Ben Chan2e6543d2014-02-05 23:26:25 -080021static const FilePath kTestUMAEventsFile("test-uma-events");
22static const char kTestMounts[] = "test-mounts";
23
Ken Mixterb2f17092011-07-22 14:59:51 -070024ACTION_P(SetMetricsPolicy, enabled) {
25 *arg0 = enabled;
26 return true;
Ken Mixter4c5daa42010-08-26 18:35:06 -070027}
28
Darin Petkov11b8eb32010-05-18 11:00:59 -070029class MetricsLibraryTest : public testing::Test {
30 protected:
31 virtual void SetUp() {
32 EXPECT_EQ(NULL, lib_.uma_events_file_);
33 lib_.Init();
34 EXPECT_TRUE(NULL != lib_.uma_events_file_);
35 lib_.uma_events_file_ = kTestUMAEventsFile.value().c_str();
Ken Mixterb2f17092011-07-22 14:59:51 -070036 device_policy_ = new policy::MockDevicePolicy();
37 EXPECT_CALL(*device_policy_, LoadPolicy())
38 .Times(AnyNumber())
39 .WillRepeatedly(Return(true));
40 EXPECT_CALL(*device_policy_, GetMetricsEnabled(_))
41 .Times(AnyNumber())
42 .WillRepeatedly(SetMetricsPolicy(true));
43 provider_ = new policy::PolicyProvider(device_policy_);
44 lib_.SetPolicyProvider(provider_);
Ken Mixter4c5daa42010-08-26 18:35:06 -070045 // Defeat metrics enabled caching between tests.
46 lib_.cached_enabled_time_ = 0;
Darin Petkov11b8eb32010-05-18 11:00:59 -070047 }
48
49 virtual void TearDown() {
Ben Chan2e6543d2014-02-05 23:26:25 -080050 base::DeleteFile(FilePath(kTestMounts), false);
51 base::DeleteFile(kTestUMAEventsFile, false);
Darin Petkov11b8eb32010-05-18 11:00:59 -070052 }
53
Ken Mixter4c5daa42010-08-26 18:35:06 -070054 void VerifyEnabledCacheHit(bool to_value);
55 void VerifyEnabledCacheEviction(bool to_value);
56
Darin Petkov11b8eb32010-05-18 11:00:59 -070057 MetricsLibrary lib_;
Ken Mixterb2f17092011-07-22 14:59:51 -070058 policy::MockDevicePolicy* device_policy_;
59 policy::PolicyProvider* provider_;
Darin Petkov11b8eb32010-05-18 11:00:59 -070060};
61
Ken Mixtereafbbdf2010-10-01 15:38:42 -070062TEST_F(MetricsLibraryTest, IsDeviceMounted) {
63 static const char kTestContents[] =
64 "0123456789abcde 0123456789abcde\nguestfs foo bar\n";
65 char buffer[1024];
66 int block_sizes[] = { 1, 2, 3, 4, 5, 6, 8, 12, 14, 16, 32, 1024 };
67 bool result;
68 EXPECT_FALSE(lib_.IsDeviceMounted("guestfs",
69 "nonexistent",
70 buffer,
71 1,
72 &result));
73 ASSERT_TRUE(file_util::WriteFile(FilePath(kTestMounts),
74 kTestContents,
75 strlen(kTestContents)));
76 EXPECT_FALSE(lib_.IsDeviceMounted("guestfs",
77 kTestMounts,
78 buffer,
79 0,
80 &result));
81 for (size_t i = 0; i < arraysize(block_sizes); ++i) {
82 EXPECT_TRUE(lib_.IsDeviceMounted("0123456789abcde",
83 kTestMounts,
84 buffer,
85 block_sizes[i],
86 &result));
87 EXPECT_TRUE(result);
88 EXPECT_TRUE(lib_.IsDeviceMounted("guestfs",
89 kTestMounts,
90 buffer,
91 block_sizes[i],
92 &result));
93 EXPECT_TRUE(result);
94 EXPECT_TRUE(lib_.IsDeviceMounted("0123456",
95 kTestMounts,
96 buffer,
97 block_sizes[i],
98 &result));
99 EXPECT_FALSE(result);
100 EXPECT_TRUE(lib_.IsDeviceMounted("9abcde",
101 kTestMounts,
102 buffer,
103 block_sizes[i],
104 &result));
105 EXPECT_FALSE(result);
106 EXPECT_TRUE(lib_.IsDeviceMounted("foo",
107 kTestMounts,
108 buffer,
109 block_sizes[i],
110 &result));
111 EXPECT_FALSE(result);
112 EXPECT_TRUE(lib_.IsDeviceMounted("bar",
113 kTestMounts,
114 buffer,
115 block_sizes[i],
116 &result));
117 EXPECT_FALSE(result);
118 }
119}
120
Ken Mixter4c5daa42010-08-26 18:35:06 -0700121TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) {
Ken Mixterb2f17092011-07-22 14:59:51 -0700122 EXPECT_CALL(*device_policy_, GetMetricsEnabled(_))
123 .WillOnce(SetMetricsPolicy(false));
Ken Mixter4c5daa42010-08-26 18:35:06 -0700124 EXPECT_FALSE(lib_.AreMetricsEnabled());
125}
126
127TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) {
128 EXPECT_TRUE(lib_.AreMetricsEnabled());
129}
130
131void MetricsLibraryTest::VerifyEnabledCacheHit(bool to_value) {
132 // We might step from one second to the next one time, but not 100
133 // times in a row.
134 for (int i = 0; i < 100; ++i) {
135 lib_.cached_enabled_time_ = 0;
Ken Mixterb2f17092011-07-22 14:59:51 -0700136 EXPECT_CALL(*device_policy_, GetMetricsEnabled(_))
137 .WillOnce(SetMetricsPolicy(!to_value));
Ken Mixter4c5daa42010-08-26 18:35:06 -0700138 ASSERT_EQ(!to_value, lib_.AreMetricsEnabled());
Julian Pastarmov0e5debf2011-08-04 11:15:13 +0200139 ON_CALL(*device_policy_, GetMetricsEnabled(_))
140 .WillByDefault(SetMetricsPolicy(to_value));
Ken Mixter4c5daa42010-08-26 18:35:06 -0700141 if (lib_.AreMetricsEnabled() == !to_value)
142 return;
143 }
144 ADD_FAILURE() << "Did not see evidence of caching";
145}
146
147void MetricsLibraryTest::VerifyEnabledCacheEviction(bool to_value) {
148 lib_.cached_enabled_time_ = 0;
Ken Mixterb2f17092011-07-22 14:59:51 -0700149 EXPECT_CALL(*device_policy_, GetMetricsEnabled(_))
150 .WillOnce(SetMetricsPolicy(!to_value));
Ken Mixter4c5daa42010-08-26 18:35:06 -0700151 ASSERT_EQ(!to_value, lib_.AreMetricsEnabled());
Ken Mixterb2f17092011-07-22 14:59:51 -0700152 EXPECT_CALL(*device_policy_, GetMetricsEnabled(_))
153 .WillOnce(SetMetricsPolicy(to_value));
Ken Mixter4c5daa42010-08-26 18:35:06 -0700154 ASSERT_LT(abs(time(NULL) - lib_.cached_enabled_time_), 5);
155 // Sleep one second (or cheat to be faster).
156 --lib_.cached_enabled_time_;
157 ASSERT_EQ(to_value, lib_.AreMetricsEnabled());
158}
159
160TEST_F(MetricsLibraryTest, AreMetricsEnabledCaching) {
161 VerifyEnabledCacheHit(false);
162 VerifyEnabledCacheHit(true);
163 VerifyEnabledCacheEviction(false);
164 VerifyEnabledCacheEviction(true);
165}
166
Darin Petkov11b8eb32010-05-18 11:00:59 -0700167TEST_F(MetricsLibraryTest, FormatChromeMessage) {
168 char buf[7];
169 const int kLen = 6;
170 EXPECT_EQ(kLen, lib_.FormatChromeMessage(7, buf, "%d", 1));
171
172 char exp[kLen];
173 sprintf(exp, "%c%c%c%c1", kLen, 0, 0, 0);
174 EXPECT_EQ(0, memcmp(exp, buf, kLen));
175}
176
177TEST_F(MetricsLibraryTest, FormatChromeMessageTooLong) {
178 char buf[7];
179 EXPECT_EQ(-1, lib_.FormatChromeMessage(7, buf, "test"));
180}
181
182TEST_F(MetricsLibraryTest, SendEnumToUMA) {
183 char buf[100];
184 const int kLen = 40;
185 EXPECT_TRUE(lib_.SendEnumToUMA("Test.EnumMetric", 1, 3));
Ben Chan2e6543d2014-02-05 23:26:25 -0800186 EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100));
Darin Petkov11b8eb32010-05-18 11:00:59 -0700187
188 char exp[kLen];
189 sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3",
190 kLen, 0, 0, 0, 0);
191 EXPECT_EQ(0, memcmp(exp, buf, kLen));
192}
193
194TEST_F(MetricsLibraryTest, SendMessageToChrome) {
195 EXPECT_TRUE(lib_.SendMessageToChrome(4, "test"));
196 EXPECT_TRUE(lib_.SendMessageToChrome(7, "content"));
197 std::string uma_events;
Ben Chan2e6543d2014-02-05 23:26:25 -0800198 EXPECT_TRUE(base::ReadFileToString(kTestUMAEventsFile, &uma_events));
Darin Petkov11b8eb32010-05-18 11:00:59 -0700199 EXPECT_EQ("testcontent", uma_events);
200}
201
202TEST_F(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation) {
203 // Checks that the library doesn't die badly if the file can't be
204 // created.
205 static const char kDoesNotExistFile[] = "/does/not/exist";
206 lib_.uma_events_file_ = kDoesNotExistFile;
207 static const char kDummyMessage[] = "Dummy Message";
208 EXPECT_FALSE(lib_.SendMessageToChrome(strlen(kDummyMessage), kDummyMessage));
Ben Chan2e6543d2014-02-05 23:26:25 -0800209 base::DeleteFile(FilePath(kDoesNotExistFile), false);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700210}
211
212TEST_F(MetricsLibraryTest, SendToUMA) {
213 char buf[100];
214 const int kLen = 37;
215 EXPECT_TRUE(lib_.SendToUMA("Test.Metric", 2, 1, 100, 50));
Ben Chan2e6543d2014-02-05 23:26:25 -0800216 EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100));
Darin Petkov11b8eb32010-05-18 11:00:59 -0700217
218 char exp[kLen];
219 sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0);
220 EXPECT_EQ(0, memcmp(exp, buf, kLen));
221}
222
Darin Petkoved824852011-01-06 10:51:47 -0800223TEST_F(MetricsLibraryTest, SendUserActionToUMA) {
224 char buf[100];
225 const int kLen = 30;
226 EXPECT_TRUE(lib_.SendUserActionToUMA("SomeKeyPressed"));
Ben Chan2e6543d2014-02-05 23:26:25 -0800227 EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100));
Darin Petkoved824852011-01-06 10:51:47 -0800228
229 char exp[kLen];
230 sprintf(exp, "%c%c%c%cuseraction%cSomeKeyPressed", kLen, 0, 0, 0, 0);
231 EXPECT_EQ(0, memcmp(exp, buf, kLen));
232}
233
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700234TEST_F(MetricsLibraryTest, SendSparseToUMA) {
235 char buf[100];
236 const int kLen = 4 + sizeof("sparsehistogram") + sizeof("Test.Sparse 1234");
237 EXPECT_TRUE(lib_.SendSparseToUMA("Test.Sparse", 1234));
Ben Chan2e6543d2014-02-05 23:26:25 -0800238 EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100));
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -0700239
240 char exp[kLen];
241 sprintf(exp, "%c%c%c%csparsehistogram%cTest.Sparse 1234", kLen, 0, 0, 0, 0);
242 EXPECT_EQ(0, memcmp(exp, buf, kLen));
243}
244
Luigi Semenzatodf3e4522011-10-14 15:31:32 -0700245TEST_F(MetricsLibraryTest, SendCrashToUMA) {
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800246 EXPECT_TRUE(lib_.SendCrashToUMA("kernel"));
247 char exp[100];
248 int len = sprintf(exp, "%c%c%c%ccrash%ckernel",
249 0, 0, 0, 0, 0) + 1;
250 exp[0] = len;
251 char buf[100];
Ben Chan2e6543d2014-02-05 23:26:25 -0800252 EXPECT_EQ(len, base::ReadFile(kTestUMAEventsFile, buf, 100));
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800253 EXPECT_EQ(0, memcmp(exp, buf, len));
254}
255
Sam Leffler10b301d2010-06-17 14:22:43 -0700256class CMetricsLibraryTest : public testing::Test {
257 protected:
258 virtual void SetUp() {
259 lib_ = CMetricsLibraryNew();
260 MetricsLibrary& ml = *reinterpret_cast<MetricsLibrary*>(lib_);
261 EXPECT_EQ(NULL, ml.uma_events_file_);
262 CMetricsLibraryInit(lib_);
263 EXPECT_TRUE(NULL != ml.uma_events_file_);
264 ml.uma_events_file_ = kTestUMAEventsFile.value().c_str();
Ken Mixterb2f17092011-07-22 14:59:51 -0700265 device_policy_ = new policy::MockDevicePolicy();
266 EXPECT_CALL(*device_policy_, LoadPolicy())
267 .Times(AnyNumber())
268 .WillRepeatedly(Return(true));
269 EXPECT_CALL(*device_policy_, GetMetricsEnabled(_))
270 .Times(AnyNumber())
271 .WillRepeatedly(SetMetricsPolicy(true));
272 provider_ = new policy::PolicyProvider(device_policy_);
273 ml.SetPolicyProvider(provider_);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700274 reinterpret_cast<MetricsLibrary*>(lib_)->cached_enabled_time_ = 0;
Sam Leffler10b301d2010-06-17 14:22:43 -0700275 }
276
277 virtual void TearDown() {
278 CMetricsLibraryDelete(lib_);
Ben Chan2e6543d2014-02-05 23:26:25 -0800279 base::DeleteFile(kTestUMAEventsFile, false);
Sam Leffler10b301d2010-06-17 14:22:43 -0700280 }
281
282 CMetricsLibrary lib_;
Ken Mixterb2f17092011-07-22 14:59:51 -0700283 policy::MockDevicePolicy* device_policy_;
284 policy::PolicyProvider* provider_;
Sam Leffler10b301d2010-06-17 14:22:43 -0700285};
286
Ken Mixter4c5daa42010-08-26 18:35:06 -0700287TEST_F(CMetricsLibraryTest, AreMetricsEnabledFalse) {
Ken Mixterb2f17092011-07-22 14:59:51 -0700288 EXPECT_CALL(*device_policy_, GetMetricsEnabled(_))
289 .WillOnce(SetMetricsPolicy(false));
Ken Mixter4c5daa42010-08-26 18:35:06 -0700290 EXPECT_FALSE(CMetricsLibraryAreMetricsEnabled(lib_));
291}
292
293TEST_F(CMetricsLibraryTest, AreMetricsEnabledTrue) {
294 EXPECT_TRUE(CMetricsLibraryAreMetricsEnabled(lib_));
295}
296
Sam Leffler10b301d2010-06-17 14:22:43 -0700297TEST_F(CMetricsLibraryTest, SendEnumToUMA) {
298 char buf[100];
299 const int kLen = 40;
300 EXPECT_TRUE(CMetricsLibrarySendEnumToUMA(lib_, "Test.EnumMetric", 1, 3));
Ben Chan2e6543d2014-02-05 23:26:25 -0800301 EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100));
Sam Leffler10b301d2010-06-17 14:22:43 -0700302
303 char exp[kLen];
304 sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3",
305 kLen, 0, 0, 0, 0);
306 EXPECT_EQ(0, memcmp(exp, buf, kLen));
307}
308
309TEST_F(CMetricsLibraryTest, SendToUMA) {
310 char buf[100];
311 const int kLen = 37;
312 EXPECT_TRUE(CMetricsLibrarySendToUMA(lib_, "Test.Metric", 2, 1, 100, 50));
Ben Chan2e6543d2014-02-05 23:26:25 -0800313 EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100));
Sam Leffler10b301d2010-06-17 14:22:43 -0700314
315 char exp[kLen];
316 sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0);
317 EXPECT_EQ(0, memcmp(exp, buf, kLen));
318}
319
Darin Petkoved824852011-01-06 10:51:47 -0800320TEST_F(CMetricsLibraryTest, SendUserActionToUMA) {
321 char buf[100];
322 const int kLen = 30;
323 EXPECT_TRUE(CMetricsLibrarySendUserActionToUMA(lib_, "SomeKeyPressed"));
Ben Chan2e6543d2014-02-05 23:26:25 -0800324 EXPECT_EQ(kLen, base::ReadFile(kTestUMAEventsFile, buf, 100));
Darin Petkoved824852011-01-06 10:51:47 -0800325
326 char exp[kLen];
327 sprintf(exp, "%c%c%c%cuseraction%cSomeKeyPressed", kLen, 0, 0, 0, 0);
328 EXPECT_EQ(0, memcmp(exp, buf, kLen));
329}
330
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800331TEST_F(CMetricsLibraryTest, SendCrashToUMA) {
332 char buf[100];
333 char exp[100];
334 int len = sprintf(exp, "%c%c%c%ccrash%cuser", 0, 0, 0, 0, 0) + 1;
335 exp[0] = len;
336 EXPECT_TRUE(CMetricsLibrarySendCrashToUMA(lib_, "user"));
Ben Chan2e6543d2014-02-05 23:26:25 -0800337 EXPECT_EQ(len, base::ReadFile(kTestUMAEventsFile, buf, 100));
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800338
339 EXPECT_EQ(0, memcmp(exp, buf, len));
340}
341
Darin Petkov11b8eb32010-05-18 11:00:59 -0700342int main(int argc, char** argv) {
343 testing::InitGoogleTest(&argc, argv);
344 return RUN_ALL_TESTS();
345}