instance_id_driver_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Copyright 2015 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/instance_id/instance_id_driver.h"
  5. #include <stddef.h>
  6. #include <cmath>
  7. #include <memory>
  8. #include "base/bind.h"
  9. #include "base/run_loop.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/test/task_environment.h"
  12. #include "base/time/time.h"
  13. #include "components/gcm_driver/gcm_buildflags.h"
  14. #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h"
  15. #include "components/gcm_driver/instance_id/instance_id.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. #if BUILDFLAG(USE_GCM_FROM_PLATFORM)
  18. #include "components/gcm_driver/instance_id/instance_id_android.h"
  19. #include "components/gcm_driver/instance_id/scoped_use_fake_instance_id_android.h"
  20. #endif // BUILDFLAG(USE_GCM_FROM_PLATFORM)
  21. namespace instance_id {
  22. namespace {
  23. const char kTestAppID1[] = "TestApp1";
  24. const char kTestAppID2[] = "TestApp2";
  25. const char kAuthorizedEntity1[] = "Sender 1";
  26. const char kAuthorizedEntity2[] = "Sender 2";
  27. const char kScope1[] = "GCM1";
  28. const char kScope2[] = "FooBar";
  29. bool VerifyInstanceID(const std::string& str) {
  30. // Checks the length.
  31. if (str.length() != static_cast<size_t>(
  32. std::ceil(InstanceID::kInstanceIDByteLength * 8 / 6.0)))
  33. return false;
  34. // Checks if it is URL-safe base64 encoded.
  35. for (auto ch : str) {
  36. if (!base::IsAsciiAlpha(ch) && !base::IsAsciiDigit(ch) &&
  37. ch != '_' && ch != '-')
  38. return false;
  39. }
  40. return true;
  41. }
  42. } // namespace
  43. class InstanceIDDriverTest : public testing::Test {
  44. public:
  45. InstanceIDDriverTest();
  46. InstanceIDDriverTest(const InstanceIDDriverTest&) = delete;
  47. InstanceIDDriverTest& operator=(const InstanceIDDriverTest&) = delete;
  48. ~InstanceIDDriverTest() override;
  49. // testing::Test:
  50. void SetUp() override;
  51. void TearDown() override;
  52. void WaitForAsyncOperation();
  53. // Recreates InstanceIDDriver to simulate restart.
  54. void RecreateInstanceIDDriver();
  55. // Sync wrappers for async version.
  56. std::string GetID(InstanceID* instance_id);
  57. base::Time GetCreationTime(InstanceID* instance_id);
  58. InstanceID::Result DeleteID(InstanceID* instance_id);
  59. std::string GetToken(InstanceID* instance_id,
  60. const std::string& authorized_entity,
  61. const std::string& scope);
  62. InstanceID::Result DeleteToken(
  63. InstanceID* instance_id,
  64. const std::string& authorized_entity,
  65. const std::string& scope);
  66. InstanceIDDriver* driver() const { return driver_.get(); }
  67. private:
  68. void GetIDCompleted(const std::string& id);
  69. void GetCreationTimeCompleted(const base::Time& creation_time);
  70. void DeleteIDCompleted(InstanceID::Result result);
  71. void GetTokenCompleted(const std::string& token, InstanceID::Result result);
  72. void DeleteTokenCompleted(InstanceID::Result result);
  73. base::test::SingleThreadTaskEnvironment task_environment_;
  74. std::unique_ptr<FakeGCMDriverForInstanceID> gcm_driver_;
  75. std::unique_ptr<InstanceIDDriver> driver_;
  76. #if BUILDFLAG(USE_GCM_FROM_PLATFORM)
  77. InstanceIDAndroid::ScopedBlockOnAsyncTasksForTesting block_async_;
  78. ScopedUseFakeInstanceIDAndroid use_fake_;
  79. #endif // BUILDFLAG(USE_GCM_FROM_PLATFORM)
  80. std::string id_;
  81. base::Time creation_time_;
  82. std::string token_;
  83. InstanceID::Result result_;
  84. bool async_operation_completed_;
  85. base::OnceClosure async_operation_completed_callback_;
  86. };
  87. InstanceIDDriverTest::InstanceIDDriverTest()
  88. : task_environment_(
  89. base::test::SingleThreadTaskEnvironment::MainThreadType::UI),
  90. result_(InstanceID::UNKNOWN_ERROR),
  91. async_operation_completed_(false) {}
  92. InstanceIDDriverTest::~InstanceIDDriverTest() {
  93. }
  94. void InstanceIDDriverTest::SetUp() {
  95. gcm_driver_ = std::make_unique<FakeGCMDriverForInstanceID>();
  96. RecreateInstanceIDDriver();
  97. }
  98. void InstanceIDDriverTest::TearDown() {
  99. driver_.reset();
  100. gcm_driver_.reset();
  101. // |gcm_driver_| owns a GCMKeyStore that owns a ProtoDatabase whose
  102. // destructor deletes the underlying LevelDB on the task runner.
  103. base::RunLoop().RunUntilIdle();
  104. }
  105. void InstanceIDDriverTest::RecreateInstanceIDDriver() {
  106. driver_ = std::make_unique<InstanceIDDriver>(gcm_driver_.get());
  107. }
  108. void InstanceIDDriverTest::WaitForAsyncOperation() {
  109. // No need to wait if async operation is not needed.
  110. if (async_operation_completed_)
  111. return;
  112. base::RunLoop run_loop;
  113. async_operation_completed_callback_ = run_loop.QuitClosure();
  114. run_loop.Run();
  115. }
  116. std::string InstanceIDDriverTest::GetID(InstanceID* instance_id) {
  117. async_operation_completed_ = false;
  118. id_.clear();
  119. instance_id->GetID(base::BindOnce(&InstanceIDDriverTest::GetIDCompleted,
  120. base::Unretained(this)));
  121. WaitForAsyncOperation();
  122. return id_;
  123. }
  124. base::Time InstanceIDDriverTest::GetCreationTime(InstanceID* instance_id) {
  125. async_operation_completed_ = false;
  126. creation_time_ = base::Time();
  127. instance_id->GetCreationTime(base::BindOnce(
  128. &InstanceIDDriverTest::GetCreationTimeCompleted, base::Unretained(this)));
  129. WaitForAsyncOperation();
  130. return creation_time_;
  131. }
  132. InstanceID::Result InstanceIDDriverTest::DeleteID(InstanceID* instance_id) {
  133. async_operation_completed_ = false;
  134. result_ = InstanceID::UNKNOWN_ERROR;
  135. instance_id->DeleteID(base::BindOnce(&InstanceIDDriverTest::DeleteIDCompleted,
  136. base::Unretained(this)));
  137. WaitForAsyncOperation();
  138. return result_;
  139. }
  140. std::string InstanceIDDriverTest::GetToken(InstanceID* instance_id,
  141. const std::string& authorized_entity,
  142. const std::string& scope) {
  143. async_operation_completed_ = false;
  144. token_.clear();
  145. result_ = InstanceID::UNKNOWN_ERROR;
  146. instance_id->GetToken(
  147. authorized_entity, scope, /*time_to_live=*/base::TimeDelta(),
  148. /*flags=*/{},
  149. base::BindRepeating(&InstanceIDDriverTest::GetTokenCompleted,
  150. base::Unretained(this)));
  151. WaitForAsyncOperation();
  152. return token_;
  153. }
  154. InstanceID::Result InstanceIDDriverTest::DeleteToken(
  155. InstanceID* instance_id,
  156. const std::string& authorized_entity,
  157. const std::string& scope) {
  158. async_operation_completed_ = false;
  159. result_ = InstanceID::UNKNOWN_ERROR;
  160. instance_id->DeleteToken(
  161. authorized_entity, scope,
  162. base::BindOnce(&InstanceIDDriverTest::DeleteTokenCompleted,
  163. base::Unretained(this)));
  164. WaitForAsyncOperation();
  165. return result_;
  166. }
  167. void InstanceIDDriverTest::GetIDCompleted(const std::string& id) {
  168. DCHECK(!async_operation_completed_);
  169. async_operation_completed_ = true;
  170. id_ = id;
  171. if (async_operation_completed_callback_)
  172. std::move(async_operation_completed_callback_).Run();
  173. }
  174. void InstanceIDDriverTest::GetCreationTimeCompleted(
  175. const base::Time& creation_time) {
  176. DCHECK(!async_operation_completed_);
  177. async_operation_completed_ = true;
  178. creation_time_ = creation_time;
  179. if (async_operation_completed_callback_)
  180. std::move(async_operation_completed_callback_).Run();
  181. }
  182. void InstanceIDDriverTest::DeleteIDCompleted(InstanceID::Result result) {
  183. DCHECK(!async_operation_completed_);
  184. async_operation_completed_ = true;
  185. result_ = result;
  186. if (async_operation_completed_callback_)
  187. std::move(async_operation_completed_callback_).Run();
  188. }
  189. void InstanceIDDriverTest::GetTokenCompleted(
  190. const std::string& token, InstanceID::Result result) {
  191. DCHECK(!async_operation_completed_);
  192. async_operation_completed_ = true;
  193. token_ = token;
  194. result_ = result;
  195. if (async_operation_completed_callback_)
  196. std::move(async_operation_completed_callback_).Run();
  197. }
  198. void InstanceIDDriverTest::DeleteTokenCompleted(InstanceID::Result result) {
  199. DCHECK(!async_operation_completed_);
  200. async_operation_completed_ = true;
  201. result_ = result;
  202. if (async_operation_completed_callback_)
  203. std::move(async_operation_completed_callback_).Run();
  204. }
  205. TEST_F(InstanceIDDriverTest, GetAndRemoveInstanceID) {
  206. EXPECT_FALSE(driver()->ExistsInstanceID(kTestAppID1));
  207. InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
  208. EXPECT_TRUE(instance_id);
  209. EXPECT_TRUE(driver()->ExistsInstanceID(kTestAppID1));
  210. driver()->RemoveInstanceID(kTestAppID1);
  211. EXPECT_FALSE(driver()->ExistsInstanceID(kTestAppID1));
  212. }
  213. TEST_F(InstanceIDDriverTest, NewID) {
  214. // Creation time should not be set when the ID is not created.
  215. InstanceID* instance_id1 = driver()->GetInstanceID(kTestAppID1);
  216. EXPECT_TRUE(GetCreationTime(instance_id1).is_null());
  217. // New ID is generated for the first time.
  218. std::string id1 = GetID(instance_id1);
  219. EXPECT_TRUE(VerifyInstanceID(id1));
  220. base::Time creation_time = GetCreationTime(instance_id1);
  221. EXPECT_FALSE(creation_time.is_null());
  222. // Same ID is returned for the same app.
  223. EXPECT_EQ(id1, GetID(instance_id1));
  224. EXPECT_EQ(creation_time, GetCreationTime(instance_id1));
  225. // New ID is generated for another app.
  226. InstanceID* instance_id2 = driver()->GetInstanceID(kTestAppID2);
  227. std::string id2 = GetID(instance_id2);
  228. EXPECT_TRUE(VerifyInstanceID(id2));
  229. EXPECT_NE(id1, id2);
  230. EXPECT_FALSE(GetCreationTime(instance_id2).is_null());
  231. }
  232. TEST_F(InstanceIDDriverTest, PersistID) {
  233. InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
  234. // Create the ID for the first time. The ID and creation time should be saved
  235. // to the store.
  236. std::string id = GetID(instance_id);
  237. EXPECT_FALSE(id.empty());
  238. base::Time creation_time = GetCreationTime(instance_id);
  239. EXPECT_FALSE(creation_time.is_null());
  240. // Simulate restart by recreating InstanceIDDriver. Same ID and creation time
  241. // should be expected.
  242. RecreateInstanceIDDriver();
  243. instance_id = driver()->GetInstanceID(kTestAppID1);
  244. EXPECT_EQ(creation_time, GetCreationTime(instance_id));
  245. EXPECT_EQ(id, GetID(instance_id));
  246. // Delete the ID. The ID and creation time should be removed from the store.
  247. EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id));
  248. EXPECT_TRUE(GetCreationTime(instance_id).is_null());
  249. // Simulate restart by recreating InstanceIDDriver. Different ID should be
  250. // expected.
  251. // Note that we do not check for different creation time since the test might
  252. // be run at a very fast server.
  253. RecreateInstanceIDDriver();
  254. instance_id = driver()->GetInstanceID(kTestAppID1);
  255. EXPECT_NE(id, GetID(instance_id));
  256. }
  257. TEST_F(InstanceIDDriverTest, DeleteID) {
  258. InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
  259. std::string id1 = GetID(instance_id);
  260. EXPECT_FALSE(id1.empty());
  261. EXPECT_FALSE(GetCreationTime(instance_id).is_null());
  262. // New ID will be generated from GetID after calling DeleteID.
  263. EXPECT_EQ(InstanceID::SUCCESS, DeleteID(instance_id));
  264. EXPECT_TRUE(GetCreationTime(instance_id).is_null());
  265. std::string id2 = GetID(instance_id);
  266. EXPECT_FALSE(id2.empty());
  267. EXPECT_NE(id1, id2);
  268. EXPECT_FALSE(GetCreationTime(instance_id).is_null());
  269. }
  270. TEST_F(InstanceIDDriverTest, GetToken) {
  271. InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
  272. std::string token1 = GetToken(instance_id, kAuthorizedEntity1, kScope1);
  273. EXPECT_FALSE(token1.empty());
  274. // Same token is returned for same authorized entity and scope.
  275. EXPECT_EQ(token1, GetToken(instance_id, kAuthorizedEntity1, kScope1));
  276. // Different token is returned for different authorized entity or scope.
  277. std::string token2 = GetToken(instance_id, kAuthorizedEntity1, kScope2);
  278. EXPECT_FALSE(token2.empty());
  279. EXPECT_NE(token1, token2);
  280. std::string token3 = GetToken(instance_id, kAuthorizedEntity2, kScope1);
  281. EXPECT_FALSE(token3.empty());
  282. EXPECT_NE(token1, token3);
  283. EXPECT_NE(token2, token3);
  284. }
  285. TEST_F(InstanceIDDriverTest, DeleteToken) {
  286. InstanceID* instance_id = driver()->GetInstanceID(kTestAppID1);
  287. // Gets 2 tokens.
  288. std::string token1 = GetToken(instance_id, kAuthorizedEntity1, kScope1);
  289. EXPECT_FALSE(token1.empty());
  290. std::string token2 = GetToken(instance_id, kAuthorizedEntity2, kScope1);
  291. EXPECT_FALSE(token1.empty());
  292. EXPECT_NE(token1, token2);
  293. // Different token is returned for same authorized entity and scope after
  294. // deletion.
  295. EXPECT_EQ(InstanceID::SUCCESS,
  296. DeleteToken(instance_id, kAuthorizedEntity1, kScope1));
  297. std::string new_token1 = GetToken(instance_id, kAuthorizedEntity1, kScope2);
  298. EXPECT_FALSE(new_token1.empty());
  299. EXPECT_NE(token1, new_token1);
  300. // The other token is not affected by the deletion.
  301. EXPECT_EQ(token2, GetToken(instance_id, kAuthorizedEntity2, kScope1));
  302. }
  303. } // namespace instance_id