blob: b602c91d65fb5e757bc6b0a2429c594c12a3d70a [file] [log] [blame]
Barry Warsawcf3d4b51997-01-03 20:03:32 +00001# Not tested:
2# socket.fromfd()
3# sktobj.getsockopt()
4# sktobj.recvfrom()
5# sktobj.sendto()
6# sktobj.setblocking()
7# sktobj.setsockopt()
8# sktobj.shutdown()
9
10
Guido van Rossum2b4d2801997-05-14 21:12:51 +000011from test_support import verbose, TestFailed
Barry Warsawcf3d4b51997-01-03 20:03:32 +000012import socket
13import os
14import time
Barry Warsawdbfb8d41997-01-13 19:35:39 +000015import string
Barry Warsawcf3d4b51997-01-03 20:03:32 +000016
17def missing_ok(str):
18 try:
19 getattr(socket, str)
20 except AttributeError:
21 pass
22
Guido van Rossuma008fa51997-10-01 04:39:05 +000023try: raise socket.error
24except socket.error: print "socket.error"
Barry Warsawcf3d4b51997-01-03 20:03:32 +000025
Guido van Rossumf518d5e1997-05-09 01:54:45 +000026socket.AF_INET
Barry Warsawcf3d4b51997-01-03 20:03:32 +000027
Guido van Rossumf518d5e1997-05-09 01:54:45 +000028socket.SOCK_STREAM
29socket.SOCK_DGRAM
30socket.SOCK_RAW
31socket.SOCK_RDM
32socket.SOCK_SEQPACKET
Barry Warsawcf3d4b51997-01-03 20:03:32 +000033
34for optional in ("AF_UNIX",
35
36 "SO_DEBUG", "SO_ACCEPTCONN", "SO_REUSEADDR", "SO_KEEPALIVE",
37 "SO_DONTROUTE", "SO_BROADCAST", "SO_USELOOPBACK", "SO_LINGER",
38 "SO_OOBINLINE", "SO_REUSEPORT", "SO_SNDBUF", "SO_RCVBUF",
39 "SO_SNDLOWAT", "SO_RCVLOWAT", "SO_SNDTIMEO", "SO_RCVTIMEO",
40 "SO_ERROR", "SO_TYPE", "SOMAXCONN",
41
42 "MSG_OOB", "MSG_PEEK", "MSG_DONTROUTE", "MSG_EOR",
43 "MSG_TRUNC", "MSG_CTRUNC", "MSG_WAITALL", "MSG_BTAG",
44 "MSG_ETAG",
45
46 "SOL_SOCKET",
47
48 "IPPROTO_IP", "IPPROTO_ICMP", "IPPROTO_IGMP",
49 "IPPROTO_GGP", "IPPROTO_TCP", "IPPROTO_EGP",
50 "IPPROTO_PUP", "IPPROTO_UDP", "IPPROTO_IDP",
51 "IPPROTO_HELLO", "IPPROTO_ND", "IPPROTO_TP",
52 "IPPROTO_XTP", "IPPROTO_EON", "IPPROTO_BIP",
53 "IPPROTO_RAW", "IPPROTO_MAX",
54
55 "IPPORT_RESERVED", "IPPORT_USERRESERVED",
56
57 "INADDR_ANY", "INADDR_BROADCAST", "INADDR_LOOPBACK",
58 "INADDR_UNSPEC_GROUP", "INADDR_ALLHOSTS_GROUP",
59 "INADDR_MAX_LOCAL_GROUP", "INADDR_NONE",
60
61 "IP_OPTIONS", "IP_HDRINCL", "IP_TOS", "IP_TTL",
62 "IP_RECVOPTS", "IP_RECVRETOPTS", "IP_RECVDSTADDR",
63 "IP_RETOPTS", "IP_MULTICAST_IF", "IP_MULTICAST_TTL",
64 "IP_MULTICAST_LOOP", "IP_ADD_MEMBERSHIP",
65 "IP_DROP_MEMBERSHIP",
66 ):
67 missing_ok(optional)
68
Fred Drakecf3527b1997-06-03 17:58:31 +000069socktype = socket.SocketType
Barry Warsawcf3d4b51997-01-03 20:03:32 +000070hostname = socket.gethostname()
71ip = socket.gethostbyname(hostname)
72hname, aliases, ipaddrs = socket.gethostbyaddr(ip)
Barry Warsawdbfb8d41997-01-13 19:35:39 +000073all_host_names = [hname] + aliases
Barry Warsawcf3d4b51997-01-03 20:03:32 +000074
75if verbose:
76 print hostname
77 print ip
78 print hname, aliases, ipaddrs
Barry Warsawdbfb8d41997-01-13 19:35:39 +000079 print all_host_names
Barry Warsawcf3d4b51997-01-03 20:03:32 +000080
Barry Warsawdbfb8d41997-01-13 19:35:39 +000081for name in all_host_names:
82 if string.find(name, '.'):
83 break
84else:
85 print 'FQDN not found'
Barry Warsawcf3d4b51997-01-03 20:03:32 +000086
87print socket.getservbyname('telnet', 'tcp')
88try:
89 socket.getservbyname('telnet', 'udp')
90except socket.error:
91 pass
92
93
Guido van Rossum2b4d2801997-05-14 21:12:51 +000094canfork = hasattr(os, 'fork')
Barry Warsawcf3d4b51997-01-03 20:03:32 +000095try:
96 PORT = 50007
Guido van Rossum2b4d2801997-05-14 21:12:51 +000097 if not canfork or os.fork():
Barry Warsawdce10051997-01-07 21:02:06 +000098 # parent is server
Barry Warsawcf3d4b51997-01-03 20:03:32 +000099 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
100 s.bind(hostname, PORT)
101 s.listen(1)
102 if verbose:
103 print 'parent accepting'
Guido van Rossum2b4d2801997-05-14 21:12:51 +0000104 if canfork:
105 conn, addr = s.accept()
Barry Warsawcf3d4b51997-01-03 20:03:32 +0000106 if verbose:
Guido van Rossum2b4d2801997-05-14 21:12:51 +0000107 print 'connected by', addr
108 # couple of interesting tests while we've got a live socket
109 f = conn.fileno()
110 if verbose:
111 print 'fileno:', f
112 p = conn.getpeername()
113 if verbose:
114 print 'peer:', p
115 n = conn.getsockname()
116 if verbose:
117 print 'sockname:', n
118 f = conn.makefile()
119 if verbose:
120 print 'file obj:', f
121 while 1:
122 data = conn.recv(1024)
123 if not data:
124 break
125 if verbose:
126 print 'received:', data
127 conn.send(data)
128 conn.close()
Barry Warsawcf3d4b51997-01-03 20:03:32 +0000129 else:
Guido van Rossum1d1394e1997-01-17 16:13:15 +0000130 try:
131 # child is client
Guido van Rossum95222741997-05-16 13:40:41 +0000132 time.sleep(5)
Guido van Rossum1d1394e1997-01-17 16:13:15 +0000133 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
134 if verbose:
135 print 'child connecting'
136 s.connect(hostname, PORT)
137 msg = 'socket test'
138 s.send(msg)
139 data = s.recv(1024)
140 if msg <> data:
141 print 'parent/client mismatch'
142 s.close()
143 finally:
144 os._exit(1)
Guido van Rossum2b4d2801997-05-14 21:12:51 +0000145except socket.error, msg:
146 raise TestFailed, msg