annotate tests/test-url.py @ 12737:7adb1274a4f9 stable

test-url: skip test when ssl module is unavailable
author Augie Fackler <durin42@gmail.com>
date Tue, 12 Oct 2010 11:02:45 -0500
parents f2937d6492c5
children e9733f96b38b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
1 #!/usr/bin/env python
12737
7adb1274a4f9 test-url: skip test when ssl module is unavailable
Augie Fackler <durin42@gmail.com>
parents: 12592
diff changeset
2 import sys
7adb1274a4f9 test-url: skip test when ssl module is unavailable
Augie Fackler <durin42@gmail.com>
parents: 12592
diff changeset
3 try:
7adb1274a4f9 test-url: skip test when ssl module is unavailable
Augie Fackler <durin42@gmail.com>
parents: 12592
diff changeset
4 import ssl
7adb1274a4f9 test-url: skip test when ssl module is unavailable
Augie Fackler <durin42@gmail.com>
parents: 12592
diff changeset
5 except ImportError:
7adb1274a4f9 test-url: skip test when ssl module is unavailable
Augie Fackler <durin42@gmail.com>
parents: 12592
diff changeset
6 sys.exit(80)
12592
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
7
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
8 def check(a, b):
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
9 if a != b:
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
10 print (a, b)
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
11
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
12 from mercurial.url import _verifycert
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
13
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
14 # Test non-wildcard certificates
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
15 check(_verifycert({'subject': ((('commonName', 'example.com'),),)}, 'example.com'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
16 None)
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
17 check(_verifycert({'subject': ((('commonName', 'example.com'),),)}, 'www.example.com'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
18 'certificate is for example.com')
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
19 check(_verifycert({'subject': ((('commonName', 'www.example.com'),),)}, 'example.com'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
20 'certificate is for www.example.com')
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
21
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
22 # Test wildcard certificates
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
23 check(_verifycert({'subject': ((('commonName', '*.example.com'),),)}, 'www.example.com'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
24 None)
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
25 check(_verifycert({'subject': ((('commonName', '*.example.com'),),)}, 'example.com'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
26 'certificate is for *.example.com')
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
27 check(_verifycert({'subject': ((('commonName', '*.example.com'),),)}, 'w.w.example.com'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
28 'certificate is for *.example.com')
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
29
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
30 # Avoid some pitfalls
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
31 check(_verifycert({'subject': ((('commonName', '*.foo'),),)}, 'foo'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
32 'certificate is for *.foo')
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
33 check(_verifycert({'subject': ((('commonName', '*o'),),)}, 'foo'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
34 'certificate is for *o')
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
35
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
36 import time
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
37 lastyear = time.gmtime().tm_year - 1
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
38 nextyear = time.gmtime().tm_year + 1
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
39 check(_verifycert({'notAfter': 'May 9 00:00:00 %s GMT' % lastyear}, 'example.com'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
40 'certificate expired May 9 00:00:00 %s GMT' % lastyear)
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
41 check(_verifycert({'notBefore': 'May 9 00:00:00 %s GMT' % nextyear}, 'example.com'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
42 'certificate not valid before May 9 00:00:00 %s GMT' % nextyear)
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
43 check(_verifycert({'notAfter': 'Sep 29 15:29:48 %s GMT' % nextyear, 'subject': ()}, 'example.com'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
44 'no commonName found in certificate')
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
45 check(_verifycert(None, 'example.com'),
f2937d6492c5 url: verify correctness of https server certificates (issue2407)
Mads Kiilerich <mads@kiilerich.com>
parents:
diff changeset
46 'no certificate received')