def test_datetime(self): if not self.testable(): return # right now we're just testing that it's possible to dumps() # Tag(0,...) because there was a bug around that. xb = self.dumps( Tag(0, datetime.datetime(1984, 1, 24, 23, 22, 21).isoformat()))
def _randTag(randob=_randob): t = Tag() # Tags 0..36 are know standard things we might implement special # decoding for. This number will grow over time, and this test # need to be adjusted to only assign unclaimed tags for Tag<->Tag # encode-decode testing. t.tag = random.randint(37, 1000000) t.value = randob() return t
class XTestCBOR(object): def _oso(self, ob): ser = self.dumps(ob) try: o2 = self.loads(ser) assert ob == o2, '%r != %r from %s' % (ob, o2, base64.b16encode(ser)) except Exception as e: sys.stderr.write( 'failure on buf len={0} {1!r} ob={2!r} {3!r}; {4}\n'.format( len(ser), hexstr(ser), ob, ser, e)) raise def _osos(self, ob): obs = self.dumps(ob) o2 = self.loads(obs) o2s = self.dumps(o2) assert obs == o2s def _oso_bytearray(self, ob): ser = self.dumps(ob) try: o2 = self.loads(bytearray(ser)) assert ob == o2, '%r != %r from %s' % (ob, o2, base64.b16encode(ser)) except Exception as e: sys.stderr.write( 'failure on buf len={0} {1!r} ob={2!r} {3!r}; {4}\n'.format( len(ser), hexstr(ser), ob, ser, e)) raise test_objects = [ 1, 0, True, False, None, -1, -1.5, 1.5, 1000, -1000, 1000000000, 2376030000, -1000000000, 1000000000000000, -1000000000000000, [], [1, 2, 3], {}, b'aoeu1234\x00\xff', u'åöéûのかめ亀', b'', u'', Tag(1234, 'aoeu'), ] def test_basic(self): if not self.testable(): return for ob in self.test_objects: self._oso(ob) def test_basic_bytearray(self): if not self.testable(): return xoso = self._oso self._oso = self._oso_bytearray try: self.test_basic() finally: self._oso = xoso def test_random_ints(self): if not self.testable(): return icount = self.speediterations() for i in _range(icount): v = random.randint(-4294967295, 0xffffffff) self._oso(v) oldv = [] for i in _range(int(icount / 10)): v = random.randint(-1000000000000000000000, 1000000000000000000000) self._oso(v) oldv.append(v) def test_randobs(self): if not self.testable(): return icount = self.speediterations() for i in _range(icount): ob = _randob() self._oso(ob) def test_tuple(self): if not self.testable(): return l = [1, 2, 3] t = tuple(l) ser = self.dumps(t) o2 = self.loads(ser) assert l == o2 def test_speed_vs_json(self): if not self.testable(): return # It should be noted that the python standard library has a C implementation of key parts of json encoding and decoding icount = self.speediterations() obs = [_randob_notag() for x in _range(icount)] st = time.time() bsers = [self.dumps(o) for o in obs] nt = time.time() cbor_ser_time = nt - st jsers = [json.dumps(o) for o in obs] jt = time.time() json_ser_time = jt - nt cbor_byte_count = sum(map(len, bsers)) json_byte_count = sum(map(len, jsers)) sys.stderr.write( 'serialized {nobs} objects into {cb} cbor bytes in {ct:.2f} seconds ({cops:.2f}/s, {cbps:.1f}B/s) and {jb} json bytes in {jt:.2f} seconds ({jops:.2f}/s, {jbps:.1f}B/s)\n' .format(nobs=len(obs), cb=cbor_byte_count, ct=cbor_ser_time, cops=len(obs) / cbor_ser_time, cbps=cbor_byte_count / cbor_ser_time, jb=json_byte_count, jt=json_ser_time, jops=len(obs) / json_ser_time, jbps=json_byte_count / json_ser_time)) bsersz = zlib.compress(b''.join(bsers)) jsersz = zlib.compress(_join_jsers(jsers)) sys.stderr.write( 'compress to {0} bytes cbor.gz and {1} bytes json.gz\n'.format( len(bsersz), len(jsersz))) st = time.time() bo2 = [self.loads(b) for b in bsers] bt = time.time() cbor_load_time = bt - st jo2 = [json.loads(b) for b in jsers] jt = time.time() json_load_time = jt - bt sys.stderr.write( 'load {nobs} objects from cbor in {ct:.2f} secs ({cops:.2f}/sec, {cbps:.1f}B/s) and json in {jt:.2f} ({jops:.2f}/sec, {jbps:.1f}B/s)\n' .format(nobs=len(obs), ct=cbor_load_time, cops=len(obs) / cbor_load_time, cbps=cbor_byte_count / cbor_load_time, jt=json_load_time, jops=len(obs) / json_load_time, jbps=json_byte_count / json_load_time)) def test_loads_none(self): if not self.testable(): return try: ob = self.loads(None) assert False, "expected ValueError when passing in None" except ValueError: pass def test_concat(self): "Test that we can concatenate output and retrieve the objects back out." if not self.testable(): return self._oso(self.test_objects) fob = StringIO() for ob in self.test_objects: self.dump(ob, fob) fob.seek(0) obs2 = [] try: while True: obs2.append(self.load(fob)) except EOFError: pass assert obs2 == self.test_objects # TODO: find more bad strings with which to fuzz CBOR def test_badread(self): if not self.testable(): return try: ob = self.loads(b'\xff') assert False, 'badread should have failed' except ValueError as ve: #logger.info('error', exc_info=True) pass except Exception as ex: logger.info('unexpected error!', exc_info=True) assert False, 'unexpected error' + str(ex)
class XTestCBOR(object): def _oso(self, ob): ser = self.dumps(ob) try: o2 = self.loads(ser) assert ob == o2, '%r != %r from %s' % (ob, o2, base64.b16encode(ser)) except Exception as e: sys.stderr.write( 'failure on buf len={0} {1!r} ob={2!r} {3!r}; {4}\n'.format( len(ser), hexstr(ser), ob, ser, e)) raise def _osos(self, ob): obs = self.dumps(ob) o2 = self.loads(obs) o2s = self.dumps(o2) assert obs == o2s def _oso_bytearray(self, ob): ser = self.dumps(ob) try: o2 = self.loads(bytearray(ser)) assert ob == o2, '%r != %r from %s' % (ob, o2, base64.b16encode(ser)) except Exception as e: sys.stderr.write( 'failure on buf len={0} {1!r} ob={2!r} {3!r}; {4}\n'.format( len(ser), hexstr(ser), ob, ser, e)) raise test_objects = [ 1, 0, True, False, None, -1, -1.5, 1.5, 1000, -1000, 1000000000, 2376030000, -1000000000, 1000000000000000, -1000000000000000, [], [1, 2, 3], {}, b'aoeu1234\x00\xff', u'åöéûのかめ亀', b'', u'', Tag(1234, 'aoeu'), ] def test_basic(self): if not self.testable(): return for ob in self.test_objects: self._oso(ob) def test_basic_bytearray(self): if not self.testable(): return xoso = self._oso self._oso = self._oso_bytearray try: self.test_basic() finally: self._oso = xoso def test_random_ints(self): if not self.testable(): return icount = self.speediterations() for i in _range(icount): v = random.randint(-4294967295, 0xffffffff) self._oso(v) oldv = [] for i in _range(int(icount / 10)): v = random.randint(-1000000000000000000000, 1000000000000000000000) self._oso(v) oldv.append(v) def test_randobs(self): if not self.testable(): return icount = self.speediterations() for i in _range(icount): ob = _randob() self._oso(ob) def test_tuple(self): if not self.testable(): return l = [1, 2, 3] t = tuple(l) ser = self.dumps(t) o2 = self.loads(ser) assert l == o2 def test_speed_vs_json(self): if not self.testable(): return # It should be noted that the python standard library has a C implementation of key parts of json encoding and decoding icount = self.speediterations() obs = [_randob_notag() for x in _range(icount)] st = time.time() bsers = [self.dumps(o) for o in obs] nt = time.time() cbor_ser_time = nt - st jsers = [json.dumps(o) for o in obs] jt = time.time() json_ser_time = jt - nt cbor_byte_count = sum(map(len, bsers)) json_byte_count = sum(map(len, jsers)) sys.stderr.write( 'serialized {nobs} objects into {cb} cbor bytes in {ct:.2f} seconds ({cops:.2f}/s, {cbps:.1f}B/s) and {jb} json bytes in {jt:.2f} seconds ({jops:.2f}/s, {jbps:.1f}B/s)\n' .format(nobs=len(obs), cb=cbor_byte_count, ct=cbor_ser_time, cops=len(obs) / cbor_ser_time, cbps=cbor_byte_count / cbor_ser_time, jb=json_byte_count, jt=json_ser_time, jops=len(obs) / json_ser_time, jbps=json_byte_count / json_ser_time)) bsersz = zlib.compress(b''.join(bsers)) jsersz = zlib.compress(_join_jsers(jsers)) sys.stderr.write( 'compress to {0} bytes cbor.gz and {1} bytes json.gz\n'.format( len(bsersz), len(jsersz))) st = time.time() bo2 = [self.loads(b) for b in bsers] bt = time.time() cbor_load_time = bt - st jo2 = [json.loads(b) for b in jsers] jt = time.time() json_load_time = jt - bt sys.stderr.write( 'load {nobs} objects from cbor in {ct:.2f} secs ({cops:.2f}/sec, {cbps:.1f}B/s) and json in {jt:.2f} ({jops:.2f}/sec, {jbps:.1f}B/s)\n' .format(nobs=len(obs), ct=cbor_load_time, cops=len(obs) / cbor_load_time, cbps=cbor_byte_count / cbor_load_time, jt=json_load_time, jops=len(obs) / json_load_time, jbps=json_byte_count / json_load_time)) def test_loads_none(self): if not self.testable(): return try: ob = self.loads(None) assert False, "expected ValueError when passing in None" except ValueError: pass def test_concat(self): "Test that we can concatenate output and retrieve the objects back out." if not self.testable(): return self._oso(self.test_objects) fob = StringIO() for ob in self.test_objects: self.dump(ob, fob) fob.seek(0) obs2 = [] try: while True: obs2.append(self.load(fob)) except EOFError: pass assert obs2 == self.test_objects # TODO: find more bad strings with which to fuzz CBOR def test_badread(self): if not self.testable(): return try: ob = self.loads(b'\xff') assert False, 'badread should have failed' except ValueError as ve: #logger.info('error', exc_info=True) pass except Exception as ex: logger.info('unexpected error!', exc_info=True) assert False, 'unexpected error' + str(ex) def test_datetime(self): if not self.testable(): return # right now we're just testing that it's possible to dumps() # Tag(0,...) because there was a bug around that. xb = self.dumps( Tag(0, datetime.datetime(1984, 1, 24, 23, 22, 21).isoformat())) def test_sortkeys(self): if not self.testable(): return obytes = [] xbytes = [] for n in _range(2, 27): ob = {u'{:02x}'.format(x): x for x in _range(n)} # ensure some "ob" have unsorted key:value entries if n in [4, 6, 9]: ob.pop('01') ob["01"] = 1 obytes.append(self.dumps(ob, sort_keys=True)) xbytes.append(self.dumps(ob, sort_keys=False)) allOGood = True someXMiss = False for i, g in enumerate(_GOLDEN_SORTED_KEYS_BYTES): if g != obytes[i]: logger.error('bad sorted result, wanted %r got %r', g, obytes[i]) allOGood = False if g != xbytes[i]: someXMiss = True assert allOGood assert someXMiss
class XTestCBOR(object): def _oso(self, ob): ser = dumps(ob) try: o2 = loads(ser) assert ob == o2, '%r != %r from %s' % (ob, o2, ubinascii.hexlify(ser)) except Exception as e: print('failure on buf len={0} {1!r} ob={2!r} {3!r}; {4}\n'.format( len(ser), hexstr(ser), ob, ser, e)) raise def _osos(self, ob): obs = dumps(ob) o2 = loads(obs) o2s = dumps(o2) assert obs == o2s def _oso_bytearray(self, ob): ser = dumps(ob) try: o2 = loads(bytearray(ser)) assert ob == o2, '%r != %r from %s' % (ob, o2, ubinascii.hexlify(ser)) except Exception as e: print('failure on buf len={0} {1!r} ob={2!r} {3!r}; {4}\n'.format( len(ser), hexstr(ser), ob, ser, e)) raise test_objects = [ 1, 0, True, False, None, -1, -1.5, 1.5, 1000, -1000, 1000000000, 2376030000, -1000000000, 1000000000000000, -1000000000000000, [], [1, 2, 3], {}, b'aoeu1234\x00\xff', u'åöéûのかめ亀', b'', u'', Tag(1234, 'aoeu'), ] def test_basic(self): for ob in self.test_objects: self._oso(ob) def test_basic_bytearray(self): xoso = self._oso self._oso = self._oso_bytearray try: self.test_basic() finally: self._oso = xoso def test_random_ints(self): #v = random.randint(-4294967295, 0xffffffff) v = 4009372298 self._oso(v) #v = random.randint(-1000000000000000000000, 1000000000000000000000) v = -3875820019684212735 self._oso(v) def test_tuple(self): l = [1, 2, 3] t = tuple(l) ser = dumps(t) o2 = loads(ser) assert l == o2 def test_loads_none(self): try: loads(None) assert False, "expected ValueError when passing in None" except ValueError: pass def test_concat(self): "Test that we can concatenate output and retrieve the objects back out." self._oso(self.test_objects) fob = StringIO() for ob in self.test_objects: dump(ob, fob) fob.seek(0) obs2 = [] try: while True: obs2.append(load(fob)) except EOFError: pass assert obs2 == self.test_objects # TODO: find more bad strings with which to fuzz CBOR def test_badread(self): try: loads(b'\xff') assert False, 'badread should have failed' except ValueError as ve: #logger.info('error', exc_info=True) pass except Exception as ex: print('unexpected error!', exc_info=True) assert False, 'unexpected error' + str(ex) def test_datetime(self): # right now we're just testing that it's possible to dumps() # Tag(0,...) because there was a bug around that. dumps(Tag(0, '1984-01-24T23:22:21')) def test_sortkeys(self): obytes = [] xbytes = [] for n in _range(2, 27): ob = {u'{:02x}'.format(x): x for x in _range(n)} obytes.append(dumps(ob, sort_keys=True)) xbytes.append(dumps(ob, sort_keys=False)) allOGood = True someXMiss = False for i, g in enumerate(_GOLDEN_SORTED_KEYS_BYTES): if g != obytes[i]: print('bad sorted result, wanted %r got %r', g, obytes[i]) allOGood = False if g != xbytes[i]: someXMiss = True assert allOGood assert someXMiss
def test_datetime(self): # right now we're just testing that it's possible to dumps() # Tag(0,...) because there was a bug around that. dumps(Tag(0, '1984-01-24T23:22:21'))