...

Source file src/github.com/google/go-containerregistry/internal/verify/verify_test.go

Documentation: github.com/google/go-containerregistry/internal/verify

     1  // Copyright 2020 Google LLC All Rights Reserved.
     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 verify
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"fmt"
    21  	"io"
    22  	"strings"
    23  	"testing"
    24  
    25  	v1 "github.com/google/go-containerregistry/pkg/v1"
    26  )
    27  
    28  func mustHash(s string, t *testing.T) v1.Hash {
    29  	h, _, err := v1.SHA256(strings.NewReader(s))
    30  	if err != nil {
    31  		t.Fatalf("v1.SHA256(%s) = %v", s, err)
    32  	}
    33  	t.Logf("Hashed: %q -> %q", s, h)
    34  	return h
    35  }
    36  
    37  func TestVerificationFailure(t *testing.T) {
    38  	want := "This is the input string."
    39  	buf := bytes.NewBufferString(want)
    40  
    41  	verified, err := ReadCloser(io.NopCloser(buf), int64(len(want)), mustHash("not the same", t))
    42  	if err != nil {
    43  		t.Fatal("ReadCloser() =", err)
    44  	}
    45  	if b, err := io.ReadAll(verified); err == nil {
    46  		t.Errorf("ReadAll() = %q; want verification error", string(b))
    47  	}
    48  }
    49  
    50  func TestVerification(t *testing.T) {
    51  	want := "This is the input string."
    52  	buf := bytes.NewBufferString(want)
    53  
    54  	verified, err := ReadCloser(io.NopCloser(buf), int64(len(want)), mustHash(want, t))
    55  	if err != nil {
    56  		t.Fatal("ReadCloser() =", err)
    57  	}
    58  	if _, err := io.ReadAll(verified); err != nil {
    59  		t.Error("ReadAll() =", err)
    60  	}
    61  }
    62  
    63  func TestVerificationSizeUnknown(t *testing.T) {
    64  	want := "This is the input string."
    65  	buf := bytes.NewBufferString(want)
    66  
    67  	verified, err := ReadCloser(io.NopCloser(buf), SizeUnknown, mustHash(want, t))
    68  	if err != nil {
    69  		t.Fatal("ReadCloser() =", err)
    70  	}
    71  	if _, err := io.ReadAll(verified); err != nil {
    72  		t.Error("ReadAll() =", err)
    73  	}
    74  }
    75  
    76  func TestBadHash(t *testing.T) {
    77  	h := v1.Hash{
    78  		Algorithm: "fake256",
    79  		Hex:       "whatever",
    80  	}
    81  	_, err := ReadCloser(io.NopCloser(strings.NewReader("hi")), 0, h)
    82  	if err == nil {
    83  		t.Errorf("ReadCloser() = %v, wanted err", err)
    84  	}
    85  }
    86  
    87  func TestBadSize(t *testing.T) {
    88  	want := "This is the input string."
    89  
    90  	// having too much content or expecting too much content returns an error.
    91  	for _, size := range []int64{3, 100} {
    92  		t.Run(fmt.Sprintf("expecting size %d", size), func(t *testing.T) {
    93  			buf := bytes.NewBufferString(want)
    94  			rc, err := ReadCloser(io.NopCloser(buf), size, mustHash(want, t))
    95  			if err != nil {
    96  				t.Fatal("ReadCloser() =", err)
    97  			}
    98  			if b, err := io.ReadAll(rc); err == nil {
    99  				t.Errorf("ReadAll() = %q; want verification error", string(b))
   100  			}
   101  		})
   102  	}
   103  }
   104  
   105  func TestDescriptor(t *testing.T) {
   106  	for _, tc := range []struct {
   107  		err  error
   108  		desc v1.Descriptor
   109  	}{{
   110  		err: errors.New("error verifying descriptor; Data == nil"),
   111  	}, {
   112  		err: errors.New(`error verifying Digest; got "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", want ":"`),
   113  		desc: v1.Descriptor{
   114  			Data: []byte("abc"),
   115  		},
   116  	}, {
   117  		err: errors.New("error verifying Size; got 3, want 0"),
   118  		desc: v1.Descriptor{
   119  			Data: []byte("abc"),
   120  			Digest: v1.Hash{
   121  				Algorithm: "sha256",
   122  				Hex:       "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
   123  			},
   124  		},
   125  	}, {
   126  		desc: v1.Descriptor{
   127  			Data: []byte("abc"),
   128  			Size: 3,
   129  			Digest: v1.Hash{
   130  				Algorithm: "sha256",
   131  				Hex:       "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
   132  			},
   133  		},
   134  	}} {
   135  		got, want := Descriptor(tc.desc), tc.err
   136  
   137  		if got == nil {
   138  			if want != nil {
   139  				t.Errorf("Descriptor(): got nil, want %v", want)
   140  			}
   141  		} else if want == nil {
   142  			t.Errorf("Descriptor(): got %v, want nil", got)
   143  		} else if got, want := got.Error(), want.Error(); got != want {
   144  			t.Errorf("Descriptor(): got %q, want %q", got, want)
   145  		}
   146  	}
   147  }
   148  

View as plain text