AdGuardHome/internal/home/tls_internal_test.go

78 lines
3.1 KiB
Go
Raw Normal View History

package home
2019-02-27 13:07:29 +03:00
import (
"testing"
"time"
Pull request: 4898-redirect-https Merge in DNS/adguard-home from 4898-redirect-https to master Updates #4898. Updates #4927. Squashed commit of the following: commit bc41b6cae7ede0f1235e3956ab49204af1c9f38d Merge: 815e2991 ac7634da Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 1 13:02:23 2022 +0300 Merge branch 'master' into 4898-redirect-https commit 815e299137224fc3c7fd46924d7b936515b95d67 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 1 12:58:28 2022 +0300 home: imp ip addr detection commit 9d4ecd9ab0e13ef6c19c3b923363bff43394ea4c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 17:23:41 2022 +0300 home: imp cyclo commit 86c47b68fe6e3916cec97eee5d34e3e6c18e4892 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 15:06:05 2022 +0300 all: imp text commit bcc25697b551668d1dab53a874e716fcadd83f09 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 11:47:57 2022 +0300 home: fix test commit bb51a74cb82eeaa977821fa7314810c7b8be55cb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Sun Oct 30 23:23:40 2022 +0300 home: imp code commit 38522330691baf8475a59ed4f40b1d45363df1e3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 28 17:00:50 2022 +0300 home: imp code commit 7284f7288feb7491560f0f5d2754044c7a9f603a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 27 19:42:57 2022 +0300 all: log changes commit 540efcb013e15294b98efe581323f75ceefc8f5a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 27 19:24:21 2022 +0300 home: imp tls
2022-11-01 13:09:52 +03:00
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert"
)
2019-02-27 13:07:29 +03:00
var testCertChainData = []byte(`-----BEGIN CERTIFICATE-----
2019-02-27 13:07:29 +03: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==
-----END CERTIFICATE-----`)
var testPrivateKeyData = []byte(`-----BEGIN PRIVATE KEY-----
2019-02-27 13:07:29 +03: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
-----END PRIVATE KEY-----`)
func TestValidateCertificates(t *testing.T) {
t.Run("bad_certificate", func(t *testing.T) {
status := &tlsConfigStatus{}
err := validateCertificates(status, []byte("bad cert"), nil, "")
Pull request: 4898-redirect-https Merge in DNS/adguard-home from 4898-redirect-https to master Updates #4898. Updates #4927. Squashed commit of the following: commit bc41b6cae7ede0f1235e3956ab49204af1c9f38d Merge: 815e2991 ac7634da Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 1 13:02:23 2022 +0300 Merge branch 'master' into 4898-redirect-https commit 815e299137224fc3c7fd46924d7b936515b95d67 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 1 12:58:28 2022 +0300 home: imp ip addr detection commit 9d4ecd9ab0e13ef6c19c3b923363bff43394ea4c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 17:23:41 2022 +0300 home: imp cyclo commit 86c47b68fe6e3916cec97eee5d34e3e6c18e4892 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 15:06:05 2022 +0300 all: imp text commit bcc25697b551668d1dab53a874e716fcadd83f09 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 11:47:57 2022 +0300 home: fix test commit bb51a74cb82eeaa977821fa7314810c7b8be55cb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Sun Oct 30 23:23:40 2022 +0300 home: imp code commit 38522330691baf8475a59ed4f40b1d45363df1e3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 28 17:00:50 2022 +0300 home: imp code commit 7284f7288feb7491560f0f5d2754044c7a9f603a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 27 19:42:57 2022 +0300 all: log changes commit 540efcb013e15294b98efe581323f75ceefc8f5a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 27 19:24:21 2022 +0300 home: imp tls
2022-11-01 13:09:52 +03:00
testutil.AssertErrorMsg(t, "empty certificate", err)
assert.False(t, status.ValidCert)
assert.False(t, status.ValidChain)
})
t.Run("bad_private_key", func(t *testing.T) {
status := &tlsConfigStatus{}
err := validateCertificates(status, nil, []byte("bad priv key"), "")
Pull request: 4898-redirect-https Merge in DNS/adguard-home from 4898-redirect-https to master Updates #4898. Updates #4927. Squashed commit of the following: commit bc41b6cae7ede0f1235e3956ab49204af1c9f38d Merge: 815e2991 ac7634da Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 1 13:02:23 2022 +0300 Merge branch 'master' into 4898-redirect-https commit 815e299137224fc3c7fd46924d7b936515b95d67 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 1 12:58:28 2022 +0300 home: imp ip addr detection commit 9d4ecd9ab0e13ef6c19c3b923363bff43394ea4c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 17:23:41 2022 +0300 home: imp cyclo commit 86c47b68fe6e3916cec97eee5d34e3e6c18e4892 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 15:06:05 2022 +0300 all: imp text commit bcc25697b551668d1dab53a874e716fcadd83f09 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 11:47:57 2022 +0300 home: fix test commit bb51a74cb82eeaa977821fa7314810c7b8be55cb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Sun Oct 30 23:23:40 2022 +0300 home: imp code commit 38522330691baf8475a59ed4f40b1d45363df1e3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 28 17:00:50 2022 +0300 home: imp code commit 7284f7288feb7491560f0f5d2754044c7a9f603a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 27 19:42:57 2022 +0300 all: log changes commit 540efcb013e15294b98efe581323f75ceefc8f5a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 27 19:24:21 2022 +0300 home: imp tls
2022-11-01 13:09:52 +03:00
testutil.AssertErrorMsg(t, "no valid keys were found", err)
assert.False(t, status.ValidKey)
})
t.Run("valid", func(t *testing.T) {
status := &tlsConfigStatus{}
err := validateCertificates(status, testCertChainData, testPrivateKeyData, "")
Pull request: 4898-redirect-https Merge in DNS/adguard-home from 4898-redirect-https to master Updates #4898. Updates #4927. Squashed commit of the following: commit bc41b6cae7ede0f1235e3956ab49204af1c9f38d Merge: 815e2991 ac7634da Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 1 13:02:23 2022 +0300 Merge branch 'master' into 4898-redirect-https commit 815e299137224fc3c7fd46924d7b936515b95d67 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 1 12:58:28 2022 +0300 home: imp ip addr detection commit 9d4ecd9ab0e13ef6c19c3b923363bff43394ea4c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 17:23:41 2022 +0300 home: imp cyclo commit 86c47b68fe6e3916cec97eee5d34e3e6c18e4892 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 15:06:05 2022 +0300 all: imp text commit bcc25697b551668d1dab53a874e716fcadd83f09 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 31 11:47:57 2022 +0300 home: fix test commit bb51a74cb82eeaa977821fa7314810c7b8be55cb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Sun Oct 30 23:23:40 2022 +0300 home: imp code commit 38522330691baf8475a59ed4f40b1d45363df1e3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 28 17:00:50 2022 +0300 home: imp code commit 7284f7288feb7491560f0f5d2754044c7a9f603a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 27 19:42:57 2022 +0300 all: log changes commit 540efcb013e15294b98efe581323f75ceefc8f5a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 27 19:24:21 2022 +0300 home: imp tls
2022-11-01 13:09:52 +03:00
assert.Error(t, err)
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)
})
2019-02-27 13:07:29 +03:00
}