2019-03-21 13:47:48 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package encryption_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/memory"
|
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-06-26 11:38:51 +01:00
|
|
|
"storj.io/storj/internal/testrand"
|
2019-06-25 10:46:29 +01:00
|
|
|
"storj.io/storj/pkg/encryption"
|
|
|
|
"storj.io/storj/pkg/storj"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/uplink/eestream"
|
2019-03-21 13:47:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
uint32Size = 4
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCalcEncryptedSize(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-07-03 19:07:44 +01:00
|
|
|
forAllCiphers(func(cipher storj.CipherSuite) {
|
2019-03-21 13:47:48 +00:00
|
|
|
for i, dataSize := range []int64{
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
1*memory.KiB.Int64() - uint32Size,
|
|
|
|
1 * memory.KiB.Int64(),
|
|
|
|
32*memory.KiB.Int64() - uint32Size,
|
|
|
|
32 * memory.KiB.Int64(),
|
|
|
|
32*memory.KiB.Int64() + 100,
|
|
|
|
} {
|
|
|
|
errTag := fmt.Sprintf("%d-%d. %+v", cipher, i, dataSize)
|
|
|
|
|
2019-07-03 19:07:44 +01:00
|
|
|
parameters := storj.EncryptionParameters{CipherSuite: cipher, BlockSize: 1 * memory.KiB.Int32()}
|
2019-03-21 13:47:48 +00:00
|
|
|
|
2019-07-03 19:07:44 +01:00
|
|
|
calculatedSize, err := encryption.CalcEncryptedSize(dataSize, parameters)
|
2019-03-21 13:47:48 +00:00
|
|
|
require.NoError(t, err, errTag)
|
|
|
|
|
2019-07-03 19:07:44 +01:00
|
|
|
encrypter, err := encryption.NewEncrypter(parameters.CipherSuite, new(storj.Key), new(storj.Nonce), int(parameters.BlockSize))
|
2019-03-21 13:47:48 +00:00
|
|
|
require.NoError(t, err, errTag)
|
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
randReader := ioutil.NopCloser(io.LimitReader(testrand.Reader(), dataSize))
|
2019-03-21 13:47:48 +00:00
|
|
|
reader := encryption.TransformReader(eestream.PadReader(randReader, encrypter.InBlockSize()), encrypter, 0)
|
|
|
|
|
|
|
|
cipherData, err := ioutil.ReadAll(reader)
|
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
assert.EqualValues(t, calculatedSize, len(cipherData), errTag)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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,
|
2019-03-21 13:47:48 +00:00
|
|
|
} {
|
|
|
|
test(cipher)
|
|
|
|
}
|
|
|
|
}
|