|
| 1 | +package expr |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mdlayher/netlink" |
| 9 | + "golang.org/x/sys/unix" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNat(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + nat NAT |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "Unmarshal DNAT specified case", |
| 20 | + nat: NAT{ |
| 21 | + Type: NATTypeDestNAT, |
| 22 | + Family: unix.NFPROTO_IPV4, |
| 23 | + RegAddrMin: 1, |
| 24 | + RegProtoMin: 2, |
| 25 | + Specified: true, |
| 26 | + }, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "Unmarshal SNAT persistent case", |
| 30 | + nat: NAT{ |
| 31 | + Type: NATTypeSourceNAT, |
| 32 | + Family: unix.NFPROTO_IPV4, |
| 33 | + RegAddrMin: 1, |
| 34 | + RegProtoMin: 2, |
| 35 | + Persistent: true, |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + for _, tt := range tests { |
| 41 | + t.Run(tt.name, func(t *testing.T) { |
| 42 | + nnat := NAT{} |
| 43 | + data, err := tt.nat.marshal(0 /* don't care in this test */) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("marshal error: %+v", err) |
| 46 | + |
| 47 | + } |
| 48 | + ad, err := netlink.NewAttributeDecoder(data) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("NewAttributeDecoder() error: %+v", err) |
| 51 | + } |
| 52 | + ad.ByteOrder = binary.BigEndian |
| 53 | + for ad.Next() { |
| 54 | + if ad.Type() == unix.NFTA_EXPR_DATA { |
| 55 | + if err := nnat.unmarshal(0, ad.Bytes()); err != nil { |
| 56 | + t.Errorf("unmarshal error: %+v", err) |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + if !reflect.DeepEqual(tt.nat, nnat) { |
| 62 | + t.Fatalf("original %+v and recovered %+v Ct structs are different", tt.nat, nnat) |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
0 commit comments