| 1 | package selfupdate |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // acceptedRepos covers every form Validate accepts. |
| 11 | var acceptedRepos = []string{ |
| 12 | "https://github.com/maintainer/klaus", |
| 13 | "https://github.com/maintainer/klaus.git", |
| 14 | "ssh://git@github.com/maintainer/klaus", |
| 15 | "ssh://git@[::1]:2222/srv/klaus.git", |
| 16 | "git://example.com/klaus.git", |
| 17 | "file:///srv/git/klaus.git", |
| 18 | "/srv/git/klaus.git", |
| 19 | "git@github.com:maintainer/klaus.git", |
| 20 | "me@host.example:~/klaus.git", |
| 21 | "https://example.com/a-b_c.d~e/f:g@h", |
| 22 | } |
| 23 | |
| 24 | func TestValidateRepo(t *testing.T) { |
| 25 | for _, repo := range acceptedRepos { |
| 26 | if err := Validate(repo, "main"); err != nil { |
| 27 | t.Errorf("Validate(%q) refused: %v", repo, err) |
| 28 | } |
| 29 | } |
| 30 | for _, repo := range []string{ |
| 31 | "", |
| 32 | "ext::sh -c touch% /tmp/pwned", |
| 33 | "ext::sh", |
| 34 | "fd::17", |
| 35 | "foo::bar", |
| 36 | "me@host::path", |
| 37 | "-uhttps://example.com/klaus", |
| 38 | "--upload-pack=touch /tmp/pwned", |
| 39 | "/srv/git/klaus.git?rev=0000000", |
| 40 | "/srv/git/klaus.git#packages.x86_64-linux.evil", |
| 41 | "https://example.com/klaus?ref=evil", |
| 42 | "/srv/git/my klaus.git", |
| 43 | "/srv/git/klaus%20repo.git", |
| 44 | "https://example.com/my%20klaus", |
| 45 | "https://example.com/repo+name", |
| 46 | "https://example.com/repo!", |
| 47 | "https://example.com/repo$x", |
| 48 | "https://example.com/repo'x", |
| 49 | "https://example.com/repo(x", |
| 50 | "https://example.com/repo,x", |
| 51 | "https://example.com/repo;x", |
| 52 | "https://example.com/repo=x", |
| 53 | "/srv/git/klaus+1.git", |
| 54 | "/srv/git/[klaus].git", |
| 55 | "/srv/git/klaus.git\n", |
| 56 | "http://example.com/klaus", |
| 57 | "ftp://example.com/klaus", |
| 58 | "file://relative/klaus", |
| 59 | "ssh://-oProxyCommand=touch/klaus", |
| 60 | "relative/klaus", |
| 61 | "host:klaus", |
| 62 | } { |
| 63 | err := Validate(repo, "main") |
| 64 | if err == nil { |
| 65 | t.Errorf("Validate(%q) accepted it", repo) |
| 66 | continue |
| 67 | } |
| 68 | if !strings.Contains(err.Error(), "self_update.repo") { |
| 69 | t.Errorf("Validate(%q) = %v; want the error to name self_update.repo", repo, err) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // TestRepoURL: ls-remote and nix read a repo by the same URL, the one the |
| 75 | // flake ref carries. |
| 76 | func TestRepoURL(t *testing.T) { |
| 77 | sha := strings.Repeat("a", 40) |
| 78 | for repo, want := range map[string]string{ |
| 79 | "/srv/git/klaus.git": "file:///srv/git/klaus.git", |
| 80 | "file:///srv/git/klaus.git": "file:///srv/git/klaus.git", |
| 81 | "https://github.com/maintainer/klaus": "https://github.com/maintainer/klaus", |
| 82 | "git@github.com:maintainer/klaus.git": "ssh://git@github.com/maintainer/klaus.git", |
| 83 | "me@host.example:/srv/klaus.git": "ssh://me@host.example/srv/klaus.git", |
| 84 | "me@host.example:~/klaus.git": "ssh://me@host.example/~/klaus.git", |
| 85 | } { |
| 86 | if got := RepoURL(repo); got != want { |
| 87 | t.Errorf("RepoURL(%q) = %q, want %q", repo, got, want) |
| 88 | } |
| 89 | if got, ref := FlakeRef(repo, "main", sha), "git+"+want+"?ref=refs/heads/main&rev="+sha+"#packages."; !strings.HasPrefix(got, ref) { |
| 90 | t.Errorf("FlakeRef(%q) = %q, want prefix %q", repo, got, ref) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // TestFlakeRefSurvivesNix: the installed nix parses each accepted repo's |
| 96 | // flake ref and prints it back unchanged, so nix fetches the URL ls-remote |
| 97 | // read. |
| 98 | func TestFlakeRefSurvivesNix(t *testing.T) { |
| 99 | if _, err := exec.LookPath("nix"); err != nil { |
| 100 | if os.Getenv("KLAUS_TEST_TOOLS") == "required" { |
| 101 | t.Fatal("nix not installed") |
| 102 | } |
| 103 | t.Skip("nix not installed") |
| 104 | } |
| 105 | sha := strings.Repeat("a", 40) |
| 106 | for _, repo := range acceptedRepos { |
| 107 | ref, _, _ := strings.Cut(FlakeRef(repo, "main", sha), "#") |
| 108 | // Only stdout is the reference: nix warns on stderr, as it does |
| 109 | // with no network. |
| 110 | var stderr strings.Builder |
| 111 | cmd := exec.Command("nix", "--extra-experimental-features", "nix-command flakes", "eval", "--raw", "--expr", |
| 112 | `builtins.flakeRefToString (builtins.parseFlakeRef "`+ref+`")`) |
| 113 | cmd.Stderr = &stderr |
| 114 | out, err := cmd.Output() |
| 115 | if err != nil { |
| 116 | t.Fatalf("nix eval of %q: %v\n%s", ref, err, stderr.String()) |
| 117 | } |
| 118 | // nix prints git+git:// by its alias git://, the same URL. |
| 119 | if got := strings.Replace(ref, "git+git://", "git://", 1); string(out) != got { |
| 120 | t.Errorf("repo %q: nix turned %q into %q", repo, got, out) |
| 121 | } |
| 122 | } |
| 123 | } |