+from os import urandom
from unittest import TestCase
from hypothesis import given
+from hypothesis.strategies import integers
from pyac import Blob
from pyac import DecodeError
class TestBlob(TestCase):
- def test_blob_encode(self) -> None:
+ @given(junk_st)
+ def test_multiple_of_chunkLen(self, junk: bytes) -> None:
blob = Blob(4, b"testdata")
encoded = dumps(blob)
self.assertSequenceEqual(
- encoded, b"\x0b\x00\x00\x00\x00\x00\x00\x00\x03\x01test\x01data\x80"
+ encoded,
+ b"\x0b\x00\x00\x00\x00\x00\x00\x00\x03\x01test\x01data\x80",
)
+ decoded, tail = loads(encoded + junk)
+ self.assertEqual(decoded.l, blob.l)
+ self.assertSequenceEqual(decoded.v, blob.v)
+ self.assertSequenceEqual(tail, junk)
@given(junk_st)
- def test_blob_decode(self, junk: bytes) -> None:
- encoded = b"\x0B\x00\x00\x00\x00\x00\x00\x00\x03\x01test\x01data\x80" + junk
- decoded, tail = loads(encoded)
+ def test_larger_of_chunkLen(self, junk: bytes) -> None:
+ blob = Blob(4, b"testdata2")
+ encoded = dumps(blob)
+ self.assertSequenceEqual(
+ encoded,
+ b"\x0b\x00\x00\x00\x00\x00\x00\x00\x03\x01test\x01data\x812",
+ )
+ decoded, tail = loads(encoded + junk)
+ self.assertEqual(decoded.l, blob.l)
+ self.assertSequenceEqual(decoded.v, blob.v)
+ self.assertSequenceEqual(tail, junk)
+
+ @given(junk_st)
+ def test_empty(self, junk: bytes) -> None:
+ blob = Blob(4, b"")
+ encoded = dumps(blob)
+ self.assertSequenceEqual(
+ encoded,
+ b"\x0b\x00\x00\x00\x00\x00\x00\x00\x03\x80",
+ )
+ decoded, tail = loads(encoded + junk)
self.assertEqual(decoded.l, 4)
- self.assertSequenceEqual(decoded.v, b"testdata")
+ self.assertSequenceEqual(decoded.v, b"")
+ self.assertSequenceEqual(tail, junk)
+
+ @given(
+ integers(min_value=1, max_value=10),
+ integers(min_value=0, max_value=8),
+ junk_st,
+ )
+ def test_symmetric_multiple(self, chunkLen: int, chunks: int, junk: bytes) -> None:
+ chunks = [urandom(chunkLen) for _ in range(chunks)]
+ encoded = b"".join((
+ b"\x0B",
+ (chunkLen-1).to_bytes(8, "big"),
+ b"".join((b"\x01" + chunk) for chunk in chunks),
+ b"\x80",
+ junk,
+ ))
+ decoded, tail = loads(encoded)
+ self.assertEqual(decoded.l, chunkLen)
+ self.assertSequenceEqual(decoded.v, b"".join(chunks))
self.assertSequenceEqual(tail, junk)
def test_throws_when_not_enough_data(self) -> None: