]> Sergey Matveev's repositories - btrtrc.git/blob - storage/safe-path_test.go
674da8fc023b42d06655b57d5fb00123ba17e74b
[btrtrc.git] / storage / safe-path_test.go
1 package storage
2
3 import (
4         "fmt"
5         "log"
6         "path/filepath"
7         "testing"
8
9         "github.com/anacrolix/torrent/metainfo"
10         qt "github.com/frankban/quicktest"
11 )
12
13 func init() {
14         log.SetFlags(log.Flags() | log.Lshortfile)
15 }
16
17 // I think these are mainly tests for bad metainfos that try to escape the client base directory.
18 var safeFilePathTests = []struct {
19         input     []string
20         expectErr bool
21 }{
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},
27         {
28                 input: []string{
29                         filepath.FromSlash(`NewSuperHeroMovie-2019-English-720p.avi /../../../../../Roaming/Microsoft/Windows/Start Menu/Programs/Startup/test3.exe`),
30                 },
31                 expectErr: true,
32         },
33 }
34
35 // Tests the ToSafeFilePath func.
36 func TestToSafeFilePath(t *testing.T) {
37         for _, _case := range safeFilePathTests {
38                 actual, err := ToSafeFilePath(_case.input...)
39                 if _case.expectErr {
40                         if err != nil {
41                                 continue
42                         }
43                         t.Errorf("%q: expected error, got output %q", _case.input, actual)
44                 }
45         }
46 }
47
48 // Check that safe file path handling still exists for the newer file-opt-maker variants.
49 func TestFileOptsSafeFilePathHandling(t *testing.T) {
50         c := qt.New(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{
55                                         {Path: _case.input},
56                                 },
57                         }
58                         client := NewFileOpts(NewFileClientOpts{
59                                 ClientBaseDir: t.TempDir(),
60                         })
61                         defer func() { c.Check(client.Close(), qt.IsNil) }()
62                         torImpl, err := client.OpenTorrent(&info, metainfo.Hash{})
63                         if _case.expectErr {
64                                 c.Check(err, qt.Not(qt.IsNil))
65                         } else {
66                                 c.Check(torImpl.Close(), qt.IsNil)
67                         }
68                 })
69         }
70 }