99640225fd
The old paths.Path type is now replaced with the new storj.Path. storj.Path is simply an alias to the built-in string type. As such it can be used just as any string, which simplifies a lot working with paths. No more conversions paths.New and path.String(). As an alias storj.Path does not define any methods. However, any functions applying to strings (like those from the strings package) gracefully apply to storj.Path too. In addition we have a few more functions defined: storj.SplitPath storj.JoinPaths encryption.EncryptPath encryption.DecryptPath encryption.DerivePathKey encryption.DeriveContentKey All code in master is migrated to the new storj.Path type. The Path example is also updated and is good for reference: /pkg/encryption/examples_test.go This PR also resolve a nonce misuse issue in path encryption: https://storjlabs.atlassian.net/browse/V3-545
88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package encryption
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
)
|
|
|
|
func TestEncryption(t *testing.T) {
|
|
for i, path := range []storj.Path{
|
|
"",
|
|
"/",
|
|
"//",
|
|
"file.txt",
|
|
"file.txt/",
|
|
"fold1/file.txt",
|
|
"fold1/fold2/file.txt",
|
|
"/fold1/fold2/fold3/file.txt",
|
|
} {
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
key := new(storj.Key)
|
|
copy(key[:], randData(storj.KeySize))
|
|
|
|
encrypted, err := EncryptPath(path, key)
|
|
if !assert.NoError(t, err, errTag) {
|
|
continue
|
|
}
|
|
|
|
decrypted, err := DecryptPath(encrypted, 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 storj.Path
|
|
depth int
|
|
errString string
|
|
}{
|
|
{"fold1/fold2/fold3/file.txt", -1, "encryption error: negative depth"},
|
|
{"fold1/fold2/fold3/file.txt", 0, ""},
|
|
{"fold1/fold2/fold3/file.txt", 1, ""},
|
|
{"fold1/fold2/fold3/file.txt", 2, ""},
|
|
{"fold1/fold2/fold3/file.txt", 3, ""},
|
|
{"fold1/fold2/fold3/file.txt", 4, ""},
|
|
{"fold1/fold2/fold3/file.txt", 5, "encryption error: depth greater than path length"},
|
|
} {
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
key := new(storj.Key)
|
|
copy(key[:], randData(storj.KeySize))
|
|
|
|
encrypted, err := EncryptPath(tt.path, key)
|
|
if !assert.NoError(t, err, errTag) {
|
|
continue
|
|
}
|
|
|
|
derivedKey, err := DerivePathKey(tt.path, key, tt.depth)
|
|
if tt.errString != "" {
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
continue
|
|
}
|
|
if !assert.NoError(t, err, errTag) {
|
|
continue
|
|
}
|
|
|
|
shared := storj.JoinPaths(storj.SplitPath(encrypted)[tt.depth:]...)
|
|
decrypted, err := DecryptPath(shared, derivedKey)
|
|
if !assert.NoError(t, err, errTag) {
|
|
continue
|
|
}
|
|
|
|
expected := storj.JoinPaths(storj.SplitPath(tt.path)[tt.depth:]...)
|
|
assert.Equal(t, expected, decrypted, errTag)
|
|
}
|
|
}
|