Addressing unit test failure

Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
This commit is contained in:
Serguei Bezverkhi 2020-02-05 16:52:43 -05:00
parent 802c2a18ab
commit 8093f4aecc
3 changed files with 19 additions and 20 deletions

View File

@ -38,7 +38,9 @@ func (e *Dynset) marshal() ([]byte, error) {
// See: https://git.netfilter.org/libnftnl/tree/src/expr/dynset.c
var opAttrs []netlink.Attribute
opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_SREG_KEY, Data: binaryutil.BigEndian.PutUint32(e.SrcRegKey)})
if e.SrcRegData != 0 {
opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_SREG_DATA, Data: binaryutil.BigEndian.PutUint32(e.SrcRegData)})
}
opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_OP, Data: binaryutil.BigEndian.PutUint32(e.Operation)})
if e.Timeout != 0 {
opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_TIMEOUT, Data: binaryutil.BigEndian.PutUint64(uint64(e.Timeout.Milliseconds()))})
@ -76,6 +78,8 @@ func (e *Dynset) unmarshal(data []byte) error {
e.SrcRegKey = ad.Uint32()
case unix.NFTA_DYNSET_SREG_DATA:
e.SrcRegData = ad.Uint32()
case unix.NFTA_DYNSET_OP:
e.Operation = ad.Uint32()
case unix.NFTA_DYNSET_TIMEOUT:
e.Timeout = time.Duration(ad.Uint64() * 1000)
case unix.NFTA_DYNSET_FLAGS:

View File

@ -2530,16 +2530,11 @@ func TestDynset(t *testing.T) {
&expr.Payload{
DestRegister: 1,
Base: expr.PayloadBaseNetworkHeader,
Offset: 12,
Len: 4,
},
&expr.Immediate{
Register: 2,
Data: []byte{0x0, 0x0, 0x0, 0x2},
Offset: uint32(12),
Len: uint32(4),
},
&expr.Dynset{
SrcRegKey: 1,
SrcRegData: 2,
SetName: set.Name,
SetID: set.ID,
Operation: uint32(unix.NFT_DYNSET_OP_UPDATE),
@ -2567,19 +2562,17 @@ func TestDynset(t *testing.T) {
if got, want := len(rules), 1; got != want {
t.Fatalf("unexpected number of rules: got %d, want %d", got, want)
}
if got, want := len(rules[0].Exprs), 3; got != want {
if got, want := len(rules[0].Exprs), 2; got != want {
t.Fatalf("unexpected number of exprs: got %d, want %d", got, want)
}
dynset, dynsetOk := rules[0].Exprs[2].(*expr.Dynset)
dynset, dynsetOk := rules[0].Exprs[1].(*expr.Dynset)
if !dynsetOk {
t.Fatalf("Exprs[3] is type %T, want *expr.Dynset", rules[0].Exprs[2])
t.Fatalf("Exprs[0] is type %T, want *expr.Dynset", rules[0].Exprs[1])
}
if want := (&expr.Dynset{
SrcRegKey: 1,
SrcRegData: 2,
SetName: set.Name,
SetID: set.ID,
Operation: uint32(unix.NFT_DYNSET_OP_UPDATE),
}); !reflect.DeepEqual(dynset, want) {
t.Errorf("dynset expr = %+v, wanted %+v", dynset, want)

View File

@ -240,6 +240,8 @@ func exprsFromMsg(b []byte) ([]expr.Any, error) {
e = &expr.Redir{}
case "nat":
e = &expr.NAT{}
case "dynset":
e = &expr.Dynset{}
}
if e == nil {
// TODO: introduce an opaque expression type so that users know