Beispiel #1
0
    def test_sanity(self):
        VECTORS = [
            {
                'name': 'empty',
                'decoded': b'',
                'encoded': b'',
            },
            {
                'name': 'basic',
                'decoded': b'a',
                'encoded': b'|a1',
            },
            {
                'name': 'extended',
                'decoded': b'aaaabbbbbcccccc',
                'encoded': b'|a4|b5|c6',
            },
            {
                'name': 'unprintable',
                'decoded': b'aaabbb\n\n\n\nccc',
                'encoded': b'|a3|b3|\\x0a4|c3',
            },
            {
                'name': 'long',
                'decoded': (b'a' * 22) + (b'c' * 555) + (b'\xff' * 33),
                'encoded': b'|a22|c555|\\xff33',
            },
            {
                'name': 'use escape',
                'decoded': b'||||&&&&\\\\\\',
                'encoded': b'|\\x7c4|&4|\\x5c3',
            },
        ]

        srle = SRLE()
        for v in VECTORS:
            print('vector: %s' % v['name'])
            sout = io.BytesIO()
            srle.encode(io.BytesIO(v['decoded']), sout)
            self.assertEqual(
                v['encoded'],
                sout.getvalue(),
                msg='Vector %s' % v['name'],
            )
            sout2 = io.BytesIO()
            sout.seek(0)
            srle.decode(sout, sout2)
            self.assertEqual(v['decoded'],
                             sout2.getvalue(),
                             msg='Vector %s' % v['name'])
Beispiel #2
0
    def test_random(self):

        for n in range(10):

            DECODED = os.urandom(4096)

            srle = SRLE()
            sout = io.BytesIO()
            srle.encode(io.BytesIO(DECODED), sout)
            sout.seek(0)
            sout2 = io.BytesIO()
            srle.decode(sout, sout2)
            self.assertEqual(
                DECODED,
                sout2.getvalue(),
            )
Beispiel #3
0
    def test_custom_separator(self):

        SEPARATORS = ('|', 'x', SRLE.ESCAPE)
        DECODED = b'aabbbcccc'
        ENCODED = '|a2|b3|c4'

        for s in SEPARATORS:
            srle = SRLE(separator=s)

            sout = io.BytesIO()
            srle.encode(io.BytesIO(DECODED), sout)
            self.assertEqual(
                ENCODED.replace('|', srle.separator).encode('ascii'),
                sout.getvalue(),
            )
            sout.seek(0)
            sout2 = io.BytesIO()
            srle.decode(sout, sout2)
            self.assertEqual(
                DECODED,
                sout2.getvalue(),
            )