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/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/test/test_timeouts.h" | |
11 #include "base/timer.h" | |
10 #include "crypto/rsa_private_key.h" | 12 #include "crypto/rsa_private_key.h" |
11 #include "net/base/cert_test_util.h" | 13 #include "net/base/cert_test_util.h" |
12 #include "remoting/protocol/authenticator.h" | 14 #include "remoting/protocol/authenticator.h" |
13 #include "remoting/protocol/channel_authenticator.h" | 15 #include "remoting/protocol/channel_authenticator.h" |
14 #include "remoting/protocol/fake_session.h" | 16 #include "remoting/protocol/fake_session.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 18 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
17 | 19 |
18 using testing::_; | 20 using testing::_; |
19 using testing::SaveArg; | 21 using testing::SaveArg; |
20 | 22 |
21 namespace remoting { | 23 namespace remoting { |
22 namespace protocol { | 24 namespace protocol { |
23 | 25 |
24 AuthenticatorTestBase::MockChannelDoneCallback::MockChannelDoneCallback() { | 26 namespace { |
25 } | |
26 AuthenticatorTestBase::MockChannelDoneCallback::~MockChannelDoneCallback() { | |
27 } | |
28 | 27 |
29 AuthenticatorTestBase::AuthenticatorTestBase() { | 28 // Helper to ensure that both the client and host callbacks are invoked |
30 } | 29 // within a reasonable time or that the message loop is aborted. |
31 AuthenticatorTestBase::~AuthenticatorTestBase() { | 30 class ChannelDoneHelper { |
Sergey Ulanov
2012/06/05 04:29:46
In jingle_session_unittest.cc there is QuitThreadO
| |
32 } | 31 public: |
32 typedef base::Callback< | |
33 void(net::Error, scoped_ptr<net::StreamSocket>)> ConnectedCallback; | |
Sergey Ulanov
2012/06/05 04:29:46
Can we just reuse ChannelAuthenticator::DoneCallba
| |
34 | |
35 ChannelDoneHelper() : expected_calls_(1), actual_calls_(0) {} | |
Sergey Ulanov
2012/06/05 04:29:46
nit: why initialize expected_calls_ to 1?
| |
36 | |
37 // Starts the shutdown timer, expecting |OnConnected()| to be called at | |
Sergey Ulanov
2012/06/05 04:29:46
nit: OnConnected(), || are redundant here.
| |
38 // least |expected_calls| times. | |
39 void Start(int expected_calls) { | |
40 DCHECK_GT(expected_calls, 0); | |
41 expected_calls_ = expected_calls_; | |
42 actual_calls_ = 0; | |
43 timer_.Start(FROM_HERE, TestTimeouts::action_timeout(), this, | |
44 &ChannelDoneHelper::OnTimerFired); | |
45 } | |
46 | |
47 // Helper function that passes |error| and |socket| to |callback| whenever | |
48 // it is called, disarming the shutdown timer if called the required number | |
49 // of times. | |
50 void OnConnected(const ConnectedCallback& callback, | |
51 net::Error error, | |
52 scoped_ptr<net::StreamSocket> socket) { | |
53 if (++actual_calls_ == expected_calls_) { | |
54 MessageLoop::current()->Quit(); | |
55 timer_.Stop(); | |
56 } | |
57 callback.Run(error, socket.Pass()); | |
58 } | |
59 | |
60 private: | |
61 void OnTimerFired() { | |
62 MessageLoop::current()->QuitNow(); | |
63 } | |
64 | |
65 int expected_calls_; | |
66 int actual_calls_; | |
67 base::OneShotTimer<ChannelDoneHelper> timer_; | |
68 }; | |
69 | |
70 } // namespace | |
71 | |
72 AuthenticatorTestBase::MockChannelDoneCallback::MockChannelDoneCallback() {} | |
73 | |
74 AuthenticatorTestBase::MockChannelDoneCallback::~MockChannelDoneCallback() {} | |
75 | |
76 AuthenticatorTestBase::AuthenticatorTestBase() {} | |
77 | |
78 AuthenticatorTestBase::~AuthenticatorTestBase() {} | |
33 | 79 |
34 void AuthenticatorTestBase::SetUp() { | 80 void AuthenticatorTestBase::SetUp() { |
35 FilePath certs_dir(net::GetTestCertsDirectory()); | 81 FilePath certs_dir(net::GetTestCertsDirectory()); |
36 | 82 |
37 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); | 83 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); |
38 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); | 84 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &host_cert_)); |
39 | 85 |
40 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); | 86 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); |
41 std::string key_string; | 87 std::string key_string; |
42 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); | 88 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); | 125 ASSERT_NE(Authenticator::WAITING_MESSAGE, client_->state()); |
80 } while (client_->state() != Authenticator::ACCEPTED && | 126 } while (client_->state() != Authenticator::ACCEPTED && |
81 client_->state() != Authenticator::REJECTED); | 127 client_->state() != Authenticator::REJECTED); |
82 } | 128 } |
83 | 129 |
84 void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) { | 130 void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) { |
85 client_fake_socket_.reset(new FakeSocket()); | 131 client_fake_socket_.reset(new FakeSocket()); |
86 host_fake_socket_.reset(new FakeSocket()); | 132 host_fake_socket_.reset(new FakeSocket()); |
87 client_fake_socket_->PairWith(host_fake_socket_.get()); | 133 client_fake_socket_->PairWith(host_fake_socket_.get()); |
88 | 134 |
135 ChannelDoneHelper helper; | |
136 | |
89 client_auth_->SecureAndAuthenticate( | 137 client_auth_->SecureAndAuthenticate( |
90 client_fake_socket_.PassAs<net::StreamSocket>(), | 138 client_fake_socket_.PassAs<net::StreamSocket>(), |
91 base::Bind(&AuthenticatorTestBase::OnClientConnected, | 139 base::Bind(&ChannelDoneHelper::OnConnected, |
92 base::Unretained(this))); | 140 base::Unretained(&helper), |
141 base::Bind(&AuthenticatorTestBase::OnClientConnected, | |
142 base::Unretained(this)))); | |
93 | 143 |
94 host_auth_->SecureAndAuthenticate( | 144 host_auth_->SecureAndAuthenticate( |
95 host_fake_socket_.PassAs<net::StreamSocket>(), | 145 host_fake_socket_.PassAs<net::StreamSocket>(), |
96 base::Bind(&AuthenticatorTestBase::OnHostConnected, | 146 base::Bind(&ChannelDoneHelper::OnConnected, |
97 base::Unretained(this))); | 147 base::Unretained(&helper), |
148 base::Bind(&AuthenticatorTestBase::OnHostConnected, | |
149 base::Unretained(this)))); | |
98 | 150 |
99 net::StreamSocket* client_socket = NULL; | 151 net::StreamSocket* client_socket = NULL; |
100 net::StreamSocket* host_socket = NULL; | 152 net::StreamSocket* host_socket = NULL; |
101 | 153 |
102 EXPECT_CALL(client_callback_, OnDone(net::OK, _)) | 154 EXPECT_CALL(client_callback_, OnDone(net::OK, _)) |
103 .WillOnce(SaveArg<1>(&client_socket)); | 155 .WillOnce(SaveArg<1>(&client_socket)); |
104 if (expected_fail) { | 156 if (expected_fail) { |
105 EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED, NULL)); | 157 EXPECT_CALL(host_callback_, OnDone(net::ERR_FAILED, NULL)); |
106 } else { | 158 } else { |
107 EXPECT_CALL(host_callback_, OnDone(net::OK, _)) | 159 EXPECT_CALL(host_callback_, OnDone(net::OK, _)) |
108 .WillOnce(SaveArg<1>(&host_socket)); | 160 .WillOnce(SaveArg<1>(&host_socket)); |
109 } | 161 } |
110 | 162 |
111 message_loop_.RunAllPending(); | 163 // Expect two calls - once for the host, once for the client. |
164 helper.Start(2); | |
165 message_loop_.Run(); | |
112 | 166 |
113 testing::Mock::VerifyAndClearExpectations(&client_callback_); | 167 testing::Mock::VerifyAndClearExpectations(&client_callback_); |
114 testing::Mock::VerifyAndClearExpectations(&host_callback_); | 168 testing::Mock::VerifyAndClearExpectations(&host_callback_); |
115 | 169 |
116 client_socket_.reset(client_socket); | 170 client_socket_.reset(client_socket); |
117 host_socket_.reset(host_socket); | 171 host_socket_.reset(host_socket); |
172 | |
173 if (!expected_fail) { | |
174 ASSERT_TRUE(client_socket_.get() != NULL); | |
175 ASSERT_TRUE(host_socket_.get() != NULL); | |
176 } | |
118 } | 177 } |
119 | 178 |
120 void AuthenticatorTestBase::OnHostConnected( | 179 void AuthenticatorTestBase::OnHostConnected( |
121 net::Error error, | 180 net::Error error, |
122 scoped_ptr<net::StreamSocket> socket) { | 181 scoped_ptr<net::StreamSocket> socket) { |
123 host_callback_.OnDone(error, socket.get()); | 182 host_callback_.OnDone(error, socket.get()); |
124 host_socket_ = socket.Pass(); | 183 host_socket_ = socket.Pass(); |
125 } | 184 } |
126 | 185 |
127 void AuthenticatorTestBase::OnClientConnected( | 186 void AuthenticatorTestBase::OnClientConnected( |
128 net::Error error, | 187 net::Error error, |
129 scoped_ptr<net::StreamSocket> socket) { | 188 scoped_ptr<net::StreamSocket> socket) { |
130 client_callback_.OnDone(error, socket.get()); | 189 client_callback_.OnDone(error, socket.get()); |
131 client_socket_ = socket.Pass(); | 190 client_socket_ = socket.Pass(); |
132 } | 191 } |
133 | 192 |
134 } // namespace protocol | 193 } // namespace protocol |
135 } // namespace remoting | 194 } // namespace remoting |
OLD | NEW |