github.com/anchore/syft@v1.38.2/internal/file/digest_test.go (about) 1 package file 2 3 import ( 4 "context" 5 "crypto" 6 "os" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/anchore/syft/syft/file" 13 ) 14 15 func TestCleanDigestAlgorithmName(t *testing.T) { 16 tests := []struct { 17 name string 18 input string 19 want string 20 }{ 21 { 22 name: "go case", 23 input: "SHA-256", 24 want: "sha256", 25 }, 26 } 27 for _, tt := range tests { 28 t.Run(tt.name, func(t *testing.T) { 29 assert.Equal(t, tt.want, CleanDigestAlgorithmName(tt.input)) 30 }) 31 } 32 } 33 34 func TestNewDigestsFromFile(t *testing.T) { 35 require.NotEmpty(t, supportedHashAlgorithms()) 36 37 tests := []struct { 38 name string 39 fixture string 40 hashes []crypto.Hash 41 want []file.Digest 42 wantErr require.ErrorAssertionFunc 43 }{ 44 { 45 name: "check supported hash algorithms", 46 fixture: "test-fixtures/digest.txt", 47 hashes: supportedHashAlgorithms(), 48 want: []file.Digest{ 49 { 50 Algorithm: "md5", 51 Value: "e8818a24402ae7f8b874cdd9350c1b51", 52 }, 53 { 54 Algorithm: "sha1", 55 Value: "eea4671d168c81fd52e615ed9fb3531a526f4748", 56 }, 57 { 58 Algorithm: "sha224", 59 Value: "fd993e84c7afb449d34bcae7c5ee118f5c73b50170da05171523b22c", 60 }, 61 { 62 Algorithm: "sha256", 63 Value: "cbf1a703b7e4a67529d6e17114880dfa9f879f3749872e1a9d4a20ac509165ad", 64 }, 65 { 66 Algorithm: "sha384", 67 Value: "1eaded3f17fb8d7b731c9175a0f355d3a35575c3cb6cdda46a5272b632968d7257a5e6437d0efae599a81a1b2dcc81ba", 68 }, 69 { 70 Algorithm: "sha512", 71 Value: "b49d5995456edba144dce750eaa8eae12af8fd08c076d401fcf78aac4172080feb70baaa5ed8c1b05046ec278446330fbf77e8ca9e60c03945ded761a641a7e1", 72 }, 73 }, 74 }, 75 } 76 for _, tt := range tests { 77 t.Run(tt.name, func(t *testing.T) { 78 if tt.wantErr == nil { 79 tt.wantErr = require.NoError 80 } 81 82 fh, err := os.Open(tt.fixture) 83 require.NoError(t, err) 84 85 got, err := NewDigestsFromFile(context.TODO(), fh, tt.hashes) 86 tt.wantErr(t, err) 87 if err != nil { 88 return 89 } 90 assert.Equal(t, tt.want, got) 91 }) 92 } 93 } 94 95 func TestHashers(t *testing.T) { 96 tests := []struct { 97 name string 98 names []string 99 want []crypto.Hash 100 wantErr require.ErrorAssertionFunc 101 }{ 102 { 103 name: "check supported hash algorithms", 104 names: []string{"MD-5", "shA1", "sHa224", "sha---256", "sha384", "sha512"}, 105 want: []crypto.Hash{ 106 crypto.MD5, 107 crypto.SHA1, 108 crypto.SHA224, 109 crypto.SHA256, 110 crypto.SHA384, 111 crypto.SHA512, 112 }, 113 }, 114 { 115 name: "error on unsupported hash algorithm", 116 names: []string{"made-up"}, 117 wantErr: require.Error, 118 }, 119 } 120 for _, tt := range tests { 121 t.Run(tt.name, func(t *testing.T) { 122 if tt.wantErr == nil { 123 tt.wantErr = require.NoError 124 } 125 got, err := Hashers(tt.names...) 126 tt.wantErr(t, err) 127 if err != nil { 128 return 129 } 130 assert.Equal(t, tt.want, got) 131 }) 132 } 133 }