mirror of
https://github.com/logos-messaging/go-libp2p-rendezvous.git
synced 2026-01-04 05:43:06 +00:00
test address and cookie packing
This commit is contained in:
parent
8c1227209a
commit
53dfbc7ae1
56
db_test.go
Normal file
56
db_test.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package rendezvous
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"math/rand"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPackAddrs(t *testing.T) {
|
||||||
|
addrs := make([][]byte, 5)
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
addrs[i] = make([]byte, rand.Intn(256))
|
||||||
|
}
|
||||||
|
|
||||||
|
packed := packAddrs(addrs)
|
||||||
|
unpacked, err := unpackAddrs(packed)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(addrs) != len(unpacked) {
|
||||||
|
t.Fatal("unpacked address length mismatch")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, addr := range addrs {
|
||||||
|
if !bytes.Equal(addr, unpacked[i]) {
|
||||||
|
t.Fatal("unpacked addr not equal to original")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPackCookie(t *testing.T) {
|
||||||
|
nonce := make([]byte, 16)
|
||||||
|
_, err := rand.Read(nonce)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
counter := rand.Int63()
|
||||||
|
ns := "blah"
|
||||||
|
|
||||||
|
cookie := packCookie(counter, ns, nonce)
|
||||||
|
|
||||||
|
if !validCookie(cookie, ns, nonce) {
|
||||||
|
t.Fatal("packed an invalid cookie")
|
||||||
|
}
|
||||||
|
|
||||||
|
xcounter, err := unpackCookie(cookie)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if counter != xcounter {
|
||||||
|
t.Fatal("unpacked cookie counter not equal to original")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user