2018-10-23 11:05:44 -04:00
|
|
|
package trust
|
2017-08-24 18:43:55 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-08-24 18:43:55 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-08-24 18:43:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTrustTag(t *testing.T) {
|
|
|
|
digest := stringid.GenerateRandomID()
|
|
|
|
trustedTag := "tag"
|
|
|
|
|
|
|
|
var ctx trustTagContext
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
trustTagCtx trustTagContext
|
|
|
|
expValue string
|
|
|
|
call func() string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
trustTagContext{
|
|
|
|
s: SignedTagInfo{Name: trustedTag,
|
|
|
|
Digest: digest,
|
|
|
|
Signers: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
digest,
|
|
|
|
ctx.Digest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
trustTagContext{
|
|
|
|
s: SignedTagInfo{Name: trustedTag,
|
|
|
|
Digest: digest,
|
|
|
|
Signers: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
trustedTag,
|
|
|
|
ctx.SignedTag,
|
|
|
|
},
|
|
|
|
// Empty signers makes a row with empty string
|
|
|
|
{
|
|
|
|
trustTagContext{
|
|
|
|
s: SignedTagInfo{Name: trustedTag,
|
|
|
|
Digest: digest,
|
|
|
|
Signers: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
ctx.Signers,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
trustTagContext{
|
|
|
|
s: SignedTagInfo{Name: trustedTag,
|
|
|
|
Digest: digest,
|
2017-09-18 21:05:10 -04:00
|
|
|
Signers: []string{"alice", "bob", "claire"},
|
2017-08-24 18:43:55 -04:00
|
|
|
},
|
|
|
|
},
|
2017-09-18 21:05:10 -04:00
|
|
|
"alice, bob, claire",
|
2017-08-24 18:43:55 -04:00
|
|
|
ctx.Signers,
|
|
|
|
},
|
|
|
|
// alphabetic signing on Signers
|
|
|
|
{
|
|
|
|
trustTagContext{
|
|
|
|
s: SignedTagInfo{Name: trustedTag,
|
|
|
|
Digest: digest,
|
2017-09-18 21:05:10 -04:00
|
|
|
Signers: []string{"claire", "bob", "alice"},
|
2017-08-24 18:43:55 -04:00
|
|
|
},
|
|
|
|
},
|
2017-09-18 21:05:10 -04:00
|
|
|
"alice, bob, claire",
|
2017-08-24 18:43:55 -04:00
|
|
|
ctx.Signers,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.trustTagCtx
|
|
|
|
v := c.call()
|
|
|
|
if v != c.expValue {
|
|
|
|
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrustTagContextWrite(t *testing.T) {
|
|
|
|
|
|
|
|
cases := []struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
context formatter.Context
|
2017-08-24 18:43:55 -04:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// Errors
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: "{{InvalidFunction}}",
|
|
|
|
},
|
|
|
|
`Template parsing error: template: :1: function "InvalidFunction" not defined
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: "{{nil}}",
|
|
|
|
},
|
|
|
|
`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Table Format
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: NewTrustTagFormat(),
|
|
|
|
},
|
|
|
|
`SIGNED TAG DIGEST SIGNERS
|
|
|
|
tag1 deadbeef alice
|
|
|
|
tag2 aaaaaaaa alice, bob
|
|
|
|
tag3 bbbbbbbb
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range cases {
|
|
|
|
signedTags := []SignedTagInfo{
|
|
|
|
{Name: "tag1", Digest: "deadbeef", Signers: []string{"alice"}},
|
|
|
|
{Name: "tag2", Digest: "aaaaaaaa", Signers: []string{"alice", "bob"}},
|
|
|
|
{Name: "tag3", Digest: "bbbbbbbb", Signers: []string{}},
|
|
|
|
}
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
testcase.context.Output = out
|
2018-10-23 11:05:44 -04:00
|
|
|
err := TagWrite(testcase.context, signedTags)
|
2017-08-24 18:43:55 -04:00
|
|
|
if err != nil {
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, testcase.expected)
|
2017-08-24 18:43:55 -04:00
|
|
|
} else {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(testcase.expected, out.String()))
|
2017-08-24 18:43:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// With no trust data, the TagWrite will print an empty table:
|
2017-08-24 18:43:55 -04:00
|
|
|
// it's up to the caller to decide whether or not to print this versus an error
|
|
|
|
func TestTrustTagContextEmptyWrite(t *testing.T) {
|
|
|
|
|
|
|
|
emptyCase := struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
context formatter.Context
|
2017-08-24 18:43:55 -04:00
|
|
|
expected string
|
|
|
|
}{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: NewTrustTagFormat(),
|
|
|
|
},
|
|
|
|
`SIGNED TAG DIGEST SIGNERS
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
emptySignedTags := []SignedTagInfo{}
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
emptyCase.context.Output = out
|
2018-10-23 11:05:44 -04:00
|
|
|
err := TagWrite(emptyCase.context, emptySignedTags)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(emptyCase.expected, out.String()))
|
2017-08-24 18:43:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSignerInfoContextEmptyWrite(t *testing.T) {
|
|
|
|
emptyCase := struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
context formatter.Context
|
2017-08-24 18:43:55 -04:00
|
|
|
expected string
|
|
|
|
}{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: NewSignerInfoFormat(),
|
|
|
|
},
|
|
|
|
`SIGNER KEYS
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
emptySignerInfo := []SignerInfo{}
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
emptyCase.context.Output = out
|
|
|
|
err := SignerInfoWrite(emptyCase.context, emptySignerInfo)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(emptyCase.expected, out.String()))
|
2017-08-24 18:43:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSignerInfoContextWrite(t *testing.T) {
|
|
|
|
cases := []struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
context formatter.Context
|
2017-08-24 18:43:55 -04:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// Errors
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: "{{InvalidFunction}}",
|
|
|
|
},
|
|
|
|
`Template parsing error: template: :1: function "InvalidFunction" not defined
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: "{{nil}}",
|
|
|
|
},
|
|
|
|
`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Table Format
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: NewSignerInfoFormat(),
|
|
|
|
Trunc: true,
|
|
|
|
},
|
|
|
|
`SIGNER KEYS
|
|
|
|
alice key11, key12
|
|
|
|
bob key21
|
|
|
|
eve foobarbazqux, key31, key32
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// No truncation
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: NewSignerInfoFormat(),
|
|
|
|
},
|
|
|
|
`SIGNER KEYS
|
|
|
|
alice key11, key12
|
|
|
|
bob key21
|
|
|
|
eve foobarbazquxquux, key31, key32
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range cases {
|
2018-07-08 15:08:17 -04:00
|
|
|
signerInfo := []SignerInfo{
|
2017-08-24 18:43:55 -04:00
|
|
|
{Name: "alice", Keys: []string{"key11", "key12"}},
|
|
|
|
{Name: "bob", Keys: []string{"key21"}},
|
|
|
|
{Name: "eve", Keys: []string{"key31", "key32", "foobarbazquxquux"}},
|
|
|
|
}
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
testcase.context.Output = out
|
|
|
|
err := SignerInfoWrite(testcase.context, signerInfo)
|
|
|
|
if err != nil {
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, testcase.expected)
|
2017-08-24 18:43:55 -04:00
|
|
|
} else {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(testcase.expected, out.String()))
|
2017-08-24 18:43:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|