2019-06-10 09:33:19 +01:00
|
|
|
package home
|
2019-02-27 10:07:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-03-11 14:32:58 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2019-02-27 10:07:29 +00:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
var testCertChainData = []byte(`-----BEGIN CERTIFICATE-----
|
2019-02-27 10:07:29 +00:00
|
|
|
MIICKzCCAZSgAwIBAgIJAMT9kPVJdM7LMA0GCSqGSIb3DQEBCwUAMC0xFDASBgNV
|
|
|
|
BAoMC0FkR3VhcmQgTHRkMRUwEwYDVQQDDAxBZEd1YXJkIEhvbWUwHhcNMTkwMjI3
|
|
|
|
MDkyNDIzWhcNNDYwNzE0MDkyNDIzWjAtMRQwEgYDVQQKDAtBZEd1YXJkIEx0ZDEV
|
|
|
|
MBMGA1UEAwwMQWRHdWFyZCBIb21lMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
|
|
|
|
gQCwvwUnPJiOvLcOaWmGu6Y68ksFr13nrXBcsDlhxlXy8PaohVi3XxEmt2OrVjKW
|
|
|
|
QFw/bdV4fZ9tdWFAVRRkgeGbIZzP7YBD1Ore/O5SQ+DbCCEafvjJCcXQIrTeKFE6
|
|
|
|
i9G3aSMHs0Pwq2LgV8U5mYotLrvyFiE8QPInJbDDMpaFYwIDAQABo1MwUTAdBgNV
|
|
|
|
HQ4EFgQUdLUmQpEqrhn4eKO029jYd2AAZEQwHwYDVR0jBBgwFoAUdLUmQpEqrhn4
|
|
|
|
eKO029jYd2AAZEQwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOBgQB8
|
|
|
|
LwlXfbakf7qkVTlCNXgoY7RaJ8rJdPgOZPoCTVToEhT6u/cb1c2qp8QB0dNExDna
|
|
|
|
b0Z+dnODTZqQOJo6z/wIXlcUrnR4cQVvytXt8lFn+26l6Y6EMI26twC/xWr+1swq
|
|
|
|
Muj4FeWHVDerquH4yMr1jsYLD3ci+kc5sbIX6TfVxQ==
|
2022-10-14 17:37:14 +01:00
|
|
|
-----END CERTIFICATE-----`)
|
|
|
|
|
|
|
|
var testPrivateKeyData = []byte(`-----BEGIN PRIVATE KEY-----
|
2019-02-27 10:07:29 +00:00
|
|
|
MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALC/BSc8mI68tw5p
|
|
|
|
aYa7pjrySwWvXeetcFywOWHGVfLw9qiFWLdfESa3Y6tWMpZAXD9t1Xh9n211YUBV
|
|
|
|
FGSB4ZshnM/tgEPU6t787lJD4NsIIRp++MkJxdAitN4oUTqL0bdpIwezQ/CrYuBX
|
|
|
|
xTmZii0uu/IWITxA8iclsMMyloVjAgMBAAECgYEAmjzoG1h27UDkIlB9BVWl95TP
|
|
|
|
QVPLB81D267xNFDnWk1Lgr5zL/pnNjkdYjyjgpkBp1yKyE4gHV4skv5sAFWTcOCU
|
|
|
|
QCgfPfUn/rDFcxVzAdJVWAa/CpJNaZgjTPR8NTGU+Ztod+wfBESNCP5tbnuw0GbL
|
|
|
|
MuwdLQJGbzeJYpsNysECQQDfFHYoRNfgxHwMbX24GCoNZIgk12uDmGTA9CS5E+72
|
|
|
|
9t3V1y4CfXxSkfhqNbd5RWrUBRLEw9BKofBS7L9NMDKDAkEAytQoIueE1vqEAaRg
|
|
|
|
a3A1YDUekKesU5wKfKfKlXvNgB7Hwh4HuvoQS9RCvVhf/60Dvq8KSu6hSjkFRquj
|
|
|
|
FQ5roQJBAMwKwyiCD5MfJPeZDmzcbVpiocRQ5Z4wPbffl9dRTDnIA5AciZDthlFg
|
|
|
|
An/jMjZSMCxNl6UyFcqt5Et1EGVhuFECQQCZLXxaT+qcyHjlHJTMzuMgkz1QFbEp
|
|
|
|
O5EX70gpeGQMPDK0QSWpaazg956njJSDbNCFM4BccrdQbJu1cW4qOsfBAkAMgZuG
|
|
|
|
O88slmgTRHX4JGFmy3rrLiHNI2BbJSuJ++Yllz8beVzh6NfvuY+HKRCmPqoBPATU
|
|
|
|
kXS9jgARhhiWXJrk
|
2022-10-14 17:37:14 +01:00
|
|
|
-----END PRIVATE KEY-----`)
|
2021-03-11 14:32:58 +00:00
|
|
|
|
|
|
|
func TestValidateCertificates(t *testing.T) {
|
|
|
|
t.Run("bad_certificate", func(t *testing.T) {
|
2022-10-14 17:37:14 +01:00
|
|
|
status := &tlsConfigStatus{}
|
|
|
|
err := validateCertificates(status, []byte("bad cert"), nil, "")
|
2022-11-01 10:09:52 +00:00
|
|
|
testutil.AssertErrorMsg(t, "empty certificate", err)
|
2022-10-14 17:37:14 +01:00
|
|
|
assert.False(t, status.ValidCert)
|
|
|
|
assert.False(t, status.ValidChain)
|
2021-03-11 14:32:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad_private_key", func(t *testing.T) {
|
2022-10-14 17:37:14 +01:00
|
|
|
status := &tlsConfigStatus{}
|
|
|
|
err := validateCertificates(status, nil, []byte("bad priv key"), "")
|
2022-11-01 10:09:52 +00:00
|
|
|
testutil.AssertErrorMsg(t, "no valid keys were found", err)
|
2022-10-14 17:37:14 +01:00
|
|
|
assert.False(t, status.ValidKey)
|
2021-03-11 14:32:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
2022-10-14 17:37:14 +01:00
|
|
|
status := &tlsConfigStatus{}
|
|
|
|
err := validateCertificates(status, testCertChainData, testPrivateKeyData, "")
|
2022-11-01 10:09:52 +00:00
|
|
|
assert.Error(t, err)
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
notBefore := time.Date(2019, 2, 27, 9, 24, 23, 0, time.UTC)
|
|
|
|
notAfter := time.Date(2046, 7, 14, 9, 24, 23, 0, time.UTC)
|
|
|
|
|
|
|
|
assert.True(t, status.ValidCert)
|
|
|
|
assert.False(t, status.ValidChain)
|
|
|
|
assert.True(t, status.ValidKey)
|
|
|
|
assert.Equal(t, "RSA", status.KeyType)
|
|
|
|
assert.Equal(t, "CN=AdGuard Home,O=AdGuard Ltd", status.Subject)
|
|
|
|
assert.Equal(t, "CN=AdGuard Home,O=AdGuard Ltd", status.Issuer)
|
|
|
|
assert.Equal(t, notBefore, status.NotBefore)
|
|
|
|
assert.Equal(t, notAfter, status.NotAfter)
|
|
|
|
assert.True(t, status.ValidPair)
|
2021-03-11 14:32:58 +00:00
|
|
|
})
|
2019-02-27 10:07:29 +00:00
|
|
|
}
|