Skip to content

Commit

Permalink
Add tests and remove Duplicated Hex() for identity
Browse files Browse the repository at this point in the history
  • Loading branch information
abbychau committed Jun 25, 2018
1 parent 97d49da commit d5fbc7b
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
noise.exe
noise
chat

vendor
3 changes: 2 additions & 1 deletion crypto/hash.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package crypto

import (
"golang.org/x/crypto/blake2b"
"math/big"

"golang.org/x/crypto/blake2b"
)

func Hash(s *big.Int) *big.Int {
Expand Down
32 changes: 32 additions & 0 deletions crypto/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package crypto

import (
"bytes"
"fmt"
"math/big"
"testing"
)

func TestHash(t *testing.T) {
r := Hash(big.NewInt(123))

n := new(big.Int)
n, ok := n.SetString("89391711502145780362310349925943903708999319576398061903082165979787487688967", 10)
if ok {
if n.String() != r.String() {
fmt.Printf("%v \n%v \n", r, n)
panic("Hash error")
}
} else {
panic("Big Int error")
}
}

func TestHashBytes(t *testing.T) {
r := HashBytes([]byte("123"))
n := []byte{245, 214, 123, 174, 115, 176, 225, 13, 13, 253, 48, 67, 179, 244, 241, 0, 173, 160, 20, 197, 195, 123, 213, 206, 151, 129, 59, 19, 245, 171, 43, 207}
if !bytes.Equal(n, r) {
fmt.Printf("%v \n%v \n", r, n)
panic("Hash error")
}
}
12 changes: 6 additions & 6 deletions dht/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (t *RoutingTable) Update(target peer.ID) {
// Returns an unique list of all peers within the routing network (excluding yourself).
func (t *RoutingTable) GetPeers() (peers []peer.ID) {
visited := make(map[string]struct{})
visited[t.self.Hex()] = struct{}{}
visited[t.self.PublicKeyHex()] = struct{}{}

t.buckets.Range(func(key, value interface{}) bool {
bucket := value.(*Bucket)
Expand All @@ -86,9 +86,9 @@ func (t *RoutingTable) GetPeers() (peers []peer.ID) {

for e := bucket.Front(); e != nil; e = e.Next() {
id := e.Value.(peer.ID)
if _, seen := visited[id.Hex()]; !seen {
if _, seen := visited[id.PublicKeyHex()]; !seen {
peers = append(peers, id)
visited[id.Hex()] = struct{}{}
visited[id.PublicKeyHex()] = struct{}{}
}
}

Expand All @@ -102,7 +102,7 @@ func (t *RoutingTable) GetPeers() (peers []peer.ID) {
// Returns an unique list of all peer addresses within the routing network.
func (t *RoutingTable) GetPeerAddresses() (peers []string) {
visited := make(map[string]struct{})
visited[t.self.Hex()] = struct{}{}
visited[t.self.PublicKeyHex()] = struct{}{}

t.buckets.Range(func(key, value interface{}) bool {
bucket := value.(*Bucket)
Expand All @@ -111,9 +111,9 @@ func (t *RoutingTable) GetPeerAddresses() (peers []string) {

for e := bucket.Front(); e != nil; e = e.Next() {
id := e.Value.(peer.ID)
if _, seen := visited[id.Hex()]; !seen {
if _, seen := visited[id.PublicKeyHex()]; !seen {
peers = append(peers, id.Address)
visited[id.Hex()] = struct{}{}
visited[id.PublicKeyHex()] = struct{}{}
}
}

Expand Down
3 changes: 3 additions & 0 deletions dht/routes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dht

//TODO make function testable
9 changes: 5 additions & 4 deletions network/discovery/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package discovery

import (
"context"
"sync"

"github.com/perlin-network/noise/log"
"github.com/perlin-network/noise/network"
"github.com/perlin-network/noise/peer"
"github.com/perlin-network/noise/protobuf"
"sync"
)

func bootstrapPeers(network *network.Network, target peer.ID, count int) (addresses []string, publicKeys [][]byte) {
queue := []peer.ID{target}

visited := make(map[string]struct{})
visited[network.Keys.PublicKeyHex()] = struct{}{}
visited[target.Hex()] = struct{}{}
visited[target.PublicKeyHex()] = struct{}{}

for len(queue) > 0 {
var wait sync.WaitGroup
Expand Down Expand Up @@ -69,9 +70,9 @@ func bootstrapPeers(network *network.Network, target peer.ID, count int) (addres
for _, id := range response.Peers {
p := peer.ID(*id)

if _, seen := visited[p.Hex()]; !seen {
if _, seen := visited[p.PublicKeyHex()]; !seen {
queue = append(queue, p)
visited[p.Hex()] = struct{}{}
visited[p.PublicKeyHex()] = struct{}{}

addresses = append(addresses, p.Address)

Expand Down
11 changes: 5 additions & 6 deletions peer/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/hex"
"fmt"

"github.com/perlin-network/noise/protobuf"
"golang.org/x/crypto/ed25519"
)
Expand Down Expand Up @@ -31,10 +32,12 @@ func (id ID) Less(other interface{}) bool {
return false
}

func (id ID) Hex() string {
return hex.EncodeToString(id.PublicKey[:])
// PublicKeyHex generate EncodeToString return of Public Key
func (id ID) PublicKeyHex() string {
return hex.EncodeToString(id.PublicKey)
}

// Xor (^) operation of public key
func (id ID) Xor(other ID) ID {
var result [IdSize]byte
for i := 0; i < IdSize; i++ {
Expand All @@ -54,7 +57,3 @@ func (id ID) PrefixLen() int {
}
return IdSize*8 - 1
}

func (id ID) PublicKeyHex() string {
return hex.EncodeToString(id.PublicKey)
}
61 changes: 61 additions & 0 deletions peer/identity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package peer

import (
"bytes"
"fmt"
"testing"

"github.com/perlin-network/noise/peer"
)

func TestID(t *testing.T) {

testPublicKey := []byte("12345678901234567890123456789012")
testPublicKey1 := []byte("12345678901234567890123456789011")
testPublicKey2 := []byte("12345678901234567890123456789013")
testAddr := "localhost:12345"

id := peer.CreateID(testAddr, testPublicKey)

if !bytes.Equal(id.PublicKey, testPublicKey) {
fmt.Printf("%s \n%s", id.PublicKey, testPublicKey)
panic("Wrong Public Key")
}

if id.Address != testAddr {
panic("Wrong Address")
}

if id.String() != "ID{PublicKey: [49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 54 55 56 57 48 49 50], Address: localhost:12345}" {
fmt.Printf(id.String())
panic("String() error")
}

if !id.Equals(peer.CreateID(testAddr, testPublicKey)) {
panic("Equals() error")
}

if id.Less(peer.CreateID(testAddr, testPublicKey1)) {
panic("Less() error 1")
}

if !id.Less(peer.CreateID(testAddr, testPublicKey2)) {
panic("Less() error 2")
}

if id.PublicKeyHex() != "3132333435363738393031323334353637383930313233343536373839303132" {
fmt.Print(id.PublicKeyHex())
panic("PublicKeyHex() error or hex.EncodeToString() changed defination?")
}

comparee := peer.CreateID(
testAddr,
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
)

if !comparee.Equals(id.Xor(peer.CreateID(testAddr, testPublicKey2))) {
fmt.Printf("%v\n%v", comparee, id.Xor(peer.CreateID(testAddr, testPublicKey2)))
panic("Xor() error")
}

}

0 comments on commit d5fbc7b

Please sign in to comment.