Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | # Test just the SSL support in the socket module, in a moderately bogus way. |
2 | ||
3 | from test import test_support | |
4 | import socket | |
5 | import time | |
6 | ||
7 | # Optionally test SSL support. This requires the 'network' resource as given | |
8 | # on the regrtest command line. | |
9 | skip_expected = not (test_support.is_resource_enabled('network') and | |
10 | hasattr(socket, "ssl")) | |
11 | ||
12 | def test_basic(): | |
13 | test_support.requires('network') | |
14 | ||
15 | import urllib | |
16 | ||
17 | socket.RAND_status() | |
18 | try: | |
19 | socket.RAND_egd(1) | |
20 | except TypeError: | |
21 | pass | |
22 | else: | |
23 | print "didn't raise TypeError" | |
24 | socket.RAND_add("this is a random string", 75.0) | |
25 | ||
26 | f = urllib.urlopen('https://sf.net') | |
27 | buf = f.read() | |
28 | f.close() | |
29 | ||
30 | def test_rude_shutdown(): | |
31 | try: | |
32 | import thread | |
33 | except ImportError: | |
34 | return | |
35 | ||
36 | # some random port to connect to | |
37 | PORT = 9934 | |
38 | def listener(): | |
39 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
40 | s.bind(('', PORT)) | |
41 | s.listen(5) | |
42 | s.accept() | |
43 | del s | |
44 | thread.exit() | |
45 | ||
46 | def connector(): | |
47 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
48 | s.connect(('localhost', PORT)) | |
49 | try: | |
50 | ssl_sock = socket.ssl(s) | |
51 | except socket.sslerror: | |
52 | pass | |
53 | else: | |
54 | raise test_support.TestFailed, \ | |
55 | 'connecting to closed SSL socket failed' | |
56 | ||
57 | thread.start_new_thread(listener, ()) | |
58 | time.sleep(1) | |
59 | connector() | |
60 | ||
61 | def test_main(): | |
62 | if not hasattr(socket, "ssl"): | |
63 | raise test_support.TestSkipped("socket module has no ssl support") | |
64 | test_rude_shutdown() | |
65 | test_basic() | |
66 | ||
67 | if __name__ == "__main__": | |
68 | test_main() |