gencert_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package cryptutil
  10. import (
  11. "errors"
  12. "flag"
  13. "fmt"
  14. "os"
  15. "strings"
  16. "testing"
  17. "time"
  18. "devt.de/krotik/common/fileutil"
  19. )
  20. const certDir = "certs"
  21. const invalidFileName = "**" + string(0x0)
  22. func TestMain(m *testing.M) {
  23. flag.Parse()
  24. // Setup
  25. if res, _ := fileutil.PathExists(certDir); res {
  26. os.RemoveAll(certDir)
  27. }
  28. err := os.Mkdir(certDir, 0770)
  29. if err != nil {
  30. fmt.Print("Could not create test directory:", err.Error())
  31. os.Exit(1)
  32. }
  33. // Run the tests
  34. res := m.Run()
  35. // Teardown
  36. err = os.RemoveAll(certDir)
  37. if err != nil {
  38. fmt.Print("Could not remove test directory:", err.Error())
  39. }
  40. os.Exit(res)
  41. }
  42. func TestGenCert(t *testing.T) {
  43. checkGeneration := func(ecdsaCurve string) error {
  44. // Generate a certificate and private key
  45. err := GenCert(certDir, "cert.pem", "key.pem", "localhost,127.0.0.1", "", 365*24*time.Hour, true, 2048, ecdsaCurve)
  46. if err != nil {
  47. return err
  48. }
  49. // Check that the files were generated
  50. if ok, _ := fileutil.PathExists(certDir + "/key.pem"); !ok {
  51. return errors.New("Private key was not generated")
  52. }
  53. if ok, _ := fileutil.PathExists(certDir + "/cert.pem"); !ok {
  54. return errors.New("Certificate was not generated")
  55. }
  56. _, err = ReadX509CertsFromFile(certDir + "/cert.pem")
  57. if err != nil {
  58. return err
  59. }
  60. return nil
  61. }
  62. if err := checkGeneration(""); err != nil {
  63. t.Error(err)
  64. return
  65. }
  66. if err := checkGeneration("P224"); err != nil {
  67. t.Error(err)
  68. return
  69. }
  70. if err := checkGeneration("P256"); err != nil {
  71. t.Error(err)
  72. return
  73. }
  74. if err := checkGeneration("P384"); err != nil {
  75. t.Error(err)
  76. return
  77. }
  78. if err := checkGeneration("P521"); err != nil {
  79. t.Error(err)
  80. return
  81. }
  82. // Test error cases
  83. err := GenCert(certDir, "cert.pem", "key.pem", "", "", 365*24*time.Hour, true, 2048, "")
  84. if err.Error() != "Host required for certificate generation" {
  85. t.Error(err)
  86. return
  87. }
  88. err = GenCert(certDir, "cert.pem", "key.pem", "localhost", "", 365*24*time.Hour, true, 2048, "xxx")
  89. if err.Error() != `Failed to generate private key: Unrecognized elliptic curve: "xxx"` {
  90. t.Error(err)
  91. return
  92. }
  93. err = GenCert(certDir, "cert.pem", "key.pem", "localhost", "xxx", 365*24*time.Hour, true, 2048, "")
  94. if err.Error() != `Failed to parse creation date: parsing time "xxx" as "Jan 2 15:04:05 2006": cannot parse "xxx" as "Jan"` {
  95. t.Error(err)
  96. return
  97. }
  98. err = GenCert(certDir, "cert.pem", invalidFileName, "localhost", "", 365*24*time.Hour, true, 2048, "")
  99. if !strings.HasPrefix(err.Error(), "Failed to open") {
  100. t.Error(err)
  101. return
  102. }
  103. err = GenCert(certDir, invalidFileName, "key.pem", "localhost", "", 365*24*time.Hour, true, 2048, "")
  104. if !strings.HasPrefix(err.Error(), "Failed to open") {
  105. t.Error(err)
  106. return
  107. }
  108. if publicKey(nil) != nil {
  109. t.Error("Unexpected result")
  110. return
  111. }
  112. if pemBlockForKey(nil) != nil {
  113. t.Error("Unexpected result")
  114. return
  115. }
  116. }