data: data/cbor (RFC 8949: indefinite lengths, half floats, tags 0/1 dates, bignums, self-describe, tagged / simple / undefined values, sequences); RFC appendix A + cbor2 fixtures, byte-exact re-encode
This commit is contained in:
1 parent
4f44e5ebe3
commit
2ef071cb16
6 files changed
+952
No files matched your search
@@ -0,0 +1,108 @@
|
||||
# data/cbor fixtures: RFC 8949 Appendix A examples and random values encoded by Python's cbor2 (through nix): every
|
||||
# integer width (and bignums), floats, text / byte strings, nested arrays / maps, dates (tags 0 / 1), other tags,
|
||||
# simple values, undefined, indefinite-length items (written by hand), with the canonical form of cbor2's decode.
|
||||
# tests/gen/cbor.luau { appendix = { { hex, data, canon } }, safe = { { data, canon } }, wide = { { data, canon,
|
||||
# canonStr } } } safe: our decode + encode gives the same bytes; wide: big integers (doubles / exact text)
|
||||
import importlib.util, os, pickle
|
||||
|
||||
_spec = importlib.util.spec_from_file_location("_data_common", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_data_common.py"))
|
||||
C = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(C)
|
||||
|
||||
APPENDIX = """00 01 0a 17 1818 1819 1864 1903e8 1a000f4240 1b000000e8d4a51000 1bffffffffffffffff c249010000000000000000
|
||||
3bffffffffffffffff c349010000000000000000 20 29 3863 3903e7 f90000 f98000 f93c00 fb3ff199999999999a f93e00 f97bff
|
||||
fa47c35000 fa7f7fffff fb7e37e43c8800759c f90001 f90400 f9c400 fbc010666666666666 f97c00 f97e00 f9fc00 fa7f800000
|
||||
fa7fc00000 faff800000 fb7ff0000000000000 fb7ff8000000000000 fbfff0000000000000 f4 f5 f6 f7 f0 f818 f8ff
|
||||
c074323031332d30332d32315432303a30343a30305a c11a514b67b0 c1fb41d452d9ec200000 d74401020304
|
||||
d818456449455446 d82076687474703a2f2f7777772e6578616d706c652e636f6d 40 4401020304 60 6161 6449455446 62225c
|
||||
62c3bc 63e6b0b4 64f0908591 80 83010203 8301820203820405 98190102030405060708090a0b0c0d0e0f101112131415161718181819
|
||||
a0 a201020304 a26161016162820203 826161a161626163 a56161614161626142616361436164614461656145 5f42010243030405ff
|
||||
7f657374726561646d696e67ff 9fff 9f018202039f0405ffff 9f01820203820405ff 83018202039f0405ff 83019f0203ff820405
|
||||
9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff bf61610161629f0203ffff 826161bf61626163ff
|
||||
bf6346756ef563416d7421ff"""
|
||||
|
||||
BODY = r'''
|
||||
import cbor2, pickle, random, sys, datetime
|
||||
from collections.abc import Mapping
|
||||
HEXES = sys.argv[1:] if False else HEXLIST
|
||||
def conv(v):
|
||||
if v is cbor2.undefined:
|
||||
return ["undefined"]
|
||||
if isinstance(v, cbor2.CBORSimpleValue):
|
||||
return ["simple", v.value]
|
||||
if isinstance(v, cbor2.CBORTag):
|
||||
return ["tag", v.tag, conv(v.value)]
|
||||
if isinstance(v, (list, tuple)):
|
||||
return [conv(x) for x in v]
|
||||
if isinstance(v, Mapping) or hasattr(v, "items"):
|
||||
return {(tuple(conv(k)) if isinstance(k, list) else k): conv(x) for k, x in v.items()}
|
||||
return v
|
||||
def cstr(v):
|
||||
if isinstance(v, cbor2.CBORTag):
|
||||
return cbor2.CBORTag(v.tag, cstr(v.value))
|
||||
if isinstance(v, int) and not isinstance(v, bool) and abs(v) >= 2**53:
|
||||
return str(v)
|
||||
if isinstance(v, (list, tuple)):
|
||||
return [cstr(x) for x in v]
|
||||
if isinstance(v, Mapping) or hasattr(v, "items"):
|
||||
return {k: cstr(x) for k, x in v.items()}
|
||||
return v
|
||||
out = {"appendix": [], "safe": [], "wide": []}
|
||||
for h in HEXES:
|
||||
data = bytes.fromhex(h)
|
||||
try:
|
||||
v = cbor2.loads(data)
|
||||
except Exception:
|
||||
out["appendix"].append({"hex": h, "data": data, "error": True}) # e.g. f818: invalid since RFC 8949
|
||||
continue
|
||||
if type(v).__name__ in ("Decimal", "Fraction", "UUID") or (isinstance(v, str) and h.startswith("d820")):
|
||||
continue # semantic tags cbor2 interprets and we keep tagged
|
||||
out["appendix"].append({"hex": h, "data": data, "canon": canon(conv(v)), "canonStr": canon(conv(cstr(v)))})
|
||||
rng = random.Random(9)
|
||||
def rint(wide):
|
||||
lim = rng.choice([23, 24, 255, 256, 65535, 65536, 2**32 - 1, 2**32, 2**53 - 1])
|
||||
if wide and rng.random() < 0.4:
|
||||
lim = rng.choice([2**64 - 1, 2**70])
|
||||
v = rng.randrange(0, lim + 1)
|
||||
return -v - 1 if rng.random() < 0.5 else v
|
||||
def rstr():
|
||||
return "".join(rng.choice("abé中\U0001f600 \n\x00") for _ in range(rng.choice([0, 1, 23, 24, 255, 300])))
|
||||
def rval(d, wide):
|
||||
r = rng.random()
|
||||
if d > 3 or r < 0.5:
|
||||
k = rng.randrange(10)
|
||||
if k == 0: return None
|
||||
if k == 1: return rng.random() < 0.5
|
||||
if k in (2, 3): return rint(wide)
|
||||
if k == 4:
|
||||
f = rng.uniform(-1e6, 1e6) * 10 ** rng.randrange(-5, 5)
|
||||
return f if not f.is_integer() else f + 0.5
|
||||
if k == 5: return rstr()
|
||||
if k == 6: return rng.randbytes(rng.choice([0, 3, 24, 256]))
|
||||
if k == 7:
|
||||
return datetime.datetime.fromtimestamp(rng.randrange(0, 2**33), datetime.timezone.utc)
|
||||
if k == 8:
|
||||
return cbor2.CBORTag(rng.choice([6, 1000, 100000]), rval(d + 1, wide))
|
||||
return rng.choice([cbor2.undefined, cbor2.CBORSimpleValue(rng.choice([0, 19, 32, 255]))])
|
||||
if r < 0.75:
|
||||
return [rval(d + 1, wide) for _ in range(rng.choice([0, 1, 3, 23, 24] if d < 2 else [0, 1, 3]))]
|
||||
m = {}
|
||||
for _ in range(rng.choice([0, 1, 3, 23, 24] if d < 2 else [0, 1, 3])):
|
||||
key = rint(False) if rng.random() < 0.3 else "".join(rng.choice("abcé") for _ in range(rng.randrange(6)))
|
||||
m[key] = rval(d + 1, wide)
|
||||
return m
|
||||
for i in range(250):
|
||||
data = cbor2.dumps(rval(0, False))
|
||||
out["safe"].append({"data": data, "canon": canon(conv(cbor2.loads(data)))})
|
||||
for i in range(120):
|
||||
data = cbor2.dumps(rval(0, True))
|
||||
v = cbor2.loads(data)
|
||||
out["wide"].append({"data": data, "canon": canon(conv(v)), "canonStr": canon(conv(cstr(v)))})
|
||||
sys.stdout.buffer.write(pickle.dumps(out, protocol=4))
|
||||
'''
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
hexes = APPENDIX.split()
|
||||
out = pickle.loads(C.python(ctx, ["cbor2"], "HEXLIST = %r\n" % hexes + BODY))
|
||||
ctx.emit("cbor", out)
|
||||
Reference in new issue
Block a user