certificates/cas/apiv1/options_test.go

133 lines
3.7 KiB
Go
Raw Normal View History

2020-09-16 19:34:42 +00:00
package apiv1
import (
"context"
"crypto"
"crypto/x509"
"sync"
"testing"
)
type testCAS struct {
name string
}
2023-05-10 06:47:28 +00:00
func (t *testCAS) CreateCertificate(*CreateCertificateRequest) (*CreateCertificateResponse, error) {
2020-09-16 19:34:42 +00:00
return nil, nil
}
2023-05-10 06:47:28 +00:00
func (t *testCAS) RenewCertificate(*RenewCertificateRequest) (*RenewCertificateResponse, error) {
2020-09-16 19:34:42 +00:00
return nil, nil
}
2023-05-10 06:47:28 +00:00
func (t *testCAS) RevokeCertificate(*RevokeCertificateRequest) (*RevokeCertificateResponse, error) {
2020-09-16 19:34:42 +00:00
return nil, nil
}
2023-05-10 06:47:28 +00:00
//nolint:gocritic // ignore sloppy test func name
2020-09-16 19:34:42 +00:00
func mockRegister(t *testing.T) {
t.Helper()
Register(SoftCAS, func(ctx context.Context, opts Options) (CertificateAuthorityService, error) {
return &testCAS{name: SoftCAS}, nil
})
Register(CloudCAS, func(ctx context.Context, opts Options) (CertificateAuthorityService, error) {
return &testCAS{name: CloudCAS}, nil
})
t.Cleanup(func() {
registry = new(sync.Map)
})
}
func TestOptions_Validate(t *testing.T) {
mockRegister(t)
type fields struct {
Type string
CredentialsFile string
2020-10-20 01:44:27 +00:00
CertificateAuthority string
2020-09-16 19:34:42 +00:00
Issuer *x509.Certificate
Signer crypto.Signer
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{"empty", fields{}, false},
{"SoftCAS", fields{SoftCAS, "", "", nil, nil}, false},
{"CloudCAS", fields{CloudCAS, "", "", nil, nil}, false},
{"softcas", fields{"softcas", "", "", nil, nil}, false},
{"CLOUDCAS", fields{"CLOUDCAS", "", "", nil, nil}, false},
{"fail", fields{"FailCAS", "", "", nil, nil}, true},
}
t.Run("nil", func(t *testing.T) {
var o *Options
if err := o.Validate(); err != nil {
t.Errorf("Options.Validate() error = %v, wantErr %v", err, false)
}
})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &Options{
Type: tt.fields.Type,
CredentialsFile: tt.fields.CredentialsFile,
2020-10-20 01:44:27 +00:00
CertificateAuthority: tt.fields.CertificateAuthority,
CertificateChain: []*x509.Certificate{tt.fields.Issuer},
2020-09-16 19:34:42 +00:00
Signer: tt.fields.Signer,
}
if err := o.Validate(); (err != nil) != tt.wantErr {
t.Errorf("Options.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2020-09-21 22:11:25 +00:00
func TestOptions_Is(t *testing.T) {
2020-09-16 19:34:42 +00:00
mockRegister(t)
type fields struct {
Type string
CredentialsFile string
2020-10-20 01:44:27 +00:00
CertificateAuthority string
2020-09-16 19:34:42 +00:00
Issuer *x509.Certificate
Signer crypto.Signer
}
type args struct {
t Type
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{"empty", fields{}, args{}, true},
{"SoftCAS", fields{SoftCAS, "", "", nil, nil}, args{"SoftCAS"}, true},
{"CloudCAS", fields{CloudCAS, "", "", nil, nil}, args{"CloudCAS"}, true},
{"softcas", fields{"softcas", "", "", nil, nil}, args{SoftCAS}, true},
{"CLOUDCAS", fields{"CLOUDCAS", "", "", nil, nil}, args{CloudCAS}, true},
{"UnknownCAS", fields{"UnknownCAS", "", "", nil, nil}, args{"UnknownCAS"}, true},
{"fail", fields{CloudCAS, "", "", nil, nil}, args{"SoftCAS"}, false},
{"fail", fields{SoftCAS, "", "", nil, nil}, args{"CloudCAS"}, false},
}
t.Run("nil", func(t *testing.T) {
var o *Options
2020-09-21 22:11:25 +00:00
if got := o.Is(SoftCAS); got != true {
t.Errorf("Options.Is() = %v, want %v", got, true)
2020-09-16 19:34:42 +00:00
}
})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &Options{
Type: tt.fields.Type,
CredentialsFile: tt.fields.CredentialsFile,
2020-10-20 01:44:27 +00:00
CertificateAuthority: tt.fields.CertificateAuthority,
CertificateChain: []*x509.Certificate{tt.fields.Issuer},
2020-09-16 19:34:42 +00:00
Signer: tt.fields.Signer,
}
2020-09-21 22:11:25 +00:00
if got := o.Is(tt.args.t); got != tt.want {
t.Errorf("Options.Is() = %v, want %v", got, tt.want)
2020-09-16 19:34:42 +00:00
}
})
}
}