From 622cfce1ad3f043838a86f00ebced924efee78a1 Mon Sep 17 00:00:00 2001 From: kaiyou Date: Thu, 7 May 2020 11:52:50 +0200 Subject: [PATCH] Add unit tests for node encoding/decoding --- common/node_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 common/node_test.go diff --git a/common/node_test.go b/common/node_test.go new file mode 100644 index 0000000..240677f --- /dev/null +++ b/common/node_test.go @@ -0,0 +1,28 @@ +package common + +import ( + "net" + "reflect" + "testing" +) + +func Test_Node_Encode_Decode(t *testing.T) { + pubKey := "abcdefghijklmnopkqstuvwxyzABCDEF" + _, ipv4, _ := net.ParseCIDR("10.0.0.1/32") + _, ipv6, _ := net.ParseCIDR("2001:db8::1/128") + + for _, ip := range []*net.IPNet{ipv4, ipv6} { + node := Node{ + nodeMeta: nodeMeta{ + OverlayAddr: *ip, + PubKey: pubKey, + }, + } + encoded, _ := node.Encode(1024) + new := Node{Meta: encoded} + new.Decode() + if !reflect.DeepEqual(node.nodeMeta, new.nodeMeta) { + t.Errorf("node encoding then decoding mismatch: %s / %s", node.nodeMeta, new.nodeMeta) + } + } +}