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
|
package bundler
// This test file contains tests on checking the correctness of BundleFromRemote
import (
"net"
"strings"
"testing"
"github.com/cloudflare/cfssl/ubiquity"
)
// remoteTest defines a test case for BundleFromRemote. Hostname and ip are the test inputs.
// bundlerConstructor points the bundler ctor and errorCallback handles the error checking.
type remoteTest struct {
hostname string
ip string
bundlerConstructor func(*testing.T) (b *Bundler)
errorCallback func(*testing.T, *remoteTest, error)
bundleCallback func(*testing.T, *remoteTest, *Bundle)
}
const (
RSACertSite = "rsa2048.badssl.com"
SelfSignedSSLSite = "self-signed.badssl.com"
MismatchedHostnameSite = "wrong.host.badssl.com"
ECCCertSite = "ecc256.badssl.com"
InvalidSite = "cloudflare1337.com"
ValidSNI = "badssl.com"
ValidSNIWildcard = "badssl.com"
SNISANWildcard = "*.badssl.com"
InvalidIP = "300.300.300.300"
)
func getBundleHostnameChecker(hostname string) func(*testing.T, *remoteTest, *Bundle) {
return func(t *testing.T, test *remoteTest, bundle *Bundle) {
if bundle == nil {
t.Fatalf("Nil bundle returned hostname=%q ip=%q", test.hostname, test.ip)
}
var found = false
for _, h := range bundle.Hostnames {
if h == hostname {
found = true
}
}
if !found {
t.Errorf("hostname expected but not found: %s hostname=%q ip=%q found=%v", hostname, test.hostname, test.ip, bundle.Hostnames)
}
}
}
func expectErrorMessages(expectedContents []string) func(*testing.T, *remoteTest, error) {
return func(t *testing.T, test *remoteTest, err error) {
if err == nil {
t.Fatalf("Expected error has %s. Got nothing. hostname=%q ip=%q", expectedContents, test.hostname, test.ip)
} else {
for _, expected := range expectedContents {
if !strings.Contains(err.Error(), expected) {
t.Fatalf("Expected error has %s. Got %s. hostname=%q ip=%q", expected, err.Error(), test.hostname, test.ip)
}
}
}
}
}
// test cases of BundleFromRemote
var remoteTests = []remoteTest{
{
hostname: RSACertSite,
bundlerConstructor: newBundler,
errorCallback: nil,
},
{
hostname: ECCCertSite,
bundlerConstructor: newBundler,
errorCallback: nil,
},
{
hostname: SelfSignedSSLSite,
bundlerConstructor: newBundler,
errorCallback: expectErrorMessages([]string{`"code":12`}), // only check it is a 12xx error
},
{
hostname: MismatchedHostnameSite,
bundlerConstructor: newBundler,
errorCallback: expectErrorMessages([]string{`"code":12`}), // only check it is a 12xx error
},
{
hostname: InvalidSite,
bundlerConstructor: newBundler,
errorCallback: expectErrorMessages([]string{`"code":6000`, "dial tcp: lookup cloudflare1337.com"}),
},
{
hostname: InvalidIP,
bundlerConstructor: newBundler,
errorCallback: expectErrorMessages([]string{`"code":6000`, "dial tcp: lookup 300.300.300.300"}),
},
{
ip: InvalidIP,
bundlerConstructor: newBundler,
errorCallback: expectErrorMessages([]string{`"code":6000`, "dial tcp: lookup 300.300.300.300"}),
},
}
// TestBundleFromRemote goes through the test cases defined in remoteTests and run them through. See above for test case definitions.
func TestBundleFromRemote(t *testing.T) {
t.Skip("expired cert https://github.com/cloudflare/cfssl/issues/1237")
for _, bf := range []BundleFlavor{Ubiquitous, Optimal} {
for _, test := range remoteTests {
b := test.bundlerConstructor(t)
bundle, err := b.BundleFromRemote(test.hostname, test.ip, bf)
if test.errorCallback != nil {
test.errorCallback(t, &test, err)
} else {
if err != nil {
t.Fatalf("expected no error. but an error occurred hostname=%q ip=%q errpr=%q", test.hostname, test.ip, err.Error())
}
if test.bundleCallback != nil {
test.bundleCallback(t, &test, bundle)
}
}
}
}
}
func resolveHostIP(host string) string {
addrs, err := net.LookupHost(host)
if err != nil {
panic(err)
}
if len(addrs) == 0 {
panic("failed to resolve " + host)
}
return addrs[0]
}
var remoteSNITests = []remoteTest{
{
hostname: ValidSNI,
bundlerConstructor: newBundler,
errorCallback: nil,
bundleCallback: getBundleHostnameChecker(ValidSNI),
},
{
hostname: ValidSNIWildcard,
bundlerConstructor: newBundler,
errorCallback: nil,
bundleCallback: getBundleHostnameChecker(SNISANWildcard),
},
{
hostname: ValidSNI,
ip: resolveHostIP(ValidSNI),
bundlerConstructor: newBundler,
errorCallback: nil,
bundleCallback: getBundleHostnameChecker(ValidSNI),
},
{
hostname: ValidSNIWildcard,
ip: resolveHostIP(ValidSNIWildcard),
bundlerConstructor: newBundler,
errorCallback: nil,
bundleCallback: getBundleHostnameChecker(SNISANWildcard),
},
}
// TestBundleFromRemoteSNI goes through the test cases defined in remoteSNITests and run them through. See above for test case definitions.
func TestBundleFromRemoteSNI(t *testing.T) {
t.Skip("expired cert https://github.com/cloudflare/cfssl/issues/1237")
for _, bf := range []BundleFlavor{Ubiquitous, Optimal} {
for _, test := range remoteSNITests {
b := test.bundlerConstructor(t)
bundle, err := b.BundleFromRemote(test.hostname, test.ip, bf)
if test.errorCallback != nil {
test.errorCallback(t, &test, err)
} else {
if err != nil {
t.Errorf("expected no error. but an error occurred: %s", err.Error())
}
if test.bundleCallback != nil {
test.bundleCallback(t, &test, bundle)
}
}
}
}
}
func TestBundleFromRemoteFlavor(t *testing.T) {
// This test was crafted for the specific cert bundle that benflare.us was
// serving. The majority of the functionality is validated via the other
// bundle tests.
t.Skip("skipped; need new example site for test")
b := newBundler(t)
ubiquity.Platforms = nil
ubiquity.LoadPlatforms(testMetadata)
bundle, err := b.BundleFromRemote(ECCCertSite, "", Ubiquitous)
if err != nil {
t.Fatalf("expected no error. but an error occurred: %s", err.Error())
}
if len(bundle.Chain) != 3 {
t.Error("expected 3-cert bundle. Got ", len(bundle.Chain))
}
if len(bundle.Status.Untrusted) != 0 {
t.Error("expected no untrusted platforms. Got ", bundle.Status.Untrusted)
}
bundle, err = b.BundleFromRemote(ECCCertSite, "", Optimal)
if err != nil {
t.Errorf("expected no error. but an error occurred: %s", err.Error())
}
if len(bundle.Chain) != 2 {
t.Error("expected 2-cert bundle. Got ", len(bundle.Chain))
}
}
|