package mapatest import ( "fmt" "net/http" "net/http/httptest" "net/url" "regexp" "sort" "strconv" "strings" "testing" "time" ) // IMG1, scope (images spec §6), extending gate 1 and L6. Ana has a // private page with an image, a pending upload, and the ids of both // typed into a team page by Bo. Bo's output is byte-identical with or // without each of those: the team page and its island, its Markdown // screen, /links/resolve, GET /i/ and its remove page (the // same status, headers and body as an id that never existed), and // POST /images/copy. // imageIDs draws image public ids from a counter: what random ids give // in expectation, made repeatable so two worlds compare byte for byte. func imageIDs(start int64) func() int64 { next := start return func() int64 { next++ return next } } func base36(value int64) string { return strconv.FormatInt(value, 36) } // Ana's two ids in the world that has them, and an id that never was. var ( anaPrivate = base36(800001) anaPending = base36(800002) neverWas = base36(799999) ) func img1World(t *testing.T, private bool) (*rig, string) { t.Helper() rig := newRig(t) store := rig.built.Store store.SetClock(func() time.Time { return gate1Clock }) store.SetNonces(streams()) if private { store.SetImageNonces(imageIDs(800000)) secret := rig.upload(t, Ana, pngOf(t, 1)) rig.post(t, Ana, "/scratch", url.Values{"body": { secret.markdown("my payslip")}}) rig.upload(t, Ana, pngOf(t, 2)) } store.SetImageNonces(imageIDs(5000)) team := rig.upload(t, Bo, pngOf(t, 3)) typed := team.markdown("ours") + "\n\n![typed](/i/" + anaPrivate + ") ![typed too](/i/" + anaPending + ")" gallery := rig.createPage(t, Bo, "Gallery", typed) // A conflict for Bo to be shown, in both worlds (Fable L13): Ana and // Bo edit the block holding the typed ids from the same revision. base := revisionAs(t, rig, Bo, gallery) rig.post(t, Ana, gallery, url.Values{"revision": {base}, "body": {typed + " Ana."}}) rig.post(t, Bo, gallery, url.Values{"revision": {base}, "body": {typed + " Bo."}}) return rig, gallery } // editRevision is the Markdown screen's revision: random in both worlds. var editRevision = regexp.MustCompile(`name="revision" value="[0-9]+"`) // revisionNumber is a revision in a URL or a form: random in both // worlds, so compared as one value. var revisionNumber = regexp.MustCompile(`(/history/|data-version="|` + `name="(?:current|revision|version)" value=")[0-9]+`) func steady(page string) string { return revisionNumber.ReplaceAllString(steadyRevisions(page), `${1}R`) } // exchange is one response, whole: status, headers and body. func exchange(recorder *httptest.ResponseRecorder) string { var keys []string for key := range recorder.Header() { keys = append(keys, key) } sort.Strings(keys) var out strings.Builder fmt.Fprintf(&out, "%d\n", recorder.Code) for _, key := range keys { fmt.Fprintf(&out, "%s: %v\n", key, recorder.Header()[key]) } out.WriteString(recorder.Body.String()) return out.String() } func boSeesImages(t *testing.T, rig *rig, gallery string) map[string]string { t.Helper() seen := map[string]string{ "page": rig.page(t, Bo, gallery), "edit": editRevision.ReplaceAllString( rig.page(t, Bo, gallery+"/edit"), `name="revision" value="R"`), } _, states := rig.postJSON(Bo, "/links/resolve", url.Values{ "image": {anaPrivate, anaPending, neverWas}, "page": {strings.TrimPrefix(gallery, "/p/")}}) seen["resolve"] = fmt.Sprint(states) for _, id := range []string{anaPrivate, anaPending, neverWas} { seen["get "+id] = exchange(rig.get(t, Bo, "/i/"+id)) seen["remove "+id] = exchange(rig.get(t, Bo, "/i/"+id+"/remove")) } // History's rendered first revision, and the conflict view (Fable // L13): each draws the typed ids. history := rig.page(t, Bo, gallery+"/history") first := lastBetween(history, gallery+"/history/", `"`) seen["history"] = rig.page(t, Bo, gallery+"/history/"+first) seen["conflict"] = rig.page(t, Bo, gallery+"/conflict") copied := rig.post(t, Bo, "/images/copy", url.Values{ "id": {anaPrivate, anaPending}}) seen["copy"] = exchange(copied) return seen } func TestIMG1ImagesLeakNothing(t *testing.T) { quiet, gallery := img1World(t, false) busy, busyGallery := img1World(t, true) if gallery != busyGallery { t.Fatalf("the worlds differ before Ana writes: %s, %s", gallery, busyGallery) } without, with := boSeesImages(t, quiet, gallery), boSeesImages(t, busy, gallery) for key, want := range without { want, got := steady(want), steady(with[key]) if got != want { at := 0 for at < len(want) && at < len(got) && want[at] == got[at] { at++ } t.Errorf("Bo's %s differs when Ana has images:\n%q\n%q", key, want[max(0, at-60):min(len(want), at+120)], got[max(0, at-60):min(len(got), at+120)]) } } for _, id := range []string{anaPrivate, anaPending} { if with["get "+id] != with["get "+neverWas] || with["remove "+id] != with["remove "+neverWas] { t.Errorf("Bo's answer for Ana's %s is not a missing id's", id) } } for _, key := range []string{"page", "history", "conflict"} { if !strings.Contains(without[key], `