forked from jackpal/Taipei-Torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolveBindIP_test.go
69 lines (62 loc) · 1.61 KB
/
resolveBindIP_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
package main
import (
"fmt"
"net"
"strings"
"testing"
)
type bindIPTestData []struct {
in string
out string
}
var bindIPTests = bindIPTestData{
{"", ""},
{"junk", "error"},
{"junk[999]", "error"},
{"192.168.1.10", "192.168.1.10"},
{"2001:db8:85a3::8a2e:370:7334", "2001:db8:85a3::8a2e:370:7334"},
}
func TestResolveBindIP(t *testing.T) {
for _, tt := range bindIPTests {
resolveBindIPAddrCase(t, tt.in, tt.out)
}
}
func resolveBindIPAddrCase(t *testing.T, in string, out string) {
bindIPAddr, err := resolveBindIPAddr(in)
if err != nil {
if out != "error" {
t.Errorf("resolveBindIPAddr(%q) => error %v, want %q", in, err, out)
}
} else {
bindIP := bindIPAddr.String()
if bindIP != out {
t.Errorf("resolveBindIPAddr(%q) => %v, want %q", in, bindIP, out)
}
}
}
func TestDynamicParseBindIP(t *testing.T) {
netIFs, err := net.Interfaces()
if err != nil {
t.Errorf("net.Interfaces() => %v", err)
}
if len(netIFs) == 0 {
t.Errorf("net.Interfaces() => no interfaces")
}
netIF := netIFs[0]
addrs, err := netIFs[0].Addrs()
if err != nil {
t.Errorf("%v.Addrs() => %v", netIF.Name, err)
}
if len(addrs) == 0 {
t.Errorf("%v.Addrs() => no addresses", netIF.Name)
}
resolveBindIPAddrCase(t, netIF.Name, stripMask(addrs[0].String()))
resolveBindIPAddrCase(t, fmt.Sprintf("%v[%d]", netIF.Name, -1), "error")
resolveBindIPAddrCase(t, fmt.Sprintf("%v[%d]", netIF.Name, len(addrs)), "error")
for i, addr := range addrs {
resolveBindIPAddrCase(t, fmt.Sprintf("%v[%d]", netIF.Name, i), stripMask(addr.String()))
}
}
func stripMask(ipnet string) string {
return strings.SplitN(ipnet, "/", 2)[0]
}