From cb1b5c48145f9c23eae95f922c6af804466bcb42 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Wed, 4 Jun 2025 01:30:57 +0900 Subject: Handle summaries of certs not retrieved --- tst/test_listen.py | 79 ++++++++++-------------------------------------------- 1 file changed, 14 insertions(+), 65 deletions(-) (limited to 'tst/test_listen.py') diff --git a/tst/test_listen.py b/tst/test_listen.py index e204d4f..cc6a9a1 100644 --- a/tst/test_listen.py +++ b/tst/test_listen.py @@ -22,21 +22,18 @@ from copy import deepcopy from email.parser import BytesHeaderParser from functools import partial from pathlib import Path -from string import ascii_letters from tempfile import mkstemp from urllib.parse import urljoin, urlsplit from xml.etree.ElementTree import (XML, XMLParser, indent, tostring as str_from_xml) -from xml.sax.saxutils import escape from hypothesis import HealthCheck, given, settings -from hypothesis.strategies import (builds, composite, data, +from hypothesis.strategies import (booleans, builds, composite, data, datetimes, integers, lists, text) from hypothesis.provisional import domains, urls from scadere.check import base64_from_str -from scadere.listen import (body, entry, handle, is_subdomain, path, - str_from_base64, with_trailing_slash, xml) +from scadere.listen import handle, is_subdomain, path, with_trailing_slash ATOM_NAMESPACES = {'': 'http://www.w3.org/2005/Atom'} XHTML_NAMESPACES = {'': 'http://www.w3.org/1999/xhtml'} @@ -52,13 +49,13 @@ def serials(): return builds(lambda n: hex(n).removeprefix('0x'), integers(0, 256**20-1)) -def ca_names(): +def base64s(): """Return a Hypothesis strategy for CA names.""" - return text().map(lambda name: base64_from_str(name)) + return text().map(base64_from_str) -@given(domains(), ports(), ca_names(), serials()) -def test_path(hostname, port, issuer, serial): +@given(domains(), ports(), base64s(), serials()) +def test_path_with_cert(hostname, port, issuer, serial): r = path(hostname, port, issuer, serial).split('/') assert r[0] == hostname assert int(r[1]) == port @@ -66,57 +63,9 @@ def test_path(hostname, port, issuer, serial): assert r[3] == serial -@given(domains(), ports(), ca_names(), serials(), datetimes(), datetimes()) -def test_body(hostname, port, issuer, serial, not_before, not_after): - r = body(not_before, not_after, hostname, port, serial, issuer) - assert r[-1][0] == 'dl' - d = dict(zip((v for k, v in r[-1][1:] if k == 'dt'), - (v for k, v in r[-1][1:] if k == 'dd'))) - assert d['Domain'] == hostname - assert d['Port'] == port - assert d['Issuer'] == str_from_base64(issuer) - assert d['Serial number'] == serial - assert d['Valid from'] == not_before - assert d['Valid until'] == not_after - - -@given(urls(), domains(), ports(), - ca_names(), serials(), datetimes(), datetimes()) -def test_atom_entry(base_url, hostname, port, - issuer, serial, not_before, not_after): - cert = not_before, not_after, hostname, port, serial, issuer - r = str_from_xml(xml(entry(base_url, cert)), - 'unicode', short_empty_elements=False) - issuer_str = str_from_base64(issuer) - url = urljoin(base_url, path(hostname, port, issuer, serial)) - assert r == f''' - - {escape(issuer_str)} - - -
-

TLS certificate information

-
-
Domain
-
{hostname}
-
Port
-
{port}
-
Issuer
-
{escape(issuer_str)}
-
Serial number
-
{serial}
-
Valid from
-
{not_before.isoformat()}
-
Valid until
-
{not_after.isoformat()}
-
-
-
- {url} - - TLS cert for {hostname} will expire at {not_after} - {not_before.isoformat()} -
''' +@given(domains(), ports(), base64s()) +def test_path_without_cert(hostname, port, error): + assert path(hostname, port, error, 'N/A') == f'{hostname}/{port}' @given(domains(), lists(domains())) @@ -135,13 +84,13 @@ def test_is_subdomain(subject, objects): @composite def certificates(draw): """Return a Hypothesis strategy for certificate summaries.""" + valid = draw(booleans()) not_before = draw(datetimes()).isoformat() - not_after = draw(datetimes()).isoformat() + not_after = draw(datetimes()).isoformat() if valid else 'N/A' hostname = draw(domains()) port = draw(ports()) - serial = draw(serials()) - # Free-formed UTF-8 could easily creates malformed XML. - issuer = base64_from_str(draw(text(ascii_letters))) + serial = draw(serials()) if valid else 'N/A' + issuer = draw(base64s()) return f'{not_before} {not_after} {hostname} {port} {serial} {issuer}' @@ -245,7 +194,7 @@ async def check_server(sockets, func, *args): @given(urls().filter(is_base_url).filter(has_usual_path), lists(certificates(), min_size=1)) @settings(deadline=None) -async def test_http_200(base_url, certs): +async def test_content(base_url, certs): base_path = urlsplit(base_url).path with tmp_cert_file(certs) as cert_file: handler = partial(handle, cert_file, base_url) -- cgit 1.4.1