From 833e8c9406050c9fdca382e41d4070023f5bebfc Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 29 Oct 2019 20:51:48 +0200 Subject: [PATCH] Fix overflow problem. Fix compilation warnings. --- libp2p/crypto/ecnist.nim | 5 +--- libp2p/crypto/minasn1.nim | 1 - libp2p/crypto/rsa.nim | 3 +-- libp2p/crypto/secp.nim | 9 ++++--- libp2p/multiaddress.nim | 1 - libp2p/multibase.nim | 12 ++++------ libp2p/peer.nim | 3 +-- libp2p/protobuf/minprotobuf.nim | 30 +++++++++++------------ libp2p/protocols/secure/secio.nim | 16 ++++++------- libp2p/vbuffer.nim | 1 - tests/testbase32.nim | 2 ++ tests/testbase58.nim | 2 ++ tests/testbase64.nim | 2 ++ tests/testbufferstream.nim | 10 ++++---- tests/testcid.nim | 6 +++-- tests/testcrypto.nim | 2 ++ tests/testdaemon.nim | 2 ++ tests/testecnist.nim | 8 ++++--- tests/tested25519.nim | 4 +++- tests/testidentify.nim | 18 +++++++------- tests/testmplex.nim | 40 ++++++++++++++++--------------- tests/testmultiaddress.nim | 6 +++-- tests/testmultibase.nim | 6 +++-- tests/testmultihash.nim | 2 ++ tests/testmultistream.nim | 40 ++++++++++++++++--------------- tests/testpeer.nim | 2 ++ tests/testpubsub.nim | 25 +++++++++---------- tests/testrsa.nim | 10 ++++---- tests/testsecp256k1.nim | 4 +++- tests/testswitch.nim | 20 +++++++++------- tests/testtransport.nim | 8 ++++--- tests/testvarint.nim | 2 ++ 32 files changed, 166 insertions(+), 136 deletions(-) diff --git a/libp2p/crypto/ecnist.nim b/libp2p/crypto/ecnist.nim index cd59d2da4..27b09ade7 100644 --- a/libp2p/crypto/ecnist.nim +++ b/libp2p/crypto/ecnist.nim @@ -114,7 +114,7 @@ proc checkPublic(key: openarray[byte], curve: cint): uint32 = var x = [0x00'u8, 0x01'u8] var impl = brEcGetDefault() var orderlen = 0 - var order = impl.order(curve, addr orderlen) + discard impl.order(curve, addr orderlen) result = impl.mul(cast[ptr cuchar](unsafeAddr ckey[0]), len(ckey), cast[ptr cuchar](addr x[0]), len(x), curve) @@ -766,7 +766,6 @@ proc initRaw*(sig: var EcSignature, data: openarray[byte]): bool = ## or ``Sig521Length``. ## ## Procedure returns ``true`` on success, ``false`` otherwise. - var curve: cint let length = len(data) if (length == Sig256Length) or (length == Sig384Length) or (length == Sig521Length): @@ -815,7 +814,6 @@ proc scalarMul*(pub: EcPublicKey, sec: EcPrivateKey): EcPublicKey = if pub.key.curve == sec.key.curve: var key = new EcPublicKey if key.copy(pub): - var slength = cint(0) let poffset = key.getOffset() let soffset = sec.getOffset() if poffset >= 0 and soffset >= 0: @@ -840,7 +838,6 @@ proc toSecret*(pubkey: EcPublicKey, seckey: EcPrivateKey, ## for `secp384r1` and 66 bytes for `secp521r1`. doAssert((not isNil(pubkey)) and (not isNil(seckey))) var mult = scalarMul(pubkey, seckey) - var length = 0 if not isNil(mult): if seckey.key.curve == BR_EC_SECP256R1: result = Secret256Length diff --git a/libp2p/crypto/minasn1.nim b/libp2p/crypto/minasn1.nim index cf856649d..2a3933ad7 100644 --- a/libp2p/crypto/minasn1.nim +++ b/libp2p/crypto/minasn1.nim @@ -472,7 +472,6 @@ proc read*(ab: var Asn1Buffer, field: var Asn1Field): Asn1Status = tag, ttag, offset: int length, tlength: uint64 klass: Asn1Class - res: Asn1Status inclass: bool inclass = false diff --git a/libp2p/crypto/rsa.nim b/libp2p/crypto/rsa.nim index ef57d4599..dddb2a62e 100644 --- a/libp2p/crypto/rsa.nim +++ b/libp2p/crypto/rsa.nim @@ -386,7 +386,6 @@ proc init*(key: var RsaPrivateKey, data: openarray[byte]): Asn1Status = ## Procedure returns ``Asn1Status``. var field, rawn, rawpube, rawprie, rawp, rawq, rawdp, rawdq, rawiq: Asn1Field - version: uint64 var ab = Asn1Buffer.init(data) result = ab.read(field) @@ -484,7 +483,7 @@ proc init*(key: var RsaPublicKey, data: openarray[byte]): Asn1Status = ## ``data``. ## ## Procedure returns ``Asn1Status``. - var field, rawn, rawe, oid: Asn1Field + var field, rawn, rawe: Asn1Field var ab = Asn1Buffer.init(data) result = ab.read(field) diff --git a/libp2p/crypto/secp.nim b/libp2p/crypto/secp.nim index f41fcc15b..6dc497346 100644 --- a/libp2p/crypto/secp.nim +++ b/libp2p/crypto/secp.nim @@ -6,7 +6,6 @@ ## at your option. ## This file may not be copied, modified, or distributed except according to ## those terms. -import strutils import secp256k1, nimcrypto/sysrand, nimcrypto/utils, nimcrypto/hash, nimcrypto/sha2 export sha2 @@ -268,10 +267,10 @@ proc toBytes*(key: SkPublicKey, data: var openarray[byte]): int = var length = csize(len(data)) result = SkRawPublicKeySize if len(data) >= SkRawPublicKeySize: - let res = secp256k1_ec_pubkey_serialize(ctx.context, - cast[ptr cuchar](addr data[0]), - addr length, unsafeAddr key, - SECP256K1_EC_COMPRESSED) + discard secp256k1_ec_pubkey_serialize(ctx.context, + cast[ptr cuchar](addr data[0]), + addr length, unsafeAddr key, + SECP256K1_EC_COMPRESSED) proc toBytes*(sig: SkSignature, data: var openarray[byte]): int = ## Serialize Secp256k1 `signature` ``sig`` to raw binary form and store it diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index 2e8f6d1db..562945456 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -572,7 +572,6 @@ proc `$`*(value: MultiAddress): string = ## Return string representation of MultiAddress ``value``. var header: uint64 var vb = value - var data = newSeq[byte]() var parts = newSeq[string]() var part: string while true: diff --git a/libp2p/multibase.nim b/libp2p/multibase.nim index 5e0b9e9ee..1f76641a1 100644 --- a/libp2p/multibase.nim +++ b/libp2p/multibase.nim @@ -11,7 +11,7 @@ ## ## TODO: ## 1. base32z -import tables, strutils +import tables import base32, base58, base64 type @@ -20,18 +20,14 @@ type MultiBase* = object - MBDecoder = proc(inbytes: openarray[char], - outbytes: var openarray[byte], - outlen: var int): MultibaseStatus {.nimcall.} - MBEncoder = proc(inbytes: openarray[byte], - outbytes: var openarray[char], - outlen: var int): MultibaseStatus {.nimcall.} MBCodeSize = proc(length: int): int {.nimcall.} MBCodec = object code: char name: string - encr: MBEncoder + encr: proc(inbytes: openarray[byte], + outbytes: var openarray[char], + outlen: var int): MultibaseStatus {.nimcall.} decr: proc(inbytes: openarray[char], outbytes: var openarray[byte], outlen: var int): MultibaseStatus {.nimcall.} diff --git a/libp2p/peer.nim b/libp2p/peer.nim index 40f364c9f..bf1a2463a 100644 --- a/libp2p/peer.nim +++ b/libp2p/peer.nim @@ -16,7 +16,7 @@ import protobuf/minprotobuf const maxInlineKeyLength* = 42 -# TODO: add proper on disc serialization +# TODO: add proper on disc serialization # using peer-id protobuf format type PeerID* = object @@ -165,7 +165,6 @@ proc init*(t: typedesc[PeerID], pubkey: PublicKey): PeerID = ## Create new peer id from public key ``pubkey``. var pubraw = pubkey.getBytes() var mh: MultiHash - var codec: MultiCodec if len(pubraw) <= maxInlineKeyLength: mh = MultiHash.digest("identity", pubraw) else: diff --git a/libp2p/protobuf/minprotobuf.nim b/libp2p/protobuf/minprotobuf.nim index 888f8b6fd..f1ca7b126 100644 --- a/libp2p/protobuf/minprotobuf.nim +++ b/libp2p/protobuf/minprotobuf.nim @@ -156,13 +156,13 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) = 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) - pb.buffer[pb.offset + 2] = byte((value shr 16) and 0xFF'u32) - pb.buffer[pb.offset + 3] = byte((value shr 24) and 0xFF'u32) - pb.buffer[pb.offset + 4] = byte((value shr 32) and 0xFF'u32) - pb.buffer[pb.offset + 5] = byte((value shr 40) and 0xFF'u32) - pb.buffer[pb.offset + 6] = byte((value shr 48) and 0xFF'u32) - pb.buffer[pb.offset + 7] = byte((value shr 56) and 0xFF'u32) + pb.buffer[pb.offset + 1] = byte((value shr 8) and 0xFF'u64) + pb.buffer[pb.offset + 2] = byte((value shr 16) and 0xFF'u64) + pb.buffer[pb.offset + 3] = byte((value shr 24) and 0xFF'u64) + pb.buffer[pb.offset + 4] = byte((value shr 32) and 0xFF'u64) + pb.buffer[pb.offset + 5] = byte((value shr 40) and 0xFF'u64) + pb.buffer[pb.offset + 6] = byte((value shr 48) and 0xFF'u64) + pb.buffer[pb.offset + 7] = byte((value shr 56) and 0xFF'u64) pb.offset += 8 of ProtoFieldKind.Fixed32: doAssert(pb.isEnough(4)) @@ -196,17 +196,17 @@ proc finish*(pb: var ProtoBuffer) = pb.offset = pos elif WithUint32BeLength in pb.options: let size = uint(len(pb.buffer) - 4) - pb.buffer[0] = byte((size shr 24) and 0xFF) - pb.buffer[1] = byte((size shr 16) and 0xFF) - pb.buffer[2] = byte((size shr 8) and 0xFF) - pb.buffer[3] = byte(size and 0xFF) + pb.buffer[0] = byte((size shr 24) and 0xFF'u) + pb.buffer[1] = byte((size shr 16) and 0xFF'u) + pb.buffer[2] = byte((size shr 8) and 0xFF'u) + pb.buffer[3] = byte(size and 0xFF'u) pb.offset = 4 elif WithUint32LeLength in pb.options: let size = uint(len(pb.buffer) - 4) - pb.buffer[0] = byte(size and 0xFF) - pb.buffer[1] = byte((size shr 8) and 0xFF) - pb.buffer[2] = byte((size shr 16) and 0xFF) - pb.buffer[3] = byte((size shr 24) and 0xFF) + pb.buffer[0] = byte(size and 0xFF'u) + pb.buffer[1] = byte((size shr 8) and 0xFF'u) + pb.buffer[2] = byte((size shr 16) and 0xFF'u) + pb.buffer[3] = byte((size shr 24) and 0xFF'u) pb.offset = 4 else: pb.offset = 0 diff --git a/libp2p/protocols/secure/secio.nim b/libp2p/protocols/secure/secio.nim index 8433fb6aa..534bcf088 100644 --- a/libp2p/protocols/secure/secio.nim +++ b/libp2p/protocols/secure/secio.nim @@ -9,12 +9,12 @@ ## those terms. import chronos, chronicles import nimcrypto/[sysrand, hmac, sha2, sha, hash, rijndael, twofish, bcmode] -import secure, - ../../connection, - ../../crypto/crypto, +import secure, + ../../connection, + ../../crypto/crypto, ../../crypto/ecnist, - ../../protobuf/minprotobuf, - ../../peer, + ../../protobuf/minprotobuf, + ../../peer, ../../stream/bufferstream export hmac, sha2, sha, hash, rijndael, bcmode @@ -208,9 +208,9 @@ proc writeMessage*(sconn: SecureConnection, message: seq[byte]) {.async.} = sconn.writerMac.finish(msg.toOpenArray(mo, mo + macsize - 1)) sconn.writerMac.reset() let length = len(message) + macsize - msg[0] = byte(length shr 24) - msg[1] = byte(length shr 16) - msg[2] = byte(length shr 8) + msg[0] = byte((length shr 24) and 0xFF) + msg[1] = byte((length shr 16) and 0xFF) + msg[2] = byte((length shr 8) and 0xFF) msg[3] = byte(length and 0xFF) trace "Writing message", message = toHex(msg) try: diff --git a/libp2p/vbuffer.nim b/libp2p/vbuffer.nim index f632e2ce2..1ee5b9d66 100644 --- a/libp2p/vbuffer.nim +++ b/libp2p/vbuffer.nim @@ -80,7 +80,6 @@ proc writeSeq*[T: byte|char](vb: var VBuffer, value: openarray[T]) = proc writeArray*[T: byte|char](vb: var VBuffer, value: openarray[T]) = ## Write array ``value`` to buffer ``vb``, value will NOT be prefixed with ## varint length of the array. - var length = 0 if len(value) > 0: vb.buffer.setLen(len(vb.buffer) + len(value)) copyMem(addr vb.buffer[vb.offset], unsafeAddr value[0], len(value)) diff --git a/tests/testbase32.nim b/tests/testbase32.nim index 9ee6133f2..4812833fc 100644 --- a/tests/testbase32.nim +++ b/tests/testbase32.nim @@ -1,6 +1,8 @@ import unittest import ../libp2p/base32 +when defined(nimHasUsed): {.used.} + const TVBaseUpperPadding = [ ["f", "MY======"], ["fo", "MZXQ===="], diff --git a/tests/testbase58.nim b/tests/testbase58.nim index fdba59e57..32df60c5d 100644 --- a/tests/testbase58.nim +++ b/tests/testbase58.nim @@ -1,6 +1,8 @@ import unittest import ../libp2p/base58 +when defined(nimHasUsed): {.used.} + proc hexToBytes*(a: string, result: var openarray[byte]) = doAssert(len(a) == 2 * len(result)) var i = 0 diff --git a/tests/testbase64.nim b/tests/testbase64.nim index 61d5b5d0b..22ea7d978 100644 --- a/tests/testbase64.nim +++ b/tests/testbase64.nim @@ -1,6 +1,8 @@ import unittest import ../libp2p/base64 +when defined(nimHasUsed): {.used.} + const TVBasePadding = [ ["f", "Zg=="], ["fo", "Zm8="], diff --git a/tests/testbufferstream.nim b/tests/testbufferstream.nim index 72a128d2b..afbf20b2a 100644 --- a/tests/testbufferstream.nim +++ b/tests/testbufferstream.nim @@ -2,6 +2,8 @@ import unittest, deques, sequtils, strformat import chronos import ../libp2p/stream/bufferstream +when defined(nimHasUsed): {.used.} + suite "BufferStream": test "push data to buffer": proc testPushTo(): Future[bool] {.async.} = @@ -150,7 +152,7 @@ suite "BufferStream": test "write ptr": proc testWritePtr(): Future[bool] {.async.} = - proc writeHandler(data: seq[byte]) {.async, gcsafe.} = + proc writeHandler(data: seq[byte]) {.async, gcsafe.} = check cast[string](data) == "Hello!" let buff = newBufferStream(writeHandler, 10) @@ -165,7 +167,7 @@ suite "BufferStream": test "write string": proc testWritePtr(): Future[bool] {.async.} = - proc writeHandler(data: seq[byte]) {.async, gcsafe.} = + proc writeHandler(data: seq[byte]) {.async, gcsafe.} = check cast[string](data) == "Hello!" let buff = newBufferStream(writeHandler, 10) @@ -179,7 +181,7 @@ suite "BufferStream": test "write bytes": proc testWritePtr(): Future[bool] {.async.} = - proc writeHandler(data: seq[byte]) {.async, gcsafe.} = + proc writeHandler(data: seq[byte]) {.async, gcsafe.} = check cast[string](data) == "Hello!" let buff = newBufferStream(writeHandler, 10) @@ -194,7 +196,7 @@ suite "BufferStream": test "write should happen in order": proc testWritePtr(): Future[bool] {.async.} = var count = 1 - proc writeHandler(data: seq[byte]) {.async, gcsafe.} = + proc writeHandler(data: seq[byte]) {.async, gcsafe.} = check cast[string](data) == &"Msg {$count}" count.inc diff --git a/tests/testcid.nim b/tests/testcid.nim index 70b89fd75..d714626c2 100644 --- a/tests/testcid.nim +++ b/tests/testcid.nim @@ -1,6 +1,8 @@ import unittest import ../libp2p/[cid, multihash, multicodec] +when defined(nimHasUsed): {.used.} + suite "Content identifier CID test suite": test "CIDv0 test vector": @@ -13,7 +15,7 @@ suite "Content identifier CID test suite": cid0.mhash().mcodec == multiCodec("sha2-256") var res = 0 try: - var cidb0 = Cid.init("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII") + discard Cid.init("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII") except CidError: res = 1 check res == 1 @@ -29,7 +31,7 @@ suite "Content identifier CID test suite": cid1.contentType() == multiCodec("raw") cid1.mhash().mcodec == multiCodec("sha2-256") hex(cid1) == chex - + test "Comparison test": var msg = "Hello World!" var mmsg = "Hello World!Hello World!" diff --git a/tests/testcrypto.nim b/tests/testcrypto.nim index be2d0476f..305be3dcb 100644 --- a/tests/testcrypto.nim +++ b/tests/testcrypto.nim @@ -13,6 +13,8 @@ import unittest import nimcrypto/utils import ../libp2p/crypto/crypto +when defined(nimHasUsed): {.used.} + const PrivateKeys = [ """080012BE023082013A020100024100AD8A7D5B0EB7C852C1464E4567651F412C diff --git a/tests/testdaemon.nim b/tests/testdaemon.nim index efe0728ba..946c20c8e 100644 --- a/tests/testdaemon.nim +++ b/tests/testdaemon.nim @@ -3,6 +3,8 @@ import chronos import ../libp2p/daemon/daemonapi, ../libp2p/multiaddress, ../libp2p/multicodec, ../libp2p/cid, ../libp2p/multihash, ../libp2p/peer +when defined(nimHasUsed): {.used.} + proc identitySpawnTest(): Future[bool] {.async.} = var api = await newDaemonApi() var data = await api.identity() diff --git a/tests/testecnist.nim b/tests/testecnist.nim index 0382113a7..15b4b1469 100644 --- a/tests/testecnist.nim +++ b/tests/testecnist.nim @@ -10,6 +10,8 @@ import unittest import nimcrypto/utils import ../libp2p/crypto/ecnist +when defined(nimHasUsed): {.used.} + const TestsCount = 10 # number of random tests @@ -392,7 +394,7 @@ suite "EC NIST-P256/384/521 test suite": var sersk = kp.seckey.getBytes() var serpk = kp.pubkey.getBytes() var sersig = sig.getBytes() - var seckey = EcPrivateKey.init(sersk) + discard EcPrivateKey.init(sersk) var pubkey = EcPublicKey.init(serpk) var csig = EcSignature.init(sersig) check csig.verify(message, pubkey) == true @@ -499,7 +501,7 @@ suite "EC NIST-P256/384/521 test suite": var sersk = kp.seckey.getBytes() var serpk = kp.pubkey.getBytes() var sersig = sig.getBytes() - var seckey = EcPrivateKey.init(sersk) + discard EcPrivateKey.init(sersk) var pubkey = EcPublicKey.init(serpk) var csig = EcSignature.init(sersig) check csig.verify(message, pubkey) == true @@ -606,7 +608,7 @@ suite "EC NIST-P256/384/521 test suite": var sersk = kp.seckey.getBytes() var serpk = kp.pubkey.getBytes() var sersig = sig.getBytes() - var seckey = EcPrivateKey.init(sersk) + discard EcPrivateKey.init(sersk) var pubkey = EcPublicKey.init(serpk) var csig = EcSignature.init(sersig) check csig.verify(message, pubkey) == true diff --git a/tests/tested25519.nim b/tests/tested25519.nim index 40ceb2835..597c58a8b 100644 --- a/tests/tested25519.nim +++ b/tests/tested25519.nim @@ -12,6 +12,8 @@ import unittest import nimcrypto/utils import ../libp2p/crypto/ed25519/ed25519 +when defined(nimHasUsed): {.used.} + const TestsCount = 20 const SecretKeys = [ @@ -174,7 +176,7 @@ suite "Ed25519 test suite": var sersk = kp.seckey.getBytes() var serpk = kp.pubkey.getBytes() var sersig = sig.getBytes() - var seckey = EdPrivateKey.init(sersk) + discard EdPrivateKey.init(sersk) var pubkey = EdPublicKey.init(serpk) var csig = EdSignature.init(sersig) check csig.verify(message, pubkey) == true diff --git a/tests/testidentify.nim b/tests/testidentify.nim index d193e2d67..9c5c08543 100644 --- a/tests/testidentify.nim +++ b/tests/testidentify.nim @@ -1,16 +1,18 @@ import unittest, options import chronos, strutils, sequtils -import ../libp2p/[protocols/identify, - multiaddress, - peerinfo, - peer, - connection, - multistream, +import ../libp2p/[protocols/identify, + multiaddress, + peerinfo, + peer, + connection, + multistream, transports/transport, - transports/tcptransport, - protocols/protocol, + transports/tcptransport, + protocols/protocol, crypto/crypto] +when defined(nimHasUsed): {.used.} + suite "Identify": test "handle identify message": proc testHandle(): Future[bool] {.async.} = diff --git a/tests/testmplex.nim b/tests/testmplex.nim index f4076e9f5..adb1105c5 100644 --- a/tests/testmplex.nim +++ b/tests/testmplex.nim @@ -1,23 +1,25 @@ import unittest, sequtils, sugar, strformat, options, strformat import chronos, nimcrypto/utils, chronicles -import ../libp2p/[connection, - stream/lpstream, - stream/bufferstream, - transports/tcptransport, - transports/transport, - protocols/identify, +import ../libp2p/[connection, + stream/lpstream, + stream/bufferstream, + transports/tcptransport, + transports/transport, + protocols/identify, multiaddress, - muxers/mplex/mplex, - muxers/mplex/coder, + muxers/mplex/mplex, + muxers/mplex/coder, muxers/mplex/types, muxers/mplex/lpchannel] +when defined(nimHasUsed): {.used.} + suite "Mplex": test "encode header with channel id 0": proc testEncodeHeader(): Future[bool] {.async.} = proc encHandler(msg: seq[byte]) {.async.} = check msg == fromHex("000873747265616d2031") - + let stream = newBufferStream(encHandler) let conn = newConnection(stream) await conn.writeMsg(0, MessageType.New, cast[seq[byte]]("stream 1")) @@ -30,7 +32,7 @@ suite "Mplex": proc testEncodeHeader(): Future[bool] {.async.} = proc encHandler(msg: seq[byte]) {.async.} = check msg == fromHex("88010873747265616d2031") - + let stream = newBufferStream(encHandler) let conn = newConnection(stream) await conn.writeMsg(17, MessageType.New, cast[seq[byte]]("stream 1")) @@ -44,7 +46,7 @@ suite "Mplex": var step = 0 proc encHandler(msg: seq[byte]) {.async.} = check msg == fromHex("020873747265616d2031") - + let stream = newBufferStream(encHandler) let conn = newConnection(stream) await conn.writeMsg(0, MessageType.MsgOut, cast[seq[byte]]("stream 1")) @@ -70,7 +72,7 @@ suite "Mplex": test "decode header with channel id 0": proc testDecodeHeader(): Future[bool] {.async.} = - proc encHandler(msg: seq[byte]) {.async.} = discard + proc encHandler(msg: seq[byte]) {.async.} = discard let stream = newBufferStream(encHandler) let conn = newConnection(stream) await stream.pushTo(fromHex("000873747265616d2031")) @@ -86,7 +88,7 @@ suite "Mplex": test "decode header and body with channel id 0": proc testDecodeHeader(): Future[bool] {.async.} = - proc encHandler(msg: seq[byte]) {.async.} = discard + proc encHandler(msg: seq[byte]) {.async.} = discard let stream = newBufferStream(encHandler) let conn = newConnection(stream) await stream.pushTo(fromHex("021668656C6C6F2066726F6D206368616E6E656C20302121")) @@ -103,7 +105,7 @@ suite "Mplex": test "decode header and body with channel id other than 0": proc testDecodeHeader(): Future[bool] {.async.} = - proc encHandler(msg: seq[byte]) {.async.} = discard + proc encHandler(msg: seq[byte]) {.async.} = discard let stream = newBufferStream(encHandler) let conn = newConnection(stream) await stream.pushTo(fromHex("8a011668656C6C6F2066726F6D206368616E6E656C20302121")) @@ -123,7 +125,7 @@ suite "Mplex": let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0") proc connHandler(conn: Connection) {.async, gcsafe.} = - proc handleMplexListen(stream: Connection) {.async, gcsafe.} = + proc handleMplexListen(stream: Connection) {.async, gcsafe.} = let msg = await stream.readLp() check cast[string](msg) == "Hello from stream!" await stream.close() @@ -152,7 +154,7 @@ suite "Mplex": let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0") proc connHandler(conn: Connection) {.async, gcsafe.} = - proc handleMplexListen(stream: Connection) {.async, gcsafe.} = + proc handleMplexListen(stream: Connection) {.async, gcsafe.} = await stream.writeLp("Hello from stream!") await stream.close() @@ -185,7 +187,7 @@ suite "Mplex": var count = 1 var listenConn: Connection proc connHandler(conn: Connection) {.async, gcsafe.} = - proc handleMplexListen(stream: Connection) {.async, gcsafe.} = + proc handleMplexListen(stream: Connection) {.async, gcsafe.} = let msg = await stream.readLp() check cast[string](msg) == &"stream {count}!" count.inc @@ -224,7 +226,7 @@ suite "Mplex": var listenConn: Connection proc connHandler(conn: Connection) {.async, gcsafe.} = listenConn = conn - proc handleMplexListen(stream: Connection) {.async, gcsafe.} = + proc handleMplexListen(stream: Connection) {.async, gcsafe.} = let msg = await stream.readLp() check cast[string](msg) == &"stream {count} from dialer!" await stream.writeLp(&"stream {count} from listener!") @@ -245,7 +247,7 @@ suite "Mplex": let mplexDial = newMplex(conn) let dialFut = mplexDial.handle() - dialFut.addCallback(proc(udata: pointer = nil) {.gcsafe.} + dialFut.addCallback(proc(udata: pointer = nil) {.gcsafe.} = debug "completed dialer") for i in 1..10: let stream = await mplexDial.newStream("dialer stream") diff --git a/tests/testmultiaddress.nim b/tests/testmultiaddress.nim index b75676720..22c92dca2 100644 --- a/tests/testmultiaddress.nim +++ b/tests/testmultiaddress.nim @@ -1,6 +1,8 @@ import unittest import ../libp2p/multiaddress +when defined(nimHasUsed): {.used.} + type PatternVector = object pattern: MaPattern @@ -303,7 +305,7 @@ suite "MultiAddress test suite": for item in FailureVectors: var r = false try: - var a = MultiAddress.init(item) + discard MultiAddress.init(item) except: r = true check r == true @@ -319,7 +321,7 @@ suite "MultiAddress test suite": for item in RustFailureVectors: var r = false try: - var a = MultiAddress.init(item) + discard MultiAddress.init(item) except: r = true check r == true diff --git a/tests/testmultibase.nim b/tests/testmultibase.nim index 1679e8ccd..de97c9ef6 100644 --- a/tests/testmultibase.nim +++ b/tests/testmultibase.nim @@ -1,6 +1,8 @@ import unittest import ../libp2p/multibase +when defined(nimHasUsed): {.used.} + const GoTestVectors = [ [ "identity", @@ -292,12 +294,12 @@ suite "MultiBase test suite": var r1 = false var r2 = false try: - var enc = MultiBase.encode("unknwon", data) + discard MultiBase.encode("unknwon", data) except MultiBaseError: r1 = true try: - var dec = MultiBase.decode("\x01\x00") + discard MultiBase.decode("\x01\x00") except MultiBaseError: r2 = true diff --git a/tests/testmultihash.nim b/tests/testmultihash.nim index 26522308b..4b98e7158 100644 --- a/tests/testmultihash.nim +++ b/tests/testmultihash.nim @@ -2,6 +2,8 @@ import unittest import nimcrypto/utils import ../libp2p/multihash +when defined(nimHasUsed): {.used.} + const RustTestVectors = [ [ diff --git a/tests/testmultistream.nim b/tests/testmultistream.nim index 16629ab7e..7c3c566c2 100644 --- a/tests/testmultistream.nim +++ b/tests/testmultistream.nim @@ -1,17 +1,19 @@ import unittest, strutils, sequtils, sugar, strformat, options import chronos -import ../libp2p/connection, +import ../libp2p/connection, ../libp2p/multistream, - ../libp2p/stream/lpstream, - ../libp2p/connection, - ../libp2p/multiaddress, - ../libp2p/transports/transport, - ../libp2p/transports/tcptransport, + ../libp2p/stream/lpstream, + ../libp2p/connection, + ../libp2p/multiaddress, + ../libp2p/transports/transport, + ../libp2p/transports/tcptransport, ../libp2p/protocols/protocol, - ../libp2p/crypto/crypto, + ../libp2p/crypto/crypto, ../libp2p/peerinfo, ../libp2p/peer +when defined(nimHasUsed): {.used.} + ## Mock stream for select test type TestSelectStream = ref object of LPStream @@ -43,10 +45,10 @@ method readExactly*(s: TestSelectStream, cstring("\0x3na\n"), "\0x3na\n".len()) -method write*(s: TestSelectStream, msg: seq[byte], msglen = -1) +method write*(s: TestSelectStream, msg: seq[byte], msglen = -1) {.async, gcsafe.} = discard -method write*(s: TestSelectStream, msg: string, msglen = -1) +method write*(s: TestSelectStream, msg: string, msglen = -1) {.async, gcsafe.} = discard method close(s: TestSelectStream) {.async, gcsafe.} = s.closed = true @@ -65,7 +67,7 @@ type method readExactly*(s: TestLsStream, pbytes: pointer, - nbytes: int): + nbytes: int): Future[void] {.async.} = case s.step: of 1: @@ -112,7 +114,7 @@ type method readExactly*(s: TestNaStream, pbytes: pointer, - nbytes: int): + nbytes: int): Future[void] {.async, gcsafe.} = case s.step: of 1: @@ -168,7 +170,7 @@ suite "Multistream select": peerInfo.peerId = some(PeerID.init(seckey)) var protocol: LPProtocol = new LPProtocol proc testHandler(conn: Connection, - proto: string): + proto: string): Future[void] {.async, gcsafe.} = check proto == "/test/proto/1.0.0" await conn.close() @@ -197,7 +199,7 @@ suite "Multistream select": peerInfo.peerId = some(PeerID.init(seckey)) var protocol: LPProtocol = new LPProtocol proc testHandler(conn: Connection, - proto: string): + proto: string): Future[void] {.async, gcsafe.} = discard protocol.handler = testHandler ms.addHandler("/test/proto1/1.0.0", protocol) @@ -224,7 +226,7 @@ suite "Multistream select": peerInfo.peerId = some(PeerID.init(seckey)) var protocol: LPProtocol = new LPProtocol proc testHandler(conn: Connection, - proto: string): + proto: string): Future[void] {.async, gcsafe.} = discard protocol.handler = testHandler ms.addHandler("/unabvailable/proto/1.0.0", protocol) @@ -244,7 +246,7 @@ suite "Multistream select": peerInfo.peerId = some(PeerID.init(seckey)) var protocol: LPProtocol = new LPProtocol proc testHandler(conn: Connection, - proto: string): + proto: string): Future[void] {.async, gcsafe.} = check proto == "/test/proto/1.0.0" await conn.writeLp("Hello!") @@ -285,7 +287,7 @@ suite "Multistream select": protocol.handler = proc(conn: Connection, proto: string) {.async, gcsafe.} = await conn.close() proc testHandler(conn: Connection, - proto: string): + proto: string): Future[void] {.async.} = discard protocol.handler = testHandler msListen.addHandler("/test/proto1/1.0.0", protocol) @@ -317,7 +319,7 @@ suite "Multistream select": peerInfo.peerId = some(PeerID.init(seckey)) var protocol: LPProtocol = new LPProtocol proc testHandler(conn: Connection, - proto: string): + proto: string): Future[void] {.async, gcsafe.} = check proto == "/test/proto/1.0.0" await conn.writeLp("Hello!") @@ -337,7 +339,7 @@ suite "Multistream select": let transport2: TcpTransport = newTransport(TcpTransport) let conn = await transport2.dial(transport1.ma) - check (await msDial.select(conn, + check (await msDial.select(conn, @["/test/proto/1.0.0", "/test/no/proto/1.0.0"])) == "/test/proto/1.0.0" let hello = cast[string](await conn.readLp()) @@ -356,7 +358,7 @@ suite "Multistream select": peerInfo.peerId = some(PeerID.init(seckey)) var protocol: LPProtocol = new LPProtocol proc testHandler(conn: Connection, - proto: string): + proto: string): Future[void] {.async, gcsafe.} = await conn.writeLp(&"Hello from {proto}!") await conn.close() diff --git a/tests/testpeer.nim b/tests/testpeer.nim index 5d013f432..fb5b88bff 100644 --- a/tests/testpeer.nim +++ b/tests/testpeer.nim @@ -13,6 +13,8 @@ import unittest import nimcrypto/utils import ../libp2p/crypto/crypto, ../libp2p/peer, ../libp2p/base58 +when defined(nimHasUsed): {.used.} + const # Test vectors are generated using # https://github.com/libp2p/go-libp2p-crypto/blob/master/key.go diff --git a/tests/testpubsub.nim b/tests/testpubsub.nim index 0d831ee17..3aeb279ec 100644 --- a/tests/testpubsub.nim +++ b/tests/testpubsub.nim @@ -6,7 +6,6 @@ ## at your option. ## This file may not be copied, modified, or distributed except according to ## those terms. - import unittest, options, tables, sugar, sequtils import chronos, chronicles import ../libp2p/[switch, @@ -28,12 +27,14 @@ import ../libp2p/[switch, protocols/pubsub/pubsub, protocols/pubsub/floodsub] +when defined(nimHasUsed): {.used.} + proc createMplex(conn: Connection): Muxer = result = newMplex(conn) -proc createNode(privKey: Option[PrivateKey] = none(PrivateKey), - address: string = "/ip4/127.0.0.1/tcp/0", - triggerSelf: bool = false): Switch = +proc createNode(privKey: Option[PrivateKey] = none(PrivateKey), + address: string = "/ip4/127.0.0.1/tcp/0", + triggerSelf: bool = false): Switch = var peerInfo: PeerInfo var seckey = privKey if privKey.isNone: @@ -59,7 +60,7 @@ proc generateNodes*(num: Natural): seq[Switch] = for i in 0.. B": + test "FloodSub basic publish/subscribe A -> B": proc testBasicPubSub(): Future[bool] {.async.} = var passed: bool - proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = + proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = check topic == "foobar" passed = true @@ -94,7 +95,7 @@ suite "PubSub": test "FloodSub basic publish/subscribe B -> A": proc testBasicPubSub(): Future[bool] {.async.} = - proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = + proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = check topic == "foobar" var nodes = generateNodes(2) @@ -115,10 +116,10 @@ suite "PubSub": check: waitFor(testBasicPubSub()) == true - test "FloodSub multiple peers, no self trigger": + test "FloodSub multiple peers, no self trigger": proc testBasicFloodSub(): Future[bool] {.async.} = var passed: int - proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = + proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = check topic == "foobar" passed.inc() @@ -147,10 +148,10 @@ suite "PubSub": check: waitFor(testBasicFloodSub()) == true - test "FloodSub multiple peers, with self trigger": + test "FloodSub multiple peers, with self trigger": proc testBasicFloodSub(): Future[bool] {.async.} = var passed: int - proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = + proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = check topic == "foobar" passed.inc() diff --git a/tests/testrsa.nim b/tests/testrsa.nim index 52e618723..fd6afceb8 100644 --- a/tests/testrsa.nim +++ b/tests/testrsa.nim @@ -10,6 +10,8 @@ import unittest import nimcrypto/utils import ../libp2p/crypto/rsa +when defined(nimHasUsed): {.used.} + const TestsCount = 20 # number of random tests @@ -418,7 +420,7 @@ suite "RSA 512/1024/2048/4096 test suite": var sersk = kp.seckey.getBytes() var serpk = kp.pubkey.getBytes() var sersig = sig.getBytes() - var seckey = RsaPrivateKey.init(sersk) + discard RsaPrivateKey.init(sersk) var pubkey = RsaPublicKey.init(serpk) var csig = RsaSignature.init(sersig) check csig.verify(message, pubkey) == true @@ -434,7 +436,7 @@ suite "RSA 512/1024/2048/4096 test suite": var sersk = kp.seckey.getBytes() var serpk = kp.pubkey.getBytes() var sersig = sig.getBytes() - var seckey = RsaPrivateKey.init(sersk) + discard RsaPrivateKey.init(sersk) var pubkey = RsaPublicKey.init(serpk) var csig = RsaSignature.init(sersig) check csig.verify(message, pubkey) == true @@ -449,7 +451,7 @@ suite "RSA 512/1024/2048/4096 test suite": var sersk = kp.seckey.getBytes() var serpk = kp.pubkey.getBytes() var sersig = sig.getBytes() - var seckey = RsaPrivateKey.init(sersk) + discard RsaPrivateKey.init(sersk) var pubkey = RsaPublicKey.init(serpk) var csig = RsaSignature.init(sersig) check csig.verify(message, pubkey) == true @@ -465,7 +467,7 @@ suite "RSA 512/1024/2048/4096 test suite": var sersk = kp.seckey.getBytes() var serpk = kp.pubkey.getBytes() var sersig = sig.getBytes() - var seckey = RsaPrivateKey.init(sersk) + discard RsaPrivateKey.init(sersk) var pubkey = RsaPublicKey.init(serpk) var csig = RsaSignature.init(sersig) check csig.verify(message, pubkey) == true diff --git a/tests/testsecp256k1.nim b/tests/testsecp256k1.nim index 84cba99fd..26e760865 100644 --- a/tests/testsecp256k1.nim +++ b/tests/testsecp256k1.nim @@ -10,6 +10,8 @@ import unittest import ../libp2p/crypto/secp import nimcrypto/utils +when defined(nimHasUsed): {.used.} + suite "Secp256k1 testing suite": const TestsCount = 20 test "Private key serialize/deserialize test": @@ -62,7 +64,7 @@ suite "Secp256k1 testing suite": var sersk = kp.seckey.getBytes() var serpk = kp.pubkey.getBytes() var sersig = sig.getBytes() - var seckey = SkPrivateKey.init(sersk) + discard SkPrivateKey.init(sersk) var pubkey = SkPublicKey.init(serpk) var csig = SkSignature.init(sersig) check csig.verify(message, pubkey) == true diff --git a/tests/testswitch.nim b/tests/testswitch.nim index c72886595..7e42eccee 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -1,29 +1,31 @@ import unittest, tables, options import chronos, chronicles -import ../libp2p/[switch, +import ../libp2p/[switch, multistream, - protocols/identify, + protocols/identify, connection, transports/transport, transports/tcptransport, - multiaddress, + multiaddress, peerinfo, - crypto/crypto, + crypto/crypto, peer, - protocols/protocol, + protocols/protocol, muxers/muxer, - muxers/mplex/mplex, + muxers/mplex/mplex, muxers/mplex/types, protocols/secure/secio, protocols/secure/secure] +when defined(nimHasUsed): {.used.} + const TestCodec = "/test/proto/1.0.0" type TestProto = ref object of LPProtocol method init(p: TestProto) {.gcsafe.} = - proc handle(conn: Connection, proto: string) {.async, gcsafe.} = + proc handle(conn: Connection, proto: string) {.async, gcsafe.} = let msg = cast[string](await conn.readLp()) check "Hello!" == msg await conn.writeLp("Hello!") @@ -33,7 +35,7 @@ method init(p: TestProto) {.gcsafe.} = p.handler = handle suite "Switch": - test "e2e use switch": + test "e2e use switch": proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) = let seckey = PrivateKey.random(RSA) var peerInfo: PeerInfo @@ -54,7 +56,7 @@ suite "Switch": proc testSwitch(): Future[bool] {.async, gcsafe.} = let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0") let ma2: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0") - + var peerInfo1, peerInfo2: PeerInfo var switch1, switch2: Switch (switch1, peerInfo1) = createSwitch(ma1) diff --git a/tests/testtransport.nim b/tests/testtransport.nim index 756238e41..1c0205bf0 100644 --- a/tests/testtransport.nim +++ b/tests/testtransport.nim @@ -1,11 +1,13 @@ import unittest import chronos -import ../libp2p/[connection, - transports/transport, +import ../libp2p/[connection, + transports/transport, transports/tcptransport, - multiaddress, + multiaddress, wire] +when defined(nimHasUsed): {.used.} + suite "TCP transport": test "test listener: handle write": proc testListener(): Future[bool] {.async, gcsafe.} = diff --git a/tests/testvarint.nim b/tests/testvarint.nim index c60185391..63e6b9184 100644 --- a/tests/testvarint.nim +++ b/tests/testvarint.nim @@ -1,6 +1,8 @@ import unittest import ../libp2p/varint +when defined(nimHasUsed): {.used.} + const PBedgeValues = [ 0'u64, (1'u64 shl 7) - 1'u64, (1'u64 shl 7), (1'u64 shl 14) - 1'u64,