assert() -> doAssert()
and refactored the "test" task in libp2p.nimble
This commit is contained in:
parent
a736334fe1
commit
7cbb89c7e4
|
@ -11,18 +11,11 @@ requires "nim > 0.18.0",
|
||||||
"nimcrypto >= 0.3.9",
|
"nimcrypto >= 0.3.9",
|
||||||
"chronos"
|
"chronos"
|
||||||
|
|
||||||
|
import ospaths, strutils
|
||||||
|
|
||||||
task test, "Runs the test suite":
|
task test, "Runs the test suite":
|
||||||
exec "nim c -r tests/testvarint"
|
for filename in listFiles("tests"):
|
||||||
exec "nim c -r tests/testbase58"
|
if filename.startsWith("tests" / "test") and filename.endsWith(".nim"):
|
||||||
exec "nim c -r tests/testbase32"
|
exec "nim c -r " & filename
|
||||||
exec "nim c -r tests/testbase64"
|
rmFile filename[0..^5]
|
||||||
exec "nim c -r tests/testmultiaddress"
|
|
||||||
exec "nim c -r tests/testmultihash"
|
|
||||||
exec "nim c -r tests/testmultibase"
|
|
||||||
exec "nim c -r tests/testcid"
|
|
||||||
exec "nim c -r tests/testecnist"
|
|
||||||
exec "nim c -r tests/testrsa"
|
|
||||||
exec "nim c -r tests/tested25519"
|
|
||||||
exec "nim c -r tests/testcrypto"
|
|
||||||
exec "nim c -r tests/testpeer"
|
|
||||||
exec "nim c -r tests/testdaemon"
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ proc random*(t: typedesc[PrivateKey], scheme: PKScheme,
|
||||||
##
|
##
|
||||||
## ``bits`` is number of bits for RSA key, ``bits`` value must be in
|
## ``bits`` is number of bits for RSA key, ``bits`` value must be in
|
||||||
## [512, 4096], default value is 2048 bits.
|
## [512, 4096], default value is 2048 bits.
|
||||||
assert(scheme in SupportedSchemes)
|
doAssert(scheme in SupportedSchemes)
|
||||||
result = PrivateKey(scheme: scheme)
|
result = PrivateKey(scheme: scheme)
|
||||||
if scheme == RSA:
|
if scheme == RSA:
|
||||||
result.rsakey = RsaPrivateKey.random(bits)
|
result.rsakey = RsaPrivateKey.random(bits)
|
||||||
|
@ -102,7 +102,7 @@ proc random*(t: typedesc[KeyPair], scheme: PKScheme,
|
||||||
##
|
##
|
||||||
## ``bits`` is number of bits for RSA key, ``bits`` value must be in
|
## ``bits`` is number of bits for RSA key, ``bits`` value must be in
|
||||||
## [512, 4096], default value is 2048 bits.
|
## [512, 4096], default value is 2048 bits.
|
||||||
assert(scheme in SupportedSchemes)
|
doAssert(scheme in SupportedSchemes)
|
||||||
result.seckey = PrivateKey(scheme: scheme)
|
result.seckey = PrivateKey(scheme: scheme)
|
||||||
result.pubkey = PublicKey(scheme: scheme)
|
result.pubkey = PublicKey(scheme: scheme)
|
||||||
if scheme == RSA:
|
if scheme == RSA:
|
||||||
|
|
|
@ -336,9 +336,9 @@ proc asn1EncodeOid*(dest: var openarray[byte], value: openarray[int]): int =
|
||||||
## but number of bytes (octets) required will be returned.
|
## but number of bytes (octets) required will be returned.
|
||||||
var buffer: array[16, byte]
|
var buffer: array[16, byte]
|
||||||
result = 1
|
result = 1
|
||||||
assert(len(value) >= 2)
|
doAssert(len(value) >= 2)
|
||||||
assert(value[0] >= 1 and value[0] < 2)
|
doAssert(value[0] >= 1 and value[0] < 2)
|
||||||
assert(value[1] >= 1 and value[1] <= 39)
|
doAssert(value[1] >= 1 and value[1] <= 39)
|
||||||
var oidlen = 1
|
var oidlen = 1
|
||||||
for i in 2..<len(value):
|
for i in 2..<len(value):
|
||||||
oidlen += asn1EncodeTag(buffer, cast[uint64](value[i]))
|
oidlen += asn1EncodeTag(buffer, cast[uint64](value[i]))
|
||||||
|
@ -693,7 +693,7 @@ proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag) =
|
||||||
##
|
##
|
||||||
## This procedure must be used to write `NULL`, `0` or empty `BIT STRING`,
|
## This procedure must be used to write `NULL`, `0` or empty `BIT STRING`,
|
||||||
## `OCTET STRING` types.
|
## `OCTET STRING` types.
|
||||||
assert(tag in {Asn1Tag.Null, Asn1Tag.Integer, Asn1Tag.BitString,
|
doAssert(tag in {Asn1Tag.Null, Asn1Tag.Integer, Asn1Tag.BitString,
|
||||||
Asn1Tag.OctetString})
|
Asn1Tag.OctetString})
|
||||||
var length: int
|
var length: int
|
||||||
if tag == Asn1Tag.Null:
|
if tag == Asn1Tag.Null:
|
||||||
|
@ -739,7 +739,7 @@ proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag,
|
||||||
##
|
##
|
||||||
## For `BIT STRING` you can use ``bits`` argument to specify number of used
|
## For `BIT STRING` you can use ``bits`` argument to specify number of used
|
||||||
## bits.
|
## bits.
|
||||||
assert(tag in {Asn1Tag.Integer, Asn1Tag.OctetString, Asn1Tag.BitString,
|
doAssert(tag in {Asn1Tag.Integer, Asn1Tag.OctetString, Asn1Tag.BitString,
|
||||||
Asn1Tag.Oid})
|
Asn1Tag.Oid})
|
||||||
var length: int
|
var length: int
|
||||||
if tag == Asn1Tag.Integer:
|
if tag == Asn1Tag.Integer:
|
||||||
|
@ -761,7 +761,7 @@ proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag,
|
||||||
abc.offset += length
|
abc.offset += length
|
||||||
|
|
||||||
proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, value: Asn1Composite) =
|
proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, value: Asn1Composite) =
|
||||||
assert(len(value) > 0, "Composite value not finished")
|
doAssert(len(value) > 0, "Composite value not finished")
|
||||||
var length: int
|
var length: int
|
||||||
if value.tag == Asn1Tag.Sequence:
|
if value.tag == Asn1Tag.Sequence:
|
||||||
length = asn1EncodeSequence(abc.toOpenArray(), value.buffer)
|
length = asn1EncodeSequence(abc.toOpenArray(), value.buffer)
|
||||||
|
|
|
@ -135,15 +135,15 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) =
|
||||||
var res: VarintStatus
|
var res: VarintStatus
|
||||||
pb.buffer.setLen(len(pb.buffer) + vsizeof(field))
|
pb.buffer.setLen(len(pb.buffer) + vsizeof(field))
|
||||||
res = PB.putUVarint(pb.toOpenArray(), length, protoHeader(field))
|
res = PB.putUVarint(pb.toOpenArray(), length, protoHeader(field))
|
||||||
assert(res == VarintStatus.Success)
|
doAssert(res == VarintStatus.Success)
|
||||||
pb.offset += length
|
pb.offset += length
|
||||||
case field.kind
|
case field.kind
|
||||||
of ProtoFieldKind.Varint:
|
of ProtoFieldKind.Varint:
|
||||||
res = PB.putUVarint(pb.toOpenArray(), length, field.vint)
|
res = PB.putUVarint(pb.toOpenArray(), length, field.vint)
|
||||||
assert(res == VarintStatus.Success)
|
doAssert(res == VarintStatus.Success)
|
||||||
pb.offset += length
|
pb.offset += length
|
||||||
of ProtoFieldKind.Fixed64:
|
of ProtoFieldKind.Fixed64:
|
||||||
assert(pb.isEnough(8))
|
doAssert(pb.isEnough(8))
|
||||||
var value = cast[uint64](field.vfloat64)
|
var value = cast[uint64](field.vfloat64)
|
||||||
pb.buffer[pb.offset] = byte(value and 0xFF'u32)
|
pb.buffer[pb.offset] = byte(value and 0xFF'u32)
|
||||||
pb.buffer[pb.offset + 1] = byte((value shr 8) and 0xFF'u32)
|
pb.buffer[pb.offset + 1] = byte((value shr 8) and 0xFF'u32)
|
||||||
|
@ -155,7 +155,7 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) =
|
||||||
pb.buffer[pb.offset + 7] = byte((value shr 56) and 0xFF'u32)
|
pb.buffer[pb.offset + 7] = byte((value shr 56) and 0xFF'u32)
|
||||||
pb.offset += 8
|
pb.offset += 8
|
||||||
of ProtoFieldKind.Fixed32:
|
of ProtoFieldKind.Fixed32:
|
||||||
assert(pb.isEnough(4))
|
doAssert(pb.isEnough(4))
|
||||||
var value = cast[uint32](field.vfloat32)
|
var value = cast[uint32](field.vfloat32)
|
||||||
pb.buffer[pb.offset] = byte(value and 0xFF'u32)
|
pb.buffer[pb.offset] = byte(value and 0xFF'u32)
|
||||||
pb.buffer[pb.offset + 1] = byte((value shr 8) and 0xFF'u32)
|
pb.buffer[pb.offset + 1] = byte((value shr 8) and 0xFF'u32)
|
||||||
|
@ -164,9 +164,9 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) =
|
||||||
pb.offset += 4
|
pb.offset += 4
|
||||||
of ProtoFieldKind.Length:
|
of ProtoFieldKind.Length:
|
||||||
res = PB.putUVarint(pb.toOpenArray(), length, uint(len(field.vbuffer)))
|
res = PB.putUVarint(pb.toOpenArray(), length, uint(len(field.vbuffer)))
|
||||||
assert(res == VarintStatus.Success)
|
doAssert(res == VarintStatus.Success)
|
||||||
pb.offset += length
|
pb.offset += length
|
||||||
assert(pb.isEnough(len(field.vbuffer)))
|
doAssert(pb.isEnough(len(field.vbuffer)))
|
||||||
if len(field.vbuffer) > 0:
|
if len(field.vbuffer) > 0:
|
||||||
copyMem(addr pb.buffer[pb.offset], unsafeAddr field.vbuffer[0],
|
copyMem(addr pb.buffer[pb.offset], unsafeAddr field.vbuffer[0],
|
||||||
len(field.vbuffer))
|
len(field.vbuffer))
|
||||||
|
@ -176,13 +176,13 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) =
|
||||||
|
|
||||||
proc finish*(pb: var ProtoBuffer) =
|
proc finish*(pb: var ProtoBuffer) =
|
||||||
## Prepare protobuf's buffer ``pb`` for writing to stream.
|
## Prepare protobuf's buffer ``pb`` for writing to stream.
|
||||||
assert(len(pb.buffer) > 0)
|
doAssert(len(pb.buffer) > 0)
|
||||||
if WithVarintLength in pb.options:
|
if WithVarintLength in pb.options:
|
||||||
let size = uint(len(pb.buffer) - 10)
|
let size = uint(len(pb.buffer) - 10)
|
||||||
let pos = 10 - vsizeof(size)
|
let pos = 10 - vsizeof(size)
|
||||||
var usedBytes = 0
|
var usedBytes = 0
|
||||||
let res = PB.putUVarint(pb.buffer.toOpenArray(pos, 9), usedBytes, size)
|
let res = PB.putUVarint(pb.buffer.toOpenArray(pos, 9), usedBytes, size)
|
||||||
assert(res == VarintStatus.Success)
|
doAssert(res == VarintStatus.Success)
|
||||||
pb.offset = pos
|
pb.offset = pos
|
||||||
else:
|
else:
|
||||||
pb.offset = 0
|
pb.offset = 0
|
||||||
|
@ -273,6 +273,6 @@ proc enterSubmessage*(pb: var ProtoBuffer): int =
|
||||||
proc skipSubmessage*(pb: var ProtoBuffer) =
|
proc skipSubmessage*(pb: var ProtoBuffer) =
|
||||||
## Skip current protobuf's sub-message and adjust internal offset to the
|
## Skip current protobuf's sub-message and adjust internal offset to the
|
||||||
## end of sub-message.
|
## end of sub-message.
|
||||||
assert(pb.length != 0)
|
doAssert(pb.length != 0)
|
||||||
pb.offset += pb.length
|
pb.offset += pb.length
|
||||||
pb.length = 0
|
pb.length = 0
|
||||||
|
|
|
@ -64,7 +64,7 @@ proc writeVarint*(vb: var VBuffer, value: LPSomeUVarint) =
|
||||||
vb.buffer.setLen(len(vb.buffer) + vsizeof(v))
|
vb.buffer.setLen(len(vb.buffer) + vsizeof(v))
|
||||||
let res = LP.putUVarint(toOpenArray(vb.buffer, vb.offset, len(vb.buffer) - 1),
|
let res = LP.putUVarint(toOpenArray(vb.buffer, vb.offset, len(vb.buffer) - 1),
|
||||||
length, v)
|
length, v)
|
||||||
assert(res == VarintStatus.Success)
|
doAssert(res == VarintStatus.Success)
|
||||||
vb.offset += length
|
vb.offset += length
|
||||||
|
|
||||||
proc writeSeq*[T: byte|char](vb: var VBuffer, value: openarray[T]) =
|
proc writeSeq*[T: byte|char](vb: var VBuffer, value: openarray[T]) =
|
||||||
|
@ -74,7 +74,7 @@ proc writeSeq*[T: byte|char](vb: var VBuffer, value: openarray[T]) =
|
||||||
vb.buffer.setLen(len(vb.buffer) + vsizeof(len(value)) + len(value))
|
vb.buffer.setLen(len(vb.buffer) + vsizeof(len(value)) + len(value))
|
||||||
let res = LP.putUVarint(toOpenArray(vb.buffer, vb.offset, len(vb.buffer) - 1),
|
let res = LP.putUVarint(toOpenArray(vb.buffer, vb.offset, len(vb.buffer) - 1),
|
||||||
length, uint(len(value)))
|
length, uint(len(value)))
|
||||||
assert(res == VarintStatus.Success)
|
doAssert(res == VarintStatus.Success)
|
||||||
vb.offset += length
|
vb.offset += length
|
||||||
if len(value) > 0:
|
if len(value) > 0:
|
||||||
copyMem(addr vb.buffer[vb.offset], unsafeAddr value[0], len(value))
|
copyMem(addr vb.buffer[vb.offset], unsafeAddr value[0], len(value))
|
||||||
|
|
Loading…
Reference in New Issue