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

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

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

Powered by Google App Engine
This is Rietveld 408576698