|
| 1 | +/*! |
| 2 | +Copyright (c) the purl authors |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +import { describe, expect, it } from 'vitest' |
| 24 | + |
| 25 | +import npmBuiltinNames from '../data/npm/builtin-names.json' |
| 26 | +import npmLegacyNames from '../data/npm/legacy-names.json' |
| 27 | +import { PackageURL } from '../dist/package-url.js' |
| 28 | + |
| 29 | +function getNpmId(purl: any) { |
| 30 | + const { name, namespace } = purl |
| 31 | + return `${namespace?.length > 0 ? `${namespace}/` : ''}${name}` |
| 32 | +} |
| 33 | + |
| 34 | +describe('PackageURL type-specific tests', () => { |
| 35 | + describe('npm', () => { |
| 36 | + it("should allow legacy names to be mixed case, match a builtin, or contain ~'!()* characters", () => { |
| 37 | + // Tests npm legacy package exceptions (historical packages with special names) |
| 38 | + for (const legacyName of npmLegacyNames) { |
| 39 | + let purl |
| 40 | + expect(() => { |
| 41 | + const parts = legacyName.split('/') |
| 42 | + const namespace = parts.length > 1 ? parts[0] : '' |
| 43 | + const name = parts.at(-1) |
| 44 | + purl = new PackageURL( |
| 45 | + 'npm', |
| 46 | + namespace, |
| 47 | + name, |
| 48 | + undefined, |
| 49 | + undefined, |
| 50 | + undefined, |
| 51 | + ) |
| 52 | + }).not.toThrow() |
| 53 | + const id = purl ? getNpmId(purl) : '' |
| 54 | + const isBuiltin = npmBuiltinNames.includes(id) |
| 55 | + const isMixedCased = /[A-Z]/.test(id) |
| 56 | + const containsIllegalCharacters = /[~'!()*]/.test(id) |
| 57 | + expect( |
| 58 | + isBuiltin || isMixedCased || containsIllegalCharacters, |
| 59 | + `assert for ${legacyName}`, |
| 60 | + ) |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + it('should not allow non-legacy builtin names', () => { |
| 65 | + // Tests npm builtin module validation (only legacy builtins allowed) |
| 66 | + for (const builtinName of npmBuiltinNames) { |
| 67 | + if (!npmLegacyNames.includes(builtinName)) { |
| 68 | + expect(() => { |
| 69 | + const parts = builtinName.split('/') |
| 70 | + const namespace = parts.length > 1 ? parts[0] : '' |
| 71 | + const name = parts.at(-1) |
| 72 | + |
| 73 | + new PackageURL( |
| 74 | + 'npm', |
| 75 | + namespace, |
| 76 | + name, |
| 77 | + undefined, |
| 78 | + undefined, |
| 79 | + undefined, |
| 80 | + ) |
| 81 | + }, `assert for ${builtinName}`).toThrow() |
| 82 | + } |
| 83 | + } |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + describe('pub', () => { |
| 88 | + it('should normalize dashes to underscores', () => { |
| 89 | + // Tests pub-specific normalization (dashes to underscores per spec) |
| 90 | + const purlWithDashes = new PackageURL( |
| 91 | + 'pub', |
| 92 | + '', |
| 93 | + 'flutter-downloader', |
| 94 | + '1.0.0', |
| 95 | + undefined, |
| 96 | + undefined, |
| 97 | + ) |
| 98 | + expect(purlWithDashes.toString()).toBe('pkg:pub/[email protected]') |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe('pypi', () => { |
| 103 | + it('should handle pypi package-urls per the purl-spec', () => { |
| 104 | + // Tests PyPI-specific normalizations (lowercase, underscores to dashes) |
| 105 | + const purlMixedCasing = PackageURL.fromString('pkg:pypi/[email protected]') |
| 106 | + expect(purlMixedCasing.toString()).toBe('pkg:pypi/[email protected]') |
| 107 | + const purlWithUnderscore = PackageURL.fromString( |
| 108 | + |
| 109 | + ) |
| 110 | + expect(purlWithUnderscore.toString()).toBe( |
| 111 | + |
| 112 | + ) |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
0 commit comments