1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/protocol/pairing_registry.h"
#include <stdlib.h>
#include <algorithm>
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "remoting/protocol/protocol_mock_objects.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using remoting::protocol::PairingRegistry;
// Verify that a pairing Dictionary has correct entries, but doesn't include
// any shared secret.
void VerifyPairing(PairingRegistry::Pairing expected,
const base::DictionaryValue& actual) {
std::string value;
EXPECT_TRUE(actual.GetString(PairingRegistry::kClientNameKey, &value));
EXPECT_EQ(expected.client_name(), value);
EXPECT_TRUE(actual.GetString(PairingRegistry::kClientIdKey, &value));
EXPECT_EQ(expected.client_id(), value);
EXPECT_FALSE(actual.HasKey(PairingRegistry::kSharedSecretKey));
}
// SaveCallback that expects to be called with success = true.
void ExpectSaveSuccess(bool success) {
EXPECT_TRUE(success);
}
} // namespace
namespace remoting {
namespace protocol {
class PairingRegistryTest : public testing::Test {
public:
void CompareSecret(const std::string& expected,
PairingRegistry::Pairing actual) {
EXPECT_EQ(expected, actual.shared_secret());
secret_ = actual.shared_secret();
got_secret_ = true;
}
void set_pairings(scoped_ptr<base::ListValue> pairings) {
pairings_ = pairings.Pass();
}
protected:
std::string secret_;
bool got_secret_;
scoped_ptr<base::ListValue> pairings_;
};
TEST_F(PairingRegistryTest, CreateAndGetPairings) {
MockPairingRegistryDelegate* mock_delegate =
new MockPairingRegistryDelegate();
scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate);
scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass()));
PairingRegistry::Pairing pairing_1 = registry->CreatePairing("my_client");
mock_delegate->RunCallback();
PairingRegistry::Pairing pairing_2 = registry->CreatePairing("my_client");
mock_delegate->RunCallback();
registry->GetPairing(pairing_1.client_id(),
base::Bind(&PairingRegistryTest::CompareSecret,
base::Unretained(this),
pairing_1.shared_secret()));
got_secret_ = false;
mock_delegate->RunCallback();
EXPECT_TRUE(got_secret_);
std::string secret_1 = secret_;
// Check that the second client is paired with a different shared secret.
registry->GetPairing(pairing_2.client_id(),
base::Bind(&PairingRegistryTest::CompareSecret,
base::Unretained(this),
pairing_2.shared_secret()));
got_secret_ = false;
mock_delegate->RunCallback();
EXPECT_TRUE(got_secret_);
EXPECT_NE(secret_, secret_1);
}
TEST_F(PairingRegistryTest, GetAllPairings) {
MockPairingRegistryDelegate* mock_delegate =
new MockPairingRegistryDelegate();
scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate);
scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass()));
PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
mock_delegate->RunCallback();
PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
mock_delegate->RunCallback();
registry->GetAllPairings(
base::Bind(&PairingRegistryTest::set_pairings,
base::Unretained(this)));
mock_delegate->RunCallback();
ASSERT_EQ(2u, pairings_->GetSize());
const base::DictionaryValue* actual_pairing_1;
const base::DictionaryValue* actual_pairing_2;
ASSERT_TRUE(pairings_->GetDictionary(0, &actual_pairing_1));
ASSERT_TRUE(pairings_->GetDictionary(1, &actual_pairing_2));
// Ordering is not guaranteed, so swap if necessary.
std::string actual_client_id;
ASSERT_TRUE(actual_pairing_1->GetString(PairingRegistry::kClientIdKey,
&actual_client_id));
if (actual_client_id != pairing_1.client_id()) {
std::swap(actual_pairing_1, actual_pairing_2);
}
VerifyPairing(pairing_1, *actual_pairing_1);
VerifyPairing(pairing_2, *actual_pairing_2);
}
TEST_F(PairingRegistryTest, DeletePairing) {
MockPairingRegistryDelegate* mock_delegate =
new MockPairingRegistryDelegate();
scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate);
scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass()));
PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
mock_delegate->RunCallback();
PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
mock_delegate->RunCallback();
registry->DeletePairing(pairing_1.client_id(), base::Bind(ExpectSaveSuccess));
mock_delegate->RunCallback();
// Re-read the list, and verify it only has the pairing_2 client.
registry->GetAllPairings(
base::Bind(&PairingRegistryTest::set_pairings,
base::Unretained(this)));
mock_delegate->RunCallback();
ASSERT_EQ(1u, pairings_->GetSize());
const base::DictionaryValue* actual_pairing_2;
ASSERT_TRUE(pairings_->GetDictionary(0, &actual_pairing_2));
std::string actual_client_id;
ASSERT_TRUE(actual_pairing_2->GetString(PairingRegistry::kClientIdKey,
&actual_client_id));
EXPECT_EQ(pairing_2.client_id(), actual_client_id);
}
TEST_F(PairingRegistryTest, ClearAllPairings) {
MockPairingRegistryDelegate* mock_delegate =
new MockPairingRegistryDelegate();
scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate);
scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass()));
PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client1");
mock_delegate->RunCallback();
PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client2");
mock_delegate->RunCallback();
registry->ClearAllPairings(base::Bind(ExpectSaveSuccess));
// Re-read the list, and verify it is empty.
registry->GetAllPairings(
base::Bind(&PairingRegistryTest::set_pairings,
base::Unretained(this)));
mock_delegate->RunCallback();
EXPECT_TRUE(pairings_->empty());
}
} // namespace protocol
} // namespace remoting
|