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