1997-01-03 16:03:32 -04:00
|
|
|
# Not tested:
|
1998-03-26 15:42:58 -04:00
|
|
|
# socket.fromfd()
|
|
|
|
# sktobj.getsockopt()
|
|
|
|
# sktobj.recvfrom()
|
|
|
|
# sktobj.sendto()
|
|
|
|
# sktobj.setblocking()
|
|
|
|
# sktobj.setsockopt()
|
|
|
|
# sktobj.shutdown()
|
1997-01-03 16:03:32 -04:00
|
|
|
|
|
|
|
|
2001-01-17 17:51:36 -04:00
|
|
|
from test_support import verbose, TestFailed
|
1997-01-03 16:03:32 -04:00
|
|
|
import socket
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
def missing_ok(str):
|
|
|
|
try:
|
1998-03-26 15:42:58 -04:00
|
|
|
getattr(socket, str)
|
1997-01-03 16:03:32 -04:00
|
|
|
except AttributeError:
|
1998-03-26 15:42:58 -04:00
|
|
|
pass
|
1997-01-03 16:03:32 -04:00
|
|
|
|
1997-10-01 01:39:05 -03:00
|
|
|
try: raise socket.error
|
|
|
|
except socket.error: print "socket.error"
|
1997-01-03 16:03:32 -04:00
|
|
|
|
1997-05-08 22:54:45 -03:00
|
|
|
socket.AF_INET
|
1997-01-03 16:03:32 -04:00
|
|
|
|
1997-05-08 22:54:45 -03:00
|
|
|
socket.SOCK_STREAM
|
|
|
|
socket.SOCK_DGRAM
|
|
|
|
socket.SOCK_RAW
|
|
|
|
socket.SOCK_RDM
|
|
|
|
socket.SOCK_SEQPACKET
|
1997-01-03 16:03:32 -04:00
|
|
|
|
|
|
|
for optional in ("AF_UNIX",
|
|
|
|
|
1998-03-26 15:42:58 -04:00
|
|
|
"SO_DEBUG", "SO_ACCEPTCONN", "SO_REUSEADDR", "SO_KEEPALIVE",
|
|
|
|
"SO_DONTROUTE", "SO_BROADCAST", "SO_USELOOPBACK", "SO_LINGER",
|
|
|
|
"SO_OOBINLINE", "SO_REUSEPORT", "SO_SNDBUF", "SO_RCVBUF",
|
|
|
|
"SO_SNDLOWAT", "SO_RCVLOWAT", "SO_SNDTIMEO", "SO_RCVTIMEO",
|
|
|
|
"SO_ERROR", "SO_TYPE", "SOMAXCONN",
|
|
|
|
|
|
|
|
"MSG_OOB", "MSG_PEEK", "MSG_DONTROUTE", "MSG_EOR",
|
|
|
|
"MSG_TRUNC", "MSG_CTRUNC", "MSG_WAITALL", "MSG_BTAG",
|
|
|
|
"MSG_ETAG",
|
|
|
|
|
|
|
|
"SOL_SOCKET",
|
|
|
|
|
|
|
|
"IPPROTO_IP", "IPPROTO_ICMP", "IPPROTO_IGMP",
|
|
|
|
"IPPROTO_GGP", "IPPROTO_TCP", "IPPROTO_EGP",
|
|
|
|
"IPPROTO_PUP", "IPPROTO_UDP", "IPPROTO_IDP",
|
|
|
|
"IPPROTO_HELLO", "IPPROTO_ND", "IPPROTO_TP",
|
|
|
|
"IPPROTO_XTP", "IPPROTO_EON", "IPPROTO_BIP",
|
|
|
|
"IPPROTO_RAW", "IPPROTO_MAX",
|
|
|
|
|
|
|
|
"IPPORT_RESERVED", "IPPORT_USERRESERVED",
|
|
|
|
|
|
|
|
"INADDR_ANY", "INADDR_BROADCAST", "INADDR_LOOPBACK",
|
|
|
|
"INADDR_UNSPEC_GROUP", "INADDR_ALLHOSTS_GROUP",
|
|
|
|
"INADDR_MAX_LOCAL_GROUP", "INADDR_NONE",
|
|
|
|
|
|
|
|
"IP_OPTIONS", "IP_HDRINCL", "IP_TOS", "IP_TTL",
|
|
|
|
"IP_RECVOPTS", "IP_RECVRETOPTS", "IP_RECVDSTADDR",
|
|
|
|
"IP_RETOPTS", "IP_MULTICAST_IF", "IP_MULTICAST_TTL",
|
|
|
|
"IP_MULTICAST_LOOP", "IP_ADD_MEMBERSHIP",
|
|
|
|
"IP_DROP_MEMBERSHIP",
|
|
|
|
):
|
1997-01-03 16:03:32 -04:00
|
|
|
missing_ok(optional)
|
|
|
|
|
1997-06-03 14:58:31 -03:00
|
|
|
socktype = socket.SocketType
|
1997-01-03 16:03:32 -04:00
|
|
|
hostname = socket.gethostname()
|
|
|
|
ip = socket.gethostbyname(hostname)
|
|
|
|
hname, aliases, ipaddrs = socket.gethostbyaddr(ip)
|
1997-01-13 15:35:39 -04:00
|
|
|
all_host_names = [hname] + aliases
|
1997-01-03 16:03:32 -04:00
|
|
|
|
|
|
|
if verbose:
|
|
|
|
print hostname
|
|
|
|
print ip
|
|
|
|
print hname, aliases, ipaddrs
|
1997-01-13 15:35:39 -04:00
|
|
|
print all_host_names
|
1997-01-03 16:03:32 -04:00
|
|
|
|
1997-01-13 15:35:39 -04:00
|
|
|
for name in all_host_names:
|
2001-02-09 08:03:45 -04:00
|
|
|
if name.find('.'):
|
1998-03-26 15:42:58 -04:00
|
|
|
break
|
1997-01-13 15:35:39 -04:00
|
|
|
else:
|
|
|
|
print 'FQDN not found'
|
1997-01-03 16:03:32 -04:00
|
|
|
|
2001-03-23 13:40:16 -04:00
|
|
|
if hasattr(socket, 'getservbyname'):
|
|
|
|
print socket.getservbyname('telnet', 'tcp')
|
|
|
|
try:
|
|
|
|
socket.getservbyname('telnet', 'udp')
|
|
|
|
except socket.error:
|
|
|
|
pass
|
1997-01-03 16:03:32 -04:00
|
|
|
|
2001-12-09 04:57:46 -04:00
|
|
|
import sys
|
|
|
|
if not sys.platform.startswith('java'):
|
|
|
|
try:
|
|
|
|
# On some versions, this loses a reference
|
|
|
|
orig = sys.getrefcount(__name__)
|
|
|
|
socket.getnameinfo(__name__,0)
|
|
|
|
except SystemError:
|
|
|
|
if sys.getrefcount(__name__) <> orig:
|
|
|
|
raise TestFailed,"socket.getnameinfo loses a reference"
|
2001-11-02 19:34:52 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
# On some versions, this crashes the interpreter.
|
|
|
|
socket.getnameinfo(('x', 0, 0, 0), 0)
|
2001-11-09 16:37:43 -04:00
|
|
|
except socket.error:
|
2001-11-02 19:34:52 -04:00
|
|
|
pass
|
1997-01-03 16:03:32 -04:00
|
|
|
|
1997-05-14 18:12:51 -03:00
|
|
|
canfork = hasattr(os, 'fork')
|
1997-01-03 16:03:32 -04:00
|
|
|
try:
|
|
|
|
PORT = 50007
|
2002-06-06 18:08:16 -03:00
|
|
|
msg = 'socket test\n'
|
1997-05-14 18:12:51 -03:00
|
|
|
if not canfork or os.fork():
|
1998-03-26 15:42:58 -04:00
|
|
|
# parent is server
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2000-06-29 14:00:54 -03:00
|
|
|
s.bind(("127.0.0.1", PORT))
|
1998-03-26 15:42:58 -04:00
|
|
|
s.listen(1)
|
|
|
|
if verbose:
|
|
|
|
print 'parent accepting'
|
|
|
|
if canfork:
|
|
|
|
conn, addr = s.accept()
|
|
|
|
if verbose:
|
|
|
|
print 'connected by', addr
|
|
|
|
# couple of interesting tests while we've got a live socket
|
|
|
|
f = conn.fileno()
|
|
|
|
if verbose:
|
|
|
|
print 'fileno:', f
|
|
|
|
p = conn.getpeername()
|
|
|
|
if verbose:
|
|
|
|
print 'peer:', p
|
|
|
|
n = conn.getsockname()
|
|
|
|
if verbose:
|
|
|
|
print 'sockname:', n
|
|
|
|
f = conn.makefile()
|
|
|
|
if verbose:
|
|
|
|
print 'file obj:', f
|
2002-06-06 18:08:16 -03:00
|
|
|
data = conn.recv(1024)
|
|
|
|
if verbose:
|
|
|
|
print 'received:', data
|
|
|
|
conn.sendall(data)
|
|
|
|
|
|
|
|
# Perform a few tests on the windows file object
|
|
|
|
if verbose:
|
|
|
|
print "Staring _fileobject tests..."
|
|
|
|
f = socket._fileobject (conn, 'rb', 8192)
|
|
|
|
first_seg = f.read(7)
|
|
|
|
second_seg = f.read(5)
|
|
|
|
if not first_seg == 'socket ' or not second_seg == 'test\n':
|
|
|
|
print "Error performing read with the python _fileobject class"
|
|
|
|
os._exit (1)
|
|
|
|
elif verbose:
|
|
|
|
print "_fileobject buffered read works"
|
|
|
|
f.write (data)
|
|
|
|
f.flush ()
|
|
|
|
|
|
|
|
buf = ''
|
1998-03-26 15:42:58 -04:00
|
|
|
while 1:
|
2002-06-06 18:08:16 -03:00
|
|
|
char = f.read(1)
|
|
|
|
if not char:
|
|
|
|
print "Error performing unbuffered read with the python ", \
|
|
|
|
"_fileobject class"
|
|
|
|
os._exit (1)
|
|
|
|
buf += char
|
|
|
|
if buf == msg:
|
|
|
|
if verbose:
|
|
|
|
print "__fileobject unbuffered read works"
|
1998-03-26 15:42:58 -04:00
|
|
|
break
|
2002-06-06 18:08:16 -03:00
|
|
|
if verbose:
|
|
|
|
# If we got this far, write() must work as well
|
|
|
|
print "__fileobject write works"
|
|
|
|
f.write(buf)
|
|
|
|
f.flush()
|
|
|
|
|
|
|
|
line = f.readline()
|
|
|
|
if not line == msg:
|
|
|
|
print "Error perferming readline with the python _fileobject class"
|
|
|
|
os._exit (1)
|
|
|
|
f.write(line)
|
|
|
|
f.flush()
|
|
|
|
if verbose:
|
|
|
|
print "__fileobject readline works"
|
|
|
|
|
1998-03-26 15:42:58 -04:00
|
|
|
conn.close()
|
1997-01-03 16:03:32 -04:00
|
|
|
else:
|
1998-03-26 15:42:58 -04:00
|
|
|
try:
|
|
|
|
# child is client
|
|
|
|
time.sleep(5)
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
if verbose:
|
|
|
|
print 'child connecting'
|
2000-06-29 14:00:54 -03:00
|
|
|
s.connect(("127.0.0.1", PORT))
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
iteration = 0
|
|
|
|
while 1:
|
|
|
|
s.send(msg)
|
|
|
|
data = s.recv(12)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
if msg != data:
|
|
|
|
print "parent/client mismatch. Failed in %s iteration. Received: [%s]" \
|
|
|
|
%(iteration, data)
|
|
|
|
time.sleep (1)
|
|
|
|
iteration += 1
|
1998-03-26 15:42:58 -04:00
|
|
|
s.close()
|
|
|
|
finally:
|
|
|
|
os._exit(1)
|
1997-05-14 18:12:51 -03:00
|
|
|
except socket.error, msg:
|
|
|
|
raise TestFailed, msg
|