-
Notifications
You must be signed in to change notification settings - Fork 15
/
client_tls_test.go
110 lines (91 loc) · 3.56 KB
/
client_tls_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
// Copyright 2021-2024 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package restful
import (
"context"
"crypto/tls"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestHTTPS(t *testing.T) {
assert := assert.New(t)
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
cert, err := tls.LoadX509KeyPair("test_certs/tls.crt", "test_certs/tls.key")
assert.Nil(err)
srv.TLS.Certificates = []tls.Certificate{cert}
srv.URL = strings.ReplaceAll(srv.URL, "127.0.0.1", "localhost")
defer srv.Close()
client := NewClient().Root(srv.URL).TLSRootCerts("test_certs", false).HTTPS(nil)
err = client.Get(context.Background(), "/NEF", nil)
assert.Nil(err)
}
func TestHTTPSMTLS(t *testing.T) {
assert := assert.New(t)
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
cert, err := tls.LoadX509KeyPair("test_certs/tls.crt", "test_certs/tls.key")
assert.Nil(err)
srv.TLS.Certificates = []tls.Certificate{cert}
srv.TLS.ClientCAs = NewCertPool("test_certs", true)
srv.TLS.ClientAuth = tls.RequireAndVerifyClientCert
srv.URL = strings.ReplaceAll(srv.URL, "127.0.0.1", "localhost")
defer srv.Close()
assert.NoError(NewClient().Root(srv.URL).TLSRootCerts("test_certs", false).TLSOwnCerts("test_certs").Get(context.Background(), "/NEF", nil)) // Own cert set
assert.Error(NewClient().Root(srv.URL).TLSRootCerts("test_certs", false).Get(context.Background(), "/NEF", nil)) // Own cert not set
}
func TestHTTPSMTLSServer(t *testing.T) {
assert := assert.New(t)
OwnTLSCert = "test_certs/tls.crt"
OwnTLSKey = "test_certs/tls.key"
ClientCAs = "test_certs"
HandleFunc("/NEF", func() {})
go StartTLS(false, true, false)
time.Sleep(10 * time.Millisecond)
assert.NoError(NewClient().Root("https://127.0.0.1:8443").TLSRootCerts("test_certs", false).TLSOwnCerts("test_certs").Get(context.Background(), "/NEF", nil)) // Own cert set
assert.Error(NewClient().Root("https://127.0.0.1:8443").TLSRootCerts("test_certs", false).Get(context.Background(), "/NEF", nil)) // Own cert not set
}
func TestHTTPSInsecure(t *testing.T) {
assert := assert.New(t)
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal("big/nothing", r.Header.Get("Content-type"))
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
client := NewClient().Root(srv.URL).Insecure()
headers := make(http.Header)
headers.Set(ContentTypeHeader, "big/nothing")
_, err := client.SendRecv(context.Background(), http.MethodGet, "/NEF", headers, nil, nil)
assert.Nil(err)
}
func TestHTTPSCertFail(t *testing.T) {
assert := assert.New(t)
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
client := NewH2Client().Root(srv.URL)
assert.Equal("h2", client.Kind)
client.TLS(nil)
client.TLS(&tls.Config{})
client.TLSRootCerts("", false)
client.TLSRootCerts("/nonexisting", false)
client.TLSRootCerts(".", false) // finds ./test_certs/
client.TLSOwnCerts("/nonexisting")
client.TLSOwnCerts("./test_certs")
err := client.Get(context.Background(), "/NEF", nil)
assert.NotNil(err)
_, err = client.SendRecv(context.Background(), http.MethodGet, "/NEF", nil, nil, nil)
assert.NotNil(err)
}
func TestAppendCert(t *testing.T) {
appendCert("kutyafüle", nil)
appendCert("client_tls_test.go", nil)
}