about summary refs log tree commit diff
path: root/tst/test_listen.py
blob: d3a8052fc02a4f069d433939e800586c0aa6ce07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Tests for the HTTP server
# Copyright (C) 2025  Nguyễn Gia Phong
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

from base64 import urlsafe_b64decode as from_base64

from hypothesis import given
from hypothesis.strategies import integers, datetimes, text
from hypothesis.provisional import domains

from scadere.listen import body, path


@given(domains(), integers(1, 65535), text(), integers(0, 256**20))
def test_path(hostname, port, issuer, serial):
    r = path(hostname, port, issuer, hex(serial).removeprefix('0x')).split('/')
    assert(r[0] == hostname)
    assert(int(r[1]) == port)
    assert(from_base64(r[2]).decode() == issuer)
    assert(int(r[3], 16) == serial)


@given(domains(), integers(1, 65535), text(), integers(0, 256**20),
       datetimes(), datetimes())
def test_body(hostname, port, issuer, serial, not_before, not_after):
    r = body(not_before, not_after, hostname, port,
             hex(serial).removeprefix('0x'), 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'] == issuer)
    assert(int(d['Serial number'], 16) == serial)
    assert(d['Valid from'] == not_before)
    assert(d['Valid until'] == not_after)