def test_vectors(self): here = os.path.dirname(__file__) jf = os.path.abspath( os.path.join(here, '../../../test-vectors/appendix_a.json')) if not os.path.exists(jf): logging.warning( 'cannot find test-vectors/appendix_a.json, tried: %r', jf) return if _IS_PY3: testfile = open(jf, 'r') tv = json.load(testfile) else: testfile = open(jf, 'rb') tv = json.load(testfile) anyerr = False for row in tv: rhex = row.get('hex') if 'decoded' in row: decoded = row['decoded'] _check(row, decoded) continue elif 'diagnostic' in row: diag = row['diagnostic'] checkf = _DIAGNOSTIC_TESTS.get(diag) if checkf is not None: _check_foo(row, checkf) continue # variously verbose log of what we're not testing: cbdata = base64.b64decode(row['cbor']) try: pd = pyloads(cbdata) except: if rhex and (rhex in _EXPECT_EXCEPTION): pass else: logging.error('failed to py load hex=%s diag=%r', rhex, row.get('diagnostic'), exc_info=True) pd = '' cd = None if cloads is not None: try: cd = cloads(cbdata) except: if rhex and (rhex in _EXPECT_EXCEPTION): pass else: logging.error('failed to c load hex=%s diag=%r', rhex, row.get('diagnostic'), exc_info=True) cd = '' logging.warning('skipping hex=%s diag=%r py=%s c=%s', rhex, row.get('diagnostic'), pd, cd) testfile.close() assert not anyerr
def _check_foo(row, checkf): cbdata = base64.b64decode(row['cbor']) if cloads is not None: cb = cloads(cbdata) if not checkf(cb): anyerr = True sys.stderr.write('expected {0!r} got {1!r} c failed to decode cbor {2}\n'.format(decoded, cb, base64.b16encode(cbdata))) cb = pyloads(cbdata) if not checkf(cb): anyerr = True sys.stderr.write('expected {0!r} got {1!r} py failed to decode cbor {2}\n'.format(decoded, cb, base64.b16encode(cbdata)))
def test_vectors(self): here = os.path.dirname(__file__) jf = os.path.abspath(os.path.join(here, '../../../test-vectors/appendix_a.json')) if not os.path.exists(jf): logging.warning('cannot find test-vectors/appendix_a.json, tried: %r', jf) return if _IS_PY3: testfile = open(jf, 'r') tv = json.load(testfile) else: testfile = open(jf, 'rb') tv = json.load(testfile) anyerr = False for row in tv: rhex = row.get('hex') if 'decoded' in row: decoded = row['decoded'] _check(row, decoded) continue elif 'diagnostic' in row: diag = row['diagnostic'] checkf = _DIAGNOSTIC_TESTS.get(diag) if checkf is not None: _check_foo(row, checkf) continue # variously verbose log of what we're not testing: cbdata = base64.b64decode(row['cbor']) try: pd = pyloads(cbdata) except: if rhex and (rhex in _EXPECT_EXCEPTION): pass else: logging.error('failed to py load hex=%s diag=%r', rhex, row.get('diagnostic'), exc_info=True) pd = '' cd = None if cloads is not None: try: cd = cloads(cbdata) except: if rhex and (rhex in _EXPECT_EXCEPTION): pass else: logging.error('failed to c load hex=%s diag=%r', rhex, row.get('diagnostic'), exc_info=True) cd = '' logging.warning('skipping hex=%s diag=%r py=%s c=%s', rhex, row.get('diagnostic'), pd, cd) testfile.close() assert not anyerr
def _check_foo(row, checkf): cbdata = base64.b64decode(row['cbor']) if cloads is not None: cb = cloads(cbdata) if not checkf(cb): anyerr = True sys.stderr.write( 'expected {0!r} got {1!r} c failed to decode cbor {2}\n'. format(decoded, cb, base64.b16encode(cbdata))) cb = pyloads(cbdata) if not checkf(cb): anyerr = True sys.stderr.write( 'expected {0!r} got {1!r} py failed to decode cbor {2}\n'.format( decoded, cb, base64.b16encode(cbdata)))