selfsign_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package selfsign
  2. import (
  3. "crypto/x509"
  4. "encoding/pem"
  5. "net"
  6. "net/url"
  7. "os"
  8. "reflect"
  9. "testing"
  10. "time"
  11. "github.com/cloudflare/cfssl/config"
  12. "github.com/cloudflare/cfssl/helpers"
  13. )
  14. const (
  15. keyFile = "testdata/localhost.key"
  16. csrFile = "testdata/localhost.csr"
  17. csr2File = "testdata/sans.csr"
  18. )
  19. func TestDefaultSign(t *testing.T) {
  20. csrBytes, err := os.ReadFile(csrFile)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. keyBytes, err := os.ReadFile(keyFile)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. priv, err := helpers.ParsePrivateKeyPEM(keyBytes)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. profile := config.DefaultConfig()
  33. profile.Expiry = 10 * time.Hour
  34. _, err = Sign(priv, csrBytes, profile)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. }
  39. func TestSANs(t *testing.T) {
  40. t.Skip("broken relating to https://github.com/cloudflare/cfssl/issues/1230")
  41. csrBytes, err := os.ReadFile(csr2File)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. keyBytes, err := os.ReadFile(keyFile)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. priv, err := helpers.ParsePrivateKeyPEM(keyBytes)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. profile := config.DefaultConfig()
  54. profile.Expiry = 10 * time.Hour
  55. certPEM, err := Sign(priv, csrBytes, profile)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. p, _ := pem.Decode(certPEM)
  60. if p == nil || p.Type != "CERTIFICATE" {
  61. // this seems unlikely
  62. t.Fatalf("failed creating certificate")
  63. }
  64. cert, err := x509.ParseCertificate(p.Bytes)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. expectedEmailAddresses := []string{"jdoe@example.com"}
  69. if !reflect.DeepEqual(cert.EmailAddresses, expectedEmailAddresses) {
  70. t.Errorf("cert should have contained EmailAddresses %#v but had %#v", expectedEmailAddresses, cert.EmailAddresses)
  71. }
  72. expectedDNSNames := []string{"cloudflare.com", "www.cloudflare.com"}
  73. if !reflect.DeepEqual(cert.DNSNames, expectedDNSNames) {
  74. t.Errorf("cert should have contained DNSNames %#v but had %#v", expectedDNSNames, cert.DNSNames)
  75. }
  76. expectedIPAddresses := []net.IP{{0xc0, 0xa8, 0x0, 0x1}}
  77. if !reflect.DeepEqual(cert.IPAddresses, expectedIPAddresses) {
  78. t.Errorf("cert should have contained IPAddresses %#v but had %#v", expectedIPAddresses, cert.IPAddresses)
  79. }
  80. expectedURIs := []*url.URL{{Scheme: "https", Host: "www.cloudflare.com"}}
  81. if !reflect.DeepEqual(cert.URIs, expectedURIs) {
  82. t.Errorf("cert should have contained URIs %#v but had %#v", expectedURIs, cert.URIs)
  83. }
  84. }