chihaya: Add Equal function for Peer & add tests

This commit is contained in:
Josh de Kock 2016-03-31 22:47:06 +01:00
parent e2da9cdc3e
commit 36e533ba02
2 changed files with 52 additions and 0 deletions

View file

@ -72,3 +72,11 @@ type Peer struct {
type Params interface {
String(key string) (string, error)
}
// Equal reports whether peer and x are the same.
func (peer *Peer) Equal(x *Peer) bool {
if peer.ID == x.ID && peer.Port == x.Port && peer.IP.Equal(x.IP) {
return true
}
return false
}

44
chihaya_test.go Normal file
View file

@ -0,0 +1,44 @@
// Copyright 2016 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package chihaya
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
)
var (
peers = []struct {
peerID string
ip string
port uint16
}{
{"-AZ3034-6wfG2wk6wWLc", "250.183.81.177", 5720},
{"-BS5820-oy4La2MWGEFj", "fd45:7856:3dae::48", 2878},
{"-TR0960-6ep6svaa61r4", "fd45:7856:3dae::48", 2878},
{"-BS5820-oy4La2MWGEFj", "fd0a:29a8:8445::38", 2878},
{"-BS5820-oy4La2MWGEFj", "fd45:7856:3dae::48", 8999},
}
builtPeers []*Peer
)
func TestPeerEquality(t *testing.T) {
// Build peers from test data.
for i := 0; i < len(peers); i++ {
builtPeers = append(builtPeers, &Peer{
ID: PeerID(peers[i].peerID),
IP: net.ParseIP(peers[i].ip),
Port: peers[i].port,
})
}
assert.True(t, builtPeers[0].Equal(builtPeers[0]))
assert.False(t, builtPeers[0].Equal(builtPeers[1]))
assert.True(t, builtPeers[1].Equal(builtPeers[1]))
assert.False(t, builtPeers[1].Equal(builtPeers[2]))
assert.False(t, builtPeers[1].Equal(builtPeers[3]))
assert.False(t, builtPeers[1].Equal(builtPeers[4]))
}