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