gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/siapath_test.go (about)

     1  package modules
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gitlab.com/NebulousLabs/errors"
     7  )
     8  
     9  // TestSiapathValidate verifies that the validate function correctly validates
    10  // SiaPaths.
    11  func TestSiapathValidate(t *testing.T) {
    12  	var pathtests = []struct {
    13  		in    string
    14  		valid bool
    15  	}{
    16  		{"valid/siapath", true},
    17  		{"../../../directory/traversal", false},
    18  		{"testpath", true},
    19  		{"valid/siapath/../with/directory/traversal", false},
    20  		{"validpath/test", true},
    21  		{"..validpath/..test", true},
    22  		{"./invalid/path", false},
    23  		{".../path", true},
    24  		{"valid./path", true},
    25  		{"valid../path", true},
    26  		{"valid/path./test", true},
    27  		{"valid/path../test", true},
    28  		{"test/path", true},
    29  		{"/leading/slash", false},
    30  		{"foo/./bar", false},
    31  		{"", false},
    32  		{"blank/end/", false},
    33  		{"double//dash", false},
    34  		{"../", false},
    35  		{"./", false},
    36  		{".", false},
    37  	}
    38  	for _, pathtest := range pathtests {
    39  		siaPath := SiaPath{
    40  			Path: pathtest.in,
    41  		}
    42  		err := siaPath.Validate(false)
    43  		if err != nil && pathtest.valid {
    44  			t.Fatal("validateSiapath failed on valid path: ", pathtest.in)
    45  		}
    46  		if err == nil && !pathtest.valid {
    47  			t.Fatal("validateSiapath succeeded on invalid path: ", pathtest.in)
    48  		}
    49  	}
    50  }
    51  
    52  // TestSiapath tests that the NewSiaPath, LoadString, and Join methods function correctly
    53  func TestSiapath(t *testing.T) {
    54  	var pathtests = []struct {
    55  		in    string
    56  		valid bool
    57  	}{
    58  		{"valid/siapath", true},
    59  		{"\\some\\windows\\path", true}, // clean converts OS separators
    60  		{"../../../directory/traversal", false},
    61  		{"testpath", true},
    62  		{"valid/siapath/../with/directory/traversal", false},
    63  		{"validpath/test", true},
    64  		{"..validpath/..test", true},
    65  		{"./invalid/path", false},
    66  		{".../path", true},
    67  		{"valid./path", true},
    68  		{"valid../path", true},
    69  		{"valid/path./test", true},
    70  		{"valid/path../test", true},
    71  		{"test/path", true},
    72  		{"/leading/slash", true}, // clean will trim leading slashes so this is a valid input
    73  		{"foo/./bar", false},
    74  		{"", false},
    75  		{"blank/end/", true}, // clean will trim trailing slashes so this is a valid input
    76  		{"double//dash", false},
    77  		{"../", false},
    78  		{"./", false},
    79  		{".", false},
    80  		{"dollar$sign", true},
    81  		{"and&sign", true},
    82  		{"single`quote", true},
    83  		{"full:colon", true},
    84  		{"semi;colon", true},
    85  		{"hash#tag", true},
    86  		{"percent%sign", true},
    87  		{"at@sign", true},
    88  		{"less<than", true},
    89  		{"greater>than", true},
    90  		{"equal=to", true},
    91  		{"question?mark", true},
    92  		{"open[bracket", true},
    93  		{"close]bracket", true},
    94  		{"open{bracket", true},
    95  		{"close}bracket", true},
    96  		{"carrot^top", true},
    97  		{"pipe|pipe", true},
    98  		{"tilda~tilda", true},
    99  		{"plus+sign", true},
   100  		{"minus-sign", true},
   101  		{"under_score", true},
   102  		{"comma,comma", true},
   103  		{"apostrophy's", true},
   104  		{`quotation"marks`, true},
   105  	}
   106  
   107  	// Test NewSiaPath
   108  	for _, pathtest := range pathtests {
   109  		_, err := NewSiaPath(pathtest.in)
   110  		// Verify expected Error
   111  		if err != nil && pathtest.valid {
   112  			t.Fatal("validateSiapath failed on valid path: ", pathtest.in)
   113  		}
   114  		if err == nil && !pathtest.valid {
   115  			t.Fatal("validateSiapath succeeded on invalid path: ", pathtest.in)
   116  		}
   117  	}
   118  
   119  	// Test LoadString
   120  	var sp SiaPath
   121  	for _, pathtest := range pathtests {
   122  		err := sp.LoadString(pathtest.in)
   123  		// Verify expected Error
   124  		if err != nil && pathtest.valid {
   125  			t.Fatal("validateSiapath failed on valid path: ", pathtest.in)
   126  		}
   127  		if err == nil && !pathtest.valid {
   128  			t.Fatal("validateSiapath succeeded on invalid path: ", pathtest.in)
   129  		}
   130  	}
   131  
   132  	// Test Join
   133  	sp, err := NewSiaPath("test")
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  	for _, pathtest := range pathtests {
   138  		_, err = sp.Join(pathtest.in)
   139  		// Verify expected Error
   140  		if err != nil && pathtest.valid {
   141  			t.Fatal("validateSiapath failed on valid path: ", pathtest.in)
   142  		}
   143  		if err == nil && !pathtest.valid {
   144  			t.Fatal("validateSiapath succeeded on invalid path: ", pathtest.in)
   145  		}
   146  	}
   147  }
   148  
   149  // TestSiapathRebase tests the SiaPath.Rebase method.
   150  func TestSiapathRebase(t *testing.T) {
   151  	var rebasetests = []struct {
   152  		oldBase string
   153  		newBase string
   154  		siaPath string
   155  		result  string
   156  	}{
   157  		{"a/b", "a", "a/b/myfile", "a/myfile"}, // basic rebase
   158  		{"a/b", "", "a/b/myfile", "myfile"},    // newBase is root
   159  		{"", "b", "myfile", "b/myfile"},        // oldBase is root
   160  		{"a/a", "a/b", "a/a", "a/b"},           // folder == oldBase
   161  	}
   162  
   163  	for _, test := range rebasetests {
   164  		var oldBase, newBase SiaPath
   165  		var err1, err2 error
   166  		if test.oldBase == "" {
   167  			oldBase = RootSiaPath()
   168  		} else {
   169  			oldBase, err1 = newSiaPath(test.oldBase)
   170  		}
   171  		if test.newBase == "" {
   172  			newBase = RootSiaPath()
   173  		} else {
   174  			newBase, err2 = newSiaPath(test.newBase)
   175  		}
   176  		file, err3 := newSiaPath(test.siaPath)
   177  		expectedPath, err4 := newSiaPath(test.result)
   178  		if err := errors.Compose(err1, err2, err3, err4); err != nil {
   179  			t.Fatal(err)
   180  		}
   181  		// Rebase the path
   182  		res, err := file.Rebase(oldBase, newBase)
   183  		if err != nil {
   184  			t.Fatal(err)
   185  		}
   186  		// Check result.
   187  		if !res.Equals(expectedPath) {
   188  			t.Fatalf("'%v' doesn't match '%v'", res.String(), expectedPath.String())
   189  		}
   190  	}
   191  }