Skip to content

Commit 6c899df

Browse files
committed
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. Signed-off-by: Alexander Kanavin <[email protected]>
1 parent 9685dc2 commit 6c899df

File tree

3 files changed

+11
-55
lines changed

3 files changed

+11
-55
lines changed

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: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,7 @@ def data_file(*filename):
5656
ONLYKEY = data_file('certdata', 'ssl_key.pem')
5757
SIGNED_CERTFILE = data_file('certdata', 'keycert3.pem')
5858
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-
59+
PEERCERT = eval(open(data_file('certdata', 'keycert3.pem.reference')).read())
7760

7861
def simple_server_sslcontext():
7962
server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)

Lib/test/test_ssl.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,7 @@ def data_file(*name):
8282
CAFILE_NEURONIO = data_file("capath", "4e1295a3.0")
8383
CAFILE_CACERT = data_file("capath", "5ed36f99.0")
8484

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-
}
85+
CERTFILE_INFO = eval(open(data_file('keycert.pem.reference')).read())
10086

10187
# empty CRL
10288
CRLFILE = data_file("revocation.crl")
@@ -105,23 +91,7 @@ def data_file(*name):
10591
SIGNED_CERTFILE = data_file("keycert3.pem")
10692
SIGNED_CERTFILE_HOSTNAME = 'localhost'
10793

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

12696
SIGNED_CERTFILE2 = data_file("keycert4.pem")
12797
SIGNED_CERTFILE2_HOSTNAME = 'fakehostname'

0 commit comments

Comments
 (0)