Skip to content

Commit

Permalink
Merge pull request #35 from slackhq/better-client-test
Browse files Browse the repository at this point in the history
Slightly better netlink client tests
  • Loading branch information
nbrownus authored Aug 8, 2017
2 parents ac3d335 + 963c344 commit 01f125e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
6 changes: 5 additions & 1 deletion audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ func main() {
el.Fatal(err)
}

nlClient := NewNetlinkClient(config.GetInt("socket_buffer.receive"))
nlClient, err := NewNetlinkClient(config.GetInt("socket_buffer.receive"))
if err != nil {
el.Fatal(err)
}

marshaller := NewAuditMarshaller(
writer,
uint16(config.GetInt("events.min")),
Expand Down
9 changes: 5 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync/atomic"
"syscall"
"time"
"fmt"
)

// Endianness is an alias for what we assume is the current machine endianness
Expand Down Expand Up @@ -42,10 +43,10 @@ type NetlinkClient struct {
}

// NewNetlinkClient creates a new NetLinkClient and optionally tries to modify the netlink recv buffer
func NewNetlinkClient(recvSize int) *NetlinkClient {
func NewNetlinkClient(recvSize int) (*NetlinkClient, error) {
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_AUDIT)
if err != nil {
el.Fatalln("Could not create a socket:", err)
return nil, fmt.Errorf("Could not create a socket: %s", err)
}

n := &NetlinkClient{
Expand All @@ -56,7 +57,7 @@ func NewNetlinkClient(recvSize int) *NetlinkClient {

if err = syscall.Bind(fd, n.address); err != nil {
syscall.Close(fd)
el.Fatalln("Could not bind to netlink socket:", err)
return nil, fmt.Errorf("Could not bind to netlink socket: %s", err)
}

// Set the buffer size if we were asked
Expand All @@ -78,7 +79,7 @@ func NewNetlinkClient(recvSize int) *NetlinkClient {
}
}()

return n
return n, nil
}

// Send will send a packet and payload to the netlink socket without waiting for a response
Expand Down
23 changes: 14 additions & 9 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,20 @@ func TestNewNetlinkClient(t *testing.T) {
lb, elb := hookLogger()
defer resetLogger()

n := NewNetlinkClient(1024)

assert.True(t, (n.fd > 0), "No file descriptor")
assert.True(t, (n.address != nil), "Address was nil")
assert.Equal(t, uint32(0), n.seq, "Seq should start at 0")
assert.True(t, MAX_AUDIT_MESSAGE_LENGTH >= len(n.buf), "Client buffer is too small")

assert.Equal(t, "Socket receive buffer size: ", lb.String()[:28], "Expected some nice log lines")
assert.Equal(t, "", elb.String(), "Did not expect any error messages")
n, err := NewNetlinkClient(1024)

assert.Nil(t, err)
if n == nil {
t.Fatal("Expected a netlink client but had an error instead!")
} else {
assert.True(t, (n.fd > 0), "No file descriptor")
assert.True(t, (n.address != nil), "Address was nil")
assert.Equal(t, uint32(0), n.seq, "Seq should start at 0")
assert.True(t, MAX_AUDIT_MESSAGE_LENGTH >= len(n.buf), "Client buffer is too small")

assert.Equal(t, "Socket receive buffer size: ", lb.String()[:28], "Expected some nice log lines")
assert.Equal(t, "", elb.String(), "Did not expect any error messages")
}
}

// Helper to make a client listening on a unix socket
Expand Down

0 comments on commit 01f125e

Please sign in to comment.