...

Source file src/github.com/letsencrypt/boulder/cmd/ocsp-responder/main_test.go

Documentation: github.com/letsencrypt/boulder/cmd/ocsp-responder

     1  package notmain
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
    13  	"golang.org/x/crypto/ocsp"
    14  
    15  	blog "github.com/letsencrypt/boulder/log"
    16  	"github.com/letsencrypt/boulder/metrics"
    17  	"github.com/letsencrypt/boulder/ocsp/responder"
    18  	"github.com/letsencrypt/boulder/test"
    19  )
    20  
    21  func TestMux(t *testing.T) {
    22  	reqBytes, err := os.ReadFile("./testdata/ocsp.req")
    23  	test.AssertNotError(t, err, "failed to read OCSP request")
    24  	req, err := ocsp.ParseRequest(reqBytes)
    25  	test.AssertNotError(t, err, "failed to parse OCSP request")
    26  
    27  	doubleSlashBytes, err := base64.StdEncoding.DecodeString("MFMwUTBPME0wSzAJBgUrDgMCGgUABBR+5mrncpqz/PiiIGRsFqEtYHEIXQQUqEpqYwR93brm0Tm3pkVl7/Oo7KECEgO/AC2R1FW8hePAj4xp//8Jhw==")
    28  	test.AssertNotError(t, err, "failed to decode double slash OCSP request")
    29  	doubleSlashReq, err := ocsp.ParseRequest(doubleSlashBytes)
    30  	test.AssertNotError(t, err, "failed to parse double slash OCSP request")
    31  
    32  	respBytes, err := os.ReadFile("./testdata/ocsp.resp")
    33  	test.AssertNotError(t, err, "failed to read OCSP response")
    34  	resp, err := ocsp.ParseResponse(respBytes, nil)
    35  	test.AssertNotError(t, err, "failed to parse OCSP response")
    36  
    37  	responses := map[string]*responder.Response{
    38  		req.SerialNumber.String():            {Response: resp, Raw: respBytes},
    39  		doubleSlashReq.SerialNumber.String(): {Response: resp, Raw: respBytes},
    40  	}
    41  	src, err := responder.NewMemorySource(responses, blog.NewMock())
    42  	test.AssertNotError(t, err, "failed to create inMemorySource")
    43  
    44  	h := mux("/foobar/", src, time.Second, metrics.NoopRegisterer, []otelhttp.Option{}, blog.NewMock(), 1000)
    45  
    46  	type muxTest struct {
    47  		method   string
    48  		path     string
    49  		reqBody  []byte
    50  		respBody []byte
    51  	}
    52  	mts := []muxTest{
    53  		{"POST", "/foobar/", reqBytes, respBytes},
    54  		{"GET", "/", nil, nil},
    55  		{"GET", "/foobar/MFMwUTBPME0wSzAJBgUrDgMCGgUABBR+5mrncpqz/PiiIGRsFqEtYHEIXQQUqEpqYwR93brm0Tm3pkVl7/Oo7KECEgO/AC2R1FW8hePAj4xp//8Jhw==", nil, respBytes},
    56  	}
    57  	for i, mt := range mts {
    58  		w := httptest.NewRecorder()
    59  		r, err := http.NewRequest(mt.method, mt.path, bytes.NewReader(mt.reqBody))
    60  		if err != nil {
    61  			t.Fatalf("#%d, NewRequest: %s", i, err)
    62  		}
    63  		h.ServeHTTP(w, r)
    64  		if w.Code != http.StatusOK {
    65  			t.Errorf("Code: want %d, got %d", http.StatusOK, w.Code)
    66  		}
    67  		if !bytes.Equal(w.Body.Bytes(), mt.respBody) {
    68  			t.Errorf("Mismatched body: want %#v, got %#v", mt.respBody, w.Body.Bytes())
    69  		}
    70  	}
    71  }
    72  

View as plain text