storj/pkg/encryption/encryption_test.go
Egon Elbre 5d0816430f
rename all the things (#2531)
* rename pkg/linksharing to linksharing
* rename pkg/httpserver to linksharing/httpserver
* rename pkg/eestream to uplink/eestream
* rename pkg/stream to uplink/stream
* rename pkg/metainfo/kvmetainfo to uplink/metainfo/kvmetainfo
* rename pkg/auth/signing to pkg/signing
* rename pkg/storage to uplink/storage
* rename pkg/accounting to satellite/accounting
* rename pkg/audit to satellite/audit
* rename pkg/certdb to satellite/certdb
* rename pkg/discovery to satellite/discovery
* rename pkg/overlay to satellite/overlay
* rename pkg/datarepair to satellite/repair
2019-07-28 08:55:36 +03:00

70 lines
1.8 KiB
Go

// 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"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/encryption"
"storj.io/storj/pkg/storj"
"storj.io/storj/uplink/eestream"
)
const (
uint32Size = 4
)
func TestCalcEncryptedSize(t *testing.T) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
forAllCiphers(func(cipher storj.CipherSuite) {
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)
parameters := storj.EncryptionParameters{CipherSuite: cipher, BlockSize: 1 * memory.KiB.Int32()}
calculatedSize, err := encryption.CalcEncryptedSize(dataSize, parameters)
require.NoError(t, err, errTag)
encrypter, err := encryption.NewEncrypter(parameters.CipherSuite, new(storj.Key), new(storj.Nonce), int(parameters.BlockSize))
require.NoError(t, err, errTag)
randReader := ioutil.NopCloser(io.LimitReader(testrand.Reader(), dataSize))
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)
}
})
}
func forAllCiphers(test func(cipher storj.CipherSuite)) {
for _, cipher := range []storj.CipherSuite{
storj.EncNull,
storj.EncAESGCM,
storj.EncSecretBox,
} {
test(cipher)
}
}