Add multihash test.

This commit is contained in:
cheatfate 2018-12-05 03:01:00 +02:00
parent 53fc4cce41
commit bdcfa1f189
3 changed files with 82 additions and 4 deletions

View File

@ -17,3 +17,4 @@ task test, "Runs the test suite":
exec "nim c -r tests/testbase58"
exec "nim c -r tests/testbase32"
exec "nim c -r tests/testmultiaddress"
exec "nim c -r tests/testmultihash"

View File

@ -491,7 +491,7 @@ proc `==`*[T](mh: MultiHash, mdigest: MDigest[T]): bool =
proc `==`*[T](mdigest: MDigest[T], mh: MultiHash): bool {.inline.} =
## Compares MultiHash with nimcrypto's MDigest[T], returns ``true`` if
## hashes are equal, ``false`` otherwise.
## hashes are equal, ``false`` otherwise.
result = `==`(mh, mdigest)
proc `==`*(a, b: MultiHash): bool =
@ -519,6 +519,3 @@ proc `$`*(value: MultiHash): string =
let digest = toHex(value.data.buffer.toOpenArray(value.dpos,
value.dpos + value.size - 1))
result = multihashName(value.code) & "/" & digest
when isMainModule:
echo MultiHash.init58("QmPGqitiRv1TPuCNtcwsMNRPEhx9SZP5qANPNcQBA53BgM")

80
tests/testmultihash.nim Normal file
View File

@ -0,0 +1,80 @@
import unittest
import ../libp2p/multihash
const
RustTestVectors = [
# TODO: SHA1
# [
# "sha1",
# "beep boop",
# "11147c8357577f51d4f0a8d393aa1aaafb28863d9421"
# ],
[
"sha2-256",
"helloworld",
"1220936A185CAAA266BB9CBE981E9E05CB78CD732B0B3280EB944412BB6F8F8F07AF",
],
[
"sha2-256",
"beep boop",
"122090EA688E275D580567325032492B597BC77221C62493E76330B85DDDA191EF7C",
],
[
"sha2-512",
"hello world",
"1340309ECC489C12D6EB4CC40F50C902F2B4D0ED77EE511A7C7A9BCD3CA86D4CD86F989DD35BC5FF499670DA34255B45B0CFD830E81F605DCF7DC5542E93AE9CD76F"
],
[
"sha3-224",
"hello world",
"171CDFB7F18C77E928BB56FAEB2DA27291BD790BC1045CDE45F3210BB6C5"
],
[
"sha3-256",
"hello world",
"1620644BCC7E564373040999AAC89E7622F3CA71FBA1D972FD94A31C3BFBF24E3938"
],
[
"sha3-384",
"hello world",
"153083BFF28DDE1B1BF5810071C6643C08E5B05BDB836EFFD70B403EA8EA0A634DC4997EB1053AA3593F590F9C63630DD90B"
],
[
"sha3-512",
"hello world",
"1440840006653E9AC9E95117A15C915CAAB81662918E925DE9E004F774FF82D7079A40D4D27B1B372657C61D46D470304C88C788B3A4527AD074D1DCCBEE5DBAA99A"
],
[
"keccak-224",
"hello world",
"1A1C25F3ECFEBABE99686282F57F5C9E1F18244CFEE2813D33F955AAE568"
],
[
"keccak-256",
"hello world",
"1B2047173285A8D7341E5E972FC677286384F802F8EF42A5EC5F03BBFA254CB01FAD"
],
[
"keccak-384",
"hello world",
"1C3065FC99339A2A40E99D3C40D695B22F278853CA0F925CDE4254BCAE5E22ECE47E6441F91B6568425ADC9D95B0072EB49F"
],
[
"keccak-512",
"hello world",
"1D403EE2B40047B8060F68C67242175660F4174D0AF5C01D47168EC20ED619B0B7C42181F40AA1046F39E2EF9EFC6910782A998E0013D172458957957FAC9405B67D"
]
]
suite "MultiHash test suite":
test "rust-multihash test vectors":
for item in RustTestVectors:
var msg = item[1]
var bmsg = cast[seq[byte]](msg)
var mh1 = MultiHash.digest(item[0], bmsg)
var mh2 = MultiHash.init(item[2])
check:
hex(mh1) == item[2]
hex(mh1) == hex(mh2)
mh1 == mh2