cas_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package cas
  2. import (
  3. "context"
  4. "crypto/ed25519"
  5. "crypto/x509"
  6. "crypto/x509/pkix"
  7. "fmt"
  8. "reflect"
  9. "testing"
  10. "go.step.sm/crypto/kms"
  11. kmsapi "go.step.sm/crypto/kms/apiv1"
  12. "github.com/smallstep/certificates/cas/apiv1"
  13. "github.com/smallstep/certificates/cas/softcas"
  14. )
  15. type mockCAS struct{}
  16. func (m *mockCAS) CreateCertificate(*apiv1.CreateCertificateRequest) (*apiv1.CreateCertificateResponse, error) {
  17. panic("not implemented")
  18. }
  19. func (m *mockCAS) RenewCertificate(*apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
  20. panic("not implemented")
  21. }
  22. func (m *mockCAS) RevokeCertificate(*apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
  23. panic("not implemented")
  24. }
  25. func TestNew(t *testing.T) {
  26. expected := &softcas.SoftCAS{
  27. CertificateChain: []*x509.Certificate{{Subject: pkix.Name{CommonName: "Test Issuer"}}},
  28. Signer: ed25519.PrivateKey{},
  29. }
  30. apiv1.Register(apiv1.Type("nockCAS"), func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
  31. return nil, fmt.Errorf("an error")
  32. })
  33. type args struct {
  34. ctx context.Context
  35. opts apiv1.Options
  36. }
  37. tests := []struct {
  38. name string
  39. args args
  40. want CertificateAuthorityService
  41. wantErr bool
  42. }{
  43. {"ok default", args{context.Background(), apiv1.Options{
  44. CertificateChain: []*x509.Certificate{{Subject: pkix.Name{CommonName: "Test Issuer"}}},
  45. Signer: ed25519.PrivateKey{},
  46. }}, expected, false},
  47. {"ok softcas", args{context.Background(), apiv1.Options{
  48. Type: "softcas",
  49. CertificateChain: []*x509.Certificate{{Subject: pkix.Name{CommonName: "Test Issuer"}}},
  50. Signer: ed25519.PrivateKey{},
  51. }}, expected, false},
  52. {"ok SoftCAS", args{context.Background(), apiv1.Options{
  53. Type: "SoftCAS",
  54. CertificateChain: []*x509.Certificate{{Subject: pkix.Name{CommonName: "Test Issuer"}}},
  55. Signer: ed25519.PrivateKey{},
  56. }}, expected, false},
  57. {"fail empty", args{context.Background(), apiv1.Options{}}, (*softcas.SoftCAS)(nil), true},
  58. {"fail type", args{context.Background(), apiv1.Options{Type: "FailCAS"}}, nil, true},
  59. {"fail load", args{context.Background(), apiv1.Options{Type: "nockCAS"}}, nil, true},
  60. }
  61. for _, tt := range tests {
  62. t.Run(tt.name, func(t *testing.T) {
  63. got, err := New(tt.args.ctx, tt.args.opts)
  64. if (err != nil) != tt.wantErr {
  65. t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
  66. return
  67. }
  68. if !reflect.DeepEqual(got, tt.want) {
  69. t.Errorf("New() = %#v, want %v", got, tt.want)
  70. }
  71. })
  72. }
  73. }
  74. func TestNewCreator(t *testing.T) {
  75. keyManager, err := kms.New(context.Background(), kmsapi.Options{})
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. apiv1.Register(apiv1.Type("nockCAS"), func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
  80. return &mockCAS{}, nil
  81. })
  82. type args struct {
  83. ctx context.Context
  84. opts apiv1.Options
  85. }
  86. tests := []struct {
  87. name string
  88. args args
  89. want CertificateAuthorityCreator
  90. wantErr bool
  91. }{
  92. {"ok empty", args{context.Background(), apiv1.Options{}}, &softcas.SoftCAS{}, false},
  93. {"ok softcas", args{context.Background(), apiv1.Options{
  94. Type: "softcas",
  95. }}, &softcas.SoftCAS{}, false},
  96. {"ok SoftCAS", args{context.Background(), apiv1.Options{
  97. Type: "SoftCAS",
  98. KeyManager: keyManager,
  99. }}, &softcas.SoftCAS{KeyManager: keyManager}, false},
  100. {"fail type", args{context.Background(), apiv1.Options{Type: "FailCAS"}}, nil, true},
  101. {"fail no creator", args{context.Background(), apiv1.Options{Type: "nockCAS"}}, nil, true},
  102. }
  103. for _, tt := range tests {
  104. t.Run(tt.name, func(t *testing.T) {
  105. got, err := NewCreator(tt.args.ctx, tt.args.opts)
  106. if (err != nil) != tt.wantErr {
  107. t.Errorf("NewCreator() error = %v, wantErr %v", err, tt.wantErr)
  108. return
  109. }
  110. if !reflect.DeepEqual(got, tt.want) {
  111. t.Errorf("NewCreator() = %v, want %v", got, tt.want)
  112. }
  113. })
  114. }
  115. }