2015-08-21 04:24:30 +00:00
|
|
|
package schema1
|
2015-01-02 23:24:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/libtrust"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testEnv struct {
|
2015-11-03 19:03:17 +00:00
|
|
|
name, tag string
|
|
|
|
invalidSigned *SignedManifest
|
|
|
|
signed *SignedManifest
|
|
|
|
pk libtrust.PrivateKey
|
2015-01-02 23:24:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifestMarshaling(t *testing.T) {
|
|
|
|
env := genEnv(t)
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// Check that the all field is the same as json.MarshalIndent with these
|
2015-01-02 23:24:10 +00:00
|
|
|
// parameters.
|
|
|
|
p, err := json.MarshalIndent(env.signed, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error marshaling manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
if !bytes.Equal(p, env.signed.all) {
|
|
|
|
t.Fatalf("manifest bytes not equal: %q != %q", string(env.signed.all), string(p))
|
2015-01-02 23:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifestUnmarshaling(t *testing.T) {
|
|
|
|
env := genEnv(t)
|
|
|
|
|
|
|
|
var signed SignedManifest
|
2015-08-21 04:50:15 +00:00
|
|
|
if err := json.Unmarshal(env.signed.all, &signed); err != nil {
|
2015-01-02 23:24:10 +00:00
|
|
|
t.Fatalf("error unmarshaling signed manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(&signed, env.signed) {
|
|
|
|
t.Fatalf("manifests are different after unmarshaling: %v != %v", signed, env.signed)
|
|
|
|
}
|
2015-11-03 19:03:17 +00:00
|
|
|
|
2015-01-02 23:24:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifestVerification(t *testing.T) {
|
|
|
|
env := genEnv(t)
|
|
|
|
|
2015-01-02 23:46:47 +00:00
|
|
|
publicKeys, err := Verify(env.signed)
|
2015-01-02 23:24:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error verifying manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(publicKeys) == 0 {
|
|
|
|
t.Fatalf("no public keys found in signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
var found bool
|
|
|
|
publicKey := env.pk.PublicKey()
|
|
|
|
// ensure that one of the extracted public keys matches the private key.
|
|
|
|
for _, candidate := range publicKeys {
|
|
|
|
if candidate.KeyID() == publicKey.KeyID() {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("expected public key, %v, not found in verified keys: %v", publicKey, publicKeys)
|
|
|
|
}
|
2015-11-03 19:03:17 +00:00
|
|
|
|
|
|
|
// Check that an invalid manifest fails verification
|
|
|
|
_, err = Verify(env.invalidSigned)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Invalid manifest should not pass Verify()")
|
|
|
|
}
|
2015-01-02 23:24:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func genEnv(t *testing.T) *testEnv {
|
|
|
|
pk, err := libtrust.GenerateECP256PrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error generating test key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name, tag := "foo/bar", "test"
|
|
|
|
|
2015-11-03 19:03:17 +00:00
|
|
|
invalid := Manifest{
|
2015-08-21 04:24:30 +00:00
|
|
|
Versioned: SchemaVersion,
|
|
|
|
Name: name,
|
|
|
|
Tag: tag,
|
2015-01-02 23:24:10 +00:00
|
|
|
FSLayers: []FSLayer{
|
|
|
|
{
|
|
|
|
BlobSum: "asdf",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BlobSum: "qwer",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-11-03 19:03:17 +00:00
|
|
|
valid := Manifest{
|
|
|
|
Versioned: SchemaVersion,
|
|
|
|
Name: name,
|
|
|
|
Tag: tag,
|
|
|
|
FSLayers: []FSLayer{
|
|
|
|
{
|
|
|
|
BlobSum: "asdf",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
History: []History{
|
|
|
|
{
|
|
|
|
V1Compatibility: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sm, err := Sign(&valid, pk)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error signing manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidSigned, err := Sign(&invalid, pk)
|
2015-01-02 23:24:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error signing manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &testEnv{
|
2015-11-03 19:03:17 +00:00
|
|
|
name: name,
|
|
|
|
tag: tag,
|
|
|
|
invalidSigned: invalidSigned,
|
|
|
|
signed: sm,
|
|
|
|
pk: pk,
|
2015-01-02 23:24:10 +00:00
|
|
|
}
|
|
|
|
}
|