//go:build linux package console import ( "errors" "os" "syscall" "testing" "github.com/carlosframework/platform/internal/hostops" ) func init() { // The shared test TMPDIR is tmpfs, which production must refuse because // its receipts do not survive boot. Model only its filesystem UUID; keep // the real file descriptor, inode and DB UUID for all SQL/fence tests. sqlFilesystemUUID = func(fd uintptr) ([16]byte, error) { var fs syscall.Statfs_t if err := syscall.Fstatfs(int(fd), &fs); err != nil { return [16]byte{}, err } if fs.Type == 0x01021994 { return [16]byte{0x54, 0x45, 0x53, 0x54}, nil } return readSQLFilesystemUUID(fd) } } func TestPlan9SQLIdentitySurvivesDeviceRenumber(t *testing.T) { uuid := [16]byte{1, 2, 3, 4} st := syscall.Stat_t{Dev: 17, Ino: 99} before, err := sqlPhysicalIdentityFrom([]byte("machine"), uuid, st) if err != nil { t.Fatal(err) } st.Dev = 4097 after, err := sqlPhysicalIdentityFrom([]byte("machine"), uuid, st) if err != nil || before != after { t.Fatalf("device renumber changed identity: %q %q %v", before, after, err) } st.Ino++ copied, err := sqlPhysicalIdentityFrom([]byte("machine"), uuid, st) if err != nil || copied == before { t.Fatal("copy on the same filesystem claimed original inode") } st.Ino-- uuid[0]++ copied, err = sqlPhysicalIdentityFrom([]byte("machine"), uuid, st) if err != nil || copied == before { t.Fatal("different filesystem claimed original inode") } if _, err = sqlPhysicalIdentityFrom([]byte("machine"), [16]byte{}, st); err == nil { t.Fatal("missing authoritative filesystem UUID accepted") } } func TestPlan9SQLIdentityUnsupportedFilesystemRefuses(t *testing.T) { f, err := os.Open("/proc/self/status") if err != nil { t.Fatal(err) } defer f.Close() if _, err = readSQLFilesystemUUID(f.Fd()); !errors.Is(err, hostops.ErrContract) { t.Fatalf("proc filesystem accepted: %v", err) } } func TestPlan9Fix1SQLFilesystemProbeAcceptsBtrfs(t *testing.T) { for _, tc := range []struct { name string typ int64 want bool }{{"ext4", 0xef53, true}, {"xfs", 0x58465342, true}, {"btrfs", 0x9123683e, true}, {"tmpfs", 0x01021994, false}, {"proc", 0x9fa0, false}} { if got := sqlFilesystemSupported(tc.typ); got != tc.want { t.Errorf("%s: supported=%v, want %v", tc.name, got, tc.want) } } }