Skip to content

Commit

Permalink
Factorize test certificates serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pquentin committed Jan 16, 2020
1 parent 84abc7f commit d44f0e5
Showing 1 changed file with 14 additions and 54 deletions.
68 changes: 14 additions & 54 deletions test/conftest.py
Expand Up @@ -54,14 +54,22 @@ def certs_dir(tmp_path_factory):


@contextlib.contextmanager
def run_server_in_thread(scheme, host, ca_certs, server_certs):
def run_server_in_thread(scheme, host, tmpdir, ca, server_cert):
ca_cert_path = str(tmpdir / "ca.pem")
server_cert_path = str(tmpdir / "server.pem")
server_key_path = str(tmpdir / "server.key")
ca.cert_pem.write_to_path(ca_cert_path)
server_cert.private_key_pem.write_to_path(server_key_path)
server_cert.cert_chain_pems[0].write_to_path(server_cert_path)
server_certs = {"keyfile": server_key_path, "certfile": server_cert_path}

io_loop = ioloop.IOLoop.current()
app = web.Application([(r".*", TestingApp)])
server, port = run_tornado_app(app, io_loop, server_certs, scheme, host)
server_thread = threading.Thread(target=io_loop.start)
server_thread.start()

yield ServerConfig(host, port, ca_certs)
yield ServerConfig(host, port, ca_cert_path)

io_loop.add_callback(server.stop)
io_loop.add_callback(io_loop.stop)
Expand All @@ -75,19 +83,7 @@ def no_san_server(tmp_path_factory):
# only common name, no subject alternative names
server_cert = ca.issue_cert(common_name=u"localhost")

ca_cert_path = str(tmpdir / "ca.pem")
server_cert_path = str(tmpdir / "server.pem")
server_key_path = str(tmpdir / "server.key")
ca.cert_pem.write_to_path(ca_cert_path)
server_cert.private_key_pem.write_to_path(server_key_path)
server_cert.cert_chain_pems[0].write_to_path(server_cert_path)

with run_server_in_thread(
"https",
"localhost",
ca_cert_path,
{"keyfile": server_key_path, "certfile": server_cert_path},
) as cfg:
with run_server_in_thread("https", "localhost", tmpdir, ca, server_cert) as cfg:
yield cfg


Expand All @@ -98,19 +94,7 @@ def ip_san_server(tmp_path_factory):
# IP address in Subject Alternative Name
server_cert = ca.issue_cert(u"127.0.0.1")

ca_cert_path = str(tmpdir / "ca.pem")
server_cert_path = str(tmpdir / "server.pem")
server_key_path = str(tmpdir / "server.key")
ca.cert_pem.write_to_path(ca_cert_path)
server_cert.private_key_pem.write_to_path(server_key_path)
server_cert.cert_chain_pems[0].write_to_path(server_cert_path)

with run_server_in_thread(
"https",
"127.0.0.1",
ca_cert_path,
{"keyfile": server_key_path, "certfile": server_cert_path},
) as cfg:
with run_server_in_thread("https", "127.0.0.1", tmpdir, ca, server_cert) as cfg:
yield cfg


Expand All @@ -121,19 +105,7 @@ def ipv6_addr_server(tmp_path_factory):
# IP address in Common Name
server_cert = ca.issue_cert(common_name=u"::1")

ca_cert_path = str(tmpdir / "ca.pem")
server_cert_path = str(tmpdir / "server.pem")
server_key_path = str(tmpdir / "server.key")
ca.cert_pem.write_to_path(ca_cert_path)
server_cert.private_key_pem.write_to_path(server_key_path)
server_cert.cert_chain_pems[0].write_to_path(server_cert_path)

with run_server_in_thread(
"https",
"::1",
ca_cert_path,
{"keyfile": server_key_path, "certfile": server_cert_path},
) as cfg:
with run_server_in_thread("https", "::1", tmpdir, ca, server_cert) as cfg:
yield cfg


Expand All @@ -144,17 +116,5 @@ def ipv6_san_server(tmp_path_factory):
# IP address in Subject Alternative Name
server_cert = ca.issue_cert(u"::1")

ca_cert_path = str(tmpdir / "ca.pem")
server_cert_path = str(tmpdir / "server.pem")
server_key_path = str(tmpdir / "server.key")
ca.cert_pem.write_to_path(ca_cert_path)
server_cert.private_key_pem.write_to_path(server_key_path)
server_cert.cert_chain_pems[0].write_to_path(server_cert_path)

with run_server_in_thread(
"https",
"::1",
ca_cert_path,
{"keyfile": server_key_path, "certfile": server_cert_path},
) as cfg:
with run_server_in_thread("https", "::1", tmpdir, ca, server_cert) as cfg:
yield cfg

0 comments on commit d44f0e5

Please sign in to comment.