Debian Codegraph @

limit_test.go

raw symbols SCIP
1 package yaml_test
2
3 import (
4 "strings"
5 "testing"
6
7 "go.yaml.in/yaml/v2"
8 )
9
10 var limitTests = []struct {
11 name string
12 data []byte
13 error string
14 }{
15 {
16 name: "1000kb of maps with 100 aliases",
17 data: []byte(`{a: &a [{a}` + strings.Repeat(`,{a}`, 1000*1024/4-100) + `], b: &b [*a` + strings.Repeat(`,*a`, 99) + `]}`),
18 error: "yaml: document contains excessive aliasing",
19 }, {
20 name: "1000kb of deeply nested slices",
21 data: []byte(strings.Repeat(`[`, 1000*1024)),
22 error: "yaml: exceeded max depth of 10000",
23 }, {
24 name: "1000kb of deeply nested maps",
25 data: []byte("x: " + strings.Repeat(`{`, 1000*1024)),
26 error: "yaml: exceeded max depth of 10000",
27 }, {
28 name: "1000kb of deeply nested indents",
29 data: []byte(strings.Repeat(`- `, 1000*1024)),
30 error: "yaml: exceeded max depth of 10000",
31 }, {
32 name: "1000kb of 1000-indent lines",
33 data: []byte(strings.Repeat(strings.Repeat(`- `, 1000)+"\n", 1024/2)),
34 },
35 {name: "1kb of maps", data: []byte(`a: &a [{a}` + strings.Repeat(`,{a}`, 1*1024/4-1) + `]`)},
36 {name: "10kb of maps", data: []byte(`a: &a [{a}` + strings.Repeat(`,{a}`, 10*1024/4-1) + `]`)},
37 {name: "100kb of maps", data: []byte(`a: &a [{a}` + strings.Repeat(`,{a}`, 100*1024/4-1) + `]`)},
38 {name: "1000kb of maps", data: []byte(`a: &a [{a}` + strings.Repeat(`,{a}`, 1000*1024/4-1) + `]`)},
39 {name: "1000kb slice nested at max-depth", data: []byte(strings.Repeat(`[`, 10000) + `1` + strings.Repeat(`,1`, 1000*1024/2-20000-1) + strings.Repeat(`]`, 10000))},
40 {name: "1000kb slice nested in maps at max-depth", data: []byte("{a,b:\n" + strings.Repeat(" {a,b:", 10000-2) + ` [1` + strings.Repeat(",1", 1000*1024/2-6*10000-1) + `]` + strings.Repeat(`}`, 10000-1))},
41 {name: "1000kb of 10000-nested lines", data: []byte(strings.Repeat(`- `+strings.Repeat(`[`, 10000)+strings.Repeat(`]`, 10000)+"\n", 1000*1024/20000))},
42 }
43
44 func TestLimits(t *testing.T) {
45 if testing.Short() {
46 return
47 }
48 for _, tc := range limitTests {
49 var v interface{}
50 err := yaml.Unmarshal(tc.data, &v)
51 if len(tc.error) > 0 {
52 if err == nil || !strings.Contains(err.Error(), tc.error) {
53 t.Fatalf("testcase: %s. Expected error containing %q, got %v", tc.name, tc.error, err)
54 }
55 } else {
56 if err != nil {
57 t.Fatalf("testcase: %s. Unmarshal() got error %v", tc.name, err)
58 }
59 }
60 }
61 }
62
63 func Benchmark1000KB100Aliases(b *testing.B) {
64 benchmark(b, "1000kb of maps with 100 aliases")
65 }
66 func Benchmark1000KBDeeplyNestedSlices(b *testing.B) {
67 benchmark(b, "1000kb of deeply nested slices")
68 }
69 func Benchmark1000KBDeeplyNestedMaps(b *testing.B) {
70 benchmark(b, "1000kb of deeply nested maps")
71 }
72 func Benchmark1000KBDeeplyNestedIndents(b *testing.B) {
73 benchmark(b, "1000kb of deeply nested indents")
74 }
75 func Benchmark1000KB1000IndentLines(b *testing.B) {
76 benchmark(b, "1000kb of 1000-indent lines")
77 }
78 func Benchmark1KBMaps(b *testing.B) {
79 benchmark(b, "1kb of maps")
80 }
81 func Benchmark10KBMaps(b *testing.B) {
82 benchmark(b, "10kb of maps")
83 }
84 func Benchmark100KBMaps(b *testing.B) {
85 benchmark(b, "100kb of maps")
86 }
87 func Benchmark1000KBMaps(b *testing.B) {
88 benchmark(b, "1000kb of maps")
89 }
90
91 func BenchmarkDeepSlice(b *testing.B) {
92 benchmark(b, "1000kb slice nested at max-depth")
93 }
94
95 func BenchmarkDeepFlow(b *testing.B) {
96 benchmark(b, "1000kb slice nested in maps at max-depth")
97 }
98
99 func Benchmark1000KBMaxDepthNested(b *testing.B) {
100 benchmark(b, "1000kb of 10000-nested lines")
101 }
102
103 func benchmark(b *testing.B, name string) {
104 for _, t := range limitTests {
105 if t.name != name {
106 continue
107 }
108
109 b.ResetTimer()
110
111 for i := 0; i < b.N; i++ {
112 var v interface{}
113 err := yaml.Unmarshal(t.data, &v)
114 if len(t.error) > 0 {
115 if err == nil {
116 b.Errorf("expected error, got none")
117 } else if err.Error() != t.error {
118 b.Errorf("expected error '%s', got '%s'", t.error, err.Error())
119 }
120 } else {
121 if err != nil {
122 b.Errorf("unexpected error: %v", err)
123 }
124 }
125 }
126
127 return
128 }
129
130 b.Errorf("testcase %q not found", name)
131 }