diff --git a/pcap/pcap.go b/pcap/pcap.go index cfaa4415d..9a794ab2c 100644 --- a/pcap/pcap.go +++ b/pcap/pcap.go @@ -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 diff --git a/pcap/pcap_test.go b/pcap/pcap_test.go index 4ae29780e..107f46a83 100644 --- a/pcap/pcap_test.go +++ b/pcap/pcap_test.go @@ -13,6 +13,7 @@ import ( "log" "os" "testing" + "time" "github.com/google/gopacket" "github.com/google/gopacket/layers" @@ -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 {