Skip to content

Commit

Permalink
Replace crude base-64 encoded test file embedding with embed package …
Browse files Browse the repository at this point in the history
…functionality
  • Loading branch information
fako1024 committed May 6, 2023
1 parent 61e01bd commit 7c74849
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 57 deletions.
35 changes: 0 additions & 35 deletions capture/pcap/pcap_data_test.go

This file was deleted.

58 changes: 36 additions & 22 deletions capture/pcap/pcap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package pcap

import (
"bytes"
"encoding/base64"
"embed"
"io"
"io/fs"
"net"
"os"
"path/filepath"
"testing"

"github.com/fako1024/slimcap/capture"
Expand All @@ -14,7 +16,12 @@ import (
"github.com/stretchr/testify/require"
)

var testDataLE, testDataBE []byte
const (
pcapTestInputNPackets = 6
testDataPath = "testdata"
)

var testData []fs.DirEntry

func TestInvalidInput(t *testing.T) {

Expand Down Expand Up @@ -53,12 +60,14 @@ func TestInvalidInput(t *testing.T) {

func TestReader(t *testing.T) {

for _, testData := range [][]byte{
testDataLE,
testDataBE,
} {
for _, dirent := range testData {
file, err := os.Open(filepath.Join(testDataPath, dirent.Name()))
require.Nil(t, err)
defer func() {
require.Nil(t, file.Close())
}()

src, err := NewSource("pcap", bytes.NewBuffer(testData))
src, err := NewSource("pcap", file)
require.Nil(t, err)

require.Equal(t, &link.Link{
Expand Down Expand Up @@ -86,7 +95,7 @@ func TestReader(t *testing.T) {
require.Equal(t, capture.PacketUnknown, pktType)
require.Nil(t, ipLayer)

src.NextPacketFn(func(payload []byte, totalLen uint32, pktType, ipLayerOffset byte) error {
err = src.NextPacketFn(func(payload []byte, totalLen uint32, pktType, ipLayerOffset byte) error {
require.Nil(t, payload)
require.Zero(t, totalLen)
require.Equal(t, capture.PacketUnknown, pktType)
Expand All @@ -107,11 +116,14 @@ func TestReader(t *testing.T) {

func TestMockPipe(t *testing.T) {

for _, testData := range [][]byte{
testDataLE,
testDataBE,
} {
src, err := NewSource("pcap", bytes.NewBuffer(testData))
for _, dirent := range testData {
file, err := os.Open(filepath.Join(testDataPath, dirent.Name()))
require.Nil(t, err)
defer func() {
require.Nil(t, file.Close())
}()

src, err := NewSource("pcap", file)
require.Nil(t, err)

// Setup a mock source
Expand Down Expand Up @@ -232,12 +244,14 @@ func TestCaptureMethods(t *testing.T) {
}

func testCaptureMethods(t *testing.T, fn func(t *testing.T, src *Source)) {
for _, dirent := range testData {
file, err := os.Open(filepath.Join(testDataPath, dirent.Name()))
require.Nil(t, err)
defer func() {
require.Nil(t, file.Close())
}()

for _, testData := range [][]byte{
testDataLE,
testDataBE,
} {
src, err := NewSource("pcap", bytes.NewBuffer(testData))
src, err := NewSource("pcap", file)
require.Nil(t, err)

for i := 0; i < pcapTestInputNPackets; i++ {
Expand All @@ -248,13 +262,13 @@ func testCaptureMethods(t *testing.T, fn func(t *testing.T, src *Source)) {
}
}

//go:embed testdata/*
var pcaps embed.FS

func TestMain(m *testing.M) {

var err error
if testDataLE, err = base64.StdEncoding.DecodeString(pcapTestInputLE); err != nil {
panic(err)
}
if testDataBE, err = base64.StdEncoding.DecodeString(pcapTestInputBE); err != nil {
if testData, err = pcaps.ReadDir(testDataPath); err != nil {
panic(err)
}

Expand Down
Binary file added capture/pcap/testdata/be.pcap
Binary file not shown.
Binary file added capture/pcap/testdata/be.pcap.gz
Binary file not shown.
Binary file added capture/pcap/testdata/le.pcap
Binary file not shown.
Binary file added capture/pcap/testdata/le.pcap.gz
Binary file not shown.

0 comments on commit 7c74849

Please sign in to comment.