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
|
package certificate
import (
"errors"
"fmt"
"net"
"testing"
"github.com/smallstep/assert"
)
func TestTrimURL(t *testing.T) {
type newTest struct {
input, host string
isURL bool
err error
}
tests := map[string]newTest{
"true-http": {"https://smallstep.com", "smallstep.com", true, nil},
"true-tcp": {"tcp://smallstep.com:8080", "smallstep.com:8080", true, nil},
"true-tls": {"tls://smallstep.com/onboarding", "smallstep.com", true, nil},
"false": {"./certs/root_ca.crt", "", false, nil},
"false-err": {"https://google.com hello", "", false, errors.New("error parsing URL 'https://google.com hello'")},
"true-http-case": {"hTtPs://sMaLlStEp.cOm", "sMaLlStEp.cOm", true, nil},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
host, isURL, err := trimURL(tc.input)
assert.Equals(t, tc.host, host)
assert.Equals(t, tc.isURL, isURL)
if err != nil {
if assert.NotNil(t, tc.err) {
assert.HasPrefix(t, err.Error(), tc.err.Error())
}
} else {
assert.Nil(t, tc.err)
}
})
}
}
func TestGetPeerCertificateServerName(t *testing.T) {
host := "smallstep.com"
serverName := host
ips, err := net.LookupIP(host)
if err != nil {
t.Fatalf("unknown host %s: %s", host, err)
}
var addr string
for i, ip := range ips {
if ip.To4() != nil {
addr = ips[i].String()
break
}
}
if len(addr) == 0 {
assert.FatalError(t, errors.New("could not find ipv4 address for smallstep.com"))
return
}
type newTest struct {
addr, serverName string
err error
}
tests := map[string]newTest{
"sni-disabled-host": {host, "", nil},
"sni-enabled-host": {host, serverName, nil},
"sni-disabled-ip": {addr, "", fmt.Errorf("failed to connect: x509: cannot validate certificate for %s because it doesn't contain any IP SANs", addr)},
"sni-enabled-ip": {addr, serverName, nil},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
_, err := getPeerCertificates(tc.addr, tc.serverName, "", false)
if err != nil {
if assert.NotNil(t, tc.err) {
assert.HasPrefix(t, err.Error(), tc.err.Error())
}
} else {
assert.Nil(t, tc.err)
}
})
}
}
|