From 1361c3e981c2a5eb958956ca7eeda5b2183b406e Mon Sep 17 00:00:00 2001 From: Timur Demin Date: Sat, 8 Aug 2020 20:57:56 +0500 Subject: [PATCH] Add tests and benchmarks for vendored code --- crypto_test.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 crypto_test.go diff --git a/crypto_test.go b/crypto_test.go new file mode 100644 index 0000000..90aa4a0 --- /dev/null +++ b/crypto_test.go @@ -0,0 +1,69 @@ +package main + +import ( + "net" + "os" + "regexp" + "testing" + + "github.com/yggdrasil-network/yggdrasil-go/src/address" + "github.com/yggdrasil-network/yggdrasil-go/src/crypto" +) + +var ( + testAddr *address.Address + testPub *crypto.BoxPubKey + testNodeID *crypto.NodeID + testRegex *regexp.Regexp +) + +func TestAddrForNodeID(t *testing.T) { + for i := 20; i > 0; i-- { + pub, _ := crypto.NewBoxKeys() + id := crypto.GetNodeID(pub) + origIP := net.IP(address.AddrForNodeID(id)[:]) + modIP := net.IP(AddrForNodeID(id)[:]) + if !origIP.Equal(modIP) { + t.Errorf("got %s, expected %s", modIP, origIP) + } + } +} + +func TestMain(m *testing.M) { + testPub, _ = crypto.NewBoxKeys() + testNodeID = crypto.GetNodeID(testPub) + testRegex = regexp.MustCompile("::") + os.Exit(m.Run()) +} + +func BenchmarkOrigAddrForNodeID(b *testing.B) { + for i := 0; i < b.N; i++ { + testAddr = address.AddrForNodeID(testNodeID) + } +} + +func BenchmarkModdedAddrForNodeID(b *testing.B) { + for i := 0; i < b.N; i++ { + testAddr = AddrForNodeID(testNodeID) + } +} + +// measures overall performance of code from cmd/genkeys +func BenchmarkOrigLoop(b *testing.B) { + for i := 0; i < b.N; i++ { + pub, _ := crypto.NewBoxKeys() + id := crypto.GetNodeID(pub) + ip := net.IP(address.AddrForNodeID(id)[:]).String() + testRegex.MatchString(ip) + } +} + +// measures overall performance of functions we vendor +func BenchmarkModdedLoop(b *testing.B) { + for i := 0; i < b.N; i++ { + pub, _ := crypto.NewBoxKeys() + id := crypto.GetNodeID(pub) + ip := net.IP(AddrForNodeID(id)[:]).String() + testRegex.MatchString(ip) + } +}