about summary refs log tree commit diff
path: root/tst
diff options
context:
space:
mode:
Diffstat (limited to 'tst')
-rw-r--r--tst/test_check.py28
-rw-r--r--tst/test_listen.py30
2 files changed, 25 insertions, 33 deletions
diff --git a/tst/test_check.py b/tst/test_check.py
index c5516e2..4b2c955 100644
--- a/tst/test_check.py
+++ b/tst/test_check.py
@@ -28,7 +28,8 @@ from trustme import CA
 from scadere.check import base64_from_str, check, printable
 from scadere.listen import parse_summary, str_from_base64
 
-SECONDS_AGO = datetime.now(tz=timezone.utc)
+# Times in X.509 certificates are YYYYMMDDHHMMSSZ (RFC 5280)
+SECONDS_AGO = datetime.now(timezone.utc).replace(microsecond=0)
 NEXT_DAY = SECONDS_AGO + timedelta(days=1)
 NEXT_WEEK = SECONDS_AGO + timedelta(days=7)
 
@@ -44,11 +45,6 @@ async def noop(reader, writer):
     await writer.wait_closed()
 
 
-def failed_to_get_cert(summary):
-    """Return if any field is N/A."""
-    return any(field == 'N/A' for field in summary)
-
-
 async def get_cert_summary(netloc, after, ca):
     """Fetch TLS certificate expiration summary for netloc."""
     loop = get_running_loop()
@@ -77,19 +73,19 @@ async def test_check(domain, ca_name, not_after, after, trust_ca):
         summary = await get_cert_summary((domain, port), after,
                                          ca if trust_ca else None)
         if not trust_ca:
-            assert failed_to_get_cert(summary)
-            assert 'self-signed certificate' in str_from_base64(summary[-1])
+            assert summary[0] is None
+            assert 'self-signed certificate' in summary[5]
         elif not_after == SECONDS_AGO:
-            assert failed_to_get_cert(summary)
-            assert 'certificate has expired' in str_from_base64(summary[-1])
+            assert summary[0] is None
+            assert 'certificate has expired' in summary[5]
         elif not printable(ca_name):
-            assert failed_to_get_cert(summary)
-            assert 'control character' in str_from_base64(summary[-1])
+            assert summary[0] is None
+            assert 'control character' in summary[5]
         elif not_after > after:
             assert summary is None
         else:
-            assert summary[0] == SECONDS_AGO.isoformat(timespec='seconds')
-            assert summary[1] == not_after.isoformat(timespec='seconds')
+            assert summary[0] == SECONDS_AGO
+            assert summary[1] == not_after
             assert summary[2] == domain
-            assert int(summary[3]) == port
-            assert str_from_base64(summary[5]) == ca_name
+            assert summary[3] == port
+            assert summary[5] == ca_name
diff --git a/tst/test_listen.py b/tst/test_listen.py
index 55a69e0..ee95c3c 100644
--- a/tst/test_listen.py
+++ b/tst/test_listen.py
@@ -37,7 +37,8 @@ from hypothesis.provisional import domains, urls
 from pytest import raises
 
 from scadere.check import base64_from_str, printable
-from scadere.listen import handle, is_subdomain, path, with_trailing_slash, xml
+from scadere.listen import (handle, is_subdomain, path,
+                            str_from_base64, with_trailing_slash, xml)
 
 ATOM_NAMESPACES = {'': 'http://www.w3.org/2005/Atom'}
 XHTML_NAMESPACES = {'': 'http://www.w3.org/1999/xhtml'}
@@ -58,18 +59,13 @@ def base64s():
     return text().filter(printable).map(base64_from_str)
 
 
-@given(domains(), ports(), base64s(), serials())
-def test_path_with_cert(hostname, port, issuer, serial):
-    r = path(hostname, port, issuer, serial).split('/')
+@given(domains(), ports(), serials(), text())
+def test_path(hostname, port, number, string):
+    r = path(hostname, port, number, string).split('/')
     assert r[0] == hostname
     assert int(r[1]) == port
-    assert r[2] == issuer
-    assert int(r[3]) == serial
-
-
-@given(domains(), ports(), base64s())
-def test_path_without_cert(hostname, port, error):
-    assert path(hostname, port, error, 'N/A') == f'{hostname}/{port}'
+    assert int(r[3]) == number
+    assert str_from_base64(r[2]) == string
 
 
 @given(domains(), lists(domains()))
@@ -87,7 +83,7 @@ def test_is_subdomain(subject, objects):
 
 def xml_unsupported_type(child):
     """Check if child is of a type supported by the XML constructor."""
-    return not isinstance(child, (tuple, str, datetime))
+    return not isinstance(child, (tuple, str, int, datetime))
 
 
 @given(text(), from_type(type).flatmap(from_type).filter(xml_unsupported_type))
@@ -100,13 +96,13 @@ def test_xml_unsupported_type(tag, child):
 def certificates(draw):
     """Return a Hypothesis strategy for certificate summaries."""
     valid = draw(booleans())
-    not_before = draw(datetimes()).isoformat()
-    not_after = draw(datetimes()).isoformat() if valid else 'N/A'
+    not_before = draw(datetimes()).isoformat() if valid else 'N/A'
+    not_after = draw(datetimes()).isoformat()
     hostname = draw(domains())
     port = draw(ports())
-    serial = draw(serials()) if valid else 'N/A'
-    issuer = draw(base64s())
-    return f'{not_before} {not_after} {hostname} {port} {serial} {issuer}'
+    number = draw(serials())
+    string = draw(base64s())
+    return f'{not_before} {not_after} {hostname} {port} {number} {string}'
 
 
 @contextmanager