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"
|
2019-07-05 09:36:35 +01:00
|
|
|
"storj.io/storj/pkg/paths"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
2019-07-05 09:36:35 +01:00
|
|
|
func newStore(key storj.Key) *Store {
|
|
|
|
store := NewStore()
|
|
|
|
if err := store.Add("bucket", paths.Unencrypted{}, paths.Encrypted{}, key); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreEncryption(t *testing.T) {
|
2019-07-03 19:07:44 +01:00
|
|
|
forAllCiphers(func(cipher storj.CipherSuite) {
|
2019-07-05 09:36:35 +01:00
|
|
|
for i, rawPath := range []string{
|
2018-11-13 12:21:52 +00:00
|
|
|
"",
|
|
|
|
"/",
|
|
|
|
"//",
|
|
|
|
"file.txt",
|
|
|
|
"file.txt/",
|
|
|
|
"fold1/file.txt",
|
|
|
|
"fold1/fold2/file.txt",
|
|
|
|
"/fold1/fold2/fold3/file.txt",
|
|
|
|
} {
|
2019-07-05 09:36:35 +01:00
|
|
|
errTag := fmt.Sprintf("test:%d path:%q cipher:%v", i, rawPath, cipher)
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2019-07-05 09:36:35 +01:00
|
|
|
store := newStore(testrand.Key())
|
|
|
|
path := paths.NewUnencrypted(rawPath)
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2019-07-05 09:36:35 +01:00
|
|
|
encPath, err := EncryptPath("bucket", path, cipher, store)
|
2018-11-13 12:21:52 +00:00
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
|
2019-07-05 09:36:35 +01:00
|
|
|
decPath, err := DecryptPath("bucket", encPath, cipher, store)
|
2018-11-13 12:21:52 +00:00
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-05 09:36:35 +01:00
|
|
|
assert.Equal(t, rawPath, decPath.Raw(), 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
|
|
|
|
2019-07-03 19:07:44 +01:00
|
|
|
func forAllCiphers(test func(cipher storj.CipherSuite)) {
|
|
|
|
for _, cipher := range []storj.CipherSuite{
|
|
|
|
storj.EncNull,
|
|
|
|
storj.EncAESGCM,
|
|
|
|
storj.EncSecretBox,
|
2018-11-13 12:21:52 +00:00
|
|
|
} {
|
|
|
|
test(cipher)
|
2018-10-25 21:28:16 +01:00
|
|
|
}
|
|
|
|
}
|