...

Source file src/github.com/sigstore/rekor/pkg/util/sha_test.go

Documentation: github.com/sigstore/rekor/pkg/util

     1  // Copyright 2022 The Sigstore Authors.
     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 util
    16  
    17  import (
    18  	"crypto"
    19  	"testing"
    20  )
    21  
    22  func TestPrefixSHA(t *testing.T) {
    23  	var testCases = []struct {
    24  		input string
    25  		want  string
    26  	}{
    27  		{
    28  			input: "123",
    29  			want:  "123",
    30  		},
    31  		{
    32  			input: "sha512:abc",
    33  			want:  "sha512:abc",
    34  		},
    35  		{
    36  			input: "09b80428c53912d4174162fd5b7c7d485bdcc3ab",
    37  			want:  "sha1:09b80428c53912d4174162fd5b7c7d485bdcc3ab",
    38  		},
    39  		{
    40  			input: "b9869be95b24001702120dd5dd673a9bd8447446fb57220388d8d0a48c738808",
    41  			want:  "sha256:b9869be95b24001702120dd5dd673a9bd8447446fb57220388d8d0a48c738808",
    42  		},
    43  		{
    44  			input: "cfd356237e261871e8f92ae6710a75a65a925ae121d94d28533f008bd3e00b5472d261b5d0e1ab4082e3078dd1ad2af57876ed3c1c797c4097dbed870f458408",
    45  			want:  "sha512:cfd356237e261871e8f92ae6710a75a65a925ae121d94d28533f008bd3e00b5472d261b5d0e1ab4082e3078dd1ad2af57876ed3c1c797c4097dbed870f458408",
    46  		},
    47  		{
    48  			input: "78674b244bc9cba8ecb6dcb660b059728236e36b2f30fbcd6e17b1b64255f3ac596fbe5c84d1cc9d2a0979513260de09",
    49  			want:  "sha384:78674b244bc9cba8ecb6dcb660b059728236e36b2f30fbcd6e17b1b64255f3ac596fbe5c84d1cc9d2a0979513260de09",
    50  		},
    51  	}
    52  
    53  	for _, tr := range testCases {
    54  		got := PrefixSHA(tr.input)
    55  		if got != tr.want {
    56  			t.Errorf("Got '%s' expected '%s'", got, tr.want)
    57  		}
    58  	}
    59  }
    60  
    61  func TestUnprefixSHA(t *testing.T) {
    62  	type prefixedSHA struct {
    63  		crypto.Hash
    64  		string
    65  	}
    66  	var testCases = []struct {
    67  		input string
    68  		want  prefixedSHA
    69  	}{
    70  		{
    71  			input: "87428fc522803d31065e7bce3cf03fe475096631e5e07bbd7a0fde60c4cf25c7",
    72  			want: prefixedSHA{
    73  				crypto.SHA256,
    74  				"87428fc522803d31065e7bce3cf03fe475096631e5e07bbd7a0fde60c4cf25c7",
    75  			},
    76  		},
    77  		{
    78  			input: "sha512:162b0b32f02482d5aca0a7c93dd03ceac3acd7e410a5f18f3fb990fc958ae0df6f32233b91831eaf99ca581a8c4ddf9c8ba315ac482db6d4ea01cc7884a635be",
    79  			want: prefixedSHA{
    80  				crypto.SHA512,
    81  				"162b0b32f02482d5aca0a7c93dd03ceac3acd7e410a5f18f3fb990fc958ae0df6f32233b91831eaf99ca581a8c4ddf9c8ba315ac482db6d4ea01cc7884a635be",
    82  			},
    83  		},
    84  		{
    85  			input: "09b80428c53912d4174162fd5b7c7d485bdcc3ab",
    86  			want: prefixedSHA{
    87  				crypto.SHA1,
    88  				"09b80428c53912d4174162fd5b7c7d485bdcc3ab",
    89  			},
    90  		},
    91  		{
    92  			input: "cfd356237e261871e8f92ae6710a75a65a925ae121d94d28533f008bd3e00b5472d261b5d0e1ab4082e3078dd1ad2af57876ed3c1c797c4097dbed870f458408",
    93  			want: prefixedSHA{
    94  				crypto.SHA512,
    95  				"cfd356237e261871e8f92ae6710a75a65a925ae121d94d28533f008bd3e00b5472d261b5d0e1ab4082e3078dd1ad2af57876ed3c1c797c4097dbed870f458408",
    96  			},
    97  		},
    98  		{
    99  			input: "78674b244bc9cba8ecb6dcb660b059728236e36b2f30fbcd6e17b1b64255f3ac596fbe5c84d1cc9d2a0979513260de09",
   100  			want: prefixedSHA{
   101  				crypto.SHA384,
   102  				"78674b244bc9cba8ecb6dcb660b059728236e36b2f30fbcd6e17b1b64255f3ac596fbe5c84d1cc9d2a0979513260de09",
   103  			},
   104  		},
   105  		{
   106  			input: "sha384:78674b244bc9cba8ecb6dcb660b059728236e36b2f30fbcd6e17b1b64255f3ac596fbe5c84d1cc9d2a0979513260de09",
   107  			want: prefixedSHA{
   108  				crypto.SHA384,
   109  				"78674b244bc9cba8ecb6dcb660b059728236e36b2f30fbcd6e17b1b64255f3ac596fbe5c84d1cc9d2a0979513260de09",
   110  			},
   111  		},
   112  	}
   113  
   114  	for _, tr := range testCases {
   115  		algo, value := UnprefixSHA(tr.input)
   116  		got := prefixedSHA{algo, value}
   117  		if got != tr.want {
   118  			t.Errorf("Got '%v' expected '%v' (input %s)", got, tr.want, tr.input)
   119  		}
   120  	}
   121  }
   122  

View as plain text