github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/api/renter_test.go (about)

     1  package api
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/url"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/NebulousLabs/Sia/build"
    10  	"github.com/NebulousLabs/Sia/crypto"
    11  	"github.com/NebulousLabs/Sia/modules/renter"
    12  )
    13  
    14  // createRandFile creates a file on disk and fills it with random bytes.
    15  func createRandFile(path string, size int) error {
    16  	data, err := crypto.RandBytes(size)
    17  	if err != nil {
    18  		return err
    19  	}
    20  	return ioutil.WriteFile(path, data, 0600)
    21  }
    22  
    23  // TestRenterPaths tests that the /renter routes handle path parameters
    24  // properly.
    25  func TestRenterPaths(t *testing.T) {
    26  	st, err := createServerTester("TestRenterPaths")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer st.server.Close()
    31  
    32  	// Announce the host.
    33  	err = st.announceHost()
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	// Create a file.
    39  	path := filepath.Join(build.SiaTestingDir, "api", "TestRenterPaths", "test.dat")
    40  	err = createRandFile(path, 1024)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	// Upload to host.
    46  	uploadValues := url.Values{}
    47  	uploadValues.Set("source", path)
    48  	uploadValues.Set("renew", "true")
    49  	err = st.stdPostAPI("/renter/upload/foo/bar/test", uploadValues)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	// File should be listed by the renter.
    55  	var rf RenterFiles
    56  	err = st.getAPI("/renter/files", &rf)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if len(rf.Files) != 1 || rf.Files[0].SiaPath != "foo/bar/test" {
    61  		t.Fatal("/renter/files did not return correct file:", rf)
    62  	}
    63  }
    64  
    65  // TestRenterConflicts tests that the renter handles naming conflicts properly.
    66  func TestRenterConflicts(t *testing.T) {
    67  	if testing.Short() {
    68  		t.SkipNow()
    69  	}
    70  	st, err := createServerTester("TestRenterConflicts")
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	defer st.server.Close()
    75  
    76  	// Announce the host.
    77  	err = st.announceHost()
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	// Create a file.
    83  	path := filepath.Join(build.SiaTestingDir, "api", "TestRenterConflicts", "test.dat")
    84  	err = createRandFile(path, 1024)
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	// Upload to host, using a path designed to cause conflicts. The renter
    90  	// should automatically create a folder called foo/bar.sia. Later, we'll
    91  	// exploit this by uploading a file called foo/bar.
    92  	uploadValues := url.Values{}
    93  	uploadValues.Set("source", path)
    94  	uploadValues.Set("renew", "true")
    95  	err = st.stdPostAPI("/renter/upload/foo/bar.sia/test", uploadValues)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	// File should be listed by the renter.
   101  	var rf RenterFiles
   102  	err = st.getAPI("/renter/files", &rf)
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	if len(rf.Files) != 1 || rf.Files[0].SiaPath != "foo/bar.sia/test" {
   107  		t.Fatal("/renter/files did not return correct file:", rf)
   108  	}
   109  
   110  	// Upload using the same nickname.
   111  	err = st.stdPostAPI("/renter/upload/foo/bar.sia/test", uploadValues)
   112  	if err == renter.ErrPathOverload {
   113  		t.Fatalf("expected %v, got %v", renter.ErrPathOverload, err)
   114  	}
   115  
   116  	// Upload using nickname that conflicts with folder.
   117  	err = st.stdPostAPI("/renter/upload/foo/bar", uploadValues)
   118  	if err == nil {
   119  		t.Fatal("expecting conflict error, got nil")
   120  	}
   121  }