OLD | NEW |
---|---|
(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 } | |
sacomoto
2015/07/10 10:54:14
The current behavior when |CryptAuthGCMManagerImpl
Tim Song
2015/07/10 20:52:04
This is the intended result. It should still repor
| |
133 | |
134 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, | |
135 RegistrationFailsThenSucceeds) { | |
136 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); | |
137 RegisterWithGCM(gcm::GCMClient::NETWORK_ERROR); | |
138 EXPECT_EQ(std::string(), gcm_manager_.GetRegistrationId()); | |
139 RegisterWithGCM(gcm::GCMClient::SUCCESS); | |
140 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId()); | |
141 } | |
142 | |
143 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ConcurrentRegistrations) { | |
144 // If multiple RegisterWithGCM() calls are made concurrently, only one | |
145 // registration attempt should actually be made. | |
146 EXPECT_CALL(gcm_driver_, RegisterImpl(kCryptAuthGCMAppId, _)); | |
147 gcm_manager_.RegisterWithGCM(); | |
148 gcm_manager_.RegisterWithGCM(); | |
149 gcm_manager_.RegisterWithGCM(); | |
150 | |
151 EXPECT_CALL(*this, OnGCMRegistrationResultProxy(true)); | |
152 gcm_driver_.RegisterFinished(kCryptAuthGCMAppId, kNewGCMRegistrationId, | |
153 gcm::GCMClient::SUCCESS); | |
154 EXPECT_EQ(kNewGCMRegistrationId, gcm_manager_.GetRegistrationId()); | |
155 } | |
156 | |
157 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ReenrollmentMessagesReceived) { | |
158 EXPECT_CALL(*this, OnReenrollMessageProxy()).Times(2); | |
159 | |
160 gcm::GCMClient::IncomingMessage message; | |
161 message.data["registrationTickleType"] = "1"; // FORCE_ENROLLMENT | |
162 message.collapse_key = kCryptAuthMessageCollapseKey; | |
163 message.sender_id = kCryptAuthGCMSenderId; | |
164 | |
165 gcm::GCMAppHandler* gcm_app_handler = | |
166 static_cast<gcm::GCMAppHandler*>(&gcm_manager_); | |
167 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); | |
168 message.data["registrationTickleType"] = "2"; // UPDATE_ENROLLMENT | |
169 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); | |
170 } | |
171 | |
172 TEST_F(ProximityAuthCryptAuthGCMManagerImplTest, ResyncMessagesReceived) { | |
173 EXPECT_CALL(*this, OnResyncMessageProxy()).Times(2); | |
174 | |
175 gcm::GCMClient::IncomingMessage message; | |
176 message.data["registrationTickleType"] = "3"; // DEVICES_SYNC | |
177 message.collapse_key = kCryptAuthMessageCollapseKey; | |
178 message.sender_id = kCryptAuthGCMSenderId; | |
179 | |
180 gcm::GCMAppHandler* gcm_app_handler = | |
181 static_cast<gcm::GCMAppHandler*>(&gcm_manager_); | |
182 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); | |
183 gcm_app_handler->OnMessage(kCryptAuthGCMAppId, message); | |
184 } | |
185 | |
186 } // namespace proximity_auth | |
OLD | NEW |