This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 213
/
client_test.go
235 lines (222 loc) · 6.7 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package rabbitmq
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/compose/transporter/client"
)
const rootPEM = `
-----BEGIN CERTIFICATE-----
MIIEBDCCAuygAwIBAgIDAjppMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT
MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i
YWwgQ0EwHhcNMTMwNDA1MTUxNTU1WhcNMTUwNDA0MTUxNTU1WjBJMQswCQYDVQQG
EwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzElMCMGA1UEAxMcR29vZ2xlIEludGVy
bmV0IEF1dGhvcml0eSBHMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AJwqBHdc2FCROgajguDYUEi8iT/xGXAaiEZ+4I/F8YnOIe5a/mENtzJEiaB0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509.CertPool {
pool := x509.NewCertPool()
pool.AppendCertsFromPEM([]byte(rootPEM))
return pool
}
clientTests = []struct {
name string
options []ClientOptionFunc // input
expected *Client // expected result
expectedErr error // expected error
}{
{
"default_client",
make([]ClientOptionFunc, 0),
defaultClient,
nil,
},
{
"with_url_fake",
[]ClientOptionFunc{WithURI(DefaultURI)},
&Client{
uri: DefaultURI,
},
nil,
},
{
"with_uri_invalid",
[]ClientOptionFunc{WithURI("amqp:// localhost:5672")},
&Client{},
client.InvalidURIError{URI: "amqp:// localhost:5672", Err: "Invalid URI must not contain whitespace"},
},
{
"with_ssl",
[]ClientOptionFunc{WithSSL(true)},
&Client{
uri: DefaultURI,
tlsConfig: &tls.Config{InsecureSkipVerify: true, RootCAs: x509.NewCertPool()},
},
nil,
},
{
"with_ssl_with_certs",
[]ClientOptionFunc{WithSSL(true), WithCACerts([]string{rootPEM})},
&Client{
uri: DefaultURI,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
},
nil,
},
{
"with_ssl_with_cert_file",
[]ClientOptionFunc{WithSSL(true), WithCACerts([]string{"testdata/ca.pem"})},
&Client{
uri: DefaultURI,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
},
nil,
},
{
"with_ssl_with_cert_file_permission_denied",
[]ClientOptionFunc{WithSSL(true), WithCACerts([]string{"testdata/ca_no_perms.pem"})},
&Client{
uri: DefaultURI,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
},
&os.PathError{Op: "open", Path: "testdata/ca_no_perms.pem", Err: os.ErrPermission},
},
{
"with_certs",
[]ClientOptionFunc{WithCACerts([]string{rootPEM})},
&Client{
uri: DefaultURI,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
},
nil,
},
{
"with_certs_invalid",
[]ClientOptionFunc{WithCACerts([]string{"notacert"})},
&Client{
uri: DefaultURI,
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: certPool()},
},
client.ErrInvalidCert,
},
}
)
func TestNewClient(t *testing.T) {
os.Chmod("testdata/ca_no_perms.pem", 0222)
defer os.Chmod("testdata/ca_no_perms.pem", 0644)
for _, ct := range clientTests {
actual, err := NewClient(ct.options...)
if ct.expectedErr != nil && !reflect.DeepEqual(err.Error(), ct.expectedErr.Error()) {
t.Fatalf("[%s] unexpected NewClient error, expected %+v, got %+v\n", ct.name, ct.expectedErr, err)
}
if err == nil {
if ct.expected.uri != actual.uri {
t.Errorf("[%s] Client uri mismatch\nexpected %+v\ngot %+v", ct.name, ct.expected.uri, actual.uri)
}
if ct.expected.tlsConfig != nil && !reflect.DeepEqual(ct.expected.tlsConfig.InsecureSkipVerify, actual.tlsConfig.InsecureSkipVerify) {
t.Errorf("[%s] Client uri mismatch\nexpected %+v\ngot %+v", ct.name, ct.expected.tlsConfig.InsecureSkipVerify, actual.tlsConfig.InsecureSkipVerify)
}
if ct.expected.tlsConfig != nil && !reflect.DeepEqual(ct.expected.tlsConfig.RootCAs, actual.tlsConfig.RootCAs) {
t.Errorf("[%s] Client uri mismatch\nexpected %+v\ngot %+v", ct.name, ct.expected.tlsConfig.RootCAs, actual.tlsConfig.RootCAs)
}
// && !reflect.DeepEqual(ct.expected, actual) {
// t.Errorf(actual.tlsConfig)
}
}
}
var (
caCertPool = func() *x509.CertPool {
pool := x509.NewCertPool()
c, _ := ioutil.ReadFile("/tmp/rabbitmq/ca.crt")
pool.AppendCertsFromPEM(c)
return pool
}
caBadCertPool = func() *x509.CertPool {
pool := x509.NewCertPool()
c, _ := ioutil.ReadFile("/tmp/rabbitmq_bad_cert/ca.crt")
pool.AppendCertsFromPEM(c)
return pool
}
connectTests = []struct {
name string
client *Client
expectedErr error
}{
{
"default connect",
&Client{
uri: DefaultURI,
},
nil,
},
{
"failed authenticated connect",
&Client{
uri: "amqp://guest:[email protected]:5672/",
},
client.ConnectError{Reason: `Exception (403) Reason: "username or password not allowed"`},
},
{
"connect with ssl and verify",
&Client{
uri: "amqps://localhost:5000",
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: caCertPool()},
},
nil,
},
{
"connect with ssl skip verify",
&Client{
uri: "amqps://localhost:5000",
tlsConfig: &tls.Config{InsecureSkipVerify: true, RootCAs: x509.NewCertPool()},
},
nil,
},
{
"connect with bad ssl cert",
&Client{
uri: "amqps://localhost:5000",
tlsConfig: &tls.Config{InsecureSkipVerify: false, RootCAs: caBadCertPool()},
},
client.ConnectError{Reason: "x509: certificate signed by unknown authority"},
},
}
)
func TestConnect(t *testing.T) {
if testing.Short() {
t.Skip("skipping Connect in short mode")
}
for _, ct := range connectTests {
_, err := ct.client.Connect()
if err != ct.expectedErr {
t.Fatalf("[%s] unexpected Connect error, expected %+v, got %+v\n", ct.name, ct.expectedErr, err)
}
if err == nil {
ct.client.Close()
}
}
}