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/ssl_hmac_channel_authenticator.h" | 5 #include "remoting/protocol/ssl_hmac_channel_authenticator.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "crypto/rsa_private_key.h" | 12 #include "crypto/rsa_private_key.h" |
13 #include "net/base/cert_test_util.h" | 13 #include "net/base/cert_test_util.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "remoting/protocol/authenticator_test_base.h" | |
15 #include "remoting/protocol/connection_tester.h" | 16 #include "remoting/protocol/connection_tester.h" |
16 #include "remoting/protocol/fake_session.h" | 17 #include "remoting/protocol/fake_session.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 20 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
20 | 21 |
21 using testing::_; | 22 using testing::_; |
22 using testing::NotNull; | 23 using testing::NotNull; |
23 using testing::SaveArg; | 24 using testing::SaveArg; |
24 | 25 |
25 namespace remoting { | 26 namespace remoting { |
26 namespace protocol { | 27 namespace protocol { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 const char kTestSharedSecret[] = "1234-1234-5678"; | 31 const char kTestSharedSecret[] = "1234-1234-5678"; |
31 const char kTestSharedSecretBad[] = "0000-0000-0001"; | 32 const char kTestSharedSecretBad[] = "0000-0000-0001"; |
32 | 33 |
33 class MockChannelDoneCallback { | 34 class MockChannelDoneCallback { |
34 public: | 35 public: |
35 MOCK_METHOD2(OnDone, void(net::Error error, net::StreamSocket* socket)); | 36 MOCK_METHOD2(OnDone, void(net::Error error, net::StreamSocket* socket)); |
36 }; | 37 }; |
37 | 38 |
38 } // namespace | 39 } // namespace |
39 | 40 |
40 class SslHmacChannelAuthenticatorTest : public testing::Test { | 41 class SslHmacChannelAuthenticatorTest : public AuthenticatorTestBase { |
Sergey Ulanov
2012/06/05 04:29:46
AuthenticatorTestBase was supposed to be a base cl
| |
41 public: | 42 public: |
42 SslHmacChannelAuthenticatorTest() { | 43 SslHmacChannelAuthenticatorTest() {} |
43 } | 44 virtual ~SslHmacChannelAuthenticatorTest() {} |
44 virtual ~SslHmacChannelAuthenticatorTest() { | |
45 } | |
46 | 45 |
47 protected: | 46 private: |
48 virtual void SetUp() OVERRIDE { | |
49 FilePath certs_dir(net::GetTestCertsDirectory()); | |
50 | |
51 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); | |
52 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); | |
53 | |
54 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); | |
55 std::string key_string; | |
56 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); | |
57 std::vector<uint8> key_vector( | |
58 reinterpret_cast<const uint8*>(key_string.data()), | |
59 reinterpret_cast<const uint8*>(key_string.data() + | |
60 key_string.length())); | |
61 private_key_.reset( | |
62 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); | |
63 } | |
64 | |
65 void RunChannelAuth(bool expected_fail) { | |
66 client_fake_socket_.reset(new FakeSocket()); | |
67 host_fake_socket_.reset(new FakeSocket()); | |
68 client_fake_socket_->PairWith(host_fake_socket_.get()); | |
69 | |
70 client_auth_->SecureAndAuthenticate( | |
71 client_fake_socket_.PassAs<net::StreamSocket>(), | |
72 base::Bind(&SslHmacChannelAuthenticatorTest::OnClientConnected, | |
73 base::Unretained(this))); | |
74 | |
75 host_auth_->SecureAndAuthenticate( | |
76 host_fake_socket_.PassAs<net::StreamSocket>(), | |
77 base::Bind(&SslHmacChannelAuthenticatorTest::OnHostConnected, | |
78 base::Unretained(this))); | |
79 | |
80 if (expected_fail) { | |
81 EXPECT_CALL(client_callback_, OnDone(net::ERR_FAILED, NULL)); | |
82 EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED, NULL)); | |
83 } else { | |
84 EXPECT_CALL(client_callback_, OnDone(net::OK, NotNull())); | |
85 EXPECT_CALL(host_callback_, OnDone(net::OK, NotNull())); | |
86 } | |
87 | |
88 message_loop_.RunAllPending(); | |
89 } | |
90 | |
91 void OnHostConnected(net::Error error, | |
92 scoped_ptr<net::StreamSocket> socket) { | |
93 host_callback_.OnDone(error, socket.get()); | |
94 host_socket_ = socket.Pass(); | |
95 } | |
96 | |
97 void OnClientConnected(net::Error error, | |
98 scoped_ptr<net::StreamSocket> socket) { | |
99 client_callback_.OnDone(error, socket.get()); | |
100 client_socket_ = socket.Pass(); | |
101 } | |
102 | |
103 MessageLoop message_loop_; | |
104 | |
105 scoped_ptr<crypto::RSAPrivateKey> private_key_; | |
106 std::string host_cert_; | |
107 scoped_ptr<FakeSocket> client_fake_socket_; | |
108 scoped_ptr<FakeSocket> host_fake_socket_; | |
109 scoped_ptr<ChannelAuthenticator> client_auth_; | |
110 scoped_ptr<ChannelAuthenticator> host_auth_; | |
111 MockChannelDoneCallback client_callback_; | |
112 MockChannelDoneCallback host_callback_; | |
113 scoped_ptr<net::StreamSocket> client_socket_; | |
114 scoped_ptr<net::StreamSocket> host_socket_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(SslHmacChannelAuthenticatorTest); | 47 DISALLOW_COPY_AND_ASSIGN(SslHmacChannelAuthenticatorTest); |
117 }; | 48 }; |
118 | 49 |
119 // Verify that a channel can be connected using a valid shared secret. | 50 // Verify that a channel can be connected using a valid shared secret. |
120 TEST_F(SslHmacChannelAuthenticatorTest, SuccessfulAuth) { | 51 TEST_F(SslHmacChannelAuthenticatorTest, SuccessfulAuth) { |
121 client_auth_ = SslHmacChannelAuthenticator::CreateForClient( | 52 client_auth_ = SslHmacChannelAuthenticator::CreateForClient( |
122 host_cert_, kTestSharedSecret); | 53 host_cert_, kTestSharedSecret); |
123 host_auth_ = SslHmacChannelAuthenticator::CreateForHost( | 54 host_auth_ = SslHmacChannelAuthenticator::CreateForHost( |
124 host_cert_, private_key_.get(), kTestSharedSecret); | 55 host_cert_, private_key_.get(), kTestSharedSecret); |
125 | 56 |
126 RunChannelAuth(false); | 57 RunChannelAuth(false); |
127 | 58 |
128 EXPECT_TRUE(client_socket_.get() != NULL); | |
129 EXPECT_TRUE(host_socket_.get() != NULL); | |
130 | |
131 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), | 59 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
132 100, 2); | 60 100, 2); |
133 | 61 |
134 tester.Start(); | 62 tester.Start(); |
135 message_loop_.Run(); | 63 message_loop_.Run(); |
136 tester.CheckResults(); | 64 tester.CheckResults(); |
137 } | 65 } |
138 | 66 |
139 // Verify that channels cannot be using invalid shared secret. | 67 // Verify that channels cannot be using invalid shared secret. |
140 TEST_F(SslHmacChannelAuthenticatorTest, InvalidChannelSecret) { | 68 TEST_F(SslHmacChannelAuthenticatorTest, InvalidChannelSecret) { |
141 client_auth_ = SslHmacChannelAuthenticator::CreateForClient( | 69 client_auth_ = SslHmacChannelAuthenticator::CreateForClient( |
142 host_cert_, kTestSharedSecretBad); | 70 host_cert_, kTestSharedSecretBad); |
143 host_auth_ = SslHmacChannelAuthenticator::CreateForHost( | 71 host_auth_ = SslHmacChannelAuthenticator::CreateForHost( |
144 host_cert_, private_key_.get(), kTestSharedSecret); | 72 host_cert_, private_key_.get(), kTestSharedSecret); |
145 | 73 |
146 RunChannelAuth(true); | 74 RunChannelAuth(true); |
147 | 75 |
148 EXPECT_TRUE(host_socket_.get() == NULL); | 76 EXPECT_TRUE(host_socket_.get() == NULL); |
149 } | 77 } |
150 | 78 |
151 } // namespace protocol | 79 } // namespace protocol |
152 } // namespace remoting | 80 } // namespace remoting |
OLD | NEW |