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 <string> |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/stringprintf.h" |
| 9 #include "crypto/symmetric_key.h" |
| 10 #include "net/base/io_buffer.h" |
| 11 #include "remoting/protocol/secure_p2p_socket.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace remoting { |
| 15 namespace protocol { |
| 16 |
| 17 namespace { |
| 18 class TestSocket : public net::Socket { |
| 19 public: |
| 20 TestSocket() {} |
| 21 |
| 22 // Socket implementation. |
| 23 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 24 net::CompletionCallback* callback) { |
| 25 memcpy(buf->data(), buffer_.data(), buffer_.length()); |
| 26 int size = buffer_.length(); |
| 27 buffer_.clear(); |
| 28 return size; |
| 29 } |
| 30 |
| 31 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 32 net::CompletionCallback* callback) { |
| 33 buffer_ = std::string(buf->data(), buf_len); |
| 34 return buf_len; |
| 35 } |
| 36 |
| 37 virtual bool SetReceiveBufferSize(int32 size) { |
| 38 return true; |
| 39 } |
| 40 |
| 41 virtual bool SetSendBufferSize(int32 size) { |
| 42 return true; |
| 43 } |
| 44 |
| 45 std::string GetBuffer() const { |
| 46 return buffer_; |
| 47 } |
| 48 |
| 49 private: |
| 50 std::string buffer_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(TestSocket); |
| 53 }; |
| 54 } // namespace |
| 55 |
| 56 TEST(SecureP2PSocketTest, WriteAndRead) { |
| 57 TestSocket test_socket; |
| 58 SecureP2PSocket secure_socket(&test_socket, "1234567890123456"); |
| 59 |
| 60 const std::string kWritePattern = "Hello world! This is a nice day."; |
| 61 scoped_refptr<net::IOBuffer> write_buf = |
| 62 new net::StringIOBuffer(kWritePattern); |
| 63 scoped_refptr<net::IOBuffer> read_buf = new net::IOBufferWithSize(2048); |
| 64 |
| 65 for (int i = 0; i < 5; ++i) { |
| 66 size_t written = secure_socket.Write(write_buf, |
| 67 kWritePattern.length(), NULL); |
| 68 EXPECT_EQ(kWritePattern.length(), written); |
| 69 EXPECT_EQ(kWritePattern.length() + 44, test_socket.GetBuffer().length()); |
| 70 |
| 71 std::string hex_packet; |
| 72 for (size_t j = 0; j < test_socket.GetBuffer().length(); ++j) { |
| 73 base::StringAppendF(&hex_packet, "%02x", |
| 74 (uint8)test_socket.GetBuffer()[j]); |
| 75 } |
| 76 LOG(INFO) << hex_packet; |
| 77 |
| 78 size_t read = secure_socket.Read(read_buf, 2048, NULL); |
| 79 EXPECT_EQ(kWritePattern.length(), read); |
| 80 EXPECT_EQ(0, memcmp(kWritePattern.data(), read_buf->data(), |
| 81 kWritePattern.length())); |
| 82 } |
| 83 } |
| 84 |
| 85 } // namespace protocol |
| 86 } // namespace remoting |
OLD | NEW |