From 963c344ad49de6938a80f9c7acbc30d2b9a251f5 Mon Sep 17 00:00:00 2001 From: Nate Brown Date: Mon, 7 Aug 2017 12:20:50 -0700 Subject: [PATCH] Slightly better netlink client tests --- audit.go | 6 +++++- client.go | 9 +++++---- client_test.go | 23 ++++++++++++++--------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/audit.go b/audit.go index d5a7a77..9cc6cb0 100644 --- a/audit.go +++ b/audit.go @@ -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")), diff --git a/client.go b/client.go index 2859639..7538cb1 100644 --- a/client.go +++ b/client.go @@ -7,6 +7,7 @@ import ( "sync/atomic" "syscall" "time" + "fmt" ) // Endianness is an alias for what we assume is the current machine endianness @@ -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{ @@ -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 @@ -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 diff --git a/client_test.go b/client_test.go index bc79df1..440fd23 100644 --- a/client_test.go +++ b/client_test.go @@ -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