diff --git a/libbeat/common/flowhash/communityid_test.go b/libbeat/common/flowhash/communityid_test.go index 4398d60d946d..be6dbae91c4c 100644 --- a/libbeat/common/flowhash/communityid_test.go +++ b/libbeat/common/flowhash/communityid_test.go @@ -23,7 +23,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "net" "os" "path/filepath" @@ -33,9 +32,10 @@ import ( "github.com/google/gopacket" "github.com/google/gopacket/layers" - "github.com/google/gopacket/pcap" + "github.com/google/gopacket/pcapgo" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const ( @@ -60,7 +60,7 @@ func TestPCAPFiles(t *testing.T) { if *update { data := strings.Join(result, "") - err = ioutil.WriteFile(goldenName, []byte(data), 0644) + err = os.WriteFile(goldenName, []byte(data), 0644) if err != nil { t.Fatal(err) } @@ -104,11 +104,13 @@ func typeCodeCode(tc uint16) uint8 { func getFlowsFromPCAP(t testing.TB, name, pcapFile string) []string { t.Helper() - r, err := pcap.OpenOffline(pcapFile) - if err != nil { - t.Fatal(err, name) - } - defer r.Close() + f, err := os.Open(pcapFile) + require.NoError(t, err) + + defer f.Close() + + r, err := pcapgo.NewReader(f) + require.NoError(t, err) packetSource := gopacket.NewPacketSource(r, r.LinkType()) var flows []string diff --git a/x-pack/filebeat/input/netflow/integration_test.go b/x-pack/filebeat/input/netflow/integration_test.go index b7bb2a3203d4..61e0f21ba2ac 100644 --- a/x-pack/filebeat/input/netflow/integration_test.go +++ b/x-pack/filebeat/input/netflow/integration_test.go @@ -30,7 +30,7 @@ import ( "github.com/elastic/elastic-agent-libs/monitoring" "github.com/google/gopacket" - "github.com/google/gopacket/pcap" + "github.com/google/gopacket/pcapgo" "github.com/stretchr/testify/require" ) @@ -217,17 +217,21 @@ func TestNetFlowIntegration(t *testing.T) { conn, err := net.DialUDP("udp", nil, udpAddr) require.NoError(t, err, "failed to open UDP connection") - // for more info look testdata/integration/test.md - f, err := pcap.OpenOffline("testdata/integration/test.pcap") + f, err := os.Open("testdata/integration/test.pcap") require.NoError(t, err, "failed to open pcap file") defer f.Close() + + r, err := pcapgo.NewReader(f) + require.NoError(t, err) + + // for more info look testdata/integration/test.md expectedEventsNumbers := 32 var totalBytes, totalPackets int rateLimit := 3000 limiter := rate.NewLimiter(rate.Limit(rateLimit), rateLimit) - packetSource := gopacket.NewPacketSource(f, f.LinkType()) + packetSource := gopacket.NewPacketSource(r, r.LinkType()) for pkt := range packetSource.Packets() { if totalPackets%rateLimit == 0 { diff --git a/x-pack/filebeat/input/netflow/netflow_test.go b/x-pack/filebeat/input/netflow/netflow_test.go index 127445af1b85..65383df4a98b 100644 --- a/x-pack/filebeat/input/netflow/netflow_test.go +++ b/x-pack/filebeat/input/netflow/netflow_test.go @@ -18,7 +18,7 @@ import ( "time" "github.com/google/gopacket" - "github.com/google/gopacket/pcap" + "github.com/google/gopacket/pcapgo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gopkg.in/yaml.v2" @@ -123,15 +123,18 @@ func TestNetFlow(t *testing.T) { conn, err := net.DialUDP("udp", nil, udpAddr) require.NoError(t, err) - f, err := pcap.OpenOffline(file) + f, err := os.Open(file) require.NoError(t, err) defer f.Close() + r, err := pcapgo.NewReader(f) + require.NoError(t, err) + goldenData := readGoldenFile(t, filepath.Join(goldenDir, testName+".pcap.golden.json")) // Process packets in PCAP and get flow records. var totalBytes, totalPackets int - packetSource := gopacket.NewPacketSource(f, f.LinkType()) + packetSource := gopacket.NewPacketSource(r, r.LinkType()) for pkt := range packetSource.Packets() { payloadData := pkt.TransportLayer().LayerPayload() @@ -341,11 +344,12 @@ func getFlowsFromDat(t testing.TB, name string, testCase TestCase) TestResult { func getFlowsFromPCAP(t testing.TB, name, pcapFile string) TestResult { t.Helper() - r, err := pcap.OpenOffline(pcapFile) - if err != nil { - t.Fatal(err) - } - defer r.Close() + f, err := os.Open(pcapFile) + require.NoError(t, err) + defer f.Close() + + r, err := pcapgo.NewReader(f) + require.NoError(t, err) config := decoder.NewConfig(). WithProtocols(protocol.Registry.All()...).