tests/test-cbor.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Fri, 28 Feb 2020 14:34:37 +0100
changeset 44464 23df5ecfa365
parent 43996 6dbb18e1ac8d
child 45942 89a2afe31e82
permissions -rw-r--r--
nodemap: check that a simple lookup works fine I realised the code was not exercised much. Differential Revision: https://phab.mercurial-scm.org/D8180
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
from __future__ import absolute_import
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
41160
a9905045b992 tests: get access to thirdparty.cbor without requiring it to be installed
Augie Fackler <augie@google.com>
parents: 40124
diff changeset
     3
import os
a9905045b992 tests: get access to thirdparty.cbor without requiring it to be installed
Augie Fackler <augie@google.com>
parents: 40124
diff changeset
     4
import sys
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
import unittest
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
41160
a9905045b992 tests: get access to thirdparty.cbor without requiring it to be installed
Augie Fackler <augie@google.com>
parents: 40124
diff changeset
     7
# TODO migrate to canned cbor test strings and stop using thirdparty.cbor
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
     8
tpp = os.path.normpath(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
     9
    os.path.join(os.path.dirname(__file__), '..', 'mercurial', 'thirdparty')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    10
)
41160
a9905045b992 tests: get access to thirdparty.cbor without requiring it to be installed
Augie Fackler <augie@google.com>
parents: 40124
diff changeset
    11
if not os.path.exists(tpp):
a9905045b992 tests: get access to thirdparty.cbor without requiring it to be installed
Augie Fackler <augie@google.com>
parents: 40124
diff changeset
    12
    # skip, not in a repo
a9905045b992 tests: get access to thirdparty.cbor without requiring it to be installed
Augie Fackler <augie@google.com>
parents: 40124
diff changeset
    13
    sys.exit(80)
a9905045b992 tests: get access to thirdparty.cbor without requiring it to be installed
Augie Fackler <augie@google.com>
parents: 40124
diff changeset
    14
sys.path[0:0] = [tpp]
a9905045b992 tests: get access to thirdparty.cbor without requiring it to be installed
Augie Fackler <augie@google.com>
parents: 40124
diff changeset
    15
import cbor
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    16
41160
a9905045b992 tests: get access to thirdparty.cbor without requiring it to be installed
Augie Fackler <augie@google.com>
parents: 40124
diff changeset
    17
del sys.path[0]
a9905045b992 tests: get access to thirdparty.cbor without requiring it to be installed
Augie Fackler <augie@google.com>
parents: 40124
diff changeset
    18
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    19
from mercurial.utils import cborutil
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    20
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    22
class TestCase(unittest.TestCase):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    23
    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    24
        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    25
        # the regex version.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    26
        assertRaisesRegex = (  # camelcase-required
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    27
            unittest.TestCase.assertRaisesRegexp
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    28
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    29
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    30
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
def loadit(it):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
    return cbor.loads(b''.join(it))
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    34
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    35
class BytestringTests(TestCase):
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
    def testsimple(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
        self.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    38
            list(cborutil.streamencode(b'foobar')), [b'\x46', b'foobar']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    39
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    40
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    41
        self.assertEqual(loadit(cborutil.streamencode(b'foobar')), b'foobar')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    42
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    43
        self.assertEqual(cborutil.decodeall(b'\x46foobar'), [b'foobar'])
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
        self.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    46
            cborutil.decodeall(b'\x46foobar\x45fizbi'), [b'foobar', b'fizbi']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    47
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    48
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
    def testlong(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
        source = b'x' * 1048576
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
        self.assertEqual(loadit(cborutil.streamencode(source)), source)
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    54
        encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    55
        self.assertEqual(cborutil.decodeall(encoded), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    56
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
    def testfromiter(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
        # This is the example from RFC 7049 Section 2.2.2.
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
        source = [b'\xaa\xbb\xcc\xdd', b'\xee\xff\x99']
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
        self.assertEqual(
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    62
            list(cborutil.streamencodebytestringfromiter(source)),
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
            [
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    64
                b'\x5f',
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
                b'\x44',
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
                b'\xaa\xbb\xcc\xdd',
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
                b'\x43',
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    68
                b'\xee\xff\x99',
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    69
                b'\xff',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    70
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    71
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
        self.assertEqual(
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
            loadit(cborutil.streamencodebytestringfromiter(source)),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    75
            b''.join(source),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    76
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    77
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    78
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    79
            cborutil.decodeall(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    80
                b'\x5f\x44\xaa\xbb\xcc\xdd' b'\x43\xee\xff\x99\xff'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    81
            ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    82
            [b'\xaa\xbb\xcc\xdd', b'\xee\xff\x99', b''],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    83
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    84
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    85
        for i, chunk in enumerate(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    86
            cborutil.decodeall(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    87
                b'\x5f\x44\xaa\xbb\xcc\xdd' b'\x43\xee\xff\x99\xff'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    88
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
    89
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    90
            self.assertIsInstance(chunk, cborutil.bytestringchunk)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    91
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    92
            if i == 0:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    93
                self.assertTrue(chunk.isfirst)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    94
            else:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    95
                self.assertFalse(chunk.isfirst)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    96
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    97
            if i == 2:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    98
                self.assertTrue(chunk.islast)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
    99
            else:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   100
                self.assertFalse(chunk.islast)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   101
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   102
    def testfromiterlarge(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   103
        source = [b'a' * 16, b'b' * 128, b'c' * 1024, b'd' * 1048576]
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   104
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   105
        self.assertEqual(
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   106
            loadit(cborutil.streamencodebytestringfromiter(source)),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   107
            b''.join(source),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   108
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   109
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   110
    def testindefinite(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   111
        source = b'\x00\x01\x02\x03' + b'\xff' * 16384
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   112
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   113
        it = cborutil.streamencodeindefinitebytestring(source, chunksize=2)
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   114
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   115
        self.assertEqual(next(it), b'\x5f')
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   116
        self.assertEqual(next(it), b'\x42')
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   117
        self.assertEqual(next(it), b'\x00\x01')
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   118
        self.assertEqual(next(it), b'\x42')
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   119
        self.assertEqual(next(it), b'\x02\x03')
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   120
        self.assertEqual(next(it), b'\x42')
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   121
        self.assertEqual(next(it), b'\xff\xff')
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   122
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   123
        dest = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   124
            cborutil.streamencodeindefinitebytestring(source, chunksize=42)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   125
        )
37899
2b3b6187c316 tests: port test-cbor.py to Python 3
Augie Fackler <augie@google.com>
parents: 37711
diff changeset
   126
        self.assertEqual(cbor.loads(dest), source)
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   127
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   128
        self.assertEqual(b''.join(cborutil.decodeall(dest)), source)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   129
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   130
        for chunk in cborutil.decodeall(dest):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   131
            self.assertIsInstance(chunk, cborutil.bytestringchunk)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   132
            self.assertIn(len(chunk), (0, 8, 42))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   133
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   134
        encoded = b'\x5f\xff'
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   135
        b = cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   136
        self.assertEqual(b, [b''])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   137
        self.assertTrue(b[0].isfirst)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   138
        self.assertTrue(b[0].islast)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   139
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   140
    def testdecodevariouslengths(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   141
        for i in (0, 1, 22, 23, 24, 25, 254, 255, 256, 65534, 65535, 65536):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   142
            source = b'x' * i
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   143
            encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   144
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   145
            if len(source) < 24:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   146
                hlen = 1
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   147
            elif len(source) < 256:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   148
                hlen = 2
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   149
            elif len(source) < 65536:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   150
                hlen = 3
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   151
            elif len(source) < 1048576:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   152
                hlen = 5
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   153
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   154
            self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   155
                cborutil.decodeitem(encoded),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   156
                (True, source, hlen + len(source), cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   157
            )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   158
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   159
    def testpartialdecode(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   160
        encoded = b''.join(cborutil.streamencode(b'foobar'))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   161
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   162
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   163
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   164
            (False, None, -6, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   165
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   166
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   167
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   168
            (False, None, -5, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   169
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   170
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   171
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   172
            (False, None, -4, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   173
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   174
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   175
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   176
            (False, None, -3, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   177
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   178
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   179
            cborutil.decodeitem(encoded[0:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   180
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   181
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   182
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   183
            cborutil.decodeitem(encoded[0:6]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   184
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   185
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   186
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   187
            cborutil.decodeitem(encoded[0:7]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   188
            (True, b'foobar', 7, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   189
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   190
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   191
    def testpartialdecodevariouslengths(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   192
        lens = [
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   193
            2,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   194
            3,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   195
            10,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   196
            23,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   197
            24,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   198
            25,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   199
            31,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   200
            100,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   201
            254,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   202
            255,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   203
            256,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   204
            257,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   205
            16384,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   206
            65534,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   207
            65535,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   208
            65536,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   209
            65537,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   210
            131071,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   211
            131072,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   212
            131073,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   213
            1048575,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   214
            1048576,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   215
            1048577,
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   216
        ]
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   217
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   218
        for size in lens:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   219
            if size < 24:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   220
                hlen = 1
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   221
            elif size < 2 ** 8:
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   222
                hlen = 2
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   223
            elif size < 2 ** 16:
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   224
                hlen = 3
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   225
            elif size < 2 ** 32:
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   226
                hlen = 5
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   227
            else:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   228
                assert False
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   229
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   230
            source = b'x' * size
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   231
            encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   232
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   233
            res = cborutil.decodeitem(encoded[0:1])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   234
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   235
            if hlen > 1:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   236
                self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   237
                    res, (False, None, -(hlen - 1), cborutil.SPECIAL_NONE)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   238
                )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   239
            else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   240
                self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   241
                    res,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   242
                    (False, None, -(size + hlen - 1), cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   243
                )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   244
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   245
            # Decoding partial header reports remaining header size.
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   246
            for i in range(hlen - 1):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   247
                self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   248
                    cborutil.decodeitem(encoded[0 : i + 1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   249
                    (False, None, -(hlen - i - 1), cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   250
                )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   251
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   252
            # Decoding complete header reports item size.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   253
            self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   254
                cborutil.decodeitem(encoded[0:hlen]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   255
                (False, None, -size, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   256
            )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   257
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   258
            # Decoding single byte after header reports item size - 1
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   259
            self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   260
                cborutil.decodeitem(encoded[0 : hlen + 1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   261
                (False, None, -(size - 1), cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   262
            )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   263
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   264
            # Decoding all but the last byte reports -1 needed.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   265
            self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   266
                cborutil.decodeitem(encoded[0 : hlen + size - 1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   267
                (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   268
            )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   269
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   270
            # Decoding last byte retrieves value.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   271
            self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   272
                cborutil.decodeitem(encoded[0 : hlen + size]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   273
                (True, source, hlen + size, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   274
            )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   275
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   276
    def testindefinitepartialdecode(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   277
        encoded = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   278
            cborutil.streamencodebytestringfromiter([b'foobar', b'biz'])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   279
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   280
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   281
        # First item should be begin of bytestring special.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   282
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   283
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   284
            (True, None, 1, cborutil.SPECIAL_START_INDEFINITE_BYTESTRING),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   285
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   286
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   287
        # Second item should be the first chunk. But only available when
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   288
        # we give it 7 bytes (1 byte header + 6 byte chunk).
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   289
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   290
            cborutil.decodeitem(encoded[1:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   291
            (False, None, -6, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   292
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   293
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   294
            cborutil.decodeitem(encoded[1:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   295
            (False, None, -5, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   296
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   297
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   298
            cborutil.decodeitem(encoded[1:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   299
            (False, None, -4, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   300
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   301
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   302
            cborutil.decodeitem(encoded[1:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   303
            (False, None, -3, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   304
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   305
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   306
            cborutil.decodeitem(encoded[1:6]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   307
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   308
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   309
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   310
            cborutil.decodeitem(encoded[1:7]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   311
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   312
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   313
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   314
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   315
            cborutil.decodeitem(encoded[1:8]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   316
            (True, b'foobar', 7, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   317
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   318
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   319
        # Third item should be second chunk. But only available when
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   320
        # we give it 4 bytes (1 byte header + 3 byte chunk).
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   321
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   322
            cborutil.decodeitem(encoded[8:9]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   323
            (False, None, -3, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   324
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   325
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   326
            cborutil.decodeitem(encoded[8:10]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   327
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   328
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   329
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   330
            cborutil.decodeitem(encoded[8:11]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   331
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   332
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   333
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   334
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   335
            cborutil.decodeitem(encoded[8:12]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   336
            (True, b'biz', 4, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   337
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   338
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   339
        # Fourth item should be end of indefinite stream marker.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   340
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   341
            cborutil.decodeitem(encoded[12:13]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   342
            (True, None, 1, cborutil.SPECIAL_INDEFINITE_BREAK),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   343
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   344
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   345
        # Now test the behavior when going through the decoder.
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   346
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   347
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   348
            cborutil.sansiodecoder().decode(encoded[0:1]), (False, 1, 0)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   349
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   350
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   351
            cborutil.sansiodecoder().decode(encoded[0:2]), (False, 1, 6)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   352
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   353
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   354
            cborutil.sansiodecoder().decode(encoded[0:3]), (False, 1, 5)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   355
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   356
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   357
            cborutil.sansiodecoder().decode(encoded[0:4]), (False, 1, 4)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   358
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   359
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   360
            cborutil.sansiodecoder().decode(encoded[0:5]), (False, 1, 3)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   361
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   362
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   363
            cborutil.sansiodecoder().decode(encoded[0:6]), (False, 1, 2)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   364
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   365
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   366
            cborutil.sansiodecoder().decode(encoded[0:7]), (False, 1, 1)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   367
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   368
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   369
            cborutil.sansiodecoder().decode(encoded[0:8]), (True, 8, 0)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   370
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   371
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   372
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   373
            cborutil.sansiodecoder().decode(encoded[0:9]), (True, 8, 3)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   374
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   375
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   376
            cborutil.sansiodecoder().decode(encoded[0:10]), (True, 8, 2)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   377
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   378
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   379
            cborutil.sansiodecoder().decode(encoded[0:11]), (True, 8, 1)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   380
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   381
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   382
            cborutil.sansiodecoder().decode(encoded[0:12]), (True, 12, 0)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   383
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   384
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   385
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   386
            cborutil.sansiodecoder().decode(encoded[0:13]), (True, 13, 0)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   387
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   388
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   389
        decoder = cborutil.sansiodecoder()
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   390
        decoder.decode(encoded[0:8])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   391
        values = decoder.getavailable()
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   392
        self.assertEqual(values, [b'foobar'])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   393
        self.assertTrue(values[0].isfirst)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   394
        self.assertFalse(values[0].islast)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   395
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   396
        self.assertEqual(decoder.decode(encoded[8:12]), (True, 4, 0))
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   397
        values = decoder.getavailable()
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   398
        self.assertEqual(values, [b'biz'])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   399
        self.assertFalse(values[0].isfirst)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   400
        self.assertFalse(values[0].islast)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   401
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   402
        self.assertEqual(decoder.decode(encoded[12:]), (True, 1, 0))
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   403
        values = decoder.getavailable()
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   404
        self.assertEqual(values, [b''])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   405
        self.assertFalse(values[0].isfirst)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   406
        self.assertTrue(values[0].islast)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   407
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   408
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   409
class StringTests(TestCase):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   410
    def testdecodeforbidden(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   411
        encoded = b'\x63foo'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   412
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   413
            cborutil.CBORDecodeError, 'string major type not supported'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   414
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   415
            cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   416
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   417
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   418
class IntTests(TestCase):
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   419
    def testsmall(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   420
        self.assertEqual(list(cborutil.streamencode(0)), [b'\x00'])
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   421
        self.assertEqual(cborutil.decodeall(b'\x00'), [0])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   422
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   423
        self.assertEqual(list(cborutil.streamencode(1)), [b'\x01'])
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   424
        self.assertEqual(cborutil.decodeall(b'\x01'), [1])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   425
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   426
        self.assertEqual(list(cborutil.streamencode(2)), [b'\x02'])
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   427
        self.assertEqual(cborutil.decodeall(b'\x02'), [2])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   428
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   429
        self.assertEqual(list(cborutil.streamencode(3)), [b'\x03'])
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   430
        self.assertEqual(cborutil.decodeall(b'\x03'), [3])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   431
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   432
        self.assertEqual(list(cborutil.streamencode(4)), [b'\x04'])
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   433
        self.assertEqual(cborutil.decodeall(b'\x04'), [4])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   434
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   435
        # Multiple value decode works.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   436
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   437
            cborutil.decodeall(b'\x00\x01\x02\x03\x04'), [0, 1, 2, 3, 4]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   438
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   439
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   440
    def testnegativesmall(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   441
        self.assertEqual(list(cborutil.streamencode(-1)), [b'\x20'])
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   442
        self.assertEqual(cborutil.decodeall(b'\x20'), [-1])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   443
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   444
        self.assertEqual(list(cborutil.streamencode(-2)), [b'\x21'])
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   445
        self.assertEqual(cborutil.decodeall(b'\x21'), [-2])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   446
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   447
        self.assertEqual(list(cborutil.streamencode(-3)), [b'\x22'])
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   448
        self.assertEqual(cborutil.decodeall(b'\x22'), [-3])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   449
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   450
        self.assertEqual(list(cborutil.streamencode(-4)), [b'\x23'])
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   451
        self.assertEqual(cborutil.decodeall(b'\x23'), [-4])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   452
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   453
        self.assertEqual(list(cborutil.streamencode(-5)), [b'\x24'])
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   454
        self.assertEqual(cborutil.decodeall(b'\x24'), [-5])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   455
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   456
        # Multiple value decode works.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   457
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   458
            cborutil.decodeall(b'\x20\x21\x22\x23\x24'), [-1, -2, -3, -4, -5]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   459
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   460
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   461
    def testrange(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   462
        for i in range(-70000, 70000, 10):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   463
            encoded = b''.join(cborutil.streamencode(i))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   464
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   465
            self.assertEqual(encoded, cbor.dumps(i))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   466
            self.assertEqual(cborutil.decodeall(encoded), [i])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   467
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   468
    def testdecodepartialubyte(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   469
        encoded = b''.join(cborutil.streamencode(250))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   470
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   471
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   472
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   473
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   474
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   475
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   476
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   477
            (True, 250, 2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   478
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   479
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   480
    def testdecodepartialbyte(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   481
        encoded = b''.join(cborutil.streamencode(-42))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   482
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   483
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   484
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   485
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   486
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   487
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   488
            (True, -42, 2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   489
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   490
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   491
    def testdecodepartialushort(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   492
        encoded = b''.join(cborutil.streamencode(2 ** 15))
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   493
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   494
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   495
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   496
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   497
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   498
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   499
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   500
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   501
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   502
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   503
            cborutil.decodeitem(encoded[0:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   504
            (True, 2 ** 15, 3, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   505
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   506
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   507
    def testdecodepartialshort(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   508
        encoded = b''.join(cborutil.streamencode(-1024))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   509
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   510
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   511
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   512
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   513
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   514
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   515
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   516
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   517
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   518
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   519
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   520
            (True, -1024, 3, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   521
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   522
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   523
    def testdecodepartialulong(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   524
        encoded = b''.join(cborutil.streamencode(2 ** 28))
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   525
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   526
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   527
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   528
            (False, None, -4, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   529
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   530
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   531
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   532
            (False, None, -3, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   533
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   534
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   535
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   536
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   537
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   538
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   539
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   540
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   541
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   542
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   543
            cborutil.decodeitem(encoded[0:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   544
            (True, 2 ** 28, 5, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   545
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   546
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   547
    def testdecodepartiallong(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   548
        encoded = b''.join(cborutil.streamencode(-1048580))
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   549
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   550
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   551
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   552
            (False, None, -4, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   553
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   554
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   555
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   556
            (False, None, -3, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   557
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   558
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   559
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   560
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   561
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   562
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   563
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   564
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   565
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   566
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   567
            cborutil.decodeitem(encoded[0:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   568
            (True, -1048580, 5, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   569
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   570
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   571
    def testdecodepartialulonglong(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   572
        encoded = b''.join(cborutil.streamencode(2 ** 32))
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   573
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   574
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   575
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   576
            (False, None, -8, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   577
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   578
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   579
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   580
            (False, None, -7, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   581
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   582
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   583
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   584
            (False, None, -6, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   585
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   586
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   587
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   588
            (False, None, -5, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   589
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   590
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   591
            cborutil.decodeitem(encoded[0:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   592
            (False, None, -4, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   593
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   594
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   595
            cborutil.decodeitem(encoded[0:6]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   596
            (False, None, -3, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   597
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   598
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   599
            cborutil.decodeitem(encoded[0:7]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   600
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   601
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   602
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   603
            cborutil.decodeitem(encoded[0:8]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   604
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   605
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   606
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   607
            cborutil.decodeitem(encoded[0:9]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   608
            (True, 2 ** 32, 9, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   609
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   610
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   611
        with self.assertRaisesRegex(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   612
            cborutil.CBORDecodeError, 'input data not fully consumed'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   613
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   614
            cborutil.decodeall(encoded[0:1])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   615
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   616
        with self.assertRaisesRegex(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   617
            cborutil.CBORDecodeError, 'input data not fully consumed'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   618
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   619
            cborutil.decodeall(encoded[0:2])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   620
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   621
    def testdecodepartiallonglong(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   622
        encoded = b''.join(cborutil.streamencode(-7000000000))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   623
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   624
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   625
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   626
            (False, None, -8, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   627
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   628
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   629
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   630
            (False, None, -7, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   631
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   632
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   633
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   634
            (False, None, -6, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   635
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   636
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   637
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   638
            (False, None, -5, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   639
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   640
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   641
            cborutil.decodeitem(encoded[0:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   642
            (False, None, -4, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   643
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   644
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   645
            cborutil.decodeitem(encoded[0:6]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   646
            (False, None, -3, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   647
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   648
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   649
            cborutil.decodeitem(encoded[0:7]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   650
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   651
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   652
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   653
            cborutil.decodeitem(encoded[0:8]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   654
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   655
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   656
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   657
            cborutil.decodeitem(encoded[0:9]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   658
            (True, -7000000000, 9, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   659
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   660
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   661
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   662
class ArrayTests(TestCase):
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   663
    def testempty(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   664
        self.assertEqual(list(cborutil.streamencode([])), [b'\x80'])
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   665
        self.assertEqual(loadit(cborutil.streamencode([])), [])
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   666
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   667
        self.assertEqual(cborutil.decodeall(b'\x80'), [[]])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   668
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   669
    def testbasic(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   670
        source = [b'foo', b'bar', 1, -10]
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   671
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   672
        chunks = [b'\x84', b'\x43', b'foo', b'\x43', b'bar', b'\x01', b'\x29']
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   673
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   674
        self.assertEqual(list(cborutil.streamencode(source)), chunks)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   675
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   676
        self.assertEqual(cborutil.decodeall(b''.join(chunks)), [source])
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   677
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   678
    def testemptyfromiter(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   679
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   680
            b''.join(cborutil.streamencodearrayfromiter([])), b'\x9f\xff'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   681
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   682
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   683
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   684
            cborutil.CBORDecodeError, 'indefinite length uint not allowed'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   685
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   686
            cborutil.decodeall(b'\x9f\xff')
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   687
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   688
    def testfromiter1(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   689
        source = [b'foo']
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   690
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   691
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   692
            list(cborutil.streamencodearrayfromiter(source)),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   693
            [b'\x9f', b'\x43', b'foo', b'\xff',],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   694
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   695
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   696
        dest = b''.join(cborutil.streamencodearrayfromiter(source))
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   697
        self.assertEqual(cbor.loads(dest), source)
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   698
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   699
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   700
            cborutil.CBORDecodeError, 'indefinite length uint not allowed'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   701
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   702
            cborutil.decodeall(dest)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   703
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   704
    def testtuple(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   705
        source = (b'foo', None, 42)
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   706
        encoded = b''.join(cborutil.streamencode(source))
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   707
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   708
        self.assertEqual(cbor.loads(encoded), list(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   709
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   710
        self.assertEqual(cborutil.decodeall(encoded), [list(source)])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   711
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   712
    def testpartialdecode(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   713
        source = list(range(4))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   714
        encoded = b''.join(cborutil.streamencode(source))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   715
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   716
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   717
            (True, 4, 1, cborutil.SPECIAL_START_ARRAY),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   718
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   719
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   720
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   721
            (True, 4, 1, cborutil.SPECIAL_START_ARRAY),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   722
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   723
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   724
        source = list(range(23))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   725
        encoded = b''.join(cborutil.streamencode(source))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   726
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   727
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   728
            (True, 23, 1, cborutil.SPECIAL_START_ARRAY),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   729
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   730
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   731
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   732
            (True, 23, 1, cborutil.SPECIAL_START_ARRAY),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   733
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   734
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   735
        source = list(range(24))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   736
        encoded = b''.join(cborutil.streamencode(source))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   737
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   738
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   739
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   740
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   741
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   742
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   743
            (True, 24, 2, cborutil.SPECIAL_START_ARRAY),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   744
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   745
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   746
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   747
            (True, 24, 2, cborutil.SPECIAL_START_ARRAY),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   748
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   749
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   750
        source = list(range(256))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   751
        encoded = b''.join(cborutil.streamencode(source))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   752
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   753
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   754
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   755
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   756
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   757
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   758
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   759
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   760
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   761
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   762
            (True, 256, 3, cborutil.SPECIAL_START_ARRAY),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   763
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   764
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   765
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   766
            (True, 256, 3, cborutil.SPECIAL_START_ARRAY),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   767
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   768
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   769
    def testnested(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   770
        source = [[], [], [[], [], []]]
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   771
        encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   772
        self.assertEqual(cborutil.decodeall(encoded), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   773
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   774
        source = [True, None, [True, 0, 2], [None], [], [[[]], -87]]
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   775
        encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   776
        self.assertEqual(cborutil.decodeall(encoded), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   777
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   778
        # A set within an array.
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   779
        source = [None, {b'foo', b'bar', None, False}, set()]
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   780
        encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   781
        self.assertEqual(cborutil.decodeall(encoded), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   782
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   783
        # A map within an array.
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   784
        source = [None, {}, {b'foo': b'bar', True: False}, [{}]]
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   785
        encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   786
        self.assertEqual(cborutil.decodeall(encoded), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   787
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   788
    def testindefinitebytestringvalues(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   789
        # Single value array whose value is an empty indefinite bytestring.
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   790
        encoded = b'\x81\x5f\x40\xff'
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   791
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   792
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   793
            cborutil.CBORDecodeError,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   794
            'indefinite length bytestrings not ' 'allowed as array values',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   795
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   796
            cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   797
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   798
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   799
class SetTests(TestCase):
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   800
    def testempty(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   801
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   802
            list(cborutil.streamencode(set())), [b'\xd9\x01\x02', b'\x80',]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   803
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   804
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   805
        self.assertEqual(cborutil.decodeall(b'\xd9\x01\x02\x80'), [set()])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   806
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   807
    def testset(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   808
        source = {b'foo', None, 42}
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   809
        encoded = b''.join(cborutil.streamencode(source))
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   810
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   811
        self.assertEqual(cbor.loads(encoded), source)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   812
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   813
        self.assertEqual(cborutil.decodeall(encoded), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   814
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   815
    def testinvalidtag(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   816
        # Must use array to encode sets.
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   817
        encoded = b'\xd9\x01\x02\xa0'
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   818
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   819
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   820
            cborutil.CBORDecodeError,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   821
            'expected array after finite set ' 'semantic tag',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   822
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   823
            cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   824
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   825
    def testpartialdecode(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   826
        # Semantic tag item will be 3 bytes. Set header will be variable
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   827
        # depending on length.
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   828
        encoded = b''.join(cborutil.streamencode({i for i in range(23)}))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   829
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   830
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   831
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   832
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   833
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   834
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   835
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   836
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   837
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   838
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   839
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   840
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   841
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   842
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   843
            (True, 23, 4, cborutil.SPECIAL_START_SET),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   844
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   845
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   846
            cborutil.decodeitem(encoded[0:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   847
            (True, 23, 4, cborutil.SPECIAL_START_SET),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   848
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   849
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   850
        encoded = b''.join(cborutil.streamencode({i for i in range(24)}))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   851
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   852
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   853
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   854
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   855
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   856
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   857
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   858
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   859
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   860
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   861
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   862
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   863
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   864
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   865
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   866
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   867
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   868
            cborutil.decodeitem(encoded[0:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   869
            (True, 24, 5, cborutil.SPECIAL_START_SET),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   870
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   871
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   872
            cborutil.decodeitem(encoded[0:6]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   873
            (True, 24, 5, cborutil.SPECIAL_START_SET),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   874
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   875
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   876
        encoded = b''.join(cborutil.streamencode({i for i in range(256)}))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   877
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   878
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   879
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   880
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   881
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   882
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   883
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   884
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   885
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   886
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   887
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   888
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   889
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   890
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   891
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   892
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   893
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   894
            cborutil.decodeitem(encoded[0:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   895
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   896
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   897
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   898
            cborutil.decodeitem(encoded[0:6]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   899
            (True, 256, 6, cborutil.SPECIAL_START_SET),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   900
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   901
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   902
    def testinvalidvalue(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   903
        encoded = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   904
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   905
                b'\xd9\x01\x02',  # semantic tag
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   906
                b'\x81',  # array of size 1
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   907
                b'\x5f\x43foo\xff',  # indefinite length bytestring "foo"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   908
            ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   909
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   910
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   911
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   912
            cborutil.CBORDecodeError,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   913
            'indefinite length bytestrings not ' 'allowed as set values',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   914
        ):
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   915
            cborutil.decodeall(encoded)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   916
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   917
        encoded = b''.join([b'\xd9\x01\x02', b'\x81', b'\x80',])  # empty array
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   918
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   919
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   920
            cborutil.CBORDecodeError, 'collections not allowed as set values'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   921
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   922
            cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   923
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   924
        encoded = b''.join([b'\xd9\x01\x02', b'\x81', b'\xa0',])  # empty map
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   925
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   926
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   927
            cborutil.CBORDecodeError, 'collections not allowed as set values'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   928
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   929
            cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   930
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   931
        encoded = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   932
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   933
                b'\xd9\x01\x02',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   934
                b'\x81',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   935
                b'\xd9\x01\x02\x81\x01',  # set with integer 1
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   936
            ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   937
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   938
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   939
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   940
            cborutil.CBORDecodeError, 'collections not allowed as set values'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   941
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   942
            cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   943
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   944
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   945
class BoolTests(TestCase):
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   946
    def testbasic(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   947
        self.assertEqual(list(cborutil.streamencode(True)), [b'\xf5'])
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   948
        self.assertEqual(list(cborutil.streamencode(False)), [b'\xf4'])
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   949
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   950
        self.assertIs(loadit(cborutil.streamencode(True)), True)
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   951
        self.assertIs(loadit(cborutil.streamencode(False)), False)
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   952
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   953
        self.assertEqual(cborutil.decodeall(b'\xf4'), [False])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   954
        self.assertEqual(cborutil.decodeall(b'\xf5'), [True])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   955
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   956
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   957
            cborutil.decodeall(b'\xf4\xf5\xf5\xf4'), [False, True, True, False]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   958
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   959
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   960
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   961
class NoneTests(TestCase):
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   962
    def testbasic(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   963
        self.assertEqual(list(cborutil.streamencode(None)), [b'\xf6'])
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   964
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   965
        self.assertIs(loadit(cborutil.streamencode(None)), None)
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   966
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   967
        self.assertEqual(cborutil.decodeall(b'\xf6'), [None])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   968
        self.assertEqual(cborutil.decodeall(b'\xf6\xf6'), [None, None])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   969
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   970
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   971
class MapTests(TestCase):
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   972
    def testempty(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   973
        self.assertEqual(list(cborutil.streamencode({})), [b'\xa0'])
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   974
        self.assertEqual(loadit(cborutil.streamencode({})), {})
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   975
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   976
        self.assertEqual(cborutil.decodeall(b'\xa0'), [{}])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   977
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   978
    def testemptyindefinite(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   979
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   980
            list(cborutil.streamencodemapfromiter([])), [b'\xbf', b'\xff']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   981
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   982
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   983
        self.assertEqual(loadit(cborutil.streamencodemapfromiter([])), {})
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   984
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   985
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   986
            cborutil.CBORDecodeError, 'indefinite length uint not allowed'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   987
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   988
            cborutil.decodeall(b'\xbf\xff')
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   989
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   990
    def testone(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   991
        source = {b'foo': b'bar'}
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   992
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   993
            list(cborutil.streamencode(source)),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   994
            [b'\xa1', b'\x43', b'foo', b'\x43', b'bar'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
   995
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   996
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   997
        self.assertEqual(loadit(cborutil.streamencode(source)), source)
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   998
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
   999
        self.assertEqual(cborutil.decodeall(b'\xa1\x43foo\x43bar'), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1000
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1001
    def testmultiple(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1002
        source = {
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1003
            b'foo': b'bar',
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1004
            b'baz': b'value1',
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1005
        }
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1006
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1007
        self.assertEqual(loadit(cborutil.streamencode(source)), source)
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1008
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1009
        self.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1010
            loadit(cborutil.streamencodemapfromiter(source.items())), source
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1011
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1012
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1013
        encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1014
        self.assertEqual(cborutil.decodeall(encoded), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1015
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1016
    def testcomplex(self):
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1017
        source = {
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1018
            b'key': 1,
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1019
            2: -10,
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1020
        }
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1021
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1022
        self.assertEqual(loadit(cborutil.streamencode(source)), source)
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1023
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1024
        self.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1025
            loadit(cborutil.streamencodemapfromiter(source.items())), source
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1026
        )
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1027
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1028
        encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1029
        self.assertEqual(cborutil.decodeall(encoded), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1030
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1031
    def testnested(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1032
        source = {b'key1': None, b'key2': {b'sub1': b'sub2'}, b'sub2': {}}
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1033
        encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1034
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1035
        self.assertEqual(cborutil.decodeall(encoded), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1036
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1037
        source = {
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1038
            b'key1': [],
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1039
            b'key2': [None, False],
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1040
            b'key3': {b'foo', b'bar'},
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1041
            b'key4': {},
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1042
        }
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1043
        encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1044
        self.assertEqual(cborutil.decodeall(encoded), [source])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1045
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1046
    def testillegalkey(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1047
        encoded = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1048
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1049
                # map header + len 1
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1050
                b'\xa1',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1051
                # indefinite length bytestring "foo" in key position
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1052
                b'\x5f\x03foo\xff',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1053
            ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1054
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1055
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1056
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1057
            cborutil.CBORDecodeError,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1058
            'indefinite length bytestrings not ' 'allowed as map keys',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1059
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1060
            cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1061
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1062
        encoded = b''.join([b'\xa1', b'\x80', b'\x43foo',])  # empty array
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1063
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1064
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1065
            cborutil.CBORDecodeError, 'collections not supported as map keys'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1066
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1067
            cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1068
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1069
    def testillegalvalue(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1070
        encoded = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1071
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1072
                b'\xa1',  # map headers
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1073
                b'\x43foo',  # key
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1074
                b'\x5f\x03bar\xff',  # indefinite length value
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1075
            ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1076
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1077
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1078
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1079
            cborutil.CBORDecodeError,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1080
            'indefinite length bytestrings not ' 'allowed as map values',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1081
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1082
            cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1083
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1084
    def testpartialdecode(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1085
        source = {b'key1': b'value1'}
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1086
        encoded = b''.join(cborutil.streamencode(source))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1087
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1088
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1089
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1090
            (True, 1, 1, cborutil.SPECIAL_START_MAP),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1091
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1092
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1093
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1094
            (True, 1, 1, cborutil.SPECIAL_START_MAP),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1095
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1096
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1097
        source = {b'key%d' % i: None for i in range(23)}
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1098
        encoded = b''.join(cborutil.streamencode(source))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1099
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1100
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1101
            (True, 23, 1, cborutil.SPECIAL_START_MAP),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1102
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1103
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1104
        source = {b'key%d' % i: None for i in range(24)}
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1105
        encoded = b''.join(cborutil.streamencode(source))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1106
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1107
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1108
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1109
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1110
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1111
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1112
            (True, 24, 2, cborutil.SPECIAL_START_MAP),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1113
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1114
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1115
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1116
            (True, 24, 2, cborutil.SPECIAL_START_MAP),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1117
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1118
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1119
        source = {b'key%d' % i: None for i in range(256)}
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1120
        encoded = b''.join(cborutil.streamencode(source))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1121
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1122
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1123
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1124
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1125
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1126
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1127
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1128
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1129
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1130
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1131
            (True, 256, 3, cborutil.SPECIAL_START_MAP),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1132
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1133
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1134
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1135
            (True, 256, 3, cborutil.SPECIAL_START_MAP),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1136
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1137
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1138
        source = {b'key%d' % i: None for i in range(65536)}
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1139
        encoded = b''.join(cborutil.streamencode(source))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1140
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1141
            cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1142
            (False, None, -4, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1143
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1144
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1145
            cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1146
            (False, None, -3, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1147
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1148
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1149
            cborutil.decodeitem(encoded[0:3]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1150
            (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1151
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1152
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1153
            cborutil.decodeitem(encoded[0:4]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1154
            (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1155
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1156
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1157
            cborutil.decodeitem(encoded[0:5]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1158
            (True, 65536, 5, cborutil.SPECIAL_START_MAP),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1159
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1160
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1161
            cborutil.decodeitem(encoded[0:6]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1162
            (True, 65536, 5, cborutil.SPECIAL_START_MAP),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1163
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1164
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1165
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1166
class SemanticTagTests(TestCase):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1167
    def testdecodeforbidden(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1168
        for i in range(500):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1169
            if i == cborutil.SEMANTIC_TAG_FINITE_SET:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1170
                continue
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1171
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1172
            tag = cborutil.encodelength(cborutil.MAJOR_TYPE_SEMANTIC, i)
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1173
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1174
            encoded = tag + cborutil.encodelength(cborutil.MAJOR_TYPE_UINT, 42)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1175
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1176
            # Partial decode is incomplete.
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1177
            if i < 24:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1178
                pass
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1179
            elif i < 256:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1180
                self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1181
                    cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1182
                    (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1183
                )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1184
            elif i < 65536:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1185
                self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1186
                    cborutil.decodeitem(encoded[0:1]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1187
                    (False, None, -2, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1188
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1189
                self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1190
                    cborutil.decodeitem(encoded[0:2]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1191
                    (False, None, -1, cborutil.SPECIAL_NONE),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1192
                )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1193
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1194
            with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1195
                cborutil.CBORDecodeError, r'semantic tag \d+ not allowed'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1196
            ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1197
                cborutil.decodeitem(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1198
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1199
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1200
class SpecialTypesTests(TestCase):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1201
    def testforbiddentypes(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1202
        for i in range(256):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1203
            if i == cborutil.SUBTYPE_FALSE:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1204
                continue
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1205
            elif i == cborutil.SUBTYPE_TRUE:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1206
                continue
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1207
            elif i == cborutil.SUBTYPE_NULL:
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1208
                continue
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1209
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1210
            encoded = cborutil.encodelength(cborutil.MAJOR_TYPE_SPECIAL, i)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1211
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1212
            with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1213
                cborutil.CBORDecodeError, r'special type \d+ not allowed'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1214
            ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1215
                cborutil.decodeitem(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1216
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1217
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1218
class SansIODecoderTests(TestCase):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1219
    def testemptyinput(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1220
        decoder = cborutil.sansiodecoder()
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1221
        self.assertEqual(decoder.decode(b''), (False, 0, 0))
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1222
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1223
39413
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1224
class BufferingDecoderTests(TestCase):
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1225
    def testsimple(self):
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1226
        source = [
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1227
            b'foobar',
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1228
            b'x' * 128,
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1229
            {b'foo': b'bar'},
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1230
            True,
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1231
            False,
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1232
            None,
43996
6dbb18e1ac8d tests: avoid using a list comprehension to fill a list with fixed values
Matt Harbison <matt_harbison@yahoo.com>
parents: 43076
diff changeset
  1233
            [None] * 128,
39413
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1234
        ]
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1235
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1236
        encoded = b''.join(cborutil.streamencode(source))
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1237
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1238
        for step in range(1, 32):
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1239
            decoder = cborutil.bufferingdecoder()
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1240
            start = 0
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1241
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1242
            while start < len(encoded):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1243
                decoder.decode(encoded[start : start + step])
39413
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1244
                start += step
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1245
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1246
            self.assertEqual(decoder.getavailable(), [source])
babad5ebaf0a cborutil: add a buffering decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39412
diff changeset
  1247
40124
b638219a23c3 cborutil: cast bytearray to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39413
diff changeset
  1248
    def testbytearray(self):
b638219a23c3 cborutil: cast bytearray to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39413
diff changeset
  1249
        source = b''.join(cborutil.streamencode(b'foobar'))
b638219a23c3 cborutil: cast bytearray to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39413
diff changeset
  1250
b638219a23c3 cborutil: cast bytearray to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39413
diff changeset
  1251
        decoder = cborutil.bufferingdecoder()
b638219a23c3 cborutil: cast bytearray to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39413
diff changeset
  1252
        decoder.decode(bytearray(source))
b638219a23c3 cborutil: cast bytearray to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39413
diff changeset
  1253
b638219a23c3 cborutil: cast bytearray to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39413
diff changeset
  1254
        self.assertEqual(decoder.getavailable(), [b'foobar'])
b638219a23c3 cborutil: cast bytearray to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39413
diff changeset
  1255
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1256
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1257
class DecodeallTests(TestCase):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1258
    def testemptyinput(self):
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1259
        self.assertEqual(cborutil.decodeall(b''), [])
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1260
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1261
    def testpartialinput(self):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1262
        encoded = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1263
            [b'\x82', b'\x01',]  # array of 2 elements  # integer 1
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1264
        )
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1265
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1266
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1267
            cborutil.CBORDecodeError, 'input data not complete'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1268
        ):
39411
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1269
            cborutil.decodeall(encoded)
aeb551a3bb8a cborutil: implement sans I/O decoder
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37899
diff changeset
  1270
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1271
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1272
if __name__ == '__main__':
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1273
    import silenttestrunner
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41549
diff changeset
  1274
37711
65a23cc8e75b cborutil: implement support for streaming encoding, bytestring decoding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
  1275
    silenttestrunner.main(__name__)