11package netfilter
22
33import (
4+ "errors"
45 "strings"
56 "testing"
67
@@ -209,7 +210,7 @@ func TestAttributeMarshalErrors(t *testing.T) {
209210 }
210211}
211212
212- func TestAttributeUnmarshalErrors (t * testing.T ) {
213+ func TestAttributeDecoderErrors (t * testing.T ) {
213214 tests := []struct {
214215 name string
215216 b []byte
@@ -233,31 +234,19 @@ func TestAttributeUnmarshalErrors(t *testing.T) {
233234 },
234235 err : errInvalidAttributeFlags ,
235236 },
237+ {
238+ name : "decoding invalid attribute" ,
239+ b : []byte {4 , 0 , 0 },
240+ err : errors .New ("invalid attribute; length too short or too large" ),
241+ },
236242 }
237243
238244 for _ , tt := range tests {
239245 t .Run (tt .name , func (t * testing.T ) {
240- ad , err := NewAttributeDecoder (tt .b )
241- if err != nil {
242- t .Fatal ("unexpected error creating AttributeDecoder:" , err )
243- }
244-
245- _ , err = unmarshalAttributes (ad )
246- if err == nil {
247- t .Fatal ("unmarshal did not error" )
248- }
249-
250- if tt .err != nil {
251- if want , got := tt .err , err ; want != got {
252- t .Fatalf ("unexpected error:\n - want: %v\n - got: %v" ,
253- want , got .Error ())
254- }
255- } else if tt .errWrap != "" {
256- if ! strings .HasPrefix (err .Error (), tt .errWrap + ":" ) {
257- t .Fatalf ("unexpected wrapped error:\n - expected prefix: %v\n - error string: %v" ,
258- tt .errWrap , err )
259- }
260- }
246+ _ , err := UnmarshalAttributes (tt .b )
247+ require .Error (t , err )
248+ require .Error (t , tt .err )
249+ require .EqualError (t , err , tt .err .Error ())
261250 })
262251 }
263252}
@@ -402,7 +391,7 @@ func TestAttributeMarshalTwoWay(t *testing.T) {
402391 }
403392
404393 // Unmarshal binary content into nested structures
405- attrs , err := unmarshalAttributes (ad )
394+ attrs , err := decodeAttributes (ad )
406395 require .NoError (t , err )
407396
408397 assert .Empty (t , cmp .Diff (tt .attrs , attrs ))
@@ -417,3 +406,7 @@ func TestAttributeMarshalTwoWay(t *testing.T) {
417406 })
418407 }
419408}
409+
410+ func TestErrors (t * testing.T ) {
411+ assert .EqualError (t , encodeAttributes (nil , nil ), errNilAttributeEncoder .Error ())
412+ }
0 commit comments