package main // Exhibit 12. A person uploads in storage's own page. The zoo proves the same path works: it asks // storage's /app/roundtrip to run the browser's upload (begin, presigned PUT to B2, commit, list), then // downloads what storage hands back and compares the bytes with what it sent. // call_app("storage", "/app/roundtrip", true, "POST", bytes) // rc.app_headers("storage", true) // The check does this as the robot, through our own /api/12/roundtrip. import "core:fmt" import "ratcloud:rc" Roundtrip :: struct { size: int `json:"size"`, got: int `json:"got"`, matched: bool `json:"matched"`, sha256: string `json:"sha256"`, } roundtrip_once :: proc() -> (Roundtrip, string) { bytes := random_bytes(4096 + random_int(0, 4096)) a := call_app("storage", "/app/roundtrip?path=zoo-roundtrip.bin", true, "POST", string(bytes), "application/octet-stream", 30_000) r := parse(a.body) if a.status != 200 || r == nil do return {}, fmt.tprintf("storage's roundtrip answered %d: %s%s", a.status, clip(a.body, 160), a.err) if !jbool(r, "listed") do return {}, "uploaded, but the file was not in the listing" // The download is a presigned B2 URL, fetched like a browser would: no assertion on it. back := rc.fetch({url = jstr(r, "download"), follow = true, timeout_ms = 30_000}) if back.status != 200 do return {}, fmt.tprintf("the download answered %d", back.status) sum := sha256(bytes) return {len(bytes), len(back.body), eq_bytes(sum, sha256(back.body)), hex(sum)[:12]}, "" } e12 :: proc() -> Exhibit { return { n = 12, wing = "Storage", title = "Your files", promise = "You can upload a file, see it listed, and download it again unchanged.", block = "storage keeps each person's files (B2-backed) behind their sign-in; an app links to it and never holds storage's keys. The check here uploads random bytes, lists them, downloads them and compares.", src = {"e12_your_files.odin", #load("e12_your_files.odin", string)}, show = proc(me: ^rc.User) -> string { d, s, found := stored_data(12, Roundtrip) last := `
No matching roundtrip recorded yet.
` if found && d.matched do last = fmt.tprintf(`Last roundtrip, %s: %d bytes uploaded, listed and downloaded again: matched
`, ago(s.at), d.size) return cat(last, `Open storage, upload something, and download it again.
`) }, api = proc(a: ^Api) -> bool { if a.do_ != "roundtrip" || !a.post do return false if a.me == nil {reply_err(a, 401, "sign in first"); return true} r, err := roundtrip_once() if err != "" {reply_err(a, 502, err); return true} reply(a, r) return true }, // The robot asks the zoo to run the roundtrip as it (so the assertion names the robot). check = proc() -> Check { b, err := robot_at() if err != "" do return fail(err) b.timeout_ms = 40_000 r := post(b, self_url("/api/12/roundtrip")) b.timeout_ms = 15_000 if r.status != 200 || r.j == nil do return fail(fmt.tprintf("the roundtrip failed (%d): %s", r.status, jstr(r.j, "error") if r.j != nil else clip(r.body, 120))) j: Roundtrip json_into(r.body, &j) if !j.matched do return fail(fmt.tprintf("the downloaded copy differs from what was uploaded (%d bytes up, %d down)", j.size, j.got), j) return pass(fmt.tprintf("uploaded %d random bytes, listed them, downloaded them again: matched (sha256 %s)", j.size, j.sha256), j) }, } }