github.com/dtroyer-salad/og2/v2@v2.0.0-20240412154159-c47231610877/content/verifiers_test.go (about)

     1  /*
     2  Copyright The ORAS Authors.
     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  
    16  package content
    17  
    18  import (
    19  	"bytes"
    20  	"crypto/rand"
    21  	_ "crypto/sha256"
    22  	"io"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/opencontainers/go-digest"
    27  )
    28  
    29  // Copied from https://github.com/opencontainers/go-digest/blob/master/digest.go
    30  
    31  // FromBytes digests the input and returns a Digest.
    32  func FromBytes(p []byte) digest.Digest {
    33  	return digest.Canonical.FromBytes(p)
    34  }
    35  
    36  // Copied from https://github.com/opencontainers/go-digest/blob/master/verifiers_test.go
    37  
    38  func TestDigestVerifier(t *testing.T) {
    39  	p := make([]byte, 1<<20)
    40  	rand.Read(p)
    41  	content := FromBytes(p)
    42  
    43  	verifier := content.Verifier()
    44  
    45  	io.Copy(verifier, bytes.NewReader(p))
    46  
    47  	if !verifier.Verified() {
    48  		t.Fatalf("bytes not verified")
    49  	}
    50  }
    51  
    52  // TestVerifierUnsupportedDigest ensures that unsupported digest validation is
    53  // flowing through verifier creation.
    54  func TestVerifierUnsupportedDigest(t *testing.T) {
    55  	for _, testcase := range []struct {
    56  		Name     string
    57  		Digest   digest.Digest
    58  		Expected interface{} // expected panic target
    59  	}{
    60  		{
    61  			Name:     "Empty",
    62  			Digest:   "",
    63  			Expected: "no ':' separator in digest \"\"",
    64  		},
    65  		{
    66  			Name:     "EmptyAlg",
    67  			Digest:   ":",
    68  			Expected: "empty digest algorithm, validate before calling Algorithm.Hash()",
    69  		},
    70  		{
    71  			Name:     "Unsupported",
    72  			Digest:   digest.Digest("bean:0123456789abcdef"),
    73  			Expected: "bean not available (make sure it is imported)",
    74  		},
    75  		{
    76  			Name:     "Garbage",
    77  			Digest:   digest.Digest("sha256-garbage:pure"),
    78  			Expected: "sha256-garbage not available (make sure it is imported)",
    79  		},
    80  	} {
    81  		t.Run(testcase.Name, func(t *testing.T) {
    82  			expected := testcase.Expected
    83  			defer func() {
    84  				recovered := recover()
    85  				if !reflect.DeepEqual(recovered, expected) {
    86  					t.Fatalf("unexpected recover: %v != %v", recovered, expected)
    87  				}
    88  			}()
    89  
    90  			_ = testcase.Digest.Verifier()
    91  		})
    92  	}
    93  }