| 1 | package layout |
| 2 | |
| 3 | import ( |
| 4 | "io/fs" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // rawDirLiteral matches the raw directory as a path literal: a quote or a |
| 14 | // slash, then ".instances" as a whole word. The bare substring would also |
| 15 | // match ordinary field names such as s.cli.instances. |
| 16 | var rawDirLiteral = regexp.MustCompile(`["/]\.instances\b`) |
| 17 | |
| 18 | // The raw directories are named in exactly one package, plus bootstrap's |
| 19 | // tmpfiles and fixture lines that create them: a second caller would be a |
| 20 | // second way to bypass the bind mount's nosymfollow (design §3.2). |
| 21 | func TestRawInstancesDirectoryIsNamedOnlyHere(t *testing.T) { |
| 22 | root := filepath.Join("..", "..") |
| 23 | var files []string |
| 24 | err := filepath.WalkDir(filepath.Join(root, "internal"), func(path string, d fs.DirEntry, err error) error { |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | if !d.IsDir() && strings.HasSuffix(path, ".go") { |
| 29 | files = append(files, path) |
| 30 | } |
| 31 | return nil |
| 32 | }) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | top, err := filepath.Glob(filepath.Join(root, "*.go")) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | files = append(files, top...) |
| 41 | if len(top) == 0 { |
| 42 | t.Fatal("no .go files at the repository root; the walk is looking in the wrong place") |
| 43 | } |
| 44 | for _, path := range files { |
| 45 | if strings.HasSuffix(path, "_test.go") { |
| 46 | continue |
| 47 | } |
| 48 | slash := filepath.ToSlash(path) |
| 49 | if strings.Contains(slash, "internal/layout/") || strings.HasSuffix(slash, "internal/host/bootstrap.go") { |
| 50 | continue |
| 51 | } |
| 52 | b, err := os.ReadFile(path) |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if loc := rawDirLiteral.FindIndex(b); loc != nil { |
| 57 | line := 1 + strings.Count(string(b[:loc[0]]), "\n") |
| 58 | t.Errorf("%s:%d names the raw .instances directory; only internal/layout (and bootstrap's tmpfiles lines) may", slash, line) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // The pattern must catch the literal forms and nothing else. |
| 64 | func TestRawDirLiteralPattern(t *testing.T) { |
| 65 | for _, s := range []string{`filepath.Join(d, ".instances")`, `"/data/.instances/x"`, `"/run/carlos/.instances"`} { |
| 66 | if !rawDirLiteral.MatchString(s) { |
| 67 | t.Errorf("missed %s", s) |
| 68 | } |
| 69 | } |
| 70 | for _, s := range []string{`s.cli.instances.allow`, `"/data/instances"`, `".instancesX"`} { |
| 71 | if rawDirLiteral.MatchString(s) { |
| 72 | t.Errorf("matched %s", s) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // With a real bind mount, a rename THROUGH the mount answers EXDEV and the |
| 78 | // pass's rename through the raw directory succeeds. Needs CAP_SYS_ADMIN; |
| 79 | // skipped where it cannot mount. |
| 80 | func TestRenameThroughTheRawDirectoryAvoidsEXDEV(t *testing.T) { |
| 81 | if os.Geteuid() != 0 { |
| 82 | t.Skip("needs root to bind mount") |
| 83 | } |
| 84 | base := t.TempDir() |
| 85 | raw, view := filepath.Join(base, ".instances"), filepath.Join(base, "instances") |
| 86 | if err := os.MkdirAll(raw, 0o700); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | if err := os.MkdirAll(view, 0o700); err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | if out, err := exec.Command("mount", "--bind", "-o", "nosymfollow", raw, view).CombinedOutput(); err != nil { |
| 93 | t.Skipf("cannot bind mount here: %v %s", err, out) |
| 94 | } |
| 95 | defer exec.Command("umount", view).Run() |
| 96 | src := filepath.Join(base, "h.db") |
| 97 | if err := os.WriteFile(src, []byte("x"), 0o600); err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | if err := os.Rename(src, filepath.Join(view, "h.db")); err == nil { |
| 101 | t.Fatal("rename through the bind mount succeeded; the EXDEV premise of §3.2 does not hold here") |
| 102 | } |
| 103 | if err := os.Rename(src, filepath.Join(raw, "h.db")); err != nil { |
| 104 | t.Fatalf("rename through the raw directory: %v", err) |
| 105 | } |
| 106 | if _, err := os.Stat(filepath.Join(view, "h.db")); err != nil { |
| 107 | t.Fatalf("the view does not show the renamed file: %v", err) |
| 108 | } |
| 109 | } |
| 110 | |