gcm_driver_desktop_unittest.cc 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. // Copyright 2014 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. #include "components/gcm_driver/gcm_driver_desktop.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include "base/bind.h"
  8. #include "base/callback_helpers.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/location.h"
  11. #include "base/run_loop.h"
  12. #include "base/strings/string_util.h"
  13. #include "base/task/current_thread.h"
  14. #include "base/test/task_environment.h"
  15. #include "base/test/test_simple_task_runner.h"
  16. #include "base/threading/thread.h"
  17. #include "base/threading/thread_task_runner_handle.h"
  18. #include "base/time/time.h"
  19. #include "components/gcm_driver/crypto/gcm_encryption_provider.h"
  20. #include "components/gcm_driver/fake_gcm_app_handler.h"
  21. #include "components/gcm_driver/fake_gcm_client.h"
  22. #include "components/gcm_driver/fake_gcm_client_factory.h"
  23. #include "components/gcm_driver/gcm_app_handler.h"
  24. #include "components/gcm_driver/gcm_client_factory.h"
  25. #include "components/gcm_driver/gcm_connection_observer.h"
  26. #include "components/prefs/pref_registry_simple.h"
  27. #include "components/prefs/testing_pref_service.h"
  28. #include "net/url_request/url_request_context_getter.h"
  29. #include "net/url_request/url_request_test_util.h"
  30. #include "services/network/public/cpp/shared_url_loader_factory.h"
  31. #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
  32. #include "services/network/test/test_network_connection_tracker.h"
  33. #include "services/network/test/test_url_loader_factory.h"
  34. #include "testing/gtest/include/gtest/gtest.h"
  35. namespace gcm {
  36. namespace {
  37. const char kTestAppID1[] = "TestApp1";
  38. const char kTestAppID2[] = "TestApp2";
  39. const char kUserID1[] = "user1";
  40. const char kScope[] = "GCM";
  41. const char kInstanceID1[] = "IID1";
  42. const char kInstanceID2[] = "IID2";
  43. class FakeGCMConnectionObserver : public GCMConnectionObserver {
  44. public:
  45. FakeGCMConnectionObserver();
  46. ~FakeGCMConnectionObserver() override;
  47. // gcm::GCMConnectionObserver implementation:
  48. void OnConnected(const net::IPEndPoint& ip_endpoint) override;
  49. void OnDisconnected() override;
  50. bool connected() const { return connected_; }
  51. private:
  52. bool connected_;
  53. };
  54. FakeGCMConnectionObserver::FakeGCMConnectionObserver() : connected_(false) {
  55. }
  56. FakeGCMConnectionObserver::~FakeGCMConnectionObserver() {
  57. }
  58. void FakeGCMConnectionObserver::OnConnected(
  59. const net::IPEndPoint& ip_endpoint) {
  60. connected_ = true;
  61. }
  62. void FakeGCMConnectionObserver::OnDisconnected() {
  63. connected_ = false;
  64. }
  65. void PumpCurrentLoop() {
  66. base::RunLoop(base::RunLoop::Type::kNestableTasksAllowed).RunUntilIdle();
  67. }
  68. void PumpUILoop() {
  69. PumpCurrentLoop();
  70. }
  71. std::vector<std::string> ToSenderList(const std::string& sender_ids) {
  72. return base::SplitString(
  73. sender_ids, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  74. }
  75. } // namespace
  76. class GCMDriverTest : public testing::Test {
  77. public:
  78. enum WaitToFinish {
  79. DO_NOT_WAIT,
  80. WAIT
  81. };
  82. GCMDriverTest();
  83. GCMDriverTest(const GCMDriverTest&) = delete;
  84. GCMDriverTest& operator=(const GCMDriverTest&) = delete;
  85. ~GCMDriverTest() override;
  86. // testing::Test:
  87. void SetUp() override;
  88. void TearDown() override;
  89. GCMDriverDesktop* driver() { return driver_.get(); }
  90. FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); }
  91. FakeGCMConnectionObserver* gcm_connection_observer() {
  92. return gcm_connection_observer_.get();
  93. }
  94. const std::string& registration_id() const { return registration_id_; }
  95. GCMClient::Result registration_result() const { return registration_result_; }
  96. const std::string& send_message_id() const { return send_message_id_; }
  97. GCMClient::Result send_result() const { return send_result_; }
  98. GCMClient::Result unregistration_result() const {
  99. return unregistration_result_;
  100. }
  101. const std::string& p256dh() const { return p256dh_; }
  102. const std::string& auth_secret() const { return auth_secret_; }
  103. void PumpIOLoop();
  104. void ClearResults();
  105. bool HasAppHandlers() const;
  106. FakeGCMClient* GetGCMClient();
  107. void CreateDriver();
  108. void ShutdownDriver();
  109. void AddAppHandlers();
  110. void RemoveAppHandlers();
  111. void Register(const std::string& app_id,
  112. const std::vector<std::string>& sender_ids,
  113. WaitToFinish wait_to_finish);
  114. void Send(const std::string& app_id,
  115. const std::string& receiver_id,
  116. const OutgoingMessage& message,
  117. WaitToFinish wait_to_finish);
  118. void GetEncryptionInfo(const std::string& app_id,
  119. WaitToFinish wait_to_finish);
  120. void Unregister(const std::string& app_id, WaitToFinish wait_to_finish);
  121. void WaitForAsyncOperation();
  122. void RegisterCompleted(const std::string& registration_id,
  123. GCMClient::Result result);
  124. void SendCompleted(const std::string& message_id, GCMClient::Result result);
  125. void GetEncryptionInfoCompleted(std::string p256dh, std::string auth_secret);
  126. void UnregisterCompleted(GCMClient::Result result);
  127. void AsyncOperationCompleted() {
  128. if (async_operation_completed_callback_)
  129. std::move(async_operation_completed_callback_).Run();
  130. }
  131. void set_async_operation_completed_callback(base::OnceClosure callback) {
  132. async_operation_completed_callback_ = std::move(callback);
  133. }
  134. private:
  135. base::ScopedTempDir temp_dir_;
  136. TestingPrefServiceSimple prefs_;
  137. base::test::SingleThreadTaskEnvironment task_environment_{
  138. base::test::SingleThreadTaskEnvironment::MainThreadType::UI};
  139. base::Thread io_thread_;
  140. network::TestURLLoaderFactory test_url_loader_factory_;
  141. std::unique_ptr<GCMDriverDesktop> driver_;
  142. std::unique_ptr<FakeGCMAppHandler> gcm_app_handler_;
  143. std::unique_ptr<FakeGCMConnectionObserver> gcm_connection_observer_;
  144. base::OnceClosure async_operation_completed_callback_;
  145. std::string registration_id_;
  146. GCMClient::Result registration_result_;
  147. std::string send_message_id_;
  148. GCMClient::Result send_result_;
  149. GCMClient::Result unregistration_result_;
  150. std::string p256dh_;
  151. std::string auth_secret_;
  152. };
  153. GCMDriverTest::GCMDriverTest()
  154. : io_thread_("IOThread"),
  155. registration_result_(GCMClient::UNKNOWN_ERROR),
  156. send_result_(GCMClient::UNKNOWN_ERROR),
  157. unregistration_result_(GCMClient::UNKNOWN_ERROR) {}
  158. GCMDriverTest::~GCMDriverTest() {
  159. }
  160. void GCMDriverTest::SetUp() {
  161. io_thread_.Start();
  162. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  163. }
  164. void GCMDriverTest::TearDown() {
  165. if (!driver_)
  166. return;
  167. ShutdownDriver();
  168. driver_.reset();
  169. PumpIOLoop();
  170. io_thread_.Stop();
  171. task_environment_.RunUntilIdle();
  172. ASSERT_TRUE(temp_dir_.Delete());
  173. }
  174. void GCMDriverTest::PumpIOLoop() {
  175. base::RunLoop run_loop;
  176. io_thread_.task_runner()->PostTaskAndReply(
  177. FROM_HERE, base::BindOnce(&PumpCurrentLoop), run_loop.QuitClosure());
  178. run_loop.Run();
  179. }
  180. void GCMDriverTest::ClearResults() {
  181. registration_id_.clear();
  182. registration_result_ = GCMClient::UNKNOWN_ERROR;
  183. send_message_id_.clear();
  184. send_result_ = GCMClient::UNKNOWN_ERROR;
  185. unregistration_result_ = GCMClient::UNKNOWN_ERROR;
  186. }
  187. bool GCMDriverTest::HasAppHandlers() const {
  188. return !driver_->app_handlers().empty();
  189. }
  190. FakeGCMClient* GCMDriverTest::GetGCMClient() {
  191. return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting());
  192. }
  193. void GCMDriverTest::CreateDriver() {
  194. GCMClient::ChromeBuildInfo chrome_build_info;
  195. chrome_build_info.product_category_for_subtypes = "com.chrome.macosx";
  196. driver_ = std::make_unique<GCMDriverDesktop>(
  197. std::unique_ptr<GCMClientFactory>(new FakeGCMClientFactory(
  198. base::ThreadTaskRunnerHandle::Get(), io_thread_.task_runner())),
  199. chrome_build_info, &prefs_, temp_dir_.GetPath(),
  200. /*remove_account_mappings_with_email_key=*/true, base::DoNothing(),
  201. base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
  202. &test_url_loader_factory_),
  203. network::TestNetworkConnectionTracker::GetInstance(),
  204. base::ThreadTaskRunnerHandle::Get(), io_thread_.task_runner(),
  205. task_environment_.GetMainThreadTaskRunner());
  206. gcm_app_handler_ = std::make_unique<FakeGCMAppHandler>();
  207. gcm_connection_observer_ = std::make_unique<FakeGCMConnectionObserver>();
  208. driver_->AddConnectionObserver(gcm_connection_observer_.get());
  209. }
  210. void GCMDriverTest::ShutdownDriver() {
  211. if (gcm_connection_observer())
  212. driver()->RemoveConnectionObserver(gcm_connection_observer());
  213. driver()->Shutdown();
  214. }
  215. void GCMDriverTest::AddAppHandlers() {
  216. driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get());
  217. driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get());
  218. }
  219. void GCMDriverTest::RemoveAppHandlers() {
  220. driver_->RemoveAppHandler(kTestAppID1);
  221. driver_->RemoveAppHandler(kTestAppID2);
  222. }
  223. void GCMDriverTest::Register(const std::string& app_id,
  224. const std::vector<std::string>& sender_ids,
  225. WaitToFinish wait_to_finish) {
  226. base::RunLoop run_loop;
  227. async_operation_completed_callback_ = run_loop.QuitClosure();
  228. driver_->Register(app_id, sender_ids,
  229. base::BindOnce(&GCMDriverTest::RegisterCompleted,
  230. base::Unretained(this)));
  231. if (wait_to_finish == WAIT)
  232. run_loop.Run();
  233. }
  234. void GCMDriverTest::Send(const std::string& app_id,
  235. const std::string& receiver_id,
  236. const OutgoingMessage& message,
  237. WaitToFinish wait_to_finish) {
  238. base::RunLoop run_loop;
  239. async_operation_completed_callback_ = run_loop.QuitClosure();
  240. driver_->Send(
  241. app_id, receiver_id, message,
  242. base::BindOnce(&GCMDriverTest::SendCompleted, base::Unretained(this)));
  243. if (wait_to_finish == WAIT)
  244. run_loop.Run();
  245. }
  246. void GCMDriverTest::GetEncryptionInfo(const std::string& app_id,
  247. WaitToFinish wait_to_finish) {
  248. base::RunLoop run_loop;
  249. async_operation_completed_callback_ = run_loop.QuitClosure();
  250. driver_->GetEncryptionInfo(
  251. app_id, base::BindOnce(&GCMDriverTest::GetEncryptionInfoCompleted,
  252. base::Unretained(this)));
  253. if (wait_to_finish == WAIT)
  254. run_loop.Run();
  255. }
  256. void GCMDriverTest::Unregister(const std::string& app_id,
  257. WaitToFinish wait_to_finish) {
  258. base::RunLoop run_loop;
  259. async_operation_completed_callback_ = run_loop.QuitClosure();
  260. driver_->Unregister(app_id,
  261. base::BindOnce(&GCMDriverTest::UnregisterCompleted,
  262. base::Unretained(this)));
  263. if (wait_to_finish == WAIT)
  264. run_loop.Run();
  265. }
  266. void GCMDriverTest::WaitForAsyncOperation() {
  267. base::RunLoop run_loop;
  268. async_operation_completed_callback_ = run_loop.QuitClosure();
  269. run_loop.Run();
  270. }
  271. void GCMDriverTest::RegisterCompleted(const std::string& registration_id,
  272. GCMClient::Result result) {
  273. registration_id_ = registration_id;
  274. registration_result_ = result;
  275. AsyncOperationCompleted();
  276. }
  277. void GCMDriverTest::SendCompleted(const std::string& message_id,
  278. GCMClient::Result result) {
  279. send_message_id_ = message_id;
  280. send_result_ = result;
  281. AsyncOperationCompleted();
  282. }
  283. void GCMDriverTest::GetEncryptionInfoCompleted(std::string p256dh,
  284. std::string auth_secret) {
  285. p256dh_ = std::move(p256dh);
  286. auth_secret_ = std::move(auth_secret);
  287. AsyncOperationCompleted();
  288. }
  289. void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) {
  290. unregistration_result_ = result;
  291. AsyncOperationCompleted();
  292. }
  293. TEST_F(GCMDriverTest, Create) {
  294. // Create GCMDriver first. By default GCM is set to delay start.
  295. CreateDriver();
  296. EXPECT_FALSE(driver()->IsStarted());
  297. // Adding an app handler will not start GCM.
  298. AddAppHandlers();
  299. PumpIOLoop();
  300. PumpUILoop();
  301. EXPECT_FALSE(driver()->IsStarted());
  302. EXPECT_FALSE(driver()->IsConnected());
  303. EXPECT_FALSE(gcm_connection_observer()->connected());
  304. // The GCM registration will kick off the GCM.
  305. Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
  306. EXPECT_TRUE(driver()->IsStarted());
  307. EXPECT_TRUE(driver()->IsConnected());
  308. EXPECT_TRUE(gcm_connection_observer()->connected());
  309. }
  310. TEST_F(GCMDriverTest, Shutdown) {
  311. CreateDriver();
  312. EXPECT_FALSE(HasAppHandlers());
  313. AddAppHandlers();
  314. EXPECT_TRUE(HasAppHandlers());
  315. ShutdownDriver();
  316. EXPECT_FALSE(HasAppHandlers());
  317. EXPECT_FALSE(driver()->IsConnected());
  318. EXPECT_FALSE(gcm_connection_observer()->connected());
  319. }
  320. TEST_F(GCMDriverTest, StartOrStopGCMOnDemand) {
  321. CreateDriver();
  322. PumpIOLoop();
  323. PumpUILoop();
  324. EXPECT_FALSE(driver()->IsStarted());
  325. // Adding an app handler will not start GCM.
  326. driver()->AddAppHandler(kTestAppID1, gcm_app_handler());
  327. PumpIOLoop();
  328. PumpUILoop();
  329. EXPECT_FALSE(driver()->IsStarted());
  330. // The GCM registration will kick off the GCM.
  331. Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
  332. EXPECT_TRUE(driver()->IsStarted());
  333. // Add another app handler.
  334. driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
  335. PumpIOLoop();
  336. PumpUILoop();
  337. EXPECT_TRUE(driver()->IsStarted());
  338. // GCMClient remains active after one app handler is gone.
  339. driver()->RemoveAppHandler(kTestAppID1);
  340. PumpIOLoop();
  341. PumpUILoop();
  342. EXPECT_TRUE(driver()->IsStarted());
  343. // GCMClient should be stopped after the last app handler is gone.
  344. driver()->RemoveAppHandler(kTestAppID2);
  345. PumpIOLoop();
  346. PumpUILoop();
  347. EXPECT_FALSE(driver()->IsStarted());
  348. // GCMClient is restarted after an app handler has been added.
  349. driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
  350. PumpIOLoop();
  351. PumpUILoop();
  352. EXPECT_TRUE(driver()->IsStarted());
  353. }
  354. TEST_F(GCMDriverTest, RegisterFailed) {
  355. std::vector<std::string> sender_ids;
  356. sender_ids.push_back("sender1");
  357. CreateDriver();
  358. // Registration fails when the no app handler is added.
  359. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  360. EXPECT_TRUE(registration_id().empty());
  361. EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
  362. }
  363. TEST_F(GCMDriverTest, UnregisterFailed) {
  364. CreateDriver();
  365. // Unregistration fails when the no app handler is added.
  366. Unregister(kTestAppID1, GCMDriverTest::WAIT);
  367. EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result());
  368. }
  369. TEST_F(GCMDriverTest, SendFailed) {
  370. OutgoingMessage message;
  371. message.id = "1";
  372. message.data["key1"] = "value1";
  373. CreateDriver();
  374. // Sending fails when the no app handler is added.
  375. Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
  376. EXPECT_TRUE(send_message_id().empty());
  377. EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
  378. }
  379. TEST_F(GCMDriverTest, DISABLED_GCMClientNotReadyBeforeRegistration) {
  380. CreateDriver();
  381. PumpIOLoop();
  382. PumpUILoop();
  383. // Make GCMClient not ready until PerformDelayedStart is called.
  384. GetGCMClient()->set_start_mode_overridding(
  385. FakeGCMClient::FORCE_TO_ALWAYS_DELAY_START_GCM);
  386. AddAppHandlers();
  387. // The registration is on hold until GCMClient is ready.
  388. std::vector<std::string> sender_ids;
  389. sender_ids.push_back("sender1");
  390. Register(kTestAppID1,
  391. sender_ids,
  392. GCMDriverTest::DO_NOT_WAIT);
  393. PumpIOLoop();
  394. PumpUILoop();
  395. EXPECT_TRUE(registration_id().empty());
  396. EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
  397. // Register operation will be invoked after GCMClient becomes ready.
  398. GetGCMClient()->PerformDelayedStart();
  399. WaitForAsyncOperation();
  400. EXPECT_FALSE(registration_id().empty());
  401. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  402. }
  403. TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) {
  404. CreateDriver();
  405. PumpIOLoop();
  406. PumpUILoop();
  407. // Make GCMClient not ready until PerformDelayedStart is called.
  408. GetGCMClient()->set_start_mode_overridding(
  409. FakeGCMClient::FORCE_TO_ALWAYS_DELAY_START_GCM);
  410. AddAppHandlers();
  411. // The sending is on hold until GCMClient is ready.
  412. OutgoingMessage message;
  413. message.id = "1";
  414. message.data["key1"] = "value1";
  415. message.data["key2"] = "value2";
  416. Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT);
  417. PumpIOLoop();
  418. PumpUILoop();
  419. EXPECT_TRUE(send_message_id().empty());
  420. EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
  421. // Send operation will be invoked after GCMClient becomes ready.
  422. GetGCMClient()->PerformDelayedStart();
  423. WaitForAsyncOperation();
  424. EXPECT_EQ(message.id, send_message_id());
  425. EXPECT_EQ(GCMClient::SUCCESS, send_result());
  426. }
  427. // Tests a single instance of GCMDriver.
  428. class GCMDriverFunctionalTest : public GCMDriverTest {
  429. public:
  430. GCMDriverFunctionalTest();
  431. GCMDriverFunctionalTest(const GCMDriverFunctionalTest&) = delete;
  432. GCMDriverFunctionalTest& operator=(const GCMDriverFunctionalTest&) = delete;
  433. ~GCMDriverFunctionalTest() override;
  434. // GCMDriverTest:
  435. void SetUp() override;
  436. };
  437. GCMDriverFunctionalTest::GCMDriverFunctionalTest() {
  438. }
  439. GCMDriverFunctionalTest::~GCMDriverFunctionalTest() {
  440. }
  441. void GCMDriverFunctionalTest::SetUp() {
  442. GCMDriverTest::SetUp();
  443. CreateDriver();
  444. AddAppHandlers();
  445. PumpIOLoop();
  446. PumpUILoop();
  447. }
  448. TEST_F(GCMDriverFunctionalTest, DISABLED_Register) {
  449. std::vector<std::string> sender_ids;
  450. sender_ids.push_back("sender1");
  451. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  452. const std::string expected_registration_id =
  453. FakeGCMClient::GenerateGCMRegistrationID(sender_ids);
  454. EXPECT_EQ(expected_registration_id, registration_id());
  455. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  456. }
  457. // This test is flaky, see https://crbug.com/1010462
  458. TEST_F(GCMDriverFunctionalTest, DISABLED_RegisterError) {
  459. std::vector<std::string> sender_ids;
  460. sender_ids.push_back("sender1@error");
  461. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  462. EXPECT_TRUE(registration_id().empty());
  463. EXPECT_NE(GCMClient::SUCCESS, registration_result());
  464. }
  465. // This test is flaky, see https://crbug.com/1010462
  466. TEST_F(GCMDriverFunctionalTest, DISABLED_RegisterAgainWithSameSenderIDs) {
  467. std::vector<std::string> sender_ids;
  468. sender_ids.push_back("sender1");
  469. sender_ids.push_back("sender2");
  470. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  471. const std::string expected_registration_id =
  472. FakeGCMClient::GenerateGCMRegistrationID(sender_ids);
  473. EXPECT_EQ(expected_registration_id, registration_id());
  474. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  475. // Clears the results the would be set by the Register callback in preparation
  476. // to call register 2nd time.
  477. ClearResults();
  478. // Calling register 2nd time with the same set of sender IDs but different
  479. // ordering will get back the same registration ID.
  480. std::vector<std::string> another_sender_ids;
  481. another_sender_ids.push_back("sender2");
  482. another_sender_ids.push_back("sender1");
  483. Register(kTestAppID1, another_sender_ids, GCMDriverTest::WAIT);
  484. EXPECT_EQ(expected_registration_id, registration_id());
  485. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  486. }
  487. // This test is flaky, see https://crbug.com/1010462
  488. TEST_F(GCMDriverFunctionalTest, DISABLED_RegisterAgainWithDifferentSenderIDs) {
  489. std::vector<std::string> sender_ids;
  490. sender_ids.push_back("sender1");
  491. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  492. const std::string expected_registration_id =
  493. FakeGCMClient::GenerateGCMRegistrationID(sender_ids);
  494. EXPECT_EQ(expected_registration_id, registration_id());
  495. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  496. // Make sender IDs different.
  497. sender_ids.push_back("sender2");
  498. const std::string expected_registration_id2 =
  499. FakeGCMClient::GenerateGCMRegistrationID(sender_ids);
  500. // Calling register 2nd time with the different sender IDs will get back a new
  501. // registration ID.
  502. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  503. EXPECT_EQ(expected_registration_id2, registration_id());
  504. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  505. }
  506. TEST_F(GCMDriverFunctionalTest, UnregisterExplicitly) {
  507. std::vector<std::string> sender_ids;
  508. sender_ids.push_back("sender1");
  509. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  510. EXPECT_FALSE(registration_id().empty());
  511. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  512. Unregister(kTestAppID1, GCMDriverTest::WAIT);
  513. EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
  514. }
  515. // TODO(crbug.com/1009185): Test is failing on ASan build.
  516. #if defined(ADDRESS_SANITIZER)
  517. TEST_F(GCMDriverFunctionalTest, DISABLED_UnregisterRemovesEncryptionInfo) {
  518. #else
  519. TEST_F(GCMDriverFunctionalTest, UnregisterRemovesEncryptionInfo) {
  520. #endif
  521. std::vector<std::string> sender_ids;
  522. sender_ids.push_back("sender1");
  523. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  524. EXPECT_FALSE(registration_id().empty());
  525. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  526. GetEncryptionInfo(kTestAppID1, GCMDriverTest::WAIT);
  527. EXPECT_FALSE(p256dh().empty());
  528. EXPECT_FALSE(auth_secret().empty());
  529. const std::string app_p256dh = p256dh();
  530. const std::string app_auth_secret = auth_secret();
  531. GetEncryptionInfo(kTestAppID1, GCMDriverTest::WAIT);
  532. EXPECT_EQ(app_p256dh, p256dh());
  533. EXPECT_EQ(app_auth_secret, auth_secret());
  534. Unregister(kTestAppID1, GCMDriverTest::WAIT);
  535. EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
  536. GetEncryptionInfo(kTestAppID1, GCMDriverTest::WAIT);
  537. // The GCMKeyStore eagerly creates new keying material for registrations that
  538. // don't have any associated with them, so the most appropriate check to do is
  539. // to verify that the returned material is different from before.
  540. EXPECT_NE(app_p256dh, p256dh());
  541. EXPECT_NE(app_auth_secret, auth_secret());
  542. }
  543. TEST_F(GCMDriverFunctionalTest, DISABLED_UnregisterWhenAsyncOperationPending) {
  544. std::vector<std::string> sender_ids;
  545. sender_ids.push_back("sender1");
  546. // First start registration without waiting for it to complete.
  547. Register(kTestAppID1, sender_ids, GCMDriverTest::DO_NOT_WAIT);
  548. // Test that unregistration fails with async operation pending when there is a
  549. // registration already in progress.
  550. Unregister(kTestAppID1, GCMDriverTest::WAIT);
  551. EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
  552. unregistration_result());
  553. // Complete the unregistration.
  554. WaitForAsyncOperation();
  555. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  556. // Start unregistration without waiting for it to complete. This time no async
  557. // operation is pending.
  558. Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
  559. // Test that unregistration fails with async operation pending when there is
  560. // an unregistration already in progress.
  561. Unregister(kTestAppID1, GCMDriverTest::WAIT);
  562. EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
  563. unregistration_result());
  564. ClearResults();
  565. // Complete unregistration.
  566. WaitForAsyncOperation();
  567. EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
  568. }
  569. TEST_F(GCMDriverFunctionalTest, RegisterWhenAsyncOperationPending) {
  570. std::vector<std::string> sender_ids;
  571. sender_ids.push_back("sender1");
  572. // First start registration without waiting for it to complete.
  573. Register(kTestAppID1, sender_ids, GCMDriverTest::DO_NOT_WAIT);
  574. // Test that registration fails with async operation pending when there is a
  575. // registration already in progress.
  576. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  577. EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
  578. registration_result());
  579. ClearResults();
  580. // Complete the registration.
  581. WaitForAsyncOperation();
  582. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  583. }
  584. // This test is flaky, see https://crbug.com/1010462
  585. TEST_F(GCMDriverFunctionalTest, DISABLED_RegisterAfterUnfinishedUnregister) {
  586. // Register and wait for it to complete.
  587. std::vector<std::string> sender_ids;
  588. sender_ids.push_back("sender1");
  589. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  590. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  591. EXPECT_EQ(FakeGCMClient::GenerateGCMRegistrationID(sender_ids),
  592. registration_id());
  593. // Clears the results the would be set by the Register callback in preparation
  594. // to call register 2nd time.
  595. ClearResults();
  596. // Start unregistration without waiting for it to complete.
  597. Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
  598. // Register immediately after unregistration is not completed.
  599. sender_ids.push_back("sender2");
  600. Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
  601. // We need one more waiting since the waiting in Register is indeed for
  602. // uncompleted Unregister.
  603. WaitForAsyncOperation();
  604. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  605. EXPECT_EQ(FakeGCMClient::GenerateGCMRegistrationID(sender_ids),
  606. registration_id());
  607. }
  608. TEST_F(GCMDriverFunctionalTest, Send) {
  609. OutgoingMessage message;
  610. message.id = "1@ack";
  611. message.data["key1"] = "value1";
  612. message.data["key2"] = "value2";
  613. Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
  614. EXPECT_EQ(message.id, send_message_id());
  615. EXPECT_EQ(GCMClient::SUCCESS, send_result());
  616. gcm_app_handler()->WaitForNotification();
  617. EXPECT_EQ(message.id, gcm_app_handler()->acked_message_id());
  618. EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
  619. }
  620. TEST_F(GCMDriverFunctionalTest, SendError) {
  621. OutgoingMessage message;
  622. // Embedding error in id will tell the mock to simulate the send error.
  623. message.id = "1@error";
  624. message.data["key1"] = "value1";
  625. message.data["key2"] = "value2";
  626. Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
  627. EXPECT_EQ(message.id, send_message_id());
  628. EXPECT_EQ(GCMClient::SUCCESS, send_result());
  629. // Wait for the send error.
  630. gcm_app_handler()->WaitForNotification();
  631. EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT,
  632. gcm_app_handler()->received_event());
  633. EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
  634. EXPECT_EQ(message.id,
  635. gcm_app_handler()->send_error_details().message_id);
  636. EXPECT_NE(GCMClient::SUCCESS,
  637. gcm_app_handler()->send_error_details().result);
  638. EXPECT_EQ(message.data,
  639. gcm_app_handler()->send_error_details().additional_data);
  640. }
  641. // This test is flaky, see https://crbug.com/1010462
  642. TEST_F(GCMDriverFunctionalTest, DISABLED_MessageReceived) {
  643. // GCM registration has to be performed otherwise GCM will not be started.
  644. Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
  645. IncomingMessage message;
  646. message.data["key1"] = "value1";
  647. message.data["key2"] = "value2";
  648. message.sender_id = "sender";
  649. GetGCMClient()->ReceiveMessage(kTestAppID1, message);
  650. gcm_app_handler()->WaitForNotification();
  651. EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
  652. gcm_app_handler()->received_event());
  653. EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
  654. EXPECT_EQ(message.data, gcm_app_handler()->message().data);
  655. EXPECT_TRUE(gcm_app_handler()->message().collapse_key.empty());
  656. EXPECT_EQ(message.sender_id, gcm_app_handler()->message().sender_id);
  657. }
  658. // This test is flaky, see https://crbug.com/1010462
  659. TEST_F(GCMDriverFunctionalTest, DISABLED_MessageWithCollapseKeyReceived) {
  660. // GCM registration has to be performed otherwise GCM will not be started.
  661. Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
  662. IncomingMessage message;
  663. message.data["key1"] = "value1";
  664. message.collapse_key = "collapse_key_value";
  665. message.sender_id = "sender";
  666. GetGCMClient()->ReceiveMessage(kTestAppID1, message);
  667. gcm_app_handler()->WaitForNotification();
  668. EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
  669. gcm_app_handler()->received_event());
  670. EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
  671. EXPECT_EQ(message.data, gcm_app_handler()->message().data);
  672. EXPECT_EQ(message.collapse_key,
  673. gcm_app_handler()->message().collapse_key);
  674. }
  675. TEST_F(GCMDriverFunctionalTest, EncryptedMessageReceivedError) {
  676. // GCM registration has to be performed otherwise GCM will not be started.
  677. Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
  678. IncomingMessage message;
  679. // All required information to trigger the encryption path, but with an
  680. // invalid Crypto-Key header value to trigger an error.
  681. message.data["encryption"] = "salt=ysyxqlYTgE0WvcZrmHbUbg";
  682. message.data["crypto-key"] = "hey=thereisnopublickey";
  683. message.sender_id = "sender";
  684. message.raw_data = "foobar";
  685. GetGCMClient()->SetRecording(true);
  686. GetGCMClient()->ReceiveMessage(kTestAppID1, message);
  687. PumpIOLoop();
  688. PumpUILoop();
  689. PumpIOLoop();
  690. EXPECT_EQ(FakeGCMAppHandler::DECRYPTION_FAILED_EVENT,
  691. gcm_app_handler()->received_event());
  692. GCMClient::GCMStatistics statistics = GetGCMClient()->GetStatistics();
  693. EXPECT_TRUE(statistics.is_recording);
  694. EXPECT_EQ(
  695. 1u, statistics.recorded_activities.decryption_failure_activities.size());
  696. }
  697. TEST_F(GCMDriverFunctionalTest, MessagesDeleted) {
  698. // GCM registration has to be performed otherwise GCM will not be started.
  699. Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
  700. GetGCMClient()->DeleteMessages(kTestAppID1);
  701. gcm_app_handler()->WaitForNotification();
  702. EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
  703. gcm_app_handler()->received_event());
  704. EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
  705. }
  706. TEST_F(GCMDriverFunctionalTest, LastTokenFetchTime) {
  707. // GCM registration has to be performed otherwise GCM will not be started.
  708. Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
  709. EXPECT_EQ(base::Time(), driver()->GetLastTokenFetchTime());
  710. base::Time fetch_time = base::Time::Now();
  711. driver()->SetLastTokenFetchTime(fetch_time);
  712. EXPECT_EQ(fetch_time, driver()->GetLastTokenFetchTime());
  713. }
  714. class GCMDriverInstanceIDTest : public GCMDriverTest {
  715. public:
  716. GCMDriverInstanceIDTest();
  717. GCMDriverInstanceIDTest(const GCMDriverInstanceIDTest&) = delete;
  718. GCMDriverInstanceIDTest& operator=(const GCMDriverInstanceIDTest&) = delete;
  719. ~GCMDriverInstanceIDTest() override;
  720. void GetReady();
  721. void GetInstanceID(const std::string& app_id, WaitToFinish wait_to_finish);
  722. void GetInstanceIDDataCompleted(const std::string& instance_id,
  723. const std::string& extra_data);
  724. void GetToken(const std::string& app_id,
  725. const std::string& authorized_entity,
  726. const std::string& scope,
  727. WaitToFinish wait_to_finish);
  728. void DeleteToken(const std::string& app_id,
  729. const std::string& authorized_entity,
  730. const std::string& scope,
  731. WaitToFinish wait_to_finish);
  732. void AddInstanceIDData(const std::string& app_id,
  733. const std::string& instance_id,
  734. const std::string& extra_data);
  735. void RemoveInstanceIDData(const std::string& app_id);
  736. std::string instance_id() const { return instance_id_; }
  737. std::string extra_data() const { return extra_data_; }
  738. int instance_id_resolved_counter() const {
  739. return instance_id_resolved_counter_;
  740. }
  741. private:
  742. std::string instance_id_;
  743. std::string extra_data_;
  744. int instance_id_resolved_counter_ = 0;
  745. };
  746. GCMDriverInstanceIDTest::GCMDriverInstanceIDTest() {
  747. }
  748. GCMDriverInstanceIDTest::~GCMDriverInstanceIDTest() {
  749. }
  750. void GCMDriverInstanceIDTest::GetReady() {
  751. CreateDriver();
  752. AddAppHandlers();
  753. PumpIOLoop();
  754. PumpUILoop();
  755. }
  756. void GCMDriverInstanceIDTest::GetInstanceID(const std::string& app_id,
  757. WaitToFinish wait_to_finish) {
  758. base::RunLoop run_loop;
  759. set_async_operation_completed_callback(run_loop.QuitClosure());
  760. driver()->GetInstanceIDHandlerInternal()->GetInstanceIDData(
  761. app_id,
  762. base::BindOnce(&GCMDriverInstanceIDTest::GetInstanceIDDataCompleted,
  763. base::Unretained(this)));
  764. if (wait_to_finish == WAIT)
  765. run_loop.Run();
  766. }
  767. void GCMDriverInstanceIDTest::GetInstanceIDDataCompleted(
  768. const std::string& instance_id, const std::string& extra_data) {
  769. instance_id_ = instance_id;
  770. extra_data_ = extra_data;
  771. instance_id_resolved_counter_++;
  772. AsyncOperationCompleted();
  773. }
  774. void GCMDriverInstanceIDTest::GetToken(const std::string& app_id,
  775. const std::string& authorized_entity,
  776. const std::string& scope,
  777. WaitToFinish wait_to_finish) {
  778. base::RunLoop run_loop;
  779. set_async_operation_completed_callback(run_loop.QuitClosure());
  780. driver()->GetInstanceIDHandlerInternal()->GetToken(
  781. app_id, authorized_entity, scope, /*time_to_live=*/base::TimeDelta(),
  782. base::BindOnce(&GCMDriverTest::RegisterCompleted,
  783. base::Unretained(this)));
  784. if (wait_to_finish == WAIT)
  785. run_loop.Run();
  786. }
  787. void GCMDriverInstanceIDTest::DeleteToken(const std::string& app_id,
  788. const std::string& authorized_entity,
  789. const std::string& scope,
  790. WaitToFinish wait_to_finish) {
  791. base::RunLoop run_loop;
  792. set_async_operation_completed_callback(run_loop.QuitClosure());
  793. driver()->GetInstanceIDHandlerInternal()->DeleteToken(
  794. app_id, authorized_entity, scope,
  795. base::BindOnce(&GCMDriverTest::UnregisterCompleted,
  796. base::Unretained(this)));
  797. if (wait_to_finish == WAIT)
  798. run_loop.Run();
  799. }
  800. void GCMDriverInstanceIDTest::AddInstanceIDData(const std::string& app_id,
  801. const std::string& instance_id,
  802. const std::string& extra_data) {
  803. driver()->GetInstanceIDHandlerInternal()->AddInstanceIDData(
  804. app_id, instance_id, extra_data);
  805. }
  806. void GCMDriverInstanceIDTest::RemoveInstanceIDData(const std::string& app_id) {
  807. driver()->GetInstanceIDHandlerInternal()->RemoveInstanceIDData(app_id);
  808. }
  809. TEST_F(GCMDriverInstanceIDTest, InstanceIDData) {
  810. GetReady();
  811. AddInstanceIDData(kTestAppID1, kInstanceID1, "Foo");
  812. GetInstanceID(kTestAppID1, GCMDriverTest::WAIT);
  813. EXPECT_EQ(kInstanceID1, instance_id());
  814. EXPECT_EQ("Foo", extra_data());
  815. EXPECT_EQ(1, instance_id_resolved_counter());
  816. RemoveInstanceIDData(kTestAppID1);
  817. GetInstanceID(kTestAppID1, GCMDriverTest::WAIT);
  818. EXPECT_TRUE(instance_id().empty());
  819. EXPECT_TRUE(extra_data().empty());
  820. EXPECT_EQ(2, instance_id_resolved_counter());
  821. AddInstanceIDData(kTestAppID1, kInstanceID1, "Bar");
  822. GetInstanceID(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
  823. GetInstanceID(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
  824. WaitForAsyncOperation();
  825. WaitForAsyncOperation();
  826. EXPECT_EQ(kInstanceID1, instance_id());
  827. EXPECT_EQ("Bar", extra_data());
  828. EXPECT_EQ(4, instance_id_resolved_counter());
  829. }
  830. // This test is flaky, see https://crbug.com/1010462
  831. TEST_F(GCMDriverInstanceIDTest,
  832. DISABLED_GCMClientNotReadyBeforeInstanceIDData) {
  833. CreateDriver();
  834. PumpIOLoop();
  835. PumpUILoop();
  836. // Make GCMClient not ready until PerformDelayedStart is called.
  837. GetGCMClient()->set_start_mode_overridding(
  838. FakeGCMClient::FORCE_TO_ALWAYS_DELAY_START_GCM);
  839. AddAppHandlers();
  840. // All operations are on hold until GCMClient is ready.
  841. AddInstanceIDData(kTestAppID1, kInstanceID1, "Foo");
  842. AddInstanceIDData(kTestAppID2, kInstanceID2, "Bar");
  843. RemoveInstanceIDData(kTestAppID1);
  844. GetInstanceID(kTestAppID2, GCMDriverTest::DO_NOT_WAIT);
  845. PumpIOLoop();
  846. PumpUILoop();
  847. EXPECT_TRUE(instance_id().empty());
  848. EXPECT_TRUE(extra_data().empty());
  849. // All operations will be performed after GCMClient becomes ready.
  850. GetGCMClient()->PerformDelayedStart();
  851. WaitForAsyncOperation();
  852. EXPECT_EQ(kInstanceID2, instance_id());
  853. EXPECT_EQ("Bar", extra_data());
  854. }
  855. TEST_F(GCMDriverInstanceIDTest, DISABLED_GetToken) {
  856. GetReady();
  857. const std::string expected_token =
  858. FakeGCMClient::GenerateInstanceIDToken(kUserID1, kScope);
  859. GetToken(kTestAppID1, kUserID1, kScope, GCMDriverTest::WAIT);
  860. EXPECT_EQ(expected_token, registration_id());
  861. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  862. }
  863. TEST_F(GCMDriverInstanceIDTest, GetTokenError) {
  864. GetReady();
  865. std::string error_entity = "sender@error";
  866. GetToken(kTestAppID1, error_entity, kScope, GCMDriverTest::WAIT);
  867. EXPECT_TRUE(registration_id().empty());
  868. EXPECT_NE(GCMClient::SUCCESS, registration_result());
  869. }
  870. TEST_F(GCMDriverInstanceIDTest, GCMClientNotReadyBeforeGetToken) {
  871. CreateDriver();
  872. PumpIOLoop();
  873. PumpUILoop();
  874. // Make GCMClient not ready until PerformDelayedStart is called.
  875. GetGCMClient()->set_start_mode_overridding(
  876. FakeGCMClient::FORCE_TO_ALWAYS_DELAY_START_GCM);
  877. AddAppHandlers();
  878. // GetToken operation is on hold until GCMClient is ready.
  879. GetToken(kTestAppID1, kUserID1, kScope, GCMDriverTest::DO_NOT_WAIT);
  880. PumpIOLoop();
  881. PumpUILoop();
  882. EXPECT_TRUE(registration_id().empty());
  883. EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
  884. // GetToken operation will be invoked after GCMClient becomes ready.
  885. GetGCMClient()->PerformDelayedStart();
  886. WaitForAsyncOperation();
  887. EXPECT_FALSE(registration_id().empty());
  888. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  889. }
  890. TEST_F(GCMDriverInstanceIDTest, DeleteToken) {
  891. GetReady();
  892. const std::string expected_token =
  893. FakeGCMClient::GenerateInstanceIDToken(kUserID1, kScope);
  894. GetToken(kTestAppID1, kUserID1, kScope, GCMDriverTest::WAIT);
  895. EXPECT_EQ(expected_token, registration_id());
  896. EXPECT_EQ(GCMClient::SUCCESS, registration_result());
  897. DeleteToken(kTestAppID1, kUserID1, kScope, GCMDriverTest::WAIT);
  898. EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
  899. }
  900. TEST_F(GCMDriverInstanceIDTest, GCMClientNotReadyBeforeDeleteToken) {
  901. CreateDriver();
  902. PumpIOLoop();
  903. PumpUILoop();
  904. // Make GCMClient not ready until PerformDelayedStart is called.
  905. GetGCMClient()->set_start_mode_overridding(
  906. FakeGCMClient::FORCE_TO_ALWAYS_DELAY_START_GCM);
  907. AddAppHandlers();
  908. // DeleteToken operation is on hold until GCMClient is ready.
  909. DeleteToken(kTestAppID1, kUserID1, kScope, GCMDriverTest::DO_NOT_WAIT);
  910. PumpIOLoop();
  911. PumpUILoop();
  912. EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result());
  913. // DeleteToken operation will be invoked after GCMClient becomes ready.
  914. GetGCMClient()->PerformDelayedStart();
  915. WaitForAsyncOperation();
  916. EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
  917. }
  918. } // namespace gcm