gencert_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package gencert
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. "github.com/cloudflare/cfssl/cli"
  7. )
  8. func TestGencertMain(t *testing.T) {
  9. c := cli.Config{
  10. IsCA: true,
  11. }
  12. err := gencertMain([]string{"../testdata/csr.json"}, c)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. c = cli.Config{
  17. IsCA: true,
  18. CAKeyFile: "../testdata/ca-key.pem",
  19. }
  20. err = gencertMain([]string{"../testdata/csr.json"}, c)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. c = cli.Config{
  25. CAFile: "../testdata/ca.pem",
  26. CAKeyFile: "../testdata/ca-key.pem",
  27. }
  28. err = gencertMain([]string{"../testdata/csr.json"}, c)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. c = cli.Config{
  33. RenewCA: true,
  34. CAFile: "../testdata/ca.pem",
  35. CAKeyFile: "../testdata/ca-key.pem",
  36. }
  37. err = gencertMain([]string{}, c)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. }
  42. func TestGencertFile(t *testing.T) {
  43. c := cli.Config{
  44. IsCA: true,
  45. CAKeyFile: "file:../testdata/ca-key.pem",
  46. }
  47. err := gencertMain([]string{"../testdata/csr.json"}, c)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. c = cli.Config{
  52. CAFile: "file:../testdata/ca.pem",
  53. CAKeyFile: "file:../testdata/ca-key.pem",
  54. }
  55. err = gencertMain([]string{"../testdata/csr.json"}, c)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. c = cli.Config{
  60. RenewCA: true,
  61. CAFile: "file:../testdata/ca.pem",
  62. CAKeyFile: "file:../testdata/ca-key.pem",
  63. }
  64. err = gencertMain([]string{}, c)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. }
  69. func TestGencertEnv(t *testing.T) {
  70. tempCaCert, _ := os.ReadFile("../testdata/ca.pem")
  71. tempCaKey, _ := os.ReadFile("../testdata/ca-key.pem")
  72. os.Setenv("ca", string(tempCaCert))
  73. os.Setenv("ca_key", string(tempCaKey))
  74. c := cli.Config{
  75. IsCA: true,
  76. CAKeyFile: "env:ca_key",
  77. }
  78. err := gencertMain([]string{"../testdata/csr.json"}, c)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. c = cli.Config{
  83. CAFile: "env:ca",
  84. CAKeyFile: "env:ca_key",
  85. }
  86. err = gencertMain([]string{"../testdata/csr.json"}, c)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. c = cli.Config{
  91. RenewCA: true,
  92. CAFile: "env:ca",
  93. CAKeyFile: "env:ca_key",
  94. }
  95. err = gencertMain([]string{}, c)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. }
  100. func TestBadGencertEnv(t *testing.T) {
  101. tempCaCert, _ := os.ReadFile("../testdata/ca.pem")
  102. tempCaKey, _ := os.ReadFile("../testdata/ca-key.pem")
  103. os.Setenv("ca", string(tempCaCert))
  104. os.Setenv("ca_key", string(tempCaKey))
  105. c := cli.Config{
  106. RenewCA: true,
  107. CAFile: "ca",
  108. CAKeyFile: "env:ca_key",
  109. }
  110. err := gencertMain([]string{}, c)
  111. if err == nil {
  112. t.Fatal("No prefix provided, should report an error")
  113. }
  114. c = cli.Config{
  115. RenewCA: true,
  116. CAFile: "env:ca",
  117. CAKeyFile: "ca_key",
  118. }
  119. err = gencertMain([]string{}, c)
  120. if err == nil {
  121. t.Fatal("No prefix provided, should report an error")
  122. }
  123. c = cli.Config{
  124. RenewCA: true,
  125. CAFile: "env:ca",
  126. CAKeyFile: "en:ca_key",
  127. }
  128. err = gencertMain([]string{}, c)
  129. if err == nil {
  130. t.Fatal("Unsupported prefix, should report error")
  131. }
  132. c = cli.Config{
  133. RenewCA: true,
  134. CAFile: "env:ca",
  135. CAKeyFile: "env:file:ca_key",
  136. }
  137. err = gencertMain([]string{}, c)
  138. if err == nil {
  139. t.Fatal("Multiple prefixes, should report error")
  140. }
  141. }
  142. func TestBadGencertMain(t *testing.T) {
  143. err := gencertMain([]string{"../testdata/csr.json"}, cli.Config{})
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. err = gencertMain([]string{"../testdata/csr.json"}, cli.Config{CAFile: "../testdata/ca.pem"})
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. err = gencertMain([]string{}, cli.Config{RenewCA: true})
  152. if err == nil {
  153. t.Fatal("No CA or Key provided, should report error")
  154. }
  155. err = gencertMain([]string{}, cli.Config{})
  156. if err == nil {
  157. t.Fatal("Not enough argument, should report error")
  158. }
  159. err = gencertMain([]string{"../testdata/bad_csr.json"}, cli.Config{})
  160. if err == nil {
  161. t.Fatal("Bad CSR JSON, should report error")
  162. }
  163. err = gencertMain([]string{"../testdata/nothing"}, cli.Config{})
  164. if err == nil {
  165. t.Fatal("Trying to read a non-existence file, should report error")
  166. }
  167. err = gencertMain([]string{"../testdata/csr.json"}, cli.Config{IsCA: true, CAKeyFile: "../../testdata/garbage.crt"})
  168. if err == nil {
  169. t.Fatal("Bad CA, should report error")
  170. }
  171. err = gencertMain([]string{"../testdata/csr.json"}, cli.Config{CAFile: "../testdata/ca.pem", Remote: "123::::123"})
  172. if err == nil {
  173. t.Fatal("Invalid remote, should reort error")
  174. }
  175. }
  176. func TestOidMain(t *testing.T) {
  177. c := cli.Config{
  178. CAFile: "../testdata/ca.pem",
  179. CAKeyFile: "../testdata/ca-key.pem",
  180. }
  181. err := gencertMain([]string{"../testdata/bad_oid_csr.json"}, c)
  182. if err == nil {
  183. t.Fatal("Expected error")
  184. }
  185. if !strings.Contains(err.Error(), "invalid OID part abc") {
  186. t.Fatalf("Unexpected error: %s", err.Error())
  187. }
  188. }