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