options_test.go 3.7 KB

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