oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapatest
2
3import (
4 "fmt"
5 "net/http"
6 "net/http/httptest"
7 "net/url"
8 "regexp"
9 "sort"
10 "strconv"
11 "strings"
12 "testing"
13 "time"
14)
15
16// IMG1, scope (images spec §6), extending gate 1 and L6. Ana has a
17// private page with an image, a pending upload, and the ids of both
18// typed into a team page by Bo. Bo's output is byte-identical with or
19// without each of those: the team page and its island, its Markdown
20// screen, /links/resolve, GET /i/<Ana's id> and its remove page (the
21// same status, headers and body as an id that never existed), and
22// POST /images/copy.
23
24// imageIDs draws image public ids from a counter: what random ids give
25// in expectation, made repeatable so two worlds compare byte for byte.
26func imageIDs(start int64) func() int64 {
27 next := start
28 return func() int64 {
29 next++
30 return next
31 }
32}
33
34func base36(value int64) string { return strconv.FormatInt(value, 36) }
35
36// Ana's two ids in the world that has them, and an id that never was.
37var (
38 anaPrivate = base36(800001)
39 anaPending = base36(800002)
40 neverWas = base36(799999)
41)
42
43func img1World(t *testing.T, private bool) (*rig, string) {
44 t.Helper()
45 rig := newRig(t)
46 store := rig.built.Store
47 store.SetClock(func() time.Time { return gate1Clock })
48 store.SetNonces(streams())
49 if private {
50 store.SetImageNonces(imageIDs(800000))
51 secret := rig.upload(t, Ana, pngOf(t, 1))
52 rig.post(t, Ana, "/scratch", url.Values{"body": {
53 secret.markdown("my payslip")}})
54 rig.upload(t, Ana, pngOf(t, 2))
55 }
56 store.SetImageNonces(imageIDs(5000))
57 team := rig.upload(t, Bo, pngOf(t, 3))
58 typed := team.markdown("ours") + "\n\n![typed](/i/" + anaPrivate +
59 ") ![typed too](/i/" + anaPending + ")"
60 gallery := rig.createPage(t, Bo, "Gallery", typed)
61 // A conflict for Bo to be shown, in both worlds (Fable L13): Ana and
62 // Bo edit the block holding the typed ids from the same revision.
63 base := revisionAs(t, rig, Bo, gallery)
64 rig.post(t, Ana, gallery, url.Values{"revision": {base},
65 "body": {typed + " Ana."}})
66 rig.post(t, Bo, gallery, url.Values{"revision": {base},
67 "body": {typed + " Bo."}})
68 return rig, gallery
69}
70
71// editRevision is the Markdown screen's revision: random in both worlds.
72var editRevision = regexp.MustCompile(`name="revision" value="[0-9]+"`)
73
74// revisionNumber is a revision in a URL or a form: random in both
75// worlds, so compared as one value.
76var revisionNumber = regexp.MustCompile(`(/history/|data-version="|` +
77 `name="(?:current|revision|version)" value=")[0-9]+`)
78
79func steady(page string) string {
80 return revisionNumber.ReplaceAllString(steadyRevisions(page), `${1}R`)
81}
82
83// exchange is one response, whole: status, headers and body.
84func exchange(recorder *httptest.ResponseRecorder) string {
85 var keys []string
86 for key := range recorder.Header() {
87 keys = append(keys, key)
88 }
89 sort.Strings(keys)
90 var out strings.Builder
91 fmt.Fprintf(&out, "%d\n", recorder.Code)
92 for _, key := range keys {
93 fmt.Fprintf(&out, "%s: %v\n", key, recorder.Header()[key])
94 }
95 out.WriteString(recorder.Body.String())
96 return out.String()
97}
98
99func boSeesImages(t *testing.T, rig *rig, gallery string) map[string]string {
100 t.Helper()
101 seen := map[string]string{
102 "page": rig.page(t, Bo, gallery),
103 "edit": editRevision.ReplaceAllString(
104 rig.page(t, Bo, gallery+"/edit"), `name="revision" value="R"`),
105 }
106 _, states := rig.postJSON(Bo, "/links/resolve", url.Values{
107 "image": {anaPrivate, anaPending, neverWas},
108 "page": {strings.TrimPrefix(gallery, "/p/")}})
109 seen["resolve"] = fmt.Sprint(states)
110 for _, id := range []string{anaPrivate, anaPending, neverWas} {
111 seen["get "+id] = exchange(rig.get(t, Bo, "/i/"+id))
112 seen["remove "+id] = exchange(rig.get(t, Bo, "/i/"+id+"/remove"))
113 }
114 // History's rendered first revision, and the conflict view (Fable
115 // L13): each draws the typed ids.
116 history := rig.page(t, Bo, gallery+"/history")
117 first := lastBetween(history, gallery+"/history/", `"`)
118 seen["history"] = rig.page(t, Bo, gallery+"/history/"+first)
119 seen["conflict"] = rig.page(t, Bo, gallery+"/conflict")
120 copied := rig.post(t, Bo, "/images/copy", url.Values{
121 "id": {anaPrivate, anaPending}})
122 seen["copy"] = exchange(copied)
123 return seen
124}
125
126func TestIMG1ImagesLeakNothing(t *testing.T) {
127 quiet, gallery := img1World(t, false)
128 busy, busyGallery := img1World(t, true)
129 if gallery != busyGallery {
130 t.Fatalf("the worlds differ before Ana writes: %s, %s", gallery,
131 busyGallery)
132 }
133 without, with := boSeesImages(t, quiet, gallery),
134 boSeesImages(t, busy, gallery)
135 for key, want := range without {
136 want, got := steady(want), steady(with[key])
137 if got != want {
138 at := 0
139 for at < len(want) && at < len(got) && want[at] == got[at] {
140 at++
141 }
142 t.Errorf("Bo's %s differs when Ana has images:\n%q\n%q", key,
143 want[max(0, at-60):min(len(want), at+120)],
144 got[max(0, at-60):min(len(got), at+120)])
145 }
146 }
147 for _, id := range []string{anaPrivate, anaPending} {
148 if with["get "+id] != with["get "+neverWas] ||
149 with["remove "+id] != with["remove "+neverWas] {
150 t.Errorf("Bo's answer for Ana's %s is not a missing id's", id)
151 }
152 }
153 for _, key := range []string{"page", "history", "conflict"} {
154 if !strings.Contains(without[key], `<img src="/i/`) ||
155 !strings.Contains(without[key], `mapa-image="unavailable"`) {
156 t.Fatalf("the comparison is vacuous: %s draws no image", key)
157 }
158 }
159}
160
161// Ana's own view of the same world shows her images, or the gate would
162// pass because nobody sees any.
163func TestIMG1AnaSeesHerOwn(t *testing.T) {
164 busy, _ := img1World(t, true)
165 if got := busy.get(t, Ana, "/i/"+anaPrivate); got.Code != http.StatusOK {
166 t.Fatalf("Ana's own private image: %d", got.Code)
167 }
168 if got := busy.get(t, Ana, "/i/"+anaPending); got.Code != http.StatusOK {
169 t.Fatalf("Ana's own pending image: %d", got.Code)
170 }
171}
172
173// IMG4, nothing written by a GET (images spec §6), extending gate 10
174// and L7: GET /i/{id} (200 and 304), a page with images, its island,
175// and the remove confirm page leave every row as it was.
176func TestIMG4AGetWritesNothing(t *testing.T) {
177 rig := newRig(t)
178 shown := rig.upload(t, Ana, pngOf(t, 1))
179 page := rig.createPage(t, Ana, "Pictures", shown.markdown("a"))
180 pending := rig.upload(t, Ana, pngOf(t, 2))
181 before := dumpAll(t, rig)
182 for _, path := range []string{shown.Src, page, page + "/edit",
183 shown.Src + "/remove", pending.Src, "/i/" + neverWas} {
184 rig.get(t, Ana, path)
185 rig.get(t, Bo, path)
186 }
187 request := httptest.NewRequest(http.MethodGet, shown.Src, nil)
188 request.Header.Set("If-None-Match", `"`+shown.ID+`"`)
189 recorder := httptest.NewRecorder()
190 rig.built.As(Bo).ServeHTTP(recorder, request)
191 if recorder.Code != http.StatusNotModified {
192 t.Fatalf("If-None-Match: %d", recorder.Code)
193 }
194 if dumpAll(t, rig) != before {
195 t.Fatal("a GET wrote to the database")
196 }
197 rig.post(t, Ana, shown.Src+"/remove", nil)
198 if dumpAll(t, rig) == before {
199 t.Fatal("the dump is vacuous: a removal did not change it")
200 }
201}
202