error_test.go raw
1 // Copyright (c) 2020-2022 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 ecdsa
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 {ErrSigTooShort, "ErrSigTooShort"},
19 {ErrSigTooLong, "ErrSigTooLong"},
20 {ErrSigInvalidSeqID, "ErrSigInvalidSeqID"},
21 {ErrSigInvalidDataLen, "ErrSigInvalidDataLen"},
22 {ErrSigMissingSTypeID, "ErrSigMissingSTypeID"},
23 {ErrSigMissingSLen, "ErrSigMissingSLen"},
24 {ErrSigInvalidSLen, "ErrSigInvalidSLen"},
25 {ErrSigInvalidRIntID, "ErrSigInvalidRIntID"},
26 {ErrSigZeroRLen, "ErrSigZeroRLen"},
27 {ErrSigNegativeR, "ErrSigNegativeR"},
28 {ErrSigTooMuchRPadding, "ErrSigTooMuchRPadding"},
29 {ErrSigRIsZero, "ErrSigRIsZero"},
30 {ErrSigRTooBig, "ErrSigRTooBig"},
31 {ErrSigInvalidSIntID, "ErrSigInvalidSIntID"},
32 {ErrSigZeroSLen, "ErrSigZeroSLen"},
33 {ErrSigNegativeS, "ErrSigNegativeS"},
34 {ErrSigTooMuchSPadding, "ErrSigTooMuchSPadding"},
35 {ErrSigSIsZero, "ErrSigSIsZero"},
36 {ErrSigSTooBig, "ErrSigSTooBig"},
37 {ErrSigInvalidLen, "ErrSigInvalidLen"},
38 {ErrSigInvalidRecoveryCode, "ErrSigInvalidRecoveryCode"},
39 {ErrSigOverflowsPrime, "ErrSigOverflowsPrime"},
40 {ErrPointNotOnCurve, "ErrPointNotOnCurve"},
41 }
42
43 for i, test := range tests {
44 result := test.in.Error()
45 if result != test.want {
46 t.Errorf("#%d: got: %s want: %s", i, result, test.want)
47 continue
48 }
49 }
50 }
51
52 // TestError tests the error output for the Error type.
53 func TestError(t *testing.T) {
54 tests := []struct {
55 in Error
56 want string
57 }{
58 {
59 Error{Description: "some error"},
60 "some error",
61 }, {
62 Error{Description: "human-readable error"},
63 "human-readable error",
64 },
65 }
66 for i, test := range tests {
67 result := test.in.Error()
68 if result != test.want {
69 t.Errorf("#%d: got: %s want: %s", i, result, test.want)
70 continue
71 }
72 }
73 }
74
75 // TestErrorKindIsAs ensures both ErrorKind and Error can be identified as being
76 // a specific error kind via errors.Is and unwrapped via errors.As.
77 func TestErrorKindIsAs(t *testing.T) {
78 tests := []struct {
79 name string
80 err error
81 target error
82 wantMatch bool
83 wantAs ErrorKind
84 }{
85 {
86 name: "ErrSigTooShort == ErrSigTooShort",
87 err: ErrSigTooShort,
88 target: ErrSigTooShort,
89 wantMatch: true,
90 wantAs: ErrSigTooShort,
91 }, {
92 name: "Error.ErrSigTooShort == ErrSigTooShort",
93 err: signatureError(ErrSigTooShort, ""),
94 target: ErrSigTooShort,
95 wantMatch: true,
96 wantAs: ErrSigTooShort,
97 }, {
98 name: "Error.ErrSigTooShort == Error.ErrSigTooShort",
99 err: signatureError(ErrSigTooShort, ""),
100 target: signatureError(ErrSigTooShort, ""),
101 wantMatch: true,
102 wantAs: ErrSigTooShort,
103 }, {
104 name: "ErrSigTooLong != ErrSigTooShort",
105 err: ErrSigTooLong,
106 target: ErrSigTooShort,
107 wantMatch: false,
108 wantAs: ErrSigTooLong,
109 }, {
110 name: "Error.ErrSigTooLong != ErrSigTooShort",
111 err: signatureError(ErrSigTooLong, ""),
112 target: ErrSigTooShort,
113 wantMatch: false,
114 wantAs: ErrSigTooLong,
115 }, {
116 name: "ErrSigTooLong != Error.ErrSigTooShort",
117 err: ErrSigTooLong,
118 target: signatureError(ErrSigTooShort, ""),
119 wantMatch: false,
120 wantAs: ErrSigTooLong,
121 }, {
122 name: "Error.ErrSigTooLong != Error.ErrSigTooShort",
123 err: signatureError(ErrSigTooLong, ""),
124 target: signatureError(ErrSigTooShort, ""),
125 wantMatch: false,
126 wantAs: ErrSigTooLong,
127 },
128 }
129 for _, test := range tests {
130 // Ensure the error matches or not depending on the expected result.
131 result := errors.Is(test.err, test.target)
132 if result != test.wantMatch {
133 t.Errorf(
134 "%s: incorrect error identification -- got %v, want %v",
135 test.name, result, test.wantMatch,
136 )
137 continue
138 }
139 // Ensure the underlying error kind can be unwrapped and is the
140 // expected code.
141 var kind ErrorKind
142 if !errors.As(test.err, &kind) {
143 t.Errorf("%s: unable to unwrap to error", test.name)
144 continue
145 }
146 if !errors.Is(kind, test.wantAs) {
147 t.Errorf(
148 "%s: unexpected unwrapped error -- got %v, want %v",
149 test.name, kind, test.wantAs,
150 )
151 continue
152 }
153 }
154 }
155