Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions io/audit_record_nil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io

import (
"runtime/debug"
"testing"

"github.com/dreadl0ck/netcap/encoder"
"github.com/dreadl0ck/netcap/types"
)

// TestAuditRecordSerializationOnZeroValue guards against nil dereferences in the
// serialization helpers. Optional nested messages are nil whenever the protocol
// did not carry them, which is the common case rather than an edge case: BFD
// packets without authentication used to segfault the whole capture here.
func TestAuditRecordSerializationOnZeroValue(t *testing.T) {
// Encode() dereferences the encoder config, which the --encode path installs.
encoder.SetConfig(&encoder.Config{ZScore: true})

covered := 0

for num, name := range types.Type_name {
typ := types.Type(num)
if typ == types.Type_NC_Header {
continue
}

record, ok := InitRecord(typ).(types.AuditRecord)
if !ok {
// Not every enum entry has a registered audit record implementation.
continue
}
covered++

t.Run(name, func(t *testing.T) {
// Recover so one broken type does not hide the rest.
defer func() {
if r := recover(); r != nil {
t.Fatalf("panic: %v\n%s", r, debug.Stack())
}
}()

header := record.CSVHeader()
values := record.CSVRecord()
if len(header) != len(values) {
t.Errorf("CSVHeader has %d fields, CSVRecord has %d", len(header), len(values))
}
if encoded := record.Encode(); encoded == nil && len(header) > 0 {
t.Error("Encode returned nil")
}
if _, err := record.JSON(); err != nil {
t.Errorf("JSON: %v", err)
}
record.Time()
record.Src()
record.Dst()
record.NetcapType()
record.SetPacketContext(&types.PacketContext{})

// Prometheus panics when the label cardinality of the vector does
// not match the values Inc() derives from CSVRecord. Calling it
// twice is safe: WithLabelValues returns the same child.
record.Inc()
record.Inc()
})
}

if covered < 50 {
t.Fatalf("only %d audit record types covered, expected the full set", covered)
}
}

// TestBFDNilAuthHeader pins the specific regression: BFD without authentication.
func TestBFDNilAuthHeader(t *testing.T) {
bfd := &types.BFD{Timestamp: 1, AuthPresent: false}
if bfd.AuthHeader != nil {
t.Fatal("expected a nil AuthHeader")
}
if got := bfd.CSVRecord(); got[len(got)-1] != "" {
t.Errorf("AuthHeader field = %q, want empty", got[len(got)-1])
}

withAuth := &types.BFD{
Timestamp: 1,
AuthPresent: true,
AuthHeader: &types.BFDAuthHeader{AuthType: 2, KeyID: 3, SequenceNumber: 4, Data: []byte{0xab}},
}
if got := withAuth.CSVRecord(); got[len(got)-1] == "" {
t.Error("populated AuthHeader must still be serialized")
}
}
7 changes: 6 additions & 1 deletion types/bfd.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ func (b *BFD) Time() int64 {
return b.Timestamp
}

func (bah BFDAuthHeader) getString() string {
func (bah *BFDAuthHeader) getString() string {
// BFD packets without authentication carry no auth header.
if bah == nil {
return ""
}

var b strings.Builder

b.WriteString(StructureBegin)
Expand Down
6 changes: 5 additions & 1 deletion types/cisco_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ func (cd *CiscoDiscovery) Time() int64 {
return cd.Timestamp
}

func (v CiscoDiscoveryValue) toString() string {
func (v *CiscoDiscoveryValue) toString() string {
if v == nil {
return ""
}

var b strings.Builder

b.WriteString(StructureBegin)
Expand Down
32 changes: 32 additions & 0 deletions types/cisco_discovery_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func (a *CiscoDiscoveryInfo) Time() int64 {
}

func (c *CDPHello) toString() string {
if c == nil {
return ""
}

var b strings.Builder

b.WriteString(StructureBegin)
Expand Down Expand Up @@ -177,6 +181,10 @@ func (c *CDPHello) toString() string {
}

func (c *CDPCapabilities) toString() string {
if c == nil {
return ""
}

var b strings.Builder

b.WriteString(StructureBegin)
Expand Down Expand Up @@ -204,6 +212,10 @@ func (c *CDPCapabilities) toString() string {
}

func (i *IPNet) toString() string {
if i == nil {
return ""
}

var b strings.Builder

b.WriteString(StructureBegin)
Expand All @@ -216,6 +228,10 @@ func (i *IPNet) toString() string {
}

func (c *CDPVLANDialogue) toString() string {
if c == nil {
return ""
}

var b strings.Builder

b.WriteString(StructureBegin)
Expand All @@ -228,6 +244,10 @@ func (c *CDPVLANDialogue) toString() string {
}

func (c *CDPPowerDialogue) toString() string {
if c == nil {
return ""
}

vals := make([]string, 0, len(c.Values))

for _, v := range c.Values {
Expand All @@ -248,6 +268,10 @@ func (c *CDPPowerDialogue) toString() string {
}

func (c *CDPSparePairPoE) toString() string {
if c == nil {
return ""
}

var b strings.Builder

b.WriteString(StructureBegin)
Expand All @@ -264,6 +288,10 @@ func (c *CDPSparePairPoE) toString() string {
}

func (c *CDPEnergyWise) toString() string {
if c == nil {
return ""
}

var b strings.Builder

b.WriteString(StructureBegin)
Expand Down Expand Up @@ -304,6 +332,10 @@ func (c *CDPEnergyWise) toString() string {
}

func (c *CDPLocation) toString() string {
if c == nil {
return ""
}

var b strings.Builder

b.WriteString(StructureBegin)
Expand Down
6 changes: 5 additions & 1 deletion types/dhcp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ func (d *DHCPv4) Time() int64 {
return d.Timestamp
}

func (d DHCPOption) toString() string {
func (d *DHCPOption) toString() string {
if d == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(formatInt32(d.Type))
Expand Down
6 changes: 5 additions & 1 deletion types/dhcp6.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ func (d *DHCPv6) Time() int64 {
return d.Timestamp
}

func (d DHCPv6Option) toString() string {
func (d *DHCPv6Option) toString() string {
if d == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(formatInt32(d.Code))
Expand Down
20 changes: 20 additions & 0 deletions types/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func (d *DNS) Time() int64 {
}

func (q *DNSQuestion) toString() string {
if q == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(q.Name)
Expand All @@ -145,6 +149,10 @@ func (q *DNSQuestion) toString() string {
}

func (q *DNSResourceRecord) toString() string {
if q == nil {
return ""
}

txts := make([]string, 0, len(q.TXTs))
for _, t := range q.TXTs {
txts = append(txts, string(t))
Expand Down Expand Up @@ -188,6 +196,10 @@ func (q *DNSResourceRecord) toString() string {
}

func (q *DNSSOA) toString() string {
if q == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(string(q.MName))
Expand All @@ -208,6 +220,10 @@ func (q *DNSSOA) toString() string {
}

func (q *DNSSRV) toString() string {
if q == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(formatInt32(q.Priority))
Expand All @@ -222,6 +238,10 @@ func (q *DNSSRV) toString() string {
}

func (q *DNSMX) toString() string {
if q == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(formatInt32(q.Preference))
Expand Down
32 changes: 30 additions & 2 deletions types/dot11.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ func (d *Dot11) Time() int64 {
return d.Timestamp
}

func (d Dot11QOS) toString() string {
func (d *Dot11QOS) toString() string {
if d == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(formatInt32(d.TID))
Expand All @@ -103,7 +107,11 @@ func (d Dot11QOS) toString() string {
return b.String()
}

func (d Dot11HTControl) toString() string {
func (d *Dot11HTControl) toString() string {
if d == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(strconv.FormatBool(d.ACConstraint))
Expand All @@ -118,6 +126,10 @@ func (d Dot11HTControl) toString() string {
}

func (d *Dot11HTControlVHT) toString() string {
if d == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(strconv.FormatBool(d.MRQ))
Expand All @@ -144,6 +156,10 @@ func (d *Dot11HTControlVHT) toString() string {
}

func (d *Dot11HTControlMFB) toString() string {
if d == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(formatInt32(d.NumSTS))
Expand All @@ -158,6 +174,10 @@ func (d *Dot11HTControlMFB) toString() string {
}

func (d *Dot11HTControlHT) toString() string {
if d == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(d.LinkAdapationControl.toString())
Expand All @@ -176,6 +196,10 @@ func (d *Dot11HTControlHT) toString() string {
}

func (d *Dot11LinkAdapationControl) toString() string {
if d == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(strconv.FormatBool(d.TRQ))
Expand All @@ -194,6 +218,10 @@ func (d *Dot11LinkAdapationControl) toString() string {
}

func (d *Dot11ASEL) toString() string {
if d == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(formatInt32(d.Command))
Expand Down
6 changes: 5 additions & 1 deletion types/geneve.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func (i *Geneve) Time() int64 {
return i.Timestamp
}

func (i GeneveOption) toString() string {
func (i *GeneveOption) toString() string {
if i == nil {
return ""
}

var b strings.Builder
b.WriteString(StructureBegin)
b.WriteString(formatInt32(i.Class))
Expand Down
2 changes: 0 additions & 2 deletions types/gre.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ var fieldsGRE = []string{
fieldRouting, // *GRERouting
fieldSrcIP,
fieldDstIP,
fieldSrcPort,
fieldDstPort,
}

// CSVHeader returns the CSV header for the audit record.
Expand Down
Loading
Loading