File: crl_test.go

package info (click to toggle)
golang-github-smallstep-certificates 0.28.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,684 kB
  • sloc: sh: 367; makefile: 129
file content (93 lines) | stat: -rw-r--r-- 3,679 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package api

import (
	"bytes"
	"context"
	"encoding/pem"
	"io"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/go-chi/chi/v5"
	"github.com/pkg/errors"
	"github.com/smallstep/certificates/authority"
	"github.com/smallstep/certificates/errs"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func Test_CRL(t *testing.T) {
	data := []byte{1, 2, 3, 4}
	pemData := pem.EncodeToMemory(&pem.Block{
		Type:  "X509 CRL",
		Bytes: data,
	})
	pemData = bytes.TrimSpace(pemData)
	emptyPEMData := pem.EncodeToMemory(&pem.Block{
		Type:  "X509 CRL",
		Bytes: nil,
	})
	emptyPEMData = bytes.TrimSpace(emptyPEMData)
	tests := []struct {
		name              string
		url               string
		err               error
		statusCode        int
		crlInfo           *authority.CertificateRevocationListInfo
		expectedBody      []byte
		expectedHeaders   http.Header
		expectedErrorJSON string
	}{
		{"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.der"`}}, ""},
		{"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"`}}, ""},
		{"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.der"`}}, ""},
		{"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"`}}, ""},
		{"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."}`},
		{"fail/nil", "http://example.com/crl", nil, http.StatusNotFound, nil, nil, http.Header{}, `{"status":404,"message":"no CRL available"}`},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			mockMustAuthority(t, &mockAuthority{ret1: tt.crlInfo, err: tt.err})

			chiCtx := chi.NewRouteContext()
			req := httptest.NewRequest("GET", tt.url, http.NoBody)
			req = req.WithContext(context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx))
			w := httptest.NewRecorder()
			CRL(w, req)
			res := w.Result()

			assert.Equal(t, tt.statusCode, res.StatusCode)

			body, err := io.ReadAll(res.Body)
			res.Body.Close()
			require.NoError(t, err)

			if tt.statusCode >= 300 {
				assert.JSONEq(t, tt.expectedErrorJSON, string(bytes.TrimSpace(body)))
				return
			}

			// check expected header values
			for _, h := range []string{"content-type", "content-disposition"} {
				v := tt.expectedHeaders.Get(h)
				require.NotEmpty(t, v)

				actual := res.Header.Get(h)
				assert.Equal(t, v, actual)
			}

			// check expires header value
			assert.NotEmpty(t, res.Header.Get("expires"))
			t1, err := time.Parse(time.RFC1123, res.Header.Get("expires"))
			if assert.NoError(t, err) {
				assert.False(t, t1.IsZero())
			}

			// check body contents
			assert.Equal(t, tt.expectedBody, bytes.TrimSpace(body))
		})
	}
}