...

Source file src/github.com/opencontainers/go-digest/algorithm_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  	_ "crypto/sha256"
    22  	_ "crypto/sha512"
    23  	"flag"
    24  	"fmt"
    25  	"strings"
    26  	"testing"
    27  )
    28  
    29  func TestFlagInterface(t *testing.T) {
    30  	var (
    31  		alg     Algorithm
    32  		flagSet flag.FlagSet
    33  	)
    34  
    35  	flagSet.Var(&alg, "algorithm", "set the digest algorithm")
    36  	for _, testcase := range []struct {
    37  		Name     string
    38  		Args     []string
    39  		Err      error
    40  		Expected Algorithm
    41  	}{
    42  		{
    43  			Name: "Invalid",
    44  			Args: []string{"-algorithm", "bean"},
    45  			Err:  ErrDigestUnsupported,
    46  		},
    47  		{
    48  			Name:     "Default",
    49  			Args:     []string{"unrelated"},
    50  			Expected: "sha256",
    51  		},
    52  		{
    53  			Name:     "Other",
    54  			Args:     []string{"-algorithm", "sha512"},
    55  			Expected: "sha512",
    56  		},
    57  	} {
    58  		t.Run(testcase.Name, func(t *testing.T) {
    59  			alg = Canonical
    60  			if err := flagSet.Parse(testcase.Args); err != testcase.Err {
    61  				if testcase.Err == nil {
    62  					t.Fatal("unexpected error", err)
    63  				}
    64  
    65  				// check that flag package returns correct error
    66  				if !strings.Contains(err.Error(), testcase.Err.Error()) {
    67  					t.Fatalf("unexpected error: %v != %v", err, testcase.Err)
    68  				}
    69  				return
    70  			}
    71  
    72  			if alg != testcase.Expected {
    73  				t.Fatalf("unexpected algorithm: %v != %v", alg, testcase.Expected)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestFroms(t *testing.T) {
    80  	p := make([]byte, 1<<20)
    81  	rand.Read(p)
    82  
    83  	for alg := range algorithms {
    84  		h := alg.Hash()
    85  		h.Write(p)
    86  		expected := Digest(fmt.Sprintf("%s:%x", alg, h.Sum(nil)))
    87  		readerDgst, err := alg.FromReader(bytes.NewReader(p))
    88  		if err != nil {
    89  			t.Fatalf("error calculating hash from reader: %v", err)
    90  		}
    91  
    92  		dgsts := []Digest{
    93  			alg.FromBytes(p),
    94  			alg.FromString(string(p)),
    95  			readerDgst,
    96  		}
    97  
    98  		if alg == Canonical {
    99  			readerDgst, err := FromReader(bytes.NewReader(p))
   100  			if err != nil {
   101  				t.Fatalf("error calculating hash from reader: %v", err)
   102  			}
   103  
   104  			dgsts = append(dgsts,
   105  				FromBytes(p),
   106  				FromString(string(p)),
   107  				readerDgst)
   108  		}
   109  		for _, dgst := range dgsts {
   110  			if dgst != expected {
   111  				t.Fatalf("unexpected digest %v != %v", dgst, expected)
   112  			}
   113  		}
   114  	}
   115  }
   116  

View as plain text