From ae8f84674dee9ee04f789519b2a8d93d493bcee8 Mon Sep 17 00:00:00 2001 From: Aleksei Ilin Date: Fri, 2 Aug 2024 19:48:29 +0200 Subject: [PATCH] ct: Add test --- expr/ct_test.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 expr/ct_test.go diff --git a/expr/ct_test.go b/expr/ct_test.go new file mode 100644 index 0000000..b495860 --- /dev/null +++ b/expr/ct_test.go @@ -0,0 +1,69 @@ +package expr + +import ( + "encoding/binary" + "reflect" + "testing" + + "github.com/mdlayher/netlink" + "golang.org/x/sys/unix" +) + +func TestCt(t *testing.T) { + t.Parallel() + tests := []struct { + name string + ct Ct + }{ + { + name: "Unmarshal Ct status case", + ct: Ct{ + Register: 1, + Key: CtKeySTATUS, + }, + }, + { + name: "Unmarshal Ct proto-dst direction original case", + ct: Ct{ + Register: 1, + Key: CtKeyPROTODST, + Direction: 0, // direction: original + }, + }, + { + name: "Unmarshal Ct src direction reply case", + ct: Ct{ + Register: 1, + Key: CtKeySRC, + Direction: 1, // direction: reply + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + nct := Ct{} + data, err := tt.ct.marshal(0 /* don't care in this test */) + if err != nil { + t.Fatalf("marshal error: %+v", err) + + } + ad, err := netlink.NewAttributeDecoder(data) + if err != nil { + t.Fatalf("NewAttributeDecoder() error: %+v", err) + } + ad.ByteOrder = binary.BigEndian + for ad.Next() { + if ad.Type() == unix.NFTA_EXPR_DATA { + if err := nct.unmarshal(0, ad.Bytes()); err != nil { + t.Errorf("unmarshal error: %+v", err) + break + } + } + } + if !reflect.DeepEqual(tt.ct, nct) { + t.Fatalf("original %+v and recovered %+v Ct structs are different", tt.ct, nct) + } + }) + } +}