OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <queue> |
| 6 #include <string> |
| 7 |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/stringprintf.h" |
| 10 #include "crypto/symmetric_key.h" |
| 11 #include "net/base/io_buffer.h" |
| 12 #include "remoting/protocol/secure_p2p_socket.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace remoting { |
| 16 namespace protocol { |
| 17 |
| 18 namespace { |
| 19 class TestSocket : public net::Socket { |
| 20 public: |
| 21 TestSocket() {} |
| 22 |
| 23 // Socket implementation. |
| 24 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 25 net::CompletionCallback* callback) { |
| 26 std::string buffer = buffer_.front(); |
| 27 buffer_.pop(); |
| 28 |
| 29 memcpy(buf->data(), buffer.data(), buffer.length()); |
| 30 int size = buffer.length(); |
| 31 return size; |
| 32 } |
| 33 |
| 34 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 35 net::CompletionCallback* callback) { |
| 36 buffer_.push(std::string(buf->data(), buf_len)); |
| 37 return buf_len; |
| 38 } |
| 39 |
| 40 virtual bool SetReceiveBufferSize(int32 size) { |
| 41 return true; |
| 42 } |
| 43 |
| 44 virtual bool SetSendBufferSize(int32 size) { |
| 45 return true; |
| 46 } |
| 47 |
| 48 std::string GetFrontBuffer() const { |
| 49 return buffer_.front(); |
| 50 } |
| 51 |
| 52 void PushBuffer(std::string buffer) { |
| 53 buffer_.push(buffer); |
| 54 } |
| 55 |
| 56 private: |
| 57 std::queue<std::string> buffer_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(TestSocket); |
| 60 }; |
| 61 } // namespace |
| 62 |
| 63 TEST(SecureP2PSocketTest, WriteAndRead) { |
| 64 TestSocket* test_socket = new TestSocket(); |
| 65 SecureP2PSocket secure_socket(test_socket, "1234567890123456"); |
| 66 |
| 67 const std::string kWritePattern = "Hello world! This is a nice day."; |
| 68 scoped_refptr<net::IOBuffer> write_buf = |
| 69 new net::StringIOBuffer(kWritePattern); |
| 70 scoped_refptr<net::IOBuffer> read_buf = new net::IOBufferWithSize(2048); |
| 71 |
| 72 for (int i = 0; i < 5; ++i) { |
| 73 size_t written = secure_socket.Write(write_buf, |
| 74 kWritePattern.length(), NULL); |
| 75 EXPECT_EQ(kWritePattern.length(), written); |
| 76 EXPECT_EQ(kWritePattern.length() + 44, |
| 77 test_socket->GetFrontBuffer().length()); |
| 78 |
| 79 std::string hex_packet; |
| 80 for (size_t j = 0; j < test_socket->GetFrontBuffer().length(); ++j) { |
| 81 base::StringAppendF(&hex_packet, "%02x", |
| 82 (uint8)test_socket->GetFrontBuffer()[j]); |
| 83 } |
| 84 LOG(INFO) << hex_packet; |
| 85 |
| 86 size_t read = secure_socket.Read(read_buf, 2048, NULL); |
| 87 EXPECT_EQ(kWritePattern.length(), read); |
| 88 EXPECT_EQ(0, memcmp(kWritePattern.data(), read_buf->data(), |
| 89 kWritePattern.length())); |
| 90 } |
| 91 } |
| 92 |
| 93 TEST(SecureP2PSocketTest, ReadRetry) { |
| 94 TestSocket* test_socket = new TestSocket(); |
| 95 SecureP2PSocket secure_socket(test_socket, "1234567890123456"); |
| 96 |
| 97 const std::string kWritePattern = "Hello world! This is a nice day."; |
| 98 scoped_refptr<net::IOBuffer> write_buf = |
| 99 new net::StringIOBuffer(kWritePattern); |
| 100 scoped_refptr<net::IOBuffer> read_buf = new net::IOBufferWithSize(2048); |
| 101 |
| 102 // Push some garbage to the socket. |
| 103 test_socket->PushBuffer("0"); |
| 104 test_socket->PushBuffer("00"); |
| 105 test_socket->PushBuffer("0000"); |
| 106 test_socket->PushBuffer("00000000"); |
| 107 test_socket->PushBuffer("0000000000000000"); |
| 108 test_socket->PushBuffer("00000000000000000000000000000000"); |
| 109 |
| 110 // Then write some real stuff. |
| 111 size_t written = secure_socket.Write(write_buf, |
| 112 kWritePattern.length(), NULL); |
| 113 EXPECT_EQ(kWritePattern.length(), written); |
| 114 |
| 115 size_t read = secure_socket.Read(read_buf, 2048, NULL); |
| 116 EXPECT_EQ(kWritePattern.length(), read); |
| 117 EXPECT_EQ(0, memcmp(kWritePattern.data(), read_buf->data(), |
| 118 kWritePattern.length())); |
| 119 } |
| 120 |
| 121 } // namespace protocol |
| 122 } // namespace remoting |
OLD | NEW |