blob: e8288227e38b451add77766b5626b7889c78081a [file] [log] [blame]
Georg Brandlf899dfa2008-05-18 09:12:20 +00001"""Unittests for the various HTTPServer modules.
2
3Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>,
4Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest.
5"""
6
7from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
8from SimpleHTTPServer import SimpleHTTPRequestHandler
9from CGIHTTPServer import CGIHTTPRequestHandler
Gregory P. Smith923ba362009-04-06 06:33:26 +000010import CGIHTTPServer
Georg Brandlf899dfa2008-05-18 09:12:20 +000011
12import os
13import sys
Senthil Kumaranb643fc62010-09-30 06:40:56 +000014import re
Georg Brandlf899dfa2008-05-18 09:12:20 +000015import base64
16import shutil
17import urllib
18import httplib
19import tempfile
Georg Brandlf899dfa2008-05-18 09:12:20 +000020
21import unittest
Senthil Kumaranb643fc62010-09-30 06:40:56 +000022
23from StringIO import StringIO
24
Georg Brandlf899dfa2008-05-18 09:12:20 +000025from test import test_support
Victor Stinner6a102812010-04-27 23:55:59 +000026threading = test_support.import_module('threading')
Georg Brandlf899dfa2008-05-18 09:12:20 +000027
28
29class NoLogRequestHandler:
30 def log_message(self, *args):
31 # don't write log messages to stderr
32 pass
33
Senthil Kumaranb643fc62010-09-30 06:40:56 +000034class SocketlessRequestHandler(BaseHTTPRequestHandler):
35 def __init__(self):
36 self.get_called = False
37 self.protocol_version = "HTTP/1.1"
38
39 def do_GET(self):
40 self.get_called = True
41 self.send_response(200)
42 self.send_header('Content-Type', 'text/html')
43 self.end_headers()
44 self.wfile.write('<html><body>Data</body></html>\r\n')
45
46 def log_message(self, format, *args):
47 pass
48
49
Georg Brandlf899dfa2008-05-18 09:12:20 +000050
51class TestServerThread(threading.Thread):
52 def __init__(self, test_object, request_handler):
53 threading.Thread.__init__(self)
54 self.request_handler = request_handler
55 self.test_object = test_object
Georg Brandlf899dfa2008-05-18 09:12:20 +000056
57 def run(self):
58 self.server = HTTPServer(('', 0), self.request_handler)
59 self.test_object.PORT = self.server.socket.getsockname()[1]
Antoine Pitrou1ca8c192010-04-25 21:15:50 +000060 self.test_object.server_started.set()
61 self.test_object = None
Georg Brandlf899dfa2008-05-18 09:12:20 +000062 try:
Antoine Pitrou1ca8c192010-04-25 21:15:50 +000063 self.server.serve_forever(0.05)
Georg Brandlf899dfa2008-05-18 09:12:20 +000064 finally:
65 self.server.server_close()
66
67 def stop(self):
68 self.server.shutdown()
69
70
71class BaseTestCase(unittest.TestCase):
72 def setUp(self):
Antoine Pitrou85bd5872009-10-27 18:50:52 +000073 self._threads = test_support.threading_setup()
Nick Coghlan87c03b32009-10-17 15:23:08 +000074 os.environ = test_support.EnvironmentVarGuard()
Antoine Pitrou1ca8c192010-04-25 21:15:50 +000075 self.server_started = threading.Event()
Georg Brandlf899dfa2008-05-18 09:12:20 +000076 self.thread = TestServerThread(self, self.request_handler)
77 self.thread.start()
Antoine Pitrou1ca8c192010-04-25 21:15:50 +000078 self.server_started.wait()
Georg Brandlf899dfa2008-05-18 09:12:20 +000079
80 def tearDown(self):
Georg Brandlf899dfa2008-05-18 09:12:20 +000081 self.thread.stop()
Nick Coghlan87c03b32009-10-17 15:23:08 +000082 os.environ.__exit__()
Antoine Pitrou85bd5872009-10-27 18:50:52 +000083 test_support.threading_cleanup(*self._threads)
Georg Brandlf899dfa2008-05-18 09:12:20 +000084
85 def request(self, uri, method='GET', body=None, headers={}):
86 self.connection = httplib.HTTPConnection('localhost', self.PORT)
87 self.connection.request(method, uri, body, headers)
88 return self.connection.getresponse()
89
Senthil Kumaranb643fc62010-09-30 06:40:56 +000090class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
91 """Test the functionaility of the BaseHTTPServer focussing on
92 BaseHTTPRequestHandler.
93 """
94
95 HTTPResponseMatch = re.compile('HTTP/1.[0-9]+ 200 OK')
96
97 def setUp (self):
98 self.handler = SocketlessRequestHandler()
99
100 def send_typical_request(self, message):
101 input = StringIO(message)
102 output = StringIO()
103 self.handler.rfile = input
104 self.handler.wfile = output
105 self.handler.handle_one_request()
106 output.seek(0)
107 return output.readlines()
108
109 def verify_get_called(self):
110 self.assertTrue(self.handler.get_called)
111
112 def verify_expected_headers(self, headers):
113 for fieldName in 'Server: ', 'Date: ', 'Content-Type: ':
114 self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1)
115
116 def verify_http_server_response(self, response):
117 match = self.HTTPResponseMatch.search(response)
118 self.assertTrue(match is not None)
119
120 def test_http_1_1(self):
121 result = self.send_typical_request('GET / HTTP/1.1\r\n\r\n')
122 self.verify_http_server_response(result[0])
123 self.verify_expected_headers(result[1:-1])
124 self.verify_get_called()
125 self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n')
126
127 def test_http_1_0(self):
128 result = self.send_typical_request('GET / HTTP/1.0\r\n\r\n')
129 self.verify_http_server_response(result[0])
130 self.verify_expected_headers(result[1:-1])
131 self.verify_get_called()
132 self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n')
133
134 def test_http_0_9(self):
135 result = self.send_typical_request('GET / HTTP/0.9\r\n\r\n')
136 self.assertEqual(len(result), 1)
137 self.assertEqual(result[0], '<html><body>Data</body></html>\r\n')
138 self.verify_get_called()
139
140 def test_with_continue_1_0(self):
141 result = self.send_typical_request('GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n')
142 self.verify_http_server_response(result[0])
143 self.verify_expected_headers(result[1:-1])
144 self.verify_get_called()
145 self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n')
146
Georg Brandlf899dfa2008-05-18 09:12:20 +0000147
148class BaseHTTPServerTestCase(BaseTestCase):
149 class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
150 protocol_version = 'HTTP/1.1'
151 default_request_version = 'HTTP/1.1'
152
153 def do_TEST(self):
154 self.send_response(204)
155 self.send_header('Content-Type', 'text/html')
156 self.send_header('Connection', 'close')
157 self.end_headers()
158
159 def do_KEEP(self):
160 self.send_response(204)
161 self.send_header('Content-Type', 'text/html')
162 self.send_header('Connection', 'keep-alive')
163 self.end_headers()
164
165 def do_KEYERROR(self):
166 self.send_error(999)
167
168 def do_CUSTOM(self):
169 self.send_response(999)
170 self.send_header('Content-Type', 'text/html')
171 self.send_header('Connection', 'close')
172 self.end_headers()
173
174 def setUp(self):
175 BaseTestCase.setUp(self)
176 self.con = httplib.HTTPConnection('localhost', self.PORT)
177 self.con.connect()
178
179 def test_command(self):
180 self.con.request('GET', '/')
181 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000182 self.assertEqual(res.status, 501)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000183
184 def test_request_line_trimming(self):
185 self.con._http_vsn_str = 'HTTP/1.1\n'
186 self.con.putrequest('GET', '/')
187 self.con.endheaders()
188 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000189 self.assertEqual(res.status, 501)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000190
191 def test_version_bogus(self):
192 self.con._http_vsn_str = 'FUBAR'
193 self.con.putrequest('GET', '/')
194 self.con.endheaders()
195 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000196 self.assertEqual(res.status, 400)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000197
198 def test_version_digits(self):
199 self.con._http_vsn_str = 'HTTP/9.9.9'
200 self.con.putrequest('GET', '/')
201 self.con.endheaders()
202 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000203 self.assertEqual(res.status, 400)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000204
205 def test_version_none_get(self):
206 self.con._http_vsn_str = ''
207 self.con.putrequest('GET', '/')
208 self.con.endheaders()
209 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000210 self.assertEqual(res.status, 501)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000211
212 def test_version_none(self):
213 self.con._http_vsn_str = ''
214 self.con.putrequest('PUT', '/')
215 self.con.endheaders()
216 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000217 self.assertEqual(res.status, 400)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000218
219 def test_version_invalid(self):
220 self.con._http_vsn = 99
221 self.con._http_vsn_str = 'HTTP/9.9'
222 self.con.putrequest('GET', '/')
223 self.con.endheaders()
224 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000225 self.assertEqual(res.status, 505)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000226
227 def test_send_blank(self):
228 self.con._http_vsn_str = ''
229 self.con.putrequest('', '')
230 self.con.endheaders()
231 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000232 self.assertEqual(res.status, 400)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000233
234 def test_header_close(self):
235 self.con.putrequest('GET', '/')
236 self.con.putheader('Connection', 'close')
237 self.con.endheaders()
238 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000239 self.assertEqual(res.status, 501)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000240
241 def test_head_keep_alive(self):
242 self.con._http_vsn_str = 'HTTP/1.1'
243 self.con.putrequest('GET', '/')
244 self.con.putheader('Connection', 'keep-alive')
245 self.con.endheaders()
246 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000247 self.assertEqual(res.status, 501)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000248
249 def test_handler(self):
250 self.con.request('TEST', '/')
251 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000252 self.assertEqual(res.status, 204)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000253
254 def test_return_header_keep_alive(self):
255 self.con.request('KEEP', '/')
256 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000257 self.assertEqual(res.getheader('Connection'), 'keep-alive')
Georg Brandlf899dfa2008-05-18 09:12:20 +0000258 self.con.request('TEST', '/')
259
260 def test_internal_key_error(self):
261 self.con.request('KEYERROR', '/')
262 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000263 self.assertEqual(res.status, 999)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000264
265 def test_return_custom_status(self):
266 self.con.request('CUSTOM', '/')
267 res = self.con.getresponse()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000268 self.assertEqual(res.status, 999)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000269
270
271class SimpleHTTPServerTestCase(BaseTestCase):
272 class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):
273 pass
274
275 def setUp(self):
276 BaseTestCase.setUp(self)
Georg Brandl7bb16532008-05-20 06:47:31 +0000277 self.cwd = os.getcwd()
278 basetempdir = tempfile.gettempdir()
279 os.chdir(basetempdir)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000280 self.data = 'We are the knights who say Ni!'
Georg Brandl7bb16532008-05-20 06:47:31 +0000281 self.tempdir = tempfile.mkdtemp(dir=basetempdir)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000282 self.tempdir_name = os.path.basename(self.tempdir)
Georg Brandl7bb16532008-05-20 06:47:31 +0000283 temp = open(os.path.join(self.tempdir, 'test'), 'wb')
284 temp.write(self.data)
285 temp.close()
Georg Brandlf899dfa2008-05-18 09:12:20 +0000286
287 def tearDown(self):
288 try:
Georg Brandl7bb16532008-05-20 06:47:31 +0000289 os.chdir(self.cwd)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000290 try:
291 shutil.rmtree(self.tempdir)
292 except:
293 pass
294 finally:
295 BaseTestCase.tearDown(self)
296
297 def check_status_and_reason(self, response, status, data=None):
298 body = response.read()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000299 self.assertTrue(response)
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000300 self.assertEqual(response.status, status)
301 self.assertIsNotNone(response.reason)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000302 if data:
303 self.assertEqual(data, body)
304
305 def test_get(self):
306 #constructs the path relative to the root directory of the HTTPServer
Georg Brandl7bb16532008-05-20 06:47:31 +0000307 response = self.request(self.tempdir_name + '/test')
Georg Brandlf899dfa2008-05-18 09:12:20 +0000308 self.check_status_and_reason(response, 200, data=self.data)
309 response = self.request(self.tempdir_name + '/')
310 self.check_status_and_reason(response, 200)
311 response = self.request(self.tempdir_name)
312 self.check_status_and_reason(response, 301)
313 response = self.request('/ThisDoesNotExist')
314 self.check_status_and_reason(response, 404)
315 response = self.request('/' + 'ThisDoesNotExist' + '/')
316 self.check_status_and_reason(response, 404)
317 f = open(os.path.join(self.tempdir_name, 'index.html'), 'w')
318 response = self.request('/' + self.tempdir_name + '/')
319 self.check_status_and_reason(response, 200)
320 if os.name == 'posix':
321 # chmod won't work as expected on Windows platforms
322 os.chmod(self.tempdir, 0)
323 response = self.request(self.tempdir_name + '/')
324 self.check_status_and_reason(response, 404)
325 os.chmod(self.tempdir, 0755)
326
327 def test_head(self):
328 response = self.request(
Georg Brandl7bb16532008-05-20 06:47:31 +0000329 self.tempdir_name + '/test', method='HEAD')
Georg Brandlf899dfa2008-05-18 09:12:20 +0000330 self.check_status_and_reason(response, 200)
331 self.assertEqual(response.getheader('content-length'),
332 str(len(self.data)))
333 self.assertEqual(response.getheader('content-type'),
334 'application/octet-stream')
335
336 def test_invalid_requests(self):
337 response = self.request('/', method='FOO')
338 self.check_status_and_reason(response, 501)
339 # requests must be case sensitive,so this should fail too
340 response = self.request('/', method='get')
341 self.check_status_and_reason(response, 501)
342 response = self.request('/', method='GETs')
343 self.check_status_and_reason(response, 501)
344
345
346cgi_file1 = """\
347#!%s
348
349print "Content-type: text/html"
350print
351print "Hello World"
352"""
353
354cgi_file2 = """\
355#!%s
356import cgi
357
358print "Content-type: text/html"
359print
360
361form = cgi.FieldStorage()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000362print "%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"),
363 form.getfirst("bacon"))
Georg Brandlf899dfa2008-05-18 09:12:20 +0000364"""
365
366class CGIHTTPServerTestCase(BaseTestCase):
367 class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler):
368 pass
369
370 def setUp(self):
371 BaseTestCase.setUp(self)
372 self.parent_dir = tempfile.mkdtemp()
373 self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin')
374 os.mkdir(self.cgi_dir)
375
Florent Xicluna0805e6e2010-03-22 17:18:18 +0000376 # The shebang line should be pure ASCII: use symlink if possible.
377 # See issue #7668.
378 if hasattr(os, 'symlink'):
379 self.pythonexe = os.path.join(self.parent_dir, 'python')
380 os.symlink(sys.executable, self.pythonexe)
381 else:
382 self.pythonexe = sys.executable
383
Georg Brandlf899dfa2008-05-18 09:12:20 +0000384 self.file1_path = os.path.join(self.cgi_dir, 'file1.py')
385 with open(self.file1_path, 'w') as file1:
Florent Xicluna0805e6e2010-03-22 17:18:18 +0000386 file1.write(cgi_file1 % self.pythonexe)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000387 os.chmod(self.file1_path, 0777)
388
389 self.file2_path = os.path.join(self.cgi_dir, 'file2.py')
390 with open(self.file2_path, 'w') as file2:
Florent Xicluna0805e6e2010-03-22 17:18:18 +0000391 file2.write(cgi_file2 % self.pythonexe)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000392 os.chmod(self.file2_path, 0777)
393
Georg Brandl7bb16532008-05-20 06:47:31 +0000394 self.cwd = os.getcwd()
Georg Brandlf899dfa2008-05-18 09:12:20 +0000395 os.chdir(self.parent_dir)
396
397 def tearDown(self):
398 try:
Georg Brandl7bb16532008-05-20 06:47:31 +0000399 os.chdir(self.cwd)
Florent Xicluna0805e6e2010-03-22 17:18:18 +0000400 if self.pythonexe != sys.executable:
401 os.remove(self.pythonexe)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000402 os.remove(self.file1_path)
403 os.remove(self.file2_path)
404 os.rmdir(self.cgi_dir)
405 os.rmdir(self.parent_dir)
406 finally:
407 BaseTestCase.tearDown(self)
408
Gregory P. Smith923ba362009-04-06 06:33:26 +0000409 def test_url_collapse_path_split(self):
410 test_vectors = {
411 '': ('/', ''),
412 '..': IndexError,
413 '/.//..': IndexError,
414 '/': ('/', ''),
415 '//': ('/', ''),
416 '/\\': ('/', '\\'),
417 '/.//': ('/', ''),
418 'cgi-bin/file1.py': ('/cgi-bin', 'file1.py'),
419 '/cgi-bin/file1.py': ('/cgi-bin', 'file1.py'),
420 'a': ('/', 'a'),
421 '/a': ('/', 'a'),
422 '//a': ('/', 'a'),
423 './a': ('/', 'a'),
424 './C:/': ('/C:', ''),
425 '/a/b': ('/a', 'b'),
426 '/a/b/': ('/a/b', ''),
427 '/a/b/c/..': ('/a/b', ''),
428 '/a/b/c/../d': ('/a/b', 'd'),
429 '/a/b/c/../d/e/../f': ('/a/b/d', 'f'),
430 '/a/b/c/../d/e/../../f': ('/a/b', 'f'),
431 '/a/b/c/../d/e/.././././..//f': ('/a/b', 'f'),
432 '../a/b/c/../d/e/.././././..//f': IndexError,
433 '/a/b/c/../d/e/../../../f': ('/a', 'f'),
434 '/a/b/c/../d/e/../../../../f': ('/', 'f'),
435 '/a/b/c/../d/e/../../../../../f': IndexError,
436 '/a/b/c/../d/e/../../../../f/..': ('/', ''),
437 }
438 for path, expected in test_vectors.iteritems():
439 if isinstance(expected, type) and issubclass(expected, Exception):
440 self.assertRaises(expected,
441 CGIHTTPServer._url_collapse_path_split, path)
442 else:
443 actual = CGIHTTPServer._url_collapse_path_split(path)
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000444 self.assertEqual(expected, actual,
445 msg='path = %r\nGot: %r\nWanted: %r' %
446 (path, actual, expected))
Gregory P. Smith923ba362009-04-06 06:33:26 +0000447
Georg Brandlf899dfa2008-05-18 09:12:20 +0000448 def test_headers_and_content(self):
449 res = self.request('/cgi-bin/file1.py')
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000450 self.assertEqual(('Hello World\n', 'text/html', 200),
451 (res.read(), res.getheader('Content-type'), res.status))
Georg Brandlf899dfa2008-05-18 09:12:20 +0000452
453 def test_post(self):
454 params = urllib.urlencode({'spam' : 1, 'eggs' : 'python', 'bacon' : 123456})
455 headers = {'Content-type' : 'application/x-www-form-urlencoded'}
456 res = self.request('/cgi-bin/file2.py', 'POST', params, headers)
457
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000458 self.assertEqual(res.read(), '1, python, 123456\n')
Georg Brandlf899dfa2008-05-18 09:12:20 +0000459
460 def test_invaliduri(self):
461 res = self.request('/cgi-bin/invalid')
462 res.read()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000463 self.assertEqual(res.status, 404)
Georg Brandlf899dfa2008-05-18 09:12:20 +0000464
465 def test_authorization(self):
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000466 headers = {'Authorization' : 'Basic %s' %
467 base64.b64encode('username:pass')}
Georg Brandlf899dfa2008-05-18 09:12:20 +0000468 res = self.request('/cgi-bin/file1.py', 'GET', headers=headers)
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000469 self.assertEqual(('Hello World\n', 'text/html', 200),
470 (res.read(), res.getheader('Content-type'), res.status))
Georg Brandlf899dfa2008-05-18 09:12:20 +0000471
Gregory P. Smith923ba362009-04-06 06:33:26 +0000472 def test_no_leading_slash(self):
473 # http://bugs.python.org/issue2254
474 res = self.request('cgi-bin/file1.py')
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000475 self.assertEqual(('Hello World\n', 'text/html', 200),
Gregory P. Smith923ba362009-04-06 06:33:26 +0000476 (res.read(), res.getheader('Content-type'), res.status))
477
Georg Brandlf899dfa2008-05-18 09:12:20 +0000478
479def test_main(verbose=None):
480 try:
481 cwd = os.getcwd()
Senthil Kumaranb643fc62010-09-30 06:40:56 +0000482 test_support.run_unittest(BaseHTTPRequestHandlerTestCase,
483 BaseHTTPServerTestCase,
484 SimpleHTTPServerTestCase,
485 CGIHTTPServerTestCase
486 )
Georg Brandlf899dfa2008-05-18 09:12:20 +0000487 finally:
488 os.chdir(cwd)
489
490if __name__ == '__main__':
491 test_main()