github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/formats/gosbomjson/to_format_model_test.go (about) 1 package gosbomjson 2 3 import ( 4 "testing" 5 6 "github.com/nextlinux/gosbom/gosbom/file" 7 "github.com/nextlinux/gosbom/gosbom/formats/gosbomjson/model" 8 "github.com/nextlinux/gosbom/gosbom/source" 9 "github.com/scylladb/go-set/strset" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 stereoscopeFile "github.com/anchore/stereoscope/pkg/file" 14 ) 15 16 func Test_toSourceModel(t *testing.T) { 17 allSchemes := strset.New() 18 for _, s := range source.AllSchemes { 19 allSchemes.Add(string(s)) 20 } 21 testedSchemes := strset.New() 22 23 tests := []struct { 24 name string 25 src source.Metadata 26 expected model.Source 27 }{ 28 { 29 name: "directory", 30 src: source.Metadata{ 31 ID: "test-id", 32 Scheme: source.DirectoryScheme, 33 Path: "some/path", 34 }, 35 expected: model.Source{ 36 ID: "test-id", 37 Type: "directory", 38 Target: "some/path", 39 }, 40 }, 41 { 42 name: "file", 43 src: source.Metadata{ 44 ID: "test-id", 45 Scheme: source.FileScheme, 46 Path: "some/path", 47 }, 48 expected: model.Source{ 49 ID: "test-id", 50 Type: "file", 51 Target: "some/path", 52 }, 53 }, 54 { 55 name: "image", 56 src: source.Metadata{ 57 ID: "test-id", 58 Scheme: source.ImageScheme, 59 ImageMetadata: source.ImageMetadata{ 60 UserInput: "user-input", 61 ID: "id...", 62 ManifestDigest: "digest...", 63 MediaType: "type...", 64 }, 65 }, 66 expected: model.Source{ 67 ID: "test-id", 68 Type: "image", 69 Target: source.ImageMetadata{ 70 UserInput: "user-input", 71 ID: "id...", 72 ManifestDigest: "digest...", 73 MediaType: "type...", 74 RepoDigests: []string{}, 75 Tags: []string{}, 76 }, 77 }, 78 }, 79 } 80 for _, test := range tests { 81 t.Run(test.name, func(t *testing.T) { 82 // track each scheme tested (passed or not) 83 testedSchemes.Add(string(test.src.Scheme)) 84 85 // assert the model transformation is correct 86 actual, err := toSourceModel(test.src) 87 require.NoError(t, err) 88 assert.Equal(t, test.expected, actual) 89 }) 90 } 91 92 // assert all possible schemes were under test 93 assert.ElementsMatch(t, allSchemes.List(), testedSchemes.List(), "not all source.Schemes are under test") 94 } 95 96 func Test_toFileType(t *testing.T) { 97 98 badType := stereoscopeFile.Type(0x1337) 99 var allTypesTested []stereoscopeFile.Type 100 tests := []struct { 101 ty stereoscopeFile.Type 102 name string 103 }{ 104 { 105 ty: stereoscopeFile.TypeRegular, 106 name: "RegularFile", 107 }, 108 { 109 ty: stereoscopeFile.TypeDirectory, 110 name: "Directory", 111 }, 112 { 113 ty: stereoscopeFile.TypeSymLink, 114 name: "SymbolicLink", 115 }, 116 { 117 ty: stereoscopeFile.TypeHardLink, 118 name: "HardLink", 119 }, 120 { 121 ty: stereoscopeFile.TypeSocket, 122 name: "Socket", 123 }, 124 { 125 ty: stereoscopeFile.TypeCharacterDevice, 126 name: "CharacterDevice", 127 }, 128 { 129 ty: stereoscopeFile.TypeBlockDevice, 130 name: "BlockDevice", 131 }, 132 { 133 ty: stereoscopeFile.TypeFIFO, 134 name: "FIFONode", 135 }, 136 { 137 ty: stereoscopeFile.TypeIrregular, 138 name: "IrregularFile", 139 }, 140 { 141 ty: badType, 142 name: "Unknown", 143 }, 144 } 145 for _, tt := range tests { 146 t.Run(tt.name, func(t *testing.T) { 147 assert.Equalf(t, tt.name, toFileType(tt.ty), "toFileType(%v)", tt.ty) 148 if tt.ty != badType { 149 allTypesTested = append(allTypesTested, tt.ty) 150 } 151 }) 152 } 153 154 assert.ElementsMatch(t, allTypesTested, stereoscopeFile.AllTypes(), "not all file.Types are under test") 155 } 156 157 func Test_toFileMetadataEntry(t *testing.T) { 158 coords := file.Coordinates{ 159 RealPath: "/path", 160 FileSystemID: "x", 161 } 162 tests := []struct { 163 name string 164 metadata *file.Metadata 165 want *model.FileMetadataEntry 166 }{ 167 { 168 name: "no metadata", 169 }, 170 { 171 name: "no file info", 172 metadata: &file.Metadata{ 173 FileInfo: nil, 174 }, 175 want: &model.FileMetadataEntry{ 176 Type: stereoscopeFile.TypeRegular.String(), 177 }, 178 }, 179 { 180 name: "with file info", 181 metadata: &file.Metadata{ 182 FileInfo: &stereoscopeFile.ManualInfo{ 183 ModeValue: 1, 184 }, 185 }, 186 want: &model.FileMetadataEntry{ 187 Mode: 1, 188 Type: stereoscopeFile.TypeRegular.String(), 189 }, 190 }, 191 } 192 for _, tt := range tests { 193 t.Run(tt.name, func(t *testing.T) { 194 assert.Equal(t, tt.want, toFileMetadataEntry(coords, tt.metadata)) 195 }) 196 } 197 }