Skip to content

Commit 37aae6e

Browse files
committed
Update lint rules, use testify/assert
1 parent 03a668d commit 37aae6e

21 files changed

+324
-548
lines changed

.golangci.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ linters-settings:
1919
recommendations:
2020
- errors
2121
forbidigo:
22+
analyze-types: true
2223
forbid:
2324
- ^fmt.Print(f|ln)?$
2425
- ^log.(Panic|Fatal|Print)(f|ln)?$
2526
- ^os.Exit$
2627
- ^panic$
2728
- ^print(ln)?$
29+
- p: ^testing.T.(Error|Errorf|Fatal|Fatalf|Fail|FailNow)$
30+
pkg: ^testing$
31+
msg: "use testify/assert instead"
2832
varnamelen:
2933
max-distance: 12
3034
min-name-length: 2
@@ -127,9 +131,12 @@ issues:
127131
exclude-dirs-use-default: false
128132
exclude-rules:
129133
# Allow complex tests and examples, better to be self contained
130-
- path: (examples|main\.go|_test\.go)
134+
- path: (examples|main\.go)
131135
linters:
136+
- gocognit
132137
- forbidigo
138+
- path: _test\.go
139+
linters:
133140
- gocognit
134141

135142
# Allow forbidden identifiers in CLI commands

client_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,11 @@ func TestClientWithSTUN(t *testing.T) {
108108
// Block until go routine is started to make two almost parallel requests
109109
<-started
110110

111-
if _, err = client.SendBindingRequestTo(to); err != nil {
112-
t.Fatal(err)
113-
}
111+
_, err = client.SendBindingRequestTo(to)
112+
assert.NoError(t, err)
114113

115114
<-finished
116-
if err1 != nil {
117-
t.Fatal(err)
118-
}
119-
115+
assert.NoErrorf(t, err1, "should succeed: %v", err)
120116
assert.NoError(t, pc.Close())
121117
})
122118

@@ -244,8 +240,7 @@ func TestTCPClient(t *testing.T) {
244240

245241
peerAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:12345")
246242
require.NoError(t, err)
247-
err = client.CreatePermission(peerAddr)
248-
require.NoError(t, err)
243+
require.NoError(t, client.CreatePermission(peerAddr))
249244

250245
var cid proto.ConnectionID = 5
251246
transactionID := [stun.TransactionIDSize]byte{1, 2, 3}
@@ -259,8 +254,7 @@ func TestTCPClient(t *testing.T) {
259254
msg, err := stun.Build(attrs...)
260255
require.NoError(t, err)
261256

262-
err = client.handleSTUNMessage(msg.Raw, peerAddr)
263-
require.NoError(t, err)
257+
require.NoError(t, client.handleSTUNMessage(msg.Raw, peerAddr))
264258

265259
// Shutdown
266260
require.NoError(t, allocation.Close())

internal/allocation/allocation_manager_test.go

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ func TestManager(t *testing.T) {
3535

3636
network := "udp4"
3737
turnSocket, err := net.ListenPacket(network, "0.0.0.0:0")
38-
if err != nil {
39-
panic(err)
40-
}
38+
assert.NoError(t, err)
4139

4240
for _, tc := range tt {
4341
f := tc.f
@@ -54,15 +52,17 @@ func subTestCreateInvalidAllocation(t *testing.T, turnSocket net.PacketConn) {
5452
m, err := newTestManager()
5553
assert.NoError(t, err)
5654

57-
if a, err := m.CreateAllocation(nil, turnSocket, 0, proto.DefaultLifetime); a != nil || err == nil {
58-
t.Errorf("Illegally created allocation with nil FiveTuple")
59-
}
60-
if a, err := m.CreateAllocation(randomFiveTuple(), nil, 0, proto.DefaultLifetime); a != nil || err == nil {
61-
t.Errorf("Illegally created allocation with nil turnSocket")
62-
}
63-
if a, err := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, 0); a != nil || err == nil {
64-
t.Errorf("Illegally created allocation with 0 lifetime")
65-
}
55+
a, err := m.CreateAllocation(nil, turnSocket, 0, proto.DefaultLifetime)
56+
assert.Nil(t, a, "Illegally created allocation with nil FiveTuple")
57+
assert.Error(t, err, "Illegally created allocation with nil FiveTuple")
58+
59+
a, err = m.CreateAllocation(randomFiveTuple(), nil, 0, proto.DefaultLifetime)
60+
assert.Nil(t, a, "Illegally created allocation with nil turnSocket")
61+
assert.Error(t, err, "Illegally created allocation with nil turnSocket")
62+
63+
a, err = m.CreateAllocation(randomFiveTuple(), turnSocket, 0, 0)
64+
assert.Nil(t, a, "Illegally created allocation with 0 lifetime")
65+
assert.Error(t, err, "Illegally created allocation with 0 lifetime")
6666
}
6767

6868
// Test valid Allocation creations.
@@ -73,13 +73,12 @@ func subTestCreateAllocation(t *testing.T, turnSocket net.PacketConn) {
7373
assert.NoError(t, err)
7474

7575
fiveTuple := randomFiveTuple()
76-
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime); a == nil || err != nil {
77-
t.Errorf("Failed to create allocation %v %v", a, err)
78-
}
76+
a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime)
77+
assert.NotNil(t, a, "Failed to create allocation")
78+
assert.NoError(t, err, "Failed to create allocation")
7979

80-
if a := m.GetAllocation(fiveTuple); a == nil {
81-
t.Errorf("Failed to get allocation right after creation")
82-
}
80+
a = m.GetAllocation(fiveTuple)
81+
assert.NotNil(t, a, "Failed to get allocation right after creation")
8382
}
8483

8584
// Test that two allocations can't be created with the same FiveTuple.
@@ -90,13 +89,13 @@ func subTestCreateAllocationDuplicateFiveTuple(t *testing.T, turnSocket net.Pack
9089
assert.NoError(t, err)
9190

9291
fiveTuple := randomFiveTuple()
93-
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime); a == nil || err != nil {
94-
t.Errorf("Failed to create allocation %v %v", a, err)
95-
}
92+
a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime)
93+
assert.NotNil(t, a, "Failed to create allocation")
94+
assert.NoError(t, err, "Failed to create allocation")
9695

97-
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime); a != nil || err == nil {
98-
t.Errorf("Was able to create allocation with same FiveTuple twice")
99-
}
96+
a, err = m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime)
97+
assert.Nil(t, a, "Was able to create allocation with same FiveTuple twice")
98+
assert.Error(t, err, "Was able to create allocation with same FiveTuple twice")
10099
}
101100

102101
func subTestDeleteAllocation(t *testing.T, turnSocket net.PacketConn) {
@@ -106,18 +105,16 @@ func subTestDeleteAllocation(t *testing.T, turnSocket net.PacketConn) {
106105
assert.NoError(t, err)
107106

108107
fiveTuple := randomFiveTuple()
109-
if a, err := manager.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime); a == nil || err != nil {
110-
t.Errorf("Failed to create allocation %v %v", a, err)
111-
}
108+
a, err := manager.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime)
109+
assert.NotNil(t, a, "Failed to create allocation")
110+
assert.NoError(t, err, "Failed to create allocation")
112111

113-
if a := manager.GetAllocation(fiveTuple); a == nil {
114-
t.Errorf("Failed to get allocation right after creation")
115-
}
112+
a = manager.GetAllocation(fiveTuple)
113+
assert.NotNil(t, a, "Failed to get allocation right after creation")
116114

117115
manager.DeleteAllocation(fiveTuple)
118-
if a := manager.GetAllocation(fiveTuple); a != nil {
119-
t.Errorf("Get allocation with %v should be nil after delete", fiveTuple)
120-
}
116+
a = manager.GetAllocation(fiveTuple)
117+
assert.Nilf(t, a, "Failed to delete allocation %v", fiveTuple)
121118
}
122119

123120
// Test that allocation should be closed if timeout.
@@ -134,19 +131,15 @@ func subTestAllocationTimeout(t *testing.T, turnSocket net.PacketConn) {
134131
fiveTuple := randomFiveTuple()
135132

136133
a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, lifetime)
137-
if err != nil {
138-
t.Errorf("Failed to create allocation with %v", fiveTuple)
139-
}
134+
assert.NoErrorf(t, err, "Failed to create allocation with %v", fiveTuple)
140135

141136
allocations[index] = a
142137
}
143138

144139
// Make sure all allocations timeout
145140
time.Sleep(lifetime + time.Second)
146141
for _, alloc := range allocations {
147-
if !isClose(alloc.RelaySocket) {
148-
t.Error("Allocation relay socket should be closed if lifetime timeout")
149-
}
142+
assert.True(t, isClose(alloc.RelaySocket), "Allocation relay socket should be closed if lifetime timeout")
150143
}
151144
}
152145

@@ -166,15 +159,10 @@ func subTestManagerClose(t *testing.T, turnSocket net.PacketConn) {
166159

167160
// Make a1 timeout
168161
time.Sleep(2 * time.Second)
169-
170-
if err := manager.Close(); err != nil {
171-
t.Errorf("Manager close with error: %v", err)
172-
}
162+
assert.NoError(t, manager.Close())
173163

174164
for _, alloc := range allocations {
175-
if !isClose(alloc.RelaySocket) {
176-
t.Error("Manager's allocations should be closed")
177-
}
165+
assert.True(t, isClose(alloc.RelaySocket), "Manager's allocations should be closed")
178166
}
179167
}
180168

internal/allocation/allocation_test.go

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,13 @@ func subTestGetPermission(t *testing.T) {
5151
alloc := NewAllocation(nil, nil, nil)
5252

5353
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:3478")
54-
if err != nil {
55-
t.Fatalf("failed to resolve: %s", err)
56-
}
54+
assert.NoError(t, err)
5755

5856
addr2, err := net.ResolveUDPAddr("udp", "127.0.0.1:3479")
59-
if err != nil {
60-
t.Fatalf("failed to resolve: %s", err)
61-
}
57+
assert.NoError(t, err)
6258

6359
addr3, err := net.ResolveUDPAddr("udp", "127.0.0.2:3478")
64-
if err != nil {
65-
t.Fatalf("failed to resolve: %s", err)
66-
}
60+
assert.NoError(t, err)
6761

6862
perms := &Permission{
6963
Addr: addr,
@@ -95,9 +89,7 @@ func subTestAddPermission(t *testing.T) {
9589
alloc := NewAllocation(nil, nil, nil)
9690

9791
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:3478")
98-
if err != nil {
99-
t.Fatalf("failed to resolve: %s", err)
100-
}
92+
assert.NoError(t, err)
10193

10294
p := &Permission{
10395
Addr: addr,
@@ -116,9 +108,7 @@ func subTestRemovePermission(t *testing.T) {
116108
alloc := NewAllocation(nil, nil, nil)
117109

118110
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:3478")
119-
if err != nil {
120-
t.Fatalf("failed to resolve: %s", err)
121-
}
111+
assert.NoError(t, err)
122112

123113
p := &Permission{
124114
Addr: addr,
@@ -141,9 +131,7 @@ func subTestAddChannelBind(t *testing.T) {
141131
alloc := NewAllocation(nil, nil, nil)
142132

143133
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:3478")
144-
if err != nil {
145-
t.Fatalf("failed to resolve: %s", err)
146-
}
134+
assert.NoError(t, err)
147135

148136
c := NewChannelBind(proto.MinChannelNumber, addr, nil)
149137

@@ -167,9 +155,7 @@ func subTestGetChannelByNumber(t *testing.T) {
167155
alloc := NewAllocation(nil, nil, nil)
168156

169157
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:3478")
170-
if err != nil {
171-
t.Fatalf("failed to resolve: %s", err)
172-
}
158+
assert.NoError(t, err)
173159

174160
c := NewChannelBind(proto.MinChannelNumber, addr, nil)
175161

@@ -188,9 +174,7 @@ func subTestGetChannelByAddr(t *testing.T) {
188174
alloc := NewAllocation(nil, nil, nil)
189175

190176
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:3478")
191-
if err != nil {
192-
t.Fatalf("failed to resolve: %s", err)
193-
}
177+
assert.NoError(t, err)
194178

195179
c := NewChannelBind(proto.MinChannelNumber, addr, nil)
196180

@@ -210,9 +194,7 @@ func subTestRemoveChannelBind(t *testing.T) {
210194
alloc := NewAllocation(nil, nil, nil)
211195

212196
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:3478")
213-
if err != nil {
214-
t.Fatalf("failed to resolve: %s", err)
215-
}
197+
assert.NoError(t, err)
216198

217199
c := NewChannelBind(proto.MinChannelNumber, addr, nil)
218200

@@ -250,9 +232,7 @@ func subTestAllocationClose(t *testing.T) {
250232
network := "udp"
251233

252234
l, err := net.ListenPacket(network, "0.0.0.0:0")
253-
if err != nil {
254-
panic(err)
255-
}
235+
assert.NoError(t, err)
256236

257237
alloc := NewAllocation(nil, nil, nil)
258238
alloc.RelaySocket = l
@@ -261,18 +241,15 @@ func subTestAllocationClose(t *testing.T) {
261241

262242
// Add channel
263243
addr, err := net.ResolveUDPAddr(network, "127.0.0.1:3478")
264-
if err != nil {
265-
t.Fatalf("failed to resolve: %s", err)
266-
}
244+
assert.NoError(t, err)
267245

268246
c := NewChannelBind(proto.MinChannelNumber, addr, nil)
269247
_ = alloc.AddChannelBind(c, proto.DefaultLifetime)
270248

271249
// Add permission
272250
alloc.AddPermission(NewPermission(addr, nil))
273251

274-
err = alloc.Close()
275-
assert.Nil(t, err, "should succeed")
252+
assert.Nil(t, alloc.Close(), "should succeed")
276253
assert.True(t, isClose(alloc.RelaySocket), "should be closed")
277254
}
278255

@@ -285,15 +262,11 @@ func subTestPacketHandler(t *testing.T) {
285262

286263
// TURN server initialization
287264
turnSocket, err := net.ListenPacket(network, "127.0.0.1:0")
288-
if err != nil {
289-
panic(err)
290-
}
265+
assert.NoError(t, err)
291266

292267
// Client listener initialization
293268
clientListener, err := net.ListenPacket(network, "127.0.0.1:0")
294-
if err != nil {
295-
panic(err)
296-
}
269+
assert.NoError(t, err)
297270

298271
dataCh := make(chan []byte)
299272
// Client listener read data
@@ -314,17 +287,13 @@ func subTestPacketHandler(t *testing.T) {
314287
DstAddr: turnSocket.LocalAddr(),
315288
}, turnSocket, 0, proto.DefaultLifetime)
316289

317-
assert.Nil(t, err, "should succeed")
290+
assert.NoError(t, err, "should succeed")
318291

319292
peerListener1, err := net.ListenPacket(network, "127.0.0.1:0")
320-
if err != nil {
321-
panic(err)
322-
}
293+
assert.NoError(t, err)
323294

324295
peerListener2, err := net.ListenPacket(network, "127.0.0.1:0")
325-
if err != nil {
326-
panic(err)
327-
}
296+
assert.NoError(t, err)
328297

329298
// Add permission with peer1 address
330299
alloc.AddPermission(NewPermission(peerListener1.LocalAddr(), manager.log))
@@ -345,12 +314,10 @@ func subTestPacketHandler(t *testing.T) {
345314
assert.True(t, stun.IsMessage(data), "should be stun message")
346315

347316
var msg stun.Message
348-
err = stun.Decode(data, &msg)
349-
assert.Nil(t, err, "decode data to stun message failed")
317+
assert.NoError(t, stun.Decode(data, &msg), "decode data to stun message failed")
350318

351319
var msgData proto.Data
352-
err = msgData.GetFrom(&msg)
353-
assert.Nil(t, err, "get data from stun message failed")
320+
assert.NoError(t, msgData.GetFrom(&msg), "get data from stun message failed")
354321
assert.Equal(t, targetText, string(msgData), "get message doesn't equal the target text")
355322

356323
// Test for channel bind and channel data
@@ -364,8 +331,7 @@ func subTestPacketHandler(t *testing.T) {
364331
channelData := proto.ChannelData{
365332
Raw: data,
366333
}
367-
err = channelData.Decode()
368-
assert.Nil(t, err, fmt.Sprintf("channel data decode with error: %v", err))
334+
assert.NoError(t, channelData.Decode(), fmt.Sprintf("channel data decode with error: %v", err))
369335
assert.Equal(t, channelBind.Number, channelData.Number, "get channel data's number is invalid")
370336
assert.Equal(t, targetText2, string(channelData.Data), "get data doesn't equal the target text.")
371337

0 commit comments

Comments
 (0)