2020-03-23 06:06:44 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-11-22 10:56:43 +00:00
|
|
|
"go.uber.org/mock/gomock"
|
2020-03-23 06:06:44 +00:00
|
|
|
)
|
|
|
|
|
2020-03-30 09:26:02 +00:00
|
|
|
func TestIsCodeExpired(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
creationDate time.Time
|
|
|
|
expiry time.Duration
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"not expired",
|
|
|
|
args{
|
|
|
|
creationDate: time.Now(),
|
|
|
|
expiry: time.Duration(5 * time.Minute),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2020-04-21 15:00:32 +00:00
|
|
|
{
|
|
|
|
"never expires",
|
|
|
|
args{
|
|
|
|
creationDate: time.Now().Add(-5 * time.Minute),
|
|
|
|
expiry: 0,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2020-03-30 09:26:02 +00:00
|
|
|
{
|
|
|
|
"expired",
|
|
|
|
args{
|
|
|
|
creationDate: time.Now().Add(-5 * time.Minute),
|
|
|
|
expiry: time.Duration(5 * time.Minute),
|
|
|
|
},
|
|
|
|
true,
|
2020-03-23 06:06:44 +00:00
|
|
|
},
|
|
|
|
}
|
2020-03-30 09:26:02 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := IsCodeExpired(tt.args.creationDate, tt.args.expiry); got != tt.want {
|
|
|
|
t.Errorf("IsCodeExpired() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 06:06:44 +00:00
|
|
|
|
2020-03-30 09:26:02 +00:00
|
|
|
func TestVerifyCode(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
creationDate time.Time
|
|
|
|
expiry time.Duration
|
|
|
|
cryptoCode *CryptoValue
|
|
|
|
verificationCode string
|
|
|
|
g Generator
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"expired",
|
|
|
|
args{
|
|
|
|
creationDate: time.Now().Add(-5 * time.Minute),
|
|
|
|
expiry: 5 * time.Minute,
|
|
|
|
cryptoCode: nil,
|
|
|
|
verificationCode: "",
|
2023-06-20 15:34:06 +00:00
|
|
|
g: createMockGenerator(t, createMockCrypto(t)),
|
2020-03-30 09:26:02 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"unsupported alg err",
|
|
|
|
args{
|
|
|
|
creationDate: time.Now(),
|
|
|
|
expiry: 5 * time.Minute,
|
|
|
|
cryptoCode: nil,
|
|
|
|
verificationCode: "code",
|
|
|
|
g: createMockGenerator(t, createMockCrypto(t)),
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"encryption alg ok",
|
|
|
|
args{
|
|
|
|
creationDate: time.Now(),
|
|
|
|
expiry: 5 * time.Minute,
|
|
|
|
cryptoCode: &CryptoValue{
|
|
|
|
CryptoType: TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
verificationCode: "code",
|
2020-05-11 08:16:27 +00:00
|
|
|
g: createMockGenerator(t, CreateMockEncryptionAlg(gomock.NewController(t))),
|
2020-03-30 09:26:02 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-04-05 09:35:49 +00:00
|
|
|
if err := VerifyCode(tt.args.creationDate, tt.args.expiry, tt.args.cryptoCode, tt.args.verificationCode, tt.args.g.Alg()); (err != nil) != tt.wantErr {
|
2020-03-30 09:26:02 +00:00
|
|
|
t.Errorf("VerifyCode() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-23 06:06:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 09:26:02 +00:00
|
|
|
func Test_verifyEncryptedCode(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
cryptoCode *CryptoValue
|
|
|
|
verificationCode string
|
|
|
|
alg EncryptionAlgorithm
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil error",
|
|
|
|
args{
|
|
|
|
cryptoCode: nil,
|
|
|
|
verificationCode: "",
|
2020-05-11 08:16:27 +00:00
|
|
|
alg: CreateMockEncryptionAlg(gomock.NewController(t)),
|
2020-03-30 09:26:02 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"wrong cryptotype error",
|
|
|
|
args{
|
|
|
|
cryptoCode: &CryptoValue{
|
|
|
|
CryptoType: TypeHash,
|
|
|
|
Crypted: nil,
|
|
|
|
},
|
|
|
|
verificationCode: "",
|
2020-05-11 08:16:27 +00:00
|
|
|
alg: CreateMockEncryptionAlg(gomock.NewController(t)),
|
2020-03-30 09:26:02 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"wrong algorithm error",
|
|
|
|
args{
|
|
|
|
cryptoCode: &CryptoValue{
|
|
|
|
CryptoType: TypeEncryption,
|
|
|
|
Algorithm: "enc2",
|
|
|
|
Crypted: nil,
|
|
|
|
},
|
|
|
|
verificationCode: "",
|
2020-05-11 08:16:27 +00:00
|
|
|
alg: CreateMockEncryptionAlg(gomock.NewController(t)),
|
2020-03-30 09:26:02 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"wrong key id error",
|
|
|
|
args{
|
|
|
|
cryptoCode: &CryptoValue{
|
|
|
|
CryptoType: TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
Crypted: nil,
|
|
|
|
},
|
|
|
|
verificationCode: "wrong",
|
2020-05-11 08:16:27 +00:00
|
|
|
alg: CreateMockEncryptionAlg(gomock.NewController(t)),
|
2020-03-30 09:26:02 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"wrong verification code error",
|
|
|
|
args{
|
|
|
|
cryptoCode: &CryptoValue{
|
|
|
|
CryptoType: TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
verificationCode: "wrong",
|
2020-05-11 08:16:27 +00:00
|
|
|
alg: CreateMockEncryptionAlg(gomock.NewController(t)),
|
2020-03-30 09:26:02 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"verification code ok",
|
|
|
|
args{
|
|
|
|
cryptoCode: &CryptoValue{
|
|
|
|
CryptoType: TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
verificationCode: "code",
|
2020-05-11 08:16:27 +00:00
|
|
|
alg: CreateMockEncryptionAlg(gomock.NewController(t)),
|
2020-03-30 09:26:02 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if err := verifyEncryptedCode(tt.args.cryptoCode, tt.args.verificationCode, tt.args.alg); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("verifyEncryptedCode() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-23 06:06:44 +00:00
|
|
|
}
|