package provider import ( "archive/zip" "os" "path/filepath" "runtime" "testing" ) func TestInferKind(t *testing.T) { cases := map[string]string{ "https://x.test/setup.msi": "msi", "https://x.test/setup.EXE": "exe", "https://x.test/app.dmg": "dmg", "https://x.test/app.pkg": "pkg", "https://x.test/app_amd64.deb": "deb", "https://x.test/app.x86_64.rpm": "rpm", "https://x.test/tool.zip": "zip", "https://x.test/tool.tar.gz": "tar.gz", "https://x.test/tool.tgz": "tar.gz", "https://x.test/Tool-1.2.AppImage": "appimage", "https://x.test/download?product=firefox&os=win64": "", // The extension has to be in the path, not the query string: a // download endpoint that merely mentions .msi in a parameter tells us // nothing reliable, and such entries must set `kind` explicitly. "https://download.mozilla.org/?product=firefox-msi-latest-ssl": "", "https://x.test/setup.msi?token=abc#frag": "msi", } for url, want := range cases { if got := InferKind(url); got != want { t.Errorf("InferKind(%q) = %q, want %q", url, got, want) } } } func TestMSIProductCode(t *testing.T) { guid, ok := msiProductCode(`MsiExec.exe /I{90160000-008C-0000-1000-0000000FF1CE}`) if !ok || guid != "{90160000-008C-0000-1000-0000000FF1CE}" { t.Errorf("got %q, %v", guid, ok) } if _, ok := msiProductCode(`"C:\Program Files\App\uninstall.exe" /S`); ok { t.Error("a non-MSI uninstall string should not yield a product code") } if _, ok := msiProductCode("MsiExec.exe /I no-braces"); ok { t.Error("a malformed MSI string should not yield a product code") } } func TestSplitCommandLine(t *testing.T) { name, args := splitCommandLine(`"C:\Program Files\App\uninst.exe" /S /norestart`) if name != `C:\Program Files\App\uninst.exe` { t.Errorf("name = %q", name) } if len(args) != 2 || args[0] != "/S" || args[1] != "/norestart" { t.Errorf("args = %v", args) } if name, args := splitCommandLine("uninst.exe"); name != "uninst.exe" || len(args) != 0 { t.Errorf("bare command parsed as %q %v", name, args) } if name, _ := splitCommandLine(" "); name != "" { t.Errorf("blank command line parsed as %q", name) } } func TestWingetListVersion(t *testing.T) { out := "Name Id Version Available Source\n" + "-----------------------------------------------------------------\n" + "Google Chrome Google.Chrome 129.0.6668.90 winget\n" if got := wingetListVersion(out, "Google.Chrome"); got != "129.0.6668.90" { t.Errorf("version = %q", got) } if got := wingetListVersion(out, "Mozilla.Firefox"); got != "" { t.Errorf("expected no version for an absent id, got %q", got) } } func TestExtractZipRejectsPathTraversal(t *testing.T) { dir := t.TempDir() archive := filepath.Join(dir, "evil.zip") f, err := os.Create(archive) if err != nil { t.Fatal(err) } zw := zip.NewWriter(f) w, err := zw.Create("../escaped.txt") if err != nil { t.Fatal(err) } if _, err := w.Write([]byte("nope")); err != nil { t.Fatal(err) } if err := zw.Close(); err != nil { t.Fatal(err) } f.Close() dest := filepath.Join(dir, "out") if err := extractZip(archive, dest); err == nil { t.Fatal("expected extractZip to refuse an entry outside the destination") } if _, err := os.Stat(filepath.Join(dir, "escaped.txt")); err == nil { t.Fatal("a file escaped the destination directory") } } func TestExtractZipWritesFiles(t *testing.T) { dir := t.TempDir() archive := filepath.Join(dir, "good.zip") f, err := os.Create(archive) if err != nil { t.Fatal(err) } zw := zip.NewWriter(f) w, err := zw.Create("bin/tool.txt") if err != nil { t.Fatal(err) } if _, err := w.Write([]byte("hello")); err != nil { t.Fatal(err) } if err := zw.Close(); err != nil { t.Fatal(err) } f.Close() dest := filepath.Join(dir, "out") if err := extractZip(archive, dest); err != nil { t.Fatal(err) } got, err := os.ReadFile(filepath.Join(dest, "bin", "tool.txt")) if err != nil { t.Fatal(err) } if string(got) != "hello" { t.Errorf("content = %q", got) } } // TestRegistryCoversEveryCatalogProvider makes sure a provider named in the // catalog can actually be looked up, and that the OS lists are sane. func TestRegistryIsConsistent(t *testing.T) { for _, name := range Names() { p, ok := Get(name) if !ok { t.Fatalf("Names() returned %q but Get() does not know it", name) } if p.Name() != name { t.Errorf("provider registered as %q reports Name() = %q", name, p.Name()) } if len(p.OSes()) == 0 { t.Errorf("provider %q supports no operating systems", name) } if p.Label() == "" { t.Errorf("provider %q has no label", name) } } } func TestForOSFiltersByPlatform(t *testing.T) { for _, goos := range []string{"windows", "darwin", "linux"} { providers := ForOS(goos) if len(providers) == 0 { t.Errorf("no providers for %s", goos) } for _, p := range providers { found := false for _, os := range p.OSes() { if os == goos { found = true } } if !found { t.Errorf("ForOS(%q) returned %q", goos, p.Name()) } } } if runtime.GOOS == "windows" { for _, p := range ForOS("windows") { if p.Name() == "apt" { t.Error("apt should not be offered on windows") } } } } func TestIsRebootExit(t *testing.T) { for _, code := range []int{1641, 3010} { if !isRebootExit(code) { t.Errorf("%d should be a reboot exit", code) } } for _, code := range []int{0, 1, 1618} { if isRebootExit(code) { t.Errorf("%d should not be a reboot exit", code) } } }