crl_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/pem"
  6. "io"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "time"
  11. "github.com/go-chi/chi/v5"
  12. "github.com/pkg/errors"
  13. "github.com/smallstep/certificates/authority"
  14. "github.com/smallstep/certificates/errs"
  15. "github.com/stretchr/testify/assert"
  16. "github.com/stretchr/testify/require"
  17. )
  18. func Test_CRL(t *testing.T) {
  19. data := []byte{1, 2, 3, 4}
  20. pemData := pem.EncodeToMemory(&pem.Block{
  21. Type: "X509 CRL",
  22. Bytes: data,
  23. })
  24. pemData = bytes.TrimSpace(pemData)
  25. emptyPEMData := pem.EncodeToMemory(&pem.Block{
  26. Type: "X509 CRL",
  27. Bytes: nil,
  28. })
  29. emptyPEMData = bytes.TrimSpace(emptyPEMData)
  30. tests := []struct {
  31. name string
  32. url string
  33. err error
  34. statusCode int
  35. crlInfo *authority.CertificateRevocationListInfo
  36. expectedBody []byte
  37. expectedHeaders http.Header
  38. expectedErrorJSON string
  39. }{
  40. {"ok", "http://example.com/crl", nil, http.StatusOK, &authority.CertificateRevocationListInfo{Data: data}, data, http.Header{"Content-Type": []string{"application/pkix-crl"}, "Content-Disposition": []string{`attachment; filename="crl.crl"`}}, ""},
  41. {"ok/pem", "http://example.com/crl?pem=true", nil, http.StatusOK, &authority.CertificateRevocationListInfo{Data: data}, pemData, http.Header{"Content-Type": []string{"application/x-pem-file"}, "Content-Disposition": []string{`attachment; filename="crl.pem"`}}, ""},
  42. {"ok/empty", "http://example.com/crl", nil, http.StatusOK, &authority.CertificateRevocationListInfo{Data: nil}, nil, http.Header{"Content-Type": []string{"application/pkix-crl"}, "Content-Disposition": []string{`attachment; filename="crl.crl"`}}, ""},
  43. {"ok/empty-pem", "http://example.com/crl?pem=true", nil, http.StatusOK, &authority.CertificateRevocationListInfo{Data: nil}, emptyPEMData, http.Header{"Content-Type": []string{"application/x-pem-file"}, "Content-Disposition": []string{`attachment; filename="crl.pem"`}}, ""},
  44. {"fail/internal", "http://example.com/crl", errs.Wrap(http.StatusInternalServerError, errors.New("failure"), "authority.GetCertificateRevocationList"), http.StatusInternalServerError, nil, nil, http.Header{}, `{"status":500,"message":"The certificate authority encountered an Internal Server Error. Please see the certificate authority logs for more info."}`},
  45. {"fail/nil", "http://example.com/crl", nil, http.StatusNotFound, nil, nil, http.Header{}, `{"status":404,"message":"no CRL available"}`},
  46. }
  47. for _, tt := range tests {
  48. t.Run(tt.name, func(t *testing.T) {
  49. mockMustAuthority(t, &mockAuthority{ret1: tt.crlInfo, err: tt.err})
  50. chiCtx := chi.NewRouteContext()
  51. req := httptest.NewRequest("GET", tt.url, http.NoBody)
  52. req = req.WithContext(context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx))
  53. w := httptest.NewRecorder()
  54. CRL(w, req)
  55. res := w.Result()
  56. assert.Equal(t, tt.statusCode, res.StatusCode)
  57. body, err := io.ReadAll(res.Body)
  58. res.Body.Close()
  59. require.NoError(t, err)
  60. if tt.statusCode >= 300 {
  61. assert.JSONEq(t, tt.expectedErrorJSON, string(bytes.TrimSpace(body)))
  62. return
  63. }
  64. // check expected header values
  65. for _, h := range []string{"content-type", "content-disposition"} {
  66. v := tt.expectedHeaders.Get(h)
  67. require.NotEmpty(t, v)
  68. actual := res.Header.Get(h)
  69. assert.Equal(t, v, actual)
  70. }
  71. // check expires header value
  72. assert.NotEmpty(t, res.Header.Get("expires"))
  73. t1, err := time.Parse(time.RFC1123, res.Header.Get("expires"))
  74. if assert.NoError(t, err) {
  75. assert.False(t, t1.IsZero())
  76. }
  77. // check body contents
  78. assert.Equal(t, tt.expectedBody, bytes.TrimSpace(body))
  79. })
  80. }
  81. }