-
Notifications
You must be signed in to change notification settings - Fork 589
/
http_client_test.go
236 lines (212 loc) · 5.58 KB
/
http_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
236
package gmtls
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/tjfoc/gmsm/x509"
)
var _ExpectRawContent = []byte("Hello World!")
// 启动HTTP测试服务器
func bootHttpServer(t *testing.T) {
serveMux := http.NewServeMux()
serveMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write(_ExpectRawContent)
})
fmt.Println(">> HTTP :50053 running...")
err := http.ListenAndServe(":50053", serveMux)
if err != nil {
t.Fatal(err)
}
}
// 启动GM HTTPS测试服务器
func bootGMHTTPSServer(t *testing.T) {
sigCert, err := LoadX509KeyPair(
"websvr/certs/sm2_sign_cert.cer",
"websvr/certs/sm2_sign_key.pem")
if err != nil {
t.Fatal(err)
}
encCert, err := LoadX509KeyPair(
"websvr/certs/sm2_enc_cert.cer",
"websvr/certs/sm2_enc_key.pem")
if err != nil {
t.Fatal(err)
}
config := &Config{
GMSupport: &GMSupport{},
Certificates: []Certificate{sigCert, encCert},
InsecureSkipVerify: true,
}
if err != nil {
panic(err)
}
ln, err := Listen("tcp", ":50054", config)
if err != nil {
t.Fatal(err)
}
defer ln.Close()
serveMux := http.NewServeMux()
serveMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write(_ExpectRawContent)
})
fmt.Println(">> HTTP :50054 [GMSSL] running...")
err = http.Serve(ln, serveMux)
if err != nil {
t.Fatal(err)
}
}
// 启动GM HTTPS测试服务器 双向身份认证
func bootGMAuthHTTPSServer(t *testing.T) {
sigCert, err := LoadX509KeyPair(
"websvr/certs/sm2_sign_cert.cer",
"websvr/certs/sm2_sign_key.pem")
if err != nil {
t.Fatal(err)
}
encCert, err := LoadX509KeyPair(
"websvr/certs/sm2_enc_cert.cer",
"websvr/certs/sm2_enc_key.pem")
if err != nil {
t.Fatal(err)
}
certPool := x509.NewCertPool()
cacert, err := ioutil.ReadFile("websvr/certs/SM2_CA.cer")
if err != nil {
t.Fatal(err)
}
certPool.AppendCertsFromPEM(cacert)
config := &Config{
GMSupport: &GMSupport{},
Certificates: []Certificate{sigCert, encCert},
ClientAuth: RequireAndVerifyClientCert,
ClientCAs: certPool,
}
if err != nil {
panic(err)
}
ln, err := Listen("tcp", ":50055", config)
if err != nil {
t.Fatal(err)
}
defer ln.Close()
serveMux := http.NewServeMux()
serveMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write(_ExpectRawContent)
})
fmt.Println(">> HTTP :50055 [GMSSL] Client Auth running...")
err = http.Serve(ln, serveMux)
if err != nil {
t.Fatal(err)
}
}
// HTTP 客户端连接测试
func TestSimpleNewHTTPSClient1(t *testing.T) {
go bootHttpServer(t)
//go bootGMHTTPSServer(t)
/*
HTTP 连接测试
*/
time.Sleep(time.Second)
httpClient := NewCustomHTTPSClient(nil)
response, err := httpClient.Get("http://localhost:50053")
if err != nil {
t.Fatal(err)
}
defer response.Body.Close()
raw, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(raw, _ExpectRawContent) {
t.Fatalf(">> HTTP响应内容与期待内容不符, expect %s, actual: %s", string(_ExpectRawContent), string(raw))
}
}
// GM HTTPS 客户端连接测试
func TestNewHTTPSClient2(t *testing.T) {
go bootGMHTTPSServer(t)
/*
GM HTTPS 连接测试
*/
time.Sleep(time.Second)
// 信任的根证书
config, err := createClientGMTLSConfig("", "", []string{"websvr/certs/SM2_CA.cer"})
if err != nil {
t.Fatal(err)
}
httpClient := NewCustomHTTPSClient(config)
response, err := httpClient.Get("https://localhost:50054")
if err != nil {
t.Fatal(err)
}
defer response.Body.Close()
raw, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(raw, _ExpectRawContent) {
t.Fatalf(">> GM HTTPS响应内容与期待内容不符, expect %s, actual: %s", string(_ExpectRawContent), string(raw))
}
}
// GM HTTPS 客户端连接测试 双向身份认证
func TestNewHTTPSClient3(t *testing.T) {
go bootGMAuthHTTPSServer(t)
/*
GM HTTPS 双向身份认证
*/
time.Sleep(time.Second)
config, err := createClientGMTLSConfig("websvr/certs/sm2_auth_key.pem", "websvr/certs/sm2_auth_cert.cer", []string{"websvr/certs/SM2_CA.cer"})
if err != nil {
t.Fatal(err)
}
httpClient := NewCustomHTTPSClient(config)
response, err := httpClient.Get("https://localhost:50055")
if err != nil {
t.Fatal(err)
}
defer response.Body.Close()
raw, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(raw, _ExpectRawContent) {
t.Fatalf(">> GM HTTPS响应内容与期待内容不符, expect %s, actual: %s", string(_ExpectRawContent), string(raw))
}
}
func createClientGMTLSConfig(keyPath string, certPath string, caPaths []string) (*Config, error) {
cfg := &Config{
GMSupport: &GMSupport{},
}
cfg.Certificates = []Certificate{}
if keyPath != "" && certPath != "" {
cert, err := LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, fmt.Errorf("load gm X509 keyPair error: %v", err)
}
cfg.Certificates = append(cfg.Certificates, cert)
}
var pool *x509.CertPool = nil
if len(caPaths) > 0 {
pool = x509.NewCertPool()
for _, certPath := range caPaths {
caCrt, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, err
}
ok := pool.AppendCertsFromPEM(caCrt)
if !ok {
return nil, fmt.Errorf("append cert to pool fail at %s", certPath)
}
}
}
cfg.MinVersion = VersionGMSSL
cfg.MaxVersion = VersionTLS12
cfg.PreferServerCipherSuites = true
// cfg.CipherSuites use default value []uint16{GMTLS_SM2_WITH_SM4_SM3, GMTLS_ECDHE_SM2_WITH_SM4_SM3}
cfg.RootCAs = pool
// cfg.ServerName = "localhost"
cfg.InsecureSkipVerify = false
return cfg, nil
}