github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/golang/scan_binary_test.go (about) 1 package golang 2 3 import ( 4 "fmt" 5 "io" 6 "runtime/debug" 7 "testing" 8 9 "github.com/kastenhq/goversion/version" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func Test_getBuildInfo(t *testing.T) { 14 type args struct { 15 r io.ReaderAt 16 } 17 tests := []struct { 18 name string 19 args args 20 wantBi *debug.BuildInfo 21 wantErr assert.ErrorAssertionFunc 22 }{ 23 { 24 name: "recover from panic", 25 args: args{ 26 r: nil, // trying to use a nil reader will cause a panic 27 }, 28 wantBi: nil, // we should not return anything useful 29 wantErr: assert.Error, 30 }, 31 } 32 for _, tt := range tests { 33 t.Run(tt.name, func(t *testing.T) { 34 gotBi, err := getBuildInfo(tt.args.r) 35 if !tt.wantErr(t, err, fmt.Sprintf("getBuildInfo(%v)", tt.args.r)) { 36 return 37 } 38 assert.Equalf(t, tt.wantBi, gotBi, "getBuildInfo(%v)", tt.args.r) 39 }) 40 } 41 } 42 43 func Test_getCryptoSettingsFromVersion(t *testing.T) { 44 for _, tt := range []struct { 45 name string 46 version version.Version 47 result []string 48 }{ 49 { 50 name: "standard crypto", 51 version: version.Version{ 52 StandardCrypto: true, 53 }, 54 result: []string{"standard-crypto"}, 55 }, 56 { 57 name: "boring crypto", 58 version: version.Version{ 59 BoringCrypto: true, 60 }, 61 result: []string{"boring-crypto"}, 62 }, 63 { // Should never see this. Boring crypto is required for fipsonly 64 name: "fipsonly", 65 version: version.Version{ 66 FIPSOnly: true, 67 }, 68 result: []string{"crypto/tls/fipsonly"}, 69 }, 70 { 71 name: "boring crypto and fipsonly", 72 version: version.Version{ 73 BoringCrypto: true, 74 FIPSOnly: true, 75 }, 76 result: []string{"boring-crypto", "crypto/tls/fipsonly"}, 77 }, 78 { // Should never see this. 79 name: "boring and standard crypto!", 80 version: version.Version{ 81 BoringCrypto: true, 82 StandardCrypto: true, 83 }, 84 result: []string{"boring-crypto", "standard-crypto"}, 85 }, 86 { // Should never see this. Boring crypto is required for fipsonly 87 name: "fipsonly and standard crypto!", 88 version: version.Version{ 89 FIPSOnly: true, 90 StandardCrypto: true, 91 }, 92 result: []string{"crypto/tls/fipsonly", "standard-crypto"}, 93 }, 94 95 { // Should never see this. Boring crypto is required for fipsonly 96 name: "fipsonly boringcrypto and standard crypto!", 97 version: version.Version{ 98 FIPSOnly: true, 99 StandardCrypto: true, 100 BoringCrypto: true, 101 }, 102 result: []string{"crypto/tls/fipsonly", "standard-crypto", "boring-crypto"}, 103 }, 104 } { 105 t.Run(tt.name, func(t *testing.T) { 106 res := getCryptoSettingsFromVersion(tt.version) 107 assert.ElementsMatch(t, res, tt.result) 108 }) 109 } 110 }