config_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package tls_test
  2. import (
  3. gotls "crypto/tls"
  4. "crypto/x509"
  5. "testing"
  6. "time"
  7. "v2ray.com/core/common/protocol/tls/cert"
  8. . "v2ray.com/core/transport/internet/tls"
  9. . "v2ray.com/ext/assert"
  10. )
  11. func TestCertificateIssuing(t *testing.T) {
  12. assert := With(t)
  13. certificate := ParseCertificate(cert.MustGenerate(nil, cert.Authority(true), cert.KeyUsage(x509.KeyUsageCertSign)))
  14. certificate.Usage = Certificate_AUTHORITY_ISSUE
  15. c := &Config{
  16. Certificate: []*Certificate{
  17. certificate,
  18. },
  19. }
  20. tlsConfig := c.GetTLSConfig()
  21. v2rayCert, err := tlsConfig.GetCertificate(&gotls.ClientHelloInfo{
  22. ServerName: "www.v2ray.com",
  23. })
  24. assert(err, IsNil)
  25. x509Cert, err := x509.ParseCertificate(v2rayCert.Certificate[0])
  26. assert(err, IsNil)
  27. assert(x509Cert.NotAfter.After(time.Now()), IsTrue)
  28. }
  29. func TestExpiredCertificate(t *testing.T) {
  30. assert := With(t)
  31. caCert := cert.MustGenerate(nil, cert.Authority(true), cert.KeyUsage(x509.KeyUsageCertSign))
  32. expiredCert := cert.MustGenerate(caCert, cert.NotAfter(time.Now().Add(time.Minute*-2)), cert.CommonName("www.v2ray.com"), cert.DNSNames("www.v2ray.com"))
  33. certificate := ParseCertificate(caCert)
  34. certificate.Usage = Certificate_AUTHORITY_ISSUE
  35. certificate2 := ParseCertificate(expiredCert)
  36. c := &Config{
  37. Certificate: []*Certificate{
  38. certificate,
  39. certificate2,
  40. },
  41. }
  42. tlsConfig := c.GetTLSConfig()
  43. v2rayCert, err := tlsConfig.GetCertificate(&gotls.ClientHelloInfo{
  44. ServerName: "www.v2ray.com",
  45. })
  46. assert(err, IsNil)
  47. x509Cert, err := x509.ParseCertificate(v2rayCert.Certificate[0])
  48. assert(err, IsNil)
  49. assert(x509Cert.NotAfter.After(time.Now()), IsTrue)
  50. }