OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/pairing_registry.h" | 5 #include "remoting/protocol/pairing_registry.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
| 9 #include "base/bind.h" |
9 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "remoting/protocol/protocol_mock_objects.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
12 | 14 |
13 namespace remoting { | 15 namespace remoting { |
14 namespace protocol { | 16 namespace protocol { |
15 | 17 |
16 class PairingRegistryTest : public testing::Test { | 18 class PairingRegistryTest : public testing::Test { |
17 }; | |
18 | |
19 class MockDelegate : public PairingRegistry::Delegate { | |
20 public: | 19 public: |
21 // MockDelegate saves to an explicit external PairedClients instance because | 20 virtual void SetUp() OVERRIDE { |
22 // PairingRegistry takes ownership of it and makes no guarantees about its | 21 got_secret_ = false; |
23 // lifetime, so this is a simple way of getting access to pairing results. | |
24 MockDelegate(PairingRegistry::PairedClients* paired_clients) | |
25 : paired_clients_(paired_clients) { | |
26 } | 22 } |
27 | 23 |
28 virtual void Save( | 24 void CompareSecret(const std::string& expected, |
29 const PairingRegistry::PairedClients& paired_clients) OVERRIDE { | 25 PairingRegistry::Pairing actual) { |
30 *paired_clients_ = paired_clients; | 26 EXPECT_EQ(actual.shared_secret, expected); |
| 27 got_secret_ = true; |
31 } | 28 } |
32 | 29 |
33 protected: | 30 protected: |
34 PairingRegistry::PairedClients* paired_clients_; | 31 bool got_secret_; |
35 }; | 32 }; |
36 | 33 |
37 TEST_F(PairingRegistryTest, LoadAndLookup) { | 34 TEST_F(PairingRegistryTest, GetPairing) { |
38 PairingRegistry::Pairing client_info = { | 35 PairingRegistry::Pairing client_info = { |
39 "client_id", | 36 "client_id", |
40 "client_name", | 37 "client_name", |
41 "shared_secret" | 38 "shared_secret" |
42 }; | 39 }; |
43 PairingRegistry::PairedClients clients; | 40 MockPairingRegistryDelegate* mock_delegate = |
44 clients[client_info.client_id] = client_info; | 41 new MockPairingRegistryDelegate(); |
45 scoped_ptr<PairingRegistry::Delegate> delegate(new MockDelegate(&clients)); | 42 mock_delegate->AddPairing(client_info); |
| 43 scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate); |
46 | 44 |
47 scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass(), | 45 scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass())); |
48 clients)); | |
49 | 46 |
50 std::string secret = registry->GetSecret(client_info.client_id); | 47 registry->GetPairing(client_info.client_id, |
51 EXPECT_EQ(secret, client_info.shared_secret); | 48 base::Bind(&PairingRegistryTest::CompareSecret, |
| 49 base::Unretained(this), |
| 50 client_info.shared_secret)); |
| 51 mock_delegate->RunCallback(); |
| 52 EXPECT_TRUE(got_secret_); |
52 } | 53 } |
53 | 54 |
54 TEST_F(PairingRegistryTest, CreateAndSave) { | 55 TEST_F(PairingRegistryTest, AddPairing) { |
55 PairingRegistry::PairedClients clients; | 56 MockPairingRegistryDelegate* mock_delegate = |
56 scoped_ptr<PairingRegistry::Delegate> delegate(new MockDelegate(&clients)); | 57 new MockPairingRegistryDelegate(); |
| 58 scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate); |
57 | 59 |
58 scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass(), | 60 scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass())); |
59 clients)); | |
60 | 61 |
61 // Verify that we can create pairings from two clients with the same name, but | 62 // Verify that we can create pairings from two clients with the same name, but |
62 // that they aren't allocated the same client id. | 63 // that they aren't allocated the same client id. |
63 PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client_name"); | 64 PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client_name"); |
64 PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client_name"); | 65 PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client_name"); |
65 | 66 |
| 67 const PairingRegistry::PairedClients& clients = |
| 68 mock_delegate->paired_clients(); |
66 ASSERT_EQ(clients.size(), 2u); | 69 ASSERT_EQ(clients.size(), 2u); |
67 PairingRegistry::Pairing first = clients.begin()->second; | 70 PairingRegistry::Pairing first = clients.begin()->second; |
68 PairingRegistry::Pairing second = (++clients.begin())->second; | 71 PairingRegistry::Pairing second = (++clients.begin())->second; |
69 EXPECT_EQ(first.client_name, second.client_name); | 72 EXPECT_EQ(first.client_name, second.client_name); |
70 EXPECT_NE(first.client_id, second.client_id); | 73 EXPECT_NE(first.client_id, second.client_id); |
71 } | 74 } |
72 | 75 |
73 } // namespace protocol | 76 } // namespace protocol |
74 } // namespace remoting | 77 } // namespace remoting |
OLD | NEW |