| 1 | package reversesshfs |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "unsafe" |
| 10 | |
| 11 | "github.com/pkg/sftp" |
| 12 | "golang.org/x/sys/windows" |
| 13 | ) |
| 14 | |
| 15 | func ctime(t *testing.T, p string) int64 { |
| 16 | t.Helper() |
| 17 | f, err := os.Open(p) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | defer f.Close() |
| 22 | var info fileBasicInformation |
| 23 | if err := windows.GetFileInformationByHandleEx(windows.Handle(f.Fd()), windows.FileBasicInfo, |
| 24 | (*byte)(unsafe.Pointer(&info)), uint32(unsafe.Sizeof(info))); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | return info.ChangeTime |
| 28 | } |
| 29 | |
| 30 | func TestSlashPath(t *testing.T) { |
| 31 | for in, want := range map[string]string{ |
| 32 | `C:\Users\foo`: "/C:/Users/foo", |
| 33 | `C:/Users/foo`: "/C:/Users/foo", |
| 34 | `/C:/Users/foo/`: "/C:/Users/foo", |
| 35 | `C:\`: "/C:", |
| 36 | `\\server\share\foo`: "/server/share/foo", |
| 37 | } { |
| 38 | if got := slashPath(in); got != want { |
| 39 | t.Errorf("slashPath(%q) = %q, want %q", in, got, want) |
| 40 | } |
| 41 | } |
| 42 | for in, want := range map[string]string{ |
| 43 | "C:/Users/foo/bar": "/C:/Users/foo/bar", |
| 44 | "/C:/Users/foo/bar": "/C:/Users/foo/bar", |
| 45 | "bar": "/C:/Users/foo/bar", |
| 46 | ".": "/C:/Users/foo", |
| 47 | } { |
| 48 | if got := realPath("/C:/Users/foo", in); got != want { |
| 49 | t.Errorf("realPath(%q) = %q, want %q", in, got, want) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func shortBase(t *testing.T, p string) string { |
| 55 | t.Helper() |
| 56 | p16, err := windows.UTF16PtrFromString(p) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | buf := make([]uint16, windows.MAX_PATH) |
| 61 | n, err := windows.GetShortPathName(p16, &buf[0], uint32(len(buf))) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | return filepath.Base(windows.UTF16ToString(buf[:n])) |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | func TestRootedDeniesShortNames(t *testing.T) { |
| 71 | type env struct { |
| 72 | c *sftp.Client |
| 73 | root, git string |
| 74 | gitfile string |
| 75 | } |
| 76 | p := func(e env, s string) string { return filepath.Join(e.root, s) } |
| 77 | open := func(e env, s string, flags int) error { |
| 78 | f, err := e.c.OpenFile(p(e, s), flags) |
| 79 | if err == nil { |
| 80 | f.Close() |
| 81 | } |
| 82 | return err |
| 83 | } |
| 84 | attempts := []struct { |
| 85 | name string |
| 86 | f func(e env) error |
| 87 | }{ |
| 88 | {"create hook", func(e env) error { return open(e, e.git+"/hooks/pre-commit", os.O_RDWR|os.O_CREATE|os.O_TRUNC) }}, |
| 89 | {"open config for write", func(e env) error { return open(e, e.git+"/config", os.O_WRONLY) }}, |
| 90 | {"truncate on open", func(e env) error { return open(e, e.git+"/config", os.O_WRONLY|os.O_TRUNC) }}, |
| 91 | {"remove config", func(e env) error { return e.c.Remove(p(e, e.git+"/config")) }}, |
| 92 | {"rmdir hooks", func(e env) error { return e.c.RemoveDirectory(p(e, e.git+"/hooks")) }}, |
| 93 | {"rename .git away", func(e env) error { return e.c.PosixRename(p(e, e.git), p(e, "old")) }}, |
| 94 | {"rename into .git", func(e env) error { return e.c.PosixRename(p(e, "src/main.go"), p(e, e.git+"/hooks/x")) }}, |
| 95 | {"hardlink into hooks", func(e env) error { return e.c.Link(p(e, "src/main.go"), p(e, e.git+"/hooks/x")) }}, |
| 96 | {"chmod config", func(e env) error { return e.c.Chmod(p(e, e.git+"/config"), 0o444) }}, |
| 97 | {"truncate config", func(e env) error { return e.c.Truncate(p(e, e.git+"/config"), 0) }}, |
| 98 | {"write gitfile", func(e env) error { return open(e, "src/"+e.gitfile, os.O_WRONLY|os.O_TRUNC) }}, |
| 99 | {"replace gitfile", func(e env) error { return e.c.PosixRename(p(e, "src/main.go"), p(e, "src/"+e.gitfile)) }}, |
| 100 | {"remove gitfile", func(e env) error { return e.c.Remove(p(e, "src/"+e.gitfile)) }}, |
| 101 | } |
| 102 | for _, a := range attempts { |
| 103 | t.Run(a.name, func(t *testing.T) { |
| 104 | c, root := setupRooted(t, false) |
| 105 | gitfileLong := filepath.Join(root, "src", ".git") |
| 106 | if err := os.WriteFile(gitfileLong, []byte("gitdir: x"), 0o644); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | e := env{c: c, root: root, git: shortBase(t, filepath.Join(root, ".git")), gitfile: shortBase(t, gitfileLong)} |
| 110 | if strings.EqualFold(e.git, ".git") || strings.EqualFold(e.gitfile, ".git") { |
| 111 | t.Skip("8.3 names are not created on this volume") |
| 112 | } |
| 113 | if err := a.f(e); err == nil { |
| 114 | t.Error("expected an error") |
| 115 | } |
| 116 | assertUnchanged(t, root) |
| 117 | if b, err := os.ReadFile(gitfileLong); err != nil || string(b) != "gitdir: x" { |
| 118 | t.Fatalf("src/.git changed: %q, %v", b, err) |
| 119 | } |
| 120 | }) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestRootedDeniesWindowsNames(t *testing.T) { |
| 125 | junction := func(t *testing.T, root string) { |
| 126 | t.Helper() |
| 127 | |
| 128 | out, err := exec.Command("cmd", "/c", "mklink", "/J", filepath.Join(root, "jn"), filepath.Join(root, ".git")).CombinedOutput() |
| 129 | if err != nil { |
| 130 | t.Fatalf("mklink: %v: %s", err, out) |
| 131 | } |
| 132 | } |
| 133 | attempts := []struct { |
| 134 | name string |
| 135 | setup func(t *testing.T, root string) |
| 136 | path string |
| 137 | }{ |
| 138 | {"stream of .git", nil, ".git::$INDEX_ALLOCATION/hooks/pre-commit"}, |
| 139 | {"stream of a file", nil, "src/main.go:x"}, |
| 140 | {"backslashes", nil, `src\..\.git\hooks\pre-commit`}, |
| 141 | {"via junction", junction, "jn/hooks/pre-commit"}, |
| 142 | {"via junction to a file", junction, "jn/config"}, |
| 143 | } |
| 144 | for _, a := range attempts { |
| 145 | t.Run(a.name, func(t *testing.T) { |
| 146 | c, root := setupRooted(t, false) |
| 147 | if a.setup != nil { |
| 148 | a.setup(t, root) |
| 149 | } |
| 150 | if f, err := c.OpenFile(root+`\`+a.path, os.O_RDWR|os.O_CREATE|os.O_TRUNC); err == nil { |
| 151 | f.Close() |
| 152 | t.Error("expected an error") |
| 153 | } |
| 154 | assertUnchanged(t, root) |
| 155 | if fis, err := os.ReadDir(filepath.Join(root, "src")); err != nil || len(fis) != 1 { |
| 156 | t.Fatalf("src entries changed: %v, %v", fis, err) |
| 157 | } |
| 158 | }) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | func TestWritableNameWin32(t *testing.T) { |
| 164 | for _, rel := range []string{"src/a.", "src/a ", "a./b", "NUL", "src/nul.txt", "aux", "Con .txt", "COM1", "lpt9.log", "COM¹", "CONIN$"} { |
| 165 | if writableName(rel) { |
| 166 | t.Errorf("%q: writable", rel) |
| 167 | } |
| 168 | } |
| 169 | for _, rel := range []string{".", "src/main.go", "src/.a", "console", "COM10", "COMx", "nulx", "src/a.b"} { |
| 170 | if !writableName(rel) { |
| 171 | t.Errorf("%q: not writable", rel) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func TestRootedSymlinkUnsupported(t *testing.T) { |
| 177 | c, root := setupRooted(t, false) |
| 178 | if err := c.Symlink("main.go", filepath.Join(root, "src", "link")); err == nil { |
| 179 | t.Fatal("expected an error") |
| 180 | } |
| 181 | if _, err := os.Lstat(filepath.Join(root, "src", "link")); err == nil { |
| 182 | t.Fatal("the symlink was created") |
| 183 | } |
| 184 | } |