revoke_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package revoke
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/cloudflare/cfssl/certdb"
  6. "github.com/cloudflare/cfssl/certdb/sql"
  7. "github.com/cloudflare/cfssl/certdb/testdb"
  8. "github.com/cloudflare/cfssl/cli"
  9. "golang.org/x/crypto/ocsp"
  10. )
  11. var dbAccessor certdb.Accessor
  12. const (
  13. fakeAKI = "fake aki"
  14. )
  15. func prepDB() (err error) {
  16. db := testdb.SQLiteDB("../../certdb/testdb/certstore_development.db")
  17. expirationTime := time.Now().AddDate(1, 0, 0)
  18. var cert = certdb.CertificateRecord{
  19. Serial: "1",
  20. AKI: fakeAKI,
  21. Expiry: expirationTime,
  22. PEM: "unexpired cert",
  23. }
  24. dbAccessor = sql.NewAccessor(db)
  25. err = dbAccessor.InsertCertificate(cert)
  26. if err != nil {
  27. return err
  28. }
  29. return
  30. }
  31. func TestRevokeMain(t *testing.T) {
  32. err := prepDB()
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. err = revokeMain([]string{}, cli.Config{Serial: "1", AKI: fakeAKI, DBConfigFile: "../testdata/db-config.json"})
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. crs, err := dbAccessor.GetCertificate("1", fakeAKI)
  41. if err != nil {
  42. t.Fatal("Failed to get certificate")
  43. }
  44. if len(crs) != 1 {
  45. t.Fatal("Failed to get exactly one certificate")
  46. }
  47. cr := crs[0]
  48. if cr.Status != "revoked" {
  49. t.Fatal("Certificate not marked revoked after we revoked it")
  50. }
  51. err = revokeMain([]string{}, cli.Config{Serial: "1", AKI: fakeAKI, Reason: "2", DBConfigFile: "../testdata/db-config.json"})
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. crs, err = dbAccessor.GetCertificate("1", fakeAKI)
  56. if err != nil {
  57. t.Fatal("Failed to get certificate")
  58. }
  59. if len(crs) != 1 {
  60. t.Fatal("Failed to get exactly one certificate")
  61. }
  62. cr = crs[0]
  63. if cr.Reason != 2 {
  64. t.Fatal("Certificate revocation reason incorrect")
  65. }
  66. err = revokeMain([]string{}, cli.Config{Serial: "1", AKI: fakeAKI, Reason: "Superseded", DBConfigFile: "../testdata/db-config.json"})
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. crs, err = dbAccessor.GetCertificate("1", fakeAKI)
  71. if err != nil {
  72. t.Fatal("Failed to get certificate")
  73. }
  74. if len(crs) != 1 {
  75. t.Fatal("Failed to get exactly one certificate")
  76. }
  77. cr = crs[0]
  78. if cr.Reason != ocsp.Superseded {
  79. t.Fatal("Certificate revocation reason incorrect")
  80. }
  81. err = revokeMain([]string{}, cli.Config{Serial: "1", AKI: fakeAKI, Reason: "invalid_reason", DBConfigFile: "../testdata/db-config.json"})
  82. if err == nil {
  83. t.Fatal("Expected error from invalid reason")
  84. }
  85. err = revokeMain([]string{}, cli.Config{Serial: "1", AKI: fakeAKI, Reason: "999", DBConfigFile: "../testdata/db-config.json"})
  86. if err == nil {
  87. t.Fatal("Expected error from invalid reason")
  88. }
  89. err = revokeMain([]string{}, cli.Config{Serial: "2", AKI: fakeAKI, DBConfigFile: "../testdata/db-config.json"})
  90. if err == nil {
  91. t.Fatal("Expected error from unrecognized serial number")
  92. }
  93. err = revokeMain([]string{}, cli.Config{AKI: fakeAKI, DBConfigFile: "../testdata/db-config.json"})
  94. if err == nil {
  95. t.Fatal("Expected error from missing serial number")
  96. }
  97. err = revokeMain([]string{}, cli.Config{Serial: "1", AKI: fakeAKI})
  98. if err == nil {
  99. t.Fatal("Expected error from missing db config")
  100. }
  101. err = revokeMain([]string{}, cli.Config{Serial: "1", DBConfigFile: "../testdata/db-config.json"})
  102. if err == nil {
  103. t.Fatal("Expected error from missing aki")
  104. }
  105. }