-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 7.59.x] [NTWK-562] Add test for packet capture info (#30432)
Co-authored-by: pimlu <[email protected]>
- Loading branch information
1 parent
527fe0e
commit 25b842b
Showing
2 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2024-present Datadog, Inc. | ||
|
||
//go:build linux | ||
|
||
package filter | ||
|
||
import ( | ||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/afpacket" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sys/unix" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type mockPacketReader struct { | ||
data []byte | ||
ci gopacket.CaptureInfo | ||
err error | ||
} | ||
|
||
func (m *mockPacketReader) ZeroCopyReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error) { | ||
return m.data, m.ci, m.err | ||
} | ||
|
||
func mockCaptureInfo(ancillaryData []interface{}) gopacket.CaptureInfo { | ||
return gopacket.CaptureInfo{ | ||
Timestamp: time.Now(), | ||
CaptureLength: 0, | ||
Length: 0, | ||
InterfaceIndex: 0, | ||
AncillaryData: ancillaryData, | ||
} | ||
} | ||
|
||
func expectAncillaryPktType(t *testing.T, ancillaryData []interface{}, pktType uint8) { | ||
exit := make(chan struct{}) | ||
|
||
p := mockPacketReader{ | ||
data: []byte{}, | ||
ci: mockCaptureInfo(ancillaryData), | ||
err: nil, | ||
} | ||
|
||
visited := false | ||
|
||
err := visitPackets(&p, exit, func(_ []byte, info PacketInfo, _ time.Time) error { | ||
// make sure the callback ran since it's responsible for the require call | ||
visited = true | ||
|
||
// convert to linux packet info | ||
pktInfo := info.(*AFPacketInfo) | ||
// use assert so that we close the exit channel on failure | ||
assert.Equal(t, pktType, pktInfo.PktType) | ||
|
||
// trigger exit so it only reads one packet | ||
close(exit) | ||
return nil | ||
}) | ||
require.NoError(t, err) | ||
require.True(t, visited) | ||
} | ||
|
||
func TestVisitingRegularPacketOutgoing(t *testing.T) { | ||
expectAncillaryPktType(t, []interface{}{ | ||
afpacket.AncillaryPktType{ | ||
Type: unix.PACKET_OUTGOING, | ||
}, | ||
}, unix.PACKET_OUTGOING) | ||
} | ||
|
||
func TestVisitingVLANPacketOutgoing(t *testing.T) { | ||
expectAncillaryPktType(t, []interface{}{ | ||
afpacket.AncillaryVLAN{ | ||
VLAN: 0, | ||
}, | ||
afpacket.AncillaryPktType{ | ||
Type: unix.PACKET_OUTGOING, | ||
}, | ||
}, unix.PACKET_OUTGOING) | ||
} | ||
|
||
func TestVisitingRegularPacketIncoming(t *testing.T) { | ||
expectAncillaryPktType(t, []interface{}{ | ||
afpacket.AncillaryPktType{ | ||
Type: unix.PACKET_HOST, | ||
}, | ||
}, unix.PACKET_HOST) | ||
} | ||
|
||
func TestVisitingVLANPacketIncoming(t *testing.T) { | ||
expectAncillaryPktType(t, []interface{}{ | ||
afpacket.AncillaryVLAN{ | ||
VLAN: 0, | ||
}, | ||
afpacket.AncillaryPktType{ | ||
Type: unix.PACKET_HOST, | ||
}, | ||
}, unix.PACKET_HOST) | ||
} |