Skip to content

Commit 309351d

Browse files
kanavinencukou
andcommitted
Lib/test/certdata: do not hardcode reference cert data into tests
The script was simply printing the reference data and asking users to update it by hand into the test suites. This can be easily improved by writing the data into files and having the test cases load the files. Co-authored-by: Petr Viktorin <[email protected]> Signed-off-by: Alexander Kanavin <[email protected]>
1 parent 0a7a141 commit 309351d

File tree

5 files changed

+44
-55
lines changed

5 files changed

+44
-55
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{'issuer': ((('countryName', 'XY'),),
2+
(('localityName', 'Castle Anthrax'),),
3+
(('organizationName', 'Python Software Foundation'),),
4+
(('commonName', 'localhost'),)),
5+
'notAfter': 'Jan 24 04:21:36 2043 GMT',
6+
'notBefore': 'Nov 25 04:21:36 2023 GMT',
7+
'serialNumber': '53E14833F7546C29256DD0F034F776C5E983004C',
8+
'subject': ((('countryName', 'XY'),),
9+
(('localityName', 'Castle Anthrax'),),
10+
(('organizationName', 'Python Software Foundation'),),
11+
(('commonName', 'localhost'),)),
12+
'subjectAltName': (('DNS', 'localhost'),),
13+
'version': 3}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{'OCSP': ('http://testca.pythontest.net/testca/ocsp/',),
2+
'caIssuers': ('http://testca.pythontest.net/testca/pycacert.cer',),
3+
'crlDistributionPoints': ('http://testca.pythontest.net/testca/revocation.crl',),
4+
'issuer': ((('countryName', 'XY'),),
5+
(('organizationName', 'Python Software Foundation CA'),),
6+
(('commonName', 'our-ca-server'),)),
7+
'notAfter': 'Oct 28 14:23:16 2037 GMT',
8+
'notBefore': 'Aug 29 14:23:16 2018 GMT',
9+
'serialNumber': 'CB2D80995A69525C',
10+
'subject': ((('countryName', 'XY'),),
11+
(('localityName', 'Castle Anthrax'),),
12+
(('organizationName', 'Python Software Foundation'),),
13+
(('commonName', 'localhost'),)),
14+
'subjectAltName': (('DNS', 'localhost'),),
15+
'version': 3}

Lib/test/certdata/make_ssl_certs.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,12 @@ def make_ca():
219219
shutil.copy('capath/ceff1710.0', 'capath/b1930218.0')
220220

221221

222-
def print_cert(path):
222+
def write_cert_reference(path):
223223
import _ssl
224-
pprint.pprint(_ssl._test_decode_cert(path))
224+
refdata = pprint.pformat(_ssl._test_decode_cert(path))
225+
print(refdata)
226+
with open(path + '.reference', 'w') as f:
227+
f.write(refdata)
225228

226229

227230
if __name__ == '__main__':
@@ -308,6 +311,6 @@ def print_cert(path):
308311
f.write(cert)
309312

310313
unmake_ca()
311-
print("update Lib/test/test_ssl.py and Lib/test/test_asyncio/utils.py")
312-
print_cert('keycert.pem')
313-
print_cert('keycert3.pem')
314+
print("Writing out reference data for Lib/test/test_ssl.py and Lib/test/test_asyncio/utils.py")
315+
write_cert_reference('keycert.pem')
316+
write_cert_reference('keycert3.pem')

Lib/test/test_asyncio/utils.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import unittest
1616
import weakref
1717
import warnings
18+
from ast import literal_eval
1819
from unittest import mock
1920

2021
from http.server import HTTPServer
@@ -56,24 +57,8 @@ def data_file(*filename):
5657
ONLYKEY = data_file('certdata', 'ssl_key.pem')
5758
SIGNED_CERTFILE = data_file('certdata', 'keycert3.pem')
5859
SIGNING_CA = data_file('certdata', 'pycacert.pem')
59-
PEERCERT = {
60-
'OCSP': ('http://testca.pythontest.net/testca/ocsp/',),
61-
'caIssuers': ('http://testca.pythontest.net/testca/pycacert.cer',),
62-
'crlDistributionPoints': ('http://testca.pythontest.net/testca/revocation.crl',),
63-
'issuer': ((('countryName', 'XY'),),
64-
(('organizationName', 'Python Software Foundation CA'),),
65-
(('commonName', 'our-ca-server'),)),
66-
'notAfter': 'Oct 28 14:23:16 2037 GMT',
67-
'notBefore': 'Aug 29 14:23:16 2018 GMT',
68-
'serialNumber': 'CB2D80995A69525C',
69-
'subject': ((('countryName', 'XY'),),
70-
(('localityName', 'Castle Anthrax'),),
71-
(('organizationName', 'Python Software Foundation'),),
72-
(('commonName', 'localhost'),)),
73-
'subjectAltName': (('DNS', 'localhost'),),
74-
'version': 3
75-
}
76-
60+
with open(data_file('certdata', 'keycert3.pem.reference')) as file:
61+
PEERCERT = literal_eval(file.read())
7762

7863
def simple_server_sslcontext():
7964
server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)

Lib/test/test_ssl.py

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import unittest
55
import unittest.mock
6+
from ast import literal_eval
67
from test import support
78
from test.support import import_helper
89
from test.support import os_helper
@@ -82,21 +83,8 @@ def data_file(*name):
8283
CAFILE_NEURONIO = data_file("capath", "4e1295a3.0")
8384
CAFILE_CACERT = data_file("capath", "5ed36f99.0")
8485

85-
CERTFILE_INFO = {
86-
'issuer': ((('countryName', 'XY'),),
87-
(('localityName', 'Castle Anthrax'),),
88-
(('organizationName', 'Python Software Foundation'),),
89-
(('commonName', 'localhost'),)),
90-
'notAfter': 'Jan 24 04:21:36 2043 GMT',
91-
'notBefore': 'Nov 25 04:21:36 2023 GMT',
92-
'serialNumber': '53E14833F7546C29256DD0F034F776C5E983004C',
93-
'subject': ((('countryName', 'XY'),),
94-
(('localityName', 'Castle Anthrax'),),
95-
(('organizationName', 'Python Software Foundation'),),
96-
(('commonName', 'localhost'),)),
97-
'subjectAltName': (('DNS', 'localhost'),),
98-
'version': 3
99-
}
86+
with open(data_file('keycert.pem.reference')) as file:
87+
CERTFILE_INFO = literal_eval(file.read())
10088

10189
# empty CRL
10290
CRLFILE = data_file("revocation.crl")
@@ -106,23 +94,8 @@ def data_file(*name):
10694
SINGED_CERTFILE_ONLY = data_file("cert3.pem")
10795
SIGNED_CERTFILE_HOSTNAME = 'localhost'
10896

109-
SIGNED_CERTFILE_INFO = {
110-
'OCSP': ('http://testca.pythontest.net/testca/ocsp/',),
111-
'caIssuers': ('http://testca.pythontest.net/testca/pycacert.cer',),
112-
'crlDistributionPoints': ('http://testca.pythontest.net/testca/revocation.crl',),
113-
'issuer': ((('countryName', 'XY'),),
114-
(('organizationName', 'Python Software Foundation CA'),),
115-
(('commonName', 'our-ca-server'),)),
116-
'notAfter': 'Oct 28 14:23:16 2037 GMT',
117-
'notBefore': 'Aug 29 14:23:16 2018 GMT',
118-
'serialNumber': 'CB2D80995A69525C',
119-
'subject': ((('countryName', 'XY'),),
120-
(('localityName', 'Castle Anthrax'),),
121-
(('organizationName', 'Python Software Foundation'),),
122-
(('commonName', 'localhost'),)),
123-
'subjectAltName': (('DNS', 'localhost'),),
124-
'version': 3
125-
}
97+
with open(data_file('keycert3.pem.reference')) as file:
98+
PEERCERT = literal_eval(file.read())
12699

127100
SIGNED_CERTFILE2 = data_file("keycert4.pem")
128101
SIGNED_CERTFILE2_HOSTNAME = 'fakehostname'

0 commit comments

Comments
 (0)