Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl_unittest.cc

Issue 1228763002: Introduce CryptAuthGCMManager, which handles GCM interactions for CryptAuth. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #include "components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.h"
6
7 #include "base/prefs/testing_pref_service.h"
8 #include "components/gcm_driver/fake_gcm_driver.h"
9 #include "components/gcm_driver/gcm_client.h"
10 #include "components/proximity_auth/cryptauth/pref_names.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using ::testing::_;
15 using ::testing::SaveArg;
16
17 namespace proximity_auth {
18
19 namespace {
20
21 const char kCryptAuthGCMAppId[] = "com.google.chrome.cryptauth";
22 const char kCryptAuthGCMSenderId[] = "381449029288";
23 const char kExistingGCMRegistrationId[] = "cirrus";
24 const char kNewGCMRegistrationId[] = "stratus";
25 const char kCryptAuthMessageCollapseKey[] =
26 "collapse_cryptauth_sync_DEVICES_SYNC";
27
28 // Mock GCMDriver implementation for testing.
29 class MockGCMDriver : public gcm::FakeGCMDriver {
30 public:
31 MockGCMDriver() {}
32 ~MockGCMDriver() override {}
33
34 MOCK_METHOD2(AddAppHandler,
35 void(const std::string& app_id, gcm::GCMAppHandler* handler));
36
37 MOCK_METHOD2(RegisterImpl,
38 void(const std::string& app_id,
39 const std::vector<std::string>& sender_ids));
40
41 using gcm::GCMDriver::RegisterFinished;
42
43 private:
44 DISALLOW_COPY_AND_ASSIGN(MockGCMDriver);
45 };
46
47 } // namespace
48
49 class ProximityAuthCryptAuthGCMManagerImplTest
50 : public testing::Test,
51 public CryptAuthGCMManager::Observer {
52 protected:
53 ProximityAuthCryptAuthGCMManagerImplTest()
54 : gcm_manager_(&gcm_driver_, &pref_service_) {}
55
56 // testing::Test:
57 void SetUp() override {
58 CryptAuthGCMManager::RegisterPrefs(pref_service_.registry());
59 gcm_manager_.AddObserver(this);
60 EXPECT_CALL(gcm_driver_, AddAppHandler(kCryptAuthGCMAppId, &gcm_manager_));
61 gcm_manager_.StartListening();
62 }
63
64 void TearDown() override { gcm_manager_.RemoveObserver(this); }
65
66 void RegisterWithGCM(gcm::GCMClient::Result registration_result) {
67 std::vector<std::string> sender_ids;
68 EXPECT_CALL(gcm_driver_, RegisterImpl(kCryptAuthGCMAppId, _))
69 .WillOnce(SaveArg<1>(&sender_ids));
70 gcm_manager_.RegisterWithGCM();
71
72 ASSERT_EQ(1u, sender_ids.size());
73 EXPECT_EQ(kCryptAuthGCMSenderId, sender_ids[0]);
74
75 bool success = (registration_result == gcm::GCMClient::SUCCESS);
76 EXPECT_CALL(*this, OnGCMRegistrationResultProxy(success));
77 gcm_driver_.RegisterFinished(kCryptAuthGCMAppId, kNewGCMRegistrationId,
78 registration_result);
79 }
80
81 // CryptAuthGCMManager::Observer:
82 void OnGCMRegistrationResult(bool success) override {
83 OnGCMRegistrationResultProxy(success);
84 }
85
86 void OnReenrollMessage() override { OnReenrollMessageProxy(); }
87
88 void OnResyncMessage() override { OnResyncMessageProxy(); }
89
90 MOCK_METHOD1(OnGCMRegistrationResultProxy, void(bool));
91 MOCK_METHOD0(OnReenrollMessageProxy, void());
92 MOCK_METHOD0(OnResyncMessageProxy, void());
93
94 testing::StrictMock<MockGCMDriver> gcm_driver_;
95
96 TestingPrefServiceSimple pref_service_;
97
98 CryptAuthGCMManagerImpl gcm_manager_;
99
100 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthGCMManagerImplTest);
101 };
102
103 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, RegisterPrefs) {
104 TestingPrefServiceSimple pref_service;
105 CryptAuthGCMManager::RegisterPrefs(pref_service.registry());
106 EXPECT_TRUE(pref_service.FindPreference(prefs::kCryptAuthGCMRegistrationId));
107 }
108
109 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, RegistrationSucceeds) {
110 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId());
111 RegisterWithGCM(gcm::GCMClient::SUCCESS);
112 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId());
113 }
114
115 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest,
116 RegistrationSucceedsWithExistingRegistration) {
117 pref_service_.SetString(prefs::kCryptAuthGCMRegistrationId,
118 kExistingGCMRegistrationId);
119 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId());
120 RegisterWithGCM(gcm::GCMClient::SUCCESS);
121 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId());
122 EXPECT_EQ(kNewGCMRegistrationId,
123 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId));
124 }
125
126 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, RegisterWithGCMFails) {
127 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId());
128 RegisterWithGCM(gcm::GCMClient::SERVER_ERROR);
129 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId());
130 EXPECT_EQ(std::string(),
131 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId));
132 }
133
134 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest,
135 RegisterWithGCMFailsWithExistingRegistration) {
136 pref_service_.SetString(prefs::kCryptAuthGCMRegistrationId,
137 kExistingGCMRegistrationId);
138 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId());
139 RegisterWithGCM(gcm::GCMClient::SERVER_ERROR);
140 EXPECT_EQ(kExistingGCMRegistrationId, gcm_manager_.GetRegistrationId());
141 EXPECT_EQ(kExistingGCMRegistrationId,
142 pref_service_.GetString(prefs::kCryptAuthGCMRegistrationId));
143 }
144
145 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest,
146 RegistrationFailsThenSucceeds) {
147 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId());
148 RegisterWithGCM(gcm::GCMClient::NETWORK_ERROR);
149 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId());
150 RegisterWithGCM(gcm::GCMClient::SUCCESS);
151 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId());
152 }
153
154 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ConcurrentRegistrations) {
155 // If multiple RegisterWithGCM() calls are made concurrently, only one
156 // registration attempt should actually be made.
157 EXPECT_CALL(gcm_driver_, RegisterImpl(kCryptAuthGCMAppId, _));
158 gcm_manager_.RegisterWithGCM();
159 gcm_manager_.RegisterWithGCM();
160 gcm_manager_.RegisterWithGCM();
161
162 EXPECT_CALL(*this, OnGCMRegistrationResultProxy(true));
163 gcm_driver_.RegisterFinished(kCryptAuthGCMAppId, kNewGCMRegistrationId,
164 gcm::GCMClient::SUCCESS);
165 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId());
166 }
167
168 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ReenrollmentMessagesReceived) {
169 EXPECT_CALL(*this, OnReenrollMessageProxy()).Times(2);
170
171 gcm::GCMClient::IncomingMessage message;
172 message.data["registrationTickleType"] = "1"; // FORCE_ENROLLMENT
173 message.collapse_key = kCryptAuthMessageCollapseKey;
174 message.sender_id = kCryptAuthGCMSenderId;
175
176 gcm::GCMAppHandler* gcm_app_handler =
177 static_cast<gcm::GCMAppHandler*>(&gcm_manager_);
178 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message);
179 message.data["registrationTickleType"] = "2"; // UPDATE_ENROLLMENT
180 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message);
181 }
182
183 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ResyncMessagesReceived) {
184 EXPECT_CALL(*this, OnResyncMessageProxy()).Times(2);
185
186 gcm::GCMClient::IncomingMessage message;
187 message.data["registrationTickleType"] = "3"; // DEVICES_SYNC
188 message.collapse_key = kCryptAuthMessageCollapseKey;
189 message.sender_id = kCryptAuthGCMSenderId;
190
191 gcm::GCMAppHandler* gcm_app_handler =
192 static_cast<gcm::GCMAppHandler*>(&gcm_manager_);
193 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message);
194 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message);
195 }
196
197 } // namespace proximity_auth
OLDNEW
« no previous file with comments | « components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.cc ('k') | components/proximity_auth/cryptauth/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698