2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-25 21:28:16 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package encryption
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
"storj.io/storj/internal/testrand"
|
2018-10-25 21:28:16 +01:00
|
|
|
"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
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
key := testrand.Key()
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
encrypted, err := EncryptPath(path, cipher, &key)
|
2018-11-13 12:21:52 +00:00
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
decrypted, err := DecryptPath(encrypted, cipher, &key)
|
2018-11-13 12:21:52 +00:00
|
|
|
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
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
key := testrand.Key()
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
encrypted, err := EncryptPath(tt.path, cipher, &key)
|
2018-11-13 12:21:52 +00:00
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
derivedKey, err := DerivePathKey(tt.path, &key, tt.depth)
|
2018-11-13 12:21:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|