error_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package errors
  2. import (
  3. "crypto/x509"
  4. "encoding/json"
  5. "errors"
  6. "testing"
  7. )
  8. func TestNew(t *testing.T) {
  9. err := New(CertificateError, Unknown)
  10. if err == nil {
  11. t.Fatal("Error creation failed.")
  12. }
  13. if err.ErrorCode != int(CertificateError)+int(Unknown) {
  14. t.Fatal("Error code construction failed.")
  15. }
  16. if err.Message != "Unknown certificate error" {
  17. t.Fatal("Error message construction failed.")
  18. }
  19. code := New(OCSPError, ReadFailed).ErrorCode
  20. if code != 8001 {
  21. t.Fatal("Improper error code")
  22. }
  23. code = New(OCSPError, IssuerMismatch).ErrorCode
  24. if code != 8100 {
  25. t.Fatal("Improper error code")
  26. }
  27. code = New(OCSPError, InvalidStatus).ErrorCode
  28. if code != 8200 {
  29. t.Fatal("Improper error code")
  30. }
  31. code = New(CertificateError, Unknown).ErrorCode
  32. if code != 1000 {
  33. t.Fatal("Improper error code")
  34. }
  35. code = New(CertificateError, ReadFailed).ErrorCode
  36. if code != 1001 {
  37. t.Fatal("Improper error code")
  38. }
  39. code = New(CertificateError, DecodeFailed).ErrorCode
  40. if code != 1002 {
  41. t.Fatal("Improper error code")
  42. }
  43. code = New(CertificateError, ParseFailed).ErrorCode
  44. if code != 1003 {
  45. t.Fatal("Improper error code")
  46. }
  47. code = New(CertificateError, SelfSigned).ErrorCode
  48. if code != 1100 {
  49. t.Fatal("Improper error code")
  50. }
  51. code = New(CertificateError, VerifyFailed).ErrorCode
  52. if code != 1200 {
  53. t.Fatal("Improper error code")
  54. }
  55. code = New(CertificateError, BadRequest).ErrorCode
  56. if code != 1300 {
  57. t.Fatal("Improper error code")
  58. }
  59. code = New(CertificateError, MissingSerial).ErrorCode
  60. if code != 1400 {
  61. t.Fatal("Improper error code")
  62. }
  63. code = New(PrivateKeyError, Unknown).ErrorCode
  64. if code != 2000 {
  65. t.Fatal("Improper error code")
  66. }
  67. code = New(PrivateKeyError, ReadFailed).ErrorCode
  68. if code != 2001 {
  69. t.Fatal("Improper error code")
  70. }
  71. code = New(PrivateKeyError, DecodeFailed).ErrorCode
  72. if code != 2002 {
  73. t.Fatal("Improper error code")
  74. }
  75. code = New(PrivateKeyError, ParseFailed).ErrorCode
  76. if code != 2003 {
  77. t.Fatal("Improper error code")
  78. }
  79. code = New(PrivateKeyError, Encrypted).ErrorCode
  80. if code != 2100 {
  81. t.Fatal("Improper error code")
  82. }
  83. code = New(PrivateKeyError, NotRSAOrECC).ErrorCode
  84. if code != 2200 {
  85. t.Fatal("Improper error code")
  86. }
  87. code = New(PrivateKeyError, KeyMismatch).ErrorCode
  88. if code != 2300 {
  89. t.Fatal("Improper error code")
  90. }
  91. code = New(PrivateKeyError, GenerationFailed).ErrorCode
  92. if code != 2400 {
  93. t.Fatal("Improper error code")
  94. }
  95. code = New(PrivateKeyError, Unavailable).ErrorCode
  96. if code != 2500 {
  97. t.Fatal("Improper error code")
  98. }
  99. code = New(IntermediatesError, Unknown).ErrorCode
  100. if code != 3000 {
  101. t.Fatal("Improper error code")
  102. }
  103. code = New(IntermediatesError, ReadFailed).ErrorCode
  104. if code != 3001 {
  105. t.Fatal("Improper error code")
  106. }
  107. code = New(IntermediatesError, DecodeFailed).ErrorCode
  108. if code != 3002 {
  109. t.Fatal("Improper error code")
  110. }
  111. code = New(IntermediatesError, ParseFailed).ErrorCode
  112. if code != 3003 {
  113. t.Fatal("Improper error code")
  114. }
  115. code = New(RootError, Unknown).ErrorCode
  116. if code != 4000 {
  117. t.Fatal("Improper error code")
  118. }
  119. code = New(RootError, ReadFailed).ErrorCode
  120. if code != 4001 {
  121. t.Fatal("Improper error code")
  122. }
  123. code = New(RootError, DecodeFailed).ErrorCode
  124. if code != 4002 {
  125. t.Fatal("Improper error code")
  126. }
  127. code = New(RootError, ParseFailed).ErrorCode
  128. if code != 4003 {
  129. t.Fatal("Improper error code")
  130. }
  131. code = New(PolicyError, Unknown).ErrorCode
  132. if code != 5000 {
  133. t.Fatal("Improper error code")
  134. }
  135. code = New(PolicyError, NoKeyUsages).ErrorCode
  136. if code != 5100 {
  137. t.Fatal("Improper error code")
  138. }
  139. code = New(PolicyError, InvalidPolicy).ErrorCode
  140. if code != 5200 {
  141. t.Fatal("Improper error code")
  142. }
  143. code = New(PolicyError, InvalidRequest).ErrorCode
  144. if code != 5300 {
  145. t.Fatal("Improper error code")
  146. }
  147. code = New(PolicyError, UnknownProfile).ErrorCode
  148. if code != 5400 {
  149. t.Fatal("Improper error code")
  150. }
  151. code = New(DialError, Unknown).ErrorCode
  152. if code != 6000 {
  153. t.Fatal("Improper error code")
  154. }
  155. code = New(APIClientError, AuthenticationFailure).ErrorCode
  156. if code != 7100 {
  157. t.Fatal("Improper error code")
  158. }
  159. code = New(APIClientError, JSONError).ErrorCode
  160. if code != 7200 {
  161. t.Fatal("Improper error code")
  162. }
  163. code = New(APIClientError, ClientHTTPError).ErrorCode
  164. if code != 7400 {
  165. t.Fatal("Improper error code")
  166. }
  167. code = New(APIClientError, IOError).ErrorCode
  168. if code != 7300 {
  169. t.Fatal("Improper error code")
  170. }
  171. code = New(APIClientError, ServerRequestFailed).ErrorCode
  172. if code != 7500 {
  173. t.Fatal("Improper error code")
  174. }
  175. code = New(CSRError, Unknown).ErrorCode
  176. if code != 9000 {
  177. t.Fatal("Improper error code")
  178. }
  179. code = New(CSRError, ReadFailed).ErrorCode
  180. if code != 9001 {
  181. t.Fatal("Improper error code")
  182. }
  183. code = New(CSRError, DecodeFailed).ErrorCode
  184. if code != 9002 {
  185. t.Fatal("Improper error code")
  186. }
  187. code = New(CSRError, ParseFailed).ErrorCode
  188. if code != 9003 {
  189. t.Fatal("Improper error code")
  190. }
  191. code = New(CSRError, KeyMismatch).ErrorCode
  192. if code != 9300 {
  193. t.Fatal("Improper error code")
  194. }
  195. code = New(CSRError, BadRequest).ErrorCode
  196. if code != 9300 {
  197. t.Fatal("Improper error code")
  198. }
  199. code = New(CTError, Unknown).ErrorCode
  200. if code != 10000 {
  201. t.Fatal("Improper error code")
  202. }
  203. code = New(CTError, PrecertSubmissionFailed).ErrorCode
  204. if code != 10100 {
  205. t.Fatal("Improper error code")
  206. }
  207. }
  208. func TestWrap(t *testing.T) {
  209. msg := "Arbitrary error message"
  210. err := Wrap(CertificateError, Unknown, errors.New(msg))
  211. if err == nil {
  212. t.Fatal("Error creation failed.")
  213. }
  214. if err.ErrorCode != int(CertificateError)+int(Unknown) {
  215. t.Fatal("Error code construction failed.")
  216. }
  217. if err.Message != msg {
  218. t.Fatal("Error message construction failed.")
  219. }
  220. err = Wrap(CertificateError, VerifyFailed, x509.CertificateInvalidError{Reason: x509.Expired})
  221. if err == nil {
  222. t.Fatal("Error creation failed.")
  223. }
  224. if err.ErrorCode != int(CertificateError)+int(VerifyFailed)+certificateInvalid+int(x509.Expired) {
  225. t.Fatal("Error code construction failed.")
  226. }
  227. errorString := "x509: certificate has expired or is not yet valid:"
  228. if err.Message[:49] != errorString[:49] {
  229. t.Fatal("Error message construction failed.")
  230. }
  231. err = Wrap(CertificateError, VerifyFailed, x509.UnknownAuthorityError{})
  232. if err == nil {
  233. t.Fatal("Error creation failed.")
  234. }
  235. err = Wrap(RootError, Unknown, errors.New(msg))
  236. if err == nil {
  237. t.Fatal("Error creation failed.")
  238. }
  239. if err.ErrorCode != int(RootError)+int(Unknown) {
  240. t.Fatal("Error code construction failed.")
  241. }
  242. if err.Message != msg {
  243. t.Fatal("Error message construction failed.")
  244. }
  245. }
  246. func TestMarshal(t *testing.T) {
  247. msg := "Arbitrary error message"
  248. err := Wrap(CertificateError, Unknown, errors.New(msg))
  249. bytes, _ := json.Marshal(err)
  250. var received Error
  251. json.Unmarshal(bytes, &received)
  252. if received.ErrorCode != int(CertificateError)+int(Unknown) {
  253. t.Fatal("Error code construction failed.")
  254. }
  255. if received.Message != msg {
  256. t.Fatal("Error message construction failed.")
  257. }
  258. }
  259. func TestErrorString(t *testing.T) {
  260. msg := "Arbitrary error message"
  261. err := Wrap(CertificateError, Unknown, errors.New(msg))
  262. str := err.Error()
  263. if str != `{"code":1000,"message":"`+msg+`"}` {
  264. t.Fatal("Incorrect Error():", str)
  265. }
  266. }
  267. func TestHTTP(t *testing.T) {
  268. err := NewMethodNotAllowed("GET")
  269. if err == nil {
  270. t.Fatal("New Mathod Check failed")
  271. }
  272. err = NewBadRequest(errors.New("Bad Request"))
  273. if err == nil {
  274. t.Fatal("New Bad Request Check failed")
  275. }
  276. if err.StatusCode != 400 {
  277. t.Fatal("New Bad Request error code construction failed")
  278. }
  279. err = NewBadRequestString("Bad Request String")
  280. if err == nil {
  281. t.Fatal("New Bad Request String Check failed")
  282. }
  283. if err.StatusCode != 400 {
  284. t.Fatal("New Bad Request String error code construction failed")
  285. }
  286. err = NewBadRequestMissingParameter("Request Missing Parameter")
  287. if err == nil {
  288. t.Fatal("New Bad Request Missing Parameter Check failed")
  289. }
  290. if err.StatusCode != 400 {
  291. t.Fatal("New Bad Request Missing Parameter error code construction failed")
  292. }
  293. err = NewBadRequestUnwantedParameter("Unwanted Parameter Present In Request")
  294. if err == nil {
  295. t.Fatal("New Bad Request Unwanted Parameter Check failed")
  296. }
  297. if err.StatusCode != 400 {
  298. t.Fatal("New Bad Request Unwanted Parameter error code construction failed")
  299. }
  300. }
  301. func TestHTTPErrorString(t *testing.T) {
  302. method := "GET"
  303. err := NewMethodNotAllowed(method)
  304. str := err.Error()
  305. if str != `Method is not allowed:"`+method+`"` {
  306. t.Fatal("Incorrect Error():", str)
  307. }
  308. }