diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-09 19:26:04 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-09 19:26:04 +0000 |
commit | 43d4a026d2dfd4301e4cc25fa3f8f07c7e20278a (patch) | |
tree | 347b3162e50b452a805d7707d7f8eddbe1d71005 /net/udp/udp_socket_unittest.cc | |
parent | a8a6012b1346f64cec04ce8f4ca592cdae77ec2d (diff) | |
download | chromium_src-43d4a026d2dfd4301e4cc25fa3f8f07c7e20278a.zip chromium_src-43d4a026d2dfd4301e4cc25fa3f8f07c7e20278a.tar.gz chromium_src-43d4a026d2dfd4301e4cc25fa3f8f07c7e20278a.tar.bz2 |
Use IPEndPoint for UDP sockets.
BUG=None
TEST=Unittest
Review URL: http://codereview.chromium.org/6650018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77498 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/udp/udp_socket_unittest.cc')
-rw-r--r-- | net/udp/udp_socket_unittest.cc | 83 |
1 files changed, 29 insertions, 54 deletions
diff --git a/net/udp/udp_socket_unittest.cc b/net/udp/udp_socket_unittest.cc index aaf3dfa..dd2bb711 100644 --- a/net/udp/udp_socket_unittest.cc +++ b/net/udp/udp_socket_unittest.cc @@ -8,6 +8,7 @@ #include "base/basictypes.h" #include "base/metrics/histogram.h" #include "net/base/io_buffer.h" +#include "net/base/ip_endpoint.h" #include "net/base/net_errors.h" #include "net/base/net_test_suite.h" #include "net/base/net_util.h" @@ -24,18 +25,14 @@ class UDPSocketTest : public PlatformTest { public: UDPSocketTest() : buffer_(new IOBufferWithSize(kMaxRead)) { - recv_from_address_length_ = sizeof(recv_from_storage_); - recv_from_address_ = - reinterpret_cast<struct sockaddr*>(&recv_from_storage_); } // Blocks until data is read from the socket. std::string RecvFromSocket(UDPServerSocket* socket) { TestCompletionCallback callback; - recv_from_address_length_ = sizeof(struct sockaddr_in6); - int rv = socket->RecvFrom(buffer_, kMaxRead, recv_from_address_, - &recv_from_address_length_, &callback); + int rv = socket->RecvFrom(buffer_, kMaxRead, &recv_from_address_, + &callback); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); if (rv < 0) @@ -45,20 +42,18 @@ class UDPSocketTest : public PlatformTest { // Loop until |msg| has been written to the socket or until an // error occurs. - // If |sockaddr| is non-NULL, then |sockaddr| and |sockaddr_len| are used - // for the destination to send to. Otherwise, will send to the last socket - // this server received from. + // If |address| is specified, then it is used for the destination + // to send to. Otherwise, will send to the last socket this server + // received from. + int SendToSocket(UDPServerSocket* socket, std::string msg) { + return SendToSocket(socket, msg, recv_from_address_); + } + int SendToSocket(UDPServerSocket* socket, std::string msg, - struct sockaddr* sockaddr, - socklen_t sockaddr_len) { + const IPEndPoint& address) { TestCompletionCallback callback; - if (sockaddr == NULL) { - sockaddr = recv_from_address_; - sockaddr_len = recv_from_address_length_; - } - int length = msg.length(); scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); scoped_refptr<DrainableIOBuffer> buffer( @@ -67,8 +62,7 @@ class UDPSocketTest : public PlatformTest { int bytes_sent = 0; while (buffer->BytesRemaining()) { int rv = socket->SendTo(buffer, buffer->BytesRemaining(), - sockaddr, sockaddr_len, - &callback); + address, &callback); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); if (rv <= 0) @@ -82,7 +76,6 @@ class UDPSocketTest : public PlatformTest { std::string ReadSocket(UDPClientSocket* socket) { TestCompletionCallback callback; - recv_from_address_length_ = sizeof(struct sockaddr_in6); int rv = socket->Read(buffer_, kMaxRead, &callback); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); @@ -117,18 +110,16 @@ class UDPSocketTest : public PlatformTest { private: static const int kMaxRead = 1024; scoped_refptr<IOBufferWithSize> buffer_; - struct sockaddr_storage recv_from_storage_; - struct sockaddr* recv_from_address_; - socklen_t recv_from_address_length_; + struct IPEndPoint recv_from_address_; }; // Creates and address from an ip/port and returns it in |address|. -void CreateUDPAddress(std::string ip_str, int port, AddressList* address) { +void CreateUDPAddress(std::string ip_str, int port, IPEndPoint* address) { IPAddressNumber ip_number; bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); if (!rv) return; - *address = AddressList(ip_number, port, false); + *address = IPEndPoint(ip_number, port); } TEST_F(UDPSocketTest, Connect) { @@ -136,14 +127,14 @@ TEST_F(UDPSocketTest, Connect) { std::string simple_message("hello world!"); // Setup the server to listen. - AddressList bind_address; + IPEndPoint bind_address; CreateUDPAddress("0.0.0.0", kPort, &bind_address); UDPServerSocket server(NULL, NetLog::Source()); int rv = server.Listen(bind_address); EXPECT_EQ(OK, rv); // Setup the client. - AddressList server_address; + IPEndPoint server_address; CreateUDPAddress("127.0.0.1", kPort, &server_address); UDPClientSocket client(NULL, NetLog::Source()); rv = client.Connect(server_address); @@ -158,7 +149,7 @@ TEST_F(UDPSocketTest, Connect) { DCHECK(simple_message == str); // Server echoes reply. - rv = SendToSocket(&server, simple_message, NULL, 0); + rv = SendToSocket(&server, simple_message); EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); // Client waits for response. @@ -181,7 +172,7 @@ TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { std::string foreign_message("BAD MESSAGE TO GET!!"); // Setup the first server to listen. - AddressList bind_address; + IPEndPoint bind_address; CreateUDPAddress("0.0.0.0", kPort1, &bind_address); UDPServerSocket server1(NULL, NetLog::Source()); int rv = server1.Listen(bind_address); @@ -194,7 +185,7 @@ TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { EXPECT_EQ(OK, rv); // Setup the client, connected to server 1. - AddressList server_address; + IPEndPoint server_address; CreateUDPAddress("127.0.0.1", kPort1, &server_address); UDPClientSocket client(NULL, NetLog::Source()); rv = client.Connect(server_address); @@ -209,20 +200,18 @@ TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { DCHECK(simple_message == str); // Get the client's address. - AddressList client_address; + IPEndPoint client_address; rv = client.GetLocalAddress(&client_address); EXPECT_EQ(OK, rv); // Server2 sends reply. rv = SendToSocket(&server2, foreign_message, - client_address.head()->ai_addr, - client_address.head()->ai_addrlen); + client_address); EXPECT_EQ(foreign_message.length(), static_cast<size_t>(rv)); // Server1 sends reply. rv = SendToSocket(&server1, simple_message, - client_address.head()->ai_addr, - client_address.head()->ai_addrlen); + client_address); EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); // Client waits for response. @@ -244,43 +233,29 @@ TEST_F(UDPSocketTest, ClientGetLocalPeerAddresses) { for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) { net::IPAddressNumber ip_number; net::ParseIPLiteralToNumber(tests[i].remote_address, &ip_number); - net::AddressList remote_address(ip_number, 80, true); + net::IPEndPoint remote_address(ip_number, 80); net::ParseIPLiteralToNumber(tests[i].local_address, &ip_number); - net::AddressList local_address(ip_number, 80, true); + net::IPEndPoint local_address(ip_number, 80); UDPClientSocket client(NULL, NetLog::Source()); int rv = client.Connect(remote_address); EXPECT_LE(ERR_IO_PENDING, rv); - AddressList fetched_local_address; + IPEndPoint fetched_local_address; rv = client.GetLocalAddress(&fetched_local_address); EXPECT_EQ(OK, rv); - const struct addrinfo* a1 = local_address.head(); - const struct addrinfo* a2 = fetched_local_address.head(); - EXPECT_TRUE(a1 != NULL); - EXPECT_TRUE(a2 != NULL); - - EXPECT_EQ(a1->ai_family, a2->ai_family); - EXPECT_EQ(a1->ai_addrlen, a2->ai_addrlen); // TODO(mbelshe): figure out how to verify the IP and port. // The port is dynamically generated by the udp stack. // The IP is the real IP of the client, not necessarily // loopback. - //EXPECT_EQ(NetAddressToString(a1), NetAddressToString(a2)); + //EXPECT_EQ(local_address.address(), fetched_local_address.address()); - AddressList fetched_remote_address; + IPEndPoint fetched_remote_address; rv = client.GetPeerAddress(&fetched_remote_address); EXPECT_EQ(OK, rv); - a1 = remote_address.head(); - a2 = fetched_remote_address.head(); - EXPECT_TRUE(a1 != NULL); - EXPECT_TRUE(a2 != NULL); - - EXPECT_EQ(a1->ai_family, a2->ai_family); - EXPECT_EQ(a1->ai_addrlen, a2->ai_addrlen); - EXPECT_EQ(NetAddressToString(a1), NetAddressToString(a2)); + EXPECT_EQ(remote_address, fetched_remote_address); } } |