github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/pkg/cataloger/javascript/parse_yarn_lock_test.go (about) 1 package javascript 2 3 import ( 4 "testing" 5 6 "github.com/nextlinux/gosbom/gosbom/artifact" 7 "github.com/nextlinux/gosbom/gosbom/file" 8 "github.com/nextlinux/gosbom/gosbom/pkg" 9 "github.com/nextlinux/gosbom/gosbom/pkg/cataloger/internal/pkgtest" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestParseYarnBerry(t *testing.T) { 14 var expectedRelationships []artifact.Relationship 15 fixture := "test-fixtures/yarn-berry/yarn.lock" 16 locations := file.NewLocationSet(file.NewLocation(fixture)) 17 18 expectedPkgs := []pkg.Package{ 19 { 20 Name: "@babel/code-frame", 21 Version: "7.10.4", 22 Locations: locations, 23 PURL: "pkg:npm/%40babel/code-frame@7.10.4", 24 Language: pkg.JavaScript, 25 Type: pkg.NpmPkg, 26 }, 27 { 28 Name: "@types/minimatch", 29 Version: "3.0.3", 30 Locations: locations, 31 PURL: "pkg:npm/%40types/minimatch@3.0.3", 32 Language: pkg.JavaScript, 33 Type: pkg.NpmPkg, 34 }, 35 { 36 Name: "@types/qs", 37 Version: "6.9.4", 38 Locations: locations, 39 PURL: "pkg:npm/%40types/qs@6.9.4", 40 Language: pkg.JavaScript, 41 Type: pkg.NpmPkg, 42 }, 43 { 44 Name: "ajv", 45 Version: "6.12.3", 46 Locations: locations, 47 PURL: "pkg:npm/ajv@6.12.3", 48 Language: pkg.JavaScript, 49 Type: pkg.NpmPkg, 50 }, 51 { 52 Name: "asn1.js", 53 Version: "4.10.1", 54 Locations: locations, 55 PURL: "pkg:npm/asn1.js@4.10.1", 56 Language: pkg.JavaScript, 57 Type: pkg.NpmPkg, 58 }, 59 { 60 Name: "atob", 61 Version: "2.1.2", 62 Locations: locations, 63 PURL: "pkg:npm/atob@2.1.2", 64 Language: pkg.JavaScript, 65 Type: pkg.NpmPkg, 66 }, 67 { 68 Name: "aws-sdk", 69 Version: "2.706.0", 70 PURL: "pkg:npm/aws-sdk@2.706.0", 71 Locations: locations, 72 Language: pkg.JavaScript, 73 Type: pkg.NpmPkg, 74 }, 75 { 76 Name: "c0n-fab_u.laTION", 77 Version: "7.7.7", 78 Locations: locations, 79 PURL: "pkg:npm/c0n-fab_u.laTION@7.7.7", 80 Language: pkg.JavaScript, 81 Type: pkg.NpmPkg, 82 }, 83 { 84 Name: "jhipster-core", 85 Version: "7.3.4", 86 Locations: locations, 87 PURL: "pkg:npm/jhipster-core@7.3.4", 88 Language: pkg.JavaScript, 89 Type: pkg.NpmPkg, 90 }, 91 } 92 93 pkgtest.TestFileParser(t, fixture, parseYarnLock, expectedPkgs, expectedRelationships) 94 95 } 96 97 func TestParseYarnLock(t *testing.T) { 98 var expectedRelationships []artifact.Relationship 99 fixture := "test-fixtures/yarn/yarn.lock" 100 locations := file.NewLocationSet(file.NewLocation(fixture)) 101 102 expectedPkgs := []pkg.Package{ 103 { 104 Name: "@babel/code-frame", 105 Version: "7.10.4", 106 Locations: locations, 107 PURL: "pkg:npm/%40babel/code-frame@7.10.4", 108 Language: pkg.JavaScript, 109 Type: pkg.NpmPkg, 110 }, 111 { 112 Name: "@types/minimatch", 113 Version: "3.0.3", 114 Locations: locations, 115 PURL: "pkg:npm/%40types/minimatch@3.0.3", 116 Language: pkg.JavaScript, 117 Type: pkg.NpmPkg, 118 }, 119 { 120 Name: "@types/qs", 121 Version: "6.9.4", 122 Locations: locations, 123 PURL: "pkg:npm/%40types/qs@6.9.4", 124 Language: pkg.JavaScript, 125 Type: pkg.NpmPkg, 126 }, 127 { 128 Name: "ajv", 129 Version: "6.12.3", 130 Locations: locations, 131 PURL: "pkg:npm/ajv@6.12.3", 132 Language: pkg.JavaScript, 133 Type: pkg.NpmPkg, 134 }, 135 { 136 Name: "asn1.js", 137 Version: "4.10.1", 138 Locations: locations, 139 PURL: "pkg:npm/asn1.js@4.10.1", 140 Language: pkg.JavaScript, 141 Type: pkg.NpmPkg, 142 }, 143 { 144 Name: "atob", 145 Version: "2.1.2", 146 Locations: locations, 147 148 PURL: "pkg:npm/atob@2.1.2", 149 Language: pkg.JavaScript, 150 Type: pkg.NpmPkg, 151 }, 152 { 153 Name: "aws-sdk", 154 Version: "2.706.0", 155 Locations: locations, 156 PURL: "pkg:npm/aws-sdk@2.706.0", 157 Language: pkg.JavaScript, 158 Type: pkg.NpmPkg, 159 }, 160 { 161 Name: "jhipster-core", 162 Version: "7.3.4", 163 Locations: locations, 164 PURL: "pkg:npm/jhipster-core@7.3.4", 165 Language: pkg.JavaScript, 166 Type: pkg.NpmPkg, 167 }, 168 169 { 170 Name: "something-i-made-up", 171 Version: "7.7.7", 172 Locations: locations, 173 PURL: "pkg:npm/something-i-made-up@7.7.7", 174 Language: pkg.JavaScript, 175 Type: pkg.NpmPkg, 176 }, 177 } 178 179 pkgtest.TestFileParser(t, fixture, parseYarnLock, expectedPkgs, expectedRelationships) 180 181 } 182 183 func TestParseYarnFindPackageNames(t *testing.T) { 184 tests := []struct { 185 line string 186 expected string 187 }{ 188 { 189 line: `"@babel/code-frame@npm:7.10.4":`, 190 expected: "@babel/code-frame", 191 }, 192 { 193 line: `"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.10.4":`, 194 expected: "@babel/code-frame", 195 }, 196 { 197 line: "ajv@^6.10.2, ajv@^6.5.5:", 198 expected: "ajv", 199 }, 200 { 201 line: "aws-sdk@2.706.0:", 202 expected: "aws-sdk", 203 }, 204 { 205 line: "asn1.js@^4.0.0:", 206 expected: "asn1.js", 207 }, 208 { 209 line: "c0n-fab_u.laTION@^7.0.0", 210 expected: "c0n-fab_u.laTION", 211 }, 212 { 213 line: `"newtest@workspace:.":`, 214 expected: "newtest", 215 }, 216 { 217 line: `"color-convert@npm:^1.9.0":`, 218 expected: "color-convert", 219 }, 220 { 221 line: `"@npmcorp/code-frame@^7.1.0", "@npmcorp/code-frame@^7.10.4":`, 222 expected: "@npmcorp/code-frame", 223 }, 224 { 225 line: `"@npmcorp/code-frame@^7.2.3":`, 226 expected: "@npmcorp/code-frame", 227 }, 228 { 229 line: `"@s/odd-name@^7.1.2":`, 230 expected: "@s/odd-name", 231 }, 232 { 233 line: `"@/code-frame@^7.3.4":`, 234 expected: "", 235 }, 236 { 237 line: `"code-frame":`, 238 expected: "", 239 }, 240 } 241 242 for _, test := range tests { 243 t.Run(test.expected, func(t *testing.T) { 244 t.Parallel() 245 actual := findPackageName(test.line) 246 assert.Equal(t, test.expected, actual) 247 }) 248 } 249 } 250 251 func TestParseYarnFindPackageVersions(t *testing.T) { 252 tests := []struct { 253 line string 254 expected string 255 }{ 256 { 257 line: ` version "7.10.4"`, 258 expected: "7.10.4", 259 }, 260 { 261 line: ` version "7.11.5"`, 262 expected: "7.11.5", 263 }, 264 { 265 line: `version "7.12.6"`, 266 expected: "", 267 }, 268 { 269 line: ` version "0.0.0"`, 270 expected: "0.0.0", 271 }, 272 { 273 line: ` version "2" `, 274 expected: "2", 275 }, 276 { 277 line: ` version "9.3"`, 278 expected: "9.3", 279 }, 280 { 281 line: "ajv@^6.10.2, ajv@^6.5.5", 282 expected: "", 283 }, 284 { 285 line: "atob@^2.1.2:", 286 expected: "", 287 }, 288 { 289 line: `"color-convert@npm:^1.9.0":`, 290 expected: "", 291 }, 292 { 293 line: " version: 1.9.3", 294 expected: "1.9.3", 295 }, 296 { 297 line: " version: 2", 298 expected: "2", 299 }, 300 { 301 line: " version: 9.3", 302 expected: "9.3", 303 }, 304 { 305 line: "ajv@^6.10.2, ajv@^6.5.5", 306 expected: "", 307 }, 308 { 309 line: "atob@^2.1.2:", 310 expected: "", 311 }, 312 { 313 line: " version: 1.0.0-alpha+001", 314 expected: "1.0.0-alpha", 315 }, 316 { 317 line: " version: 1.0.0-beta_test+exp.sha.5114f85", 318 expected: "1.0.0-beta_test", 319 }, 320 { 321 line: " version: 1.0.0+21AF26D3-117B344092BD", 322 expected: "1.0.0", 323 }, 324 { 325 line: " version: 0.0.0-use.local", 326 expected: "0.0.0-use.local", 327 }, 328 } 329 330 for _, test := range tests { 331 t.Run(test.expected, func(t *testing.T) { 332 t.Parallel() 333 actual := findPackageVersion(test.line) 334 assert.Equal(t, test.expected, actual) 335 }) 336 } 337 }