github.com/anacrolix/torrent@v1.61.0/webseed/request_test.go (about)

     1  package webseed
     2  
     3  import (
     4  	"net/url"
     5  	"testing"
     6  
     7  	qt "github.com/go-quicktest/qt"
     8  )
     9  
    10  func TestDefaultPathEscaper(t *testing.T) {
    11  	test := func(unescaped string, parts ...string) {
    12  		assertPartsUnescape(t, unescaped, parts...)
    13  	}
    14  	for _, tc := range defaultPathEscapeTestCases {
    15  		test(tc.escaped, tc.parts...)
    16  	}
    17  }
    18  
    19  // So we can manually check, and use these to seed fuzzing.
    20  var defaultPathEscapeTestCases = []struct {
    21  	escaped string
    22  	parts   []string
    23  }{
    24  	{"/", []string{"", ""}},
    25  	{"a_b-c/d + e.f", []string{"a_b-c", "d + e.f"}},
    26  	{"a_1-b_c2/d 3. (e, f).g", []string{"a_1-b_c2", "d 3. (e, f).g"}},
    27  	{"a_b-c/d + e.f", []string{"a_b-c", "d + e.f"}},
    28  	{"a_1-b_c2/d 3. (e, f).g", []string{"a_1-b_c2", "d 3. (e, f).g"}},
    29  	{"war/and/peace", []string{"war", "and", "peace"}},
    30  	{"he//o#world/world", []string{"he//o#world", "world"}},
    31  	{`ノ┬─┬ノ ︵ ( \o°o)\`, []string{`ノ┬─┬ノ ︵ ( \o°o)\`}},
    32  	{
    33  		`%aa + %bb/Parsi Tv - سرقت و باز کردن در ماشین در کم‌تر از ۳ ثانیه + فیلم.webm`,
    34  		[]string{`%aa + %bb`, `Parsi Tv - سرقت و باز کردن در ماشین در کم‌تر از ۳ ثانیه + فیلم.webm`},
    35  	},
    36  }
    37  
    38  func assertPartsUnescape(t *testing.T, unescaped string, parts ...string) {
    39  	escaped := defaultPathEscaper(parts)
    40  	pathUnescaped, err := url.PathUnescape(escaped)
    41  	qt.Assert(t, qt.IsNil(err))
    42  	qt.Assert(t, qt.Equals(pathUnescaped, unescaped))
    43  	queryUnescaped, err := url.QueryUnescape(escaped)
    44  	qt.Assert(t, qt.IsNil(err))
    45  	qt.Assert(t, qt.Equals(queryUnescaped, unescaped))
    46  }
    47  
    48  func FuzzDefaultPathEscaper(f *testing.F) {
    49  	for _, tc := range defaultPathEscapeTestCases {
    50  		if len(tc.parts) == 2 {
    51  			f.Add(tc.parts[0], tc.parts[1])
    52  		}
    53  	}
    54  	// I think a single separator is enough to test special handling around /. Also fuzzing doesn't
    55  	// let us take []string as an input.
    56  	f.Fuzz(func(t *testing.T, first, second string) {
    57  		assertPartsUnescape(t, first+"/"+second, first, second)
    58  	})
    59  }