-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https: Minor refactoring and some new tests
- Loading branch information
Showing
4 changed files
with
145 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package https | ||
|
||
import "testing" | ||
|
||
func TestUnexportedGetCertificate(t *testing.T) { | ||
defer func() { certCache = make(map[string]Certificate) }() | ||
|
||
// When cache is empty | ||
if _, matched, defaulted := getCertificate("example.com"); matched || defaulted { | ||
t.Errorf("Got a certificate when cache was empty; matched=%v, defaulted=%v", matched, defaulted) | ||
} | ||
|
||
// When cache has one certificate in it (also is default) | ||
defaultCert := Certificate{Names: []string{"example.com", ""}} | ||
certCache[""] = defaultCert | ||
certCache["example.com"] = defaultCert | ||
if cert, matched, defaulted := getCertificate("Example.com"); !matched || defaulted || cert.Names[0] != "example.com" { | ||
t.Errorf("Didn't get a cert for 'Example.com' or got the wrong one: %v, matched=%v, defaulted=%v", cert, matched, defaulted) | ||
} | ||
if cert, matched, defaulted := getCertificate(""); !matched || defaulted || cert.Names[0] != "example.com" { | ||
t.Errorf("Didn't get a cert for '' or got the wrong one: %v, matched=%v, defaulted=%v", cert, matched, defaulted) | ||
} | ||
|
||
// When retrieving wildcard certificate | ||
certCache["*.example.com"] = Certificate{Names: []string{"*.example.com"}} | ||
if cert, matched, defaulted := getCertificate("sub.example.com"); !matched || defaulted || cert.Names[0] != "*.example.com" { | ||
t.Errorf("Didn't get wildcard cert for 'sub.example.com' or got the wrong one: %v, matched=%v, defaulted=%v", cert, matched, defaulted) | ||
} | ||
|
||
// When no certificate matches, the default is returned | ||
if cert, matched, defaulted := getCertificate("nomatch"); matched || !defaulted { | ||
t.Errorf("Expected matched=false, defaulted=true; but got matched=%v, defaulted=%v (cert: %v)", matched, defaulted, cert) | ||
} else if cert.Names[0] != "example.com" { | ||
t.Errorf("Expected default cert, got: %v", cert) | ||
} | ||
} | ||
|
||
func TestCacheCertificate(t *testing.T) { | ||
defer func() { certCache = make(map[string]Certificate) }() | ||
|
||
cacheCertificate(Certificate{Names: []string{"example.com", "sub.example.com"}}) | ||
if _, ok := certCache["example.com"]; !ok { | ||
t.Error("Expected first cert to be cached by key 'example.com', but it wasn't") | ||
} | ||
if _, ok := certCache["sub.example.com"]; !ok { | ||
t.Error("Expected first cert to be cached by key 'sub.exmaple.com', but it wasn't") | ||
} | ||
if cert, ok := certCache[""]; !ok || cert.Names[2] != "" { | ||
t.Error("Expected first cert to be cached additionally as the default certificate with empty name added, but it wasn't") | ||
} | ||
|
||
cacheCertificate(Certificate{Names: []string{"example2.com"}}) | ||
if _, ok := certCache["example2.com"]; !ok { | ||
t.Error("Expected second cert to be cached by key 'exmaple2.com', but it wasn't") | ||
} | ||
if cert, ok := certCache[""]; ok && cert.Names[0] == "example2.com" { | ||
t.Error("Expected second cert to NOT be cached as default, but it was") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package https | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"testing" | ||
) | ||
|
||
func TestGetCertificate(t *testing.T) { | ||
defer func() { certCache = make(map[string]Certificate) }() | ||
|
||
hello := &tls.ClientHelloInfo{ServerName: "example.com"} | ||
helloSub := &tls.ClientHelloInfo{ServerName: "sub.example.com"} | ||
helloNoSNI := &tls.ClientHelloInfo{} | ||
helloNoMatch := &tls.ClientHelloInfo{ServerName: "nomatch"} | ||
|
||
// When cache is empty | ||
if cert, err := GetCertificate(hello); err == nil { | ||
t.Errorf("GetCertificate should return error when cache is empty, got: %v", cert) | ||
} | ||
if cert, err := GetCertificate(helloNoSNI); err == nil { | ||
t.Errorf("GetCertificate should return error when cache is empty even if server name is blank, got: %v", cert) | ||
} | ||
|
||
// When cache has one certificate in it (also is default) | ||
defaultCert := Certificate{Names: []string{"example.com", ""}, Certificate: tls.Certificate{Leaf: &x509.Certificate{DNSNames: []string{"example.com"}}}} | ||
certCache[""] = defaultCert | ||
certCache["example.com"] = defaultCert | ||
if cert, err := GetCertificate(hello); err != nil { | ||
t.Errorf("Got an error but shouldn't have, when cert exists in cache: %v", err) | ||
} else if cert.Leaf.DNSNames[0] != "example.com" { | ||
t.Errorf("Got wrong certificate with exact match; expected 'example.com', got: %v", cert) | ||
} | ||
if cert, err := GetCertificate(helloNoSNI); err != nil { | ||
t.Errorf("Got an error with no SNI but shouldn't have, when cert exists in cache: %v", err) | ||
} else if cert.Leaf.DNSNames[0] != "example.com" { | ||
t.Errorf("Got wrong certificate for no SNI; expected 'example.com' as default, got: %v", cert) | ||
} | ||
|
||
// When retrieving wildcard certificate | ||
certCache["*.example.com"] = Certificate{Names: []string{"*.example.com"}, Certificate: tls.Certificate{Leaf: &x509.Certificate{DNSNames: []string{"*.example.com"}}}} | ||
if cert, err := GetCertificate(helloSub); err != nil { | ||
t.Errorf("Didn't get wildcard cert, got: cert=%v, err=%v ", cert, err) | ||
} else if cert.Leaf.DNSNames[0] != "*.example.com" { | ||
t.Errorf("Got wrong certificate, expected wildcard: %v", cert) | ||
} | ||
|
||
// When no certificate matches, the default is returned | ||
if cert, err := GetCertificate(helloNoMatch); err != nil { | ||
t.Errorf("Expected default certificate with no error when no matches, got err: %v", err) | ||
} else if cert.Leaf.DNSNames[0] != "example.com" { | ||
t.Errorf("Expected default cert with no matches, got: %v", cert) | ||
} | ||
} |