Skip to content

Commit 1763ede

Browse files
committed
Add tests for new methods.
1 parent 6116523 commit 1763ede

File tree

2 files changed

+163
-1
lines changed

2 files changed

+163
-1
lines changed

authority/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (a *Authority) GetRootCertificates() []*x509.Certificate {
3434
}
3535

3636
// GetRoots returns all the root certificates for this CA.
37-
func (a *Authority) GetRoots(peer *x509.Certificate) (federation []*x509.Certificate, err error) {
37+
func (a *Authority) GetRoots(peer *x509.Certificate) ([]*x509.Certificate, error) {
3838
// Check step provisioner extensions
3939
if err := a.authorizeRenewal(peer); err != nil {
4040
return nil, err

authority/root_test.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package authority
22

33
import (
4+
"crypto/x509"
45
"net/http"
6+
"reflect"
57
"testing"
68

79
"github.com/pkg/errors"
810
"github.com/smallstep/assert"
11+
"github.com/smallstep/cli/crypto/keys"
12+
"github.com/smallstep/cli/crypto/pemutil"
13+
"github.com/smallstep/cli/crypto/x509util"
914
)
1015

1116
func TestRoot(t *testing.T) {
@@ -43,3 +48,160 @@ func TestRoot(t *testing.T) {
4348
})
4449
}
4550
}
51+
52+
func TestAuthority_GetRootCertificate(t *testing.T) {
53+
cert, err := pemutil.ReadCertificate("testdata/secrets/root_ca.crt")
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
58+
tests := []struct {
59+
name string
60+
want *x509.Certificate
61+
}{
62+
{"ok", cert},
63+
}
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
a := testAuthority(t)
67+
if got := a.GetRootCertificate(); !reflect.DeepEqual(got, tt.want) {
68+
t.Errorf("Authority.GetRootCertificate() = %v, want %v", got, tt.want)
69+
}
70+
})
71+
}
72+
}
73+
74+
func TestAuthority_GetRootCertificates(t *testing.T) {
75+
cert, err := pemutil.ReadCertificate("testdata/secrets/root_ca.crt")
76+
if err != nil {
77+
t.Fatal(err)
78+
}
79+
80+
tests := []struct {
81+
name string
82+
want []*x509.Certificate
83+
}{
84+
{"ok", []*x509.Certificate{cert}},
85+
}
86+
for _, tt := range tests {
87+
t.Run(tt.name, func(t *testing.T) {
88+
a := testAuthority(t)
89+
if got := a.GetRootCertificates(); !reflect.DeepEqual(got, tt.want) {
90+
t.Errorf("Authority.GetRootCertificates() = %v, want %v", got, tt.want)
91+
}
92+
})
93+
}
94+
}
95+
96+
func TestAuthority_GetRoots(t *testing.T) {
97+
cert, err := pemutil.ReadCertificate("testdata/secrets/root_ca.crt")
98+
if err != nil {
99+
t.Fatal(err)
100+
}
101+
102+
a := testAuthority(t)
103+
pub, _, err := keys.GenerateDefaultKeyPair()
104+
assert.FatalError(t, err)
105+
leaf, err := x509util.NewLeafProfile("test", a.intermediateIdentity.Crt, a.intermediateIdentity.Key,
106+
withDefaultASN1DN(a.config.AuthorityConfig.Template), x509util.WithPublicKey(pub), x509util.WithHosts("test"))
107+
assert.FatalError(t, err)
108+
crtBytes, err := leaf.CreateCertificate()
109+
assert.FatalError(t, err)
110+
crt, err := x509.ParseCertificate(crtBytes)
111+
assert.FatalError(t, err)
112+
113+
leafFail, err := x509util.NewLeafProfile("test", a.intermediateIdentity.Crt, a.intermediateIdentity.Key,
114+
withDefaultASN1DN(a.config.AuthorityConfig.Template), x509util.WithPublicKey(pub), x509util.WithHosts("test"),
115+
withProvisionerOID("dev", a.config.AuthorityConfig.Provisioners[2].Key.KeyID),
116+
)
117+
assert.FatalError(t, err)
118+
crtFailBytes, err := leafFail.CreateCertificate()
119+
assert.FatalError(t, err)
120+
crtFail, err := x509.ParseCertificate(crtFailBytes)
121+
assert.FatalError(t, err)
122+
123+
type args struct {
124+
peer *x509.Certificate
125+
}
126+
tests := []struct {
127+
name string
128+
args args
129+
want []*x509.Certificate
130+
wantErr bool
131+
}{
132+
{"ok", args{crt}, []*x509.Certificate{cert}, false},
133+
{"fail", args{crtFail}, nil, true},
134+
}
135+
for _, tt := range tests {
136+
t.Run(tt.name, func(t *testing.T) {
137+
got, err := a.GetRoots(tt.args.peer)
138+
if (err != nil) != tt.wantErr {
139+
t.Errorf("Authority.GetRoots() error = %v, wantErr %v", err, tt.wantErr)
140+
return
141+
}
142+
if !reflect.DeepEqual(got, tt.want) {
143+
t.Errorf("Authority.GetRoots() = %v, want %v", got, tt.want)
144+
}
145+
})
146+
}
147+
}
148+
149+
func TestAuthority_GetFederation(t *testing.T) {
150+
cert, err := pemutil.ReadCertificate("testdata/secrets/root_ca.crt")
151+
if err != nil {
152+
t.Fatal(err)
153+
}
154+
155+
a := testAuthority(t)
156+
pub, _, err := keys.GenerateDefaultKeyPair()
157+
assert.FatalError(t, err)
158+
leaf, err := x509util.NewLeafProfile("test", a.intermediateIdentity.Crt, a.intermediateIdentity.Key,
159+
withDefaultASN1DN(a.config.AuthorityConfig.Template), x509util.WithPublicKey(pub), x509util.WithHosts("test"))
160+
assert.FatalError(t, err)
161+
crtBytes, err := leaf.CreateCertificate()
162+
assert.FatalError(t, err)
163+
crt, err := x509.ParseCertificate(crtBytes)
164+
assert.FatalError(t, err)
165+
166+
leafFail, err := x509util.NewLeafProfile("test", a.intermediateIdentity.Crt, a.intermediateIdentity.Key,
167+
withDefaultASN1DN(a.config.AuthorityConfig.Template), x509util.WithPublicKey(pub), x509util.WithHosts("test"),
168+
withProvisionerOID("dev", a.config.AuthorityConfig.Provisioners[2].Key.KeyID),
169+
)
170+
assert.FatalError(t, err)
171+
crtFailBytes, err := leafFail.CreateCertificate()
172+
assert.FatalError(t, err)
173+
crtFail, err := x509.ParseCertificate(crtFailBytes)
174+
assert.FatalError(t, err)
175+
176+
type args struct {
177+
peer *x509.Certificate
178+
}
179+
tests := []struct {
180+
name string
181+
args args
182+
wantFederation []*x509.Certificate
183+
wantErr bool
184+
fn func()
185+
}{
186+
{"ok", args{crt}, []*x509.Certificate{cert}, false, nil},
187+
{"fail", args{crtFail}, nil, true, nil},
188+
{"fail not a certificate", args{crt}, nil, true, func() {
189+
a.certificates.Store("foo", "bar")
190+
}},
191+
}
192+
for _, tt := range tests {
193+
t.Run(tt.name, func(t *testing.T) {
194+
if tt.fn != nil {
195+
tt.fn()
196+
}
197+
gotFederation, err := a.GetFederation(tt.args.peer)
198+
if (err != nil) != tt.wantErr {
199+
t.Errorf("Authority.GetFederation() error = %v, wantErr %v", err, tt.wantErr)
200+
return
201+
}
202+
if !reflect.DeepEqual(gotFederation, tt.wantFederation) {
203+
t.Errorf("Authority.GetFederation() = %v, want %v", gotFederation, tt.wantFederation)
204+
}
205+
})
206+
}
207+
}

0 commit comments

Comments
 (0)