oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package webread
2
3import (
4 "encoding/json"
5 "os"
6 "path/filepath"
7 "strings"
8 "syscall"
9 "testing"
10)
11
12func writeBundle(t *testing.T) string {
13 t.Helper()
14 dir := t.TempDir()
15 w := &bundleWriter{dir: dir}
16 if _, err := w.add("s1", KindText, "text/html", []byte("Title: Shop\n")); err != nil {
17 t.Fatal(err)
18 }
19 if _, err := w.addImage("s1.1", pngOf(t, 4, 3), 4, 3); err != nil {
20 t.Fatal(err)
21 }
22 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"}}
23 if _, err := w.finish(log, Spent{}); err != nil {
24 t.Fatal(err)
25 }
26 return dir
27}
28
29func TestReadBundleRoundTrip(t *testing.T) {
30 b, err := ReadBundle(writeBundle(t))
31 if err != nil {
32 t.Fatal(err)
33 }
34 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 {
35 t.Fatalf("bundle %+v", b)
36 }
37}
38
39func TestReadBundleRefusals(t *testing.T) {
40 for name, tamper := range map[string]func(t *testing.T, dir string){
41 "symlinked file": func(t *testing.T, dir string) {
42 data, _ := os.ReadFile(filepath.Join(dir, "s001.txt"))
43 other := filepath.Join(t.TempDir(), "elsewhere")
44 os.WriteFile(other, data, 0600)
45 os.Remove(filepath.Join(dir, "s001.txt"))
46 if err := os.Symlink(other, filepath.Join(dir, "s001.txt")); err != nil {
47 t.Fatal(err)
48 }
49 },
50 "symlinked manifest": func(t *testing.T, dir string) {
51 data, _ := os.ReadFile(filepath.Join(dir, ManifestName))
52 other := filepath.Join(t.TempDir(), "m.json")
53 os.WriteFile(other, data, 0600)
54 os.Remove(filepath.Join(dir, ManifestName))
55 os.Symlink(other, filepath.Join(dir, ManifestName))
56 },
57 "unknown name": func(t *testing.T, dir string) {
58 os.WriteFile(filepath.Join(dir, "notes.txt"), []byte("x"), 0600)
59 },
60 "unlisted generated name": func(t *testing.T, dir string) {
61 os.WriteFile(filepath.Join(dir, "s009.txt"), []byte("x"), 0600)
62 },
63 "hash mismatch": func(t *testing.T, dir string) {
64 os.WriteFile(filepath.Join(dir, "s001.txt"), []byte("Title: Shoq\n"), 0600)
65 },
66 "size mismatch": func(t *testing.T, dir string) {
67 os.WriteFile(filepath.Join(dir, "s001.txt"), []byte("Title: Shop!\n"), 0600)
68 },
69 "missing file": func(t *testing.T, dir string) {
70 os.Remove(filepath.Join(dir, "s002.png"))
71 },
72 "directory for a file": func(t *testing.T, dir string) {
73 os.Remove(filepath.Join(dir, "s002.png"))
74 os.Mkdir(filepath.Join(dir, "s002.png"), 0700)
75 },
76 "fifo for a file": func(t *testing.T, dir string) {
77 os.Remove(filepath.Join(dir, "s001.txt"))
78 if err := syscall.Mkfifo(filepath.Join(dir, "s001.txt"), 0600); err != nil {
79 t.Skip("mkfifo:", err)
80 }
81 },
82 "manifest names a path": func(t *testing.T, dir string) {
83 editManifest(t, dir, func(m *Manifest) { m.Files[0].Name = "../s001.txt" })
84 },
85 "manifest kind disagrees with name": func(t *testing.T, dir string) {
86 editManifest(t, dir, func(m *Manifest) { m.Files[0].Kind = KindImage })
87 },
88 "manifest duplicate": func(t *testing.T, dir string) {
89 editManifest(t, dir, func(m *Manifest) { m.Files = append(m.Files, m.Files[0]) })
90 },
91 "manifest oversize text": func(t *testing.T, dir string) {
92 editManifest(t, dir, func(m *Manifest) { m.Files[0].Size = MaxPageText + 1 })
93 },
94 "manifest unknown field": func(t *testing.T, dir string) {
95 data, _ := os.ReadFile(filepath.Join(dir, ManifestName))
96 os.WriteFile(filepath.Join(dir, ManifestName), []byte(strings.Replace(string(data), `{"version"`, `{"extra":1,"version"`, 1)), 0600)
97 },
98 "manifest too large": func(t *testing.T, dir string) {
99 os.WriteFile(filepath.Join(dir, ManifestName), []byte(strings.Repeat(" ", MaxManifestBytes+1)), 0600)
100 },
101 "image that is not a png": func(t *testing.T, dir string) {
102 data := []byte("not a png at all")
103 os.WriteFile(filepath.Join(dir, "s002.png"), data, 0600)
104 editManifest(t, dir, func(m *Manifest) { m.Files[1].Size, m.Files[1].SHA256 = int64(len(data)), sum(data) })
105 },
106 } {
107 t.Run(name, func(t *testing.T) {
108 dir := writeBundle(t)
109 tamper(t, dir)
110 if b, err := ReadBundle(dir); err == nil {
111 t.Fatalf("read a tampered bundle: %+v", b)
112 }
113 })
114 }
115 // The directory itself, through a symlink.
116 dir := writeBundle(t)
117 link := filepath.Join(t.TempDir(), "link")
118 os.Symlink(dir, link)
119 if _, err := ReadBundle(link); err == nil {
120 t.Fatal("read through a symlinked directory")
121 }
122}
123
124func editManifest(t *testing.T, dir string, edit func(*Manifest)) {
125 t.Helper()
126 data, err := os.ReadFile(filepath.Join(dir, ManifestName))
127 if err != nil {
128 t.Fatal(err)
129 }
130 var m Manifest
131 if err = json.Unmarshal(data, &m); err != nil {
132 t.Fatal(err)
133 }
134 edit(&m)
135 data, _ = json.Marshal(m)
136 if err = os.WriteFile(filepath.Join(dir, ManifestName), data, 0600); err != nil {
137 t.Fatal(err)
138 }
139}
140