Chromium Code Reviews| 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/v2_authenticator.h" | 5 #include "remoting/protocol/v2_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" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 std::vector<uint8> key_vector( | 68 std::vector<uint8> key_vector( |
| 69 reinterpret_cast<const uint8*>(key_string.data()), | 69 reinterpret_cast<const uint8*>(key_string.data()), |
| 70 reinterpret_cast<const uint8*>(key_string.data() + | 70 reinterpret_cast<const uint8*>(key_string.data() + |
| 71 key_string.length())); | 71 key_string.length())); |
| 72 private_key_.reset( | 72 private_key_.reset( |
| 73 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); | 73 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void InitAuthenticators(const std::string& client_secret, | 76 void InitAuthenticators(const std::string& client_secret, |
| 77 const std::string& host_secret) { | 77 const std::string& host_secret) { |
| 78 host_.reset(V2Authenticator::CreateForHost( | 78 host_ = V2Authenticator::CreateForHost( |
| 79 host_cert_, *private_key_, host_secret)); | 79 host_cert_, *private_key_, host_secret); |
| 80 client_.reset(V2Authenticator::CreateForClient(client_secret)); | 80 client_ = V2Authenticator::CreateForClient(client_secret); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void RunAuthExchange() { | 83 void RunAuthExchange() { |
| 84 do { | 84 do { |
| 85 scoped_ptr<buzz::XmlElement> message; | 85 scoped_ptr<buzz::XmlElement> message; |
| 86 | 86 |
| 87 // Pass message from client to host. | 87 // Pass message from client to host. |
| 88 ASSERT_EQ(Authenticator::MESSAGE_READY, client_->state()); | 88 ASSERT_EQ(Authenticator::MESSAGE_READY, client_->state()); |
| 89 message.reset(client_->GetNextMessage()); | 89 message = client_->GetNextMessage(); |
| 90 ASSERT_TRUE(message.get()); | 90 ASSERT_TRUE(message.get()); |
| 91 ASSERT_NE(Authenticator::MESSAGE_READY, client_->state()); | 91 ASSERT_NE(Authenticator::MESSAGE_READY, client_->state()); |
| 92 | 92 |
| 93 ASSERT_EQ(Authenticator::WAITING_MESSAGE, host_->state()); | 93 ASSERT_EQ(Authenticator::WAITING_MESSAGE, host_->state()); |
| 94 host_->ProcessMessage(message.get()); | 94 host_->ProcessMessage(message.get()); |
| 95 ASSERT_NE(Authenticator::WAITING_MESSAGE, host_->state()); | 95 ASSERT_NE(Authenticator::WAITING_MESSAGE, host_->state()); |
| 96 | 96 |
| 97 // Are we done yet? | 97 // Are we done yet? |
| 98 if (host_->state() == Authenticator::ACCEPTED || | 98 if (host_->state() == Authenticator::ACCEPTED || |
| 99 host_->state() == Authenticator::REJECTED) { | 99 host_->state() == Authenticator::REJECTED) { |
| 100 break; | 100 break; |
| 101 } | 101 } |
| 102 | 102 |
| 103 // Pass message from host to client. | 103 // Pass message from host to client. |
| 104 ASSERT_EQ(Authenticator::MESSAGE_READY, host_->state()); | 104 ASSERT_EQ(Authenticator::MESSAGE_READY, host_->state()); |
| 105 message.reset(host_->GetNextMessage()); | 105 message = host_->GetNextMessage(); |
| 106 ASSERT_TRUE(message.get()); | 106 ASSERT_TRUE(message.get()); |
| 107 ASSERT_NE(Authenticator::MESSAGE_READY, host_->state()); | 107 ASSERT_NE(Authenticator::MESSAGE_READY, host_->state()); |
| 108 | 108 |
| 109 ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); | 109 ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); |
| 110 client_->ProcessMessage(message.get()); | 110 client_->ProcessMessage(message.get()); |
| 111 ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); | 111 ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); |
| 112 } while (client_->state() != Authenticator::ACCEPTED && | 112 } while (client_->state() != Authenticator::ACCEPTED && |
| 113 client_->state() != Authenticator::REJECTED); | 113 client_->state() != Authenticator::REJECTED); |
| 114 } | 114 } |
| 115 | 115 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 143 message_loop_.RunAllPending(); | 143 message_loop_.RunAllPending(); |
| 144 | 144 |
| 145 client_socket_.reset(client_socket); | 145 client_socket_.reset(client_socket); |
| 146 host_socket_.reset(host_socket); | 146 host_socket_.reset(host_socket); |
| 147 } | 147 } |
| 148 | 148 |
| 149 MessageLoop message_loop_; | 149 MessageLoop message_loop_; |
| 150 | 150 |
| 151 scoped_ptr<crypto::RSAPrivateKey> private_key_; | 151 scoped_ptr<crypto::RSAPrivateKey> private_key_; |
| 152 std::string host_cert_; | 152 std::string host_cert_; |
| 153 scoped_ptr<V2Authenticator> host_; | 153 scoped_ptr<Authenticator> host_; |
| 154 scoped_ptr<V2Authenticator> client_; | 154 scoped_ptr<Authenticator> client_; |
| 155 scoped_ptr<FakeSocket> client_fake_socket_; | 155 scoped_ptr<FakeSocket> client_fake_socket_; |
| 156 scoped_ptr<FakeSocket> host_fake_socket_; | 156 scoped_ptr<FakeSocket> host_fake_socket_; |
| 157 scoped_ptr<ChannelAuthenticator> client_auth_; | 157 scoped_ptr<ChannelAuthenticator> client_auth_; |
| 158 scoped_ptr<ChannelAuthenticator> host_auth_; | 158 scoped_ptr<ChannelAuthenticator> host_auth_; |
| 159 MockChannelDoneCallback client_callback_; | 159 MockChannelDoneCallback client_callback_; |
| 160 MockChannelDoneCallback host_callback_; | 160 MockChannelDoneCallback host_callback_; |
| 161 scoped_ptr<net::StreamSocket> client_socket_; | 161 scoped_ptr<net::StreamSocket> client_socket_; |
| 162 scoped_ptr<net::StreamSocket> host_socket_; | 162 scoped_ptr<net::StreamSocket> host_socket_; |
| 163 | 163 |
| 164 DISALLOW_COPY_AND_ASSIGN(V2AuthenticatorTest); | 164 DISALLOW_COPY_AND_ASSIGN(V2AuthenticatorTest); |
| 165 }; | 165 }; |
| 166 | 166 |
| 167 TEST_F(V2AuthenticatorTest, SuccessfulAuth) { | 167 TEST_F(V2AuthenticatorTest, SuccessfulAuth) { |
| 168 ASSERT_NO_FATAL_FAILURE( | 168 ASSERT_NO_FATAL_FAILURE( |
| 169 InitAuthenticators(kTestSharedSecret, kTestSharedSecret)); | 169 InitAuthenticators(kTestSharedSecret, kTestSharedSecret)); |
| 170 ASSERT_NO_FATAL_FAILURE(RunAuthExchange()); | 170 ASSERT_NO_FATAL_FAILURE(RunAuthExchange()); |
| 171 | 171 |
| 172 ASSERT_EQ(Authenticator::ACCEPTED, host_->state()); | 172 ASSERT_EQ(Authenticator::ACCEPTED, host_->state()); |
| 173 ASSERT_EQ(Authenticator::ACCEPTED, client_->state()); | 173 ASSERT_EQ(Authenticator::ACCEPTED, client_->state()); |
| 174 | 174 |
| 175 client_auth_.reset(client_->CreateChannelAuthenticator()); | 175 client_auth_ = client_->CreateChannelAuthenticator(); |
| 176 host_auth_.reset(host_->CreateChannelAuthenticator()); | 176 host_auth_ = host_->CreateChannelAuthenticator(); |
| 177 RunChannelAuth(false); | 177 RunChannelAuth(false); |
| 178 | 178 |
| 179 EXPECT_TRUE(client_socket_.get() != NULL); | 179 EXPECT_TRUE(client_socket_.get() != NULL); |
| 180 EXPECT_TRUE(host_socket_.get() != NULL); | 180 EXPECT_TRUE(host_socket_.get() != NULL); |
| 181 | 181 |
| 182 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), | 182 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
| 183 kMessageSize, kMessages); | 183 kMessageSize, kMessages); |
| 184 | 184 |
| 185 tester.Start(); | 185 tester.Start(); |
| 186 message_loop_.Run(); | 186 message_loop_.Run(); |
| 187 tester.CheckResults(); | 187 tester.CheckResults(); |
| 188 } | 188 } |
| 189 | 189 |
| 190 // Verify that connection is rejected when secrets don't match. | 190 // Verify that connection is rejected when secrets don't match. |
| 191 TEST_F(V2AuthenticatorTest, InvalidSecret) { | 191 TEST_F(V2AuthenticatorTest, InvalidSecret) { |
| 192 ASSERT_NO_FATAL_FAILURE( | 192 ASSERT_NO_FATAL_FAILURE( |
| 193 InitAuthenticators(kTestSharedSecretBad, kTestSharedSecret)); | 193 InitAuthenticators(kTestSharedSecretBad, kTestSharedSecret)); |
| 194 ASSERT_NO_FATAL_FAILURE(RunAuthExchange()); | 194 ASSERT_NO_FATAL_FAILURE(RunAuthExchange()); |
| 195 | 195 |
| 196 ASSERT_EQ(Authenticator::REJECTED, client_->state()); | 196 ASSERT_EQ(Authenticator::REJECTED, client_->state()); |
| 197 | 197 |
| 198 // Change |client_| so that we can get the laste message. | 198 // Change |client_| so that we can get the last message. |
| 199 client_->state_ = Authenticator::MESSAGE_READY; | 199 reinterpret_cast<V2Authenticator*>(client_.get())->state_ = |
|
Wez
2012/01/19 23:23:41
All this up/down-casting complication seems unfort
Sergey Ulanov
2012/01/19 23:50:26
I already talked about it with Albert. The best po
| |
| 200 Authenticator::MESSAGE_READY; | |
| 200 | 201 |
| 201 scoped_ptr<buzz::XmlElement> message(client_->GetNextMessage()); | 202 scoped_ptr<buzz::XmlElement> message(client_->GetNextMessage()); |
| 202 ASSERT_TRUE(message.get()); | 203 ASSERT_TRUE(message.get()); |
| 203 | 204 |
| 204 ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); | 205 ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); |
| 205 host_->ProcessMessage(message.get()); | 206 host_->ProcessMessage(message.get()); |
| 206 ASSERT_EQ(Authenticator::REJECTED, host_->state()); | 207 ASSERT_EQ(Authenticator::REJECTED, host_->state()); |
| 207 } | 208 } |
| 208 | 209 |
| 209 } // namespace protocol | 210 } // namespace protocol |
| 210 } // namespace remoting | 211 } // namespace remoting |
| OLD | NEW |