bencodedemo_test.gno
1.99 Kb · 44 lines
1package bencodedemo
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/uassert/v0"
8)
9
10// TestRenderRefusesOversizedInput pins the page's own limit, which is tighter
11// than the library's: a Render runs under the chain's query gas meter and
12// gnoweb does not bound a render argument.
13func TestRenderRefusesOversizedInput(t *testing.T) {
14 big := "l" + strings.Repeat("1:a", (MaxInput-2)/3) + "e"
15 uassert.True(t, len(big) <= MaxInput, "the control input must be accepted")
16 got := Render("/" + big)
17 uassert.True(t, strings.Contains(got, "- list, "), "a legal input still decodes")
18
19 over := "l" + strings.Repeat("1:a", MaxInput) + "e"
20 got = Render("/" + over)
21 uassert.True(t, strings.Contains(got, "This page decodes at most 512."),
22 "an oversized input is refused by length, before decoding")
23 uassert.False(t, strings.Contains(got, "- list, "), "and nothing is decoded")
24}
25
26// TestRenderEscapesCallerMarkdown pins the escaping, which a pinned example
27// cannot show convincingly: the input there would have to be hostile.
28func TestRenderEscapesCallerMarkdown(t *testing.T) {
29 // 10 bytes, not 9: a wrong length is rejected before the decoded leaf is
30 // ever rendered, and the test would then only cover the echoed heading.
31 got := Render("/d3:cow10:[x](/evil)e")
32 uassert.False(t, strings.Contains(got, "Rejected"), "the input must decode")
33 uassert.False(t, strings.Contains(got, "[x](/evil)"), "the link must not survive")
34 uassert.True(t, strings.Contains(got, "**cow**: string "), "the decoded leaf is rendered")
35 uassert.Equal(t, 3, strings.Count(got, `\[x\]`), "escaped in the heading, the leaf and the re-encoding")
36}
37
38// TestRenderKeepsCallerBytes pins that the page does not trim the value it was
39// handed: a trailing space is part of a six byte string, not routing noise.
40func TestRenderKeepsCallerBytes(t *testing.T) {
41 got := Render("/6:hello ")
42 uassert.True(t, strings.Contains(got, "string hello"), "the space-bearing string decodes")
43 uassert.False(t, strings.Contains(got, "Rejected"), "and is not rejected")
44}