2020-05-11 08:16:27 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-12-06 15:57:35 +00:00
|
|
|
|
2023-11-22 10:56:43 +00:00
|
|
|
"go.uber.org/mock/gomock"
|
2021-12-06 15:57:35 +00:00
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-05-11 08:16:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func CreateMockEncryptionAlg(ctrl *gomock.Controller) EncryptionAlgorithm {
|
2023-08-15 12:47:05 +00:00
|
|
|
return createMockEncryptionAlgorithm(
|
|
|
|
ctrl,
|
|
|
|
func(code []byte) ([]byte, error) {
|
|
|
|
return code, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateMockEncryptionAlgWithCode compares the length of the value to be encrypted with the length of the provided code.
|
|
|
|
// It will return an error if they do not match.
|
|
|
|
// The provided code will be used to encrypt in favor of the value passed to the encryption.
|
|
|
|
// This function is intended to be used where the passed value is not in control, but where the returned encryption requires a static value.
|
|
|
|
func CreateMockEncryptionAlgWithCode(ctrl *gomock.Controller, code string) EncryptionAlgorithm {
|
|
|
|
return createMockEncryptionAlgorithm(
|
|
|
|
ctrl,
|
|
|
|
func(c []byte) ([]byte, error) {
|
|
|
|
if len(c) != len(code) {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "id", "invalid code length - expected %d, got %d", len(code), len(c))
|
2023-08-15 12:47:05 +00:00
|
|
|
}
|
|
|
|
return []byte(code), nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createMockEncryptionAlgorithm(ctrl *gomock.Controller, encryptFunction func(c []byte) ([]byte, error)) *MockEncryptionAlgorithm {
|
2020-05-11 08:16:27 +00:00
|
|
|
mCrypto := NewMockEncryptionAlgorithm(ctrl)
|
|
|
|
mCrypto.EXPECT().Algorithm().AnyTimes().Return("enc")
|
|
|
|
mCrypto.EXPECT().EncryptionKeyID().AnyTimes().Return("id")
|
|
|
|
mCrypto.EXPECT().DecryptionKeyIDs().AnyTimes().Return([]string{"id"})
|
2021-02-23 16:05:47 +00:00
|
|
|
mCrypto.EXPECT().Encrypt(gomock.Any()).AnyTimes().DoAndReturn(
|
2023-08-15 12:47:05 +00:00
|
|
|
encryptFunction,
|
2020-05-11 08:16:27 +00:00
|
|
|
)
|
2021-02-23 16:05:47 +00:00
|
|
|
mCrypto.EXPECT().DecryptString(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
|
2020-05-11 08:16:27 +00:00
|
|
|
func(code []byte, keyID string) (string, error) {
|
|
|
|
if keyID != "id" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return "", zerrors.ThrowInternal(nil, "id", "invalid key id")
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
return string(code), nil
|
|
|
|
},
|
|
|
|
)
|
2021-12-06 15:57:35 +00:00
|
|
|
mCrypto.EXPECT().Decrypt(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
|
|
|
|
func(code []byte, keyID string) ([]byte, error) {
|
|
|
|
if keyID != "id" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(nil, "id", "invalid key id")
|
2021-12-06 15:57:35 +00:00
|
|
|
}
|
|
|
|
return code, nil
|
|
|
|
},
|
|
|
|
)
|
2020-05-11 08:16:27 +00:00
|
|
|
return mCrypto
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:06:36 +00:00
|
|
|
func CreateMockHashAlg(ctrl *gomock.Controller) HashAlgorithm {
|
|
|
|
mCrypto := NewMockHashAlgorithm(ctrl)
|
2020-05-11 08:16:27 +00:00
|
|
|
mCrypto.EXPECT().Algorithm().AnyTimes().Return("hash")
|
2021-02-23 16:05:47 +00:00
|
|
|
mCrypto.EXPECT().Hash(gomock.Any()).AnyTimes().DoAndReturn(
|
2020-05-11 08:16:27 +00:00
|
|
|
func(code []byte) ([]byte, error) {
|
|
|
|
return code, nil
|
|
|
|
},
|
|
|
|
)
|
2021-02-23 16:05:47 +00:00
|
|
|
mCrypto.EXPECT().CompareHash(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
|
2020-05-11 08:16:27 +00:00
|
|
|
func(hashed, comparer []byte) error {
|
|
|
|
if string(hashed) != string(comparer) {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowInternal(nil, "id", "invalid")
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return mCrypto
|
|
|
|
}
|
|
|
|
|
|
|
|
func createMockCrypto(t *testing.T) Crypto {
|
|
|
|
mCrypto := NewMockCrypto(gomock.NewController(t))
|
|
|
|
mCrypto.EXPECT().Algorithm().AnyTimes().Return("crypto")
|
|
|
|
return mCrypto
|
|
|
|
}
|
|
|
|
|
|
|
|
func createMockGenerator(t *testing.T, crypto Crypto) Generator {
|
|
|
|
mGenerator := NewMockGenerator(gomock.NewController(t))
|
|
|
|
mGenerator.EXPECT().Alg().AnyTimes().Return(crypto)
|
|
|
|
return mGenerator
|
|
|
|
}
|