Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: net/udp/udp_socket_unittest.cc

Issue 8200011: Add NetLog support to UDP sockets. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: ??? Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/udp/udp_socket_libevent.cc ('k') | net/udp/udp_socket_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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 "net/udp/udp_client_socket.h" 5 #include "net/udp/udp_client_socket.h"
6 #include "net/udp/udp_server_socket.h" 6 #include "net/udp/udp_server_socket.h"
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "net/base/io_buffer.h" 12 #include "net/base/io_buffer.h"
13 #include "net/base/ip_endpoint.h" 13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_errors.h" 14 #include "net/base/net_errors.h"
15 #include "net/base/net_log_unittest.h"
15 #include "net/base/net_test_suite.h" 16 #include "net/base/net_test_suite.h"
16 #include "net/base/net_util.h" 17 #include "net/base/net_util.h"
17 #include "net/base/sys_addrinfo.h" 18 #include "net/base/sys_addrinfo.h"
18 #include "net/base/test_completion_callback.h" 19 #include "net/base/test_completion_callback.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/platform_test.h" 21 #include "testing/platform_test.h"
21 22
22 namespace net { 23 namespace net {
23 24
24 namespace { 25 namespace {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 *address = IPEndPoint(ip_number, port); 125 *address = IPEndPoint(ip_number, port);
125 } 126 }
126 127
127 TEST_F(UDPSocketTest, Connect) { 128 TEST_F(UDPSocketTest, Connect) {
128 const int kPort = 9999; 129 const int kPort = 9999;
129 std::string simple_message("hello world!"); 130 std::string simple_message("hello world!");
130 131
131 // Setup the server to listen. 132 // Setup the server to listen.
132 IPEndPoint bind_address; 133 IPEndPoint bind_address;
133 CreateUDPAddress("0.0.0.0", kPort, &bind_address); 134 CreateUDPAddress("0.0.0.0", kPort, &bind_address);
134 UDPServerSocket server(NULL, NetLog::Source()); 135 CapturingNetLog server_log(CapturingNetLog::kUnbounded);
135 int rv = server.Listen(bind_address); 136 scoped_ptr<UDPServerSocket> server(
137 new UDPServerSocket(&server_log, NetLog::Source()));
138 int rv = server->Listen(bind_address);
136 EXPECT_EQ(OK, rv); 139 EXPECT_EQ(OK, rv);
137 140
138 // Setup the client. 141 // Setup the client.
139 IPEndPoint server_address; 142 IPEndPoint server_address;
140 CreateUDPAddress("127.0.0.1", kPort, &server_address); 143 CreateUDPAddress("127.0.0.1", kPort, &server_address);
141 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, 144 CapturingNetLog client_log(CapturingNetLog::kUnbounded);
142 RandIntCallback(), 145 scoped_ptr<UDPClientSocket> client(
143 NULL, 146 new UDPClientSocket(DatagramSocket::DEFAULT_BIND,
144 NetLog::Source()); 147 RandIntCallback(),
145 rv = client.Connect(server_address); 148 &client_log,
149 NetLog::Source()));
150 rv = client->Connect(server_address);
146 EXPECT_EQ(OK, rv); 151 EXPECT_EQ(OK, rv);
147 152
148 // Client sends to the server. 153 // Client sends to the server.
149 rv = WriteSocket(&client, simple_message); 154 rv = WriteSocket(client.get(), simple_message);
150 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); 155 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
151 156
152 // Server waits for message. 157 // Server waits for message.
153 std::string str = RecvFromSocket(&server); 158 std::string str = RecvFromSocket(server.get());
154 DCHECK(simple_message == str); 159 DCHECK(simple_message == str);
155 160
156 // Server echoes reply. 161 // Server echoes reply.
157 rv = SendToSocket(&server, simple_message); 162 rv = SendToSocket(server.get(), simple_message);
158 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); 163 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv));
159 164
160 // Client waits for response. 165 // Client waits for response.
161 str = ReadSocket(&client); 166 str = ReadSocket(client.get());
162 DCHECK(simple_message == str); 167 DCHECK(simple_message == str);
168
169 // Delete sockets so they log their final events.
170 server.reset();
171 client.reset();
172
173 // Check the server's log.
174 CapturingNetLog::EntryList server_entries;
175 server_log.GetEntries(&server_entries);
176 EXPECT_EQ(4u, server_entries.size());
177 EXPECT_TRUE(LogContainsBeginEvent(
178 server_entries, 0, NetLog::TYPE_SOCKET_ALIVE));
179 EXPECT_TRUE(LogContainsEvent(
180 server_entries, 1, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE));
181 EXPECT_TRUE(LogContainsEvent(
182 server_entries, 2, NetLog::TYPE_UDP_BYTES_SENT, NetLog::PHASE_NONE));
183 EXPECT_TRUE(LogContainsEndEvent(
184 server_entries, 3, NetLog::TYPE_SOCKET_ALIVE));
185
186 // Check the client's log.
187 CapturingNetLog::EntryList client_entries;
188 client_log.GetEntries(&client_entries);
189 EXPECT_EQ(6u, client_entries.size());
190 EXPECT_TRUE(LogContainsBeginEvent(
191 client_entries, 0, NetLog::TYPE_SOCKET_ALIVE));
192 EXPECT_TRUE(LogContainsBeginEvent(
193 client_entries, 1, NetLog::TYPE_UDP_CONNECT));
194 EXPECT_TRUE(LogContainsEndEvent(
195 client_entries, 2, NetLog::TYPE_UDP_CONNECT));
196 EXPECT_TRUE(LogContainsEvent(
197 client_entries, 3, NetLog::TYPE_UDP_BYTES_SENT, NetLog::PHASE_NONE));
198 EXPECT_TRUE(LogContainsEvent(
199 client_entries, 4, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE));
200 EXPECT_TRUE(LogContainsEndEvent(
201 client_entries, 5, NetLog::TYPE_SOCKET_ALIVE));
163 } 202 }
164 203
165 // In this test, we verify that random binding logic works, which attempts 204 // In this test, we verify that random binding logic works, which attempts
166 // to bind to a random port and returns if succeeds, otherwise retries for 205 // to bind to a random port and returns if succeeds, otherwise retries for
167 // |kBindRetries| number of times. 206 // |kBindRetries| number of times.
168 207
169 // To generate the scenario, we first create |kBindRetries| number of 208 // To generate the scenario, we first create |kBindRetries| number of
170 // UDPClientSockets with default binding policy and connect to the same 209 // UDPClientSockets with default binding policy and connect to the same
171 // peer and save the used port numbers. Then we get rid of the last 210 // peer and save the used port numbers. Then we get rid of the last
172 // socket, making sure that the local port it was bound to is available. 211 // socket, making sure that the local port it was bound to is available.
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 } tests[] = { 353 } tests[] = {
315 { "127.0.00.1", "127.0.0.1", false }, 354 { "127.0.00.1", "127.0.0.1", false },
316 { "192.168.1.1", "127.0.0.1", false }, 355 { "192.168.1.1", "127.0.0.1", false },
317 { "::1", "::1", true }, 356 { "::1", "::1", true },
318 { "2001:db8:0::42", "::1", true }, 357 { "2001:db8:0::42", "::1", true },
319 }; 358 };
320 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) { 359 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) {
321 SCOPED_TRACE(std::string("Connecting from ") + tests[i].local_address + 360 SCOPED_TRACE(std::string("Connecting from ") + tests[i].local_address +
322 std::string(" to ") + tests[i].remote_address); 361 std::string(" to ") + tests[i].remote_address);
323 362
324 net::IPAddressNumber ip_number; 363 IPAddressNumber ip_number;
325 net::ParseIPLiteralToNumber(tests[i].remote_address, &ip_number); 364 ParseIPLiteralToNumber(tests[i].remote_address, &ip_number);
326 net::IPEndPoint remote_address(ip_number, 80); 365 IPEndPoint remote_address(ip_number, 80);
327 net::ParseIPLiteralToNumber(tests[i].local_address, &ip_number); 366 ParseIPLiteralToNumber(tests[i].local_address, &ip_number);
328 net::IPEndPoint local_address(ip_number, 80); 367 IPEndPoint local_address(ip_number, 80);
329 368
330 UDPClientSocket client(DatagramSocket::DEFAULT_BIND, 369 UDPClientSocket client(DatagramSocket::DEFAULT_BIND,
331 RandIntCallback(), 370 RandIntCallback(),
332 NULL, 371 NULL,
333 NetLog::Source()); 372 NetLog::Source());
334 int rv = client.Connect(remote_address); 373 int rv = client.Connect(remote_address);
335 if (tests[i].may_fail && rv == ERR_ADDRESS_UNREACHABLE) { 374 if (tests[i].may_fail && rv == ERR_ADDRESS_UNREACHABLE) {
336 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6 375 // Connect() may return ERR_ADDRESS_UNREACHABLE for IPv6
337 // addresses if IPv6 is not configured. 376 // addresses if IPv6 is not configured.
338 continue; 377 continue;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 EXPECT_EQ(rv, ERR_IO_PENDING); 439 EXPECT_EQ(rv, ERR_IO_PENDING);
401 440
402 server.Close(); 441 server.Close();
403 442
404 EXPECT_FALSE(callback.have_result()); 443 EXPECT_FALSE(callback.have_result());
405 } 444 }
406 445
407 } // namespace 446 } // namespace
408 447
409 } // namespace net 448 } // namespace net
OLDNEW
« no previous file with comments | « net/udp/udp_socket_libevent.cc ('k') | net/udp/udp_socket_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698