File: cas_test.go

package info (click to toggle)
golang-github-smallstep-certificates 0.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,720 kB
  • sloc: sh: 385; makefile: 129
file content (128 lines) | stat: -rw-r--r-- 3,974 bytes parent folder | download | duplicates (2)
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
package cas

import (
	"context"
	"crypto/ed25519"
	"crypto/x509"
	"crypto/x509/pkix"
	"fmt"
	"reflect"
	"testing"

	"go.step.sm/crypto/kms"
	kmsapi "go.step.sm/crypto/kms/apiv1"

	"github.com/smallstep/certificates/cas/apiv1"
	"github.com/smallstep/certificates/cas/softcas"
)

type mockCAS struct{}

func (m *mockCAS) CreateCertificate(*apiv1.CreateCertificateRequest) (*apiv1.CreateCertificateResponse, error) {
	panic("not implemented")
}

func (m *mockCAS) RenewCertificate(*apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
	panic("not implemented")
}

func (m *mockCAS) RevokeCertificate(*apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
	panic("not implemented")
}

func TestNew(t *testing.T) {
	expected := &softcas.SoftCAS{
		CertificateChain: []*x509.Certificate{{Subject: pkix.Name{CommonName: "Test Issuer"}}},
		Signer:           ed25519.PrivateKey{},
	}

	apiv1.Register(apiv1.Type("nockCAS"), func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
		return nil, fmt.Errorf("an error")
	})

	type args struct {
		ctx  context.Context
		opts apiv1.Options
	}
	tests := []struct {
		name    string
		args    args
		want    CertificateAuthorityService
		wantErr bool
	}{
		{"ok default", args{context.Background(), apiv1.Options{
			CertificateChain: []*x509.Certificate{{Subject: pkix.Name{CommonName: "Test Issuer"}}},
			Signer:           ed25519.PrivateKey{},
		}}, expected, false},
		{"ok softcas", args{context.Background(), apiv1.Options{
			Type:             "softcas",
			CertificateChain: []*x509.Certificate{{Subject: pkix.Name{CommonName: "Test Issuer"}}},
			Signer:           ed25519.PrivateKey{},
		}}, expected, false},
		{"ok SoftCAS", args{context.Background(), apiv1.Options{
			Type:             "SoftCAS",
			CertificateChain: []*x509.Certificate{{Subject: pkix.Name{CommonName: "Test Issuer"}}},
			Signer:           ed25519.PrivateKey{},
		}}, expected, false},
		{"fail empty", args{context.Background(), apiv1.Options{}}, (*softcas.SoftCAS)(nil), true},
		{"fail type", args{context.Background(), apiv1.Options{Type: "FailCAS"}}, nil, true},
		{"fail load", args{context.Background(), apiv1.Options{Type: "nockCAS"}}, nil, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := New(tt.args.ctx, tt.args.opts)
			if (err != nil) != tt.wantErr {
				t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("New() = %#v, want %v", got, tt.want)
			}
		})
	}
}

func TestNewCreator(t *testing.T) {
	keyManager, err := kms.New(context.Background(), kmsapi.Options{})
	if err != nil {
		t.Fatal(err)
	}

	apiv1.Register(apiv1.Type("nockCAS"), func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
		return &mockCAS{}, nil
	})

	type args struct {
		ctx  context.Context
		opts apiv1.Options
	}
	tests := []struct {
		name    string
		args    args
		want    CertificateAuthorityCreator
		wantErr bool
	}{
		{"ok empty", args{context.Background(), apiv1.Options{}}, &softcas.SoftCAS{}, false},
		{"ok softcas", args{context.Background(), apiv1.Options{
			Type: "softcas",
		}}, &softcas.SoftCAS{}, false},
		{"ok SoftCAS", args{context.Background(), apiv1.Options{
			Type:       "SoftCAS",
			KeyManager: keyManager,
		}}, &softcas.SoftCAS{KeyManager: keyManager}, false},
		{"fail type", args{context.Background(), apiv1.Options{Type: "FailCAS"}}, nil, true},
		{"fail no creator", args{context.Background(), apiv1.Options{Type: "nockCAS"}}, nil, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := NewCreator(tt.args.ctx, tt.args.opts)
			if (err != nil) != tt.wantErr {
				t.Errorf("NewCreator() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("NewCreator() = %v, want %v", got, tt.want)
			}
		})
	}
}