From 36e533ba02c074a3014e332dcb44e4faf91bf2cc Mon Sep 17 00:00:00 2001 From: Josh de Kock Date: Thu, 31 Mar 2016 22:47:06 +0100 Subject: [PATCH] chihaya: Add Equal function for Peer & add tests --- chihaya.go | 8 ++++++++ chihaya_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 chihaya_test.go diff --git a/chihaya.go b/chihaya.go index 7daba32..6ed4027 100644 --- a/chihaya.go +++ b/chihaya.go @@ -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 +} diff --git a/chihaya_test.go b/chihaya_test.go new file mode 100644 index 0000000..0186c52 --- /dev/null +++ b/chihaya_test.go @@ -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])) +}