Files
2026-12-04 13:02:12 +01:00

132 lines
4.3 KiB
Go

package remotebrowser
import (
"archive/zip"
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
// browserZip builds a zip from names to content. A name that ends in a
// slash is a folder, the file chrome is marked as a program.
func browserZip(t *testing.T, files map[string]string) []byte {
t.Helper()
var buf bytes.Buffer
w := zip.NewWriter(&buf)
for name, content := range files {
header := &zip.FileHeader{Name: name, Method: zip.Deflate}
header.SetMode(0o644)
if strings.HasSuffix(name, "/chrome") {
header.SetMode(0o755)
}
if strings.HasSuffix(name, "/") {
header.SetMode(os.ModeDir | 0o755)
}
f, err := w.CreateHeader(header)
if err != nil {
t.Fatal(err)
}
if _, err := f.Write([]byte(content)); err != nil {
t.Fatal(err)
}
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
// TestDownloadBrowserChecksTheFile: the browser is only put in place when
// the download has the pinned size and checksum and unpacks inside its own
// folder. A changed file, a file of another size and a zip with a path that
// leaves the folder are refused and leave nothing behind. A good download
// replaces the browser of an earlier release and keeps the other folders.
func TestDownloadBrowserChecksTheFile(t *testing.T) {
good := browserZip(t, map[string]string{
"chrome-linux64/": "",
"chrome-linux64/chrome": "#!/bin/sh\n",
"chrome-linux64/locales/da.pak": "pak",
})
served := good
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/"+pinnedBrowserVersion+"/linux64/chrome-linux64.zip" {
http.NotFound(w, r)
return
}
w.Write(served) //nolint:errcheck
}))
defer srv.Close()
was := pinnedBrowserHost
pinnedBrowserHost = srv.URL
t.Cleanup(func() { pinnedBrowserHost = was })
buildOf := func(zipBytes []byte) browserBuild {
sum := sha256.Sum256(zipBytes)
return browserBuild{platform: "linux64", sha256: hex.EncodeToString(sum[:]), size: int64(len(zipBytes))}
}
root := t.TempDir()
left := func() []string {
entries, _ := os.ReadDir(root)
names := []string{}
for _, e := range entries {
names = append(names, e.Name())
}
return names
}
// a file with other content of the same size
changed := bytes.Clone(good)
changed[len(changed)-1] ^= 0xff
served = changed
if err := downloadBrowser(context.Background(), root, buildOf(good)); err == nil || !strings.Contains(err.Error(), "checksum") {
t.Fatalf("a changed file was accepted: %v", err)
}
// a file that is longer than pinned
served = append(bytes.Clone(good), 0)
if err := downloadBrowser(context.Background(), root, buildOf(good)); err == nil {
t.Fatal("a longer file was accepted")
}
// a zip with a path that leaves the folder, with a matching checksum
slip := browserZip(t, map[string]string{"chrome-linux64/chrome": "x", "chrome-linux64/../../escaped": "x"})
served = slip
if err := downloadBrowser(context.Background(), root, buildOf(slip)); err == nil {
t.Fatal("a path that leaves the browser folder was unpacked")
}
if _, err := os.Stat(filepath.Join(filepath.Dir(root), "escaped")); err == nil {
t.Fatal("a file was written outside the browser folder")
}
if names := left(); len(names) != 0 {
t.Fatalf("a refused download left files behind: %v", names)
}
// a good download, next to the browser of an earlier release
// with what a stopped download left behind
for _, dir := range []string{"chromium-1321438", "chrome-1.0.0.0", "cache", "config", "unpack-123", "download-123.zip"} {
if err := os.MkdirAll(filepath.Join(root, dir), 0o755); err != nil {
t.Fatal(err)
}
}
served = good
if err := downloadBrowser(context.Background(), root, buildOf(good)); err != nil {
t.Fatalf("a good download: %v", err)
}
info, err := os.Stat(pinnedBrowserBin(root))
if err != nil || info.Mode().Perm()&0o111 == 0 {
t.Fatalf("the browser binary is not in place as a program: %v %v", info, err)
}
if _, err := os.Stat(filepath.Join(pinnedBrowserDir(root), "locales", "da.pak")); err != nil {
t.Fatalf("a file in a folder was not unpacked: %v", err)
}
got := strings.Join(left(), ",")
if got != "cache,chrome-"+pinnedBrowserVersion+",config" {
t.Fatalf("folders after the download: %s", got)
}
}