Skip to content

Commit aaaad08

Browse files
committed
test: added fuzz test for reference parser
Signed-off-by: Frédéric BIDON <fredbi@yahoo.com>
1 parent 20e9d34 commit aaaad08

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

fuzz_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package jsonreference
5+
6+
import (
7+
"iter"
8+
"slices"
9+
"strings"
10+
"testing"
11+
12+
"github.com/go-openapi/testify/v2/require"
13+
)
14+
15+
func FuzzParse(f *testing.F) {
16+
// initial seed
17+
cumulated := make([]string, 0, 100)
18+
for generator := range generators() {
19+
f.Add(generator)
20+
21+
cumulated = append(cumulated, generator)
22+
f.Add(strings.Join(cumulated, ""))
23+
}
24+
25+
ref := Ref{}
26+
f.Fuzz(func(t *testing.T, input string) {
27+
require.NotPanics(t, func() {
28+
_ = ref.parse(input)
29+
})
30+
})
31+
}
32+
33+
func generators() iter.Seq[string] {
34+
return slices.Values([]string{
35+
"",
36+
"https://localhost/%F0%9F%8C%AD#/%F0%9F%8D%94",
37+
"#/ok",
38+
"%2",
39+
"http://host/path/a/b/c#/f/a/b",
40+
"http://host/path/a/b/c",
41+
"http://host/path/a/b/c#",
42+
"#/fragment/only",
43+
"/documents/document.json",
44+
"document.json",
45+
"http://www.test.com/doc.json",
46+
"#/a/b",
47+
"http://www.test.com/doc.json",
48+
"http://www.test2.com/doc.json#bla",
49+
"http://a/b/c/d;p?q",
50+
"g:h", "g:h",
51+
"g", "http://a/b/c/g",
52+
"./g", "http://a/b/c/g",
53+
"g/", "http://a/b/c/g/",
54+
"/g", "http://a/g",
55+
"//g", "http://g",
56+
"?y", "http://a/b/c/d;p?y",
57+
"g?y", "http://a/b/c/g?y",
58+
"#s", "http://a/b/c/d;p?q#s",
59+
"g#s", "http://a/b/c/g#s",
60+
"g?y#s", "http://a/b/c/g?y#s",
61+
";x", "http://a/b/c/;x",
62+
"g;x", "http://a/b/c/g;x",
63+
"g;x?y#s", "http://a/b/c/g;x?y#s",
64+
"", "http://a/b/c/d;p?q",
65+
".", "http://a/b/c/",
66+
"./", "http://a/b/c/",
67+
"..", "http://a/b/",
68+
"../", "http://a/b/",
69+
"../g", "http://a/b/g",
70+
"../..", "http://a/",
71+
"../../", "http://a/",
72+
"../../g", "http://a/g",
73+
"http://tools.ietf.org/html/rfc3986#section-5.4.2",
74+
"../../../g", "http://a/g",
75+
"../../../../g", "http://a/g",
76+
"https://localhost/🌭#/🍔",
77+
"https://localhost/%F0%9F%8C%AD#/%F0%9F%8D%94",
78+
"/./g", "http://a/g",
79+
"/../g", "http://a/g",
80+
"g.", "http://a/b/c/g.",
81+
".g", "http://a/b/c/.g",
82+
"g..", "http://a/b/c/g..",
83+
"..g", "http://a/b/c/..g",
84+
"./../g", "http://a/b/g",
85+
"./g/.", "http://a/b/c/g/",
86+
"g/./h", "http://a/b/c/g/h",
87+
"g/../h", "http://a/b/c/h",
88+
"g;x=1/./y", "http://a/b/c/g;x=1/y",
89+
"g;x=1/../y", "http://a/b/c/y",
90+
"g?y/./x", "http://a/b/c/g?y/./x",
91+
"g?y/../x", "http://a/b/c/g?y/../x",
92+
"g#s/./x", "http://a/b/c/g#s/./x",
93+
"g#s/../x", "http://a/b/c/g#s/../x",
94+
"http:g", "http:g", // for strict parsers
95+
"http:g", "http://a/b/c/g",
96+
`a`,
97+
``, `/`, `/`, `/a~1b`, `/a~1b`, `/c%d`, `/e^f`, `/g|h`, `/i\j`, `/k"l`, `/ `, `/m~0n`,
98+
`/foo`, `/0`,
99+
})
100+
}

0 commit comments

Comments
 (0)