//go:build linux package console import ( "fmt" "os" "strings" "sync" "testing" "time" ) func TestPlan9Fix1ReportIdentityIsStableAcrossProcFailures(t *testing.T) { priorRead, priorIdentity := hostReportProcReadFile, hostReportProcessIdentity t.Cleanup(func() { hostReportProcReadFile, hostReportProcessIdentity = priorRead, priorIdentity }) hostReportProcessIdentity = sync.OnceValue(readHostReportProcessIdentity) before := hostReportProcessID("boot") hostReportProcReadFile = func(string) ([]byte, error) { return nil, os.ErrPermission } after := hostReportProcessID("boot") if before != after { t.Fatalf("one process changed its report identity: %q -> %q", before, after) } } func TestPlan9Fix1ReportCannotProveAnotherPIDNamespaceDead(t *testing.T) { bootRaw, err := os.ReadFile("/proc/sys/kernel/random/boot_id") if err != nil { t.Fatal(err) } boot := strings.TrimSpace(string(bootRaw)) next := hostReportProcessID(boot) old := fmt.Sprintf("%s:foreign-pid-namespace:999999999:1", boot) if err := hostReportCollision(plan9Report(old, time.Now()), plan9Report(next, time.Now()), time.Now()); err == nil { t.Fatal("absence in our PID namespace proved another namespace's process dead") } } func TestPlan9Fix1HiddenProcEntryDoesNotProveLivePIDDead(t *testing.T) { bootRaw, err := os.ReadFile("/proc/sys/kernel/random/boot_id") if err != nil { t.Fatal(err) } boot := strings.TrimSpace(string(bootRaw)) namespace, err := hostReportPIDNamespace() if err != nil { t.Fatal(err) } current := fmt.Sprintf("%s:%s:%d:1", boot, namespace, os.Getpid()) prior := hostReportProcReadFile t.Cleanup(func() { hostReportProcReadFile = prior }) hostReportProcReadFile = func(path string) ([]byte, error) { if path == fmt.Sprintf("/proc/%d/stat", os.Getpid()) { return nil, os.ErrNotExist } return prior(path) } if hostReportProcessGone(current, current) { t.Fatal("hidden /proc entry treated as a dead live PID") } }