github.com/lusis/distribution@v2.0.1+incompatible/manifest/manifest_test.go (about)

     1  package manifest
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/docker/libtrust"
    10  )
    11  
    12  type testEnv struct {
    13  	name, tag string
    14  	manifest  *Manifest
    15  	signed    *SignedManifest
    16  	pk        libtrust.PrivateKey
    17  }
    18  
    19  func TestManifestMarshaling(t *testing.T) {
    20  	env := genEnv(t)
    21  
    22  	// Check that the Raw field is the same as json.MarshalIndent with these
    23  	// parameters.
    24  	p, err := json.MarshalIndent(env.signed, "", "   ")
    25  	if err != nil {
    26  		t.Fatalf("error marshaling manifest: %v", err)
    27  	}
    28  
    29  	if !bytes.Equal(p, env.signed.Raw) {
    30  		t.Fatalf("manifest bytes not equal: %q != %q", string(env.signed.Raw), string(p))
    31  	}
    32  }
    33  
    34  func TestManifestUnmarshaling(t *testing.T) {
    35  	env := genEnv(t)
    36  
    37  	var signed SignedManifest
    38  	if err := json.Unmarshal(env.signed.Raw, &signed); err != nil {
    39  		t.Fatalf("error unmarshaling signed manifest: %v", err)
    40  	}
    41  
    42  	if !reflect.DeepEqual(&signed, env.signed) {
    43  		t.Fatalf("manifests are different after unmarshaling: %v != %v", signed, env.signed)
    44  	}
    45  }
    46  
    47  func TestManifestVerification(t *testing.T) {
    48  	env := genEnv(t)
    49  
    50  	publicKeys, err := Verify(env.signed)
    51  	if err != nil {
    52  		t.Fatalf("error verifying manifest: %v", err)
    53  	}
    54  
    55  	if len(publicKeys) == 0 {
    56  		t.Fatalf("no public keys found in signature")
    57  	}
    58  
    59  	var found bool
    60  	publicKey := env.pk.PublicKey()
    61  	// ensure that one of the extracted public keys matches the private key.
    62  	for _, candidate := range publicKeys {
    63  		if candidate.KeyID() == publicKey.KeyID() {
    64  			found = true
    65  			break
    66  		}
    67  	}
    68  
    69  	if !found {
    70  		t.Fatalf("expected public key, %v, not found in verified keys: %v", publicKey, publicKeys)
    71  	}
    72  }
    73  
    74  func genEnv(t *testing.T) *testEnv {
    75  	pk, err := libtrust.GenerateECP256PrivateKey()
    76  	if err != nil {
    77  		t.Fatalf("error generating test key: %v", err)
    78  	}
    79  
    80  	name, tag := "foo/bar", "test"
    81  
    82  	m := Manifest{
    83  		Versioned: Versioned{
    84  			SchemaVersion: 1,
    85  		},
    86  		Name: name,
    87  		Tag:  tag,
    88  		FSLayers: []FSLayer{
    89  			{
    90  				BlobSum: "asdf",
    91  			},
    92  			{
    93  				BlobSum: "qwer",
    94  			},
    95  		},
    96  	}
    97  
    98  	sm, err := Sign(&m, pk)
    99  	if err != nil {
   100  		t.Fatalf("error signing manifest: %v", err)
   101  	}
   102  
   103  	return &testEnv{
   104  		name:     name,
   105  		tag:      tag,
   106  		manifest: &m,
   107  		signed:   sm,
   108  		pk:       pk,
   109  	}
   110  }