...

Source file src/github.com/opencontainers/go-digest/verifiers_test.go

Documentation: github.com/opencontainers/go-digest

     1  // Copyright 2019, 2020 OCI Contributors
     2  // Copyright 2017 Docker, Inc.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     https://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package digest
    17  
    18  import (
    19  	"bytes"
    20  	"crypto/rand"
    21  	"io"
    22  	"reflect"
    23  	"testing"
    24  )
    25  
    26  func TestDigestVerifier(t *testing.T) {
    27  	p := make([]byte, 1<<20)
    28  	rand.Read(p)
    29  	digest := FromBytes(p)
    30  
    31  	verifier := digest.Verifier()
    32  
    33  	io.Copy(verifier, bytes.NewReader(p))
    34  
    35  	if !verifier.Verified() {
    36  		t.Fatalf("bytes not verified")
    37  	}
    38  }
    39  
    40  // TestVerifierUnsupportedDigest ensures that unsupported digest validation is
    41  // flowing through verifier creation.
    42  func TestVerifierUnsupportedDigest(t *testing.T) {
    43  	for _, testcase := range []struct {
    44  		Name     string
    45  		Digest   Digest
    46  		Expected interface{} // expected panic target
    47  	}{
    48  		{
    49  			Name:     "Empty",
    50  			Digest:   "",
    51  			Expected: "no ':' separator in digest \"\"",
    52  		},
    53  		{
    54  			Name:     "EmptyAlg",
    55  			Digest:   ":",
    56  			Expected: "empty digest algorithm, validate before calling Algorithm.Hash()",
    57  		},
    58  		{
    59  			Name:     "Unsupported",
    60  			Digest:   Digest("bean:0123456789abcdef"),
    61  			Expected: "bean not available (make sure it is imported)",
    62  		},
    63  		{
    64  			Name:     "Garbage",
    65  			Digest:   Digest("sha256-garbage:pure"),
    66  			Expected: "sha256-garbage not available (make sure it is imported)",
    67  		},
    68  	} {
    69  		t.Run(testcase.Name, func(t *testing.T) {
    70  			expected := testcase.Expected
    71  			defer func() {
    72  				recovered := recover()
    73  				if !reflect.DeepEqual(recovered, expected) {
    74  					t.Fatalf("unexpected recover: %v != %v", recovered, expected)
    75  				}
    76  			}()
    77  
    78  			_ = testcase.Digest.Verifier()
    79  		})
    80  	}
    81  }
    82  

View as plain text