9 "github.com/anacrolix/torrent/metainfo"
10 qt "github.com/frankban/quicktest"
14 log.SetFlags(log.Flags() | log.Lshortfile)
17 // I think these are mainly tests for bad metainfos that try to escape the client base directory.
18 var safeFilePathTests = []struct {
22 // We might want a test for invalid chars inside components, or file maker opt funcs returning
23 // absolute paths (and thus presumably clobbering earlier "makers").
24 {input: []string{"a", filepath.FromSlash(`b/..`)}, expectErr: false},
25 {input: []string{"a", filepath.FromSlash(`b/../../..`)}, expectErr: true},
26 {input: []string{"a", filepath.FromSlash(`b/../.././..`)}, expectErr: true},
29 filepath.FromSlash(`NewSuperHeroMovie-2019-English-720p.avi /../../../../../Roaming/Microsoft/Windows/Start Menu/Programs/Startup/test3.exe`),
35 // Tests the ToSafeFilePath func.
36 func TestToSafeFilePath(t *testing.T) {
37 for _, _case := range safeFilePathTests {
38 actual, err := ToSafeFilePath(_case.input...)
43 t.Errorf("%q: expected error, got output %q", _case.input, actual)
48 // Check that safe file path handling still exists for the newer file-opt-maker variants.
49 func TestFileOptsSafeFilePathHandling(t *testing.T) {
51 for i, _case := range safeFilePathTests {
52 c.Run(fmt.Sprintf("Case%v", i), func(c *qt.C) {
53 info := metainfo.Info{
54 Files: []metainfo.FileInfo{
58 client := NewFileOpts(NewFileClientOpts{
59 ClientBaseDir: t.TempDir(),
61 defer func() { c.Check(client.Close(), qt.IsNil) }()
62 torImpl, err := client.OpenTorrent(&info, metainfo.Hash{})
64 c.Check(err, qt.Not(qt.IsNil))
66 c.Check(torImpl.Close(), qt.IsNil)