Skip to content

Commit 0ed140e

Browse files
chrischdidougm
authored andcommitted
Fix error assertion in IsCertificateUntrusted
1 parent 888548b commit 0ed140e

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

vim25/soap/error.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ func IsCertificateUntrusted(err error) bool {
121121
// golang 1.20 introduce a new type to wrap 509 errors. So instead of
122122
// casting the type, now we check the error chain contains the
123123
// x509 error or not.
124-
if errors.Is(err, &x509.UnknownAuthorityError{}) {
124+
if errors.As(err, &x509.UnknownAuthorityError{}) {
125125
return true
126126
}
127127

128-
if errors.Is(err, &x509.HostnameError{}) {
128+
if errors.As(err, &x509.HostnameError{}) {
129129
return true
130130
}
131131

vim25/soap/error_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package soap
18+
19+
import (
20+
"crypto/tls"
21+
"crypto/x509"
22+
"testing"
23+
)
24+
25+
func TestIsCertificateUntrusted(t *testing.T) {
26+
type args struct {
27+
}
28+
tests := []struct {
29+
name string
30+
err error
31+
want bool
32+
}{
33+
{
34+
name: "tls.CertificateVerificationError",
35+
err: x509.HostnameError{
36+
Certificate: &x509.Certificate{},
37+
Host: "1.2.3.4",
38+
},
39+
want: true,
40+
},
41+
{
42+
name: "tls.CertificateVerificationError",
43+
err: &tls.CertificateVerificationError{
44+
UnverifiedCertificates: []*x509.Certificate{
45+
&x509.Certificate{},
46+
},
47+
Err: x509.HostnameError{
48+
Certificate: &x509.Certificate{},
49+
Host: "5.6.7.8",
50+
},
51+
},
52+
want: true,
53+
},
54+
}
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
if got := IsCertificateUntrusted(tt.err); got != tt.want {
58+
t.Errorf("IsCertificateUntrusted() = %v, want %v", got, tt.want)
59+
}
60+
})
61+
}
62+
}

0 commit comments

Comments
 (0)