def test_deserialize_empty(self): bytes_in = b'' with self.assertRaises(ValueError): sexp_from_stream(io.BytesIO(bytes_in), to_sexp_f) with self.assertRaises(ValueError): sexp_buffer_from_stream(io.BytesIO(bytes_in))
def test_deserialize_truncated_size(self): # fe means the total number of bytes in the length-prefix is 7 # one for each bit set. 5 bytes is too few bytes_in = b'\xfe ' with self.assertRaises(ValueError): sexp_from_stream(io.BytesIO(bytes_in), to_sexp_f) with self.assertRaises(ValueError): sexp_buffer_from_stream(io.BytesIO(bytes_in))
def test_deserialize_truncated_blob(self): # this is a complete length prefix. The blob is supposed to be 63 bytes # the blob itself is truncated though, it's less than 63 bytes bytes_in = b'\xbf ' with self.assertRaises(ValueError): sexp_from_stream(io.BytesIO(bytes_in), to_sexp_f) with self.assertRaises(ValueError): sexp_buffer_from_stream(io.BytesIO(bytes_in))
def test_deserialize_large_blob(self): # this length prefix is 7 bytes long, the last 6 bytes specifies the # length of the blob, which is 0xffffffffffff, or (2^48 - 1) # we don't support blobs this large, and we should fail immediately when # exceeding the max blob size, rather than trying to read this many # bytes from the stream bytes_in = b'\xfe' + b'\xff' * 6 with self.assertRaises(ValueError): sexp_from_stream(InfiniteStream(bytes_in), to_sexp_f) with self.assertRaises(ValueError): sexp_buffer_from_stream(InfiniteStream(bytes_in))
def check_serde(self, s): v = to_sexp_f(s) b = v.as_bin() v1 = sexp_from_stream(io.BytesIO(b), to_sexp_f) if v != v1: print("%s: %d %s %s" % (v, len(b), b, v1)) breakpoint() b = v.as_bin() v1 = sexp_from_stream(io.BytesIO(b), to_sexp_f) self.assertEqual(v, v1) # this copies the bytes that represent a single s-expression, just to # know where the message ends. It doesn't build a python representaion # of it buf = sexp_buffer_from_stream(io.BytesIO(b)) self.assertEqual(buf, b)
def parse(cls, f) -> "SerializedProgram": tmp = sexp_buffer_from_stream(f) return SerializedProgram.from_bytes(tmp)