root_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package authority
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/assert"
  7. )
  8. func TestRoot(t *testing.T) {
  9. a := testAuthority(t)
  10. a.certificates.Store("invaliddata", "a string") // invalid cert for testing
  11. tests := map[string]struct {
  12. sum string
  13. err *apiError
  14. }{
  15. "not-found": {"foo", &apiError{errors.New("certificate with fingerprint foo was not found"), http.StatusNotFound, context{}}},
  16. "invalid-stored-certificate": {"invaliddata", &apiError{errors.New("stored value is not a *cryto/x509.Certificate"), http.StatusInternalServerError, context{}}},
  17. "success": {"189f573cfa159251e445530847ef80b1b62a3a380ee670dcb49e33ed34da0616", nil},
  18. }
  19. for name, tc := range tests {
  20. t.Run(name, func(t *testing.T) {
  21. crt, err := a.Root(tc.sum)
  22. if err != nil {
  23. if assert.NotNil(t, tc.err) {
  24. switch v := err.(type) {
  25. case *apiError:
  26. assert.HasPrefix(t, v.err.Error(), tc.err.Error())
  27. assert.Equals(t, v.code, tc.err.code)
  28. assert.Equals(t, v.context, tc.err.context)
  29. default:
  30. t.Errorf("unexpected error type: %T", v)
  31. }
  32. }
  33. } else {
  34. if assert.Nil(t, tc.err) {
  35. assert.Equals(t, crt, a.rootX509Crt)
  36. }
  37. }
  38. })
  39. }
  40. }