1 // Copyright (c) 2012 The Chromium 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.
5 // A test application for the RLZ library.
7 // These tests should not be executed on the build server:
8 // - They assert for the failed cases.
9 // - They modify machine state (registry).
11 // These tests require write access to HKLM and HKCU.
13 // The "GGLA" brand is used to test the normal code flow of the code, and the
14 // "TEST" brand is used to test the supplementary brand code code flow.
16 #include "base/posix/eintr_wrapper.h"
17 #include "base/logging.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 #include "rlz/lib/financial_ping.h"
23 #include "rlz/lib/rlz_lib.h"
24 #include "rlz/lib/rlz_value_store.h"
25 #include "rlz/test/rlz_test_helpers.h"
29 #include "rlz/win/lib/machine_deal.h"
32 #if defined(RLZ_NETWORK_IMPLEMENTATION_CHROME_NET)
33 #include "base/mac/scoped_nsautorelease_pool.h"
34 #include "base/threading/thread.h"
35 #include "net/url_request/url_request_test_util.h"
39 class MachineDealCodeHelper
41 : public rlz_lib::MachineDealCode
47 return rlz_lib::MachineDealCode::Clear();
54 MachineDealCodeHelper() {}
55 ~MachineDealCodeHelper() {}
58 class RlzLibTest
: public RlzLibTestBase
{
61 TEST_F(RlzLibTest
, RecordProductEvent
) {
64 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
65 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
66 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
67 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
69 EXPECT_STREQ("events=I7S", cgi_50
);
71 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
72 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
73 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
75 EXPECT_STREQ("events=I7S,W1I", cgi_50
);
77 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
78 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
79 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
81 EXPECT_STREQ("events=I7S,W1I", cgi_50
);
84 TEST_F(RlzLibTest
, ClearProductEvent
) {
87 // Clear 1 of 1 events.
88 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
89 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
90 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
91 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
93 EXPECT_STREQ("events=I7S", cgi_50
);
94 EXPECT_TRUE(rlz_lib::ClearProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
95 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
96 EXPECT_FALSE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
98 EXPECT_STREQ("", cgi_50
);
100 // Clear 1 of 2 events.
101 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
102 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
103 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
104 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
105 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
106 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
108 EXPECT_STREQ("events=I7S,W1I", cgi_50
);
109 EXPECT_TRUE(rlz_lib::ClearProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
110 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
111 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
113 EXPECT_STREQ("events=W1I", cgi_50
);
115 // Clear a non-recorded event.
116 EXPECT_TRUE(rlz_lib::ClearProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
117 rlz_lib::IETB_SEARCH_BOX
, rlz_lib::FIRST_SEARCH
));
118 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
120 EXPECT_STREQ("events=W1I", cgi_50
);
124 TEST_F(RlzLibTest
, GetProductEventsAsCgi
) {
128 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
129 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
130 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
131 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
133 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
134 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
136 EXPECT_FALSE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
138 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
140 EXPECT_STREQ("events=I7S,W1I", cgi_50
);
143 TEST_F(RlzLibTest
, ClearAllAllProductEvents
) {
146 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
147 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
148 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
149 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
151 EXPECT_STREQ("events=I7S", cgi_50
);
153 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
154 EXPECT_FALSE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
156 EXPECT_STREQ("", cgi_50
);
159 TEST_F(RlzLibTest
, SetAccessPointRlz
) {
161 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, ""));
162 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, rlz_50
, 50));
163 EXPECT_STREQ("", rlz_50
);
165 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, "IeTbRlz"));
166 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, rlz_50
, 50));
167 EXPECT_STREQ("IeTbRlz", rlz_50
);
170 TEST_F(RlzLibTest
, GetAccessPointRlz
) {
173 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, ""));
174 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, rlz_1
, 1));
175 EXPECT_STREQ("", rlz_1
);
177 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, "IeTbRlz"));
178 EXPECT_FALSE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, rlz_1
, 1));
179 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, rlz_50
, 50));
180 EXPECT_STREQ("IeTbRlz", rlz_50
);
183 TEST_F(RlzLibTest
, GetPingParams
) {
184 MachineDealCodeHelper::Clear();
186 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
,
188 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IE_HOME_PAGE
, ""));
191 rlz_lib::AccessPoint points
[] =
192 {rlz_lib::IETB_SEARCH_BOX
, rlz_lib::NO_ACCESS_POINT
,
193 rlz_lib::NO_ACCESS_POINT
};
195 EXPECT_TRUE(rlz_lib::GetPingParams(rlz_lib::TOOLBAR_NOTIFIER
, points
,
197 EXPECT_STREQ("rep=2&rlz=T4:TbRlzValue", cgi
);
200 EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value"));
201 #define DCC_PARAM "&dcc=dcc_value"
206 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, ""));
207 EXPECT_TRUE(rlz_lib::GetPingParams(rlz_lib::TOOLBAR_NOTIFIER
, points
,
209 EXPECT_STREQ("rep=2&rlz=T4:" DCC_PARAM
, cgi
);
211 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
,
213 EXPECT_FALSE(rlz_lib::GetPingParams(rlz_lib::TOOLBAR_NOTIFIER
, points
,
214 cgi
, 23 + strlen(DCC_PARAM
)));
215 EXPECT_STREQ("", cgi
);
216 EXPECT_TRUE(rlz_lib::GetPingParams(rlz_lib::TOOLBAR_NOTIFIER
, points
,
217 cgi
, 24 + strlen(DCC_PARAM
)));
218 EXPECT_STREQ("rep=2&rlz=T4:TbRlzValue" DCC_PARAM
, cgi
);
220 EXPECT_TRUE(GetAccessPointRlz(rlz_lib::IE_HOME_PAGE
, cgi
, 2048));
221 points
[2] = rlz_lib::IE_HOME_PAGE
;
222 EXPECT_TRUE(rlz_lib::GetPingParams(rlz_lib::TOOLBAR_NOTIFIER
, points
,
224 EXPECT_STREQ("rep=2&rlz=T4:TbRlzValue" DCC_PARAM
, cgi
);
227 TEST_F(RlzLibTest
, IsPingResponseValid
) {
228 const char* kBadPingResponses
[] = {
230 "version: 3.0.914.7250\r\n"
231 "url: http://www.corp.google.com/~av/45/opt/SearchWithGoogleUpdate.exe\r\n"
232 "launch-action: custom-action\r\n"
233 "launch-target: SearchWithGoogleUpdate.exe\r\n"
234 "signature: c08a3f4438e1442c4fe5678ee147cf6c5516e5d62bb64e\r\n"
235 "rlz: 1R1_____en__252\r\n"
236 "rlzXX: 1R1_____en__250\r\n",
239 "version: 3.0.914.7250\r\n"
240 "url: http://www.corp.google.com/~av/45/opt/SearchWithGoogleUpdate.exe\r\n"
241 "launch-action: custom-action\r\n"
242 "launch-target: SearchWithGoogleUpdate.exe\r\n"
243 "signature: c08a3f4438e1442c4fe5678ee147cf6c5516e5d62bb64e\r\n"
244 "rlz: 1R1_____en__252\r\n"
245 "rlzXX: 1R1_____en__250\r\n"
246 "rlzT4 1T4_____en__251\r\n"
247 "rlzT4: 1T4_____en__252\r\n"
251 // Misplaced checksum.
252 "version: 3.0.914.7250\r\n"
253 "url: http://www.corp.google.com/~av/45/opt/SearchWithGoogleUpdate.exe\r\n"
254 "launch-action: custom-action\r\n"
255 "launch-target: SearchWithGoogleUpdate.exe\r\n"
256 "signature: c08a3f4438e1442c4fe5678ee147cf6c5516e5d62bb64e\r\n"
257 "rlz: 1R1_____en__252\r\n"
258 "rlzXX: 1R1_____en__250\r\n"
259 "crc32: B12CC79C\r\n"
260 "rlzT4 1T4_____en__251\r\n"
261 "rlzT4: 1T4_____en__252\r\n"
267 const char* kGoodPingResponses
[] = {
268 "version: 3.0.914.7250\r\n"
269 "url: http://www.corp.google.com/~av/45/opt/SearchWithGoogleUpdate.exe\r\n"
270 "launch-action: custom-action\r\n"
271 "launch-target: SearchWithGoogleUpdate.exe\r\n"
272 "signature: c08a3f4438e1442c4fe5678ee147cf6c5516e5d62bb64e\r\n"
273 "rlz: 1R1_____en__252\r\n"
274 "rlzXX: 1R1_____en__250\r\n"
275 "rlzT4 1T4_____en__251\r\n"
276 "rlzT4: 1T4_____en__252\r\n"
280 "version: 3.0.914.7250\r\n"
281 "url: http://www.corp.google.com/~av/45/opt/SearchWithGoogleUpdate.exe\r\n"
282 "launch-action: custom-action\r\n"
283 "launch-target: SearchWithGoogleUpdate.exe\r\n"
284 "signature: c08a3f4438e1442c4fe5678ee147cf6c5516e5d62bb64e\r\n"
285 "rlz: 1R1_____en__252\r\n"
286 "rlzXX: 1R1_____en__250\r\n"
287 "rlzT4 1T4_____en__251\r\n"
288 "rlzT4: 1T4_____en__252\r\n"
290 "crc32: D6FD55A3\r\n"
291 "extradata: not checksummed",
296 for (int i
= 0; kBadPingResponses
[i
]; i
++)
297 EXPECT_FALSE(rlz_lib::IsPingResponseValid(kBadPingResponses
[i
], NULL
));
299 for (int i
= 0; kGoodPingResponses
[i
]; i
++)
300 EXPECT_TRUE(rlz_lib::IsPingResponseValid(kGoodPingResponses
[i
], NULL
));
303 TEST_F(RlzLibTest
, ParsePingResponse
) {
304 const char* kPingResponse
=
305 "version: 3.0.914.7250\r\n"
306 "url: http://www.corp.google.com/~av/45/opt/SearchWithGoogleUpdate.exe\r\n"
307 "launch-action: custom-action\r\n"
308 "launch-target: SearchWithGoogleUpdate.exe\r\n"
309 "signature: c08a3f4438e1442c4fe5678ee147cf6c5516e5d62bb64e\r\n"
310 "rlz: 1R1_____en__252\r\n" // Invalid RLZ - no access point.
311 "rlzXX: 1R1_____en__250\r\n" // Invalid RLZ - bad access point.
312 "rlzT4 1T4_____en__251\r\n" // Invalid RLZ - missing colon.
313 "rlzT4: 1T4_____en__252\r\n" // GoodRLZ.
314 "events: I7S,W1I\r\n" // Clear all events.
320 EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value2"));
323 // Record some product events to check that they get cleared.
324 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
325 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
326 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
327 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
329 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(
330 rlz_lib::IETB_SEARCH_BOX
, "TbRlzValue"));
332 EXPECT_TRUE(rlz_lib::ParsePingResponse(rlz_lib::TOOLBAR_NOTIFIER
,
336 EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value"));
338 EXPECT_TRUE(rlz_lib::ParsePingResponse(rlz_lib::TOOLBAR_NOTIFIER
,
342 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, value
, 50));
343 EXPECT_STREQ("1T4_____en__252", value
);
344 EXPECT_FALSE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
346 EXPECT_STREQ("", value
);
348 const char* kPingResponse2
=
349 "rlzT4: 1T4_____de__253 \r\n" // Good with extra spaces.
350 "crc32: 321334F5\r\n";
351 EXPECT_TRUE(rlz_lib::ParsePingResponse(rlz_lib::TOOLBAR_NOTIFIER
,
353 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, value
, 50));
354 EXPECT_STREQ("1T4_____de__253", value
);
356 const char* kPingResponse3
=
357 "crc32: 0\r\n"; // Good RLZ - empty response.
358 EXPECT_TRUE(rlz_lib::ParsePingResponse(rlz_lib::TOOLBAR_NOTIFIER
,
360 EXPECT_STREQ("1T4_____de__253", value
);
363 // Test whether a stateful event will only be sent in financial pings once.
364 TEST_F(RlzLibTest
, ParsePingResponseWithStatefulEvents
) {
365 const char* kPingResponse
=
366 "version: 3.0.914.7250\r\n"
367 "url: http://www.corp.google.com/~av/45/opt/SearchWithGoogleUpdate.exe\r\n"
368 "launch-action: custom-action\r\n"
369 "launch-target: SearchWithGoogleUpdate.exe\r\n"
370 "signature: c08a3f4438e1442c4fe5678ee147cf6c5516e5d62bb64e\r\n"
371 "rlzT4: 1T4_____en__252\r\n" // GoodRLZ.
372 "events: I7S,W1I\r\n" // Clear all events.
373 "stateful-events: W1I\r\n" // W1I as an stateful event.
378 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
380 // Record some product events to check that they get cleared.
381 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
382 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
383 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
384 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
386 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(
387 rlz_lib::IETB_SEARCH_BOX
, "TbRlzValue"));
389 EXPECT_TRUE(rlz_lib::ParsePingResponse(rlz_lib::TOOLBAR_NOTIFIER
,
392 // Check all the events sent earlier are cleared.
394 EXPECT_FALSE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
396 EXPECT_STREQ("", value
);
398 // Record both events (one is stateless and the other is stateful) again.
399 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
400 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
401 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
402 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
404 // Check the stateful event won't be sent again while the stateless one will.
405 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
407 EXPECT_STREQ("events=I7S", value
);
409 // Test that stateful events are cleared by ClearAllProductEvents(). After
410 // calling it, trying to record a stateful again should result in it being
412 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
413 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
414 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
415 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
417 EXPECT_STREQ("events=W1I", value
);
420 class URLRequestRAII
{
422 URLRequestRAII(net::URLRequestContextGetter
* context
) {
423 rlz_lib::SetURLRequestContext(context
);
426 rlz_lib::SetURLRequestContext(NULL
);
430 TEST_F(RlzLibTest
, SendFinancialPing
) {
431 // We don't really check a value or result in this test. All this does is
432 // attempt to ping the financial server, which you can verify in Fiddler.
433 // TODO: Make this a measurable test.
435 #if defined(RLZ_NETWORK_IMPLEMENTATION_CHROME_NET)
436 #if defined(OS_MACOSX)
437 base::mac::ScopedNSAutoreleasePool pool
;
440 base::Thread::Options options
;
441 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
443 base::Thread
io_thread("rlz_unittest_io_thread");
444 ASSERT_TRUE(io_thread
.StartWithOptions(options
));
446 scoped_refptr
<net::TestURLRequestContextGetter
> context
=
447 new net::TestURLRequestContextGetter(
448 io_thread
.message_loop()->message_loop_proxy());
449 rlz_lib::SetURLRequestContext(context
.get());
451 URLRequestRAII
set_context(context
.get());
454 MachineDealCodeHelper::Clear();
456 EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value"));
459 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
,
462 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
463 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
464 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
465 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
466 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
468 rlz_lib::AccessPoint points
[] =
469 {rlz_lib::IETB_SEARCH_BOX
, rlz_lib::NO_ACCESS_POINT
,
470 rlz_lib::NO_ACCESS_POINT
};
473 rlz_lib::SendFinancialPing(rlz_lib::TOOLBAR_NOTIFIER
, points
,
474 "swg", "GGLA", "SwgProductId1234", "en-UK", false,
475 /*skip_time_check=*/true);
478 #if defined(RLZ_NETWORK_IMPLEMENTATION_CHROME_NET)
480 void ResetContext() {
481 rlz_lib::SetURLRequestContext(NULL
);
484 TEST_F(RlzLibTest
, SendFinancialPingDuringShutdown
) {
485 // rlz_lib::SendFinancialPing fails when this is set.
486 if (!rlz_lib::SupplementaryBranding::GetBrand().empty())
489 #if defined(OS_MACOSX)
490 base::mac::ScopedNSAutoreleasePool pool
;
493 base::Thread::Options options
;
494 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
496 base::Thread
io_thread("rlz_unittest_io_thread");
497 ASSERT_TRUE(io_thread
.StartWithOptions(options
));
499 scoped_refptr
<net::TestURLRequestContextGetter
> context
=
500 new net::TestURLRequestContextGetter(
501 io_thread
.message_loop()->message_loop_proxy());
502 rlz_lib::SetURLRequestContext(context
.get());
504 URLRequestRAII
set_context(context
.get());
506 rlz_lib::AccessPoint points
[] =
507 {rlz_lib::IETB_SEARCH_BOX
, rlz_lib::NO_ACCESS_POINT
,
508 rlz_lib::NO_ACCESS_POINT
};
509 rlz_lib::test::ResetSendFinancialPingInterrupted();
510 EXPECT_FALSE(rlz_lib::test::WasSendFinancialPingInterrupted());
512 base::MessageLoop loop
;
513 loop
.PostTask(FROM_HERE
, base::Bind(&ResetContext
));
515 EXPECT_FALSE(rlz_lib::SendFinancialPing(rlz_lib::TOOLBAR_NOTIFIER
, points
,
516 "swg", "GGLA", "SwgProductId1234", "en-UK", false,
517 /*skip_time_check=*/true));
519 EXPECT_TRUE(rlz_lib::test::WasSendFinancialPingInterrupted());
520 rlz_lib::test::ResetSendFinancialPingInterrupted();
524 TEST_F(RlzLibTest
, ClearProductState
) {
525 MachineDealCodeHelper::Clear();
527 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
,
529 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::GD_DESKBAND
,
532 rlz_lib::AccessPoint points
[] =
533 { rlz_lib::IETB_SEARCH_BOX
, rlz_lib::NO_ACCESS_POINT
};
535 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
536 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
537 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
538 rlz_lib::IETB_SEARCH_BOX
, rlz_lib::INSTALL
));
540 rlz_lib::AccessPoint points2
[] =
541 { rlz_lib::IETB_SEARCH_BOX
,
542 rlz_lib::GD_DESKBAND
,
543 rlz_lib::NO_ACCESS_POINT
};
546 EXPECT_TRUE(rlz_lib::GetPingParams(rlz_lib::TOOLBAR_NOTIFIER
, points2
,
548 EXPECT_STREQ("rep=2&rlz=T4:TbRlzValue,D1:GdbRlzValue", cgi
);
550 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
552 std::string
events(cgi
);
553 EXPECT_LT(0u, events
.find("I7S"));
554 EXPECT_LT(0u, events
.find("T4I"));
555 EXPECT_LT(0u, events
.find("T4R"));
557 rlz_lib::ClearProductState(rlz_lib::TOOLBAR_NOTIFIER
, points
);
559 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
,
561 EXPECT_STREQ("", cgi
);
562 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::GD_DESKBAND
,
564 EXPECT_STREQ("GdbRlzValue", cgi
);
566 EXPECT_FALSE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
568 EXPECT_STREQ("", cgi
);
573 class typed_buffer_ptr
{
574 scoped_ptr
<char[]> buffer_
;
580 explicit typed_buffer_ptr(size_t size
) : buffer_(new char[size
]) {
583 void reset(size_t size
) {
584 buffer_
.reset(new char[size
]);
588 return reinterpret_cast<T
*>(buffer_
.get());
593 bool HasAccess(PSID sid
, ACCESS_MASK access_mask
, ACL
* dacl
);
596 bool EmptyAcl(ACL
* acl
) {
597 ACL_SIZE_INFORMATION info
;
598 bool ret
= GetAclInformation(acl
, &info
, sizeof(info
), AclSizeInformation
);
601 for (DWORD i
= 0; i
< info
.AceCount
&& ret
; ++i
) {
602 ret
= DeleteAce(acl
, 0);
609 TEST_F(RlzLibTest
, HasAccess
) {
610 // Create a SID that represents ALL USERS.
611 DWORD users_sid_size
= SECURITY_MAX_SID_SIZE
;
612 typed_buffer_ptr
<SID
> users_sid(users_sid_size
);
613 CreateWellKnownSid(WinBuiltinUsersSid
, NULL
, users_sid
, &users_sid_size
);
615 // RLZ always asks for KEY_ALL_ACCESS access to the key. This is what we
618 // No ACL mean no access.
619 EXPECT_FALSE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, NULL
));
621 // Create an ACL for these tests.
622 const DWORD kMaxAclSize
= 1024;
623 typed_buffer_ptr
<ACL
> dacl(kMaxAclSize
);
624 InitializeAcl(dacl
, kMaxAclSize
, ACL_REVISION
);
626 // Empty DACL mean no access.
627 EXPECT_FALSE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, dacl
));
629 // ACE without all needed privileges should mean no access.
630 EXPECT_TRUE(AddAccessAllowedAce(dacl
, ACL_REVISION
, KEY_READ
, users_sid
));
631 EXPECT_FALSE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, dacl
));
633 // ACE without all needed privileges should mean no access.
634 EXPECT_TRUE(EmptyAcl(dacl
));
635 EXPECT_TRUE(AddAccessAllowedAce(dacl
, ACL_REVISION
, KEY_WRITE
, users_sid
));
636 EXPECT_FALSE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, dacl
));
638 // A deny ACE before an allow ACE should not give access.
639 EXPECT_TRUE(EmptyAcl(dacl
));
640 EXPECT_TRUE(AddAccessDeniedAce(dacl
, ACL_REVISION
, KEY_ALL_ACCESS
,
642 EXPECT_TRUE(AddAccessAllowedAce(dacl
, ACL_REVISION
, KEY_ALL_ACCESS
,
644 EXPECT_FALSE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, dacl
));
646 // A deny ACE before an allow ACE should not give access.
647 EXPECT_TRUE(EmptyAcl(dacl
));
648 EXPECT_TRUE(AddAccessDeniedAce(dacl
, ACL_REVISION
, KEY_READ
, users_sid
));
649 EXPECT_TRUE(AddAccessAllowedAce(dacl
, ACL_REVISION
, KEY_ALL_ACCESS
,
651 EXPECT_FALSE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, dacl
));
654 // An allow ACE without all required bits should not give access.
655 EXPECT_TRUE(EmptyAcl(dacl
));
656 EXPECT_TRUE(AddAccessAllowedAce(dacl
, ACL_REVISION
, KEY_WRITE
, users_sid
));
657 EXPECT_FALSE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, dacl
));
659 // An allow ACE with all required bits should give access.
660 EXPECT_TRUE(EmptyAcl(dacl
));
661 EXPECT_TRUE(AddAccessAllowedAce(dacl
, ACL_REVISION
, KEY_ALL_ACCESS
,
663 EXPECT_TRUE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, dacl
));
665 // A deny ACE after an allow ACE should not give access.
666 EXPECT_TRUE(EmptyAcl(dacl
));
667 EXPECT_TRUE(AddAccessAllowedAce(dacl
, ACL_REVISION
, KEY_ALL_ACCESS
,
669 EXPECT_TRUE(AddAccessDeniedAce(dacl
, ACL_REVISION
, KEY_READ
, users_sid
));
670 EXPECT_TRUE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, dacl
));
672 // An inherit-only allow ACE should not give access.
673 EXPECT_TRUE(EmptyAcl(dacl
));
674 EXPECT_TRUE(AddAccessAllowedAceEx(dacl
, ACL_REVISION
, INHERIT_ONLY_ACE
,
675 KEY_ALL_ACCESS
, users_sid
));
676 EXPECT_FALSE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, dacl
));
678 // An inherit-only deny ACE should not apply.
679 EXPECT_TRUE(EmptyAcl(dacl
));
680 EXPECT_TRUE(AddAccessDeniedAceEx(dacl
, ACL_REVISION
, INHERIT_ONLY_ACE
,
681 KEY_ALL_ACCESS
, users_sid
));
682 EXPECT_TRUE(AddAccessAllowedAce(dacl
, ACL_REVISION
, KEY_ALL_ACCESS
,
684 EXPECT_TRUE(rlz_lib::HasAccess(users_sid
, KEY_ALL_ACCESS
, dacl
));
688 TEST_F(RlzLibTest
, BrandingRecordProductEvent
) {
689 // Don't run these tests if a supplementary brand is already in place. That
690 // way we can control the branding.
691 if (!rlz_lib::SupplementaryBranding::GetBrand().empty())
696 // Record different events for the same product with diffrent branding, and
697 // make sure that the information remains separate.
698 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
700 rlz_lib::SupplementaryBranding
branding("TEST");
701 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
704 // Test that recording events with the default brand and a supplementary
705 // brand don't overwrite each other.
707 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
708 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
709 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
711 EXPECT_STREQ("events=I7S", cgi_50
);
714 rlz_lib::SupplementaryBranding
branding("TEST");
715 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
716 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::INSTALL
));
717 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
719 EXPECT_STREQ("events=I7I", cgi_50
);
722 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
724 EXPECT_STREQ("events=I7S", cgi_50
);
727 TEST_F(RlzLibTest
, BrandingSetAccessPointRlz
) {
728 // Don't run these tests if a supplementary brand is already in place. That
729 // way we can control the branding.
730 if (!rlz_lib::SupplementaryBranding::GetBrand().empty())
735 // Test that setting RLZ strings with the default brand and a supplementary
736 // brand don't overwrite each other.
738 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, "IeTbRlz"));
739 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, rlz_50
, 50));
740 EXPECT_STREQ("IeTbRlz", rlz_50
);
743 rlz_lib::SupplementaryBranding
branding("TEST");
745 EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, "SuppRlz"));
746 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, rlz_50
,
748 EXPECT_STREQ("SuppRlz", rlz_50
);
751 EXPECT_TRUE(rlz_lib::GetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX
, rlz_50
, 50));
752 EXPECT_STREQ("IeTbRlz", rlz_50
);
756 TEST_F(RlzLibTest
, BrandingWithStatefulEvents
) {
757 // Don't run these tests if a supplementary brand is already in place. That
758 // way we can control the branding.
759 if (!rlz_lib::SupplementaryBranding::GetBrand().empty())
762 const char* kPingResponse
=
763 "version: 3.0.914.7250\r\n"
764 "url: http://www.corp.google.com/~av/45/opt/SearchWithGoogleUpdate.exe\r\n"
765 "launch-action: custom-action\r\n"
766 "launch-target: SearchWithGoogleUpdate.exe\r\n"
767 "signature: c08a3f4438e1442c4fe5678ee147cf6c5516e5d62bb64e\r\n"
768 "rlzT4: 1T4_____en__252\r\n" // GoodRLZ.
769 "events: I7S,W1I\r\n" // Clear all events.
770 "stateful-events: W1I\r\n" // W1I as an stateful event.
775 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
777 rlz_lib::SupplementaryBranding
branding("TEST");
778 EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER
));
781 // Record some product events for the default and supplementary brand.
782 // Check that they get cleared only for the default brand.
783 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
784 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
785 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
786 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
789 rlz_lib::SupplementaryBranding
branding("TEST");
790 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
791 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
792 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
793 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
796 EXPECT_TRUE(rlz_lib::ParsePingResponse(rlz_lib::TOOLBAR_NOTIFIER
,
799 // Check all the events sent earlier are cleared only for default brand.
801 EXPECT_FALSE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
803 EXPECT_STREQ("", value
);
806 rlz_lib::SupplementaryBranding
branding("TEST");
807 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
809 EXPECT_STREQ("events=I7S,W1I", value
);
812 // Record both events (one is stateless and the other is stateful) again.
813 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
814 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
815 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
816 rlz_lib::IE_HOME_PAGE
, rlz_lib::INSTALL
));
818 // Check the stateful event won't be sent again while the stateless one will.
819 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
821 EXPECT_STREQ("events=I7S", value
);
824 rlz_lib::SupplementaryBranding
branding("TEST");
825 EXPECT_TRUE(rlz_lib::ParsePingResponse(rlz_lib::TOOLBAR_NOTIFIER
,
828 EXPECT_FALSE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
830 EXPECT_STREQ("", value
);
833 EXPECT_TRUE(rlz_lib::GetProductEventsAsCgi(rlz_lib::TOOLBAR_NOTIFIER
,
835 EXPECT_STREQ("events=I7S", value
);
838 #if defined(OS_POSIX)
839 class ReadonlyRlzDirectoryTest
: public RlzLibTestNoMachineState
{
841 void SetUp() override
;
844 void ReadonlyRlzDirectoryTest::SetUp() {
845 RlzLibTestNoMachineState::SetUp();
846 // Make the rlz directory non-writeable.
847 int chmod_result
= chmod(m_rlz_test_helper_
.temp_dir_
.path().value().c_str(),
849 ASSERT_EQ(0, chmod_result
);
852 TEST_F(ReadonlyRlzDirectoryTest
, WriteFails
) {
853 // The rlz test runner runs every test twice: Once normally, and once with
854 // a SupplementaryBranding on the stack. In the latter case, the rlz lock
855 // has already been acquired before the rlz directory got changed to
856 // read-only, which makes this test pointless. So run it only in the first
858 if (!rlz_lib::SupplementaryBranding::GetBrand().empty())
861 EXPECT_FALSE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
862 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::SET_TO_GOOGLE
));
865 // Regression test for http://crbug.com/121255
866 TEST_F(ReadonlyRlzDirectoryTest
, SupplementaryBrandingDoesNotCrash
) {
867 // See the comment at the top of WriteFails.
868 if (!rlz_lib::SupplementaryBranding::GetBrand().empty())
871 rlz_lib::SupplementaryBranding
branding("TEST");
872 EXPECT_FALSE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
873 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::INSTALL
));
876 // Regression test for http://crbug.com/141108
877 TEST_F(RlzLibTest
, ConcurrentStoreAccessWithProcessExitsWhileLockHeld
) {
878 // See the comment at the top of WriteFails.
879 if (!rlz_lib::SupplementaryBranding::GetBrand().empty())
882 std::vector
<pid_t
> pids
;
883 for (int i
= 0; i
< 10; ++i
) {
889 // SupplementaryBranding is a RAII object for the rlz lock.
890 rlz_lib::SupplementaryBranding
branding("TEST");
892 // Simulate a crash while holding the lock in some of the children.
893 if (i
> 0 && i
% 3 == 0)
896 // Note: Since this is in a forked child, a failing expectation won't
897 // make the test fail. It does however cause lots of "check failed"
898 // error output. The parent process will then check the exit code
899 // below to make the test fail.
900 bool success
= rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
901 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::INSTALL
);
902 EXPECT_TRUE(success
);
903 _exit(success
? 0 : 1);
913 for (size_t i
= 0; i
< pids
.size(); ++i
) {
914 if (HANDLE_EINTR(waitpid(pids
[i
], &status
, 0)) != -1)
915 EXPECT_TRUE(WIFEXITED(status
) && WEXITSTATUS(status
) == 0);
918 // No child should have the lock at this point, not even the crashed ones.
919 EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
920 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::INSTALL
));
923 TEST_F(RlzLibTest
, LockAcquistionSucceedsButStoreFileCannotBeCreated
) {
924 // See the comment at the top of WriteFails.
925 if (!rlz_lib::SupplementaryBranding::GetBrand().empty())
928 // Create a directory where the rlz file is supposed to appear. This way,
929 // the lock file can be created successfully, but creation of the rlz file
931 int mkdir_result
= mkdir(rlz_lib::testing::RlzStoreFilenameStr().c_str(),
933 ASSERT_EQ(0, mkdir_result
);
935 rlz_lib::SupplementaryBranding
branding("TEST");
936 EXPECT_FALSE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER
,
937 rlz_lib::IE_DEFAULT_SEARCH
, rlz_lib::INSTALL
));