forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_priv_pkg_test.go
More file actions
72 lines (62 loc) · 2.08 KB
/
client_priv_pkg_test.go
File metadata and controls
72 lines (62 loc) · 2.08 KB
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
package bitbucket
import (
"crypto/tls"
"encoding/pem"
"fmt"
"net"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// fetchCACertsForTest connects to the given host:port and returns the CA certs in PEM format.
// This is a test-local copy to avoid an import cycle with the tests package.
func fetchCACertsForTest(host string, port string) ([]byte, error) {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
conn, err := tls.DialWithDialer(&net.Dialer{Timeout: 5 * time.Second}, "tcp", net.JoinHostPort(host, port), tlsConfig)
if err != nil {
return nil, fmt.Errorf("failed to connect: %w", err)
}
defer func() { _ = conn.Close() }()
peerCerts := conn.ConnectionState().PeerCertificates
if len(peerCerts) == 0 {
return nil, fmt.Errorf("no certificates found")
}
var pemData []byte
for _, cert := range peerCerts {
block := &pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
}
pemData = append(pemData, pem.EncodeToMemory(block)...)
}
return pemData, nil
}
func TestAppendCaCerts_util_test(t *testing.T) {
if testing.Short() {
t.Skip("skipping test requiring network access")
}
caCerts, err := fetchCACertsForTest("bitbucket.org", "443")
if err != nil {
t.Fatalf("Error fetching CA certs using `fetchCACertsForTest`: %v", err)
}
httpClient, err := appendCaCerts(caCerts)
require.NoError(t, err)
require.NotNil(t, httpClient)
transport, ok := httpClient.Transport.(*http.Transport)
require.True(t, ok, "Transport should be *http.Transport")
require.NotNil(t, transport.TLSClientConfig, "TLSClientConfig should be set")
assert.NotNil(t, transport.TLSClientConfig.RootCAs, "RootCAs cert pool should be set")
assert.Equal(t, uint16(tls.VersionTLS12), transport.TLSClientConfig.MinVersion, "MinVersion should be TLS 1.2")
}
func TestAppendCaCerts_InvalidCert(t *testing.T) {
t.Parallel()
invalidPEM := []byte("this is not a valid PEM certificate")
httpClient, err := appendCaCerts(invalidPEM)
assert.Error(t, err)
assert.Nil(t, httpClient)
assert.Contains(t, err.Error(), "unable to append CA Certs")
}