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