v2ray-core/transport/internet/tls/config_test.go

100 lines
2.5 KiB
Go
Raw Normal View History

2018-04-10 21:02:47 +00:00
package tls_test
import (
gotls "crypto/tls"
"crypto/x509"
"testing"
"time"
2019-02-02 21:19:30 +00:00
"v2ray.com/core/common"
2018-04-10 21:02:47 +00:00
"v2ray.com/core/common/protocol/tls/cert"
. "v2ray.com/core/transport/internet/tls"
)
func TestCertificateIssuing(t *testing.T) {
certificate := ParseCertificate(cert.MustGenerate(nil, cert.Authority(true), cert.KeyUsage(x509.KeyUsageCertSign)))
certificate.Usage = Certificate_AUTHORITY_ISSUE
c := &Config{
Certificate: []*Certificate{
certificate,
},
}
tlsConfig := c.GetTLSConfig()
v2rayCert, err := tlsConfig.GetCertificate(&gotls.ClientHelloInfo{
ServerName: "www.v2ray.com",
})
2019-02-02 21:19:30 +00:00
common.Must(err)
2018-04-10 21:02:47 +00:00
x509Cert, err := x509.ParseCertificate(v2rayCert.Certificate[0])
2019-02-02 21:19:30 +00:00
common.Must(err)
2019-02-09 14:46:48 +00:00
if !x509Cert.NotAfter.After(time.Now()) {
t.Error("NotAfter: ", x509Cert.NotAfter)
}
2018-04-10 21:02:47 +00:00
}
2018-04-18 09:45:49 +00:00
func TestExpiredCertificate(t *testing.T) {
caCert := cert.MustGenerate(nil, cert.Authority(true), cert.KeyUsage(x509.KeyUsageCertSign))
expiredCert := cert.MustGenerate(caCert, cert.NotAfter(time.Now().Add(time.Minute*-2)), cert.CommonName("www.v2ray.com"), cert.DNSNames("www.v2ray.com"))
certificate := ParseCertificate(caCert)
certificate.Usage = Certificate_AUTHORITY_ISSUE
certificate2 := ParseCertificate(expiredCert)
c := &Config{
Certificate: []*Certificate{
certificate,
certificate2,
},
}
tlsConfig := c.GetTLSConfig()
v2rayCert, err := tlsConfig.GetCertificate(&gotls.ClientHelloInfo{
ServerName: "www.v2ray.com",
})
2019-02-02 21:19:30 +00:00
common.Must(err)
2018-04-18 09:45:49 +00:00
x509Cert, err := x509.ParseCertificate(v2rayCert.Certificate[0])
2019-02-02 21:19:30 +00:00
common.Must(err)
2019-02-09 14:46:48 +00:00
if !x509Cert.NotAfter.After(time.Now()) {
t.Error("NotAfter: ", x509Cert.NotAfter)
}
2018-04-18 09:45:49 +00:00
}
2018-09-10 21:55:54 +00:00
func TestInsecureCertificates(t *testing.T) {
c := &Config{
AllowInsecureCiphers: true,
}
tlsConfig := c.GetTLSConfig()
if len(tlsConfig.CipherSuites) > 0 {
t.Fatal("Unexpected tls cipher suites list: ", tlsConfig.CipherSuites)
}
}
2018-11-21 12:00:26 +00:00
func BenchmarkCertificateIssuing(b *testing.B) {
certificate := ParseCertificate(cert.MustGenerate(nil, cert.Authority(true), cert.KeyUsage(x509.KeyUsageCertSign)))
certificate.Usage = Certificate_AUTHORITY_ISSUE
c := &Config{
Certificate: []*Certificate{
certificate,
},
}
tlsConfig := c.GetTLSConfig()
lenCerts := len(tlsConfig.Certificates)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = tlsConfig.GetCertificate(&gotls.ClientHelloInfo{
ServerName: "www.v2ray.com",
})
delete(tlsConfig.NameToCertificate, "www.v2ray.com")
tlsConfig.Certificates = tlsConfig.Certificates[:lenCerts]
}
}