2018-10-05 21:48:36 +00:00
|
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/assert"
|
2018-10-19 05:26:39 +00:00
|
|
|
x509 "github.com/smallstep/cli/pkg/x509"
|
2018-10-05 21:48:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommonNameClaim_Valid(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
2018-10-19 05:26:39 +00:00
|
|
|
cnc certClaim
|
|
|
|
crt *x509.Certificate
|
2018-10-05 21:48:36 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
"empty-common-name": {
|
|
|
|
cnc: &commonNameClaim{name: "foo"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{},
|
2018-10-05 21:48:36 +00:00
|
|
|
err: errors.New("common name cannot be empty"),
|
|
|
|
},
|
|
|
|
"wrong-common-name": {
|
|
|
|
cnc: &commonNameClaim{name: "foo"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}},
|
2018-10-05 21:48:36 +00:00
|
|
|
err: errors.New("common name claim failed - got bar, want foo"),
|
|
|
|
},
|
|
|
|
"ok": {
|
|
|
|
cnc: &commonNameClaim{name: "foo"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}},
|
2018-10-05 21:48:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
err := tc.cnc.Valid(tc.crt)
|
|
|
|
if err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.Equals(t, tc.err.Error(), err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIPAddressesClaim_Valid(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
2018-10-19 05:26:39 +00:00
|
|
|
iac certClaim
|
|
|
|
crt *x509.Certificate
|
2018-10-05 21:48:36 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
"unexpected-ip": {
|
|
|
|
iac: &ipAddressesClaim{name: "127.0.0.1"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("1.1.1.1")}},
|
2018-10-05 21:48:36 +00:00
|
|
|
err: errors.New("IP addresses claim failed - got 1.1.1.1, want 127.0.0.1"),
|
|
|
|
},
|
|
|
|
"invalid-matcher-nonempty-ips": {
|
|
|
|
iac: &ipAddressesClaim{name: "invalid"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}},
|
2018-10-05 21:48:36 +00:00
|
|
|
err: errors.New("IP addresses claim failed - got [127.0.0.1], want none"),
|
|
|
|
},
|
|
|
|
"ok": {
|
|
|
|
iac: &ipAddressesClaim{name: "127.0.0.1"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}},
|
2018-10-05 21:48:36 +00:00
|
|
|
},
|
|
|
|
"ok-empty-ips": {
|
|
|
|
iac: &ipAddressesClaim{name: "127.0.0.1"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{IPAddresses: []net.IP{}},
|
2018-10-05 21:48:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
err := tc.iac.Valid(tc.crt)
|
|
|
|
if err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.Equals(t, tc.err.Error(), err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSNamesClaim_Valid(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
2018-10-19 05:26:39 +00:00
|
|
|
dnc certClaim
|
|
|
|
crt *x509.Certificate
|
2018-10-05 21:48:36 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
"wrong-dns-name": {
|
|
|
|
dnc: &dnsNamesClaim{name: "foo"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{DNSNames: []string{"foo", "bar"}},
|
2018-10-05 21:48:36 +00:00
|
|
|
err: errors.New("DNS names claim failed - got bar, want foo"),
|
|
|
|
},
|
|
|
|
"ok": {
|
|
|
|
dnc: &dnsNamesClaim{name: "foo"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{DNSNames: []string{"foo"}},
|
2018-10-05 21:48:36 +00:00
|
|
|
},
|
|
|
|
"ok-empty-dnsNames": {
|
|
|
|
dnc: &dnsNamesClaim{"foo"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{},
|
2018-10-05 21:48:36 +00:00
|
|
|
},
|
|
|
|
"ok-multiple-identical-dns-entries": {
|
|
|
|
dnc: &dnsNamesClaim{name: "foo"},
|
2018-10-19 05:26:39 +00:00
|
|
|
crt: &x509.Certificate{DNSNames: []string{"foo", "foo", "foo"}},
|
2018-10-05 21:48:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
err := tc.dnc.Valid(tc.crt)
|
|
|
|
if err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.Equals(t, tc.err.Error(), err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|