-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathhosts_test.go
183 lines (165 loc) · 4.74 KB
/
hosts_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2018 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package appnet
import (
"fmt"
"sort"
"testing"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/snet"
)
func init() {
// Trick into thinking that /etc/hosts is already loaded
loadHostsOnce.Do(func() {
hostsTableInstance = loadHostsFile("hosts_test_file")
})
}
func TestCount(t *testing.T) {
count := len(hosts().byName)
if count != 5 {
t.Errorf("wrong number of hosts in map, expected: %v, got: %v", 5, count)
}
count = len(hosts().byAddr["17-ffaa:0:1,[192.168.1.1]"])
if count != 3 {
t.Errorf("wrong number of addresses in list, expected: %v, got: %v", 3, count)
}
}
func TestAddingHost(t *testing.T) {
host := "host5"
addr := "20-ffaa:3:4,[12.34.56.0]"
// can add new host/addr
err := AddHost(host, addr)
if err != nil {
t.Error(err)
}
// cannot add host twice
err = AddHost(host, addr)
if err == nil {
t.Error("added host with same name twice")
}
// can add different host with same address
host = "host6"
err = AddHost(host, addr)
if err != nil {
t.Error(err)
}
}
func TestReadHosts(t *testing.T) {
cases := []struct {
name string
expected snet.SCIONAddress
}{
{"host1.1", mustParse("17-ffaa:0:1,[192.168.1.1]")},
{"host1.2", mustParse("17-ffaa:0:1,[192.168.1.1]")},
{"host2", mustParse("18-ffaa:1:2,[10.0.8.10]")},
{"host3", mustParse("17-ffaa:0:1,[192.168.1.1]")},
{"host4", mustParse("20-ffaa:c0ff:ee12,[::ff1:ce00:dead:10cc:baad:f00d]")},
{"commented", snet.SCIONAddress{}},
{"dummy1", snet.SCIONAddress{}},
{"dummy2", snet.SCIONAddress{}},
}
for _, c := range cases {
actual, err := GetHostByName(c.name)
if c.expected.Host == nil {
if err == nil {
t.Errorf("no result expected for '%s', got %v", c.name, actual)
}
} else {
if err != nil {
t.Error(err)
}
if c.expected.IA != actual.IA || !c.expected.Host.Equal(actual.Host) {
t.Errorf("wrong result for '%s', expected %v, got %v", c.name, c.expected, actual)
}
}
}
}
func TestReadAddresses(t *testing.T) {
equalSet := func(a, b []string) bool {
sort.Strings(a)
sort.Strings(b)
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
cases := []struct {
addr snet.SCIONAddress
expected []string
}{
{mustParse("18-ffaa:1:2,[10.0.8.10]"), []string{"host2"}},
{mustParse("17-ffaa:0:1,[192.168.1.1]"), []string{"host1.1", "host1.2", "host3"}},
{mustParse("20-ffaa:c0ff:ee12,[::ff1:ce00:dead:10cc:baad:f00d]"), []string{"host4"}},
{mustParse("1-ff00:0:1,[1.0.0.1]"), nil},
}
for _, c := range cases {
names, err := GetHostnamesByAddress(c.addr)
if c.expected == nil {
if err == nil {
t.Errorf("no result expected for %v, got %v", c.addr, names)
}
} else {
if err != nil {
t.Error(err)
}
if !equalSet(names, c.expected) {
t.Errorf("wrong result for %v, expected %v, got %v", c.addr, c.expected, names)
}
}
}
}
func mustParse(address string) snet.SCIONAddress {
a, err := snet.ParseUDPAddr(address)
if err != nil {
panic(fmt.Sprintf("test input must parse %s", err))
}
return snet.SCIONAddress{IA: a.IA, Host: addr.HostFromIP(a.Host.IP)}
}
func TestSplitHostPort(t *testing.T) {
type testCase struct {
input string
host string
port string
err bool
}
cases := []testCase{
{"1-ff00:0:0,[1.1.1.1]:80", "1-ff00:0:0,[1.1.1.1]", "80", false},
{"1-ff00:0:0,1.1.1.1:80", "1-ff00:0:0,1.1.1.1", "80", false},
{"1-ff00:0:0,[::]:80", "1-ff00:0:0,[::]", "80", false},
{"foo:80", "foo", "80", false},
{"www.example.com:666", "www.example.com", "666", false},
{"1-ff00:0:0,0:0:0:80", "", "", true},
{":foo:666", "", "", true},
{"1-ff00:0:0,[1.1.1.1]", "", "", true},
{"1-ff00:0:0,[::]", "", "", true},
{"foo", "", "", true},
}
for _, c := range cases {
host, port, err := SplitHostPort(c.input)
if err != nil && !c.err {
t.Errorf("Failed, but shouldn't have. input: %s, error: %s", c.input, err)
} else if err == nil && c.err {
t.Errorf("Did not fail, but should have. input: %s, host: %s, port: %s", c.input, host, port)
} else if err != nil {
if host != c.host || port != c.port {
t.Errorf("Bad result. input: %s, host: %s, port: %s", c.input, host, port)
}
}
}
}