...

Source file src/github.com/opencontainers/image-spec/identity/chainid_test.go

Documentation: github.com/opencontainers/image-spec/identity

     1  // Copyright 2016 The Linux Foundation
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package identity
    16  
    17  import (
    18  	_ "crypto/sha256" // required to install sha256 digest support
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/opencontainers/go-digest"
    23  )
    24  
    25  func TestChainID(t *testing.T) {
    26  	// To provide a good testing base, we define the individual links in a
    27  	// chain recursively, illustrating the calculations for each chain.
    28  	//
    29  	// Note that we use invalid digests for the unmodified identifiers here to
    30  	// make the computation more readable.
    31  	chainDigestAB := digest.FromString("sha256:a" + " " + "sha256:b")              // chain for A|B
    32  	chainDigestABC := digest.FromString(chainDigestAB.String() + " " + "sha256:c") // chain for A|B|C
    33  
    34  	for _, testcase := range []struct {
    35  		Name     string
    36  		Digests  []digest.Digest
    37  		Expected []digest.Digest
    38  	}{
    39  		{
    40  			Name: "nil",
    41  		},
    42  		{
    43  			Name:     "empty",
    44  			Digests:  []digest.Digest{},
    45  			Expected: []digest.Digest{},
    46  		},
    47  		{
    48  			Name:     "identity",
    49  			Digests:  []digest.Digest{"sha256:a"},
    50  			Expected: []digest.Digest{"sha256:a"},
    51  		},
    52  		{
    53  			Name:     "two",
    54  			Digests:  []digest.Digest{"sha256:a", "sha256:b"},
    55  			Expected: []digest.Digest{"sha256:a", chainDigestAB},
    56  		},
    57  		{
    58  			Name:     "three",
    59  			Digests:  []digest.Digest{"sha256:a", "sha256:b", "sha256:c"},
    60  			Expected: []digest.Digest{"sha256:a", chainDigestAB, chainDigestABC},
    61  		},
    62  	} {
    63  		t.Run(testcase.Name, func(t *testing.T) {
    64  			t.Log("before", testcase.Digests)
    65  
    66  			var ids []digest.Digest
    67  
    68  			if testcase.Digests != nil {
    69  				ids = make([]digest.Digest, len(testcase.Digests))
    70  				copy(ids, testcase.Digests)
    71  			}
    72  
    73  			ids = ChainIDs(ids)
    74  			t.Log("after", ids)
    75  			if !reflect.DeepEqual(ids, testcase.Expected) {
    76  				t.Errorf("unexpected chain: %v != %v", ids, testcase.Expected)
    77  			}
    78  
    79  			if len(testcase.Digests) == 0 {
    80  				return
    81  			}
    82  
    83  			// Make sure parent stays stable
    84  			if ids[0] != testcase.Digests[0] {
    85  				t.Errorf("parent changed: %v != %v", ids[0], testcase.Digests[0])
    86  			}
    87  
    88  			// make sure that the ChainID function takes the last element
    89  			id := ChainID(testcase.Digests)
    90  			if id != ids[len(ids)-1] {
    91  				t.Errorf("incorrect chain id returned from ChainID: %v != %v", id, ids[len(ids)-1])
    92  			}
    93  		})
    94  	}
    95  }
    96  

View as plain text