2018-03-19 20:53:57 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-03-24 18:39:43 +00:00
|
|
|
// testCertURICases contains the test cases for parsing and encoding
|
2018-03-19 20:53:57 +00:00
|
|
|
// the SPIFFE IDs. This is a global since it is used in multiple test functions.
|
2018-03-24 18:39:43 +00:00
|
|
|
var testCertURICases = []struct {
|
2018-03-19 20:53:57 +00:00
|
|
|
Name string
|
|
|
|
URI string
|
|
|
|
Struct interface{}
|
|
|
|
ParseError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"invalid scheme",
|
|
|
|
"http://google.com/",
|
|
|
|
nil,
|
|
|
|
"scheme",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"basic service ID",
|
|
|
|
"spiffe://1234.consul/ns/default/dc/dc01/svc/web",
|
|
|
|
&SpiffeIDService{
|
|
|
|
Host: "1234.consul",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc01",
|
|
|
|
Service: "web",
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
2018-03-24 18:46:12 +00:00
|
|
|
|
2018-03-27 03:31:17 +00:00
|
|
|
{
|
|
|
|
"service with URL-encoded values",
|
|
|
|
"spiffe://1234.consul/ns/foo%2Fbar/dc/bar%2Fbaz/svc/baz%2Fqux",
|
|
|
|
&SpiffeIDService{
|
|
|
|
Host: "1234.consul",
|
|
|
|
Namespace: "foo/bar",
|
|
|
|
Datacenter: "bar/baz",
|
|
|
|
Service: "baz/qux",
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
2018-03-24 18:46:12 +00:00
|
|
|
{
|
|
|
|
"signing ID",
|
|
|
|
"spiffe://1234.consul",
|
|
|
|
&SpiffeIDSigning{
|
|
|
|
ClusterID: "1234",
|
|
|
|
Domain: "consul",
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
2018-03-19 20:53:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 18:39:43 +00:00
|
|
|
func TestParseCertURI(t *testing.T) {
|
|
|
|
for _, tc := range testCertURICases {
|
2018-03-19 20:53:57 +00:00
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
// Parse the URI, should always be valid
|
|
|
|
uri, err := url.Parse(tc.URI)
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
// Parse the ID and check the error/return value
|
2018-03-24 18:39:43 +00:00
|
|
|
actual, err := ParseCertURI(uri)
|
2018-03-27 03:31:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Logf("parse error: %s", err.Error())
|
|
|
|
}
|
2018-03-19 20:53:57 +00:00
|
|
|
assert.Equal(tc.ParseError != "", err != nil, "error value")
|
|
|
|
if err != nil {
|
|
|
|
assert.Contains(err.Error(), tc.ParseError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(tc.Struct, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|