2018-01-25 19:22:13 +00:00
|
|
|
import
|
|
|
|
unittest, strutils,
|
|
|
|
rlp, util/json_testing
|
|
|
|
|
|
|
|
proc q(s: string): string = "\"" & s & "\""
|
|
|
|
proc i(s: string): string = s.replace(" ").replace("\n")
|
|
|
|
proc inspectMatch(r: Rlp, s: string): bool = r.inspect.i == s.i
|
|
|
|
|
|
|
|
test "empty bytes are not a proper RLP":
|
2018-02-16 15:28:19 +00:00
|
|
|
var rlp = rlpFromBytes Bytes(@[]).toRange
|
2018-01-25 19:22:13 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
not rlp.hasData
|
|
|
|
not rlp.isBlob
|
|
|
|
not rlp.isList
|
|
|
|
not rlp.isEmpty
|
|
|
|
|
2018-06-23 16:49:04 +00:00
|
|
|
expect Exception:
|
|
|
|
rlp.skipElem
|
|
|
|
|
2018-01-25 19:22:13 +00:00
|
|
|
expect Exception:
|
|
|
|
discard rlp.getType
|
|
|
|
|
|
|
|
expect Exception:
|
|
|
|
for e in rlp:
|
|
|
|
discard e.getType
|
|
|
|
|
|
|
|
test "you cannot finish a list without appending enough elements":
|
|
|
|
var writer = initRlpList(3)
|
|
|
|
writer.append "foo"
|
|
|
|
writer.append "bar"
|
|
|
|
|
|
|
|
expect PrematureFinalizationError:
|
|
|
|
let result = writer.finish
|
|
|
|
|
|
|
|
proc withNewLines(x: string): string = x & "\n"
|
|
|
|
|
2018-05-17 13:07:37 +00:00
|
|
|
test "encode/decode object":
|
2018-05-18 08:48:37 +00:00
|
|
|
type
|
|
|
|
MyEnum = enum
|
|
|
|
foo,
|
|
|
|
bar
|
|
|
|
|
|
|
|
MyObj = object
|
|
|
|
a: array[3, char]
|
|
|
|
b: int
|
|
|
|
c: MyEnum
|
2018-05-17 13:07:37 +00:00
|
|
|
|
|
|
|
var input: MyObj
|
|
|
|
input.a = ['e', 't', 'h']
|
|
|
|
input.b = 63
|
2018-05-18 08:48:37 +00:00
|
|
|
input.c = bar
|
2018-05-17 13:07:37 +00:00
|
|
|
|
|
|
|
var writer = initRlpWriter()
|
|
|
|
writer.append(input)
|
|
|
|
let bytes = writer.finish()
|
|
|
|
var rlp = rlpFromBytes(bytes)
|
|
|
|
|
|
|
|
var output = rlp.read(MyObj)
|
|
|
|
check:
|
|
|
|
input == output
|
|
|
|
|
2018-01-25 19:22:13 +00:00
|
|
|
test "encode and decode lists":
|
|
|
|
var writer = initRlpList(3)
|
|
|
|
writer.append "foo"
|
|
|
|
writer.append ["bar", "baz"]
|
|
|
|
writer.append [30, 40, 50]
|
|
|
|
|
|
|
|
var
|
|
|
|
bytes = writer.finish
|
|
|
|
rlp = rlpFromBytes bytes
|
|
|
|
|
|
|
|
check:
|
|
|
|
bytes.hexRepr == "d183666f6fc8836261728362617ac31e2832"
|
|
|
|
rlp.inspectMatch """
|
|
|
|
{
|
|
|
|
"foo"
|
|
|
|
{
|
|
|
|
"bar"
|
|
|
|
"baz"
|
|
|
|
}
|
|
|
|
{
|
|
|
|
byte 30
|
|
|
|
byte 40
|
|
|
|
byte 50
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
bytes = encodeList(6000,
|
|
|
|
"Lorem ipsum dolor sit amet",
|
|
|
|
"Donec ligula tortor, egestas eu est vitae")
|
|
|
|
|
|
|
|
rlp = rlpFromBytes bytes
|
|
|
|
check:
|
|
|
|
rlp.listLen == 3
|
|
|
|
rlp.listElem(0).toInt(int) == 6000
|
|
|
|
rlp.listElem(1).toString == "Lorem ipsum dolor sit amet"
|
|
|
|
rlp.listElem(2).toString == "Donec ligula tortor, egestas eu est vitae"
|
|
|
|
|
2018-06-23 16:49:04 +00:00
|
|
|
# test creating RLPs from other RLPs
|
|
|
|
var list = rlpFromBytes encodeList(rlp.listELem(1), rlp.listELem(0))
|
|
|
|
|
|
|
|
# test that iteration with enterList/skipElem works as expected
|
|
|
|
list.enterList
|
|
|
|
check list.toString == "Lorem ipsum dolor sit amet"
|
|
|
|
list.skipElem
|
|
|
|
check list.toInt(int32) == 6000.int32
|
|
|
|
list.skipElem
|
|
|
|
check(not list.hasData)
|
|
|
|
expect Exception: list.skipElem
|
|
|
|
|
2018-03-26 15:14:51 +00:00
|
|
|
test "toBytes":
|
|
|
|
let rlp = rlpFromHex("f2cb847f000001827666827666a040ef02798f211da2e8173d37f255be908871ae65060dbb2f77fb29c0421447f4845ab90b50")
|
|
|
|
let tok = rlp.listElem(1).toBytes()
|
|
|
|
check:
|
|
|
|
tok.len == 32
|
|
|
|
tok.hexRepr == "40ef02798f211da2e8173d37f255be908871ae65060dbb2f77fb29c0421447f4"
|
|
|
|
|
2018-03-20 13:44:17 +00:00
|
|
|
test "nested lists":
|
|
|
|
let listBytes = encode([[1, 2, 3], [5, 6, 7]])
|
|
|
|
let listRlp = rlpFromBytes listBytes
|
|
|
|
let sublistRlp0 = listRlp.listElem(0)
|
|
|
|
let sublistRlp1 = listRlp.listElem(1)
|
|
|
|
check sublistRlp0.listElem(0).toInt(int) == 1
|
|
|
|
check sublistRlp0.listElem(1).toInt(int) == 2
|
|
|
|
check sublistRlp0.listElem(2).toInt(int) == 3
|
|
|
|
check sublistRlp1.listElem(0).toInt(int) == 5
|
|
|
|
check sublistRlp1.listElem(1).toInt(int) == 6
|
|
|
|
check sublistRlp1.listElem(2).toInt(int) == 7
|
|
|
|
|
2018-01-25 19:22:13 +00:00
|
|
|
test "encoding length":
|
|
|
|
let listBytes = encode([1,2,3,4,5])
|
|
|
|
let listRlp = rlpFromBytes listBytes
|
|
|
|
check listRlp.listLen == 5
|
|
|
|
|
|
|
|
let emptyListBytes = encode ""
|
|
|
|
check emptyListBytes.len == 1
|
|
|
|
let emptyListRlp = rlpFromBytes emptyListBytes
|
|
|
|
check emptyListRlp.blobLen == 0
|
|
|
|
|
|
|
|
test "basic decoding":
|
2018-06-23 16:49:04 +00:00
|
|
|
var rlp1 = rlpFromHex("856d6f6f7365")
|
|
|
|
var rlp2 = rlpFromHex("0x856d6f6f7365")
|
|
|
|
|
|
|
|
check:
|
|
|
|
rlp1.inspect == q"moose"
|
|
|
|
rlp2.inspect == q"moose"
|
2018-01-25 19:22:13 +00:00
|
|
|
|
|
|
|
test "malformed/truncated RLP":
|
|
|
|
var rlp = rlpFromHex("b8056d6f6f7365")
|
|
|
|
expect MalformedRlpError:
|
|
|
|
discard rlp.inspect
|
2018-03-31 09:07:55 +00:00
|
|
|
|
|
|
|
test "encode byte arrays":
|
|
|
|
var b1 = [byte(1), 2, 5, 7, 8]
|
|
|
|
var b2 = [byte(6), 8, 12, 123]
|
2018-05-15 13:06:06 +00:00
|
|
|
var b3 = @[byte(122), 56, 65, 12]
|
2018-03-31 09:07:55 +00:00
|
|
|
|
|
|
|
let rlp = rlpFromBytes(encode((b1, b2, b3)))
|
|
|
|
check:
|
|
|
|
rlp.listLen == 3
|
|
|
|
rlp.listElem(0).toBytes().toSeq() == @b1
|
|
|
|
rlp.listElem(1).toBytes().toSeq() == @b2
|
|
|
|
rlp.listElem(2).toBytes().toSeq() == @b3
|
2018-06-23 16:49:04 +00:00
|
|
|
|
|
|
|
# The first byte here is the length of the datum (132 - 128 => 4)
|
|
|
|
$(rlp.listElem(1).rawData) == "R[132, 6, 8, 12, 123]"
|
|
|
|
|
2018-07-16 16:50:47 +00:00
|
|
|
test "empty byte arrays":
|
|
|
|
var
|
|
|
|
rlp = rlpFromBytes rlp.encode("")
|
|
|
|
b = rlp.toBytes
|
|
|
|
check $b == "R[]"
|