-
Notifications
You must be signed in to change notification settings - Fork 21
/
new_test.go
367 lines (332 loc) · 8.51 KB
/
new_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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package ovirtclient_test
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"net"
"net/http"
"strings"
"sync"
"testing"
ovirtclientlog "github.com/ovirt/go-ovirt-client-log/v3"
ovirtclient "github.com/ovirt/go-ovirt-client/v3"
)
func TestBadOVirtURL(t *testing.T) {
helper, err := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewTestLogger(t))
if err != nil {
t.Skipf("🚧 Skipping test: no live credentials provided.")
return
}
url := helper.GetClient().GetURL()
tls := helper.GetTLS()
username := helper.GetUsername()
password := helper.GetPassword()
logger := ovirtclientlog.NewTestLogger(t)
_, err = ovirtclient.New(
strings.TrimSuffix(strings.TrimSuffix(url, "/api"), "/api/"),
username,
password,
tls,
logger,
nil,
)
if err == nil {
t.Fatalf("Creating a connection to an endpoint not ending in /api did not result in an error.")
}
if !ovirtclient.HasErrorCode(err, ovirtclient.ENotAnOVirtEngine) {
t.Fatalf("Creating a connection to an endpoint not ending in /api has not correctly resulted in an ENotAnOVirtEngine (%v)", err)
}
}
func TestInvalidCredentials(t *testing.T) {
t.Parallel()
helper, err := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewTestLogger(t))
if err != nil {
t.Skipf("🚧 Skipping test: no live credentials provided.")
return
}
url := helper.GetClient().GetURL()
tls := helper.GetTLS()
logger := ovirtclientlog.NewTestLogger(t)
_, err = ovirtclient.New(
url,
"nonexistent@internal",
"invalid-password-for-testing-purposes",
tls,
logger,
nil,
)
if err == nil {
t.Fatal("no error returned from New on invalid credentials")
}
var e ovirtclient.EngineError
if errors.As(err, &e) {
if e.Code() != ovirtclient.EAccessDenied {
t.Fatalf("the returned error was not an EAccessDenied error (%v)", err)
}
} else {
t.Fatalf("the returned error was not an EngineError (%v)", err)
}
}
func TestBadURL(t *testing.T) {
t.Parallel()
logger := ovirtclientlog.NewTestLogger(t)
_, err := ovirtclient.New(
"https://example.com",
"nonexistent@internal",
"invalid-password-for-testing-purposes",
ovirtclient.TLS().Insecure(),
logger,
nil,
)
if err == nil {
t.Fatal("no error returned from New on invalid URL")
}
var e ovirtclient.EngineError
if errors.As(err, &e) {
if e.Code() != ovirtclient.ENotAnOVirtEngine {
t.Fatalf("the returned error was not an ENotAnOVirtEngine (%v)", err)
}
} else {
t.Fatalf("the returned error was not an EngineError (%v)", err)
}
}
func TestBadTLS(t *testing.T) {
t.Parallel()
// False CA is the CA we will give to the client
_, _, falseCACertBytes, err := createCA()
if err != nil {
t.Fatalf("failed to create false CA (%v)", err)
}
// Real CA is the CA we will use in the server
realCAPrivKey, realCACert, _, err := createCA()
if err != nil {
t.Fatalf("failed to create real CA (%v)", err)
}
serverPrivKey, serverCert, err := createSignedCert(
[]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
realCAPrivKey,
realCACert,
)
if err != nil {
t.Fatalf("failed to create server certificate (%v)", err)
}
port := getNextFreePort(t)
srv, err := newTestServer(t, port, serverCert, serverPrivKey, &noopHandler{})
if err != nil {
t.Fatal(err)
}
if err := srv.Start(); err != nil {
t.Fatal(err)
}
defer srv.Stop()
logger := ovirtclientlog.NewTestLogger(t)
_, err = ovirtclient.New(
fmt.Sprintf("https://127.0.0.1:%d", port),
"nonexistent@internal",
"invalid-password-for-testing-purposes",
ovirtclient.TLS().CACertsFromMemory(falseCACertBytes),
logger,
nil,
)
if err == nil {
t.Fatal("no error returned from New on invalid URL")
}
var e ovirtclient.EngineError
if errors.As(err, &e) {
if e.Code() != ovirtclient.ETLSError {
t.Fatalf("the returned error was not an ETLSError (%v)", err)
}
} else {
t.Fatalf("the returned error was not an EngineError (%v)", err)
}
}
func TestCredentialChangeAfterSetup(t *testing.T) {
t.Parallel()
// Real CA is the CA we will use in the server
realCAPrivKey, realCACert, realCABytes, err := createCA()
if err != nil {
t.Fatalf("failed to create real CA (%v)", err)
}
serverPrivKey, serverCert, err := createSignedCert(
[]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
realCAPrivKey,
realCACert,
)
if err != nil {
t.Fatalf("failed to create server certificate (%v)", err)
}
port := getNextFreePort(t)
srv, err := newTestServer(t, port, serverCert, serverPrivKey, &unauthorizedHandler{})
if err != nil {
t.Fatal(err)
}
if err := srv.Start(); err != nil {
t.Fatal(err)
}
defer srv.Stop()
logger := ovirtclientlog.NewTestLogger(t)
conn, err := ovirtclient.NewWithVerify(
fmt.Sprintf("https://127.0.0.1:%d", port),
"nonexistent@internal",
"invalid-password-for-testing-purposes",
ovirtclient.TLS().CACertsFromMemory(realCABytes),
logger,
nil,
func(connection ovirtclient.Client) error {
// Disable connection check on setup to simulate a credential shift after the connection
// has been established.
return nil
},
)
if err != nil {
t.Fatalf("failed to set up connection (%v)", err)
}
_, err = conn.ListStorageDomains()
if err == nil {
t.Fatalf("listing storage domains did not result in an error")
}
var e ovirtclient.EngineError
if errors.As(err, &e) {
if e.Code() != ovirtclient.EAccessDenied {
t.Fatalf("the returned error was not an EAccessDenied (%v)", err)
}
} else {
t.Fatalf("the returned error was not an EngineError (%v)", err)
}
}
func TestProxy(t *testing.T) {
counter := 0
proxy := startProxyServer(t, &counter)
t.Parallel()
// Real CA is the CA we will use in the server
realCAPrivKey, realCACert, realCABytes, err := createCA()
if err != nil {
t.Fatalf("failed to create real CA (%v)", err)
}
serverPrivKey, serverCert, err := createSignedCert(
[]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
realCAPrivKey,
realCACert,
)
if err != nil {
t.Fatalf("failed to create server certificate (%v)", err)
}
port := getNextFreePort(t)
srv, err := newTestServer(t, port, serverCert, serverPrivKey, &unauthorizedHandler{})
if err != nil {
t.Fatal(err)
}
if err := srv.Start(); err != nil {
t.Fatal(err)
}
t.Cleanup(srv.Stop)
logger := ovirtclientlog.NewTestLogger(t)
_, err = ovirtclient.New(
fmt.Sprintf("https://127.0.0.1:%d", port),
"admin@internal",
"asdf",
ovirtclient.TLS().CACertsFromMemory(realCABytes),
logger,
ovirtclient.NewExtraSettings().WithProxy(fmt.Sprintf("http://%s", proxy)),
)
if err == nil {
t.Fatalf("No error on establishing a connection (%v)", err)
}
if !ovirtclient.HasErrorCode(err, ovirtclient.EAccessDenied) {
t.Fatalf("incorrect error code returned: %v", err)
}
if counter == 0 {
t.Fatalf("Connection did not go through the proxy.")
}
}
func startProxyServer(t *testing.T, counter *int) string {
wg := sync.WaitGroup{}
wg.Add(1)
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("failed to open listen socket for the proxy (%v)", err)
}
var serverError error
//nolint:gosec
srv := http.Server{
Addr: ln.Addr().String(),
// Disable HTTP/2.
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
srv.Handler = &proxyHandler{counter: counter}
go func() {
defer wg.Done()
if err := srv.Serve(ln); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
serverError = err
}
}
}()
t.Cleanup(
func() {
if err := srv.Close(); err != nil {
t.Fatalf("failed to close listen socket (%v)", err)
}
wg.Wait()
if serverError != nil {
t.Fatalf("proxy server stopped unexpectedly (%v)", err)
}
})
return ln.Addr().String()
}
type proxyHandler struct {
counter *int
}
func (p *proxyHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
*p.counter++
if request.Method != "CONNECT" {
writer.WriteHeader(400)
return
}
target := request.URL.Host
hijacker, ok := writer.(http.Hijacker)
if !ok {
writer.WriteHeader(500)
return
}
backendConn, err := net.Dial("tcp", target)
if err != nil {
writer.WriteHeader(500)
return
}
writer.WriteHeader(200)
conn, clientBuf, err := hijacker.Hijack()
if err != nil {
return
}
if clientBuf != nil {
bytes := clientBuf.Reader.Buffered()
buf := make([]byte, bytes)
n, err := clientBuf.Read(buf)
if err != nil {
return
}
if n < bytes {
// This is a bug
return
}
if len(buf) > 0 {
_, _ = backendConn.Write(buf)
}
}
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
_, _ = io.Copy(backendConn, conn)
_ = backendConn.Close()
}()
go func() {
defer wg.Done()
_, _ = io.Copy(conn, backendConn)
_ = conn.Close()
}()
wg.Wait()
}