summaryrefslogtreecommitdiffstats
path: root/net/udp
diff options
context:
space:
mode:
authorttuttle@chromium.org <ttuttle@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-02-22 22:44:44 +0000
committerttuttle@chromium.org <ttuttle@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-02-22 22:44:44 +0000
commit391de68d8e22f47a7075b377b08bb41f3d085112 (patch)
tree588ee9cb18e2bfad9437bcdca70465b755ee64d1 /net/udp
parenta8561f482dd40f5a125020a215b177bfb312fb12 (diff)
downloadchromium_src-391de68d8e22f47a7075b377b08bb41f3d085112.zip
chromium_src-391de68d8e22f47a7075b377b08bb41f3d085112.tar.gz
chromium_src-391de68d8e22f47a7075b377b08bb41f3d085112.tar.bz2
Make UDPSocket{Libevent,Win}::Connect close socket if it fails
Right now, we leave the socket in an indeterminate state -- to the rest of the methods, it looks like Connect succeeded, but to the caller, it doesn't. Review URL: https://chromiumcodereview.appspot.com/12259034 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@184197 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/udp')
-rw-r--r--net/udp/udp_socket_libevent.cc2
-rw-r--r--net/udp/udp_socket_unittest.cc22
-rw-r--r--net/udp/udp_socket_win.cc2
3 files changed, 26 insertions, 0 deletions
diff --git a/net/udp/udp_socket_libevent.cc b/net/udp/udp_socket_libevent.cc
index f67c145..063da08 100644
--- a/net/udp/udp_socket_libevent.cc
+++ b/net/udp/udp_socket_libevent.cc
@@ -215,6 +215,8 @@ int UDPSocketLibevent::Connect(const IPEndPoint& address) {
net_log_.BeginEvent(NetLog::TYPE_UDP_CONNECT,
CreateNetLogUDPConnectCallback(&address));
int rv = InternalConnect(address);
+ if (rv != OK)
+ Close();
net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv);
return rv;
}
diff --git a/net/udp/udp_socket_unittest.cc b/net/udp/udp_socket_unittest.cc
index a80da28..6377de9 100644
--- a/net/udp/udp_socket_unittest.cc
+++ b/net/udp/udp_socket_unittest.cc
@@ -336,6 +336,28 @@ TEST_F(UDPSocketTest, ConnectRandomBind) {
STLDeleteElements(&sockets);
}
+// Return a privileged port (under 1024) so binding will fail.
+int PrivilegedRand(int min, int max) {
+ // Chosen by fair dice roll. Guaranteed to be random.
+ return 4;
+}
+
+TEST_F(UDPSocketTest, ConnectFail) {
+ IPEndPoint peer_address;
+ CreateUDPAddress("0.0.0.0", 53, &peer_address);
+
+ scoped_ptr<UDPSocket> socket(
+ new UDPSocket(DatagramSocket::RANDOM_BIND,
+ base::Bind(&PrivilegedRand),
+ NULL,
+ NetLog::Source()));
+ int rv = socket->Connect(peer_address);
+ // Connect should have failed since we couldn't bind to that port,
+ EXPECT_NE(OK, rv);
+ // Make sure that UDPSocket actually closed the socket.
+ EXPECT_FALSE(socket->is_connected());
+}
+
// In this test, we verify that connect() on a socket will have the effect
// of filtering reads on this socket only to data read from the destination
// we connected to.
diff --git a/net/udp/udp_socket_win.cc b/net/udp/udp_socket_win.cc
index 71caf17..5c6da9f 100644
--- a/net/udp/udp_socket_win.cc
+++ b/net/udp/udp_socket_win.cc
@@ -307,6 +307,8 @@ int UDPSocketWin::Connect(const IPEndPoint& address) {
net_log_.BeginEvent(NetLog::TYPE_UDP_CONNECT,
CreateNetLogUDPConnectCallback(&address));
int rv = InternalConnect(address);
+ if (rv != OK)
+ Close();
net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv);
return rv;
}