nim-libp2p-experimental/tests/testpeer.nim

157 lines
7.0 KiB
Nim

## Nim-Libp2p
## Copyright (c) 2018 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
## Test vectors was made using Go implementation
## https://github.com/libp2p/go-libp2p-peer
import unittest
import nimcrypto/utils, stew/base58
import ../libp2p/crypto/crypto, ../libp2p/peerid
when defined(nimHasUsed): {.used.}
const
# Test vectors are generated using
# https://github.com/libp2p/go-libp2p-crypto/blob/master/key.go
# https://github.com/libp2p/go-libp2p-peer/blob/master/peer.go
PrivateKeys = [
# RSA512 keys
"""080012BE023082013A020100024100CCE59D6260E7D511DB3DD2C8A3CD5B0F1
D16C8A6FD6A331A211305B677EADF48A7B06F0A74088E96458F23B453EC03A6
63985D32105017A26F7EEE8CF43FAB110203010001024056D5609396B9E9447
E037F56E4845CF8D4BD19201BF96BDAC0F58CD03D95DE35EBB015A88FD8B8CF
731F5604C3895E05A1268D218E64E0C077422AAB3762E801022100F5C1FAAC2
59F7A765C2F7B5639EF537CFAB102989924552C0EB3608936623DC1022100D5
6FAFBD6ED32E7C02D310CC5355EA4B85704D1F73AECCCAB28BDD3BBEC461510
220056286B796EB73A77C459E12399AC0DD9030A8B9E741208CA295D39BAE11
6101022016D390070170427B6ECDE5F104449EFF1EB148FE412003B4F5ED716
6AF5B4F51022100B9F2F54717615B6B34C08571098BAE081B5DDA58A35440BC
BD0222EB250682BB""",
"""080012BD0230820139020100024100A0C957A3EC20A90D511F732ECC650E4EA
EC501EC8349E8891BAF786E6AB424B5665AC229BCB241217FFC51FB7A443C85
688CE0D12A4C9C267B511488B537D2BB0203010001024068AD8CA448F79E77F
007A4AAA4216AA6293C539B52824CA4EEE45768B3896297BABA5CFDAE4D1345
3BF3B7CEC8DF4623693B7CA7F046982A6B0CFEC722338D19022100CEC09F191
08A63E059DE14B057D08D81D6F8583D9CDCB89300A8D11DED084C4D022100C7
15CDBEDAF98D9F0A73DF4880A511B70E17835DA94C32D765B57E88AC827F270
220040684127CDBF4EDDD52119D0D58A628AD02D0D50244D9E322D4269E2A8B
4AD102201316D81F90C794F8C6FBCD511A64B98A1FE3C948D5CEE4A11AB3886
A7A8018C9022075677C2486F633F74E5718681D0C75477B62F0CD14A5C2226A
11DEC5CFA96299""",
"""080012C0023082013C020100024100D6E4788FBF4FCD607D6F61E01E910CE58
14B8BBA5A0F878EDF1C5ACC46A7C3FDECDEEE990D67C3DE5AE4A0301A1621BA
1903938D08451F475393E2C863A8FE230203010001024100980168DE95BF475
197A8F01CEEC23FD3205065F85DB85A754F90E91152715D8AB693A37504D700
703C09F3B171BB5441CD6B5BF462FADDAAB896F2E4047D8681022100DFEE0F7
3DC6C9DC269E44A722007D937238299EEB7D07CE7B527AD9EE40A30E1022100
F5AB120334B11F27C475385B2769448A53D40B47AA48D40AF623C7E4CB525B8
3022100B340507451754CCFAC8E471D9610524BB60C3C867FF3CF987745DCD0
7D00BF61022100DBCAC400CE41B18818C26F3B4E9565D7048CEFD29C4D3038E
662B8D2BFFDDD1502206BC3E38AA82EBA2B99D2FF56AB19B2CE118B9A8A2AD1
AEAF45F3ED4FE957619F""",
# Ed25519 keys
"""08011240B6F99B4E4422C516F1BD135B4D2B02AE62C48388CE31AFBA16496D2
42FABE09BF3848ADABAA9F1E1230A3B94EDD3247C2395397EAFB59790B86595
F94F1CD6B9""",
"""08011240C1F64B208C6D3F52DDDC2CFDB41D5555956C6D6AC6A006C0547C94D
8AD00A639AF87C6EA4451B2C7ACF7E24AB3B8FDE206A984BB0F1C1338CB17AA
F65E944007""",
"""080112401C2228F2880999FEC64401DD33A48C9C56FAF47EEB715CEA57F9F3B
FEAA6E9E132EFF1CABC2A629690CCE7978241315A965F3A1702AC63860BE42D
72265EF250""",
# ECDSA keys
"""08031279307702010104203E5B1FE9712E6C314942A750BD67485DE3C1EFE85
B1BFB520AE8F9AE3DFA4A4CA00A06082A8648CE3D030107A14403420004DE3D
300FA36AE0E8F5D530899D83ABAB44ABF3161F162A4BC901D8E6ECDA020E8B6
D5F8DA30525E71D6851510C098E5C47C646A597FB4DCEC034E9F77C409E62""",
"""080312793077020101042027DD515F39628504923D5A8935A95DE4AECE05AF2
451067A8B584887D67B6799A00A06082A8648CE3D030107A14403420004B544
92DA731A868A0F249288F43BBAEEC5FB166BB430F4AD256399FBD67FD6255BD
5ADE57BA29BC6EF680D66A574788A03EC30B9D2F1C27A483E59FA62F6B03C""",
"""0803127930770201010420746C012FB8E6882BC696AFAAFBCC4B16F8674C1B0
07A7F949EF0D6D485171ACEA00A06082A8648CE3D030107A144034200043E41
50BEB59FEAAC43389ABC490E11172750A94A01D155FE553DA9F559CE6687CDF
6160B6C11BDD02F58D5E28A2BB1C59F991CE52A49618185C82E750A044979""",
# Secp256k1 keys
"0802122053DADF1D5A164D6B4ACDB15E24AA4C5B1D3461BDBD42ABEDB0A4404D56CED8FB",
"08021220FD659951E2ED440CC7ECE436357D123D4C8B3CF1056E3F1607FF3641FB578A1B",
"08021220B333BE3E843339E0E2CE9E083ABC119BE05C7B65B8665ADE19E172D47BF91305"
]
PeerIDs = [
"Qmdxy8GAu1pvi35xBAie9sMpMN4G9p6GK6WCNbSCDCDgyp",
"QmczLMwRH4cNaLZx1t7PTe5b7k6xKkd3RCJZSxmG3azXJK",
"QmS9Sg4ZA5Fd1hHjNNkqfuMUXX9QnEXxgvT5hfSRuKHp8p",
"12D3KooWSCxTfVvMBJbpF75PQmnFdxdBfC1ZxAGYbFc3U9MjALXz",
"12D3KooWMdZbdEudjgnCvQLoSoiqhQ4ET2gaA1d4JpC1CBkUnfzn",
"12D3KooWDFCm93uCnm8tVdk3DYxNeMFxMGBaywVSt8a8ULtdLoeX",
"QmVMT29id3TUASyfZZ6k9hmNyc2nYabCo4uMSpDw4zrgDk",
"QmXz4wPSQqYF33qB7JRdSExETu56HgWRpE9bsf75HgeXL5",
"Qmcfz2MaPjw44RfVpHKFgXwhW3uFBRBxByVEkgPhefKCJW",
"16Uiu2HAmLhLvBoYaoZfaMUKuibM6ac163GwKY74c5kiSLg5KvLpY",
"16Uiu2HAmRRrT319h5upVoC3E8vs1Qej4UF3vPPnLgrhbpHhUb2Av",
"16Uiu2HAmDrDaty3uYPgqSr1h5Cup32S2UdYo46rhqZfXPjJMABZL"
]
suite "Peer testing suite":
test "Go PeerID test vectors":
for i in 0..<len(PrivateKeys):
var seckey = PrivateKey.init(stripSpaces(PrivateKeys[i])).get()
var pubkey = seckey.getKey().get()
var p1 = PeerID.init(seckey).get()
var p2 = PeerID.init(pubkey).get()
var p3 = PeerID.init(PeerIDs[i]).get()
var b1 = Base58.decode(PeerIDs[i])
var p4 = PeerID.init(b1).get()
var buf1 = newSeq[byte](len(p1))
var buf2 = newSeq[byte](len(p2))
var buf3 = newSeq[byte](len(p3))
var buf4 = newSeq[byte](len(p4))
check:
p1 == p3
p2 == p3
p4 == p3
p1 == p2
p1 == p4
p2 == p4
p1.pretty() == PeerIDs[i]
p2.pretty() == PeerIDs[i]
p3.pretty() == PeerIDs[i]
p4.pretty() == PeerIDs[i]
p1.match(seckey) == true
p1.match(pubkey) == true
p1.getBytes() == p2.getBytes()
p1.getBytes() == p3.getBytes()
p1.getBytes() == p4.getBytes()
p1.toBytes(buf1) == len(p1)
p1.toBytes(buf2) == len(p2)
p1.toBytes(buf3) == len(p3)
p1.toBytes(buf4) == len(p4)
p1.validate() == true
p2.validate() == true
p3.validate() == true
p4.validate() == true
$p1 == $p2
$p1 == $p3
$p1 == $p4
if i in {3, 4, 5}:
var ekey1, ekey2, ekey3, ekey4: PublicKey
check:
p1.hasPublicKey() == true
p2.hasPublicKey() == true
p3.hasPublicKey() == true
p4.hasPublicKey() == true
p1.extractPublicKey(ekey1) == true
p2.extractPublicKey(ekey2) == true
p3.extractPublicKey(ekey3) == true
p4.extractPublicKey(ekey4) == true
ekey1 == pubkey
ekey2 == pubkey
ekey3 == pubkey
ekey4 == pubkey