From 53dfbc7ae159df6c601eda939133a06ddd3598bb Mon Sep 17 00:00:00 2001 From: vyzo Date: Tue, 24 Apr 2018 12:34:39 +0300 Subject: [PATCH] test address and cookie packing --- db_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 db_test.go diff --git a/db_test.go b/db_test.go new file mode 100644 index 0000000..f65949c --- /dev/null +++ b/db_test.go @@ -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") + } +}