1
1
package conntrack
2
2
3
3
import (
4
- "net"
4
+ "net/netip "
5
5
"testing"
6
6
7
- "github.com/google/go-cmp/cmp"
8
7
"github.com/stretchr/testify/assert"
9
8
"github.com/stretchr/testify/require"
10
9
"github.com/ti-mo/netfilter"
@@ -183,8 +182,8 @@ var corpusExpect = []struct {
183
182
exp : Expect {
184
183
TupleMaster : Tuple {
185
184
IP : IPTuple {
186
- SourceAddress : [] byte { 127 , 0 , 0 , 1 } ,
187
- DestinationAddress : [] byte { 127 , 0 , 0 , 2 } ,
185
+ SourceAddress : netip . MustParseAddr ( " 127.0.0.1" ) ,
186
+ DestinationAddress : netip . MustParseAddr ( " 127.0.0.2" ) ,
188
187
},
189
188
Proto : ProtoTuple {
190
189
Protocol : 6 ,
@@ -194,8 +193,8 @@ var corpusExpect = []struct {
194
193
},
195
194
Tuple : Tuple {
196
195
IP : IPTuple {
197
- SourceAddress : [] byte { 127 , 0 , 0 , 1 } ,
198
- DestinationAddress : [] byte { 127 , 0 , 0 , 2 } ,
196
+ SourceAddress : netip . MustParseAddr ( " 127.0.0.1" ) ,
197
+ DestinationAddress : netip . MustParseAddr ( " 127.0.0.2" ) ,
199
198
},
200
199
Proto : ProtoTuple {
201
200
Protocol : 6 ,
@@ -204,8 +203,8 @@ var corpusExpect = []struct {
204
203
},
205
204
Mask : Tuple {
206
205
IP : IPTuple {
207
- SourceAddress : [] byte { 255 , 255 , 255 , 255 } ,
208
- DestinationAddress : [] byte { 255 , 255 , 255 , 255 } ,
206
+ SourceAddress : netip . MustParseAddr ( " 255. 255. 255. 255" ) ,
207
+ DestinationAddress : netip . MustParseAddr ( " 255. 255. 255. 255" ) ,
209
208
},
210
209
Proto : ProtoTuple {
211
210
Protocol : 6 ,
@@ -263,11 +262,8 @@ func TestExpectUnmarshal(t *testing.T) {
263
262
for _ , tt := range corpusExpect {
264
263
t .Run (tt .name , func (t * testing.T ) {
265
264
var ex Expect
266
- assert .NoError (t , ex .unmarshal (mustDecodeAttributes (tt .attrs )))
267
-
268
- if diff := cmp .Diff (tt .exp , ex ); diff != "" {
269
- t .Fatalf ("unexpected unmarshal (-want +got):\n %s" , diff )
270
- }
265
+ require .NoError (t , ex .unmarshal (mustDecodeAttributes (tt .attrs )))
266
+ assert .Equal (t , tt .exp , ex , "unexpected unmarshal" )
271
267
})
272
268
}
273
269
@@ -355,9 +351,7 @@ func TestExpectMarshal(t *testing.T) {
355
351
},
356
352
}
357
353
358
- if diff := cmp .Diff (want , exm ); diff != "" {
359
- t .Fatalf ("unexpected Expect marshal (-want +got):\n %s" , diff )
360
- }
354
+ assert .Equal (t , want , exm , "unexpected Expect marshal" )
361
355
362
356
// Cannot marshal without tuple/mask/master Tuples
363
357
_ , err = Expect {}.marshal ()
@@ -424,10 +418,7 @@ func TestExpectNATUnmarshal(t *testing.T) {
424
418
}
425
419
426
420
require .NoError (t , err )
427
-
428
- if diff := cmp .Diff (tt .enat , enat ); diff != "" {
429
- t .Fatalf ("unexpected unmarshal (-want +got):\n %s" , diff )
430
- }
421
+ assert .Equal (t , tt .enat , enat , "unexpected unmarshal" )
431
422
})
432
423
}
433
424
}
@@ -439,8 +430,8 @@ func TestExpectNATMarshal(t *testing.T) {
439
430
Direction : true ,
440
431
Tuple : Tuple {
441
432
IP : IPTuple {
442
- SourceAddress : net . ParseIP ("baa:baa::b" ),
443
- DestinationAddress : net . ParseIP ("ef00:3f00::ba13" ),
433
+ SourceAddress : netip . MustParseAddr ("baa:baa::b" ),
434
+ DestinationAddress : netip . MustParseAddr ("ef00:3f00::ba13" ),
444
435
},
445
436
Proto : ProtoTuple {
446
437
Protocol : 13 ,
@@ -458,9 +449,7 @@ func TestExpectNATMarshal(t *testing.T) {
458
449
459
450
// Only verify first attribute (direction); Tuple marshal has its own tests
460
451
want := netfilter.Attribute {Type : uint16 (ctaExpectNATDir ), Data : []byte {0 , 0 , 0 , 1 }}
461
- if diff := cmp .Diff (want , enm .Children [0 ]); diff != "" {
462
- t .Fatalf ("unexpected ExpectNAT marshal (-want +got):\n %s" , diff )
463
- }
452
+ assert .Equal (t , want , enm .Children [0 ], "unexpected ExpectNAT marshal" )
464
453
}
465
454
466
455
func TestExpectTypeString (t * testing.T ) {
0 commit comments