]> Sergey Matveev's repositories - btrtrc.git/blobdiff - webseed/request_test.go
Drop support for go 1.20
[btrtrc.git] / webseed / request_test.go
index b59e7784d67e482298ed80deefb7274d3f9cb1ea..af3071f3fd4a61f930a94f53027ff17e88ab3a42 100644 (file)
@@ -7,23 +7,54 @@ import (
        qt "github.com/frankban/quicktest"
 )
 
-func TestTrailingPath(t *testing.T) {
+func TestDefaultPathEscaper(t *testing.T) {
        c := qt.New(t)
-       test := func(parts []string, result string) {
-               unescaped, err := url.QueryUnescape(trailingPath(parts[0], parts[1:], url.QueryEscape))
-               if !c.Check(err, qt.IsNil) {
-                       return
-               }
-               c.Check(unescaped, qt.Equals, result)
+       test := func(unescaped string, parts ...string) {
+               assertPartsUnescape(c, unescaped, parts...)
+       }
+       for _, tc := range defaultPathEscapeTestCases {
+               test(tc.escaped, tc.parts...)
        }
-       test([]string{"a_b-c", "d + e.f"}, "a_b-c/d + e.f")
-       test([]string{"a_1-b_c2", "d 3. (e, f).g"},
-               "a_1-b_c2/d 3. (e, f).g",
-       )
 }
 
-func TestTrailingPathForEmptyInfoName(t *testing.T) {
-       qt.Check(t, trailingPath("", []string{`ノ┬─┬ノ ︵ ( \o°o)\`}, url.QueryEscape), qt.Equals, "%E3%83%8E%E2%94%AC%E2%94%80%E2%94%AC%E3%83%8E+%EF%B8%B5+%28+%5Co%C2%B0o%29%5C")
-       qt.Check(t, trailingPath("", []string{"hello", "world"}, url.QueryEscape), qt.Equals, "hello/world")
-       qt.Check(t, trailingPath("war", []string{"and", "peace"}, url.QueryEscape), qt.Equals, "war/and/peace")
+// So we can manually check, and use these to seed fuzzing.
+var defaultPathEscapeTestCases = []struct {
+       escaped string
+       parts   []string
+}{
+       {"/", []string{"", ""}},
+       {"a_b-c/d + e.f", []string{"a_b-c", "d + e.f"}},
+       {"a_1-b_c2/d 3. (e, f).g", []string{"a_1-b_c2", "d 3. (e, f).g"}},
+       {"a_b-c/d + e.f", []string{"a_b-c", "d + e.f"}},
+       {"a_1-b_c2/d 3. (e, f).g", []string{"a_1-b_c2", "d 3. (e, f).g"}},
+       {"war/and/peace", []string{"war", "and", "peace"}},
+       {"he//o#world/world", []string{"he//o#world", "world"}},
+       {`ノ┬─┬ノ ︵ ( \o°o)\`, []string{`ノ┬─┬ノ ︵ ( \o°o)\`}},
+       {
+               `%aa + %bb/Parsi Tv - سرقت و باز کردن در ماشین در کم‌تر از ۳ ثانیه + فیلم.webm`,
+               []string{`%aa + %bb`, `Parsi Tv - سرقت و باز کردن در ماشین در کم‌تر از ۳ ثانیه + فیلم.webm`},
+       },
+}
+
+func assertPartsUnescape(c *qt.C, unescaped string, parts ...string) {
+       escaped := defaultPathEscaper(parts)
+       pathUnescaped, err := url.PathUnescape(escaped)
+       c.Assert(err, qt.IsNil)
+       c.Assert(pathUnescaped, qt.Equals, unescaped)
+       queryUnescaped, err := url.QueryUnescape(escaped)
+       c.Assert(err, qt.IsNil)
+       c.Assert(queryUnescaped, qt.Equals, unescaped)
+}
+
+func FuzzDefaultPathEscaper(f *testing.F) {
+       for _, tc := range defaultPathEscapeTestCases {
+               if len(tc.parts) == 2 {
+                       f.Add(tc.parts[0], tc.parts[1])
+               }
+       }
+       // I think a single separator is enough to test special handling around /. Also fuzzing doesn't
+       // let us take []string as an input.
+       f.Fuzz(func(t *testing.T, first, second string) {
+               assertPartsUnescape(qt.New(t), first+"/"+second, first, second)
+       })
 }