error_test.go raw
1 // Copyright (c) 2020 The Decred developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4
5 package secp256k1
6
7 import (
8 "errors"
9 "testing"
10 )
11
12 // TestErrorKindStringer tests the stringized output for the ErrorKind type.
13 func TestErrorKindStringer(t *testing.T) {
14 tests := []struct {
15 in ErrorKind
16 want string
17 }{
18 {ErrPubKeyInvalidLen, "ErrPubKeyInvalidLen"},
19 {ErrPubKeyInvalidFormat, "ErrPubKeyInvalidFormat"},
20 {ErrPubKeyXTooBig, "ErrPubKeyXTooBig"},
21 {ErrPubKeyYTooBig, "ErrPubKeyYTooBig"},
22 {ErrPubKeyNotOnCurve, "ErrPubKeyNotOnCurve"},
23 {ErrPubKeyMismatchedOddness, "ErrPubKeyMismatchedOddness"},
24 }
25 for i, test := range tests {
26 result := test.in.Error()
27 if result != test.want {
28 t.Errorf("#%d: got: %s want: %s", i, result, test.want)
29 continue
30 }
31 }
32 }
33
34 // TestError tests the error output for the Error type.
35 func TestError(t *testing.T) {
36 tests := []struct {
37 in Error
38 want string
39 }{
40 {
41 Error{Description: "some error"},
42 "some error",
43 }, {
44 Error{Description: "human-readable error"},
45 "human-readable error",
46 },
47 }
48 for i, test := range tests {
49 result := test.in.Error()
50 if result != test.want {
51 t.Errorf("#%d: got: %s want: %s", i, result, test.want)
52 continue
53 }
54 }
55 }
56
57 // TestErrorKindIsAs ensures both ErrorKind and Error can be identified as being
58 // a specific error kind via errors.Is and unwrapped via errors.As.
59 func TestErrorKindIsAs(t *testing.T) {
60 tests := []struct {
61 name string
62 err error
63 target error
64 wantMatch bool
65 wantAs ErrorKind
66 }{
67 {
68 name: "ErrPubKeyInvalidLen == ErrPubKeyInvalidLen",
69 err: ErrPubKeyInvalidLen,
70 target: ErrPubKeyInvalidLen,
71 wantMatch: true,
72 wantAs: ErrPubKeyInvalidLen,
73 }, {
74 name: "Error.ErrPubKeyInvalidLen == ErrPubKeyInvalidLen",
75 err: makeError(ErrPubKeyInvalidLen, ""),
76 target: ErrPubKeyInvalidLen,
77 wantMatch: true,
78 wantAs: ErrPubKeyInvalidLen,
79 }, {
80 name: "Error.ErrPubKeyInvalidLen == Error.ErrPubKeyInvalidLen",
81 err: makeError(ErrPubKeyInvalidLen, ""),
82 target: makeError(ErrPubKeyInvalidLen, ""),
83 wantMatch: true,
84 wantAs: ErrPubKeyInvalidLen,
85 }, {
86 name: "ErrPubKeyInvalidFormat != ErrPubKeyInvalidLen",
87 err: ErrPubKeyInvalidFormat,
88 target: ErrPubKeyInvalidLen,
89 wantMatch: false,
90 wantAs: ErrPubKeyInvalidFormat,
91 }, {
92 name: "Error.ErrPubKeyInvalidFormat != ErrPubKeyInvalidLen",
93 err: makeError(ErrPubKeyInvalidFormat, ""),
94 target: ErrPubKeyInvalidLen,
95 wantMatch: false,
96 wantAs: ErrPubKeyInvalidFormat,
97 }, {
98 name: "ErrPubKeyInvalidFormat != Error.ErrPubKeyInvalidLen",
99 err: ErrPubKeyInvalidFormat,
100 target: makeError(ErrPubKeyInvalidLen, ""),
101 wantMatch: false,
102 wantAs: ErrPubKeyInvalidFormat,
103 }, {
104 name: "Error.ErrPubKeyInvalidFormat != Error.ErrPubKeyInvalidLen",
105 err: makeError(ErrPubKeyInvalidFormat, ""),
106 target: makeError(ErrPubKeyInvalidLen, ""),
107 wantMatch: false,
108 wantAs: ErrPubKeyInvalidFormat,
109 },
110 }
111 for _, test := range tests {
112 // Ensure the error matches or not depending on the expected result.
113 result := errors.Is(test.err, test.target)
114 if result != test.wantMatch {
115 t.Errorf(
116 "%s: incorrect error identification -- got %v, want %v",
117 test.name, result, test.wantMatch,
118 )
119 continue
120 }
121 // Ensure the underlying error code can be unwrapped and is the expected
122 // code.
123 var kind ErrorKind
124 if !errors.As(test.err, &kind) {
125 t.Errorf("%s: unable to unwrap to error code", test.name)
126 continue
127 }
128 if kind != test.wantAs {
129 t.Errorf(
130 "%s: unexpected unwrapped error code -- got %v, want %v",
131 test.name, kind, test.wantAs,
132 )
133 continue
134 }
135 }
136 }
137