You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
6.5 KiB
Go
235 lines
6.5 KiB
Go
//go:build linux
|
|
|
|
package networkobserve
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/netip"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
conntrack "github.com/ti-mo/conntrack"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
type fakeConntrackDumper struct {
|
|
mu sync.Mutex
|
|
flows []conntrack.Flow
|
|
err error
|
|
dumpCalls int
|
|
closeCalls int
|
|
}
|
|
|
|
func (dumper *fakeConntrackDumper) Dump(*conntrack.DumpOptions) ([]conntrack.Flow, error) {
|
|
dumper.mu.Lock()
|
|
defer dumper.mu.Unlock()
|
|
dumper.dumpCalls++
|
|
return dumper.flows, dumper.err
|
|
}
|
|
|
|
func (dumper *fakeConntrackDumper) Close() error {
|
|
dumper.mu.Lock()
|
|
defer dumper.mu.Unlock()
|
|
dumper.closeCalls++
|
|
return nil
|
|
}
|
|
|
|
func (dumper *fakeConntrackDumper) calls() (int, int) {
|
|
dumper.mu.Lock()
|
|
defer dumper.mu.Unlock()
|
|
return dumper.dumpCalls, dumper.closeCalls
|
|
}
|
|
|
|
func sourceForDumper(dumper conntrackDumper, currentEntries uint64) *NetlinkSource {
|
|
return newNetlinkSource(func() (conntrackDumper, error) { return dumper, nil },
|
|
func() (uint64, error) { return currentEntries, nil }, time.Second)
|
|
}
|
|
|
|
func TestNetlinkSourceSafetyInputIsCurrentConntrackCount(t *testing.T) {
|
|
if conntrackCountPath != "/proc/sys/net/netfilter/nf_conntrack_count" {
|
|
t.Fatalf("conntrack safety input = %q", conntrackCountPath)
|
|
}
|
|
}
|
|
|
|
func TestNetlinkSourceConvertsOriginalTupleReadOnly(t *testing.T) {
|
|
dumper := &fakeConntrackDumper{flows: []conntrack.Flow{{
|
|
TupleOrig: conntrack.Tuple{
|
|
IP: conntrack.IPTuple{SourceAddress: netip.MustParseAddr("192.0.2.10"),
|
|
DestinationAddress: netip.MustParseAddr("198.51.100.20")},
|
|
Proto: conntrack.ProtoTuple{Protocol: 6, SourcePort: 50000, DestinationPort: 443},
|
|
},
|
|
Zone: 7,
|
|
Status: conntrack.StatusSeenReply,
|
|
ProtoInfo: conntrack.ProtoInfo{TCP: &conntrack.ProtoInfoTCP{State: 3}},
|
|
CountersOrig: conntrack.Counter{Packets: 1, Bytes: 64},
|
|
}}}
|
|
source := sourceForDumper(dumper, 1024)
|
|
snapshot, err := source.Snapshot(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(snapshot.Flows) != 1 || snapshot.Flows[0].SourcePort != 50000 ||
|
|
snapshot.Flows[0].DestinationPort != 443 || snapshot.Flows[0].Zone != 7 ||
|
|
!snapshot.Flows[0].SeenReply || snapshot.Flows[0].TCPState != 3 {
|
|
t.Fatalf("snapshot = %#v", snapshot)
|
|
}
|
|
dumpCalls, _ := dumper.calls()
|
|
if dumpCalls != 1 {
|
|
t.Fatalf("Dump calls = %d", dumpCalls)
|
|
}
|
|
}
|
|
|
|
func TestNetlinkSourceAllowsEmptyCurrentTable(t *testing.T) {
|
|
dumper := &fakeConntrackDumper{}
|
|
source := sourceForDumper(dumper, 0)
|
|
if _, err := source.Snapshot(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dumpCalls, _ := dumper.calls()
|
|
if dumpCalls != 1 {
|
|
t.Fatalf("Dump calls = %d", dumpCalls)
|
|
}
|
|
}
|
|
|
|
func TestNetlinkSourceRefusesCurrentTableAboveBudgetBeforeDump(t *testing.T) {
|
|
dumper := &fakeConntrackDumper{}
|
|
source := sourceForDumper(dumper, HardMaxKernelFlows+1)
|
|
if _, err := source.Snapshot(context.Background()); !errors.Is(err, ErrConntrackUnavailable) {
|
|
t.Fatalf("Snapshot error = %v", err)
|
|
}
|
|
dumpCalls, _ := dumper.calls()
|
|
if dumpCalls != 0 {
|
|
t.Fatalf("unbounded table was dumped %d times", dumpCalls)
|
|
}
|
|
}
|
|
|
|
func TestNetlinkErrorsMapToStableCapabilityReasons(t *testing.T) {
|
|
tests := []struct {
|
|
err error
|
|
want error
|
|
}{
|
|
{err: unix.EPERM, want: ErrPermissionDenied},
|
|
{err: unix.EPROTONOSUPPORT, want: ErrNetlinkUnavailable},
|
|
{err: unix.ENOENT, want: ErrConntrackUnavailable},
|
|
{err: errors.New("private kernel detail"), want: ErrConntrackUnavailable},
|
|
}
|
|
for _, test := range tests {
|
|
if got := classifyNetlinkError(test.err); !errors.Is(got, test.want) {
|
|
t.Fatalf("classify(%v) = %v, want %v", test.err, got, test.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNetlinkSourceCloseIsIdempotent(t *testing.T) {
|
|
dumper := &fakeConntrackDumper{}
|
|
source := sourceForDumper(dumper, 1)
|
|
// No connection is opened until Snapshot, so closing an idle source must
|
|
// not create or close a socket merely to satisfy shutdown.
|
|
if err := source.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := source.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, closeCalls := dumper.calls()
|
|
if closeCalls != 0 {
|
|
t.Fatalf("idle Close calls = %d", closeCalls)
|
|
}
|
|
}
|
|
|
|
type blockingConntrackDumper struct {
|
|
started chan struct{}
|
|
closed chan struct{}
|
|
startOnce sync.Once
|
|
closeOnce sync.Once
|
|
}
|
|
|
|
func newBlockingConntrackDumper() *blockingConntrackDumper {
|
|
return &blockingConntrackDumper{started: make(chan struct{}), closed: make(chan struct{})}
|
|
}
|
|
|
|
func (dumper *blockingConntrackDumper) Dump(*conntrack.DumpOptions) ([]conntrack.Flow, error) {
|
|
dumper.startOnce.Do(func() { close(dumper.started) })
|
|
<-dumper.closed
|
|
return nil, errors.New("netlink socket closed")
|
|
}
|
|
|
|
func (dumper *blockingConntrackDumper) Close() error {
|
|
dumper.closeOnce.Do(func() { close(dumper.closed) })
|
|
return nil
|
|
}
|
|
|
|
func TestNetlinkSourceCancellationInterruptsBlockedDump(t *testing.T) {
|
|
dumper := newBlockingConntrackDumper()
|
|
source := sourceForDumper(dumper, 1024)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
result := make(chan error, 1)
|
|
go func() {
|
|
_, err := source.Snapshot(ctx)
|
|
result <- err
|
|
}()
|
|
select {
|
|
case <-dumper.started:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("Dump did not start")
|
|
}
|
|
cancel()
|
|
select {
|
|
case err := <-result:
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("Snapshot error = %v", err)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("context cancellation did not interrupt Dump")
|
|
}
|
|
}
|
|
|
|
func TestNetlinkSourceHardTimeoutInterruptsBlockedDump(t *testing.T) {
|
|
dumper := newBlockingConntrackDumper()
|
|
source := newNetlinkSource(func() (conntrackDumper, error) { return dumper, nil },
|
|
func() (uint64, error) { return 1024, nil }, 20*time.Millisecond)
|
|
started := time.Now()
|
|
_, err := source.Snapshot(context.Background())
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("Snapshot error = %v", err)
|
|
}
|
|
if time.Since(started) > time.Second {
|
|
t.Fatal("hard timeout did not interrupt Dump")
|
|
}
|
|
}
|
|
|
|
func TestNetlinkSourceCloseInterruptsBlockedDump(t *testing.T) {
|
|
dumper := newBlockingConntrackDumper()
|
|
source := sourceForDumper(dumper, 1024)
|
|
result := make(chan error, 1)
|
|
go func() {
|
|
_, err := source.Snapshot(context.Background())
|
|
result <- err
|
|
}()
|
|
select {
|
|
case <-dumper.started:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("Dump did not start")
|
|
}
|
|
closed := make(chan error, 1)
|
|
go func() { closed <- source.Close() }()
|
|
select {
|
|
case err := <-closed:
|
|
if err != nil {
|
|
t.Fatalf("Close error = %v", err)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("Close did not interrupt Dump")
|
|
}
|
|
select {
|
|
case err := <-result:
|
|
if !errors.Is(err, ErrConntrackUnavailable) {
|
|
t.Fatalf("Snapshot error = %v", err)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("Snapshot remained blocked after Close")
|
|
}
|
|
}
|