forked from cpacia/dnsseeder
-
Notifications
You must be signed in to change notification settings - Fork 4
/
seeder_test.go
88 lines (76 loc) · 1.97 KB
/
seeder_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
package main
import (
"net"
"strconv"
"testing"
"github.com/btcsuite/btcd/wire"
)
func TestGetNonStdIP(t *testing.T) {
var iptests = []struct {
rip string
port uint16
encip string
}{
{"1.2.3.4", 1234, "137.195.4.210"},
{"50.123.45.67", 43210, "101.165.168.202"},
{"202.36.170.3", 65535, "199.31.255.255"},
{"123.213.132.231", 34, "12.91.0.34"},
}
for _, atest := range iptests {
newip := getNonStdIP(net.ParseIP(atest.rip), atest.port)
if newip.String() != atest.encip {
t.Errorf("real-ip: %s real-port: %v encoded-ip: %v expected-ip: %s", atest.rip, atest.port, newip, atest.encip)
}
}
}
func TestAddnNa(t *testing.T) {
// create test data struct
var td = []struct {
ip string
port int
dnsType uint32
}{
{"1.2.3.4", 28333, 1},
{"50.123.45.67", 43210, 2},
}
s := &dnsseeder{
port: 28333,
pver: 1234,
maxSize: 1,
}
s.theList = make(map[string]*node)
for _, atest := range td {
// Test NewNetAddress.
tcpAddr := &net.TCPAddr{
IP: net.ParseIP(atest.ip),
Port: atest.port,
}
na := wire.NewNetAddress(tcpAddr, 0)
ndName := net.JoinHostPort(na.IP.String(), strconv.Itoa(int(na.Port)))
result := s.addNa(na)
if result != true {
t.Errorf("failed to create new node: %s", ndName)
}
if s.theList[ndName].dnsType != atest.dnsType {
t.Errorf("node: %s dnsType:%v expected: %v", ndName, s.theList[ndName].dnsType, atest.dnsType)
}
}
tcpAddr := &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 1234,
}
na := wire.NewNetAddress(tcpAddr, 0)
result := s.addNa(na)
if result != false {
t.Errorf("node added but should have failed as seeder full: %s", net.JoinHostPort(na.IP.String(), strconv.Itoa(int(na.Port))))
}
tcpAddr = &net.TCPAddr{
IP: net.ParseIP("1.2.3.4"),
Port: 28333,
}
na = wire.NewNetAddress(tcpAddr, 0)
result = s.addNa(na)
if result != false {
t.Errorf("node added but should have failed as duplicate: %s", net.JoinHostPort(na.IP.String(), strconv.Itoa(int(na.Port))))
}
}