Skip to content

Commit

Permalink
add PCNT and PAD
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Lehner <dev@der-flo.net>
  • Loading branch information
florianl committed Dec 14, 2024
1 parent f47ab10 commit 3734c6b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 8 additions & 1 deletion f_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
tcaBasicAct
tcaBasicPolice
tcaBasicPCNT
tcaBasicPad
)

// Basic contains attributes of the basic discipline
Expand All @@ -21,6 +22,7 @@ type Basic struct {
Police *Police
Ematch *Ematch
Actions *[]*Action
Pcnt *uint64
}

// unmarshalBasic parses the Basic-encoded data and stores the result in the value pointed to by info.
Expand Down Expand Up @@ -50,7 +52,9 @@ func unmarshalBasic(data []byte, info *Basic) error {
multiError = concatError(multiError, err)
info.Actions = actions
case tcaBasicPCNT:
continue
info.Pcnt = uint64Ptr(ad.Uint64())
case tcaBasicPad:
// padding does not contain data, we just skip it
default:
return fmt.Errorf("unmarshalBasic()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
Expand All @@ -65,6 +69,9 @@ func marshalBasic(info *Basic) ([]byte, error) {
if info == nil {
return []byte{}, fmt.Errorf("Basic: %w", ErrNoArg)
}
if info.Pcnt != nil {
return []byte{}, ErrNoArgAlter
}
var multiError error

// TODO: improve logic and check combinations
Expand Down
8 changes: 7 additions & 1 deletion f_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestBasic(t *testing.T) {
}
t.Fatalf("Unexpected error: %v", err1)
}
newData := injectAttribute(t, data, []byte{}, tcaBasicPCNT)
newData := injectAttribute(t, data, []byte{}, tcaBasicPad)
val := Basic{}
err2 := unmarshalBasic(newData, &val)
if err2 != nil {
Expand All @@ -80,4 +80,10 @@ func TestBasic(t *testing.T) {
t.Fatalf("expected error but got nil")
}
})
t.Run("pcnt", func(t *testing.T) {
_, err := marshalBasic(&Basic{Pcnt: uint64Ptr(42)})
if !errors.Is(err, ErrNoArgAlter) {
t.Fatalf("expected '%v' but got '%v'", ErrNoArgAlter, err)
}
})
}

0 comments on commit 3734c6b

Please sign in to comment.