Debian Codegraph @

errors_test.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package notation
15
16 import "testing"
17
18 func TestErrorMessages(t *testing.T) {
19 tests := []struct {
20 name string
21 err error
22 want string
23 }{
24 {
25 name: "ErrorPushSignatureFailed with message",
26 err: ErrorPushSignatureFailed{Msg: "test message"},
27 want: "failed to push signature to registry with error: test message",
28 },
29 {
30 name: "ErrorPushSignatureFailed without message",
31 err: ErrorPushSignatureFailed{},
32 want: "failed to push signature to registry",
33 },
34 {
35 name: "ErrorVerificationInconclusive with message",
36 err: ErrorVerificationInconclusive{Msg: "test message"},
37 want: "test message",
38 },
39 {
40 name: "ErrorVerificationInconclusive without message",
41 err: ErrorVerificationInconclusive{},
42 want: "signature verification was inclusive due to an unexpected error",
43 },
44 {
45 name: "ErrorNoApplicableTrustPolicy with message",
46 err: ErrorNoApplicableTrustPolicy{Msg: "test message"},
47 want: "test message",
48 },
49 {
50 name: "ErrorNoApplicableTrustPolicy without message",
51 err: ErrorNoApplicableTrustPolicy{},
52 want: "there is no applicable trust policy for the given artifact",
53 },
54 {
55 name: "ErrorSignatureRetrievalFailed with message",
56 err: ErrorSignatureRetrievalFailed{Msg: "test message"},
57 want: "test message",
58 },
59 {
60 name: "ErrorSignatureRetrievalFailed without message",
61 err: ErrorSignatureRetrievalFailed{},
62 want: "unable to retrieve the digital signature from the registry",
63 },
64 {
65 name: "ErrorVerificationFailed with message",
66 err: ErrorVerificationFailed{Msg: "test message"},
67 want: "test message",
68 },
69 {
70 name: "ErrorVerificationFailed without message",
71 err: ErrorVerificationFailed{},
72 want: "signature verification failed",
73 },
74 {
75 name: "ErrorUserMetadataVerificationFailed with message",
76 err: ErrorUserMetadataVerificationFailed{Msg: "test message"},
77 want: "test message",
78 },
79 {
80 name: "ErrorUserMetadataVerificationFailed without message",
81 err: ErrorUserMetadataVerificationFailed{},
82 want: "unable to find specified metadata in the signature",
83 },
84 }
85
86 for _, tt := range tests {
87 t.Run(tt.name, func(t *testing.T) {
88 if got := tt.err.Error(); got != tt.want {
89 t.Errorf("Error() = %v, want %v", got, tt.want)
90 }
91 })
92 }
93 }
94
95 func TestCustomErrorPrintsCorrectMessage(t *testing.T) {
96 tests := []struct {
97 name string
98 err error
99 want string
100 }{
101 {
102 name: "PushSignatureFailedError with message",
103 err: PushSignatureFailedError{Msg: "test message"},
104 want: "failed to push signature to registry with error: test message",
105 },
106 {
107 name: "PushSignatureFailedError without message",
108 err: PushSignatureFailedError{},
109 want: "failed to push signature to registry",
110 },
111 {
112 name: "VerificationInconclusiveError with message",
113 err: VerificationInconclusiveError{Msg: "test message"},
114 want: "test message",
115 },
116 {
117 name: "VerificationInconclusiveError without message",
118 err: VerificationInconclusiveError{},
119 want: "signature verification was inclusive due to an unexpected error",
120 },
121 {
122 name: "NoApplicableTrustPolicyError with message",
123 err: NoApplicableTrustPolicyError{Msg: "test message"},
124 want: "test message",
125 },
126 {
127 name: "NoApplicableTrustPolicyError without message",
128 err: NoApplicableTrustPolicyError{},
129 want: "there is no applicable trust policy for the given artifact",
130 },
131 {
132 name: "SignatureRetrievalFailedError with message",
133 err: SignatureRetrievalFailedError{Msg: "test message"},
134 want: "test message",
135 },
136 {
137 name: "SignatureRetrievalFailedError without message",
138 err: SignatureRetrievalFailedError{},
139 want: "unable to retrieve the digital signature from the registry",
140 },
141 {
142 name: "VerificationFailedError with message",
143 err: VerificationFailedError{Msg: "test message"},
144 want: "test message",
145 },
146 {
147 name: "VerificationFailedError without message",
148 err: VerificationFailedError{},
149 want: "signature verification failed",
150 },
151 {
152 name: "UserMetadataVerificationFailedError with message",
153 err: UserMetadataVerificationFailedError{Msg: "test message"},
154 want: "test message",
155 },
156 {
157 name: "UserMetadataVerificationFailedError without message",
158 err: UserMetadataVerificationFailedError{},
159 want: "unable to find specified metadata in the signature",
160 },
161 }
162
163 for _, tt := range tests {
164 t.Run(tt.name, func(t *testing.T) {
165 if got := tt.err.Error(); got != tt.want {
166 t.Errorf("Error() = %v, want %v", got, tt.want)
167 }
168 })
169 }
170 }