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