| 1 | //go:build linux |
| 2 | |
| 3 | package console |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "os" |
| 8 | "syscall" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/hostops" |
| 12 | ) |
| 13 | |
| 14 | func init() { |
| 15 | // The shared test TMPDIR is tmpfs, which production must refuse because |
| 16 | // its receipts do not survive boot. Model only its filesystem UUID; keep |
| 17 | // the real file descriptor, inode and DB UUID for all SQL/fence tests. |
| 18 | sqlFilesystemUUID = func(fd uintptr) ([16]byte, error) { |
| 19 | var fs syscall.Statfs_t |
| 20 | if err := syscall.Fstatfs(int(fd), &fs); err != nil { |
| 21 | return [16]byte{}, err |
| 22 | } |
| 23 | if fs.Type == 0x01021994 { |
| 24 | return [16]byte{0x54, 0x45, 0x53, 0x54}, nil |
| 25 | } |
| 26 | return readSQLFilesystemUUID(fd) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestPlan9SQLIdentitySurvivesDeviceRenumber(t *testing.T) { |
| 31 | uuid := [16]byte{1, 2, 3, 4} |
| 32 | st := syscall.Stat_t{Dev: 17, Ino: 99} |
| 33 | before, err := sqlPhysicalIdentityFrom([]byte("machine"), uuid, st) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | st.Dev = 4097 |
| 38 | after, err := sqlPhysicalIdentityFrom([]byte("machine"), uuid, st) |
| 39 | if err != nil || before != after { |
| 40 | t.Fatalf("device renumber changed identity: %q %q %v", before, after, err) |
| 41 | } |
| 42 | st.Ino++ |
| 43 | copied, err := sqlPhysicalIdentityFrom([]byte("machine"), uuid, st) |
| 44 | if err != nil || copied == before { |
| 45 | t.Fatal("copy on the same filesystem claimed original inode") |
| 46 | } |
| 47 | st.Ino-- |
| 48 | uuid[0]++ |
| 49 | copied, err = sqlPhysicalIdentityFrom([]byte("machine"), uuid, st) |
| 50 | if err != nil || copied == before { |
| 51 | t.Fatal("different filesystem claimed original inode") |
| 52 | } |
| 53 | if _, err = sqlPhysicalIdentityFrom([]byte("machine"), [16]byte{}, st); err == nil { |
| 54 | t.Fatal("missing authoritative filesystem UUID accepted") |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestPlan9SQLIdentityUnsupportedFilesystemRefuses(t *testing.T) { |
| 59 | f, err := os.Open("/proc/self/status") |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | defer f.Close() |
| 64 | if _, err = readSQLFilesystemUUID(f.Fd()); !errors.Is(err, hostops.ErrContract) { |
| 65 | t.Fatalf("proc filesystem accepted: %v", err) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestPlan9Fix1SQLFilesystemProbeAcceptsBtrfs(t *testing.T) { |
| 70 | for _, tc := range []struct { |
| 71 | name string |
| 72 | typ int64 |
| 73 | want bool |
| 74 | }{{"ext4", 0xef53, true}, {"xfs", 0x58465342, true}, {"btrfs", 0x9123683e, true}, {"tmpfs", 0x01021994, false}, {"proc", 0x9fa0, false}} { |
| 75 | if got := sqlFilesystemSupported(tc.typ); got != tc.want { |
| 76 | t.Errorf("%s: supported=%v, want %v", tc.name, got, tc.want) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |