gitlab.com/jokerrs1/Sia@v1.3.2/modules/renter/upload_test.go (about)

     1  package renter
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/NebulousLabs/Sia/modules"
     9  )
    10  
    11  // TestRenterSiapathValidate verifies that the validateSiapath function correctly validates SiaPaths.
    12  func TestRenterSiapathValidate(t *testing.T) {
    13  	var pathtests = []struct {
    14  		in    string
    15  		valid bool
    16  	}{
    17  		{"valid/siapath", true},
    18  		{"../../../directory/traversal", false},
    19  		{"testpath", true},
    20  		{"valid/siapath/../with/directory/traversal", false},
    21  		{"validpath/test", true},
    22  		{"..validpath/..test", true},
    23  		{"./invalid/path", false},
    24  		{"test/path", true},
    25  		{"/leading/slash", false},
    26  		{"foo/./bar", false},
    27  		{"", false},
    28  	}
    29  	for _, pathtest := range pathtests {
    30  		err := validateSiapath(pathtest.in)
    31  		if err != nil && pathtest.valid {
    32  			t.Fatal("validateSiapath failed on valid path: ", pathtest.in)
    33  		}
    34  		if err == nil && !pathtest.valid {
    35  			t.Fatal("validateSiapath succeeded on invalid path: ", pathtest.in)
    36  		}
    37  	}
    38  }
    39  
    40  // TestRenterUploadDirectory verifies that the renter returns an error if a
    41  // directory is provided as the source of an upload.
    42  func TestRenterUploadInode(t *testing.T) {
    43  	if testing.Short() {
    44  		t.SkipNow()
    45  	}
    46  	rt, err := newRenterTester(t.Name())
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	defer rt.Close()
    51  
    52  	testUploadPath, err := ioutil.TempDir("", t.Name())
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	defer os.RemoveAll(testUploadPath)
    57  
    58  	ec, err := NewRSCode(defaultDataPieces, defaultParityPieces)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	params := modules.FileUploadParams{
    63  		Source:      testUploadPath,
    64  		SiaPath:     "test",
    65  		ErasureCode: ec,
    66  	}
    67  	err = rt.renter.Upload(params)
    68  	if err == nil {
    69  		t.Fatal("expected Upload to fail with empty directory as source")
    70  	}
    71  	if err != errUploadDirectory {
    72  		t.Fatal("expected errUploadDirectory, got", err)
    73  	}
    74  }