Skip to content
Open
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
2 changes: 1 addition & 1 deletion pcap/pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ func (p *Handle) SnapLen() int {

// Resolution returns the timestamp resolution of acquired timestamps before scaling to NanosecondTimestampResolution.
func (p *Handle) Resolution() gopacket.TimestampResolution {
if p.nanoSecsFactor == 1 {
if p.nanoSecsFactor != 1 {
return gopacket.TimestampResolutionMicrosecond
}
return gopacket.TimestampResolutionNanosecond
Expand Down
40 changes: 40 additions & 0 deletions pcap/pcap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"log"
"os"
"testing"
"time"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
Expand Down Expand Up @@ -261,6 +262,45 @@ func TestBPFInstruction(t *testing.T) {
}
}

func TestHandleResolution(t *testing.T) {
tests := []struct {
name string
nanoSecsFactor int64
expectedResolution gopacket.TimestampResolution
}{
{
name: "Nanosecond Resolution (Factor 1)",
nanoSecsFactor: 1,
expectedResolution: gopacket.TimestampResolutionNanosecond,
},
{
name: "Microsecond Resolution (Factor 1000)",
nanoSecsFactor: 1000,
expectedResolution: gopacket.TimestampResolutionMicrosecond,
},
{
name: "Other Factor",
nanoSecsFactor: 500,
expectedResolution: gopacket.TimestampResolutionMicrosecond,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Handle{
nanoSecsFactor: tt.nanoSecsFactor,
timeout: time.Second,
}

got := p.Resolution()

if got != tt.expectedResolution {
t.Errorf("Resolution() got = %v, want %v", got, tt.expectedResolution)
}
})
}
}

func ExampleBPF() {
handle, err := OpenOffline("test_ethernet.pcap")
if err != nil {
Expand Down