Skip to content

Commit 6b38f8d

Browse files
ellemoutonmohamedawnallah
authored andcommitted
graph/db: update TestAddrSerialization
1 parent 3fa137e commit 6b38f8d

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

graph/db/addr_test.go

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package graphdb
33
import (
44
"bytes"
55
"net"
6-
"strings"
76
"testing"
87

98
"github.com/lightningnetwork/lnd/lnwire"
@@ -54,84 +53,87 @@ var (
5453
)
5554

5655
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.
5759
expAddr net.Addr
5860
serErr string
5961
}{
6062
// Valid addresses.
6163
{
62-
expAddr: testIPV4Addr,
64+
inputAddr: testIPV4Addr,
6365
},
6466
{
65-
expAddr: testIPV6Addr,
67+
inputAddr: testIPV6Addr,
6668
},
6769
{
68-
expAddr: testOnionV2Addr,
70+
inputAddr: testOnionV2Addr,
6971
},
7072
{
71-
expAddr: testOnionV3Addr,
73+
inputAddr: testOnionV3Addr,
7274
},
7375
{
74-
expAddr: testOpaqueAddr,
76+
inputAddr: testOpaqueAddr,
7577
},
7678
{
77-
expAddr: testDNSAddr,
79+
inputAddr: testDNSAddr,
7880
},
7981

8082
// Invalid addresses.
8183
{
82-
expAddr: unknownAddrType{},
83-
serErr: ErrUnknownAddressType.Error(),
84+
inputAddr: unknownAddrType{},
85+
serErr: ErrUnknownAddressType.Error(),
8486
},
8587
{
86-
expAddr: &net.TCPAddr{
88+
inputAddr: &net.TCPAddr{
8789
// Remove last byte of IPv4 address.
8890
IP: testIP4[:len(testIP4)-1],
8991
Port: 12345,
9092
},
9193
serErr: "unable to encode",
9294
},
9395
{
94-
expAddr: &net.TCPAddr{
96+
inputAddr: &net.TCPAddr{
9597
// Add an extra byte of IPv4 address.
9698
IP: append(testIP4, 0xff),
9799
Port: 12345,
98100
},
99101
serErr: "unable to encode",
100102
},
101103
{
102-
expAddr: &net.TCPAddr{
104+
inputAddr: &net.TCPAddr{
103105
// Remove last byte of IPv6 address.
104106
IP: testIP6[:len(testIP6)-1],
105107
Port: 65535,
106108
},
107109
serErr: "unable to encode",
108110
},
109111
{
110-
expAddr: &net.TCPAddr{
112+
inputAddr: &net.TCPAddr{
111113
// Add an extra byte to the IPv6 address.
112114
IP: append(testIP6, 0xff),
113115
Port: 65535,
114116
},
115117
serErr: "unable to encode",
116118
},
117119
{
118-
expAddr: &tor.OnionAddr{
120+
inputAddr: &tor.OnionAddr{
119121
// Invalid suffix.
120122
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.inion",
121123
Port: 80,
122124
},
123125
serErr: "invalid suffix",
124126
},
125127
{
126-
expAddr: &tor.OnionAddr{
128+
inputAddr: &tor.OnionAddr{
127129
// Invalid length.
128130
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyy.onion",
129131
Port: 80,
130132
},
131133
serErr: "unknown onion service length",
132134
},
133135
{
134-
expAddr: &tor.OnionAddr{
136+
inputAddr: &tor.OnionAddr{
135137
// Invalid encoding.
136138
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyA.onion",
137139
Port: 80,
@@ -147,30 +149,20 @@ func TestAddrSerialization(t *testing.T) {
147149

148150
var b bytes.Buffer
149151
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)
166155
continue
167156
}
157+
require.NoError(t, err)
168158

169159
addr, err := DeserializeAddr(&b)
170-
if err != nil {
171-
t.Fatalf("unable to deserialize address: %v", err)
172-
}
160+
require.NoError(t, err)
173161

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+
}
175167
}
176168
}

0 commit comments

Comments
 (0)