Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 1 | # 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 Rossum | 2b4d280 | 1997-05-14 21:12:51 +0000 | [diff] [blame] | 11 | from test_support import verbose, TestFailed |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 12 | import socket |
| 13 | import os |
| 14 | import time |
Barry Warsaw | dbfb8d4 | 1997-01-13 19:35:39 +0000 | [diff] [blame] | 15 | import string |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 16 | |
| 17 | def missing_ok(str): |
| 18 | try: |
| 19 | getattr(socket, str) |
| 20 | except AttributeError: |
| 21 | pass |
| 22 | |
Guido van Rossum | a008fa5 | 1997-10-01 04:39:05 +0000 | [diff] [blame] | 23 | try: raise socket.error |
| 24 | except socket.error: print "socket.error" |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | f518d5e | 1997-05-09 01:54:45 +0000 | [diff] [blame] | 26 | socket.AF_INET |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | f518d5e | 1997-05-09 01:54:45 +0000 | [diff] [blame] | 28 | socket.SOCK_STREAM |
| 29 | socket.SOCK_DGRAM |
| 30 | socket.SOCK_RAW |
| 31 | socket.SOCK_RDM |
| 32 | socket.SOCK_SEQPACKET |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 33 | |
| 34 | for 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 Drake | cf3527b | 1997-06-03 17:58:31 +0000 | [diff] [blame] | 69 | socktype = socket.SocketType |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 70 | hostname = socket.gethostname() |
| 71 | ip = socket.gethostbyname(hostname) |
| 72 | hname, aliases, ipaddrs = socket.gethostbyaddr(ip) |
Barry Warsaw | dbfb8d4 | 1997-01-13 19:35:39 +0000 | [diff] [blame] | 73 | all_host_names = [hname] + aliases |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 74 | |
| 75 | if verbose: |
| 76 | print hostname |
| 77 | print ip |
| 78 | print hname, aliases, ipaddrs |
Barry Warsaw | dbfb8d4 | 1997-01-13 19:35:39 +0000 | [diff] [blame] | 79 | print all_host_names |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 80 | |
Barry Warsaw | dbfb8d4 | 1997-01-13 19:35:39 +0000 | [diff] [blame] | 81 | for name in all_host_names: |
| 82 | if string.find(name, '.'): |
| 83 | break |
| 84 | else: |
| 85 | print 'FQDN not found' |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 86 | |
| 87 | print socket.getservbyname('telnet', 'tcp') |
| 88 | try: |
| 89 | socket.getservbyname('telnet', 'udp') |
| 90 | except socket.error: |
| 91 | pass |
| 92 | |
| 93 | |
Guido van Rossum | 2b4d280 | 1997-05-14 21:12:51 +0000 | [diff] [blame] | 94 | canfork = hasattr(os, 'fork') |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 95 | try: |
| 96 | PORT = 50007 |
Guido van Rossum | 2b4d280 | 1997-05-14 21:12:51 +0000 | [diff] [blame] | 97 | if not canfork or os.fork(): |
Barry Warsaw | dce1005 | 1997-01-07 21:02:06 +0000 | [diff] [blame] | 98 | # parent is server |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 99 | 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 Rossum | 2b4d280 | 1997-05-14 21:12:51 +0000 | [diff] [blame] | 104 | if canfork: |
| 105 | conn, addr = s.accept() |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 106 | if verbose: |
Guido van Rossum | 2b4d280 | 1997-05-14 21:12:51 +0000 | [diff] [blame] | 107 | 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 Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 129 | else: |
Guido van Rossum | 1d1394e | 1997-01-17 16:13:15 +0000 | [diff] [blame] | 130 | try: |
| 131 | # child is client |
Guido van Rossum | 9522274 | 1997-05-16 13:40:41 +0000 | [diff] [blame] | 132 | time.sleep(5) |
Guido van Rossum | 1d1394e | 1997-01-17 16:13:15 +0000 | [diff] [blame] | 133 | 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 Rossum | 2b4d280 | 1997-05-14 21:12:51 +0000 | [diff] [blame] | 145 | except socket.error, msg: |
| 146 | raise TestFailed, msg |