errors_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package acme
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func mustJSON(t *testing.T, m map[string]interface{}) string {
  10. t.Helper()
  11. b, err := json.Marshal(m)
  12. require.NoError(t, err)
  13. return string(b)
  14. }
  15. func TestError_WithAdditionalErrorDetail(t *testing.T) {
  16. internalJSON := mustJSON(t, map[string]interface{}{
  17. "detail": "The server experienced an internal error",
  18. "type": "urn:ietf:params:acme:error:serverInternal",
  19. })
  20. malformedErr := NewError(ErrorMalformedType, "malformed error") // will result in Err == nil behavior
  21. malformedJSON := mustJSON(t, map[string]interface{}{
  22. "detail": "The request message was malformed",
  23. "type": "urn:ietf:params:acme:error:malformed",
  24. })
  25. withDetailJSON := mustJSON(t, map[string]interface{}{
  26. "detail": "Attestation statement cannot be verified: invalid property",
  27. "type": "urn:ietf:params:acme:error:badAttestationStatement",
  28. })
  29. tests := []struct {
  30. name string
  31. err *Error
  32. want string
  33. }{
  34. {"internal", NewDetailedError(ErrorServerInternalType, ""), internalJSON},
  35. {"nil err", malformedErr, malformedJSON},
  36. {"detailed", NewDetailedError(ErrorBadAttestationStatementType, "invalid property"), withDetailJSON},
  37. }
  38. for _, tt := range tests {
  39. t.Run(tt.name, func(t *testing.T) {
  40. b, err := json.Marshal(tt.err)
  41. require.NoError(t, err)
  42. // tests if the additional error detail is included in the JSON representation
  43. // of the ACME error. This is what is returned to ACME clients and being logged
  44. // by the CA.
  45. assert.JSONEq(t, tt.want, string(b))
  46. })
  47. }
  48. }
  49. func Test_newErrorRecordCreatesErrorRecords(t *testing.T) {
  50. t.Parallel()
  51. t.Run("nil", func(t *testing.T) {
  52. t.Parallel()
  53. got := newErrorRecord(nil)
  54. assert.Nil(t, got)
  55. })
  56. t.Run("ok", func(t *testing.T) {
  57. t.Parallel()
  58. exp := map[string]any{
  59. "type": "urn:ietf:params:acme:error:malformed",
  60. "detail": "The request message was malformed",
  61. "internal": "fail",
  62. }
  63. expBytes, err := json.Marshal(exp)
  64. require.NoError(t, err)
  65. got := newErrorRecord(newError(ErrorMalformedType, errors.New("fail")))
  66. gotBytes, err := json.Marshal(got)
  67. require.NoError(t, err)
  68. assert.JSONEq(t, string(expBytes), string(gotBytes))
  69. })
  70. t.Run("subproblems", func(t *testing.T) {
  71. t.Parallel()
  72. s1 := NewSubproblem(ErrorMalformedType, "first-subproblem-msg")
  73. s1.Identifier = &Identifier{Type: DNS, Value: "test.example.com"}
  74. s2 := NewSubproblem(ErrorMalformedType, "second-subproblem-msg")
  75. e := newError(ErrorMalformedType, errors.New("fail"))
  76. e.AddSubproblems(s1, s2)
  77. exp := map[string]any{
  78. "type": "urn:ietf:params:acme:error:malformed",
  79. "detail": "The request message was malformed",
  80. "subproblems": []Subproblem{s1, s2},
  81. "internal": "fail",
  82. }
  83. expBytes, err := json.Marshal(exp)
  84. require.NoError(t, err)
  85. got := newErrorRecord(e)
  86. gotBytes, err := json.Marshal(got)
  87. require.NoError(t, err)
  88. assert.JSONEq(t, string(expBytes), string(gotBytes))
  89. })
  90. }