|
1 | 1 | package authority
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "crypto/x509" |
4 | 5 | "net/http"
|
| 6 | + "reflect" |
5 | 7 | "testing"
|
6 | 8 |
|
7 | 9 | "github.com/pkg/errors"
|
8 | 10 | "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" |
9 | 14 | )
|
10 | 15 |
|
11 | 16 | func TestRoot(t *testing.T) {
|
@@ -43,3 +48,160 @@ func TestRoot(t *testing.T) {
|
43 | 48 | })
|
44 | 49 | }
|
45 | 50 | }
|
| 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