1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
package apiv1
import (
"context"
"crypto"
"crypto/x509"
"sync"
"testing"
)
type testCAS struct {
name string
}
func (t *testCAS) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
return nil, nil
}
func (t *testCAS) RenewCertificate(req *RenewCertificateRequest) (*RenewCertificateResponse, error) {
return nil, nil
}
func (t *testCAS) RevokeCertificate(req *RevokeCertificateRequest) (*RevokeCertificateResponse, error) {
return nil, nil
}
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
CertificateAuthority string
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,
CertificateAuthority: tt.fields.CertificateAuthority,
CertificateChain: []*x509.Certificate{tt.fields.Issuer},
Signer: tt.fields.Signer,
}
if err := o.Validate(); (err != nil) != tt.wantErr {
t.Errorf("Options.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestOptions_Is(t *testing.T) {
mockRegister(t)
type fields struct {
Type string
CredentialsFile string
CertificateAuthority string
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
if got := o.Is(SoftCAS); got != true {
t.Errorf("Options.Is() = %v, want %v", got, true)
}
})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &Options{
Type: tt.fields.Type,
CredentialsFile: tt.fields.CredentialsFile,
CertificateAuthority: tt.fields.CertificateAuthority,
CertificateChain: []*x509.Certificate{tt.fields.Issuer},
Signer: tt.fields.Signer,
}
if got := o.Is(tt.args.t); got != tt.want {
t.Errorf("Options.Is() = %v, want %v", got, tt.want)
}
})
}
}
|