Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
//go:build linux
package scanner
import (
"math"
"net"
"os"
"path/filepath"
"testing"
"go.uber.org/zap"
)
func TestParseHexIPPortIPv4(t *testing.T) {
// 0100007F:01BB → 127.0.0.1:443 (little-endian)
ip, port := parseHexIPPort("0100007F:01BB", false)
if port != 443 {
t.Fatalf("expected port 443, got %d", port)
}
expected := net.IPv4(127, 0, 0, 1).To4()
if !ip.Equal(expected) {
t.Fatalf("expected %s, got %s", expected, ip)
}
}
func TestParseHexIPPortIPv4Google(t *testing.T) {
// D83AC9AC:01BB → 172.201.58.216:443 (little-endian)
ip, port := parseHexIPPort("D83AC9AC:01BB", false)
if port != 443 {
t.Fatalf("expected port 443, got %d", port)
}
if ip == nil {
t.Fatal("expected non-nil IP")
}
if ip[0] != 172 || ip[1] != 201 || ip[2] != 58 || ip[3] != 216 {
t.Fatalf("unexpected IP: %s", ip)
}
}
func TestParseHexIPPortInvalidHex(t *testing.T) {
ip, _ := parseHexIPPort("ZZZZZZZZ:01BB", false)
if ip != nil {
t.Fatalf("expected nil IP for invalid hex, got %s", ip)
}
}
func TestParseHexIPPortInvalidFormat(t *testing.T) {
ip, _ := parseHexIPPort("no-colon", false)
if ip != nil {
t.Fatal("expected nil IP for missing colon separator")
}
}
func TestParseHexIPPortIPv6Mapped(t *testing.T) {
// IPv4-mapped IPv6: ::ffff:127.0.0.1 = 00000000 00000000 FFFF0000 0100007F
ip, _ := parseHexIPPort("00000000000000000000FFFF00000100007F:01BB", false)
// This is malformed for non-v6 mode — should return nil
if ip != nil {
t.Fatalf("expected nil for oversized hex without v6 flag, got %s", ip)
}
// Proper v6 parsing
var port uint16
ip, port = parseHexIPPort("0000000000000000FFFF00000100007F:01BB", true)
if port != 443 {
t.Fatalf("expected port 443, got %d", port)
}
if ip == nil {
t.Fatal("expected non-nil IP for IPv4-mapped v6")
}
expected := net.IPv4(127, 0, 0, 1).To4()
if !ip.Equal(expected) {
t.Fatalf("expected %s, got %s", expected, ip)
}
}
func TestParseHexIPPortIPv6NonMapped(t *testing.T) {
// Pure IPv6 (not mapped) should return nil
ip, _ := parseHexIPPort("00000000000000000000000000000001:01BB", true)
if ip != nil {
t.Fatalf("expected nil for non-mapped IPv6, got %s", ip)
}
}
func TestParseHexIPPortInvalidPort(t *testing.T) {
ip, port := parseHexIPPort("0100007F:ZZZZ", false)
if ip != nil || port != 0 {
t.Fatalf("expected nil/0 for invalid port hex, got %s:%d", ip, port)
}
}
func TestReadNetnsInode(t *testing.T) {
// Read our own netns inode — should be non-zero on Linux
pidDir := filepath.Join("/proc", "self")
inode := readNetnsInode(pidDir)
if inode == 0 {
t.Fatal("expected non-zero netns inode for self")
}
}
func TestReadNetnsInodeInvalidPath(t *testing.T) {
inode := readNetnsInode("/nonexistent/pid/dir")
if inode != 0 {
t.Fatalf("expected 0 for invalid path, got %d", inode)
}
}
func TestReadSocketInodes(t *testing.T) {
// Read our own fd directory — should have at least some socket inodes
fdDir := filepath.Join("/proc", "self", "fd")
inodes := readSocketInodes(fdDir)
// We don't assert count since it depends on what's open,
// but the function should not panic
if inodes == nil {
// This is okay — process might not have socket fds
t.Log("no socket inodes found (acceptable)")
}
}
func TestReadSocketInodesInvalidPath(t *testing.T) {
inodes := readSocketInodes("/nonexistent/fd/dir")
if inodes != nil {
t.Fatalf("expected nil for invalid path, got %v", inodes)
}
}
func TestScanSkipsInvalidPIDs(t *testing.T) {
logger, _ := zap.NewDevelopment()
cs := NewConnScanner("/proc", logger)
// With no known IPs, Scan should return nil immediately
events := cs.Scan()
if events != nil {
t.Fatalf("expected nil events with no known IPs, got %d", len(events))
}
}
func TestScanWithKnownIPsDoesNotPanic(t *testing.T) {
logger, _ := zap.NewDevelopment()
cs := NewConnScanner("/proc", logger)
// Add a bogus endpoint so the scanner actually iterates /proc
key := endpointKey{Port: 443}
key.Addr[0], key.Addr[1], key.Addr[2], key.Addr[3] = 192, 0, 2, 1
cs.knownEndpoints[key] = 1
// Should not panic even scanning real /proc
events := cs.Scan()
_ = events
}
func TestPIDBoundsCheckInScan(t *testing.T) {
// Verify that the PID > MaxUint32 guard exists by checking
// that the bounds constant is used correctly
if math.MaxUint32 != 4294967295 {
t.Fatal("MaxUint32 sanity check failed")
}
}
func TestConnScannerUpdateEndpoints(t *testing.T) {
logger, _ := zap.NewDevelopment()
cs := NewConnScanner("/proc", logger)
openai := endpointKey{Port: 443}
copy(openai.Addr[:], net.ParseIP("104.18.6.192").To4())
litellm := endpointKey{Port: 4000}
copy(litellm.Addr[:], net.ParseIP("10.0.0.5").To4())
cs.UpdateEndpoints(map[endpointKey]uint8{
openai: 1,
litellm: providerCustom,
})
if len(cs.knownEndpoints) != 2 {
t.Fatalf("expected 2 known endpoints, got %d", len(cs.knownEndpoints))
}
if cs.knownEndpoints[litellm] != providerCustom {
t.Fatalf("expected litellm provider for :4000 endpoint, got %d", cs.knownEndpoints[litellm])
}
}
func TestConnScannerUpdateEndpointsReplaces(t *testing.T) {
logger, _ := zap.NewDevelopment()
cs := NewConnScanner("/proc", logger)
first := endpointKey{Port: 443}
copy(first.Addr[:], net.ParseIP("1.2.3.4").To4())
cs.UpdateEndpoints(map[endpointKey]uint8{first: 1})
// A subsequent update replaces (does not merge) the set.
cs.UpdateEndpoints(map[endpointKey]uint8{})
if len(cs.knownEndpoints) != 0 {
t.Fatalf("expected endpoints to be replaced/cleared, got %d", len(cs.knownEndpoints))
}
}
func TestScanTCPFileWithInodesNonexistent(t *testing.T) {
logger, _ := zap.NewDevelopment()
cs := NewConnScanner("/proc", logger)
matches := cs.scanTCPFileWithInodes("/nonexistent/tcp", false, cs.knownEndpoints)
if matches != nil {
t.Fatalf("expected nil for nonexistent file, got %d matches", len(matches))
}
}
func TestScanTCPFileWithInodesValidFile(t *testing.T) {
logger, _ := zap.NewDevelopment()
cs := NewConnScanner("/proc", logger)
// Add a known endpoint (192.0.2.1:443) so we can match the rem_address below
key := endpointKey{Port: 443}
key.Addr[0], key.Addr[1], key.Addr[2], key.Addr[3] = 192, 0, 2, 1
cs.knownEndpoints[key] = 1
// Create a temp file simulating /proc/net/tcp
dir := t.TempDir()
tcpFile := filepath.Join(dir, "tcp")
content := ` sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
0: 0100007F:C350 010200C0:01BB 01 00000000:00000000 00:00000000 00000000 0 0 12345 1 0000000000000000 100 0 0 10 0
`
if err := os.WriteFile(tcpFile, []byte(content), 0644); err != nil {
t.Fatal(err)
}
matches := cs.scanTCPFileWithInodes(tcpFile, false, cs.knownEndpoints)
if len(matches) != 1 {
t.Fatalf("expected 1 match, got %d", len(matches))
}
if matches[0].inode != 12345 {
t.Fatalf("expected inode 12345, got %d", matches[0].inode)
}
}