(t *testing.T)
| 29 | } |
| 30 | |
| 31 | func TestBuildAuditNetlinkMessage(t *testing.T) { |
| 32 | testCases := []struct { |
| 33 | name string |
| 34 | msgType uint16 |
| 35 | message string |
| 36 | wantType uint16 |
| 37 | }{ |
| 38 | { |
| 39 | name: "simple-message", |
| 40 | msgType: auditUserLogin, |
| 41 | message: "op=login acct=test", |
| 42 | wantType: auditUserLogin, |
| 43 | }, |
| 44 | { |
| 45 | name: "message-with-quoted-fields", |
| 46 | msgType: auditUserLogin, |
| 47 | message: `op=login hostname="test-host" exe="/usr/bin/tailscaled" ts_user="user@example.com" ts_node="node.tail-scale.ts.net"`, |
| 48 | wantType: auditUserLogin, |
| 49 | }, |
| 50 | { |
| 51 | name: "message-with-special-chars", |
| 52 | msgType: auditUserLogin, |
| 53 | message: `op=login hostname="host with spaces" ts_user="user name@example.com" ts_display_name="User \"Quote\" Name"`, |
| 54 | wantType: auditUserLogin, |
| 55 | }, |
| 56 | { |
| 57 | name: "long-message-truncated", |
| 58 | msgType: auditUserLogin, |
| 59 | message: string(make([]byte, 2000)), |
| 60 | wantType: auditUserLogin, |
| 61 | }, |
| 62 | } |
| 63 | |
| 64 | for _, tc := range testCases { |
| 65 | t.Run(tc.name, func(t *testing.T) { |
| 66 | msg, err := buildAuditNetlinkMessage(tc.msgType, tc.message) |
| 67 | if err != nil { |
| 68 | t.Fatalf("buildAuditNetlinkMessage failed: %v", err) |
| 69 | } |
| 70 | |
| 71 | if len(msg) < syscall.NLMSG_HDRLEN { |
| 72 | t.Fatalf("message too short: got %d bytes, want at least %d", len(msg), syscall.NLMSG_HDRLEN) |
| 73 | } |
| 74 | |
| 75 | var nlh syscall.NlMsghdr |
| 76 | buf := bytes.NewReader(msg[:syscall.NLMSG_HDRLEN]) |
| 77 | if err := binary.Read(buf, binary.NativeEndian, &nlh); err != nil { |
| 78 | t.Fatalf("failed to parse netlink header: %v", err) |
| 79 | } |
| 80 | |
| 81 | if nlh.Type != tc.wantType { |
| 82 | t.Errorf("message type: got %d, want %d", nlh.Type, tc.wantType) |
| 83 | } |
| 84 | |
| 85 | if nlh.Flags != nlmFRequest { |
| 86 | t.Errorf("flags: got 0x%x, want 0x%x", nlh.Flags, nlmFRequest) |
| 87 | } |
| 88 |
nothing calls this directly
no test coverage detected
searching dependent graphs…