Skip to content

Commit

Permalink
Add test for the new function in tls package
Browse files Browse the repository at this point in the history
Signed-off-by: dttung2905 <ttdao.2015@accountancy.smu.edu.sg>
  • Loading branch information
dttung2905 committed Apr 22, 2023
1 parent c8efe8c commit f662140
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions pkg/util/tls_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func TestNewTLSConfig_WithPassword(t *testing.T) {
password string
issuer string
CACert string
isError bool
}{
{
name: "rsaCert_WithCACert",
Expand All @@ -195,6 +196,7 @@ func TestNewTLSConfig_WithPassword(t *testing.T) {
password: "keypass",
issuer: "O=Internet Widgits Pty Ltd,ST=Some-State,C=AU",
CACert: randomCACert,
isError: false,
},
{
name: "Cert_WithCACert",
Expand All @@ -203,6 +205,7 @@ func TestNewTLSConfig_WithPassword(t *testing.T) {
password: "keypass",
issuer: "O=Internet Widgits Pty Ltd,ST=Some-State,C=AU",
CACert: randomCACert,
isError: false,
},
{
name: "rsaCert_WithoutCACert",
Expand All @@ -211,6 +214,7 @@ func TestNewTLSConfig_WithPassword(t *testing.T) {
password: "keypass",
issuer: "O=Internet Widgits Pty Ltd,ST=Some-State,C=AU",
CACert: "",
isError: false,
},
{
name: "Cert_WithoutCACert",
Expand All @@ -219,28 +223,41 @@ func TestNewTLSConfig_WithPassword(t *testing.T) {
password: "keypass",
issuer: "O=Internet Widgits Pty Ltd,ST=Some-State,C=AU",
CACert: "",
isError: false,
},
{
name: "Cert_WithInvalidCACert",
cert: rsaCertPEM,
key: encryptedKeyPEM,
password: "keypass",
issuer: "O=Internet Widgits Pty Ltd,ST=Some-State,C=AU",
CACert: "invalidCACert",
isError: true,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
config, err := NewTLSConfigWithPassword(test.cert, test.key, test.password, test.CACert, false)
if err != nil {
t.Errorf("Should have no error: %s", err)
}
cert, err := x509.ParseCertificate(config.Certificates[0].Certificate[0])
if err != nil {
t.Errorf("Bad certificate")
}
if err != nil && !test.isError {
t.Errorf("Expected sucess but got error: %s", err)
} else if test.isError && err == nil {
t.Errorf("Expect error but got success")
} else if err == nil {
cert, err := x509.ParseCertificate(config.Certificates[0].Certificate[0])
if err != nil {
t.Errorf("Bad certificate")
}

if test.CACert != "" {
caCertPool := getRootCAs()
caCertPool.AppendCertsFromPEM([]byte(randomCACert))
if !config.RootCAs.Equal(caCertPool) {
t.Errorf("TLS config return different CA cert")
if test.CACert != "" {
caCertPool := getRootCAs()
caCertPool.AppendCertsFromPEM([]byte(randomCACert))
if !config.RootCAs.Equal(caCertPool) {
t.Errorf("TLS config return different CA cert")
}
}
if cert.Issuer.String() != test.issuer {
t.Errorf("Expected Issuer %s but got %s\n", test.issuer, cert.Issuer.String())
}
}
if cert.Issuer.String() != test.issuer {
t.Errorf("Expected Issuer %s but got %s\n", test.issuer, cert.Issuer.String())
}
})
}
Expand Down

0 comments on commit f662140

Please sign in to comment.