84 lines
2.8 KiB
Go
84 lines
2.8 KiB
Go
package geckorelay
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDistribution(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if _, err := VerifyDistribution(dir); err == nil {
|
|
t.Fatal("missing distribution accepted")
|
|
}
|
|
names := []string{"index.html", "README.txt", "gecko.js", "gecko.wasm.zst", "firefox.tar.gz", "firefox.tar.gz.notices.txt", "wrapper.tar.gz", "wrapper-MPL.txt", "emscripten.tar.gz", "emscripten.tar.gz.notices.txt", "rust-src.tar.xz", "rust-src.tar.xz.notices.txt", "zstddec.tgz", "zstddec.tgz.notices.txt", "NotoSansCJKjp-Regular.otf", "NotoSansCJK-LICENSE.txt"}
|
|
content := []byte("synthetic source and notice")
|
|
hash := sha256.Sum256(content)
|
|
var entries []DistributionFile
|
|
for _, name := range names {
|
|
if err := os.WriteFile(filepath.Join(dir, name), content, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entries = append(entries, DistributionFile{File: name, Size: int64(len(content)), SHA256: hex.EncodeToString(hash[:])})
|
|
}
|
|
raw, _ := json.Marshal(map[string]any{"format": "pageswiki.gecko-distribution.v1", "version": "v0.0.1", "files": entries})
|
|
if err := os.WriteFile(filepath.Join(dir, "manifest.json"), raw, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sum := sha256.Sum256(raw)
|
|
want := hex.EncodeToString(sum[:])
|
|
files, err := verifyDistribution(dir, want)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
relay, _ := New(testKey, dir)
|
|
relay.Distribution = files
|
|
for _, name := range []string{"index.html", "firefox.tar.gz", "manifest.json", "unknown", "../manifest.json"} {
|
|
req := httptest.NewRequest("GET", DistributionPrefix+name, nil)
|
|
req.Header.Set(GrantHeader, Sign(testKey, Grant{Subject: "test:user", Nonce: "download-test-0123456789", Expires: time.Now().Add(30 * time.Second).Unix()}))
|
|
rec := httptest.NewRecorder()
|
|
relay.ServeHTTP(rec, req)
|
|
expected := 200
|
|
if strings.Contains(name, "unknown") || strings.Contains(name, "../") {
|
|
expected = 404
|
|
}
|
|
if rec.Code != expected {
|
|
t.Fatalf("%s: %d", name, rec.Code)
|
|
}
|
|
if expected == 200 && rec.Header().Get("ETag") == "" {
|
|
t.Fatal("no ETag")
|
|
}
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "README.txt"), []byte("tampered"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := verifyDistribution(dir, want); err == nil {
|
|
t.Fatal("tampered notice accepted")
|
|
}
|
|
if err := os.Remove(filepath.Join(dir, "README.txt")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(filepath.Join(dir, "gecko.js"), filepath.Join(dir, "README.txt")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := verifyDistribution(dir, want); err == nil {
|
|
t.Fatal("symlink accepted")
|
|
}
|
|
}
|
|
|
|
func TestDistributionRealBundle(t *testing.T) {
|
|
dir := os.Getenv("PAGESWIKI_GECKO_TEST_BUNDLE")
|
|
if dir == "" {
|
|
t.Skip("opt-in complete distribution")
|
|
}
|
|
if _, err := VerifyDistribution(dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|