]> Sergey Matveev's repositories - btrtrc.git/blobdiff - storage/safe-path_test.go
Add "no name" handling and storage.NewFileOpts
[btrtrc.git] / storage / safe-path_test.go
index bf7e7ba0f1448f3dc4fc630abb37d0f7ad183750..e12d3333cdb5d626d89934d50aa01b88eb09a888 100644 (file)
@@ -1,28 +1,38 @@
 package storage
 
 import (
+       "fmt"
        "log"
        "path/filepath"
        "testing"
+
+       "github.com/anacrolix/torrent/metainfo"
+       qt "github.com/frankban/quicktest"
 )
 
 func init() {
        log.SetFlags(log.Flags() | log.Lshortfile)
 }
 
-func TestSafePath(t *testing.T) {
-       for _, _case := range []struct {
-               input     []string
-               expected  string
-               expectErr bool
-       }{
-               {input: []string{"a", filepath.FromSlash(`b/../../..`)}, expectErr: true},
-               {input: []string{"a", filepath.FromSlash(`b/../.././..`)}, expectErr: true},
-               {input: []string{
-                       filepath.FromSlash(`NewSuperHeroMovie-2019-English-720p.avi /../../../../../Roaming/Microsoft/Windows/Start Menu/Programs/Startup/test3.exe`)},
-                       expectErr: true,
-               },
-       } {
+// I think these are mainly tests for bad metainfos that try to escape the client base directory.
+var safeFilePathTests = []struct {
+       input     []string
+       expectErr bool
+}{
+       // We might want a test for invalid chars inside components, or file maker opt funcs returning
+       // absolute paths (and thus presumably clobbering earlier "makers").
+       {input: []string{"a", filepath.FromSlash(`b/..`)}, expectErr: false},
+       {input: []string{"a", filepath.FromSlash(`b/../../..`)}, expectErr: true},
+       {input: []string{"a", filepath.FromSlash(`b/../.././..`)}, expectErr: true},
+       {input: []string{
+               filepath.FromSlash(`NewSuperHeroMovie-2019-English-720p.avi /../../../../../Roaming/Microsoft/Windows/Start Menu/Programs/Startup/test3.exe`)},
+               expectErr: true,
+       },
+}
+
+// Tests the ToSafeFilePath func.
+func TestToSafeFilePath(t *testing.T) {
+       for _, _case := range safeFilePathTests {
                actual, err := ToSafeFilePath(_case.input...)
                if _case.expectErr {
                        if err != nil {
@@ -32,3 +42,27 @@ func TestSafePath(t *testing.T) {
                }
        }
 }
+
+// Check that safe file path handling still exists for the newer file-opt-maker variants.
+func TestFileOptsSafeFilePathHandling(t *testing.T) {
+       c := qt.New(t)
+       for i, _case := range safeFilePathTests {
+               c.Run(fmt.Sprintf("Case%v", i), func(c *qt.C) {
+                       info := metainfo.Info{
+                               Files: []metainfo.FileInfo{
+                                       {Path: _case.input},
+                               },
+                       }
+                       client := NewFileOpts(NewFileClientOpts{
+                               ClientBaseDir: "somedir",
+                       })
+                       defer func() { c.Check(client.Close(), qt.IsNil) }()
+                       torImpl, err := client.OpenTorrent(&info, metainfo.Hash{})
+                       if _case.expectErr {
+                               c.Check(err, qt.Not(qt.IsNil))
+                       } else {
+                               c.Check(torImpl.Close(), qt.IsNil)
+                       }
+               })
+       }
+}