Exemplo n.º 1
0
 def test_terminator_handler_until(self):
     self.assertFieldFromStreamEqual(
         b"abcdef\0gh", b"abcdef",
         BytesField(terminator=b"\0", terminator_handler='until'))
     self.assertFieldToStreamEqual(
         b"abcdef", b"abcdef",
         BytesField(terminator=b"\0", terminator_handler='until'))
Exemplo n.º 2
0
 def test_length_and_misaligned_padding(self):
     with self.assertRaises(WriteError):
         self.call_field_to_stream(BytesField(length=7, padding=b"\0\0"),
                                   b"ab")
     self.assertFieldToStreamEqual(
         b"ab\0\0\0\0\0", b"ab",
         BytesField(length=7, padding=b"\0\0", strict=False))
Exemplo n.º 3
0
 def test_dynamic_length(self):
     self.assertFieldStreamEqual(b"abc",
                                 b"abc",
                                 BytesField(length='length'),
                                 parsed_fields={'length': 3})
     self.assertFieldStreamEqual(b"abc", b"abc",
                                 BytesField(length=lambda c: 3))
Exemplo n.º 4
0
 def test_multibyte_terminator(self):
     self.assertFieldStreamEqual(b"abcdef\0\0", b"abcdef",
                                 BytesField(terminator=b"\0\0"))
     self.assertFieldFromStreamEqual(b"a\0bc\0\0def", b"a\0bc",
                                     BytesField(terminator=b"\0\0"))
     self.assertFieldStreamEqual(b"abcde\0\0\0", b"abcde\0",
                                 BytesField(terminator=b"\0\0", step=2))
Exemplo n.º 5
0
 def test_terminator_handler_until_multibyte_misaligned_field(self):
     self.assertFieldFromStreamEqual(
         b"1231231\0\0", b"1231231",
         BytesField(terminator=b"\0\0", step=3, terminator_handler='until'))
     self.assertFieldFromStreamEqual(
         b"1231231\0\0", b"1231231",
         BytesField(terminator=b"\0\0", step=1, terminator_handler='until'))
Exemplo n.º 6
0
 def test_initialize(self):
     with self.assertRaises(DefinitionError):
         BytesField(terminator=b'\0', padding=b'\0')
     with self.assertRaises(DefinitionError):
         BytesField()
     with self.assertRaises(DefinitionError):
         BytesField(terminator_handler='asdf', terminator=b'\0')
     with self.assertRaises(DefinitionError):
         BytesField(terminator_handler='until', length=3)
Exemplo n.º 7
0
 def test_terminator_handler_include(self):
     self.assertFieldStreamEqual(
         b"abcdef\0", b"abcdef\0",
         BytesField(terminator=b"\0", terminator_handler='include'))
     with self.assertRaises(WriteError):
         self.call_field_to_stream(
             BytesField(terminator=b"\0", terminator_handler='include'),
             b"asdf")
     self.call_field_to_stream(
         BytesField(terminator=b"\0",
                    terminator_handler='include',
                    strict=False), b"asdf")
Exemplo n.º 8
0
 def test_length_and_padding(self):
     self.assertFieldStreamEqual(b"a\0\0\0\0\0\0\0", b"a",
                                 BytesField(length=8, padding=b"\0"))
     self.assertFieldStreamEqual(b"aXPADXPAD", b"a",
                                 BytesField(length=9, padding=b"XPAD"))
     self.assertFieldStreamEqual(
         b"abcd\0\0", b"abcd", BytesField(length=6, padding=b"\0\0",
                                          step=2))
     self.assertFieldStreamEqual(
         b"abc\0\0\0", b"abc\0",
         BytesField(length=6, padding=b"\0\0", step=2))
     self.assertFieldStreamEqual(b"abc\0\0\0\0", b"abc",
                                 BytesField(length=7, padding=b"\0\0"))
Exemplo n.º 9
0
 def test_length_and_terminator_insufficient_bytes(self):
     with self.assertRaises(StreamExhaustedError):
         self.call_field_from_stream(BytesField(terminator=b'\0', length=3),
                                     b"abc")
     with self.assertRaises(StreamExhaustedError):
         self.call_field_from_stream(
             BytesField(terminator=b'\0\0', length=3), b"abc")
     with self.assertRaises(StreamExhaustedError):
         self.call_field_from_stream(BytesField(terminator=b'\0', length=8),
                                     b"abc")
     self.assertFieldFromStreamEqual(
         b"abc", b"abc", BytesField(length=3,
                                    terminator=b'\0',
                                    strict=False))
     self.assertFieldFromStreamEqual(
         b"ab", b"ab", BytesField(length=3, terminator=b'\0', strict=False))
Exemplo n.º 10
0
    def test_terminator_handler_until_with_peek_multibyte_misaligned_field(
            self):
        with self.subTest("step larger than terminator length"):
            pio = PeekableBytesIO(b"1231231\0\0")
            field = BytesField(terminator=b"\0\0",
                               step=3,
                               terminator_handler='until')
            result = field.from_stream(pio, ParsingContext())
            self.assertEqual(b"1231231", result[0])
            self.assertEqual(b"\0\0", pio.read(2))

        with self.subTest("step smaller than terminator length"):
            pio = PeekableBytesIO(b"1231231\0\0")
            field = BytesField(terminator=b"\0\0",
                               step=1,
                               terminator_handler='until')
            result = field.from_stream(pio, ParsingContext())
            self.assertEqual(b"1231231", result[0])
            self.assertEqual(b"\0\0", pio.read(2))
Exemplo n.º 11
0
 class Struct(Structure):
     str0 = BytesField(terminator=b'\0', terminator_handler='until')
     str1 = BytesField(length=3)
Exemplo n.º 12
0
 class Struct(Structure):
     len = IntegerField(length=1,
                        byte_order='little',
                        override=lambda c, v: v)
     str1 = BytesField(length='len')
Exemplo n.º 13
0
 def test_length_insufficient_bytes(self):
     with self.assertRaises(StreamExhaustedError):
         self.call_field_from_stream(BytesField(length=8), b"abc")
     self.call_field_from_stream(BytesField(length=8, strict=False), b"abc")
Exemplo n.º 14
0
 def test_length_and_terminator(self):
     self.assertFieldStreamEqual(b"abcdef\0", b"abcdef",
                                 BytesField(length=7, terminator=b"\0"))
     self.assertFieldFromStreamEqual(b"abc\0def", b"abc",
                                     BytesField(length=7, terminator=b"\0"))
Exemplo n.º 15
0
 class Struct(Structure):
     len = IntegerField(length=1, byte_order='little')
     str1 = BytesField(length='len')
Exemplo n.º 16
0
 def test_length(self):
     self.assertFieldStreamEqual(b"abc", b"abc", BytesField(length=3))
     self.assertFieldStreamEqual(b"", b"", BytesField(length=0))
     self.assertFieldFromStreamEqual(b"abcdef", b"abc",
                                     BytesField(length=3))
Exemplo n.º 17
0
    def test_len(self):
        self.assertEqual(3, len(BytesField(length=3)))

        self.assertEqual(6, BytesField(length=3, skip=3)._length_sum(0))
Exemplo n.º 18
0
 def test_terminator_handler_consume_length(self):
     self.assertFieldStreamEqual(
         b"abcdef\0", b"abcdef",
         BytesField(terminator=b"\0",
                    terminator_handler='consume',
                    length=7))
Exemplo n.º 19
0
 def test_negative_length(self):
     self.assertFieldStreamEqual(b"abc", b"abc", BytesField(length=-1))
     self.assertFieldStreamEqual(b"", b"", BytesField(length=-1))
     self.assertFieldStreamEqual(b"asd\0", b"asd",
                                 BytesField(length=-1, terminator=b"\0"))
Exemplo n.º 20
0
 def test_terminator_handler_until_with_peek(self):
     pio = PeekableBytesIO(b"abcdef\0gh")
     field = BytesField(terminator=b"\0", terminator_handler='until')
     result = field.from_stream(pio, ParsingContext())
     self.assertEqual(b"abcdef", result[0])
     self.assertEqual(b"\0", pio.read(1))
Exemplo n.º 21
0
 def test_length_write_insufficient_bytes(self):
     with self.assertRaises(WriteError):
         self.call_field_to_stream(BytesField(length=7), b"ab")
     self.assertFieldToStreamEqual(b"ab", b"ab",
                                   BytesField(length=7, strict=False))
Exemplo n.º 22
0
 def test_length_write_too_many_bytes(self):
     with self.assertRaises(WriteError):
         self.call_field_to_stream(BytesField(length=2), b"abcdefg")
     self.assertFieldToStreamEqual(b"ab", b"abcdefg",
                                   BytesField(length=2, strict=False))