def test_error_on_absurd_lengths(self): s = io.BytesIO() s.write(b'1000000000:pwned!,') s.seek(0) with self.assertRaises(ValueError): tnetstring.load(s) self.assertEqual(s.read(1), b':')
def test_roundtrip_file_examples(self): for data, expect in FORMAT_EXAMPLES.items(): s = io.BytesIO() s.write(data) s.write(b'OK') s.seek(0) self.assertEqual(expect, tnetstring.load(s)) self.assertEqual(b'OK', s.read()) s = io.BytesIO() tnetstring.dump(expect, s) s.write(b'OK') s.seek(0) self.assertEqual(expect, tnetstring.load(s)) self.assertEqual(b'OK', s.read())
def stream(self): """ Yields Flow objects from the dump. """ # There is a weird mingw bug that breaks .tell() when reading from stdin. try: self.fo.tell() except IOError: # pragma: no cover can_tell = False else: can_tell = True off = 0 try: while True: data = tnetstring.load(self.fo) try: data = io_compat.migrate_flow(data) except ValueError as e: raise exceptions.FlowReadException(str(e)) if can_tell: off = self.fo.tell() if data["type"] not in models.FLOW_TYPES: raise exceptions.FlowReadException( "Unknown flow type: {}".format(data["type"])) yield models.FLOW_TYPES[data["type"]].from_state(data) except ValueError: # Error is due to EOF if can_tell and self.fo.tell() == off and self.fo.read() == b'': return raise exceptions.FlowReadException("Invalid data format.")
def stream(self): """ Yields Flow objects from the dump. """ # There is a weird mingw bug that breaks .tell() when reading from stdin. try: self.fo.tell() except IOError: # pragma: no cover can_tell = False else: can_tell = True off = 0 try: while True: data = tnetstring.load(self.fo) try: data = io_compat.migrate_flow(data) except ValueError as e: raise exceptions.FlowReadException(str(e)) if can_tell: off = self.fo.tell() if data["type"] not in models.FLOW_TYPES: raise exceptions.FlowReadException("Unknown flow type: {}".format(data["type"])) yield models.FLOW_TYPES[data["type"]].from_state(data) except ValueError: # Error is due to EOF if can_tell and self.fo.tell() == off and self.fo.read() == b'': return raise exceptions.FlowReadException("Invalid data format.")
def test_roundtrip_file_random(self): for _ in range(500): v = get_random_object() s = io.BytesIO() tnetstring.dump(v, s) s.write(b'OK') s.seek(0) self.assertEqual(v, tnetstring.load(s)) self.assertEqual(b'OK', s.read())
def stream(self): """ Yields Flow objects from the dump. """ try: while True: data = tnetstring.load(self.fo) try: data = io_compat.migrate_flow(data) except ValueError as e: raise exceptions.FlowReadException(str(e)) if data["type"] not in FLOW_TYPES: raise exceptions.FlowReadException("Unknown flow type: {}".format(data["type"])) yield FLOW_TYPES[data["type"]].from_state(data) except ValueError as e: if str(e) == "not a tnetstring: empty file": return # Error is due to EOF raise exceptions.FlowReadException("Invalid data format.")
def stream(self) -> Iterable[flow.Flow]: """ Yields Flow objects from the dump. """ try: while True: data = tnetstring.load(self.fo) try: data = io_compat.migrate_flow(data) except ValueError as e: raise exceptions.FlowReadException(str(e)) if data["type"] not in FLOW_TYPES: raise exceptions.FlowReadException("Unknown flow type: {}".format(data["type"])) yield FLOW_TYPES[data["type"]].from_state(data) except ValueError as e: if str(e) == "not a tnetstring: empty file": return # Error is due to EOF raise exceptions.FlowReadException("Invalid data format.")