Skip to content

Commit 6fde377

Browse files
CopilotMalcolmnixon
andcommitted
Add package name validation to reject underscores
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
1 parent a454432 commit 6fde377

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/DemaConsulting.SpdxModel/SpdxPackage.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ public void Validate(List<string> issues, SpdxDocument? doc, bool ntia = false)
393393
if (Name.Length == 0)
394394
issues.Add("Package Invalid Package Name Field");
395395

396+
// Validate Package Name contains only valid characters
397+
if (Name.Contains('_'))
398+
issues.Add($"Package {Name} Invalid Package Name Field");
399+
396400
// Validate Package Download Location Field
397401
if (DownloadLocation.Length == 0)
398402
issues.Add($"Package {Name} Invalid Package Download Location Field");

test/DemaConsulting.SpdxModel.Tests/SpdxPackageTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,70 @@ public void Enhance()
175175
Assert.AreEqual("SomePackage", packages[1].Name);
176176
Assert.AreEqual("1.2.3", packages[1].Version);
177177
}
178+
179+
/// <summary>
180+
/// Tests the <see cref="SpdxPackage.Validate"/> method detects valid package names.
181+
/// </summary>
182+
[TestMethod]
183+
public void ValidateValidPackageNames()
184+
{
185+
var validNames = new[]
186+
{
187+
"glibc",
188+
"Apache Commons Lang",
189+
"DemaConsulting.SpdxModel",
190+
"package-with-hyphens",
191+
"package123",
192+
"Package123",
193+
"Jena",
194+
"Saxon"
195+
};
196+
197+
foreach (var name in validNames)
198+
{
199+
var package = new SpdxPackage
200+
{
201+
Name = name,
202+
DownloadLocation = "http://example.com/download"
203+
};
204+
205+
var issues = new List<string>();
206+
package.Validate(issues, null);
207+
208+
// Should not have package name validation issues
209+
Assert.IsFalse(issues.Any(i => i.Contains("Invalid Package Name")),
210+
$"Package name '{name}' should be valid but validation failed");
211+
}
212+
}
213+
214+
/// <summary>
215+
/// Tests the <see cref="SpdxPackage.Validate"/> method detects invalid package names.
216+
/// </summary>
217+
[TestMethod]
218+
public void ValidateInvalidPackageNames()
219+
{
220+
var invalidNames = new[]
221+
{
222+
"package_with_underscores",
223+
"package_name",
224+
"test_package",
225+
"my_package_name"
226+
};
227+
228+
foreach (var name in invalidNames)
229+
{
230+
var package = new SpdxPackage
231+
{
232+
Name = name,
233+
DownloadLocation = "http://example.com/download"
234+
};
235+
236+
var issues = new List<string>();
237+
package.Validate(issues, null);
238+
239+
// Should have package name validation issues
240+
Assert.IsTrue(issues.Any(i => i.Contains("Invalid Package Name")),
241+
$"Package name '{name}' should be invalid but validation passed");
242+
}
243+
}
178244
}

0 commit comments

Comments
 (0)