...

Source file src/sigs.k8s.io/release-utils/hash/hash_test.go

Documentation: sigs.k8s.io/release-utils/hash

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     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      http://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  
    17  package hash_test
    18  
    19  import (
    20  	"crypto/sha1" //nolint: gosec
    21  	"crypto/sha256"
    22  	"hash"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  	kHash "sigs.k8s.io/release-utils/hash"
    28  )
    29  
    30  func TestSHA512ForFile(t *testing.T) {
    31  	for _, tc := range []struct {
    32  		prepare     func() string
    33  		expected    string
    34  		shouldError bool
    35  	}{
    36  		{ // success
    37  			prepare: func() string {
    38  				f, err := os.CreateTemp("", "")
    39  				require.Nil(t, err)
    40  
    41  				_, err = f.WriteString("test")
    42  				require.Nil(t, err)
    43  
    44  				return f.Name()
    45  			},
    46  			expected: "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f88" +
    47  				"19a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc" +
    48  				"5fa9ad8e6f57f50028a8ff",
    49  			shouldError: false,
    50  		},
    51  		{ // error open file
    52  			prepare:     func() string { return "" },
    53  			shouldError: true,
    54  		},
    55  	} {
    56  		filename := tc.prepare()
    57  
    58  		res, err := kHash.SHA512ForFile(filename)
    59  
    60  		if tc.shouldError {
    61  			require.NotNil(t, err)
    62  		} else {
    63  			require.Nil(t, err)
    64  			require.Equal(t, tc.expected, res)
    65  		}
    66  	}
    67  }
    68  
    69  func TestSHA256ForFile(t *testing.T) {
    70  	for _, tc := range []struct {
    71  		prepare     func() string
    72  		expected    string
    73  		shouldError bool
    74  	}{
    75  		{ // success
    76  			prepare: func() string {
    77  				f, err := os.CreateTemp("", "")
    78  				require.Nil(t, err)
    79  
    80  				_, err = f.WriteString("test")
    81  				require.Nil(t, err)
    82  
    83  				return f.Name()
    84  			},
    85  			expected:    "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
    86  			shouldError: false,
    87  		},
    88  		{ // error open file
    89  			prepare:     func() string { return "" },
    90  			shouldError: true,
    91  		},
    92  	} {
    93  		filename := tc.prepare()
    94  
    95  		res, err := kHash.SHA256ForFile(filename)
    96  
    97  		if tc.shouldError {
    98  			require.NotNil(t, err)
    99  		} else {
   100  			require.Nil(t, err)
   101  			require.Equal(t, tc.expected, res)
   102  		}
   103  	}
   104  }
   105  
   106  func TestSHA1ForFile(t *testing.T) {
   107  	for _, tc := range []struct {
   108  		prepare     func() string
   109  		expected    string
   110  		shouldError bool
   111  	}{
   112  		{ // success
   113  			prepare: func() string {
   114  				f, err := os.CreateTemp("", "")
   115  				require.Nil(t, err)
   116  
   117  				_, err = f.WriteString("test")
   118  				require.Nil(t, err)
   119  
   120  				return f.Name()
   121  			},
   122  			expected:    "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",
   123  			shouldError: false,
   124  		},
   125  		{ // error open file
   126  			prepare:     func() string { return "" },
   127  			shouldError: true,
   128  		},
   129  	} {
   130  		filename := tc.prepare()
   131  
   132  		res, err := kHash.SHA1ForFile(filename)
   133  
   134  		if tc.shouldError {
   135  			require.NotNil(t, err)
   136  		} else {
   137  			require.Nil(t, err)
   138  			require.Equal(t, tc.expected, res)
   139  		}
   140  	}
   141  }
   142  
   143  func TestForFile(t *testing.T) {
   144  	for _, tc := range []struct {
   145  		prepare     func() (string, hash.Hash)
   146  		expected    string
   147  		shouldError bool
   148  	}{
   149  		{ // success
   150  			prepare: func() (string, hash.Hash) {
   151  				f, err := os.CreateTemp("", "")
   152  				require.Nil(t, err)
   153  
   154  				_, err = f.WriteString("test")
   155  				require.Nil(t, err)
   156  
   157  				return f.Name(), sha1.New() //nolint: gosec
   158  			},
   159  			expected:    "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",
   160  			shouldError: false,
   161  		},
   162  		{ // error hasher is nil
   163  			prepare: func() (string, hash.Hash) {
   164  				return "", nil
   165  			},
   166  			shouldError: true,
   167  		},
   168  		{ // error file does not exist is nil
   169  			prepare: func() (string, hash.Hash) {
   170  				return "", sha256.New()
   171  			},
   172  			shouldError: true,
   173  		},
   174  	} {
   175  		filename, hasher := tc.prepare()
   176  
   177  		res, err := kHash.ForFile(filename, hasher)
   178  
   179  		if tc.shouldError {
   180  			require.NotNil(t, err)
   181  		} else {
   182  			require.Nil(t, err)
   183  			require.Equal(t, tc.expected, res)
   184  		}
   185  	}
   186  }
   187  

View as plain text