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/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "remoting/protocol/protocol_mock_objects.h" | 11 #include "remoting/protocol/protocol_mock_objects.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace remoting { | 15 namespace remoting { |
16 namespace protocol { | 16 namespace protocol { |
17 | 17 |
18 class PairingRegistryTest : public testing::Test { | 18 class PairingRegistryTest : public testing::Test { |
19 public: | 19 public: |
20 virtual void SetUp() OVERRIDE { | |
21 got_secret_ = false; | |
22 } | |
23 | |
24 void CompareSecret(const std::string& expected, | 20 void CompareSecret(const std::string& expected, |
25 PairingRegistry::Pairing actual) { | 21 PairingRegistry::Pairing actual) { |
26 EXPECT_EQ(actual.shared_secret(), expected); | 22 EXPECT_EQ(expected, actual.shared_secret()); |
| 23 secret_ = actual.shared_secret(); |
27 got_secret_ = true; | 24 got_secret_ = true; |
28 } | 25 } |
29 | 26 |
30 protected: | 27 protected: |
| 28 std::string secret_; |
31 bool got_secret_; | 29 bool got_secret_; |
32 }; | 30 }; |
33 | 31 |
34 TEST_F(PairingRegistryTest, GetPairing) { | 32 TEST_F(PairingRegistryTest, CreateAndGetPairings) { |
35 PairingRegistry::Pairing client_info = | |
36 PairingRegistry::Pairing::Create("client_name"); | |
37 MockPairingRegistryDelegate* mock_delegate = | |
38 new MockPairingRegistryDelegate(); | |
39 mock_delegate->AddPairing(client_info, PairingRegistry::AddPairingCallback()); | |
40 scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate); | |
41 | |
42 scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass())); | |
43 | |
44 registry->GetPairing(client_info.client_id(), | |
45 base::Bind(&PairingRegistryTest::CompareSecret, | |
46 base::Unretained(this), | |
47 client_info.shared_secret())); | |
48 mock_delegate->RunCallback(); | |
49 EXPECT_TRUE(got_secret_); | |
50 } | |
51 | |
52 TEST_F(PairingRegistryTest, AddPairing) { | |
53 MockPairingRegistryDelegate* mock_delegate = | 33 MockPairingRegistryDelegate* mock_delegate = |
54 new MockPairingRegistryDelegate(); | 34 new MockPairingRegistryDelegate(); |
55 scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate); | 35 scoped_ptr<PairingRegistry::Delegate> delegate(mock_delegate); |
56 | 36 |
57 scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass())); | 37 scoped_refptr<PairingRegistry> registry(new PairingRegistry(delegate.Pass())); |
| 38 PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client_name"); |
| 39 mock_delegate->RunCallback(); |
| 40 PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client_name"); |
| 41 mock_delegate->RunCallback(); |
58 | 42 |
59 // Verify that we can create pairings from two clients with the same name, but | 43 registry->GetPairing(pairing_1.client_id(), |
60 // that they aren't allocated the same client id. | 44 base::Bind(&PairingRegistryTest::CompareSecret, |
61 PairingRegistry::Pairing pairing_1 = registry->CreatePairing("client_name"); | 45 base::Unretained(this), |
62 PairingRegistry::Pairing pairing_2 = registry->CreatePairing("client_name"); | 46 pairing_1.shared_secret())); |
| 47 got_secret_ = false; |
| 48 mock_delegate->RunCallback(); |
| 49 EXPECT_TRUE(got_secret_); |
| 50 std::string secret_1 = secret_; |
63 | 51 |
64 const PairingRegistry::PairedClients& clients = | 52 // Check that the second client is paired with a different shared secret. |
65 mock_delegate->paired_clients(); | 53 registry->GetPairing(pairing_2.client_id(), |
66 ASSERT_EQ(clients.size(), 2u); | 54 base::Bind(&PairingRegistryTest::CompareSecret, |
67 PairingRegistry::Pairing first = clients.begin()->second; | 55 base::Unretained(this), |
68 PairingRegistry::Pairing second = (++clients.begin())->second; | 56 pairing_2.shared_secret())); |
69 EXPECT_EQ(first.client_name(), second.client_name()); | 57 got_secret_ = false; |
70 EXPECT_NE(first.client_id(), second.client_id()); | 58 mock_delegate->RunCallback(); |
| 59 EXPECT_TRUE(got_secret_); |
| 60 EXPECT_NE(secret_, secret_1); |
71 } | 61 } |
72 | 62 |
73 } // namespace protocol | 63 } // namespace protocol |
74 } // namespace remoting | 64 } // namespace remoting |
OLD | NEW |