OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/v1_authenticator.h" |
| 6 |
| 7 #include "base/bind.h" |
5 #include "base/file_path.h" | 8 #include "base/file_path.h" |
6 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/message_loop.h" |
7 #include "base/path_service.h" | 11 #include "base/path_service.h" |
8 #include "crypto/rsa_private_key.h" | 12 #include "crypto/rsa_private_key.h" |
9 #include "remoting/protocol/v1_authenticator.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "remoting/protocol/authenticator.h" |
| 15 #include "remoting/protocol/channel_authenticator.h" |
| 16 #include "remoting/protocol/connection_tester.h" |
| 17 #include "remoting/protocol/fake_session.h" |
| 18 #include "remoting/protocol/v1_client_channel_authenticator.h" |
| 19 #include "remoting/protocol/v1_host_channel_authenticator.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 22 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
13 | 23 |
| 24 using testing::_; |
| 25 using testing::DeleteArg; |
| 26 using testing::SaveArg; |
| 27 |
14 namespace remoting { | 28 namespace remoting { |
15 namespace protocol { | 29 namespace protocol { |
16 | 30 |
17 namespace { | 31 namespace { |
18 const char kHostJid[] = "host1@gmail.com/123"; | 32 |
| 33 const int kMessageSize = 100; |
| 34 const int kMessages = 1; |
| 35 |
19 const char kClientJid[] = "host2@gmail.com/321"; | 36 const char kClientJid[] = "host2@gmail.com/321"; |
20 | 37 |
21 const char kTestSharedSecret[] = "1234-1234-5678"; | 38 const char kTestSharedSecret[] = "1234-1234-5678"; |
22 const char kTestSharedSecretBad[] = "0000-0000-0001"; | 39 const char kTestSharedSecretBad[] = "0000-0000-0001"; |
| 40 |
| 41 class MockChannelDoneCallback { |
| 42 public: |
| 43 MOCK_METHOD2(OnDone, void(net::Error error, net::StreamSocket* socket)); |
| 44 }; |
| 45 |
23 } // namespace | 46 } // namespace |
24 | 47 |
25 class V1AuthenticatorTest : public testing::Test { | 48 class V1AuthenticatorTest : public testing::Test { |
26 public: | 49 public: |
27 V1AuthenticatorTest() { | 50 V1AuthenticatorTest() { |
28 } | 51 } |
29 virtual ~V1AuthenticatorTest() { | 52 virtual ~V1AuthenticatorTest() { |
30 } | 53 } |
31 | 54 |
32 protected: | 55 protected: |
33 void InitAuthenticators(const std::string& client_secret, | 56 virtual void SetUp() OVERRIDE { |
34 const std::string& host_secret) { | |
35 FilePath certs_dir; | 57 FilePath certs_dir; |
36 PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir); | 58 PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir); |
37 certs_dir = certs_dir.AppendASCII("net"); | 59 certs_dir = certs_dir.AppendASCII("net"); |
38 certs_dir = certs_dir.AppendASCII("data"); | 60 certs_dir = certs_dir.AppendASCII("data"); |
39 certs_dir = certs_dir.AppendASCII("ssl"); | 61 certs_dir = certs_dir.AppendASCII("ssl"); |
40 certs_dir = certs_dir.AppendASCII("certificates"); | 62 certs_dir = certs_dir.AppendASCII("certificates"); |
41 | 63 |
42 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); | 64 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); |
43 std::string cert_der; | 65 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); |
44 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der)); | |
45 | 66 |
46 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); | 67 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); |
47 std::string key_string; | 68 std::string key_string; |
48 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); | 69 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); |
49 std::vector<uint8> key_vector( | 70 std::vector<uint8> key_vector( |
50 reinterpret_cast<const uint8*>(key_string.data()), | 71 reinterpret_cast<const uint8*>(key_string.data()), |
51 reinterpret_cast<const uint8*>(key_string.data() + | 72 reinterpret_cast<const uint8*>(key_string.data() + |
52 key_string.length())); | 73 key_string.length())); |
53 private_key_.reset( | 74 private_key_.reset( |
54 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); | 75 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); |
| 76 } |
55 | 77 |
| 78 void InitAuthenticators(const std::string& client_secret, |
| 79 const std::string& host_secret) { |
56 host_.reset(new V1HostAuthenticator( | 80 host_.reset(new V1HostAuthenticator( |
57 cert_der, private_key_.get(), host_secret, kClientJid)); | 81 host_cert_, private_key_.get(), host_secret, kClientJid)); |
58 client_.reset(new V1ClientAuthenticator(kClientJid, client_secret)); | 82 client_.reset(new V1ClientAuthenticator(kClientJid, client_secret)); |
59 } | 83 } |
60 | 84 |
61 void RunAuthExchange() { | 85 void RunAuthExchange() { |
62 do { | 86 do { |
63 scoped_ptr<buzz::XmlElement> message; | 87 scoped_ptr<buzz::XmlElement> message; |
64 | 88 |
65 // Pass message from client to host. | 89 // Pass message from client to host. |
66 ASSERT_EQ(Authenticator::MESSAGE_READY, client_->state()); | 90 ASSERT_EQ(Authenticator::MESSAGE_READY, client_->state()); |
67 message.reset(client_->GetNextMessage()); | 91 message.reset(client_->GetNextMessage()); |
(...skipping 16 matching lines...) Expand all Loading... |
84 ASSERT_TRUE(message.get()); | 108 ASSERT_TRUE(message.get()); |
85 ASSERT_NE(Authenticator::MESSAGE_READY, host_->state()); | 109 ASSERT_NE(Authenticator::MESSAGE_READY, host_->state()); |
86 | 110 |
87 ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); | 111 ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); |
88 client_->ProcessMessage(message.get()); | 112 client_->ProcessMessage(message.get()); |
89 ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); | 113 ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); |
90 } while (host_->state() != Authenticator::ACCEPTED && | 114 } while (host_->state() != Authenticator::ACCEPTED && |
91 host_->state() != Authenticator::REJECTED); | 115 host_->state() != Authenticator::REJECTED); |
92 } | 116 } |
93 | 117 |
| 118 void RunChannelAuth(bool expected_fail) { |
| 119 client_fake_socket_.reset(new FakeSocket()); |
| 120 host_fake_socket_.reset(new FakeSocket()); |
| 121 client_fake_socket_->SetPeer(host_fake_socket_.get()); |
| 122 host_fake_socket_->SetPeer(client_fake_socket_.get()); |
| 123 |
| 124 client_auth_->SecureAndAuthenticate( |
| 125 client_fake_socket_.release(), |
| 126 base::Bind(&MockChannelDoneCallback::OnDone, |
| 127 base::Unretained(&client_callback_))); |
| 128 |
| 129 host_auth_->SecureAndAuthenticate( |
| 130 host_fake_socket_.release(), |
| 131 base::Bind(&MockChannelDoneCallback::OnDone, |
| 132 base::Unretained(&host_callback_))); |
| 133 |
| 134 net::StreamSocket* client_socket = NULL; |
| 135 net::StreamSocket* host_socket = NULL; |
| 136 |
| 137 EXPECT_CALL(client_callback_, OnDone(net::OK, _)) |
| 138 .WillOnce(SaveArg<1>(&client_socket)); |
| 139 if (expected_fail) { |
| 140 EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED, NULL)); |
| 141 } else { |
| 142 EXPECT_CALL(host_callback_, OnDone(net::OK, _)) |
| 143 .WillOnce(SaveArg<1>(&host_socket)); |
| 144 } |
| 145 |
| 146 message_loop_.RunAllPending(); |
| 147 |
| 148 client_socket_.reset(client_socket); |
| 149 host_socket_.reset(host_socket); |
| 150 } |
| 151 |
| 152 MessageLoop message_loop_; |
| 153 |
94 scoped_ptr<crypto::RSAPrivateKey> private_key_; | 154 scoped_ptr<crypto::RSAPrivateKey> private_key_; |
| 155 std::string host_cert_; |
95 scoped_ptr<V1HostAuthenticator> host_; | 156 scoped_ptr<V1HostAuthenticator> host_; |
96 scoped_ptr<V1ClientAuthenticator> client_; | 157 scoped_ptr<V1ClientAuthenticator> client_; |
| 158 scoped_ptr<FakeSocket> client_fake_socket_; |
| 159 scoped_ptr<FakeSocket> host_fake_socket_; |
| 160 scoped_ptr<ChannelAuthenticator> client_auth_; |
| 161 scoped_ptr<ChannelAuthenticator> host_auth_; |
| 162 MockChannelDoneCallback client_callback_; |
| 163 MockChannelDoneCallback host_callback_; |
| 164 scoped_ptr<net::StreamSocket> client_socket_; |
| 165 scoped_ptr<net::StreamSocket> host_socket_; |
97 | 166 |
98 DISALLOW_COPY_AND_ASSIGN(V1AuthenticatorTest); | 167 DISALLOW_COPY_AND_ASSIGN(V1AuthenticatorTest); |
99 }; | 168 }; |
100 | 169 |
101 TEST_F(V1AuthenticatorTest, SuccessfulAuth) { | 170 TEST_F(V1AuthenticatorTest, SuccessfulAuth) { |
102 { | 171 { |
103 SCOPED_TRACE("RunAuthExchange"); | 172 SCOPED_TRACE("RunAuthExchange"); |
104 InitAuthenticators(kTestSharedSecret, kTestSharedSecret); | 173 InitAuthenticators(kTestSharedSecret, kTestSharedSecret); |
105 RunAuthExchange(); | 174 RunAuthExchange(); |
106 } | 175 } |
107 ASSERT_EQ(Authenticator::ACCEPTED, host_->state()); | 176 ASSERT_EQ(Authenticator::ACCEPTED, host_->state()); |
108 ASSERT_EQ(Authenticator::ACCEPTED, client_->state()); | 177 ASSERT_EQ(Authenticator::ACCEPTED, client_->state()); |
| 178 |
| 179 client_auth_.reset(client_->CreateChannelAuthenticator()); |
| 180 host_auth_.reset(host_->CreateChannelAuthenticator()); |
| 181 RunChannelAuth(false); |
| 182 |
| 183 EXPECT_TRUE(client_socket_.get() != NULL); |
| 184 EXPECT_TRUE(host_socket_.get() != NULL); |
| 185 |
| 186 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
| 187 kMessageSize, kMessages); |
| 188 |
| 189 tester.Start(); |
| 190 message_loop_.Run(); |
| 191 tester.CheckResults(); |
109 } | 192 } |
110 | 193 |
| 194 // Verify that connection is rejected when secrets don't match. |
111 TEST_F(V1AuthenticatorTest, InvalidSecret) { | 195 TEST_F(V1AuthenticatorTest, InvalidSecret) { |
112 { | 196 { |
113 SCOPED_TRACE("RunAuthExchange"); | 197 SCOPED_TRACE("RunAuthExchange"); |
114 InitAuthenticators(kTestSharedSecretBad, kTestSharedSecret); | 198 InitAuthenticators(kTestSharedSecretBad, kTestSharedSecret); |
115 RunAuthExchange(); | 199 RunAuthExchange(); |
116 } | 200 } |
117 ASSERT_EQ(Authenticator::REJECTED, host_->state()); | 201 ASSERT_EQ(Authenticator::REJECTED, host_->state()); |
118 } | 202 } |
119 | 203 |
| 204 // Verify that channels cannot be using invalid shared secret. |
| 205 TEST_F(V1AuthenticatorTest, InvalidChannelSecret) { |
| 206 client_auth_.reset(new V1ClientChannelAuthenticator( |
| 207 host_cert_, kTestSharedSecretBad)); |
| 208 host_auth_.reset(new V1HostChannelAuthenticator( |
| 209 host_cert_, private_key_.get(),kTestSharedSecret)); |
| 210 |
| 211 RunChannelAuth(true); |
| 212 |
| 213 EXPECT_TRUE(host_socket_.get() == NULL); |
| 214 } |
| 215 |
120 } // namespace protocol | 216 } // namespace protocol |
121 } // namespace remoting | 217 } // namespace remoting |
OLD | NEW |