| 1 | package mounts |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/sylvinus/agent-vm/internal/paths" |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | func TestUnmountable(t *testing.T) { |
| 15 | for dir, want := range map[string]string{ |
| 16 | "/home/me/proj": "", |
| 17 | "/home/me/my proj": "whitespace", |
| 18 | "/home/me/a\tb": "whitespace", |
| 19 | `/home/me/a"b`: "a quote", |
| 20 | `/home/me/a\b`: "a backslash", |
| 21 | "/home/me/a\x01b": "a control character", |
| 22 | "/home/me/été/proj": "", |
| 23 | "/home/me/R&D": "", |
| 24 | "/{{.Home}}": "a template", |
| 25 | "/home/me/a\xffb": "not valid UTF-8", |
| 26 | "/home/me/a\u2028b": "whitespace", |
| 27 | } { |
| 28 | why, bad := Unmountable(dir) |
| 29 | if bad != (want != "") || !strings.Contains(why, want) { |
| 30 | t.Errorf("%q: %q %v", dir, why, bad) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | func TestStageSymlink(t *testing.T) { |
| 39 | dir := t.TempDir() |
| 40 | real := filepath.Join(dir, "dotfiles", "gitconfig") |
| 41 | os.MkdirAll(filepath.Dir(real), 0o755) |
| 42 | os.WriteFile(real, []byte("v1"), 0o644) |
| 43 | link := filepath.Join(dir, ".gitconfig") |
| 44 | os.Symlink(real, link) |
| 45 | dst := filepath.Join(dir, "stage", "f") |
| 46 | if !Stage(link, dst, io.Discard) { |
| 47 | t.Fatal("not staged") |
| 48 | } |
| 49 | if fi, err := os.Lstat(dst); err != nil || fi.Mode()&os.ModeSymlink != 0 { |
| 50 | t.Fatalf("staged as a link: %v %v", fi, err) |
| 51 | } |
| 52 | os.WriteFile(real, []byte("v2"), 0o644) |
| 53 | if b, _ := os.ReadFile(dst); string(b) != "v2" { |
| 54 | t.Errorf("not in step with the file: %q", b) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | func TestUnsafeLocationNoCase(t *testing.T) { |
| 61 | s := newSandbox(t) |
| 62 | refs := Refs(s.home, filepath.Join(s.root, "self"), s.state, filepath.Join(s.state, "lima")) |
| 63 | upper := strings.ToUpper(s.home) |
| 64 | |
| 65 | |
| 66 | if !paths.NoCase() { |
| 67 | if _, bad := UnsafeLocation(upper, refs); bad { |
| 68 | t.Error("refused where case matters") |
| 69 | } |
| 70 | } |
| 71 | old := paths.NoCase |
| 72 | paths.NoCase = func() bool { return true } |
| 73 | t.Cleanup(func() { paths.NoCase = old }) |
| 74 | refs = Refs(s.home, filepath.Join(s.root, "self"), s.state, filepath.Join(s.state, "lima")) |
| 75 | if why, bad := UnsafeLocation(upper, refs); !bad || !strings.Contains(why, "your home directory") { |
| 76 | t.Errorf("home in capitals: %q %v", why, bad) |
| 77 | } |
| 78 | if why, bad := UnsafeLocation(strings.ToUpper(s.state)+"/x", refs); !bad { |
| 79 | t.Errorf("inside the state dir in capitals: %q %v", why, bad) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | func TestCredentialLocation(t *testing.T) { |
| 88 | s := newSandbox(t) |
| 89 | for _, d := range []string{".ssh/keys", ".config/gcloud", "Library/Keychains", ".local/share/keyrings", ".local/src", ".sshx", "work/.ssh-notes", ".mozilla"} { |
| 90 | os.MkdirAll(filepath.Join(s.home, d), 0o755) |
| 91 | } |
| 92 | os.MkdirAll(filepath.Join(s.root, "appdata", "app"), 0o755) |
| 93 | t.Setenv("APPDATA", filepath.Join(s.root, "appdata")) |
| 94 | t.Setenv("LOCALAPPDATA", "") |
| 95 | t.Setenv("XDG_CONFIG_HOME", "") |
| 96 | os.Symlink(s.home+"/.ssh", s.proj+"/lnk") |
| 97 | refs := s.refs() |
| 98 | for _, c := range []struct { |
| 99 | dir string |
| 100 | bad bool |
| 101 | }{ |
| 102 | {s.home + "/.ssh", true}, |
| 103 | {s.home + "/.ssh/keys", true}, |
| 104 | {s.home + "/.config", true}, |
| 105 | {s.home + "/.config/gcloud", true}, |
| 106 | {s.home + "/Library/Keychains", true}, |
| 107 | {s.home + "/.mozilla", true}, |
| 108 | {s.home + "/.local", true}, |
| 109 | {s.home + "/.local/share", true}, |
| 110 | {s.home + "/.local/share/keyrings", true}, |
| 111 | {s.proj + "/lnk", true}, |
| 112 | {s.root + "/appdata/app", true}, |
| 113 | {s.proj, false}, |
| 114 | {s.proj + "/sub", false}, |
| 115 | {s.home + "/.local/src", false}, |
| 116 | {s.home + "/.sshx", false}, |
| 117 | {s.home + "/work/.ssh-notes", false}, |
| 118 | {s.root + "/data/a", false}, |
| 119 | } { |
| 120 | why, bad := CredentialLocation(c.dir, refs) |
| 121 | if bad != c.bad { |
| 122 | t.Errorf("%s: %q %v", c.dir, why, bad) |
| 123 | } |
| 124 | if _, bad := UnsafeProject(c.dir, refs); c.bad && !bad { |
| 125 | t.Errorf("UnsafeProject lets %s through", c.dir) |
| 126 | } |
| 127 | } |
| 128 | if why, _ := CredentialLocation(s.home+"/.ssh", refs); !strings.Contains(why, "~/.ssh") { |
| 129 | t.Errorf("why: %q", why) |
| 130 | } |
| 131 | |
| 132 | os.RemoveAll(s.home + "/.aws") |
| 133 | os.MkdirAll(s.root+"/dotfiles/aws", 0o755) |
| 134 | os.Symlink(s.root+"/dotfiles/aws", s.home+"/.aws") |
| 135 | if _, bad := CredentialLocation(s.root+"/dotfiles/aws", refs); !bad { |
| 136 | t.Error("the target of ~/.aws was let through") |
| 137 | } |
| 138 | |
| 139 | old := paths.NoCase |
| 140 | paths.NoCase = func() bool { return true } |
| 141 | t.Cleanup(func() { paths.NoCase = old }) |
| 142 | refs = s.refs() |
| 143 | if _, bad := CredentialLocation(strings.ToUpper(s.home)+"/.SSH", refs); !bad { |
| 144 | t.Error("~/.SSH let through where case is ignored") |
| 145 | } |
| 146 | } |