package webread import ( "encoding/json" "os" "path/filepath" "strings" "syscall" "testing" ) func writeBundle(t *testing.T) string { t.Helper() dir := t.TempDir() w := &bundleWriter{dir: dir} if _, err := w.add("s1", KindText, "text/html", []byte("Title: Shop\n")); err != nil { t.Fatal(err) } if _, err := w.addImage("s1.1", pngOf(t, 4, 3), 4, 3); err != nil { t.Fatal(err) } log := []Entry{{Source: "s1", Origin: OriginPaul, URL: "https://shop.example.com/", File: "s001.txt"}, {Source: "s1.1", Origin: OriginExpanded, Parent: "s1", URL: "https://shop.example.com/logo.png", File: "s002.png"}} if _, err := w.finish(log, Spent{}); err != nil { t.Fatal(err) } return dir } func TestReadBundleRoundTrip(t *testing.T) { b, err := ReadBundle(writeBundle(t)) if err != nil { t.Fatal(err) } if len(b.Files) != 2 || string(b.Files[0].Data) != "Title: Shop\n" || b.Files[1].Width != 4 || b.Files[1].Kind != KindImage || len(b.Log) != 2 { t.Fatalf("bundle %+v", b) } } func TestReadBundleRefusals(t *testing.T) { for name, tamper := range map[string]func(t *testing.T, dir string){ "symlinked file": func(t *testing.T, dir string) { data, _ := os.ReadFile(filepath.Join(dir, "s001.txt")) other := filepath.Join(t.TempDir(), "elsewhere") os.WriteFile(other, data, 0600) os.Remove(filepath.Join(dir, "s001.txt")) if err := os.Symlink(other, filepath.Join(dir, "s001.txt")); err != nil { t.Fatal(err) } }, "symlinked manifest": func(t *testing.T, dir string) { data, _ := os.ReadFile(filepath.Join(dir, ManifestName)) other := filepath.Join(t.TempDir(), "m.json") os.WriteFile(other, data, 0600) os.Remove(filepath.Join(dir, ManifestName)) os.Symlink(other, filepath.Join(dir, ManifestName)) }, "unknown name": func(t *testing.T, dir string) { os.WriteFile(filepath.Join(dir, "notes.txt"), []byte("x"), 0600) }, "unlisted generated name": func(t *testing.T, dir string) { os.WriteFile(filepath.Join(dir, "s009.txt"), []byte("x"), 0600) }, "hash mismatch": func(t *testing.T, dir string) { os.WriteFile(filepath.Join(dir, "s001.txt"), []byte("Title: Shoq\n"), 0600) }, "size mismatch": func(t *testing.T, dir string) { os.WriteFile(filepath.Join(dir, "s001.txt"), []byte("Title: Shop!\n"), 0600) }, "missing file": func(t *testing.T, dir string) { os.Remove(filepath.Join(dir, "s002.png")) }, "directory for a file": func(t *testing.T, dir string) { os.Remove(filepath.Join(dir, "s002.png")) os.Mkdir(filepath.Join(dir, "s002.png"), 0700) }, "fifo for a file": func(t *testing.T, dir string) { os.Remove(filepath.Join(dir, "s001.txt")) if err := syscall.Mkfifo(filepath.Join(dir, "s001.txt"), 0600); err != nil { t.Skip("mkfifo:", err) } }, "manifest names a path": func(t *testing.T, dir string) { editManifest(t, dir, func(m *Manifest) { m.Files[0].Name = "../s001.txt" }) }, "manifest kind disagrees with name": func(t *testing.T, dir string) { editManifest(t, dir, func(m *Manifest) { m.Files[0].Kind = KindImage }) }, "manifest duplicate": func(t *testing.T, dir string) { editManifest(t, dir, func(m *Manifest) { m.Files = append(m.Files, m.Files[0]) }) }, "manifest oversize text": func(t *testing.T, dir string) { editManifest(t, dir, func(m *Manifest) { m.Files[0].Size = MaxPageText + 1 }) }, "manifest unknown field": func(t *testing.T, dir string) { data, _ := os.ReadFile(filepath.Join(dir, ManifestName)) os.WriteFile(filepath.Join(dir, ManifestName), []byte(strings.Replace(string(data), `{"version"`, `{"extra":1,"version"`, 1)), 0600) }, "manifest too large": func(t *testing.T, dir string) { os.WriteFile(filepath.Join(dir, ManifestName), []byte(strings.Repeat(" ", MaxManifestBytes+1)), 0600) }, "image that is not a png": func(t *testing.T, dir string) { data := []byte("not a png at all") os.WriteFile(filepath.Join(dir, "s002.png"), data, 0600) editManifest(t, dir, func(m *Manifest) { m.Files[1].Size, m.Files[1].SHA256 = int64(len(data)), sum(data) }) }, } { t.Run(name, func(t *testing.T) { dir := writeBundle(t) tamper(t, dir) if b, err := ReadBundle(dir); err == nil { t.Fatalf("read a tampered bundle: %+v", b) } }) } // The directory itself, through a symlink. dir := writeBundle(t) link := filepath.Join(t.TempDir(), "link") os.Symlink(dir, link) if _, err := ReadBundle(link); err == nil { t.Fatal("read through a symlinked directory") } } func editManifest(t *testing.T, dir string, edit func(*Manifest)) { t.Helper() data, err := os.ReadFile(filepath.Join(dir, ManifestName)) if err != nil { t.Fatal(err) } var m Manifest if err = json.Unmarshal(data, &m); err != nil { t.Fatal(err) } edit(&m) data, _ = json.Marshal(m) if err = os.WriteFile(filepath.Join(dir, ManifestName), data, 0600); err != nil { t.Fatal(err) } }