2018-10-25 21:28:16 +01:00
|
|
|
// 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) {
|
2018-11-13 12:21:52 +00:00
|
|
|
forAllCiphers(func(cipher storj.Cipher) {
|
|
|
|
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("%d. %+v", i, path)
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
key := new(storj.Key)
|
|
|
|
copy(key[:], randData(storj.KeySize))
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
encrypted, err := EncryptPath(path, cipher, key)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
decrypted, err := DecryptPath(encrypted, cipher, key)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
assert.Equal(t, path, decrypted, errTag)
|
|
|
|
}
|
|
|
|
})
|
2018-10-25 21:28:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeriveKey(t *testing.T) {
|
2018-11-13 12:21:52 +00:00
|
|
|
forAllCiphers(func(cipher storj.Cipher) {
|
|
|
|
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("%d. %+v", i, tt)
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
key := new(storj.Key)
|
|
|
|
copy(key[:], randData(storj.KeySize))
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
encrypted, err := EncryptPath(tt.path, cipher, key)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
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
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
shared := storj.JoinPaths(storj.SplitPath(encrypted)[tt.depth:]...)
|
|
|
|
decrypted, err := DecryptPath(shared, cipher, derivedKey)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := storj.JoinPaths(storj.SplitPath(tt.path)[tt.depth:]...)
|
|
|
|
assert.Equal(t, expected, decrypted, errTag)
|
2018-10-25 21:28:16 +01:00
|
|
|
}
|
2018-11-13 12:21:52 +00:00
|
|
|
})
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2018-11-13 12:21:52 +00:00
|
|
|
func forAllCiphers(test func(cipher storj.Cipher)) {
|
|
|
|
for _, cipher := range []storj.Cipher{
|
|
|
|
storj.Unencrypted,
|
|
|
|
storj.AESGCM,
|
|
|
|
storj.SecretBox,
|
|
|
|
} {
|
|
|
|
test(cipher)
|
2018-10-25 21:28:16 +01:00
|
|
|
}
|
|
|
|
}
|