Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Add tests and benchmarks for vendored code
  • Loading branch information
tdemin committed Aug 8, 2020
1 parent ca28095 commit 1361c3e
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions 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)
}
}

0 comments on commit 1361c3e

Please sign in to comment.