2022-08-11 08:48:58 +00:00
|
|
|
package encryption
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
aes256Key = "1234567890qwertyuiopasdfghjklzxc"
|
|
|
|
)
|
|
|
|
|
2022-08-11 08:48:58 +00:00
|
|
|
func getAES256Key() []byte {
|
|
|
|
key := make([]byte, 32)
|
2022-08-01 16:52:09 +00:00
|
|
|
copy(key[:], aes256Key)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHMAC(t *testing.T) {
|
2022-08-11 08:48:58 +00:00
|
|
|
encParam, err := NewParams(getAES256Key())
|
|
|
|
require.NoError(t, err)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
hmacKey, hmacSalt, err := encParam.HMAC()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-11 08:48:58 +00:00
|
|
|
encInfo := ObjectEncryption{
|
2022-08-01 16:52:09 +00:00
|
|
|
Enabled: true,
|
|
|
|
Algorithm: "",
|
|
|
|
HMACKey: hex.EncodeToString(hmacKey),
|
|
|
|
HMACSalt: hex.EncodeToString(hmacSalt),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = encParam.MatchObjectEncryption(encInfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
objSize = 30 * 1024 * 1024
|
|
|
|
partNum = 6
|
|
|
|
partSize = 5 * 1024 * 1024
|
|
|
|
encObjSize = 31472640 // objSize + enc headers
|
|
|
|
encPartSize = 5245440 // partSize + enc headers
|
|
|
|
)
|
|
|
|
|
2022-08-11 08:48:58 +00:00
|
|
|
func getDecrypter(t *testing.T) *Decrypter {
|
|
|
|
parts := make([]encryptedPart, partNum)
|
2022-08-01 16:52:09 +00:00
|
|
|
for i := range parts {
|
2022-08-11 08:48:58 +00:00
|
|
|
parts[i] = encryptedPart{
|
|
|
|
size: partSize,
|
|
|
|
encryptedSize: encPartSize,
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-11 08:48:58 +00:00
|
|
|
|
|
|
|
params, err := NewParams(getAES256Key())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return &Decrypter{
|
2022-08-01 16:52:09 +00:00
|
|
|
parts: parts,
|
2022-08-12 06:56:38 +00:00
|
|
|
encryption: *params,
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrypterInitParams(t *testing.T) {
|
2022-08-11 08:48:58 +00:00
|
|
|
decReader := getDecrypter(t)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
for i, tc := range []struct {
|
2022-08-11 08:48:58 +00:00
|
|
|
rng *Range
|
2022-08-01 16:52:09 +00:00
|
|
|
expSkipLen, expLn, expOff, expSeqNumber uint64
|
2022-08-11 08:48:58 +00:00
|
|
|
expDecLen, expDataRemain, expEncPartRange uint64
|
2022-08-01 16:52:09 +00:00
|
|
|
}{
|
|
|
|
{
|
2022-08-11 08:48:58 +00:00
|
|
|
rng: &Range{End: objSize - 1},
|
2022-08-01 16:52:09 +00:00
|
|
|
expSkipLen: 0,
|
|
|
|
expLn: encObjSize,
|
|
|
|
expOff: 0,
|
|
|
|
expSeqNumber: 0,
|
|
|
|
expDecLen: objSize,
|
|
|
|
expDataRemain: partSize,
|
|
|
|
expEncPartRange: encPartSize,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 08:48:58 +00:00
|
|
|
rng: &Range{End: 999999},
|
2022-08-01 16:52:09 +00:00
|
|
|
expSkipLen: 0,
|
|
|
|
expLn: 1049088,
|
|
|
|
expOff: 0,
|
|
|
|
expSeqNumber: 0,
|
|
|
|
expDecLen: 1000000,
|
|
|
|
expDataRemain: 1000000,
|
|
|
|
expEncPartRange: 1049088,
|
|
|
|
},
|
|
|
|
{
|
2022-08-11 08:48:58 +00:00
|
|
|
rng: &Range{Start: 1000000, End: 1999999},
|
2022-08-01 16:52:09 +00:00
|
|
|
expSkipLen: 16960,
|
|
|
|
expLn: 1049088,
|
|
|
|
expOff: 983520,
|
|
|
|
expSeqNumber: 15,
|
|
|
|
expDecLen: 1000000,
|
|
|
|
expDataRemain: 1000000,
|
|
|
|
expEncPartRange: 1049088,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
|
|
|
decReader.rangeParam = tc.rng
|
|
|
|
decReader.initRangeParams()
|
|
|
|
require.Equal(t, tc.expSkipLen, decReader.skipLen)
|
|
|
|
require.Equal(t, tc.expDecLen, decReader.decLen)
|
2022-08-12 09:45:58 +00:00
|
|
|
require.Equal(t, tc.expLn, decReader.length)
|
|
|
|
require.Equal(t, tc.expOff, decReader.offset)
|
2022-08-01 16:52:09 +00:00
|
|
|
require.Equal(t, tc.expDataRemain, decReader.partDataRemain)
|
|
|
|
require.Equal(t, tc.expEncPartRange, decReader.encPartRangeLen)
|
|
|
|
require.Equal(t, tc.expSeqNumber, decReader.seqNumber)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|