mugit/internal/git/paths_test.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com test: add missing tests (#5)..., 2 months ago
olexsmir@gmail.com test: add missing tests (#5)..., 2 months ago
| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "olexsmir.xyz/x/is" |
| 7 | ) |
| 8 | |
| 9 | func TestResolveName(t *testing.T) { |
| 10 | tests := []struct { |
| 11 | name string |
| 12 | input string |
| 13 | want string |
| 14 | }{ |
| 15 | {name: "empty string", input: "", want: ".git"}, |
| 16 | {name: "already suffixed", input: "myrepo.git", want: "myrepo.git"}, |
| 17 | {name: "no suffix", input: "myrepo", want: "myrepo.git"}, |
| 18 | {name: ".git.git", input: "repo.git.git", want: "repo.git.git"}, |
| 19 | { |
| 20 | name: "special characters", |
| 21 | input: "my-awesome_project", |
| 22 | want: "my-awesome_project.git", |
| 23 | }, |
| 24 | } |
| 25 | |
| 26 | for _, tt := range tests { |
| 27 | t.Run(tt.name, func(t *testing.T) { |
| 28 | is.Equal(t, ResolveName(tt.input), tt.want) |
| 29 | }) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestResolvePath(t *testing.T) { |
| 34 | base := "/repos" |
| 35 | tests := []struct { |
| 36 | name string |
| 37 | base string |
| 38 | path string |
| 39 | want string |
| 40 | }{ |
| 41 | {"simple", base, "myrepo.git", "/repos/myrepo.git"}, |
| 42 | {"empty", base, "", "/repos"}, // FIXME: block this |
| 43 | {"nested", base, "user/project.git", "/repos/user/project.git"}, // FIXME: support only one level deep paths |
| 44 | {"block path traversal", base, "../etc/passwd", "/repos/etc/passwd"}, |
| 45 | {"block absolute path", base, "/etc/passwd", "/repos/etc/passwd"}, |
| 46 | {"multiple traversal attempts", base, "../../../../../../etc/passwd", "/repos/etc/passwd"}, |
| 47 | {"base with trailing slash", "/repos/", "myrepo.git", "/repos/myrepo.git"}, |
| 48 | } |
| 49 | |
| 50 | for _, tt := range tests { |
| 51 | t.Run(tt.name, func(t *testing.T) { |
| 52 | path, err := ResolvePath(tt.base, tt.path) |
| 53 | is.Equal(t, tt.want, path) |
| 54 | is.Err(t, err, nil) |
| 55 | }) |
| 56 | } |
| 57 | } |