Move SetSocketTimeout to csocket.
This allows it to be used in iproute.py, which does not (and
arguably should not) depend on net_test.py.
Bug: 68223648
Test: all_tests.sh passes on android-4.4
Change-Id: I4584a6ad179995468f885da8bd4b2f17dfc436c8
diff --git a/net/test/csocket.py b/net/test/csocket.py
index e041093..1dd58f1 100644
--- a/net/test/csocket.py
+++ b/net/test/csocket.py
@@ -74,7 +74,8 @@
libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True)
-# TODO: Move this to a utils.py or constants.py file, once we have one.
+# TODO: Unlike most of this file, these functions aren't specific to wrapping C
+# library calls. Move them to a utils.py or constants.py file, once we have one.
def LinuxVersion():
# Example: "3.4.67-00753-gb7a556f".
# Get the part before the dash.
@@ -85,6 +86,13 @@
return version
+def SetSocketTimeout(sock, ms):
+ s = ms / 1000
+ us = (ms % 1000) * 1000
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO,
+ struct.pack("LL", s, us))
+
+
def VoidPointer(s):
return ctypes.cast(s.CPointer(), ctypes.c_void_p)
diff --git a/net/test/leak_test.py b/net/test/leak_test.py
index fe7d207..8ef4b41 100755
--- a/net/test/leak_test.py
+++ b/net/test/leak_test.py
@@ -31,7 +31,7 @@
s.bind(("::1", 0))
# Call shutdown on another thread while a recvfrom is in progress.
- net_test.SetSocketTimeout(s, 2000)
+ csocket.SetSocketTimeout(s, 2000)
def ShutdownSocket():
time.sleep(0.5)
self.assertRaisesErrno(ENOTCONN, s.shutdown, SHUT_RDWR)
diff --git a/net/test/net_test.py b/net/test/net_test.py
index e78920f..72099e8 100755
--- a/net/test/net_test.py
+++ b/net/test/net_test.py
@@ -99,12 +99,6 @@
def AddressLengthBits(version):
return {4: 32, 6: 128}[version]
-def SetSocketTimeout(sock, ms):
- s = ms / 1000
- us = (ms % 1000) * 1000
- sock.setsockopt(SOL_SOCKET, SO_RCVTIMEO, struct.pack("LL", s, us))
-
-
def SetSocketTos(s, tos):
level = {AF_INET: SOL_IP, AF_INET6: SOL_IPV6}[s.family]
option = {AF_INET: IP_TOS, AF_INET6: IPV6_TCLASS}[s.family]
@@ -119,7 +113,7 @@
# Convenience functions to create sockets.
def Socket(family, sock_type, protocol):
s = socket(family, sock_type, protocol)
- SetSocketTimeout(s, 5000)
+ csocket.SetSocketTimeout(s, 5000)
return s
diff --git a/net/test/ping6_test.py b/net/test/ping6_test.py
index ff39e84..7a260e7 100755
--- a/net/test/ping6_test.py
+++ b/net/test/ping6_test.py
@@ -468,7 +468,7 @@
s5.bind(("0.0.0.0", 167))
s4.sendto(net_test.IPV4_PING, (net_test.IPV4_ADDR, 44))
self.assertValidPingResponse(s5, net_test.IPV4_PING)
- net_test.SetSocketTimeout(s4, 100)
+ csocket.SetSocketTimeout(s4, 100)
self.assertRaisesErrno(errno.EAGAIN, s4.recv, 32768)
# If SO_REUSEADDR is turned off, then we get EADDRINUSE.
diff --git a/net/test/xfrm_test.py b/net/test/xfrm_test.py
index 131e8e8..864fd18 100755
--- a/net/test/xfrm_test.py
+++ b/net/test/xfrm_test.py
@@ -23,6 +23,7 @@
import threading
import unittest
+import csocket
import cstruct
import multinetwork_base
import net_test
@@ -243,7 +244,7 @@
# be sent from srcport to port 53. Open another socket on that port, and
# apply the inbound policy to it.
twisted_socket = socket(AF_INET, SOCK_DGRAM, 0)
- net_test.SetSocketTimeout(twisted_socket, 100)
+ csocket.SetSocketTimeout(twisted_socket, 100)
twisted_socket.bind(("0.0.0.0", 53))
# TODO: why does this work without a per-socket policy applied?