2025-05-22 16:39:37 +05:30
|
|
|
|
# This file is a modified version of Emery Hemingway’s CBOR library for Nim,
|
|
|
|
|
|
# originally available at https://github.com/ehmry/cbor-nim and released under The Unlicense.
|
|
|
|
|
|
|
|
|
|
|
|
import std/[base64, os, random, times, json, unittest]
|
2025-05-21 03:22:35 +05:30
|
|
|
|
import pkg/serde/cbor
|
|
|
|
|
|
import pkg/questionable
|
|
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
|
2025-05-31 17:38:35 +05:30
|
|
|
|
proc findVectorsFile(): string =
|
2025-05-21 03:22:35 +05:30
|
|
|
|
var parent = getCurrentDir()
|
|
|
|
|
|
while parent != "/":
|
|
|
|
|
|
result = parent / "tests" / "cbor" / "test_vector.json"
|
2025-05-31 17:38:35 +05:30
|
|
|
|
if fileExists result:
|
|
|
|
|
|
return
|
2025-05-21 03:22:35 +05:30
|
|
|
|
parent = parent.parentDir
|
|
|
|
|
|
raiseAssert "Could not find test vectors"
|
|
|
|
|
|
|
|
|
|
|
|
let js = findVectorsFile().readFile.parseJson()
|
|
|
|
|
|
|
|
|
|
|
|
suite "decode":
|
|
|
|
|
|
for v in js.items:
|
|
|
|
|
|
if v.hasKey "decoded":
|
|
|
|
|
|
let
|
|
|
|
|
|
control = $v["decoded"]
|
|
|
|
|
|
name = v["name"].getStr
|
|
|
|
|
|
test name:
|
2025-05-31 17:38:35 +05:30
|
|
|
|
let controlCbor = base64.decode v["cbor"].getStr
|
|
|
|
|
|
let js = parseCbor(controlCbor).toJson()
|
2025-05-21 03:22:35 +05:30
|
|
|
|
if js.isNil:
|
|
|
|
|
|
fail()
|
|
|
|
|
|
else:
|
|
|
|
|
|
check(control == $js)
|
|
|
|
|
|
|
|
|
|
|
|
suite "diagnostic":
|
|
|
|
|
|
for v in js.items:
|
|
|
|
|
|
if v.hasKey "diagnostic":
|
|
|
|
|
|
let
|
|
|
|
|
|
control = v["diagnostic"].getStr
|
|
|
|
|
|
name = v["name"].getStr
|
|
|
|
|
|
test name:
|
2025-05-31 17:38:35 +05:30
|
|
|
|
let controlCbor = base64.decode v["cbor"].getStr
|
|
|
|
|
|
check($parseCbor(controlCbor) == control)
|
2025-05-21 03:22:35 +05:30
|
|
|
|
|
|
|
|
|
|
suite "roundtrip":
|
|
|
|
|
|
for v in js.items:
|
|
|
|
|
|
if v["roundtrip"].getBool:
|
|
|
|
|
|
let
|
|
|
|
|
|
controlB64 = v["cbor"].getStr
|
|
|
|
|
|
controlCbor = base64.decode controlB64
|
|
|
|
|
|
name = v["name"].getStr
|
|
|
|
|
|
test name:
|
2025-05-31 17:38:35 +05:30
|
|
|
|
without testCbor =? encode(parseCbor(controlCbor)), error:
|
2025-05-22 15:39:43 +05:30
|
|
|
|
fail()
|
2025-05-21 03:22:35 +05:30
|
|
|
|
if controlCbor != testCbor:
|
|
|
|
|
|
let testB64 = base64.encode(testCbor)
|
|
|
|
|
|
check(controlB64 == testB64)
|
|
|
|
|
|
|
|
|
|
|
|
suite "hooks":
|
|
|
|
|
|
test "DateTime":
|
|
|
|
|
|
let dt = now()
|
2025-05-22 15:39:43 +05:30
|
|
|
|
|
|
|
|
|
|
without bin =? encode(dt), error:
|
|
|
|
|
|
fail()
|
2025-05-31 17:38:35 +05:30
|
|
|
|
check(parseCbor(bin).text == $dt)
|
2025-05-21 03:22:35 +05:30
|
|
|
|
test "Time":
|
|
|
|
|
|
let t = now().toTime
|
2025-05-31 17:38:35 +05:30
|
|
|
|
var bin = encode(t).tryValue
|
|
|
|
|
|
check(parseCbor(bin).getInt == t.toUnix)
|
2025-05-21 03:22:35 +05:30
|
|
|
|
|
|
|
|
|
|
test "tag":
|
2025-05-22 15:39:43 +05:30
|
|
|
|
var c = toCbor("foo").tryValue
|
2025-05-21 03:22:35 +05:30
|
|
|
|
c.tag = some(99'u64)
|
|
|
|
|
|
check c.tag == some(99'u64)
|
|
|
|
|
|
|
|
|
|
|
|
test "sorting":
|
|
|
|
|
|
var map = initCborMap()
|
2025-05-31 17:38:35 +05:30
|
|
|
|
var keys =
|
|
|
|
|
|
@[
|
2025-05-22 15:39:43 +05:30
|
|
|
|
toCbor(10).tryValue,
|
|
|
|
|
|
toCbor(100).tryValue,
|
|
|
|
|
|
toCbor(-1).tryValue,
|
|
|
|
|
|
toCbor("z").tryValue,
|
|
|
|
|
|
toCbor("aa").tryValue,
|
|
|
|
|
|
toCbor([toCbor(100).tryValue]).tryValue,
|
|
|
|
|
|
toCbor([toCbor(-1).tryValue]).tryValue,
|
|
|
|
|
|
toCbor(false).tryValue,
|
2025-05-21 03:22:35 +05:30
|
|
|
|
]
|
|
|
|
|
|
shuffle(keys)
|
2025-05-22 15:39:43 +05:30
|
|
|
|
|
2025-05-31 17:38:35 +05:30
|
|
|
|
for k in keys:
|
|
|
|
|
|
map[k] = toCbor(0).tryValue
|
2025-05-22 15:39:43 +05:30
|
|
|
|
check not map.isSorted.tryValue
|
|
|
|
|
|
check sort(map).isSuccess
|
|
|
|
|
|
check map.isSorted.tryValue
|
2025-05-21 03:41:35 +05:30
|
|
|
|
|
|
|
|
|
|
test "invalid wire type":
|
|
|
|
|
|
let node = CborNode(kind: cborText, text: "not an int")
|
2025-05-30 15:24:39 +05:30
|
|
|
|
let result = int.fromCbor(node)
|
2025-05-21 03:41:35 +05:30
|
|
|
|
|
|
|
|
|
|
check result.isFailure
|
2025-05-31 17:38:35 +05:30
|
|
|
|
check $result.error.msg ==
|
|
|
|
|
|
"deserialization to int failed: expected {cborUnsigned, cborNegative} but got cborText"
|