diff --git a/libp2p.nimble b/libp2p.nimble index 5fa9bbc90..532a397fb 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -11,18 +11,11 @@ requires "nim > 0.18.0", "nimcrypto >= 0.3.9", "chronos" +import ospaths, strutils + task test, "Runs the test suite": - exec "nim c -r tests/testvarint" - exec "nim c -r tests/testbase58" - exec "nim c -r tests/testbase32" - exec "nim c -r tests/testbase64" - 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" + for filename in listFiles("tests"): + if filename.startsWith("tests" / "test") and filename.endsWith(".nim"): + exec "nim c -r " & filename + rmFile filename[0..^5] + diff --git a/libp2p/crypto/crypto.nim b/libp2p/crypto/crypto.nim index 08bf191b1..118e3fe81 100644 --- a/libp2p/crypto/crypto.nim +++ b/libp2p/crypto/crypto.nim @@ -87,7 +87,7 @@ proc random*(t: typedesc[PrivateKey], scheme: PKScheme, ## ## ``bits`` is number of bits for RSA key, ``bits`` value must be in ## [512, 4096], default value is 2048 bits. - assert(scheme in SupportedSchemes) + doAssert(scheme in SupportedSchemes) result = PrivateKey(scheme: scheme) if scheme == RSA: 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 ## [512, 4096], default value is 2048 bits. - assert(scheme in SupportedSchemes) + doAssert(scheme in SupportedSchemes) result.seckey = PrivateKey(scheme: scheme) result.pubkey = PublicKey(scheme: scheme) if scheme == RSA: diff --git a/libp2p/crypto/minasn1.nim b/libp2p/crypto/minasn1.nim index ca5794b56..cf856649d 100644 --- a/libp2p/crypto/minasn1.nim +++ b/libp2p/crypto/minasn1.nim @@ -336,9 +336,9 @@ proc asn1EncodeOid*(dest: var openarray[byte], value: openarray[int]): int = ## but number of bytes (octets) required will be returned. var buffer: array[16, byte] result = 1 - assert(len(value) >= 2) - assert(value[0] >= 1 and value[0] < 2) - assert(value[1] >= 1 and value[1] <= 39) + doAssert(len(value) >= 2) + doAssert(value[0] >= 1 and value[0] < 2) + doAssert(value[1] >= 1 and value[1] <= 39) var oidlen = 1 for i in 2.. 0, "Composite value not finished") + doAssert(len(value) > 0, "Composite value not finished") var length: int if value.tag == Asn1Tag.Sequence: length = asn1EncodeSequence(abc.toOpenArray(), value.buffer) diff --git a/libp2p/protobuf/minprotobuf.nim b/libp2p/protobuf/minprotobuf.nim index 74e6736bb..b36028eaa 100644 --- a/libp2p/protobuf/minprotobuf.nim +++ b/libp2p/protobuf/minprotobuf.nim @@ -135,15 +135,15 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) = var res: VarintStatus pb.buffer.setLen(len(pb.buffer) + vsizeof(field)) res = PB.putUVarint(pb.toOpenArray(), length, protoHeader(field)) - assert(res == VarintStatus.Success) + doAssert(res == VarintStatus.Success) pb.offset += length case field.kind of ProtoFieldKind.Varint: res = PB.putUVarint(pb.toOpenArray(), length, field.vint) - assert(res == VarintStatus.Success) + doAssert(res == VarintStatus.Success) pb.offset += length of ProtoFieldKind.Fixed64: - assert(pb.isEnough(8)) + doAssert(pb.isEnough(8)) var value = cast[uint64](field.vfloat64) pb.buffer[pb.offset] = byte(value 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.offset += 8 of ProtoFieldKind.Fixed32: - assert(pb.isEnough(4)) + doAssert(pb.isEnough(4)) var value = cast[uint32](field.vfloat32) pb.buffer[pb.offset] = byte(value 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 of ProtoFieldKind.Length: res = PB.putUVarint(pb.toOpenArray(), length, uint(len(field.vbuffer))) - assert(res == VarintStatus.Success) + doAssert(res == VarintStatus.Success) pb.offset += length - assert(pb.isEnough(len(field.vbuffer))) + doAssert(pb.isEnough(len(field.vbuffer))) if len(field.vbuffer) > 0: copyMem(addr pb.buffer[pb.offset], unsafeAddr field.vbuffer[0], len(field.vbuffer)) @@ -176,13 +176,13 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) = proc finish*(pb: var ProtoBuffer) = ## Prepare protobuf's buffer ``pb`` for writing to stream. - assert(len(pb.buffer) > 0) + doAssert(len(pb.buffer) > 0) if WithVarintLength in pb.options: let size = uint(len(pb.buffer) - 10) let pos = 10 - vsizeof(size) var usedBytes = 0 let res = PB.putUVarint(pb.buffer.toOpenArray(pos, 9), usedBytes, size) - assert(res == VarintStatus.Success) + doAssert(res == VarintStatus.Success) pb.offset = pos else: pb.offset = 0 @@ -273,6 +273,6 @@ proc enterSubmessage*(pb: var ProtoBuffer): int = proc skipSubmessage*(pb: var ProtoBuffer) = ## Skip current protobuf's sub-message and adjust internal offset to the ## end of sub-message. - assert(pb.length != 0) + doAssert(pb.length != 0) pb.offset += pb.length pb.length = 0 diff --git a/libp2p/vbuffer.nim b/libp2p/vbuffer.nim index 0e663d66f..332d07ec4 100644 --- a/libp2p/vbuffer.nim +++ b/libp2p/vbuffer.nim @@ -64,7 +64,7 @@ proc writeVarint*(vb: var VBuffer, value: LPSomeUVarint) = vb.buffer.setLen(len(vb.buffer) + vsizeof(v)) let res = LP.putUVarint(toOpenArray(vb.buffer, vb.offset, len(vb.buffer) - 1), length, v) - assert(res == VarintStatus.Success) + doAssert(res == VarintStatus.Success) vb.offset += length 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)) let res = LP.putUVarint(toOpenArray(vb.buffer, vb.offset, len(vb.buffer) - 1), length, uint(len(value))) - assert(res == VarintStatus.Success) + doAssert(res == VarintStatus.Success) vb.offset += length if len(value) > 0: copyMem(addr vb.buffer[vb.offset], unsafeAddr value[0], len(value))