-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathfactory_config_test.go
171 lines (145 loc) · 5.8 KB
/
factory_config_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
package uaa_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/cloudfoundry/bosh-cli/v7/uaa"
)
var _ = Describe("NewConfigFromURL", func() {
It("sets host and port (443) if scheme is specified", func() {
config, err := NewConfigFromURL("https://host")
Expect(err).ToNot(HaveOccurred())
Expect(config).To(Equal(Config{Host: "host", Port: 443}))
})
It("sets host and port (443) if scheme is not specified", func() {
config, err := NewConfigFromURL("host")
Expect(err).ToNot(HaveOccurred())
Expect(config).To(Equal(Config{Host: "host", Port: 443}))
})
It("extracts port if scheme is specified", func() {
config, err := NewConfigFromURL("https://host:4443")
Expect(err).ToNot(HaveOccurred())
Expect(config).To(Equal(Config{Host: "host", Port: 4443}))
})
It("extracts port if scheme is not specified", func() {
config, err := NewConfigFromURL("host:4443")
Expect(err).ToNot(HaveOccurred())
Expect(config).To(Equal(Config{Host: "host", Port: 4443}))
})
It("extracts path if path is provided", func() {
config, err := NewConfigFromURL("httsp://host:4443/zakrules")
Expect(err).ToNot(HaveOccurred())
Expect(config).To(Equal(Config{Host: "host", Port: 4443, Path: "/zakrules"}))
})
It("works with ipv6 hosts", func() {
config, err := NewConfigFromURL("https://[2600:1f17:a63:5c00:5a20:7eec:cf9:e31f]:4443")
Expect(err).ToNot(HaveOccurred())
Expect(config).To(Equal(Config{Host: "2600:1f17:a63:5c00:5a20:7eec:cf9:e31f", Port: 4443}))
})
It("returns error if url is empty", func() {
_, err := NewConfigFromURL("")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Expected non-empty UAA URL"))
})
It("returns error if host is not specified", func() {
_, err := NewConfigFromURL("https://:443")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Expected to extract host from"))
})
It("returns error if parsing url fails", func() {
_, err := NewConfigFromURL(":/")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Parsing UAA URL"))
})
It("returns error if port cannot be extracted", func() {
_, err := NewConfigFromURL("https://host::")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Extracting host/port from URL"))
})
It("returns error if port is empty", func() {
_, err := NewConfigFromURL("host:")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Extracting port from URL"))
})
It("returns error if port cannot be parsed as int", func() {
_, err := NewConfigFromURL("https://host:abc")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(MatchRegexp("(Extracting port from URL|Parsing UAA URL)")) // go1.12 validates port in Parse
})
})
var _ = Describe("FactoryConfig", func() {
Describe("Validate", func() {
It("returns without error for basic config", func() {
err := Config{Host: "host", Port: 1, Client: "client"}.Validate()
Expect(err).ToNot(HaveOccurred())
})
It("returns error if host is empty", func() {
err := Config{}.Validate()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("Missing 'Host'"))
})
It("returns error if host is empty", func() {
err := Config{Host: "host"}.Validate()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("Missing 'Port'"))
})
It("returns error if client is empty", func() {
err := Config{Host: "host", Port: 1}.Validate()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("Missing 'Client'"))
})
It("returns error if cannot parse PEM formatted block", func() {
err := Config{
Host: "host",
Port: 1,
Client: "client",
CACert: "-",
}.Validate()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Parsing certificate 1: Missing PEM block"))
})
})
Describe("CACertPool", func() {
It("returns error if cannot parse PEM formatted block", func() {
_, err := Config{
Host: "host",
Port: 1,
Client: "client",
CACert: "-",
}.CACertPool()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Parsing certificate 1: Missing PEM block"))
})
It("does not create a cert pool from an empty string", func() {
caCert := ``
certPool, err := Config{CACert: caCert}.CACertPool()
Expect(err).ToNot(HaveOccurred())
Expect(certPool).To(BeNil())
})
It("parses the certificate", func() {
caCert := `-----BEGIN CERTIFICATE-----
MIIDXzCCAkegAwIBAgIJAPerMgLAne5vMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwIBcNMTYwMTE2MDY0NTA0WhgPMjI4OTEwMzAwNjQ1MDRa
MEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJ
bnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw
ggEKAoIBAQCtSo3KPjnVPzodb6+mNwbCdcpzVop8OmfwJ3ynQtyBEzGaKsAn4tlz
/wfQQrKFHgxqVpqcoxAlWPNMs5+iO2Jst3Gz2+oLcaDyz/EWorw0iF5q1F6+WYHp
EijY20MzaWYMyu4UhhlbJCkSGZSjujh5SFOAXQwWYJXsqjyxA9KaTD6OdH5Kpger
B9D4zogX0We00eouyvvz/sAeDbTshk9sJRGWHNFJr+TjVx2D01alU49liAL94yF6
1eEOEbE50OAhv9RNsRh6O58idaHg30bbMf1yAzcgBvh8CzIHH0BPofoF2pRfztoY
uudZ0ftJjTz4fA2h/7GOVzxemrTjx88vAgMBAAGjUDBOMB0GA1UdDgQWBBQjz5Q2
YW2kBTb4XLqKFZMSBLpi6zAfBgNVHSMEGDAWgBQjz5Q2YW2kBTb4XLqKFZMSBLpi
6zAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4IBAQA/s94M/mSGELHJWIb1
oE0IKHWajBd3Pc8+O1TZRE+ke3q+rZRfcxd2dAjq6zQHJUs2+fs0B3DyT9Wtyyoq
UrRdsgprOdf2Cuw8bMIsCQOvqWKhhdlLTnCi2xaGJawGsIkheuD1n+Il9gRQ2WGy
lACxVngPwjNYxjOE+CUnSZCuAmAfQYzqto3bNPqkgEwb7ueODeOiyhR8SKsH7ySW
QAOSxgrLBblGLWcDF9fjMeYaUnI34pHviCKeVxfgsxDR+Jg11F78sPdYLOF6ipBe
/5qTYucsY20B2EKtlscD0mSYBRwbVrSQt2RYbTCwaibxWUC13VV+YEk0NAv9Mm04
6sKO
-----END CERTIFICATE-----`
certPool, err := Config{CACert: caCert}.CACertPool()
Expect(err).ToNot(HaveOccurred())
Expect(certPool.Subjects()[0]).To(ContainSubstring("Internet Widgits Pty Ltd")) //nolint:staticcheck
})
})
})