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{
|
2022-09-29 11:21:51 -04:00
|
|
|
s: SignedTagInfo{
|
|
|
|
Name: trustedTag,
|
2017-08-24 18:43:55 -04:00
|
|
|
Digest: digest,
|
|
|
|
Signers: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
digest,
|
|
|
|
ctx.Digest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
trustTagContext{
|
2022-09-29 11:21:51 -04:00
|
|
|
s: SignedTagInfo{
|
|
|
|
Name: trustedTag,
|
2017-08-24 18:43:55 -04:00
|
|
|
Digest: digest,
|
|
|
|
Signers: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
trustedTag,
|
|
|
|
ctx.SignedTag,
|
|
|
|
},
|
|
|
|
// Empty signers makes a row with empty string
|
|
|
|
{
|
|
|
|
trustTagContext{
|
2022-09-29 11:21:51 -04:00
|
|
|
s: SignedTagInfo{
|
|
|
|
Name: trustedTag,
|
2017-08-24 18:43:55 -04:00
|
|
|
Digest: digest,
|
|
|
|
Signers: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
ctx.Signers,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
trustTagContext{
|
2022-09-29 11:21:51 -04:00
|
|
|
s: SignedTagInfo{
|
|
|
|
Name: trustedTag,
|
2017-08-24 18:43:55 -04:00
|
|
|
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{
|
2022-09-29 11:21:51 -04:00
|
|
|
s: SignedTagInfo{
|
|
|
|
Name: trustedTag,
|
2017-08-24 18:43:55 -04:00
|
|
|
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}}",
|
|
|
|
},
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
`template parsing error: template: :1: function "InvalidFunction" not defined`,
|
2017-08-24 18:43:55 -04:00
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: "{{nil}}",
|
|
|
|
},
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
`template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
|
2017-08-24 18:43:55 -04:00
|
|
|
},
|
|
|
|
// Table Format
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: NewTrustTagFormat(),
|
|
|
|
},
|
2020-08-28 17:00:21 -04:00
|
|
|
`SIGNED TAG DIGEST SIGNERS
|
|
|
|
tag1 deadbeef alice
|
|
|
|
tag2 aaaaaaaa alice, bob
|
|
|
|
tag3 bbbbbbbb
|
2017-08-24 18:43:55 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-28 17:00:21 -04:00
|
|
|
signedTags := []SignedTagInfo{
|
|
|
|
{Name: "tag1", Digest: "deadbeef", Signers: []string{"alice"}},
|
|
|
|
{Name: "tag2", Digest: "aaaaaaaa", Signers: []string{"alice", "bob"}},
|
|
|
|
{Name: "tag3", Digest: "bbbbbbbb", Signers: []string{}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(string(tc.context.Format), func(t *testing.T) {
|
|
|
|
var out bytes.Buffer
|
|
|
|
tc.context.Output = &out
|
|
|
|
|
|
|
|
if err := TagWrite(tc.context, signedTags); err != nil {
|
|
|
|
assert.Error(t, err, tc.expected)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, out.String(), tc.expected)
|
|
|
|
}
|
|
|
|
})
|
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(),
|
|
|
|
},
|
2020-08-28 17:00:21 -04:00
|
|
|
`SIGNED TAG DIGEST SIGNERS
|
2017-08-24 18:43:55 -04:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
},
|
2020-08-28 17:00:21 -04:00
|
|
|
`SIGNER KEYS
|
2017-08-24 18:43:55 -04:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
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}}",
|
|
|
|
},
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
`template parsing error: template: :1: function "InvalidFunction" not defined`,
|
2017-08-24 18:43:55 -04:00
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: "{{nil}}",
|
|
|
|
},
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
`template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
|
2017-08-24 18:43:55 -04:00
|
|
|
},
|
|
|
|
// Table Format
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: NewSignerInfoFormat(),
|
|
|
|
Trunc: true,
|
|
|
|
},
|
2020-08-28 17:00:21 -04:00
|
|
|
`SIGNER KEYS
|
|
|
|
alice key11, key12
|
|
|
|
bob key21
|
|
|
|
eve foobarbazqux, key31, key32
|
2017-08-24 18:43:55 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
// No truncation
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{
|
2017-08-24 18:43:55 -04:00
|
|
|
Format: NewSignerInfoFormat(),
|
|
|
|
},
|
2020-08-28 17:00:21 -04:00
|
|
|
`SIGNER KEYS
|
|
|
|
alice key11, key12
|
|
|
|
bob key21
|
|
|
|
eve foobarbazquxquux, key31, key32
|
2017-08-24 18:43:55 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-28 17:00:21 -04:00
|
|
|
signerInfo := []SignerInfo{
|
|
|
|
{Name: "alice", Keys: []string{"key11", "key12"}},
|
|
|
|
{Name: "bob", Keys: []string{"key21"}},
|
|
|
|
{Name: "eve", Keys: []string{"key31", "key32", "foobarbazquxquux"}},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(string(tc.context.Format), func(t *testing.T) {
|
|
|
|
var out bytes.Buffer
|
|
|
|
tc.context.Output = &out
|
|
|
|
|
|
|
|
if err := SignerInfoWrite(tc.context, signerInfo); err != nil {
|
|
|
|
assert.Error(t, err, tc.expected)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, out.String(), tc.expected)
|
|
|
|
}
|
|
|
|
})
|
2017-08-24 18:43:55 -04:00
|
|
|
}
|
|
|
|
}
|