mirror of
https://github.com/vacp2p/nim-libp2p-experimental.git
synced 2025-01-27 18:45:06 +00:00
Allow custom identify agent string (#451)
* allow custom agent version string * rework tests and add test for custom agent version
This commit is contained in:
parent
164892776b
commit
7b1e652224
@ -55,7 +55,10 @@ proc encodeMsg*(peerInfo: PeerInfo, observedAddr: Multiaddress): ProtoBuffer =
|
|||||||
result.write(4, observedAddr.data.buffer)
|
result.write(4, observedAddr.data.buffer)
|
||||||
let protoVersion = ProtoVersion
|
let protoVersion = ProtoVersion
|
||||||
result.write(5, protoVersion)
|
result.write(5, protoVersion)
|
||||||
let agentVersion = AgentVersion
|
let agentVersion = if peerInfo.agentVersion.len <= 0:
|
||||||
|
AgentVersion
|
||||||
|
else:
|
||||||
|
peerInfo.agentVersion
|
||||||
result.write(6, agentVersion)
|
result.write(6, agentVersion)
|
||||||
result.finish()
|
result.finish()
|
||||||
|
|
||||||
|
@ -49,21 +49,21 @@ template checkTrackers*() =
|
|||||||
template asyncTeardown*(body: untyped): untyped =
|
template asyncTeardown*(body: untyped): untyped =
|
||||||
teardown:
|
teardown:
|
||||||
waitFor((
|
waitFor((
|
||||||
proc() {.async.} =
|
proc() {.async, gcsafe.} =
|
||||||
body
|
body
|
||||||
)())
|
)())
|
||||||
|
|
||||||
template asyncSetup*(body: untyped): untyped =
|
template asyncSetup*(body: untyped): untyped =
|
||||||
setup:
|
setup:
|
||||||
waitFor((
|
waitFor((
|
||||||
proc() {.async.} =
|
proc() {.async, gcsafe.} =
|
||||||
body
|
body
|
||||||
)())
|
)())
|
||||||
|
|
||||||
template asyncTest*(name: string, body: untyped): untyped =
|
template asyncTest*(name: string, body: untyped): untyped =
|
||||||
test name:
|
test name:
|
||||||
waitFor((
|
waitFor((
|
||||||
proc() {.async.} =
|
proc() {.async, gcsafe.} =
|
||||||
body
|
body
|
||||||
)())
|
)())
|
||||||
|
|
||||||
|
@ -17,80 +17,103 @@ suite "Identify":
|
|||||||
teardown:
|
teardown:
|
||||||
checkTrackers()
|
checkTrackers()
|
||||||
|
|
||||||
asyncTest "handle identify message":
|
suite "handle identify message":
|
||||||
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
var
|
||||||
let remoteSecKey = PrivateKey.random(ECDSA, rng[]).get()
|
ma {.threadvar.}: MultiAddress
|
||||||
let remotePeerInfo = PeerInfo.init(
|
remoteSecKey {.threadvar.}: PrivateKey
|
||||||
remoteSecKey, [ma], ["/test/proto1/1.0.0", "/test/proto2/1.0.0"])
|
remotePeerInfo {.threadvar.}: PeerInfo
|
||||||
var serverFut: Future[void]
|
serverFut {.threadvar.}: Future[void]
|
||||||
let identifyProto1 = newIdentify(remotePeerInfo)
|
acceptFut {.threadvar.}: Future[void]
|
||||||
let msListen = newMultistream()
|
identifyProto1 {.threadvar.}: Identify
|
||||||
|
identifyProto2 {.threadvar.}: Identify
|
||||||
|
transport1 {.threadvar.}: Transport
|
||||||
|
transport2 {.threadvar.}: Transport
|
||||||
|
msListen {.threadvar.}: MultistreamSelect
|
||||||
|
msDial {.threadvar.}: MultistreamSelect
|
||||||
|
conn {.threadvar.}: Connection
|
||||||
|
|
||||||
msListen.addHandler(IdentifyCodec, identifyProto1)
|
asyncSetup:
|
||||||
|
ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
||||||
|
remoteSecKey = PrivateKey.random(ECDSA, rng[]).get()
|
||||||
|
remotePeerInfo = PeerInfo.init(
|
||||||
|
remoteSecKey, [ma], ["/test/proto1/1.0.0", "/test/proto2/1.0.0"])
|
||||||
|
|
||||||
var transport1 = TcpTransport.init()
|
transport1 = TcpTransport.init()
|
||||||
serverFut = transport1.start(ma)
|
transport2 = TcpTransport.init()
|
||||||
|
|
||||||
proc acceptHandler(): Future[void] {.async, gcsafe.} =
|
identifyProto1 = newIdentify(remotePeerInfo)
|
||||||
let conn = await transport1.accept()
|
identifyProto2 = newIdentify(remotePeerInfo)
|
||||||
await msListen.handle(conn)
|
|
||||||
|
|
||||||
let acceptFut = acceptHandler()
|
msListen = newMultistream()
|
||||||
let msDial = newMultistream()
|
msDial = newMultistream()
|
||||||
let transport2: TcpTransport = TcpTransport.init()
|
|
||||||
let conn = await transport2.dial(transport1.ma)
|
|
||||||
|
|
||||||
var peerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get(), [ma])
|
asyncTeardown:
|
||||||
let identifyProto2 = newIdentify(peerInfo)
|
await conn.close()
|
||||||
discard await msDial.select(conn, IdentifyCodec)
|
await acceptFut
|
||||||
let id = await identifyProto2.identify(conn, remotePeerInfo)
|
await transport1.stop()
|
||||||
|
await serverFut
|
||||||
|
await transport2.stop()
|
||||||
|
|
||||||
check id.pubKey.get() == remoteSecKey.getKey().get()
|
asyncTest "default agent version":
|
||||||
check id.addrs[0] == ma
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
||||||
check id.protoVersion.get() == ProtoVersion
|
serverFut = transport1.start(ma)
|
||||||
# check id.agentVersion.get() == AgentVersion
|
proc acceptHandler(): Future[void] {.async, gcsafe.} =
|
||||||
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
let c = await transport1.accept()
|
||||||
|
await msListen.handle(c)
|
||||||
|
|
||||||
await conn.close()
|
acceptFut = acceptHandler()
|
||||||
await acceptFut
|
conn = await transport2.dial(transport1.ma)
|
||||||
await transport1.stop()
|
|
||||||
await serverFut
|
|
||||||
await transport2.stop()
|
|
||||||
|
|
||||||
asyncTest "handle failed identify":
|
|
||||||
let ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
||||||
var remotePeerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get(), [ma])
|
|
||||||
let identifyProto1 = newIdentify(remotePeerInfo)
|
|
||||||
let msListen = newMultistream()
|
|
||||||
|
|
||||||
msListen.addHandler(IdentifyCodec, identifyProto1)
|
|
||||||
|
|
||||||
let transport1: TcpTransport = TcpTransport.init()
|
|
||||||
asyncCheck transport1.start(ma)
|
|
||||||
|
|
||||||
proc acceptHandler() {.async.} =
|
|
||||||
var conn: Connection
|
|
||||||
try:
|
|
||||||
conn = await transport1.accept()
|
|
||||||
await msListen.handle(conn)
|
|
||||||
except CatchableError:
|
|
||||||
discard
|
|
||||||
finally:
|
|
||||||
await conn.close()
|
|
||||||
|
|
||||||
let acceptFut = acceptHandler()
|
|
||||||
let msDial = newMultistream()
|
|
||||||
let transport2: TcpTransport = TcpTransport.init()
|
|
||||||
let conn = await transport2.dial(transport1.ma)
|
|
||||||
var localPeerInfo = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get(), [ma])
|
|
||||||
let identifyProto2 = newIdentify(localPeerInfo)
|
|
||||||
|
|
||||||
expect IdentityNoMatchError:
|
|
||||||
let pi2 = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get())
|
|
||||||
discard await msDial.select(conn, IdentifyCodec)
|
discard await msDial.select(conn, IdentifyCodec)
|
||||||
discard await identifyProto2.identify(conn, pi2)
|
let id = await identifyProto2.identify(conn, remotePeerInfo)
|
||||||
|
|
||||||
await conn.close()
|
check id.pubKey.get() == remoteSecKey.getKey().get()
|
||||||
await acceptFut.wait(5000.millis) # when no issues will not wait that long!
|
check id.addrs[0] == ma
|
||||||
await transport2.stop()
|
check id.protoVersion.get() == ProtoVersion
|
||||||
await transport1.stop()
|
check id.agentVersion.get() == AgentVersion
|
||||||
|
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
||||||
|
|
||||||
|
asyncTest "custom agent version":
|
||||||
|
const customAgentVersion = "MY CUSTOM AGENT STRING"
|
||||||
|
remotePeerInfo.agentVersion = customAgentVersion
|
||||||
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
||||||
|
|
||||||
|
serverFut = transport1.start(ma)
|
||||||
|
|
||||||
|
proc acceptHandler(): Future[void] {.async, gcsafe.} =
|
||||||
|
let c = await transport1.accept()
|
||||||
|
await msListen.handle(c)
|
||||||
|
|
||||||
|
acceptFut = acceptHandler()
|
||||||
|
conn = await transport2.dial(transport1.ma)
|
||||||
|
|
||||||
|
discard await msDial.select(conn, IdentifyCodec)
|
||||||
|
let id = await identifyProto2.identify(conn, remotePeerInfo)
|
||||||
|
|
||||||
|
check id.pubKey.get() == remoteSecKey.getKey().get()
|
||||||
|
check id.addrs[0] == ma
|
||||||
|
check id.protoVersion.get() == ProtoVersion
|
||||||
|
check id.agentVersion.get() == customAgentVersion
|
||||||
|
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
||||||
|
|
||||||
|
asyncTest "handle failed identify":
|
||||||
|
msListen.addHandler(IdentifyCodec, identifyProto1)
|
||||||
|
asyncCheck transport1.start(ma)
|
||||||
|
|
||||||
|
proc acceptHandler() {.async.} =
|
||||||
|
var conn: Connection
|
||||||
|
try:
|
||||||
|
conn = await transport1.accept()
|
||||||
|
await msListen.handle(conn)
|
||||||
|
except CatchableError:
|
||||||
|
discard
|
||||||
|
finally:
|
||||||
|
await conn.close()
|
||||||
|
|
||||||
|
acceptFut = acceptHandler()
|
||||||
|
conn = await transport2.dial(transport1.ma)
|
||||||
|
|
||||||
|
expect IdentityNoMatchError:
|
||||||
|
let pi2 = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get())
|
||||||
|
discard await msDial.select(conn, IdentifyCodec)
|
||||||
|
discard await identifyProto2.identify(conn, pi2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user