test_capsule.py (4110B)
1 # type: ignore 2 3 import unittest 4 import importlib.util 5 import pytest 6 7 if importlib.util.find_spec('aioquic'): 8 has_aioquic = True 9 from .capsule import H3Capsule, H3CapsuleDecoder 10 from aioquic.buffer import BufferReadError 11 else: 12 has_aioquic = False 13 14 15 class H3CapsuleTest(unittest.TestCase): 16 @pytest.mark.skipif(not has_aioquic, reason='not having aioquic') 17 def test_capsule(self) -> None: 18 capsule1 = H3Capsule(0x12345, b'abcde') 19 bs = capsule1.encode() 20 decoder = H3CapsuleDecoder() 21 decoder.append(bs) 22 capsule2 = next(iter(decoder)) 23 24 self.assertEqual(bs, b'\x80\x01\x23\x45\x05abcde', 'bytes') 25 self.assertEqual(capsule1.type, capsule2.type, 'type') 26 self.assertEqual(capsule1.data, capsule2.data, 'data') 27 28 @pytest.mark.skipif( 29 not has_aioquic, reason='not having aioquic') 30 def test_small_capsule(self) -> None: 31 capsule1 = H3Capsule(0, b'') 32 bs = capsule1.encode() 33 decoder = H3CapsuleDecoder() 34 decoder.append(bs) 35 capsule2 = next(iter(decoder)) 36 37 self.assertEqual(bs, b'\x00\x00', 'bytes') 38 self.assertEqual(capsule1.type, capsule2.type, 'type') 39 self.assertEqual(capsule1.data, capsule2.data, 'data') 40 41 @pytest.mark.skipif(not has_aioquic, reason='not having aioquic') 42 def test_capsule_append(self) -> None: 43 decoder = H3CapsuleDecoder() 44 decoder.append(b'\x80') 45 46 with self.assertRaises(StopIteration): 47 next(iter(decoder)) 48 49 decoder.append(b'\x01\x23') 50 with self.assertRaises(StopIteration): 51 next(iter(decoder)) 52 53 decoder.append(b'\x45\x05abcd') 54 with self.assertRaises(StopIteration): 55 next(iter(decoder)) 56 57 decoder.append(b'e\x00') 58 capsule1 = next(iter(decoder)) 59 60 self.assertEqual(capsule1.type, 0x12345, 'type') 61 self.assertEqual(capsule1.data, b'abcde', 'data') 62 63 with self.assertRaises(StopIteration): 64 next(iter(decoder)) 65 66 decoder.append(b'\x00') 67 capsule2 = next(iter(decoder)) 68 self.assertEqual(capsule2.type, 0, 'type') 69 self.assertEqual(capsule2.data, b'', 'data') 70 71 @pytest.mark.skipif(not has_aioquic, reason='not having aioquic') 72 def test_multiple_values(self) -> None: 73 decoder = H3CapsuleDecoder() 74 decoder.append(b'\x01\x02ab\x03\x04cdef') 75 76 it = iter(decoder) 77 capsule1 = next(it) 78 capsule2 = next(it) 79 with self.assertRaises(StopIteration): 80 next(it) 81 with self.assertRaises(StopIteration): 82 next(iter(decoder)) 83 84 self.assertEqual(capsule1.type, 1, 'type') 85 self.assertEqual(capsule1.data, b'ab', 'data') 86 self.assertEqual(capsule2.type, 3, 'type') 87 self.assertEqual(capsule2.data, b'cdef', 'data') 88 89 @pytest.mark.skipif(not has_aioquic, reason='not having aioquic') 90 def test_final(self) -> None: 91 decoder = H3CapsuleDecoder() 92 decoder.append(b'\x01') 93 94 with self.assertRaises(StopIteration): 95 next(iter(decoder)) 96 97 decoder.append(b'\x01a') 98 decoder.final() 99 capsule1 = next(iter(decoder)) 100 with self.assertRaises(StopIteration): 101 next(iter(decoder)) 102 103 self.assertEqual(capsule1.type, 1, 'type') 104 self.assertEqual(capsule1.data, b'a', 'data') 105 106 @pytest.mark.skipif(not has_aioquic, reason='not having aioquic') 107 def test_empty_bytes_before_fin(self) -> None: 108 decoder = H3CapsuleDecoder() 109 decoder.append(b'') 110 decoder.final() 111 112 it = iter(decoder) 113 with self.assertRaises(StopIteration): 114 next(it) 115 116 @pytest.mark.skipif(not has_aioquic, reason='not having aioquic') 117 def test_final_invalid(self) -> None: 118 decoder = H3CapsuleDecoder() 119 decoder.append(b'\x01') 120 121 with self.assertRaises(StopIteration): 122 next(iter(decoder)) 123 124 decoder.final() 125 with self.assertRaises(BufferReadError): 126 next(iter(decoder)) 127 128 129 if __name__ == '__main__': 130 unittest.main()