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/authenticator_test_base.h" | 5 #include "remoting/protocol/authenticator_test_base.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.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/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); | 49 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); |
50 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); | 50 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); |
51 | 51 |
52 base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); | 52 base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); |
53 std::string key_string; | 53 std::string key_string; |
54 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); | 54 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); |
55 std::string key_base64; | 55 std::string key_base64; |
56 base::Base64Encode(key_string, &key_base64); | 56 base::Base64Encode(key_string, &key_base64); |
57 key_pair_.reset(new KeyPair()); | 57 key_pair_.reset(new KeyPair()); |
58 key_pair_->LoadFromString(key_base64); | 58 key_pair_->LoadFromString(key_base64); |
| 59 host_public_key_ = key_pair_->GetPublicKey(); |
59 } | 60 } |
60 | 61 |
61 void AuthenticatorTestBase::RunAuthExchange() { | 62 void AuthenticatorTestBase::RunAuthExchange() { |
62 do { | 63 ContinueAuthExchange(false); |
| 64 } |
| 65 |
| 66 void AuthenticatorTestBase::ContinueAuthExchange(bool skip_client_to_host) { |
| 67 while (client_->state() != Authenticator::ACCEPTED && |
| 68 client_->state() != Authenticator::REJECTED) { |
63 scoped_ptr<buzz::XmlElement> message; | 69 scoped_ptr<buzz::XmlElement> message; |
64 | 70 |
65 // Pass message from client to host. | 71 if (!skip_client_to_host) { |
66 ASSERT_EQ(Authenticator::MESSAGE_READY, client_->state()); | 72 // Pass message from client to host. |
67 message = client_->GetNextMessage(); | 73 ASSERT_EQ(Authenticator::MESSAGE_READY, client_->state()); |
68 ASSERT_TRUE(message.get()); | 74 message = client_->GetNextMessage(); |
69 ASSERT_NE(Authenticator::MESSAGE_READY, client_->state()); | 75 ASSERT_TRUE(message.get()); |
| 76 ASSERT_NE(Authenticator::MESSAGE_READY, client_->state()); |
70 | 77 |
71 ASSERT_EQ(Authenticator::WAITING_MESSAGE, host_->state()); | 78 ASSERT_EQ(Authenticator::WAITING_MESSAGE, host_->state()); |
72 host_->ProcessMessage(message.get()); | 79 host_->ProcessMessage(message.get()); |
73 ASSERT_NE(Authenticator::WAITING_MESSAGE, host_->state()); | 80 ASSERT_NE(Authenticator::WAITING_MESSAGE, host_->state()); |
| 81 |
| 82 if (host_->state() == Authenticator::WAITING_EXTERNAL) { |
| 83 host_->PerformExternalAction(base::Bind( |
| 84 &AuthenticatorTestBase::ContinueAuthExchange, |
| 85 base::Unretained(this), true)); |
| 86 break; |
| 87 } |
| 88 } else { |
| 89 skip_client_to_host = false; |
| 90 } |
74 | 91 |
75 // Are we done yet? | 92 // Are we done yet? |
76 if (host_->state() == Authenticator::ACCEPTED || | 93 if (host_->state() == Authenticator::ACCEPTED || |
77 host_->state() == Authenticator::REJECTED) { | 94 host_->state() == Authenticator::REJECTED) { |
78 break; | 95 break; |
79 } | 96 } |
80 | 97 |
81 // Pass message from host to client. | 98 // Pass message from host to client. |
82 ASSERT_EQ(Authenticator::MESSAGE_READY, host_->state()); | 99 ASSERT_EQ(Authenticator::MESSAGE_READY, host_->state()); |
83 message = host_->GetNextMessage(); | 100 message = host_->GetNextMessage(); |
84 ASSERT_TRUE(message.get()); | 101 ASSERT_TRUE(message.get()); |
85 ASSERT_NE(Authenticator::MESSAGE_READY, host_->state()); | 102 ASSERT_NE(Authenticator::MESSAGE_READY, host_->state()); |
86 | 103 |
87 ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); | 104 ASSERT_EQ(Authenticator::WAITING_MESSAGE, client_->state()); |
88 client_->ProcessMessage(message.get()); | 105 client_->ProcessMessage(message.get()); |
89 ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); | 106 ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); |
90 } while (client_->state() != Authenticator::ACCEPTED && | 107 |
91 client_->state() != Authenticator::REJECTED); | 108 if (client_->state() == Authenticator::WAITING_EXTERNAL) { |
| 109 client_->PerformExternalAction(base::Bind( |
| 110 &AuthenticatorTestBase::ContinueAuthExchange, base::Unretained(this), |
| 111 false)); |
| 112 break; |
| 113 } |
| 114 } |
92 } | 115 } |
93 | 116 |
94 void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) { | 117 void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) { |
95 client_fake_socket_.reset(new FakeSocket()); | 118 client_fake_socket_.reset(new FakeSocket()); |
96 host_fake_socket_.reset(new FakeSocket()); | 119 host_fake_socket_.reset(new FakeSocket()); |
97 client_fake_socket_->PairWith(host_fake_socket_.get()); | 120 client_fake_socket_->PairWith(host_fake_socket_.get()); |
98 | 121 |
99 client_auth_->SecureAndAuthenticate( | |
100 client_fake_socket_.PassAs<net::StreamSocket>(), | |
101 base::Bind(&AuthenticatorTestBase::OnClientConnected, | |
102 base::Unretained(this))); | |
103 | |
104 host_auth_->SecureAndAuthenticate( | |
105 host_fake_socket_.PassAs<net::StreamSocket>(), | |
106 base::Bind(&AuthenticatorTestBase::OnHostConnected, | |
107 base::Unretained(this))); | |
108 | |
109 // Expect two callbacks to be called - the client callback and the host | 122 // Expect two callbacks to be called - the client callback and the host |
110 // callback. | 123 // callback. |
111 int callback_counter = 2; | 124 int callback_counter = 2; |
112 | 125 |
113 EXPECT_CALL(client_callback_, OnDone(net::OK)) | 126 EXPECT_CALL(client_callback_, OnDone(net::OK)) |
114 .WillOnce(QuitThreadOnCounter(&callback_counter)); | 127 .WillOnce(QuitThreadOnCounter(&callback_counter)); |
115 if (expected_fail) { | 128 if (expected_fail) { |
116 EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED)) | 129 EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED)) |
117 .WillOnce(QuitThreadOnCounter(&callback_counter)); | 130 .WillOnce(QuitThreadOnCounter(&callback_counter)); |
118 } else { | 131 } else { |
119 EXPECT_CALL(host_callback_, OnDone(net::OK)) | 132 EXPECT_CALL(host_callback_, OnDone(net::OK)) |
120 .WillOnce(QuitThreadOnCounter(&callback_counter)); | 133 .WillOnce(QuitThreadOnCounter(&callback_counter)); |
121 } | 134 } |
122 | 135 |
| 136 client_auth_->SecureAndAuthenticate( |
| 137 client_fake_socket_.PassAs<net::StreamSocket>(), |
| 138 base::Bind(&AuthenticatorTestBase::OnClientConnected, |
| 139 base::Unretained(this))); |
| 140 |
| 141 host_auth_->SecureAndAuthenticate( |
| 142 host_fake_socket_.PassAs<net::StreamSocket>(), |
| 143 base::Bind(&AuthenticatorTestBase::OnHostConnected, |
| 144 base::Unretained(this))); |
| 145 |
123 // Ensure that .Run() does not run unbounded if the callbacks are never | 146 // Ensure that .Run() does not run unbounded if the callbacks are never |
124 // called. | 147 // called. |
125 base::Timer shutdown_timer(false, false); | 148 base::Timer shutdown_timer(false, false); |
126 shutdown_timer.Start(FROM_HERE, TestTimeouts::action_timeout(), | 149 shutdown_timer.Start(FROM_HERE, TestTimeouts::action_timeout(), |
127 MessageLoop::QuitClosure()); | 150 MessageLoop::QuitClosure()); |
128 message_loop_.Run(); | 151 message_loop_.Run(); |
129 shutdown_timer.Stop(); | 152 shutdown_timer.Stop(); |
130 | 153 |
131 testing::Mock::VerifyAndClearExpectations(&client_callback_); | 154 testing::Mock::VerifyAndClearExpectations(&client_callback_); |
132 testing::Mock::VerifyAndClearExpectations(&host_callback_); | 155 testing::Mock::VerifyAndClearExpectations(&host_callback_); |
(...skipping 13 matching lines...) Expand all Loading... |
146 | 169 |
147 void AuthenticatorTestBase::OnClientConnected( | 170 void AuthenticatorTestBase::OnClientConnected( |
148 net::Error error, | 171 net::Error error, |
149 scoped_ptr<net::StreamSocket> socket) { | 172 scoped_ptr<net::StreamSocket> socket) { |
150 client_callback_.OnDone(error); | 173 client_callback_.OnDone(error); |
151 client_socket_ = socket.Pass(); | 174 client_socket_ = socket.Pass(); |
152 } | 175 } |
153 | 176 |
154 } // namespace protocol | 177 } // namespace protocol |
155 } // namespace remoting | 178 } // namespace remoting |
OLD | NEW |