-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpeer_conn_store_test.go
117 lines (85 loc) · 2.36 KB
/
peer_conn_store_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package meshboi
import (
"bytes"
"net"
"testing"
"inet.af/netaddr"
)
type FakeTun struct {
bytes.Buffer
}
func (f *FakeTun) Close() error {
return nil
}
func NewFakePeerConn(inside string, outside string) *PeerConn {
insideIP := netaddr.MustParseIP(inside)
outsideIP := netaddr.MustParseIPPort(outside)
_, client := net.Pipe()
p := NewPeerConn(insideIP, outsideIP, client, &FakeTun{})
return &p
}
type test struct {
insideIP string
outsideIP string
}
var tests = []test{
{insideIP: "192.168.4.1", outsideIP: "192.168.44.1:5000"},
{insideIP: "10.0.0.1", outsideIP: "1.1.1.1:2334"},
{insideIP: "2.2.2.2", outsideIP: "3.4.3.3:2"},
}
func TestGetByIP(t *testing.T) {
store := NewPeerConnStore()
for _, tc := range tests {
pc := NewFakePeerConn(tc.insideIP, tc.outsideIP)
store.Add(pc)
retrievedPeerConn, ok := store.GetByInsideIp(netaddr.MustParseIP(tc.insideIP))
if !ok {
t.Errorf("Couldn't find peer conn by inside IP")
}
if retrievedPeerConn != pc {
t.Errorf("Wrong peer conn returned for inside IP")
}
retrievedPeerConn, ok = store.GetByOutsideIpPort(netaddr.MustParseIPPort(tc.outsideIP))
if !ok {
t.Errorf("Couldn't find peer conn by outside IP")
}
if retrievedPeerConn != pc {
t.Errorf("Wrong peer conn returned for outside IP")
}
}
}
func TestGetNotExisting(t *testing.T) {
store := NewPeerConnStore()
_, ok := store.GetByInsideIp(netaddr.MustParseIP("192.168.1.1"))
if ok {
t.Errorf("Shouldn't have gotten a peer conn back")
}
_, ok = store.GetByOutsideIpPort(netaddr.MustParseIPPort("192.168.1.1:5000"))
if ok {
t.Errorf("Shouldn't have gotten a peer conn back")
}
}
func TestDeleteByIP(t *testing.T) {
store := NewPeerConnStore()
pc := NewFakePeerConn(tests[0].insideIP, tests[0].outsideIP)
store.Add(pc)
ok := store.RemoveByOutsideIPPort(netaddr.MustParseIPPort(tests[0].outsideIP))
if !ok {
t.Errorf("Could not remove peer conn")
}
_, ok = store.GetByInsideIp(netaddr.MustParseIP(tests[0].insideIP))
if ok {
t.Errorf("Found deleted peer")
}
_, ok = store.GetByOutsideIpPort(netaddr.MustParseIPPort(tests[0].outsideIP))
if ok {
t.Errorf("Found deleted peer")
}
}
func TestDeleteNonExistentIP(t *testing.T) {
store := NewPeerConnStore()
ok := store.RemoveByOutsideIPPort(netaddr.MustParseIPPort(tests[0].outsideIP))
if ok {
t.Errorf("Deleting a non existing IP Port shouldn't work")
}
}