crl_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package crl
  2. import (
  3. "crypto/x509"
  4. "os"
  5. "testing"
  6. )
  7. const (
  8. serverCertFile = "testdata/ca.pem"
  9. serverKeyFile = "testdata/ca-key.pem"
  10. tryTwoCert = "testdata/caTwo.pem"
  11. tryTwoKey = "testdata/ca-keyTwo.pem"
  12. serialList = "testdata/serialList"
  13. )
  14. func TestNewCRLFromFile(t *testing.T) {
  15. tryTwoKeyBytes, err := os.ReadFile(tryTwoKey)
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. tryTwoCertBytes, err := os.ReadFile(tryTwoCert)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. serialListBytes, err := os.ReadFile(serialList)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. crl, err := NewCRLFromFile(serialListBytes, tryTwoCertBytes, tryTwoKeyBytes, "0")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. certList, err := x509.ParseDERCRL(crl)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. numCerts := len(certList.TBSCertList.RevokedCertificates)
  36. expectedNum := 4
  37. if expectedNum != numCerts {
  38. t.Fatal("Wrong number of expired certificates")
  39. }
  40. }
  41. func TestNewCRLFromFileWithoutRevocations(t *testing.T) {
  42. tryTwoKeyBytes, err := os.ReadFile(tryTwoKey)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. tryTwoCertBytes, err := os.ReadFile(tryTwoCert)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. crl, err := NewCRLFromFile([]byte("\n \n"), tryTwoCertBytes, tryTwoKeyBytes, "0")
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. certList, err := x509.ParseDERCRL(crl)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. numCerts := len(certList.TBSCertList.RevokedCertificates)
  59. expectedNum := 0
  60. if expectedNum != numCerts {
  61. t.Fatal("Wrong number of expired certificates")
  62. }
  63. }