financial_ping_test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.
  4. //
  5. // A test application for the FinancialPing class.
  6. //
  7. // These tests should not be executed on the build server:
  8. // - They modify machine state (registry).
  9. //
  10. // These tests require write access to HKCU and HKLM.
  11. //
  12. // The "GGLA" brand is used to test the normal code flow of the code, and the
  13. // "TEST" brand is used to test the supplementary brand code code flow. In one
  14. // case below, the brand "GOOG" is used because the code wants to use a brand
  15. // that is neither of the two mentioned above.
  16. #include "rlz/lib/financial_ping.h"
  17. #include <stdint.h>
  18. #include "base/strings/string_util.h"
  19. #include "base/strings/stringprintf.h"
  20. #include "base/strings/utf_string_conversions.h"
  21. #include "build/build_config.h"
  22. #include "build/chromeos_buildflags.h"
  23. #include "rlz/lib/lib_values.h"
  24. #include "rlz/lib/machine_id.h"
  25. #include "rlz/lib/rlz_lib.h"
  26. #include "rlz/lib/rlz_value_store.h"
  27. #include "rlz/lib/time_util.h"
  28. #include "rlz/test/rlz_test_helpers.h"
  29. #include "testing/gmock/include/gmock/gmock.h"
  30. #include "testing/gtest/include/gtest/gtest.h"
  31. #if BUILDFLAG(IS_WIN)
  32. #include "rlz/win/lib/machine_deal.h"
  33. #else
  34. #include "base/time/time.h"
  35. #endif
  36. #if BUILDFLAG(IS_CHROMEOS_ASH)
  37. #include "chromeos/system/factory_ping_embargo_check.h"
  38. #include "rlz/chromeos/lib/rlz_value_store_chromeos.h"
  39. #endif
  40. namespace {
  41. #if BUILDFLAG(IS_CHROMEOS_ASH)
  42. void RemoveMachineIdFromUrl(std::string* url) {
  43. size_t id_offset = url->find("&id=");
  44. EXPECT_NE(std::string::npos, id_offset);
  45. url->resize(id_offset);
  46. }
  47. // Utility function to convert a |base::Time::Exploded| to "yyyy-mm-dd" format.
  48. std::string ConvertExplodedToRlzEmbargoDate(
  49. const base::Time::Exploded& exploded) {
  50. std::string rlz_embargo_date = std::to_string(exploded.year);
  51. rlz_embargo_date += '-';
  52. if (exploded.month < 10)
  53. rlz_embargo_date += '0';
  54. rlz_embargo_date += std::to_string(exploded.month);
  55. rlz_embargo_date += '-';
  56. if (exploded.day_of_month < 10)
  57. rlz_embargo_date += '0';
  58. rlz_embargo_date += std::to_string(exploded.day_of_month);
  59. return rlz_embargo_date;
  60. }
  61. #endif
  62. // Ping times in 100-nanosecond intervals.
  63. const int64_t k1MinuteInterval = 60LL * 10000000LL; // 1 minute
  64. } // namespace anonymous
  65. class FinancialPingTest : public RlzLibTestBase {
  66. };
  67. TEST_F(FinancialPingTest, FormRequest) {
  68. std::string brand_string = rlz_lib::SupplementaryBranding::GetBrand();
  69. const char* brand = brand_string.empty() ? "GGLA" : brand_string.c_str();
  70. #if BUILDFLAG(IS_WIN)
  71. EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value"));
  72. #define DCC_PARAM "&dcc=dcc_value"
  73. #else
  74. #define DCC_PARAM ""
  75. #endif
  76. EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX,
  77. "TbRlzValue"));
  78. EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER));
  79. EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER,
  80. rlz_lib::IE_DEFAULT_SEARCH, rlz_lib::SET_TO_GOOGLE));
  81. EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER,
  82. rlz_lib::IE_HOME_PAGE, rlz_lib::INSTALL));
  83. rlz_lib::AccessPoint points[] =
  84. {rlz_lib::IETB_SEARCH_BOX, rlz_lib::NO_ACCESS_POINT,
  85. rlz_lib::NO_ACCESS_POINT};
  86. // Don't check the machine Id on Chrome OS since a random one is generated
  87. // each time.
  88. std::string machine_id;
  89. #if BUILDFLAG(IS_CHROMEOS_ASH)
  90. bool got_machine_id = false;
  91. #else
  92. bool got_machine_id = rlz_lib::GetMachineId(&machine_id);
  93. #endif
  94. std::string request;
  95. EXPECT_TRUE(rlz_lib::FinancialPing::FormRequest(rlz_lib::TOOLBAR_NOTIFIER,
  96. points, "swg", brand, NULL, "en", false, &request));
  97. #if BUILDFLAG(IS_CHROMEOS_ASH)
  98. // Ignore the machine Id of the request URL. On Chrome OS a random Id is
  99. // generated with each request.
  100. RemoveMachineIdFromUrl(&request);
  101. #endif
  102. std::string expected_response;
  103. base::StringAppendF(&expected_response,
  104. "/tools/pso/ping?as=swg&brand=%s&hl=en&"
  105. "events=I7S,W1I&rep=2&rlz=T4:TbRlzValue" DCC_PARAM,
  106. brand);
  107. if (got_machine_id)
  108. base::StringAppendF(&expected_response, "&id=%s", machine_id.c_str());
  109. EXPECT_EQ(expected_response, request);
  110. EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX, ""));
  111. EXPECT_TRUE(rlz_lib::FinancialPing::FormRequest(rlz_lib::TOOLBAR_NOTIFIER,
  112. points, "swg", brand, "IdOk2", NULL, false, &request));
  113. #if BUILDFLAG(IS_CHROMEOS_ASH)
  114. // Ignore the machine Id of the request URL. On Chrome OS a random Id is
  115. // generated with each request.
  116. RemoveMachineIdFromUrl(&request);
  117. #endif
  118. expected_response.clear();
  119. base::StringAppendF(&expected_response,
  120. "/tools/pso/ping?as=swg&brand=%s&pid=IdOk2&"
  121. "events=I7S,W1I&rep=2&rlz=T4:" DCC_PARAM, brand);
  122. if (got_machine_id)
  123. base::StringAppendF(&expected_response, "&id=%s", machine_id.c_str());
  124. EXPECT_EQ(expected_response, request);
  125. EXPECT_TRUE(rlz_lib::FinancialPing::FormRequest(rlz_lib::TOOLBAR_NOTIFIER,
  126. points, "swg", brand, "IdOk", NULL, true, &request));
  127. expected_response.clear();
  128. base::StringAppendF(&expected_response,
  129. "/tools/pso/ping?as=swg&brand=%s&pid=IdOk&"
  130. "events=I7S,W1I&rep=2&rlz=T4:" DCC_PARAM, brand);
  131. EXPECT_EQ(expected_response, request);
  132. EXPECT_TRUE(rlz_lib::FinancialPing::FormRequest(rlz_lib::TOOLBAR_NOTIFIER,
  133. points, "swg", brand, NULL, NULL, true, &request));
  134. expected_response.clear();
  135. base::StringAppendF(&expected_response,
  136. "/tools/pso/ping?as=swg&brand=%s&events=I7S,W1I&rep=2"
  137. "&rlz=T4:" DCC_PARAM, brand);
  138. EXPECT_EQ(expected_response, request);
  139. // Clear all events.
  140. EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER));
  141. // Clear all RLZs.
  142. char rlz[rlz_lib::kMaxRlzLength + 1];
  143. for (int ap = rlz_lib::NO_ACCESS_POINT + 1;
  144. ap < rlz_lib::LAST_ACCESS_POINT; ap++) {
  145. rlz[0] = 0;
  146. rlz_lib::AccessPoint point = static_cast<rlz_lib::AccessPoint>(ap);
  147. if (rlz_lib::GetAccessPointRlz(point, rlz, std::size(rlz)) && rlz[0]) {
  148. rlz_lib::SetAccessPointRlz(point, "");
  149. }
  150. }
  151. EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::IETB_SEARCH_BOX,
  152. "TbRlzValue"));
  153. EXPECT_TRUE(rlz_lib::SetAccessPointRlz(rlz_lib::QUICK_SEARCH_BOX,
  154. "QsbRlzValue"));
  155. EXPECT_TRUE(rlz_lib::FinancialPing::FormRequest(rlz_lib::TOOLBAR_NOTIFIER,
  156. points, "swg", brand, NULL, NULL, false, &request));
  157. expected_response.clear();
  158. base::StringAppendF(&expected_response,
  159. "/tools/pso/ping?as=swg&brand=%s&rep=2&rlz=T4:TbRlzValue,"
  160. "Q1:QsbRlzValue" DCC_PARAM, brand);
  161. EXPECT_STREQ(expected_response.c_str(), request.c_str());
  162. if (!GetAccessPointRlz(rlz_lib::IE_HOME_PAGE, rlz, std::size(rlz))) {
  163. points[2] = rlz_lib::IE_HOME_PAGE;
  164. EXPECT_TRUE(rlz_lib::FinancialPing::FormRequest(rlz_lib::TOOLBAR_NOTIFIER,
  165. points, "swg", brand, "MyId", "en-US", true, &request));
  166. expected_response.clear();
  167. base::StringAppendF(&expected_response,
  168. "/tools/pso/ping?as=swg&brand=%s&hl=en-US&pid=MyId&rep=2"
  169. "&rlz=T4:TbRlzValue,Q1:QsbRlzValue" DCC_PARAM, brand);
  170. EXPECT_STREQ(expected_response.c_str(), request.c_str());
  171. }
  172. }
  173. TEST_F(FinancialPingTest, FormRequestBadBrand) {
  174. rlz_lib::AccessPoint points[] =
  175. {rlz_lib::IETB_SEARCH_BOX, rlz_lib::NO_ACCESS_POINT,
  176. rlz_lib::NO_ACCESS_POINT};
  177. std::string request;
  178. bool ok = rlz_lib::FinancialPing::FormRequest(rlz_lib::TOOLBAR_NOTIFIER,
  179. points, "swg", "GOOG", NULL, "en", false, &request);
  180. EXPECT_EQ(rlz_lib::SupplementaryBranding::GetBrand().empty(), ok);
  181. }
  182. static void SetLastPingTime(int64_t time, rlz_lib::Product product) {
  183. rlz_lib::ScopedRlzValueStoreLock lock;
  184. rlz_lib::RlzValueStore* store = lock.GetStore();
  185. ASSERT_TRUE(store);
  186. ASSERT_TRUE(store->HasAccess(rlz_lib::RlzValueStore::kWriteAccess));
  187. store->WritePingTime(product, time);
  188. }
  189. TEST_F(FinancialPingTest, IsPingTime) {
  190. int64_t now = rlz_lib::GetSystemTimeAsInt64();
  191. int64_t last_ping = now - rlz_lib::kEventsPingInterval - k1MinuteInterval;
  192. SetLastPingTime(last_ping, rlz_lib::TOOLBAR_NOTIFIER);
  193. // No events, last ping just over a day ago.
  194. EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER));
  195. EXPECT_FALSE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  196. false));
  197. // Has events, last ping just over a day ago.
  198. EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER,
  199. rlz_lib::IE_DEFAULT_SEARCH, rlz_lib::SET_TO_GOOGLE));
  200. EXPECT_TRUE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  201. false));
  202. // Has events, last ping just under a day ago.
  203. last_ping = now - rlz_lib::kEventsPingInterval + k1MinuteInterval;
  204. SetLastPingTime(last_ping, rlz_lib::TOOLBAR_NOTIFIER);
  205. EXPECT_FALSE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  206. false));
  207. EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER));
  208. // No events, last ping just under a week ago.
  209. last_ping = now - rlz_lib::kNoEventsPingInterval + k1MinuteInterval;
  210. SetLastPingTime(last_ping, rlz_lib::TOOLBAR_NOTIFIER);
  211. EXPECT_FALSE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  212. false));
  213. // No events, last ping just over a week ago.
  214. last_ping = now - rlz_lib::kNoEventsPingInterval - k1MinuteInterval;
  215. SetLastPingTime(last_ping, rlz_lib::TOOLBAR_NOTIFIER);
  216. EXPECT_TRUE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  217. false));
  218. // Last ping was in future (invalid).
  219. last_ping = now + k1MinuteInterval;
  220. SetLastPingTime(last_ping, rlz_lib::TOOLBAR_NOTIFIER);
  221. EXPECT_TRUE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  222. false));
  223. EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER,
  224. rlz_lib::IE_DEFAULT_SEARCH, rlz_lib::SET_TO_GOOGLE));
  225. EXPECT_TRUE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  226. false));
  227. }
  228. TEST_F(FinancialPingTest, BrandingIsPingTime) {
  229. // Don't run these tests if a supplementary brand is already in place. That
  230. // way we can control the branding.
  231. if (!rlz_lib::SupplementaryBranding::GetBrand().empty())
  232. return;
  233. int64_t now = rlz_lib::GetSystemTimeAsInt64();
  234. int64_t last_ping = now - rlz_lib::kEventsPingInterval - k1MinuteInterval;
  235. SetLastPingTime(last_ping, rlz_lib::TOOLBAR_NOTIFIER);
  236. // Has events, last ping just over a day ago.
  237. EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER,
  238. rlz_lib::IE_DEFAULT_SEARCH, rlz_lib::SET_TO_GOOGLE));
  239. EXPECT_TRUE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  240. false));
  241. {
  242. rlz_lib::SupplementaryBranding branding("TEST");
  243. SetLastPingTime(last_ping, rlz_lib::TOOLBAR_NOTIFIER);
  244. // Has events, last ping just over a day ago.
  245. EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER,
  246. rlz_lib::IE_DEFAULT_SEARCH, rlz_lib::SET_TO_GOOGLE));
  247. EXPECT_TRUE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  248. false));
  249. }
  250. last_ping = now - k1MinuteInterval;
  251. SetLastPingTime(last_ping, rlz_lib::TOOLBAR_NOTIFIER);
  252. EXPECT_FALSE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  253. false));
  254. {
  255. rlz_lib::SupplementaryBranding branding("TEST");
  256. EXPECT_TRUE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  257. false));
  258. }
  259. }
  260. TEST_F(FinancialPingTest, ClearLastPingTime) {
  261. int64_t now = rlz_lib::GetSystemTimeAsInt64();
  262. int64_t last_ping = now - rlz_lib::kEventsPingInterval + k1MinuteInterval;
  263. SetLastPingTime(last_ping, rlz_lib::TOOLBAR_NOTIFIER);
  264. // Has events, last ping just under a day ago.
  265. EXPECT_TRUE(rlz_lib::ClearAllProductEvents(rlz_lib::TOOLBAR_NOTIFIER));
  266. EXPECT_TRUE(rlz_lib::RecordProductEvent(rlz_lib::TOOLBAR_NOTIFIER,
  267. rlz_lib::IE_DEFAULT_SEARCH, rlz_lib::SET_TO_GOOGLE));
  268. EXPECT_FALSE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  269. false));
  270. EXPECT_TRUE(rlz_lib::FinancialPing::ClearLastPingTime(
  271. rlz_lib::TOOLBAR_NOTIFIER));
  272. EXPECT_TRUE(rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER,
  273. false));
  274. }
  275. #if BUILDFLAG(IS_CHROMEOS_ASH)
  276. TEST_F(FinancialPingTest, RlzEmbargoEndDate) {
  277. // Do not set last ping time, verify that |IsPingTime| returns true.
  278. EXPECT_TRUE(
  279. rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER, false));
  280. // Simulate writing a past embargo date to VPD, verify that |IsPingTime|
  281. // returns true when the embargo date has already passed.
  282. base::Time::Exploded exploded;
  283. base::Time past_rlz_embargo_date =
  284. base::Time::NowFromSystemTime() - base::Days(1);
  285. past_rlz_embargo_date.LocalExplode(&exploded);
  286. std::string past_rlz_embargo_date_value =
  287. ConvertExplodedToRlzEmbargoDate(exploded);
  288. statistics_provider_->SetMachineStatistic(
  289. chromeos::system::kRlzEmbargoEndDateKey, past_rlz_embargo_date_value);
  290. EXPECT_TRUE(
  291. rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER, false));
  292. // Simulate writing a future embargo date (less than
  293. // |kEmbargoEndDateGarbageDateThresholdDays|) to VPD, verify that
  294. // |IsPingTime| is false.
  295. base::Time future_rlz_embargo_date =
  296. base::Time::NowFromSystemTime() +
  297. chromeos::system::kEmbargoEndDateGarbageDateThreshold - base::Days(1);
  298. future_rlz_embargo_date.LocalExplode(&exploded);
  299. std::string future_rlz_embargo_date_value =
  300. ConvertExplodedToRlzEmbargoDate(exploded);
  301. statistics_provider_->SetMachineStatistic(
  302. chromeos::system::kRlzEmbargoEndDateKey, future_rlz_embargo_date_value);
  303. EXPECT_FALSE(
  304. rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER, false));
  305. // Simulate writing a future embargo date (more than
  306. // |kEmbargoEndDateGarbageDateThresholdDays|) to VPD, verify that
  307. // |IsPingTime| is true.
  308. future_rlz_embargo_date =
  309. base::Time::NowFromSystemTime() +
  310. chromeos::system::kEmbargoEndDateGarbageDateThreshold + base::Days(1);
  311. future_rlz_embargo_date.LocalExplode(&exploded);
  312. future_rlz_embargo_date_value = ConvertExplodedToRlzEmbargoDate(exploded);
  313. statistics_provider_->SetMachineStatistic(
  314. chromeos::system::kRlzEmbargoEndDateKey, future_rlz_embargo_date_value);
  315. EXPECT_TRUE(
  316. rlz_lib::FinancialPing::IsPingTime(rlz_lib::TOOLBAR_NOTIFIER, false));
  317. }
  318. #endif