| 1 |
from unittest import TestCase |
| 2 |
|
| 3 |
from aioquic.buffer import Buffer, BufferReadError, BufferWriteError, size_uint_var |
| 4 |
|
| 5 |
|
| 6 |
class BufferTest(TestCase): |
| 7 |
def test_data_slice(self): |
| 8 |
buf = Buffer(data=b"\x08\x07\x06\x05\x04\x03\x02\x01") |
| 9 |
self.assertEqual(buf.data_slice(0, 8), b"\x08\x07\x06\x05\x04\x03\x02\x01") |
| 10 |
self.assertEqual(buf.data_slice(1, 3), b"\x07\x06") |
| 11 |
|
| 12 |
with self.assertRaises(BufferReadError): |
| 13 |
buf.data_slice(-1, 3) |
| 14 |
with self.assertRaises(BufferReadError): |
| 15 |
buf.data_slice(0, 9) |
| 16 |
with self.assertRaises(BufferReadError): |
| 17 |
buf.data_slice(1, 0) |
| 18 |
|
| 19 |
def test_pull_bytes(self): |
| 20 |
buf = Buffer(data=b"\x08\x07\x06\x05\x04\x03\x02\x01") |
| 21 |
self.assertEqual(buf.pull_bytes(3), b"\x08\x07\x06") |
| 22 |
|
| 23 |
def test_pull_bytes_negative(self): |
| 24 |
buf = Buffer(data=b"\x08\x07\x06\x05\x04\x03\x02\x01") |
| 25 |
with self.assertRaises(BufferReadError): |
| 26 |
buf.pull_bytes(-1) |
| 27 |
|
| 28 |
def test_pull_bytes_truncated(self): |
| 29 |
buf = Buffer(capacity=0) |
| 30 |
with self.assertRaises(BufferReadError): |
| 31 |
buf.pull_bytes(2) |
| 32 |
self.assertEqual(buf.tell(), 0) |
| 33 |
|
| 34 |
def test_pull_bytes_zero(self): |
| 35 |
buf = Buffer(data=b"\x08\x07\x06\x05\x04\x03\x02\x01") |
| 36 |
self.assertEqual(buf.pull_bytes(0), b"") |
| 37 |
|
| 38 |
def test_pull_uint8(self): |
| 39 |
buf = Buffer(data=b"\x08\x07\x06\x05\x04\x03\x02\x01") |
| 40 |
self.assertEqual(buf.pull_uint8(), 0x08) |
| 41 |
self.assertEqual(buf.tell(), 1) |
| 42 |
|
| 43 |
def test_pull_uint8_truncated(self): |
| 44 |
buf = Buffer(capacity=0) |
| 45 |
with self.assertRaises(BufferReadError): |
| 46 |
buf.pull_uint8() |
| 47 |
self.assertEqual(buf.tell(), 0) |
| 48 |
|
| 49 |
def test_pull_uint16(self): |
| 50 |
buf = Buffer(data=b"\x08\x07\x06\x05\x04\x03\x02\x01") |
| 51 |
self.assertEqual(buf.pull_uint16(), 0x0807) |
| 52 |
self.assertEqual(buf.tell(), 2) |
| 53 |
|
| 54 |
def test_pull_uint16_truncated(self): |
| 55 |
buf = Buffer(capacity=1) |
| 56 |
with self.assertRaises(BufferReadError): |
| 57 |
buf.pull_uint16() |
| 58 |
self.assertEqual(buf.tell(), 0) |
| 59 |
|
| 60 |
def test_pull_uint32(self): |
| 61 |
buf = Buffer(data=b"\x08\x07\x06\x05\x04\x03\x02\x01") |
| 62 |
self.assertEqual(buf.pull_uint32(), 0x08070605) |
| 63 |
self.assertEqual(buf.tell(), 4) |
| 64 |
|
| 65 |
def test_pull_uint32_truncated(self): |
| 66 |
buf = Buffer(capacity=3) |
| 67 |
with self.assertRaises(BufferReadError): |
| 68 |
buf.pull_uint32() |
| 69 |
self.assertEqual(buf.tell(), 0) |
| 70 |
|
| 71 |
def test_pull_uint64(self): |
| 72 |
buf = Buffer(data=b"\x08\x07\x06\x05\x04\x03\x02\x01") |
| 73 |
self.assertEqual(buf.pull_uint64(), 0x0807060504030201) |
| 74 |
self.assertEqual(buf.tell(), 8) |
| 75 |
|
| 76 |
def test_pull_uint64_truncated(self): |
| 77 |
buf = Buffer(capacity=7) |
| 78 |
with self.assertRaises(BufferReadError): |
| 79 |
buf.pull_uint64() |
| 80 |
self.assertEqual(buf.tell(), 0) |
| 81 |
|
| 82 |
def test_push_bytes(self): |
| 83 |
buf = Buffer(capacity=3) |
| 84 |
buf.push_bytes(b"\x08\x07\x06") |
| 85 |
self.assertEqual(buf.data, b"\x08\x07\x06") |
| 86 |
self.assertEqual(buf.tell(), 3) |
| 87 |
|
| 88 |
def test_push_bytes_truncated(self): |
| 89 |
buf = Buffer(capacity=3) |
| 90 |
with self.assertRaises(BufferWriteError): |
| 91 |
buf.push_bytes(b"\x08\x07\x06\x05") |
| 92 |
self.assertEqual(buf.tell(), 0) |
| 93 |
|
| 94 |
def test_push_bytes_zero(self): |
| 95 |
buf = Buffer(capacity=3) |
| 96 |
buf.push_bytes(b"") |
| 97 |
self.assertEqual(buf.data, b"") |
| 98 |
self.assertEqual(buf.tell(), 0) |
| 99 |
|
| 100 |
def test_push_uint8(self): |
| 101 |
buf = Buffer(capacity=1) |
| 102 |
buf.push_uint8(0x08) |
| 103 |
self.assertEqual(buf.data, b"\x08") |
| 104 |
self.assertEqual(buf.tell(), 1) |
| 105 |
|
| 106 |
def test_push_uint16(self): |
| 107 |
buf = Buffer(capacity=2) |
| 108 |
buf.push_uint16(0x0807) |
| 109 |
self.assertEqual(buf.data, b"\x08\x07") |
| 110 |
self.assertEqual(buf.tell(), 2) |
| 111 |
|
| 112 |
def test_push_uint32(self): |
| 113 |
buf = Buffer(capacity=4) |
| 114 |
buf.push_uint32(0x08070605) |
| 115 |
self.assertEqual(buf.data, b"\x08\x07\x06\x05") |
| 116 |
self.assertEqual(buf.tell(), 4) |
| 117 |
|
| 118 |
def test_push_uint64(self): |
| 119 |
buf = Buffer(capacity=8) |
| 120 |
buf.push_uint64(0x0807060504030201) |
| 121 |
self.assertEqual(buf.data, b"\x08\x07\x06\x05\x04\x03\x02\x01") |
| 122 |
self.assertEqual(buf.tell(), 8) |
| 123 |
|
| 124 |
def test_seek(self): |
| 125 |
buf = Buffer(data=b"01234567") |
| 126 |
self.assertFalse(buf.eof()) |
| 127 |
self.assertEqual(buf.tell(), 0) |
| 128 |
|
| 129 |
buf.seek(4) |
| 130 |
self.assertFalse(buf.eof()) |
| 131 |
self.assertEqual(buf.tell(), 4) |
| 132 |
|
| 133 |
buf.seek(8) |
| 134 |
self.assertTrue(buf.eof()) |
| 135 |
self.assertEqual(buf.tell(), 8) |
| 136 |
|
| 137 |
with self.assertRaises(BufferReadError): |
| 138 |
buf.seek(-1) |
| 139 |
self.assertEqual(buf.tell(), 8) |
| 140 |
with self.assertRaises(BufferReadError): |
| 141 |
buf.seek(9) |
| 142 |
self.assertEqual(buf.tell(), 8) |
| 143 |
|
| 144 |
|
| 145 |
class UintVarTest(TestCase): |
| 146 |
def roundtrip(self, data, value): |
| 147 |
buf = Buffer(data=data) |
| 148 |
self.assertEqual(buf.pull_uint_var(), value) |
| 149 |
self.assertEqual(buf.tell(), len(data)) |
| 150 |
|
| 151 |
buf = Buffer(capacity=8) |
| 152 |
buf.push_uint_var(value) |
| 153 |
self.assertEqual(buf.data, data) |
| 154 |
|
| 155 |
def test_uint_var(self): |
| 156 |
|
| 157 |
self.roundtrip(b"\x00", 0) |
| 158 |
self.roundtrip(b"\x01", 1) |
| 159 |
self.roundtrip(b"\x25", 37) |
| 160 |
self.roundtrip(b"\x3f", 63) |
| 161 |
|
| 162 |
|
| 163 |
self.roundtrip(b"\x7b\xbd", 15293) |
| 164 |
self.roundtrip(b"\x7f\xff", 16383) |
| 165 |
|
| 166 |
|
| 167 |
self.roundtrip(b"\x9d\x7f\x3e\x7d", 494878333) |
| 168 |
self.roundtrip(b"\xbf\xff\xff\xff", 1073741823) |
| 169 |
|
| 170 |
|
| 171 |
self.roundtrip(b"\xc2\x19\x7c\x5e\xff\x14\xe8\x8c", 151288809941952652) |
| 172 |
self.roundtrip(b"\xff\xff\xff\xff\xff\xff\xff\xff", 4611686018427387903) |
| 173 |
|
| 174 |
def test_pull_uint_var_truncated(self): |
| 175 |
buf = Buffer(capacity=0) |
| 176 |
with self.assertRaises(BufferReadError): |
| 177 |
buf.pull_uint_var() |
| 178 |
|
| 179 |
buf = Buffer(data=b"\xff") |
| 180 |
with self.assertRaises(BufferReadError): |
| 181 |
buf.pull_uint_var() |
| 182 |
|
| 183 |
def test_push_uint_var_too_big(self): |
| 184 |
buf = Buffer(capacity=8) |
| 185 |
with self.assertRaises(ValueError) as cm: |
| 186 |
buf.push_uint_var(4611686018427387904) |
| 187 |
self.assertEqual( |
| 188 |
str(cm.exception), "Integer is too big for a variable-length integer" |
| 189 |
) |
| 190 |
|
| 191 |
def test_size_uint_var(self): |
| 192 |
self.assertEqual(size_uint_var(63), 1) |
| 193 |
self.assertEqual(size_uint_var(16383), 2) |
| 194 |
self.assertEqual(size_uint_var(1073741823), 4) |
| 195 |
self.assertEqual(size_uint_var(4611686018427387903), 8) |
| 196 |
|
| 197 |
with self.assertRaises(ValueError) as cm: |
| 198 |
size_uint_var(4611686018427387904) |
| 199 |
self.assertEqual( |
| 200 |
str(cm.exception), "Integer is too big for a variable-length integer" |
| 201 |
) |