|
| 1 | +package fuzzing |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "golang.org/x/net/html" |
| 10 | + |
| 11 | + v8 "rogchap.com/v8go" |
| 12 | +) |
| 13 | + |
| 14 | +var iso = v8.NewIsolate() |
| 15 | + |
| 16 | +var testcases = []string{ |
| 17 | + "hello", |
| 18 | + "<script>console.log('hello')</script>", |
| 19 | + "text data\n can contain all sorts of \"characters\" & symbols", |
| 20 | + "123", |
| 21 | + "</script>", |
| 22 | +} |
| 23 | + |
| 24 | +func FuzzComponentString(f *testing.F) { |
| 25 | + for _, tc := range testcases { |
| 26 | + f.Add([]byte(tc)) |
| 27 | + } |
| 28 | + f.Fuzz(func(t *testing.T, v []byte) { |
| 29 | + // Render template. |
| 30 | + buf := new(strings.Builder) |
| 31 | + |
| 32 | + values := []any{ |
| 33 | + string(v), |
| 34 | + []string{string(v)}, |
| 35 | + map[string]string{"value": string(v)}, |
| 36 | + } |
| 37 | + for _, value := range values { |
| 38 | + buf.Reset() |
| 39 | + if err := String(value).Render(context.Background(), buf); err != nil { |
| 40 | + t.Skip(err) |
| 41 | + } |
| 42 | + runTest(t, buf.String()) |
| 43 | + } |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +func FuzzComponentAny(f *testing.F) { |
| 48 | + for _, tc := range testcases { |
| 49 | + jsonValue, err := json.Marshal(tc) |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + f.Add(jsonValue) |
| 54 | + } |
| 55 | + f.Fuzz(func(t *testing.T, v []byte) { |
| 56 | + // Render template. |
| 57 | + buf := new(strings.Builder) |
| 58 | + |
| 59 | + values := []any{ |
| 60 | + string(v), |
| 61 | + []string{string(v)}, |
| 62 | + map[string]string{"value": string(v)}, |
| 63 | + } |
| 64 | + for _, value := range values { |
| 65 | + buf.Reset() |
| 66 | + if err := Any(value).Render(context.Background(), buf); err != nil { |
| 67 | + t.Skip(err) |
| 68 | + } |
| 69 | + runTest(t, buf.String()) |
| 70 | + } |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func getFirstScript(n *html.Node) *html.Node { |
| 75 | + if n.Data == "script" { |
| 76 | + return n |
| 77 | + } |
| 78 | + for c := n.FirstChild; c != nil; c = c.NextSibling { |
| 79 | + if s := getFirstScript(c); s != nil { |
| 80 | + return s |
| 81 | + } |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func runTest(t *testing.T, templateOutput string) { |
| 87 | + // Parse HTML. |
| 88 | + n, err := html.Parse(strings.NewReader(templateOutput)) |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("failed to parse HTML: %v", err) |
| 91 | + } |
| 92 | + sn := getFirstScript(n) |
| 93 | + if sn == nil { |
| 94 | + t.Fatalf("no script tag found") |
| 95 | + } |
| 96 | + |
| 97 | + // Extract JavaScript. |
| 98 | + script := sn.FirstChild.Data |
| 99 | + |
| 100 | + // Run JavaScript. |
| 101 | + v8ctx := v8.NewContext(iso) |
| 102 | + if _, err = v8ctx.RunScript(script, "component.js"); err != nil { |
| 103 | + t.Fatalf("failed to parse script: %v", err) |
| 104 | + } |
| 105 | + if _, err = v8ctx.RunScript("const result = logValue()", "component.js"); err != nil { |
| 106 | + t.Fatalf("failed to get value: %v", err) |
| 107 | + } |
| 108 | + actual, err := v8ctx.RunScript("result", "component.js") |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("failed to get result: %v", err) |
| 111 | + } |
| 112 | + defer v8ctx.Close() |
| 113 | + |
| 114 | + // Assert. |
| 115 | + if !actual.IsString() { |
| 116 | + t.Fatalf("expected boolean, got %T", actual.Object().Value) |
| 117 | + } |
| 118 | + if actual.String() != "result_ok" { |
| 119 | + t.Fatalf("expected 'result_ok', got %v", actual.Boolean()) |
| 120 | + } |
| 121 | +} |
0 commit comments