Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / protocol / pairing_registry_unittest.cc
blobf0aa2510a9a7c66374639c545740cc9217aa2bc6
1 // Copyright (c) 2012 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.
5 #include "remoting/protocol/pairing_registry.h"
7 #include <stdlib.h>
9 #include <algorithm>
11 #include "base/bind.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "base/values.h"
18 #include "remoting/protocol/protocol_mock_objects.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using testing::Sequence;
24 namespace {
26 using remoting::protocol::PairingRegistry;
28 class MockPairingRegistryCallbacks {
29 public:
30 MockPairingRegistryCallbacks() {}
31 virtual ~MockPairingRegistryCallbacks() {}
33 MOCK_METHOD1(DoneCallback, void(bool));
34 MOCK_METHOD1(GetAllPairingsCallbackPtr, void(base::ListValue*));
35 MOCK_METHOD1(GetPairingCallback, void(PairingRegistry::Pairing));
37 void GetAllPairingsCallback(scoped_ptr<base::ListValue> pairings) {
38 GetAllPairingsCallbackPtr(pairings.get());
41 private:
42 DISALLOW_COPY_AND_ASSIGN(MockPairingRegistryCallbacks);
45 // Verify that a pairing Dictionary has correct entries, but doesn't include
46 // any shared secret.
47 void VerifyPairing(PairingRegistry::Pairing expected,
48 const base::DictionaryValue& actual) {
49 std::string value;
50 EXPECT_TRUE(actual.GetString(PairingRegistry::kClientNameKey, &value));
51 EXPECT_EQ(expected.client_name(), value);
52 EXPECT_TRUE(actual.GetString(PairingRegistry::kClientIdKey, &value));
53 EXPECT_EQ(expected.client_id(), value);
55 EXPECT_FALSE(actual.HasKey(PairingRegistry::kSharedSecretKey));
58 } // namespace
60 namespace remoting {
61 namespace protocol {
63 class PairingRegistryTest : public testing::Test {
64 public:
65 void SetUp() override { callback_count_ = 0; }
67 void set_pairings(scoped_ptr<base::ListValue> pairings) {
68 pairings_ = pairings.Pass();
71 void ExpectSecret(const std::string& expected,
72 PairingRegistry::Pairing actual) {
73 EXPECT_EQ(expected, actual.shared_secret());
74 ++callback_count_;
77 void ExpectSaveSuccess(bool success) {
78 EXPECT_TRUE(success);
79 ++callback_count_;
82 protected:
83 base::MessageLoop message_loop_;
84 base::RunLoop run_loop_;
86 int callback_count_;
87 scoped_ptr<base::ListValue> pairings_;
90 TEST_F(PairingRegistryTest, CreateAndGetPairings) {
91 scoped_refptr<PairingRegistry> registry = new SynchronousPairingRegistry(
92 make_scoped_ptr(new MockPairingRegistryDelegate()));
93 PairingRegistry::Pairing pairing_1 = registry->CreatePairing("my_client");
94 PairingRegistry::Pairing pairing_2 = registry->CreatePairing("my_client");
96 EXPECT_NE(pairing_1.shared_secret(), pairing_2.shared_secret());
98 registry->GetPairing(pairing_1.client_id(),
99 base::Bind(&PairingRegistryTest::ExpectSecret,
100 base::Unretained(this),
101 pairing_1.shared_secret()));
102 EXPECT_EQ(1, callback_count_);
104 // Check that the second client is paired with a different shared secret.
105 registry->GetPairing(pairing_2.client_id(),
106 base::Bind(&PairingRegistryTest::ExpectSecret,
107 base::Unretained(this),
108 pairing_2.shared_secret()));
109 EXPECT_EQ(2, callback_count_);
112 TEST_F(PairingRegistryTest, GetAllPairings) {
113 scoped_refptr<PairingRegistry> registry = new SynchronousPairingRegistry(
114 make_scoped_ptr(new MockPairingRegistryDelegate()));
115 PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
116 PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
118 registry->GetAllPairings(
119 base::Bind(&PairingRegistryTest::set_pairings,
120 base::Unretained(this)));
122 ASSERT_EQ(2u, pairings_->GetSize());
123 const base::DictionaryValue* actual_pairing_1;
124 const base::DictionaryValue* actual_pairing_2;
125 ASSERT_TRUE(pairings_->GetDictionary(0, &actual_pairing_1));
126 ASSERT_TRUE(pairings_->GetDictionary(1, &actual_pairing_2));
128 // Ordering is not guaranteed, so swap if necessary.
129 std::string actual_client_id;
130 ASSERT_TRUE(actual_pairing_1->GetString(PairingRegistry::kClientIdKey,
131 &actual_client_id));
132 if (actual_client_id != pairing_1.client_id()) {
133 std::swap(actual_pairing_1, actual_pairing_2);
136 VerifyPairing(pairing_1, *actual_pairing_1);
137 VerifyPairing(pairing_2, *actual_pairing_2);
140 TEST_F(PairingRegistryTest, DeletePairing) {
141 scoped_refptr<PairingRegistry> registry = new SynchronousPairingRegistry(
142 make_scoped_ptr(new MockPairingRegistryDelegate()));
143 PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
144 PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
146 registry->DeletePairing(
147 pairing_1.client_id(),
148 base::Bind(&PairingRegistryTest::ExpectSaveSuccess,
149 base::Unretained(this)));
151 // Re-read the list, and verify it only has the pairing_2 client.
152 registry->GetAllPairings(
153 base::Bind(&PairingRegistryTest::set_pairings,
154 base::Unretained(this)));
156 ASSERT_EQ(1u, pairings_->GetSize());
157 const base::DictionaryValue* actual_pairing_2;
158 ASSERT_TRUE(pairings_->GetDictionary(0, &actual_pairing_2));
159 std::string actual_client_id;
160 ASSERT_TRUE(actual_pairing_2->GetString(PairingRegistry::kClientIdKey,
161 &actual_client_id));
162 EXPECT_EQ(pairing_2.client_id(), actual_client_id);
165 TEST_F(PairingRegistryTest, ClearAllPairings) {
166 scoped_refptr<PairingRegistry> registry = new SynchronousPairingRegistry(
167 make_scoped_ptr(new MockPairingRegistryDelegate()));
168 PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
169 PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
171 registry->ClearAllPairings(
172 base::Bind(&PairingRegistryTest::ExpectSaveSuccess,
173 base::Unretained(this)));
175 // Re-read the list, and verify it is empty.
176 registry->GetAllPairings(
177 base::Bind(&PairingRegistryTest::set_pairings,
178 base::Unretained(this)));
180 EXPECT_TRUE(pairings_->empty());
183 ACTION_P(QuitMessageLoop, callback) {
184 callback.Run();
187 MATCHER_P(EqualsClientName, client_name, "") {
188 return arg.client_name() == client_name;
191 MATCHER(NoPairings, "") {
192 return arg->empty();
195 TEST_F(PairingRegistryTest, SerializedRequests) {
196 MockPairingRegistryCallbacks callbacks;
197 Sequence s;
198 EXPECT_CALL(callbacks, GetPairingCallback(EqualsClientName("client1")))
199 .InSequence(s);
200 EXPECT_CALL(callbacks, GetPairingCallback(EqualsClientName("client2")))
201 .InSequence(s);
202 EXPECT_CALL(callbacks, DoneCallback(true))
203 .InSequence(s);
204 EXPECT_CALL(callbacks, GetPairingCallback(EqualsClientName("client1")))
205 .InSequence(s);
206 EXPECT_CALL(callbacks, GetPairingCallback(EqualsClientName("")))
207 .InSequence(s);
208 EXPECT_CALL(callbacks, DoneCallback(true))
209 .InSequence(s);
210 EXPECT_CALL(callbacks, GetAllPairingsCallbackPtr(NoPairings()))
211 .InSequence(s);
212 EXPECT_CALL(callbacks, GetPairingCallback(EqualsClientName("client3")))
213 .InSequence(s)
214 .WillOnce(QuitMessageLoop(run_loop_.QuitClosure()));
216 scoped_refptr<PairingRegistry> registry = new PairingRegistry(
217 base::ThreadTaskRunnerHandle::Get(),
218 make_scoped_ptr(new MockPairingRegistryDelegate()));
219 PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
220 PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
221 registry->GetPairing(
222 pairing_1.client_id(),
223 base::Bind(&MockPairingRegistryCallbacks::GetPairingCallback,
224 base::Unretained(&callbacks)));
225 registry->GetPairing(
226 pairing_2.client_id(),
227 base::Bind(&MockPairingRegistryCallbacks::GetPairingCallback,
228 base::Unretained(&callbacks)));
229 registry->DeletePairing(
230 pairing_2.client_id(),
231 base::Bind(&MockPairingRegistryCallbacks::DoneCallback,
232 base::Unretained(&callbacks)));
233 registry->GetPairing(
234 pairing_1.client_id(),
235 base::Bind(&MockPairingRegistryCallbacks::GetPairingCallback,
236 base::Unretained(&callbacks)));
237 registry->GetPairing(
238 pairing_2.client_id(),
239 base::Bind(&MockPairingRegistryCallbacks::GetPairingCallback,
240 base::Unretained(&callbacks)));
241 registry->ClearAllPairings(
242 base::Bind(&MockPairingRegistryCallbacks::DoneCallback,
243 base::Unretained(&callbacks)));
244 registry->GetAllPairings(
245 base::Bind(&MockPairingRegistryCallbacks::GetAllPairingsCallback,
246 base::Unretained(&callbacks)));
247 PairingRegistry::Pairing pairing_3 = registry->CreatePairing("client3");
248 registry->GetPairing(
249 pairing_3.client_id(),
250 base::Bind(&MockPairingRegistryCallbacks::GetPairingCallback,
251 base::Unretained(&callbacks)));
253 run_loop_.Run();
256 } // namespace protocol
257 } // namespace remoting