@@ -3,7 +3,6 @@ package graphdb
3
3
import (
4
4
"bytes"
5
5
"net"
6
- "strings"
7
6
"testing"
8
7
9
8
"github.com/lightningnetwork/lnd/lnwire"
@@ -54,84 +53,87 @@ var (
54
53
)
55
54
56
55
var addrTests = []struct {
56
+ inputAddr net.Addr
57
+ // If expAddr is not set, then the test will expect it to be equal
58
+ // to inputAddr.
57
59
expAddr net.Addr
58
60
serErr string
59
61
}{
60
62
// Valid addresses.
61
63
{
62
- expAddr : testIPV4Addr ,
64
+ inputAddr : testIPV4Addr ,
63
65
},
64
66
{
65
- expAddr : testIPV6Addr ,
67
+ inputAddr : testIPV6Addr ,
66
68
},
67
69
{
68
- expAddr : testOnionV2Addr ,
70
+ inputAddr : testOnionV2Addr ,
69
71
},
70
72
{
71
- expAddr : testOnionV3Addr ,
73
+ inputAddr : testOnionV3Addr ,
72
74
},
73
75
{
74
- expAddr : testOpaqueAddr ,
76
+ inputAddr : testOpaqueAddr ,
75
77
},
76
78
{
77
- expAddr : testDNSAddr ,
79
+ inputAddr : testDNSAddr ,
78
80
},
79
81
80
82
// Invalid addresses.
81
83
{
82
- expAddr : unknownAddrType {},
83
- serErr : ErrUnknownAddressType .Error (),
84
+ inputAddr : unknownAddrType {},
85
+ serErr : ErrUnknownAddressType .Error (),
84
86
},
85
87
{
86
- expAddr : & net.TCPAddr {
88
+ inputAddr : & net.TCPAddr {
87
89
// Remove last byte of IPv4 address.
88
90
IP : testIP4 [:len (testIP4 )- 1 ],
89
91
Port : 12345 ,
90
92
},
91
93
serErr : "unable to encode" ,
92
94
},
93
95
{
94
- expAddr : & net.TCPAddr {
96
+ inputAddr : & net.TCPAddr {
95
97
// Add an extra byte of IPv4 address.
96
98
IP : append (testIP4 , 0xff ),
97
99
Port : 12345 ,
98
100
},
99
101
serErr : "unable to encode" ,
100
102
},
101
103
{
102
- expAddr : & net.TCPAddr {
104
+ inputAddr : & net.TCPAddr {
103
105
// Remove last byte of IPv6 address.
104
106
IP : testIP6 [:len (testIP6 )- 1 ],
105
107
Port : 65535 ,
106
108
},
107
109
serErr : "unable to encode" ,
108
110
},
109
111
{
110
- expAddr : & net.TCPAddr {
112
+ inputAddr : & net.TCPAddr {
111
113
// Add an extra byte to the IPv6 address.
112
114
IP : append (testIP6 , 0xff ),
113
115
Port : 65535 ,
114
116
},
115
117
serErr : "unable to encode" ,
116
118
},
117
119
{
118
- expAddr : & tor.OnionAddr {
120
+ inputAddr : & tor.OnionAddr {
119
121
// Invalid suffix.
120
122
OnionService : "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.inion" ,
121
123
Port : 80 ,
122
124
},
123
125
serErr : "invalid suffix" ,
124
126
},
125
127
{
126
- expAddr : & tor.OnionAddr {
128
+ inputAddr : & tor.OnionAddr {
127
129
// Invalid length.
128
130
OnionService : "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyy.onion" ,
129
131
Port : 80 ,
130
132
},
131
133
serErr : "unknown onion service length" ,
132
134
},
133
135
{
134
- expAddr : & tor.OnionAddr {
136
+ inputAddr : & tor.OnionAddr {
135
137
// Invalid encoding.
136
138
OnionService : "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyA.onion" ,
137
139
Port : 80 ,
@@ -147,30 +149,20 @@ func TestAddrSerialization(t *testing.T) {
147
149
148
150
var b bytes.Buffer
149
151
for _ , test := range addrTests {
150
- err := SerializeAddr (& b , test .expAddr )
151
- switch {
152
- case err == nil && test .serErr != "" :
153
- t .Fatalf ("expected serialization err for addr %v" ,
154
- test .expAddr )
155
-
156
- case err != nil && test .serErr == "" :
157
- t .Fatalf ("unexpected serialization err for addr %v: %v" ,
158
- test .expAddr , err )
159
-
160
- case err != nil && ! strings .Contains (err .Error (), test .serErr ):
161
- t .Fatalf ("unexpected serialization err for addr %v, " +
162
- "want: %v, got %v" , test .expAddr , test .serErr ,
163
- err )
164
-
165
- case err != nil :
152
+ err := SerializeAddr (& b , test .inputAddr )
153
+ if test .serErr != "" {
154
+ require .ErrorContains (t , err , test .serErr )
166
155
continue
167
156
}
157
+ require .NoError (t , err )
168
158
169
159
addr , err := DeserializeAddr (& b )
170
- if err != nil {
171
- t .Fatalf ("unable to deserialize address: %v" , err )
172
- }
160
+ require .NoError (t , err )
173
161
174
- require .Equal (t , test .expAddr , addr )
162
+ if test .expAddr != nil {
163
+ require .Equal (t , test .expAddr , addr )
164
+ } else {
165
+ require .Equal (t , test .inputAddr , addr )
166
+ }
175
167
}
176
168
}
0 commit comments