| 1 | //go:build linux |
| 2 | |
| 3 | package console |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func TestPlan9Fix1ReportIdentityIsStableAcrossProcFailures(t *testing.T) { |
| 15 | priorRead, priorIdentity := hostReportProcReadFile, hostReportProcessIdentity |
| 16 | t.Cleanup(func() { hostReportProcReadFile, hostReportProcessIdentity = priorRead, priorIdentity }) |
| 17 | hostReportProcessIdentity = sync.OnceValue(readHostReportProcessIdentity) |
| 18 | before := hostReportProcessID("boot") |
| 19 | hostReportProcReadFile = func(string) ([]byte, error) { return nil, os.ErrPermission } |
| 20 | after := hostReportProcessID("boot") |
| 21 | if before != after { |
| 22 | t.Fatalf("one process changed its report identity: %q -> %q", before, after) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func TestPlan9Fix1ReportCannotProveAnotherPIDNamespaceDead(t *testing.T) { |
| 27 | bootRaw, err := os.ReadFile("/proc/sys/kernel/random/boot_id") |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | boot := strings.TrimSpace(string(bootRaw)) |
| 32 | next := hostReportProcessID(boot) |
| 33 | old := fmt.Sprintf("%s:foreign-pid-namespace:999999999:1", boot) |
| 34 | if err := hostReportCollision(plan9Report(old, time.Now()), plan9Report(next, time.Now()), time.Now()); err == nil { |
| 35 | t.Fatal("absence in our PID namespace proved another namespace's process dead") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestPlan9Fix1HiddenProcEntryDoesNotProveLivePIDDead(t *testing.T) { |
| 40 | bootRaw, err := os.ReadFile("/proc/sys/kernel/random/boot_id") |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | boot := strings.TrimSpace(string(bootRaw)) |
| 45 | namespace, err := hostReportPIDNamespace() |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | current := fmt.Sprintf("%s:%s:%d:1", boot, namespace, os.Getpid()) |
| 50 | prior := hostReportProcReadFile |
| 51 | t.Cleanup(func() { hostReportProcReadFile = prior }) |
| 52 | hostReportProcReadFile = func(path string) ([]byte, error) { |
| 53 | if path == fmt.Sprintf("/proc/%d/stat", os.Getpid()) { |
| 54 | return nil, os.ErrNotExist |
| 55 | } |
| 56 | return prior(path) |
| 57 | } |
| 58 | if hostReportProcessGone(current, current) { |
| 59 | t.Fatal("hidden /proc entry treated as a dead live PID") |
| 60 | } |
| 61 | } |
| 62 | |