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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# type: ignore
import unittest
import importlib.util
import pytest
if importlib.util.find_spec('aioquic'):
has_aioquic = True
from .capsule import H3Capsule, H3CapsuleDecoder
from aioquic.buffer import BufferReadError
else:
has_aioquic = False
class H3CapsuleTest(unittest.TestCase):
@pytest.mark.skipif(not has_aioquic, reason='not having aioquic')
def test_capsule(self) -> None:
capsule1 = H3Capsule(0x12345, b'abcde')
bs = capsule1.encode()
decoder = H3CapsuleDecoder()
decoder.append(bs)
capsule2 = next(iter(decoder))
self.assertEqual(bs, b'\x80\x01\x23\x45\x05abcde', 'bytes')
self.assertEqual(capsule1.type, capsule2.type, 'type')
self.assertEqual(capsule1.data, capsule2.data, 'data')
@pytest.mark.skipif(
not has_aioquic, reason='not having aioquic')
def test_small_capsule(self) -> None:
capsule1 = H3Capsule(0, b'')
bs = capsule1.encode()
decoder = H3CapsuleDecoder()
decoder.append(bs)
capsule2 = next(iter(decoder))
self.assertEqual(bs, b'\x00\x00', 'bytes')
self.assertEqual(capsule1.type, capsule2.type, 'type')
self.assertEqual(capsule1.data, capsule2.data, 'data')
@pytest.mark.skipif(not has_aioquic, reason='not having aioquic')
def test_capsule_append(self) -> None:
decoder = H3CapsuleDecoder()
decoder.append(b'\x80')
with self.assertRaises(StopIteration):
next(iter(decoder))
decoder.append(b'\x01\x23')
with self.assertRaises(StopIteration):
next(iter(decoder))
decoder.append(b'\x45\x05abcd')
with self.assertRaises(StopIteration):
next(iter(decoder))
decoder.append(b'e\x00')
capsule1 = next(iter(decoder))
self.assertEqual(capsule1.type, 0x12345, 'type')
self.assertEqual(capsule1.data, b'abcde', 'data')
with self.assertRaises(StopIteration):
next(iter(decoder))
decoder.append(b'\x00')
capsule2 = next(iter(decoder))
self.assertEqual(capsule2.type, 0, 'type')
self.assertEqual(capsule2.data, b'', 'data')
@pytest.mark.skipif(not has_aioquic, reason='not having aioquic')
def test_multiple_values(self) -> None:
decoder = H3CapsuleDecoder()
decoder.append(b'\x01\x02ab\x03\x04cdef')
it = iter(decoder)
capsule1 = next(it)
capsule2 = next(it)
with self.assertRaises(StopIteration):
next(it)
with self.assertRaises(StopIteration):
next(iter(decoder))
self.assertEqual(capsule1.type, 1, 'type')
self.assertEqual(capsule1.data, b'ab', 'data')
self.assertEqual(capsule2.type, 3, 'type')
self.assertEqual(capsule2.data, b'cdef', 'data')
@pytest.mark.skipif(not has_aioquic, reason='not having aioquic')
def test_final(self) -> None:
decoder = H3CapsuleDecoder()
decoder.append(b'\x01')
with self.assertRaises(StopIteration):
next(iter(decoder))
decoder.append(b'\x01a')
decoder.final()
capsule1 = next(iter(decoder))
with self.assertRaises(StopIteration):
next(iter(decoder))
self.assertEqual(capsule1.type, 1, 'type')
self.assertEqual(capsule1.data, b'a', 'data')
@pytest.mark.skipif(not has_aioquic, reason='not having aioquic')
def test_empty_bytes_before_fin(self) -> None:
decoder = H3CapsuleDecoder()
decoder.append(b'')
decoder.final()
it = iter(decoder)
with self.assertRaises(StopIteration):
next(it)
@pytest.mark.skipif(not has_aioquic, reason='not having aioquic')
def test_final_invalid(self) -> None:
decoder = H3CapsuleDecoder()
decoder.append(b'\x01')
with self.assertRaises(StopIteration):
next(iter(decoder))
decoder.final()
with self.assertRaises(BufferReadError):
next(iter(decoder))
if __name__ == '__main__':
unittest.main()
|