options_test.go 3.7 KB

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