storj/pkg/paths/paths_test.go
nfarah86 e228d090e8
Mutex/nsclient- WIP (#104)
* working on put request for nsclient

* working on put request for nsclient

* netstate put

* netstate put

* wip testing client

* wip - testing client
 and working through some errors

* wip - testing client
 and working through some errors

* put request works

* put request works for client

* get request working

* get request working

* get request working-minor edit

* get request working-minor edit

* list request works

* list request works

* working through delete error

* working through delete error

* fixed exp client, still working through delete error

* fixed exp client, still working through delete error

* delete works; fixed formatting issues

* delete works; fixed formatting issues

* deleted comment

* deleted comment

* resolving merge conflicts

* resolving merge conflict

* fixing merge conflict

* implemented and modified kayloyans paths file

* working on testing

* added test for path_test.go

* fixed string, read through netstate test

* deleted env variables

* initial commit for mocking out grpc client- got it working

* mocked grpc client

* mock put passed test

* 2 tests pass for PUT with mock

* put requests test pass, wip- want mini review

* get tests pass mock

* list test working

* initial commit for list test

* all list req. working, starting on delete tests

* delete tests passed

* cleaned up tests

* resolved merge conflicts

* resolved merge conflicts

* fixed linter errors

* fixed error found in travis

* initial commit for fixes from PR comments

* fixed pr comments and linting

* added error handling for api creds, and rebased

* fixes from dennis comments

* fixed pr with dennis suggestioon

* added copyrights to files

* fixed casing per dennis great comment

* fixed travis complaint on sprintf
2018-07-19 15:57:22 -07:00

227 lines
6.5 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package paths
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
for i, tt := range []struct {
path string
expected Path
}{
{"", []string{""}},
{"/", []string{""}},
{"a", []string{"a"}},
{"/a/", []string{"a"}},
{"a/b/c/d", []string{"a", "b", "c", "d"}},
{"///a//b////c/d///", []string{"a", "b", "c", "d"}},
} {
errTag := fmt.Sprintf("Test case #%d", i)
p := New(tt.path)
assert.Equal(t, tt.expected, p, errTag)
}
}
func TestNewWithSegments(t *testing.T) {
for i, tt := range []struct {
segs []string
expected Path
}{
{[]string{""}, []string{""}},
{[]string{"", ""}, []string{""}},
{[]string{"/"}, []string{""}},
{[]string{"a"}, []string{"a"}},
{[]string{"/a/"}, []string{"a"}},
{[]string{"", "a", "", "b", "c", "d", ""}, []string{"a", "b", "c", "d"}},
{[]string{"a", "b", "c", "d"}, []string{"a", "b", "c", "d"}},
{[]string{"/a", "b/", "/c/", "d"}, []string{"a", "b", "c", "d"}},
{[]string{"a/b", "c", "d"}, []string{"a", "b", "c", "d"}},
{[]string{"a/b", "c/d"}, []string{"a", "b", "c", "d"}},
{[]string{"//a/b", "c///d//"}, []string{"a", "b", "c", "d"}},
{[]string{"a/b/c/d"}, []string{"a", "b", "c", "d"}},
} {
errTag := fmt.Sprintf("Test case #%d", i)
p := New(tt.segs...)
assert.Equal(t, tt.expected, p, errTag)
}
}
func TestString(t *testing.T) {
for i, tt := range []struct {
path Path
expected string
}{
{[]string{}, ""},
{[]string{""}, ""},
{[]string{"a"}, "a"},
{[]string{"a", "b"}, "a/b"},
{[]string{"a", "b", "c", "d"}, "a/b/c/d"},
} {
errTag := fmt.Sprintf("Test case #%d", i)
s := tt.path.String()
assert.Equal(t, tt.expected, s, errTag)
}
}
func TestBytes(t *testing.T) {
for i, tt := range []struct {
path Path
expected []byte
}{
{[]string{""}, []byte{}},
{[]string{"a/b"}, []byte{97, 47, 98}},
{[]string{"a/b/c"}, []byte{97, 47, 98, 47, 99}},
{[]string{"a/b/c/d/e/f"}, []byte{97, 47, 98, 47, 99, 47, 100, 47, 101, 47, 102}},
}{
errTag := fmt.Sprintf("Test case #%d", i)
b := tt.path.Bytes()
assert.Equal(t, tt.expected, b, errTag)
}
}
func TestPrepend(t *testing.T) {
for i, tt := range []struct {
prefix string
path string
expected Path
}{
{"", "", []string{""}},
{"prefix", "", []string{"prefix"}},
{"", "my/path", []string{"my", "path"}},
{"prefix", "my/path", []string{"prefix", "my", "path"}},
{"p1/p2/p3", "my/path", []string{"p1", "p2", "p3", "my", "path"}},
}{
errTag := fmt.Sprintf("Test case #%d", i)
p := New(tt.path).Prepend(tt.prefix)
assert.Equal(t, tt.expected, p, errTag)
}
}
func TestPrependWithSegments(t *testing.T) {
for i, tt := range []struct {
segs []string
path string
expected Path
}{
{[]string{""}, "", []string{""}},
{[]string{"prefix"}, "", []string{"prefix"}},
{[]string{""}, "my/path", []string{"my", "path"}},
{[]string{"prefix"}, "my/path", []string{"prefix", "my", "path"}},
{[]string{"p1/p2/p3"}, "my/path", []string{"p1", "p2", "p3", "my", "path"}},
{[]string{"p1", "p2/p3"}, "my/path", []string{"p1", "p2", "p3", "my", "path"}},
{[]string{"p1", "p2", "p3"}, "my/path", []string{"p1", "p2", "p3", "my", "path"}},
{[]string{"", "p1", "", "", "p2", "p3", ""}, "my/path", []string{"p1", "p2", "p3", "my", "path"}},
} {
errTag := fmt.Sprintf("Test case #%d", i)
p := New(tt.path).Prepend(tt.segs...)
assert.Equal(t, tt.expected, p, errTag)
}
}
func TestAppend(t *testing.T) {
for i, tt := range []struct {
path string
suffix string
expected Path
}{
{"", "", []string{""}},
{"", "suffix", []string{"suffix"}},
{"my/path", "", []string{"my", "path"}},
{"my/path", "suffix", []string{"my", "path", "suffix"}},
{"my/path", "s1/s2/s3", []string{"my", "path", "s1", "s2", "s3"}},
} {
errTag := fmt.Sprintf("Test case #%d", i)
p := New(tt.path).Append(tt.suffix)
assert.Equal(t, tt.expected, p, errTag)
}
}
func TestAppendWithSegments(t *testing.T) {
for i, tt := range []struct {
path string
segs []string
expected Path
}{
{"", []string{""}, []string{""}},
{"", []string{"suffix"}, []string{"suffix"}},
{"my/path", []string{""}, []string{"my", "path"}},
{"my/path", []string{"suffix"}, []string{"my", "path", "suffix"}},
{"my/path", []string{"s1/s2/s3"}, []string{"my", "path", "s1", "s2", "s3"}},
{"my/path", []string{"s1", "s2/s3"}, []string{"my", "path", "s1", "s2", "s3"}},
{"my/path", []string{"s1", "s2", "s3"}, []string{"my", "path", "s1", "s2", "s3"}},
{"my/path", []string{"", "s1", "", "", "s2", "s3", ""}, []string{"my", "path", "s1", "s2", "s3"}},
} {
errTag := fmt.Sprintf("Test case #%d", i)
p := New(tt.path).Append(tt.segs...)
assert.Equal(t, tt.expected, p, errTag)
}
}
func TestEncryption(t *testing.T) {
for i, path := range []Path{
[]string{}, // empty path
[]string{""}, // empty path segment
[]string{"file.txt"},
[]string{"fold1", "file.txt"},
[]string{"fold1", "fold2", "file.txt"},
[]string{"fold1", "fold2", "fold3", "file.txt"},
} {
errTag := fmt.Sprintf("Test case #%d", i)
key := []byte("my secret")
encrypted, err := path.Encrypt(key)
if !assert.NoError(t, err, errTag) {
continue
}
decrypted, err := encrypted.Decrypt(key)
if !assert.NoError(t, err, errTag) {
continue
}
assert.Equal(t, path, decrypted, errTag)
}
}
func TestDeriveKey(t *testing.T) {
for i, tt := range []struct {
path Path
depth int
errString string
}{
{[]string{"fold1", "fold2", "fold3", "file.txt"}, -1,
"paths error: negative depth"},
{[]string{"fold1", "fold2", "fold3", "file.txt"}, 0, ""},
{[]string{"fold1", "fold2", "fold3", "file.txt"}, 1, ""},
{[]string{"fold1", "fold2", "fold3", "file.txt"}, 2, ""},
{[]string{"fold1", "fold2", "fold3", "file.txt"}, 3, ""},
{[]string{"fold1", "fold2", "fold3", "file.txt"}, 4, ""},
{[]string{"fold1", "fold2", "fold3", "file.txt"}, 5,
"paths error: depth greater than path length"},
} {
errTag := fmt.Sprintf("Test case #%d", i)
key := []byte("my secret")
encrypted, err := tt.path.Encrypt(key)
if !assert.NoError(t, err, errTag) {
continue
}
derivedKey, err := tt.path.DeriveKey(key, tt.depth)
if tt.errString != "" {
assert.EqualError(t, err, tt.errString, errTag)
continue
}
if !assert.NoError(t, err, errTag) {
continue
}
shared := encrypted[tt.depth:]
decrypted, err := shared.Decrypt(derivedKey)
if !assert.NoError(t, err, errTag) {
continue
}
assert.Equal(t, tt.path[tt.depth:], decrypted, errTag)
}
}