diff options
-rwxr-xr-x | lib/zephyr_tests.py | 20 | ||||
-rw-r--r-- | lib/zephyr_tests.txt | 13 |
2 files changed, 31 insertions, 2 deletions
diff --git a/lib/zephyr_tests.py b/lib/zephyr_tests.py index 043ed2b..5855238 100755 --- a/lib/zephyr_tests.py +++ b/lib/zephyr_tests.py @@ -593,6 +593,26 @@ def find_buildpath(): return os.path.join(opts.builddir, "lib") +def getsockname(fd): + """wrapped C lib getsocketname (works on raw fd)""" + libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("c")) + + call_getsockname = libc.getsockname + call_getsockname.argtypes = [ + c_int, # int s + POINTER(sockaddr), # struct sockaddr *name + POINTER(c_int), # socklen_t *namelen + ] + name = sockaddr(0) + namelen = c_int(sizeof(name)) + ret = call_getsockname(fd, name, namelen) + if ret == 0: + return name + # we can't get at errno until python 2.6... + print ret + raise EnvironmentError("getsockname failed") + + if __name__ == "__main__": tester = ZephyrTestSuite(builddir=find_buildpath()) tester.setup() diff --git a/lib/zephyr_tests.txt b/lib/zephyr_tests.txt index 906399a..a30cb88 100644 --- a/lib/zephyr_tests.txt +++ b/lib/zephyr_tests.txt @@ -176,6 +176,17 @@ Trivial test of ZOpenPort and ZClosePort: >>> st = _z.ZOpenPort(port) >>> assert st == 0 >>> assert _z.ZGetFD() != -1 + >>> zsock = zephyr_tests.getsockname(_z.ZGetFD()) + >>> assert zsock + >>> from socket import AF_INET + >>> assert zsock.sa_family.value == AF_INET, zsock.sa_family + +(Here we're actually using getsockname as an "is that file descriptor + a socket" test; the wrapper is weak in that it can't check for + ENOTSOCK without requiring Python 2.6, so it just throws an exception + on any return of -1. If ctypes.cast worked on sockaddr, we could + also cast it to sockaddr_in and look at the address and port...) + >>> assert port != 0 >>> zephyr_tests.ctypes_pprint(_z.ZGetDestAddr()) ... # doctest: +ELLIPSIS @@ -184,8 +195,6 @@ Trivial test of ZOpenPort and ZClosePort: sin_addr 127.0.0.1 sin_zero [ignored] -TODO: consider checking that ZGetFD is returning a socket on that port. - >>> assert _z.ZClosePort() == 0 >>> assert _z.ZGetFD() == -1 |