mirror of https://github.com/status-im/nim-eth.git
Add Packet encodings tests
This commit is contained in:
parent
5daaf73d2e
commit
79dfe88ec8
|
@ -56,7 +56,8 @@ proc runP2pTests() =
|
|||
"test_waku_mail",
|
||||
"test_protocol_handlers",
|
||||
"test_enr",
|
||||
"test_discoveryv5"
|
||||
"test_discoveryv5",
|
||||
"test_discv5_encoding"
|
||||
]:
|
||||
runTest("tests/p2p/" & filename)
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ const
|
|||
idNoncePrefix = "discovery-id-nonce"
|
||||
gcmNonceSize* = 12
|
||||
keyAgreementPrefix = "discovery v5 key agreement"
|
||||
authSchemeName = "gcm"
|
||||
authSchemeName* = "gcm"
|
||||
|
||||
type
|
||||
AuthResponse = object
|
||||
|
@ -25,12 +25,12 @@ type
|
|||
readKey: array[16, byte]
|
||||
authRespKey: array[16, byte]
|
||||
|
||||
AuthHeader = object
|
||||
auth: array[12, byte]
|
||||
idNonce: array[32, byte]
|
||||
scheme: string
|
||||
ephemeralKey: array[64, byte]
|
||||
response: seq[byte]
|
||||
AuthHeader* = object
|
||||
auth*: array[12, byte]
|
||||
idNonce*: array[32, byte]
|
||||
scheme*: string
|
||||
ephemeralKey*: array[64, byte]
|
||||
response*: seq[byte]
|
||||
|
||||
|
||||
const
|
||||
|
|
|
@ -5,6 +5,85 @@ import eth/rlp, stew/byteutils
|
|||
# According to test vectors:
|
||||
# https://github.com/ethereum/devp2p/blob/master/discv5/discv5-wire-test-vectors.md
|
||||
|
||||
suite "Discovery v5 Packet Encodings":
|
||||
# TODO: These tests are currently not completely representative for the code
|
||||
# and thus will not necessarily notice failures. Refactor/restructure code
|
||||
# where possible to make this more useful.
|
||||
test "Random Packet":
|
||||
const
|
||||
# input
|
||||
tag = "0x0101010101010101010101010101010101010101010101010101010101010101"
|
||||
authTag = "0x020202020202020202020202"
|
||||
randomData = "0x0404040404040404040404040404040404040404040404040404040404040404040404040404040404040404"
|
||||
# expected output
|
||||
randomPacketRlp = "0x01010101010101010101010101010101010101010101010101010101010101018c0202020202020202020202020404040404040404040404040404040404040404040404040404040404040404040404040404040404040404"
|
||||
|
||||
var data: seq[byte]
|
||||
data.add(hexToByteArray[32](tag))
|
||||
data.add(rlp.encode(hexToByteArray[12](authTag)))
|
||||
data.add(hexToSeqByte(randomData))
|
||||
|
||||
check data == hexToSeqByte(randomPacketRlp)
|
||||
|
||||
test "WHOAREYOU Packet":
|
||||
const
|
||||
# input
|
||||
magic = "0x0101010101010101010101010101010101010101010101010101010101010101"
|
||||
token = "0x020202020202020202020202"
|
||||
idNonce = "0x0303030303030303030303030303030303030303030303030303030303030303"
|
||||
enrSeq = 0x01'u64
|
||||
# expected output
|
||||
whoareyouPacketRlp = "0x0101010101010101010101010101010101010101010101010101010101010101ef8c020202020202020202020202a0030303030303030303030303030303030303030303030303030303030303030301"
|
||||
|
||||
let challenge = Whoareyou(authTag: hexToByteArray[12](token),
|
||||
idNonce: hexToByteArray[32](idNonce),
|
||||
recordSeq: enrSeq)
|
||||
var data = hexToSeqByte(magic)
|
||||
data.add(rlp.encode(challenge[]))
|
||||
|
||||
check data == hexToSeqByte(whoareyouPacketRlp)
|
||||
|
||||
test "Authenticated Message Packet":
|
||||
const
|
||||
# input
|
||||
tag = "0x93a7400fa0d6a694ebc24d5cf570f65d04215b6ac00757875e3f3a5f42107903"
|
||||
authTag = "0x27b5af763c446acd2749fe8e"
|
||||
idNonce = "0xe551b1c44264ab92bc0b3c9b26293e1ba4fed9128f3c3645301e8e119f179c65"
|
||||
ephemeralPubkey = "0xb35608c01ee67edff2cffa424b219940a81cf2fb9b66068b1cf96862a17d353e22524fbdcdebc609f85cbd58ebe7a872b01e24a3829b97dd5875e8ffbc4eea81"
|
||||
authRespCiphertext = "0x570fbf23885c674867ab00320294a41732891457969a0f14d11c995668858b2ad731aa7836888020e2ccc6e0e5776d0d4bc4439161798565a4159aa8620992fb51dcb275c4f755c8b8030c82918898f1ac387f606852"
|
||||
messageCiphertext = "0xa5d12a2d94b8ccb3ba55558229867dc13bfa3648"
|
||||
# expected output
|
||||
authMessageRlp = "0x93a7400fa0d6a694ebc24d5cf570f65d04215b6ac00757875e3f3a5f42107903f8cc8c27b5af763c446acd2749fe8ea0e551b1c44264ab92bc0b3c9b26293e1ba4fed9128f3c3645301e8e119f179c658367636db840b35608c01ee67edff2cffa424b219940a81cf2fb9b66068b1cf96862a17d353e22524fbdcdebc609f85cbd58ebe7a872b01e24a3829b97dd5875e8ffbc4eea81b856570fbf23885c674867ab00320294a41732891457969a0f14d11c995668858b2ad731aa7836888020e2ccc6e0e5776d0d4bc4439161798565a4159aa8620992fb51dcb275c4f755c8b8030c82918898f1ac387f606852a5d12a2d94b8ccb3ba55558229867dc13bfa3648"
|
||||
|
||||
let authHeader = AuthHeader(auth: hexToByteArray[12](authTag),
|
||||
idNonce: hexToByteArray[32](idNonce),
|
||||
scheme: authSchemeName,
|
||||
ephemeralKey: hexToByteArray[64](ephemeralPubkey),
|
||||
response: hexToSeqByte(authRespCiphertext))
|
||||
|
||||
var data: seq[byte]
|
||||
data.add(hexToSeqByte(tag))
|
||||
data.add(rlp.encode(authHeader))
|
||||
data.add(hexToSeqByte(messageCiphertext))
|
||||
|
||||
check data == hexToSeqByte(authMessageRlp)
|
||||
|
||||
test "Message Packet":
|
||||
const
|
||||
# input
|
||||
tag = "0x93a7400fa0d6a694ebc24d5cf570f65d04215b6ac00757875e3f3a5f42107903"
|
||||
authTag = "0x27b5af763c446acd2749fe8e"
|
||||
randomData = "0xa5d12a2d94b8ccb3ba55558229867dc13bfa3648"
|
||||
# expected output
|
||||
messageRlp = "0x93a7400fa0d6a694ebc24d5cf570f65d04215b6ac00757875e3f3a5f421079038c27b5af763c446acd2749fe8ea5d12a2d94b8ccb3ba55558229867dc13bfa3648"
|
||||
|
||||
var data: seq[byte]
|
||||
data.add(hexToByteArray[32](tag))
|
||||
data.add(rlp.encode(hexToByteArray[12](authTag)))
|
||||
data.add(hexToSeqByte(randomData))
|
||||
|
||||
check data == hexToSeqByte(messageRlp)
|
||||
|
||||
suite "Discovery v5 Protocol Message Encodings":
|
||||
test "Ping Request":
|
||||
var p: PingPacket
|
||||
|
|
Loading…
Reference in New Issue