Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

bounded_test.gno

2.63 Kb · 82 lines
 1package threads
 2
 3import (
 4	"strconv"
 5	"strings"
 6	"testing"
 7
 8	"gno.land/p/nt/testutils/v0"
 9)
10
11// Finding 325-cost-at-caps-1 (review run 001-cd0b6a9).
12//
13// A Render is paid for by every reader, so its cost must be bounded by what it
14// SHOWS, not by what anybody has ever written. Post and Reply are both
15// permissionless and the page key is any string, so an attacker fills a page
16// they do not own and every render of it inherits the cost. There is no delete
17// path anywhere in threads, so it is irreversible.
18//
19// These tests are the invariant, not the implementation: the rendered output
20// must stop growing once the collection is past the cap.
21
22// fill posts n threads on page from n distinct addresses.
23func fill(cur realm, t *testing.T, page string, n int) {
24	t.Helper()
25	for i := 0; i < n; i++ {
26		testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("filler" + strconv.Itoa(i))))
27		Post(cross(cur), page, "thread number "+strconv.Itoa(i))
28	}
29}
30
31func TestAPageRenderIsBoundedByWhatItShows(cur realm, t *testing.T) {
32	reset(cur)
33	const page = "gno.land/r/moul/home"
34
35	fill(cur, t, page, 40)
36	small := Render("page/" + page)
37
38	fill(cur, t, page, 160) // 200 total
39	big := Render("page/" + page)
40
41	rowsSmall := strings.Count(small, "\n|")
42	rowsBig := strings.Count(big, "\n|")
43
44	if rowsBig > rowsSmall {
45		t.Errorf("the page render grew from %d rows to %d when the board went from 40 threads to 200: "+
46			"its cost is bounded by what has been written, not by what it shows", rowsSmall, rowsBig)
47	}
48	if rowsSmall == 0 {
49		t.Fatal("no table rows rendered at all, so this test is measuring nothing")
50	}
51}
52
53func TestAThreadRenderIsBoundedByWhatItShows(cur realm, t *testing.T) {
54	reset(cur)
55	const page = "gno.land/r/moul/home"
56
57	testing.SetRealm(testing.NewUserRealm(alice))
58	id := Post(cross(cur), page, "a thread with many replies")
59
60	for i := 0; i < 40; i++ {
61		testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("rep" + strconv.Itoa(i))))
62		Reply(cross(cur), id, "reply "+strconv.Itoa(i))
63	}
64	small := Render("thread/" + strconv.FormatInt(id, 10))
65
66	for i := 40; i < 200; i++ {
67		testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("rep" + strconv.Itoa(i))))
68		Reply(cross(cur), id, "reply "+strconv.Itoa(i))
69	}
70	big := Render("thread/" + strconv.FormatInt(id, 10))
71
72	rowsSmall := strings.Count(small, "\n|")
73	rowsBig := strings.Count(big, "\n|")
74
75	if rowsBig > rowsSmall {
76		t.Errorf("the thread render grew from %d rows to %d when the replies went from 40 to 200: "+
77			"every reader pays for every reply anybody ever wrote", rowsSmall, rowsBig)
78	}
79	if rowsSmall == 0 {
80		t.Fatal("no table rows rendered at all, so this test is measuring nothing")
81	}
82}