OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 <algorithm> | |
6 #include <memory> | |
7 #include <string> | |
8 | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "blimp/common/create_blimp_message.h" | |
11 #include "blimp/common/proto/blimp_message.pb.h" | |
12 #include "blimp/common/proto/protocol_control.pb.h" | |
13 #include "blimp/net/blimp_connection.h" | |
14 #include "blimp/net/blimp_stats.h" | |
15 #include "blimp/net/tcp_client_transport.h" | |
16 #include "blimp/net/tcp_engine_transport.h" | |
17 #include "blimp/net/test_common.h" | |
18 #include "net/base/address_list.h" | |
19 #include "net/base/ip_address.h" | |
20 #include "net/base/ip_endpoint.h" | |
21 #include "net/base/net_errors.h" | |
22 #include "net/base/test_completion_callback.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 using testing::_; | |
26 using testing::SaveArg; | |
27 | |
28 namespace blimp { | |
29 | |
30 namespace { | |
31 | |
32 // Integration test for TCPEngineTransport and TCPClientTransport. | |
33 class TCPTransportTest : public testing::Test { | |
34 protected: | |
35 TCPTransportTest() | |
36 : local_address_(net::IPAddress(127, 0, 0, 1), 0), | |
37 engine_(local_address_, nullptr), | |
38 read_buffer_(new net::GrowableIOBuffer) { | |
39 size_t buf_size = std::max(payload_1_.size(), payload_2_.size()); | |
40 write_buffer_ = make_scoped_refptr( | |
41 new net::DrainableIOBuffer(new net::IOBuffer(buf_size), buf_size)); | |
42 read_buffer_->SetCapacity(buf_size); | |
43 } | |
44 | |
45 net::IPEndPoint GetLocalEndpoint() const { | |
46 net::IPEndPoint local_address; | |
47 engine_.GetLocalAddress(&local_address); | |
48 return local_address; | |
49 } | |
50 | |
51 std::string payload_1_ = "foo"; | |
52 std::string payload_2_ = "bar"; | |
53 base::MessageLoopForIO message_loop_; | |
54 net::IPEndPoint local_address_; | |
55 TCPEngineTransport engine_; | |
56 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | |
57 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
58 }; | |
59 | |
60 TEST_F(TCPTransportTest, Connect) { | |
61 net::TestCompletionCallback accept_callback; | |
62 engine_.Connect(accept_callback.callback()); | |
63 | |
64 net::TestCompletionCallback connect_callback; | |
65 TCPClientTransport client(GetLocalEndpoint(), nullptr); | |
66 client.Connect(connect_callback.callback()); | |
67 | |
68 EXPECT_EQ(net::OK, connect_callback.WaitForResult()); | |
69 EXPECT_EQ(net::OK, accept_callback.WaitForResult()); | |
70 EXPECT_NE(nullptr, client.TakeMessagePort()); | |
71 } | |
72 | |
73 TEST_F(TCPTransportTest, TwoClientConnections) { | |
74 net::TestCompletionCallback accept_callback1; | |
75 engine_.Connect(accept_callback1.callback()); | |
76 | |
77 net::TestCompletionCallback connect_callback1; | |
78 TCPClientTransport client(GetLocalEndpoint(), nullptr); | |
79 client.Connect(connect_callback1.callback()); | |
80 EXPECT_EQ(net::OK, connect_callback1.WaitForResult()); | |
81 EXPECT_EQ(net::OK, accept_callback1.WaitForResult()); | |
82 EXPECT_NE(nullptr, engine_.TakeMessagePort()); | |
83 | |
84 net::TestCompletionCallback accept_callback2; | |
85 engine_.Connect(accept_callback2.callback()); | |
86 | |
87 net::TestCompletionCallback connect_callback2; | |
88 TCPClientTransport client2(GetLocalEndpoint(), nullptr); | |
89 client2.Connect(connect_callback2.callback()); | |
90 EXPECT_EQ(net::OK, connect_callback2.WaitForResult()); | |
91 EXPECT_EQ(net::OK, accept_callback2.WaitForResult()); | |
92 EXPECT_NE(nullptr, engine_.TakeMessagePort()); | |
93 } | |
94 | |
95 TEST_F(TCPTransportTest, ExchangeMessages) { | |
96 // Start the Engine transport and connect a client to it. | |
97 net::TestCompletionCallback accept_callback; | |
98 engine_.Connect(accept_callback.callback()); | |
99 net::TestCompletionCallback client_connect_callback; | |
100 TCPClientTransport client(GetLocalEndpoint(), nullptr); | |
101 client.Connect(client_connect_callback.callback()); | |
102 EXPECT_EQ(net::OK, accept_callback.WaitForResult()); | |
103 EXPECT_EQ(net::OK, client_connect_callback.WaitForResult()); | |
104 | |
105 std::unique_ptr<MessagePort> engine_message_port = engine_.TakeMessagePort(); | |
106 std::unique_ptr<MessagePort> clientmessage_port = client.TakeMessagePort(); | |
107 | |
108 // Engine sends payload_1_ to client. | |
109 net::TestCompletionCallback read_cb1; | |
110 net::TestCompletionCallback write_cb1; | |
111 memcpy(write_buffer_->data(), payload_1_.data(), payload_1_.size()); | |
112 engine_message_port->writer()->WritePacket(write_buffer_, | |
113 write_cb1.callback()); | |
114 clientmessage_port->reader()->ReadPacket(read_buffer_, read_cb1.callback()); | |
115 EXPECT_EQ(payload_1_.size(), static_cast<size_t>(read_cb1.WaitForResult())); | |
116 EXPECT_EQ(net::OK, write_cb1.WaitForResult()); | |
117 EXPECT_TRUE( | |
118 BufferStartsWith(read_buffer_.get(), payload_1_.size(), payload_1_)); | |
119 | |
120 // Client sends payload_2_ to engine. | |
121 net::TestCompletionCallback read_cb2; | |
122 net::TestCompletionCallback write_cb2; | |
123 memcpy(write_buffer_->data(), payload_2_.data(), payload_2_.size()); | |
124 clientmessage_port->writer()->WritePacket(write_buffer_, | |
125 write_cb2.callback()); | |
126 engine_message_port->reader()->ReadPacket(read_buffer_, read_cb2.callback()); | |
127 EXPECT_EQ(payload_2_.size(), static_cast<size_t>(read_cb2.WaitForResult())); | |
128 EXPECT_EQ(net::OK, write_cb2.WaitForResult()); | |
129 EXPECT_TRUE( | |
130 BufferStartsWith(read_buffer_.get(), payload_2_.size(), payload_2_)); | |
131 } | |
132 | |
133 } // namespace | |
134 | |
135 } // namespace blimp | |
OLD | NEW |