-
Notifications
You must be signed in to change notification settings - Fork 292
/
server_test.go
104 lines (98 loc) · 2.76 KB
/
server_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
// Copyright (c) 2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"net"
"reflect"
"testing"
"github.com/decred/dcrd/addrmgr/v3"
"github.com/decred/dcrd/wire"
)
// TestHostToNetAddress ensures that hostToNetAddress behaves as expected
// given valid and invalid host name arguments.
func TestHostToNetAddress(t *testing.T) {
// Define a hostname that will cause a lookup to be performed using the
// lookupFunc provided to the address manager instance for each test.
const hostnameForLookup = "hostname.test"
const services = wire.SFNodeNetwork
tests := []struct {
name string
host string
port uint16
lookupFunc func(host string) ([]net.IP, error)
wantErr bool
want *addrmgr.NetAddress
}{{
// name: "valid onion address",
// host: "a5ccbdkubbr2jlcp.onion",
// port: 8333,
// lookupFunc: nil,
// wantErr: false,
// want: NewNetAddressFromIPPort(
// net.ParseIP("fd87:d87e:eb43:744:208d:5408:63a4:ac4f"), 8333,
// services),
// }, {
// name: "invalid onion address",
// host: "0000000000000000.onion",
// port: 8333,
// lookupFunc: nil,
// wantErr: true,
// want: nil,
// }, {
name: "unresolvable host name",
host: hostnameForLookup,
port: 8333,
lookupFunc: func(host string) ([]net.IP, error) {
return nil, fmt.Errorf("unresolvable host %v", host)
},
wantErr: true,
want: nil,
}, {
name: "not resolved host name",
host: hostnameForLookup,
port: 8333,
lookupFunc: func(host string) ([]net.IP, error) {
return nil, nil
},
wantErr: true,
want: nil,
}, {
name: "resolved host name",
host: hostnameForLookup,
port: 8333,
lookupFunc: func(host string) ([]net.IP, error) {
return []net.IP{net.ParseIP("127.0.0.1")}, nil
},
wantErr: false,
want: addrmgr.NewNetAddressFromIPPort(net.ParseIP("127.0.0.1"), 8333,
services),
}, {
name: "valid IPv4 address",
host: "12.1.2.3",
port: 8333,
lookupFunc: nil,
wantErr: false,
want: addrmgr.NewNetAddressFromIPPort(net.ParseIP("12.1.2.3"), 8333,
services),
}, {
name: "valid IPv6 address",
host: "2003::1",
port: 8333,
lookupFunc: nil,
wantErr: false,
want: addrmgr.NewNetAddressFromIPPort(net.ParseIP("2003::1"), 8333,
services),
}}
for _, test := range tests {
result, err := hostToNetAddress(test.host, test.port, services, test.lookupFunc)
if test.wantErr == true && err == nil {
t.Errorf("%q: expected error but one was not returned", test.name)
}
if !reflect.DeepEqual(result, test.want) {
t.Errorf("%q: unexpected result - got %v, want %v", test.name,
result, test.want)
}
}
}