OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "webrtc/base/gunit.h" | 14 #include "webrtc/base/gunit.h" |
15 #include "webrtc/base/ipaddress.h" | 15 #include "webrtc/base/ipaddress.h" |
16 #include "webrtc/base/socketstream.h" | 16 #include "webrtc/base/socketstream.h" |
17 #include "webrtc/base/ssladapter.h" | 17 #include "webrtc/base/ssladapter.h" |
| 18 #include "webrtc/base/sslidentity.h" |
18 #include "webrtc/base/sslstreamadapter.h" | 19 #include "webrtc/base/sslstreamadapter.h" |
19 #include "webrtc/base/sslidentity.h" | |
20 #include "webrtc/base/stream.h" | 20 #include "webrtc/base/stream.h" |
| 21 #include "webrtc/base/stringencode.h" |
21 #include "webrtc/base/virtualsocketserver.h" | 22 #include "webrtc/base/virtualsocketserver.h" |
22 | 23 |
23 static const int kTimeout = 5000; | 24 static const int kTimeout = 5000; |
24 | 25 |
25 static rtc::AsyncSocket* CreateSocket(const rtc::SSLMode& ssl_mode) { | 26 static rtc::AsyncSocket* CreateSocket(const rtc::SSLMode& ssl_mode) { |
26 rtc::SocketAddress address(rtc::IPAddress(INADDR_ANY), 0); | 27 rtc::SocketAddress address(rtc::IPAddress(INADDR_ANY), 0); |
27 | 28 |
28 rtc::AsyncSocket* socket = rtc::Thread::Current()-> | 29 rtc::AsyncSocket* socket = rtc::Thread::Current()-> |
29 socketserver()->CreateAsyncSocket( | 30 socketserver()->CreateAsyncSocket( |
30 address.family(), (ssl_mode == rtc::SSL_MODE_DTLS) ? | 31 address.family(), (ssl_mode == rtc::SSL_MODE_DTLS) ? |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 void OnServerSocketReadEvent(rtc::AsyncSocket* socket) { | 204 void OnServerSocketReadEvent(rtc::AsyncSocket* socket) { |
204 // Only a single connection is supported. | 205 // Only a single connection is supported. |
205 ASSERT_TRUE(ssl_stream_adapter_ == nullptr); | 206 ASSERT_TRUE(ssl_stream_adapter_ == nullptr); |
206 | 207 |
207 DoHandshake(server_socket_->Accept(nullptr)); | 208 DoHandshake(server_socket_->Accept(nullptr)); |
208 } | 209 } |
209 | 210 |
210 void OnSSLStreamAdapterEvent(rtc::StreamInterface* stream, int sig, int err) { | 211 void OnSSLStreamAdapterEvent(rtc::StreamInterface* stream, int sig, int err) { |
211 if (sig & rtc::SE_READ) { | 212 if (sig & rtc::SE_READ) { |
212 char buffer[4096] = ""; | 213 char buffer[4096] = ""; |
213 | |
214 size_t read; | 214 size_t read; |
215 int error; | 215 int error; |
216 | 216 |
217 // Read data received from the client and store it in our internal | 217 // Read data received from the client and store it in our internal |
218 // buffer. | 218 // buffer. |
219 rtc::StreamResult r = stream->Read(buffer, | 219 rtc::StreamResult r = |
220 sizeof(buffer) - 1, &read, &error); | 220 stream->Read(buffer, sizeof(buffer) - 1, &read, &error); |
221 if (r == rtc::SR_SUCCESS) { | 221 if (r == rtc::SR_SUCCESS) { |
222 buffer[read] = '\0'; | 222 buffer[read] = '\0'; |
223 | |
224 LOG(LS_INFO) << "Server received '" << buffer << "'"; | 223 LOG(LS_INFO) << "Server received '" << buffer << "'"; |
225 | |
226 data_ += buffer; | 224 data_ += buffer; |
227 } | 225 } |
228 } | 226 } |
229 } | 227 } |
230 | 228 |
231 private: | 229 private: |
232 void DoHandshake(rtc::AsyncSocket* socket) { | 230 void DoHandshake(rtc::AsyncSocket* socket) { |
233 rtc::SocketStream* stream = new rtc::SocketStream(socket); | 231 rtc::SocketStream* stream = new rtc::SocketStream(socket); |
234 | 232 |
235 ssl_stream_adapter_.reset(rtc::SSLStreamAdapter::Create(stream)); | 233 ssl_stream_adapter_.reset(rtc::SSLStreamAdapter::Create(stream)); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 | 327 |
330 rv = server_->Send(message); | 328 rv = server_->Send(message); |
331 ASSERT_EQ(static_cast<int>(message.length()), rv); | 329 ASSERT_EQ(static_cast<int>(message.length()), rv); |
332 | 330 |
333 // The client should have received the server's message. | 331 // The client should have received the server's message. |
334 EXPECT_EQ_WAIT(message, client_->GetReceivedData(), kTimeout); | 332 EXPECT_EQ_WAIT(message, client_->GetReceivedData(), kTimeout); |
335 | 333 |
336 LOG(LS_INFO) << "Transfer complete."; | 334 LOG(LS_INFO) << "Transfer complete."; |
337 } | 335 } |
338 | 336 |
339 private: | 337 protected: |
340 const rtc::SSLMode ssl_mode_; | 338 const rtc::SSLMode ssl_mode_; |
341 | 339 |
342 std::unique_ptr<rtc::VirtualSocketServer> vss_; | 340 std::unique_ptr<rtc::VirtualSocketServer> vss_; |
343 rtc::AutoSocketServerThread thread_; | 341 rtc::AutoSocketServerThread thread_; |
344 std::unique_ptr<SSLAdapterTestDummyServer> server_; | 342 std::unique_ptr<SSLAdapterTestDummyServer> server_; |
345 std::unique_ptr<SSLAdapterTestDummyClient> client_; | 343 std::unique_ptr<SSLAdapterTestDummyClient> client_; |
346 | 344 |
347 int handshake_wait_; | 345 int handshake_wait_; |
348 }; | 346 }; |
349 | 347 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnect) { | 380 TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnect) { |
383 TestHandshake(true); | 381 TestHandshake(true); |
384 } | 382 } |
385 | 383 |
386 // Test transfer between client and server, using RSA | 384 // Test transfer between client and server, using RSA |
387 TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransfer) { | 385 TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransfer) { |
388 TestHandshake(true); | 386 TestHandshake(true); |
389 TestTransfer("Hello, world!"); | 387 TestTransfer("Hello, world!"); |
390 } | 388 } |
391 | 389 |
| 390 TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransferWithBlockedSocket) { |
| 391 TestHandshake(true); |
| 392 |
| 393 // Tell the underlying socket to simulate being blocked. |
| 394 vss_->SetSendingBlocked(true); |
| 395 |
| 396 std::string expected; |
| 397 int rv; |
| 398 // Send messages until the SSL socket adapter starts applying backpressure. |
| 399 // Note that this may not occur immediately since there may be some amount of |
| 400 // intermediate buffering (either in our code or in BoringSSL). |
| 401 for (int i = 0; i < 1024; ++i) { |
| 402 std::string message = "Hello, world: " + rtc::ToString(i); |
| 403 rv = client_->Send(message); |
| 404 if (rv != static_cast<int>(message.size())) { |
| 405 // This test assumes either the whole message or none of it is sent. |
| 406 ASSERT_EQ(-1, rv); |
| 407 break; |
| 408 } |
| 409 expected += message; |
| 410 } |
| 411 // Assert that the loop above exited due to Send returning -1. |
| 412 ASSERT_EQ(-1, rv); |
| 413 |
| 414 // Try sending another message while blocked. -1 should be returned again and |
| 415 // it shouldn't end up received by the server later. |
| 416 EXPECT_EQ(-1, client_->Send("Never sent")); |
| 417 |
| 418 // Unblock the underlying socket. All of the buffered messages should be sent |
| 419 // without any further action. |
| 420 vss_->SetSendingBlocked(false); |
| 421 EXPECT_EQ_WAIT(expected, server_->GetReceivedData(), kTimeout); |
| 422 |
| 423 // Send another message. This previously wasn't working |
| 424 std::string final_message = "Fin."; |
| 425 expected += final_message; |
| 426 EXPECT_EQ(static_cast<int>(final_message.size()), |
| 427 client_->Send(final_message)); |
| 428 EXPECT_EQ_WAIT(expected, server_->GetReceivedData(), kTimeout); |
| 429 } |
| 430 |
392 // Test transfer between client and server, using ECDSA | 431 // Test transfer between client and server, using ECDSA |
393 TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSTransfer) { | 432 TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSTransfer) { |
394 TestHandshake(true); | 433 TestHandshake(true); |
395 TestTransfer("Hello, world!"); | 434 TestTransfer("Hello, world!"); |
396 } | 435 } |
397 | 436 |
398 // Basic tests: DTLS | 437 // Basic tests: DTLS |
399 | 438 |
400 // Test that handshake works, using RSA | 439 // Test that handshake works, using RSA |
401 TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSConnect) { | 440 TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSConnect) { |
402 TestHandshake(true); | 441 TestHandshake(true); |
403 } | 442 } |
404 | 443 |
405 // Test that handshake works, using ECDSA | 444 // Test that handshake works, using ECDSA |
406 TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSConnect) { | 445 TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSConnect) { |
407 TestHandshake(true); | 446 TestHandshake(true); |
408 } | 447 } |
409 | 448 |
410 // Test transfer between client and server, using RSA | 449 // Test transfer between client and server, using RSA |
411 TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSTransfer) { | 450 TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSTransfer) { |
412 TestHandshake(true); | 451 TestHandshake(true); |
413 TestTransfer("Hello, world!"); | 452 TestTransfer("Hello, world!"); |
414 } | 453 } |
415 | 454 |
416 // Test transfer between client and server, using ECDSA | 455 // Test transfer between client and server, using ECDSA |
417 TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSTransfer) { | 456 TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSTransfer) { |
418 TestHandshake(true); | 457 TestHandshake(true); |
419 TestTransfer("Hello, world!"); | 458 TestTransfer("Hello, world!"); |
420 } | 459 } |
OLD | NEW |