Skip to content

Add ParseExtKeyUsages tests #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion internal/scti/chain_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ var stringToKeyUsage = map[string]x509.ExtKeyUsage{

// ParseExtKeyUsages parses strings into x509ExtKeyUsage.
// Throws an error if the string does not match with a known key usage.
// TODO(phboneff): add tests
func ParseExtKeyUsages(kus []string) ([]x509.ExtKeyUsage, error) {
lExtKeyUsages := make([]x509.ExtKeyUsage, 0, len(kus))
// Validate the extended key usages list.
Expand Down
73 changes: 73 additions & 0 deletions internal/scti/chain_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,79 @@ import (
"github.com/transparency-dev/static-ct/internal/x509util"
)

func TestParseExtKeyUsages(t *testing.T) {
for _, tc := range []struct {
desc string
extKeyUsage []string
wantEKU []x509.ExtKeyUsage
wantErr bool
}{
{
desc: "empty",
extKeyUsage: []string{},
wantEKU: []x509.ExtKeyUsage{},
wantErr: false,
},
{
desc: "valid-single",
extKeyUsage: []string{"ServerAuth"},
wantEKU: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
wantErr: false,
},
{
desc: "valid-multiple",
extKeyUsage: []string{"ServerAuth", "ClientAuth"},
wantEKU: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
wantErr: false,
},
{
desc: "invalid",
extKeyUsage: []string{"InvalidUsage"},
wantEKU: nil,
wantErr: true,
},
{
desc: "mixed",
extKeyUsage: []string{"ServerAuth", "InvalidUsage"},
wantEKU: nil,
wantErr: true,
},
{
desc: "any",
extKeyUsage: []string{"Any"},
wantEKU: nil,
wantErr: false,
},
{
desc: "any-with-other-usages",
extKeyUsage: []string{"Any", "ServerAuth"},
wantEKU: nil,
wantErr: false,
},
} {
t.Run(tc.desc, func(t *testing.T) {
got, err := ParseExtKeyUsages(tc.extKeyUsage)
if tc.wantErr {
if err == nil {
t.Errorf("ParseExtKeyUsages(%v) = nil, want error", tc.extKeyUsage)
}
return
}
if err != nil {
t.Errorf("ParseExtKeyUsages(%v) = %v, want nil", tc.extKeyUsage, err)
}
if len(got) != len(tc.wantEKU) {
t.Errorf("ParseExtKeyUsages(%v) = %v, want %v", tc.extKeyUsage, got, tc.wantEKU)
}
for i, e := range tc.wantEKU {
if got[i] != e {
t.Errorf("ParseExtKeyUsages(%v) = %v, want %v", tc.extKeyUsage, got, tc.wantEKU)
}
}
})
}
}

func wipeExtensions(cert *x509.Certificate) *x509.Certificate {
cert.Extensions = cert.Extensions[:0]
return cert
Expand Down
Loading