Skip to content

Commit

Permalink
Added unit test for getCertificateWithSNI
Browse files Browse the repository at this point in the history
Added unit test for getCertificateWithSNI
  • Loading branch information
cindyxue authored Jun 30, 2020
2 parents b76410b + 3519dc6 commit d2cc061
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
102 changes: 102 additions & 0 deletions security/advancedtls/advancedtls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,105 @@ func TestWrapSyscallConn(t *testing.T) {
wrapConn)
}
}

const (
TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
)

const (
CurveP256 tls.CurveID = 23
)

const (
pointFormatUncompressed uint8 = 0
)

const (
VersionTLS10 uint16 = 0x0301
)

func TestGetCertificateSNI(t *testing.T) {
serverPeerCert1, err := tls.LoadX509KeyPair(testdata.Path("server_cert_1.pem"),
testdata.Path("server_key_1.pem"))
if err != nil {
t.Fatalf("Server is unable to parse peer certificates. Error: %v", err)
}
serverPeerCert2, err := tls.LoadX509KeyPair(testdata.Path("server_cert_2.pem"),
testdata.Path("server_key_2.pem"))
if err != nil {
t.Fatalf("Server is unable to parse peer certificates. Error: %v", err)
}
serverPeerCert3, err := tls.LoadX509KeyPair(testdata.Path("server_cert_3.pem"),
testdata.Path("server_key_3.pem"))
if err != nil {
t.Fatalf("Server is unable to parse peer certificates. Error: %v", err)
}
tests := []struct {
desc string
serverGetCert func(*tls.ClientHelloInfo) ([]*tls.Certificate, error)
}{
{
desc: "Select the certificate that matches the server name provided in clientHello",
serverGetCert: func(info *tls.ClientHelloInfo) ([]*tls.Certificate, error) {
return []*tls.Certificate{&serverPeerCert1, &serverPeerCert2, &serverPeerCert3}, nil
},
},
}
for _, test := range tests {
test := test
t.Run(test.desc, func(t *testing.T) {
serverOptions := &ServerOptions{
GetCertificate: test.serverGetCert,
}
serverConfig, err := serverOptions.config()
if err != nil {
t.Fatalf("Unable to generate serverConfig. Error: %v", err)
}
// "foo.bar.com" is the common name on server certificate server_cert_1.pem.
clientHello1 := &tls.ClientHelloInfo{
CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA},
ServerName: "foo.bar.com",
SupportedCurves: []tls.CurveID{CurveP256},
SupportedPoints: []uint8{pointFormatUncompressed},
SupportedVersions: []uint16{VersionTLS10},
}
got1, err := serverConfig.GetCertificate(clientHello1)
if err != nil {
t.Fatalf("Server is unable to parse peer certificates. Error: %v", err)
}
if !reflect.DeepEqual(*got1, serverPeerCert1) {
t.Errorf("GetCertificate() = %v, want %v", got1, serverPeerCert1)
}
// "foo.bar.server2.com" is the common name on server certificate server_cert_2.pem.
clientHello2 := &tls.ClientHelloInfo{
CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA},
ServerName: "foo.bar.server2.com",
SupportedCurves: []tls.CurveID{CurveP256},
SupportedPoints: []uint8{pointFormatUncompressed},
SupportedVersions: []uint16{VersionTLS10},
}
got2, err := serverConfig.GetCertificate(clientHello2)
if err != nil {
t.Fatalf("Server is unable to parse peer certificates. Error: %v", err)
}
if !reflect.DeepEqual(*got2, serverPeerCert2) {
t.Errorf("GetCertificate() = %v, want %v", got2, serverPeerCert2)
}
// "localhost" is the common name on server certificate server_cert_3.pem.
clientHello3 := &tls.ClientHelloInfo{
CipherSuites: []uint16{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA},
ServerName: "localhost",
SupportedCurves: []tls.CurveID{CurveP256},
SupportedPoints: []uint8{pointFormatUncompressed},
SupportedVersions: []uint16{VersionTLS10},
}
got3, err := serverConfig.GetCertificate(clientHello3)
if err != nil {
t.Fatalf("Server is unable to parse peer certificates. Error: %v", err)
}
if !reflect.DeepEqual(*got3, serverPeerCert3) {
t.Errorf("GetCertificate() = %v, want %v", got3, serverPeerCert3)
}
})
}
}
22 changes: 22 additions & 0 deletions security/advancedtls/testdata/server_cert_3.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-----BEGIN CERTIFICATE-----
MIIDjz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k8ZfmuCvyKy9sxRK6oyV1ot64tFy
YSUpJxSBcaeTt5HXLA7QgTpHMYOUttZCaVs1/4WcrOC9wh24r1mXlPvWWMp5dAZq
aVz8ZXd00OLz6v8ag4QEq/wxTeLdf1U7kJkJ0cj9/nPMLXBERQ54hVvuj5ea3oOv
FY7W
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions security/advancedtls/testdata/server_key_3.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCwVPrDQC2yT3RL
lJy76O9Jw4eUbIpAnFTw/Svqugs9UQNm2MyijMEnfXwtA0jNfXbeRNGLGKJ0gZ6I
a15v0B5jSuK9uZT9RtoC2ihstVt3gzUGqFJnOCm7UgO+zc4iVTX/8TZRDFyf0nG6
bJasN9e76LSohEKIAEpxSufJ9nmmZjozgoC3MyQ6tWt5QPQSB/pq14Rh6iTITxQ4
5B+AhzkCT3sYiUycTh8i5jeshdxWR8WAbm8EfrcGHPYmc5P7do+8zsW3TA96KPnX
l2Uym66wtyW4NRkAAPVPlmLfqUD9f7UkBmIBrfPNTesKMPaRZnYLo483f22XmCEd
6sTC+WgnAgMBAAECggEBAKAaOKlpr1QUYmJxqDHR2Nqf541zU8BAcbtyFBsHG0ds
NGuAc6Uvqf1iKYpxTfwObAx6bcwe7Ppd6oSxAEkDrWO4TdG4HDgvyakHTecOb+M+
xbmqwU8pJnMHj5ECKqTvu50M9aV3VO5kVJDRIaly9rWOH+JWXFZB9VWSjozI90GG
U11E5iSZI/ngUZL72eVqr5gOOD8lTaaLy8jHPnxOMpjZFAscPp1zy8y5DIf+Fgxc
kPzbhckxX8cpk2ATcdRK/UWNi3e/cyMxfeJvst6NwCgmXmDmfkyXMbU8wcEzEWPT
n4uf8gAaCR7fEJQa6Xo3s7q9WP86xnntaoer7V27KtkCgYEA5Y7uL2p2YcjrKfZ9
6VMPmJyDTH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