Skip to content

Commit

Permalink
Merge pull request #6 from ti-mo/bump-deps-1222
Browse files Browse the repository at this point in the history
December 2022 maintenance
  • Loading branch information
ti-mo authored Dec 16, 2022
2 parents e8114e1 + f329510 commit 3d7ebe2
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 202 deletions.
42 changes: 0 additions & 42 deletions .builds/integration-edge.yml

This file was deleted.

32 changes: 0 additions & 32 deletions .builds/integration-stable-1.12.yml

This file was deleted.

27 changes: 27 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Lint

on:
pull_request: {}
push:
branches:
- master

jobs:
build:
strategy:
matrix:
go-version: [1.18, 1.19]
runs-on: ubuntu-latest

steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out Go module
uses: actions/checkout@v3

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
32 changes: 32 additions & 0 deletions .github/workflows/linux-integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Linux Integration Test

on:
pull_request: {}
push:
branches:
- master

jobs:
build:
strategy:
matrix:
go-version: [1.18, 1.19]
runs-on: ubuntu-latest

steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out Go module
uses: actions/checkout@v3

- name: Run integration tests
run: make integration

- name: goveralls
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: cover-int.out
27 changes: 27 additions & 0 deletions .github/workflows/linux-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Linux Test

on:
pull_request: {}
push:
branches:
- master

jobs:
build:
strategy:
matrix:
go-version: [1.18, 1.19]
runs-on: ubuntu-latest

steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out Go module
uses: actions/checkout@v3

- name: Run tests
run: make test
26 changes: 26 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
linters-settings:
staticcheck:
go: "1.19"
unused:
go: "1.19"

linters:
enable:
- goerr113
- gofmt
- goimports
- govet
- ineffassign
- misspell
- staticcheck
- unused
- goheader
- errcheck
- gocyclo
- revive
- gosec
- gosimple
- lll
- misspell
- prealloc
- typecheck
21 changes: 4 additions & 17 deletions attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ type Attribute struct {

func (a Attribute) String() string {
if a.Nested {
return fmt.Sprintf("<Length %d, Type %d, Nested %t, %d Children (%v)>", len(a.Data), a.Type, a.Nested, len(a.Children), a.Children)
return fmt.Sprintf("<Length %d, Type %d, Nested %t, %d Children (%v)>",
len(a.Data), a.Type, a.Nested, len(a.Children), a.Children)
}

return fmt.Sprintf("<Length %d, Type %d, Nested %t, NetByteOrder %t, %v>", len(a.Data), a.Type, a.Nested, a.NetByteOrder, a.Data)

return fmt.Sprintf("<Length %d, Type %d, Nested %t, NetByteOrder %t, %v>",
len(a.Data), a.Type, a.Nested, a.NetByteOrder, a.Data)
}

// Uint16 interprets a non-nested Netfilter attribute in network byte order as a uint16.
Expand Down Expand Up @@ -84,7 +85,6 @@ func (a *Attribute) PutUint16(v uint16) {

// Uint32 interprets a non-nested Netfilter attribute in network byte order as a uint32.
func (a Attribute) Uint32() uint32 {

if a.Nested {
panic("Uint32: unexpected Nested attribute")
}
Expand All @@ -98,7 +98,6 @@ func (a Attribute) Uint32() uint32 {

// PutUint32 sets the Attribute's data field to a Uint32 encoded in net byte order.
func (a *Attribute) PutUint32(v uint32) {

if len(a.Data) != 4 {
a.Data = make([]byte, 4)
}
Expand All @@ -113,7 +112,6 @@ func (a Attribute) Int32() int32 {

// Uint64 interprets a non-nested Netfilter attribute in network byte order as a uint64.
func (a Attribute) Uint64() uint64 {

if a.Nested {
panic("Uint64: unexpected Nested attribute")
}
Expand All @@ -127,7 +125,6 @@ func (a Attribute) Uint64() uint64 {

// PutUint64 sets the Attribute's data field to a Uint64 encoded in net byte order.
func (a *Attribute) PutUint64(v uint64) {

if len(a.Data) != 8 {
a.Data = make([]byte, 8)
}
Expand Down Expand Up @@ -164,9 +161,7 @@ func Uint64Bytes(u uint64) []byte {
// decode fills the Attribute's Children field with Attributes
// obtained by exhausting ad.
func (a *Attribute) decode(ad *netlink.AttributeDecoder) error {

for ad.Next() {

// Copy the netlink attribute's fields into the netfilter attribute.
nfa := Attribute{
// Only consider the rightmost 14 bits for Type.
Expand Down Expand Up @@ -198,11 +193,8 @@ func (a *Attribute) decode(ad *netlink.AttributeDecoder) error {
// This function can be passed to AttributeEncoder.Nested for recursively
// encoding Attributes.
func (a *Attribute) encode(attrs []Attribute) func(*netlink.AttributeEncoder) error {

return func(ae *netlink.AttributeEncoder) error {

for _, nfa := range attrs {

if nfa.NetByteOrder && nfa.Nested {
return errInvalidAttributeFlags
}
Expand All @@ -227,7 +219,6 @@ func (a *Attribute) encode(attrs []Attribute) func(*netlink.AttributeEncoder) er
// a byte array. This byte array should be taken from the netlink.Message's
// Data payload after the nfHeaderLen offset.
func decodeAttributes(ad *netlink.AttributeDecoder) ([]Attribute, error) {

// Use the Children element of the Attribute to decode into.
// Attribute already has nested decoding implemented on the type.
var a Attribute
Expand All @@ -247,7 +238,6 @@ func decodeAttributes(ad *netlink.AttributeDecoder) ([]Attribute, error) {

// encodeAttributes encodes a list of Attributes into the given netlink.AttributeEncoder.
func encodeAttributes(ae *netlink.AttributeEncoder, attrs []Attribute) error {

if ae == nil {
return errNilAttributeEncoder
}
Expand All @@ -260,9 +250,7 @@ func encodeAttributes(ae *netlink.AttributeEncoder, attrs []Attribute) error {
// This byte slice can then be copied into a netlink.Message's Data field after
// the nfHeaderLen offset.
func MarshalAttributes(attrs []Attribute) ([]byte, error) {

ae := NewAttributeEncoder()

if err := encodeAttributes(ae, attrs); err != nil {
return nil, err
}
Expand All @@ -277,7 +265,6 @@ func MarshalAttributes(attrs []Attribute) ([]byte, error) {

// UnmarshalAttributes unmarshals a byte slice into a list of Attributes.
func UnmarshalAttributes(b []byte) ([]Attribute, error) {

ad, err := NewAttributeDecoder(b)
if err != nil {
return nil, err
Expand Down
12 changes: 2 additions & 10 deletions attribute_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package netfilter

import (
"errors"
"strings"
"testing"

Expand Down Expand Up @@ -103,10 +102,8 @@ func TestAttributeString(t *testing.T) {
{
name: "attribute w/ nested attribute",
attr: Attribute{
Nested: true,
Children: []Attribute{
Attribute{},
},
Nested: true,
Children: []Attribute{{}},
},
txt: "<Length 0, Type 0, Nested true, 1 Children ([<Length 0, Type 0, Nested false, NetByteOrder false, []>])>",
},
Expand Down Expand Up @@ -223,7 +220,6 @@ func TestAttributeDecoderErrors(t *testing.T) {
8, 0, 0, 192, // 192 = nested + netByteOrder
0, 0, 0, 0,
},
err: errInvalidAttributeFlags,
},
{
name: "invalid attribute flags on nested attribute",
Expand All @@ -232,21 +228,17 @@ func TestAttributeDecoderErrors(t *testing.T) {
8, 0, 0, 192, // 192 = nested + netByteOrder
0, 0, 0, 0,
},
err: errInvalidAttributeFlags,
},
{
name: "decoding invalid attribute",
b: []byte{4, 0, 0},
err: errors.New("invalid attribute; length too short or too large"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := UnmarshalAttributes(tt.b)
require.Error(t, err)
require.Error(t, tt.err)
require.EqualError(t, err, tt.err.Error())
})
}
}
Expand Down
Loading

0 comments on commit 3d7ebe2

Please sign in to comment.