Skip to content

Commit 544771c

Browse files
authored
Merge branch 'main' into ppandrate_complianceStandardValidation
2 parents 10ead12 + 873135f commit 544771c

File tree

5 files changed

+93
-13
lines changed

5 files changed

+93
-13
lines changed

src/Microsoft.Sbom.Parsers.Spdx30SbomParser/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal static class Constants
1515
internal const string DataLicenceValue = "CC0-1.0";
1616
internal const string SPDXDocumentIdValue = "SPDXRef-DOCUMENT";
1717
internal const string RootPackageIdValue = "SPDXRef-RootPackage";
18+
internal const string SPDXRefFile = "SPDXRef-File";
1819
internal const string SPDXDocumentNameFormatString = "{0} {1}";
1920
internal const string PackageSupplierFormatString = "Organization: {0}";
2021
internal const string SPDXVersionHeaderName = "spdxVersion";

src/Microsoft.Sbom.Parsers.Spdx30SbomParser/Utils/SPDXExtensions.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public static class SPDXExtensions
3131
/// </summary>
3232
public static string GenerateSpdxId(Element element, string id) => $"SPDXRef-{element.Type}-{GetStringHash(id)}";
3333

34+
public static string GetSpdxId(Element element, string id)
35+
{
36+
var spdxFileId = $"{Constants.SPDXRefFile}-{element.Name}-{id}";
37+
return SpdxIdAllowedCharsRegex.Replace(spdxFileId, "-");
38+
}
39+
3440
/// <summary>
3541
/// Returns the SPDX-compliant external document ID.
3642
/// </summary>
@@ -64,8 +70,7 @@ public static string GetSpdxElementId(SbomPackage packageInfo)
6470
}
6571

6672
/// <summary>
67-
/// Adds a SPDXID property to the given file. The id of the file should be the same
68-
/// for any build as long as the contents of the file haven't changed.
73+
/// Gets the SHA1 checksum value for a file.
6974
/// </summary>
7075
/// <param name="fileName"></param>
7176
/// <param name="checksums"></param>
@@ -146,7 +151,12 @@ public static void AddSpdxId(this PackageVerificationCode packageVerificationCod
146151
packageVerificationCode.SpdxId = GenerateSpdxId(packageVerificationCode, packageVerificationCode.Algorithm.ToString());
147152
}
148153

149-
public static void AddSpdxId(this Element element, string id)
154+
public static void AddSpdxId(this File element, string id)
155+
{
156+
element.SpdxId = GetSpdxId(element, id);
157+
}
158+
159+
public static void AddSpdxId(this Package element, string id)
150160
{
151161
element.SpdxId = GenerateSpdxId(element, id);
152162
}

test/Microsoft.Sbom.Parsers.Spdx30SbomParser.Tests/Parser/GeneratorTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,37 @@ public void GenerateJsonDocumentTest_File()
108108
Assert.AreEqual(expectedJsonContentAsString, generatedJsonString);
109109
}
110110

111+
[TestMethod]
112+
public void GenerateJsonDocumentTest_FilesWithDifferingChecksums_CreatesDifferentSpdxFiles()
113+
{
114+
var fileInfo1 = new InternalSbomFileInfo
115+
{
116+
Checksum = GetSampleChecksums(),
117+
FileCopyrightText = "sampleCopyright",
118+
LicenseConcluded = "sampleLicense1",
119+
LicenseInfoInFiles = new List<string> { "sampleLicense1" },
120+
Path = "/sample/path",
121+
};
122+
123+
var fileInfo2 = new InternalSbomFileInfo
124+
{
125+
Checksum = GetDifferentSampleChecksums(),
126+
FileCopyrightText = "sampleCopyright",
127+
LicenseConcluded = "sampleLicense1",
128+
LicenseInfoInFiles = new List<string> { "sampleLicense1" },
129+
Path = "/sample/path",
130+
};
131+
132+
var generatorResultFile1 = generator.GenerateJsonDocument(fileInfo1);
133+
var generatorResultFile2 = generator.GenerateJsonDocument(fileInfo2);
134+
135+
// Compare entity IDs which is the same as the SPDX ID for each file.
136+
Assert.AreNotEqual(generatorResultFile1.ResultMetadata.EntityId, generatorResultFile2.ResultMetadata.EntityId);
137+
Assert.IsTrue(generatorResultFile1.ResultMetadata.EntityId.Contains("sha1Value"));
138+
Assert.IsTrue(generatorResultFile2.ResultMetadata.EntityId.Contains("DIFFsha1Value"));
139+
Assert.AreNotEqual(generatorResultFile1, generatorResultFile2);
140+
}
141+
111142
[TestMethod]
112143
public void GenerateJsonDocumentTest_ExternalMap()
113144
{
@@ -191,6 +222,22 @@ private List<Checksum> GetSampleChecksums()
191222
};
192223
}
193224

225+
private List<Checksum> GetDifferentSampleChecksums()
226+
{
227+
return new List<Checksum>
228+
{
229+
new Checksum
230+
{
231+
Algorithm = AlgorithmName.SHA1,
232+
ChecksumValue = "DIFFsha1Value"
233+
}, new Checksum
234+
{
235+
Algorithm = AlgorithmName.SHA256,
236+
ChecksumValue = "DIFFsha256Value"
237+
},
238+
};
239+
}
240+
194241
private IInternalMetadataProvider CreateInternalMetadataProvider()
195242
{
196243
ISbomConfig sbomConfig = new SbomConfig(fileSystemMock.Object)

test/Microsoft.Sbom.Parsers.Spdx30SbomParser.Tests/Parser/JsonStrings/SbomFileJsonStrings.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class SbomFileJsonStrings
1212
""name"": ""./sample/path"",
1313
""software_copyrightText"": ""sampleCopyright"",
1414
""creationInfo"": ""_:creationinfo"",
15-
""spdxId"": ""SPDXRef-software_File-B4A9F99A3A03B9273AE34753D96564CB4F2B0FAD885BBD36B0DD619E9E8AC967"",
15+
""spdxId"": ""SPDXRef-File-.-sample-path-sha1Value"",
1616
""verifiedUsing"": [
1717
{
1818
""algorithm"": ""sha1"",
@@ -38,7 +38,7 @@ public static class SbomFileJsonStrings
3838
""type"": ""AnyLicenseInfo""
3939
},
4040
{
41-
""from"": ""SPDXRef-software_File-B4A9F99A3A03B9273AE34753D96564CB4F2B0FAD885BBD36B0DD619E9E8AC967"",
41+
""from"": ""SPDXRef-File-.-sample-path-sha1Value"",
4242
""relationshipType"": ""HAS_CONCLUDED_LICENSE"",
4343
""to"": [
4444
""SPDXRef-AnyLicenseInfo-3BA3FA6D3D66FE2BA75992BB0850D080F1223256368A76C77BEF8E0F6AC71896""
@@ -48,7 +48,7 @@ public static class SbomFileJsonStrings
4848
""type"": ""Relationship""
4949
},
5050
{
51-
""from"": ""SPDXRef-software_File-B4A9F99A3A03B9273AE34753D96564CB4F2B0FAD885BBD36B0DD619E9E8AC967"",
51+
""from"": ""SPDXRef-File-.-sample-path-sha1Value"",
5252
""relationshipType"": ""HAS_DECLARED_LICENSE"",
5353
""to"": [
5454
""SPDXRef-AnyLicenseInfo-3BA3FA6D3D66FE2BA75992BB0850D080F1223256368A76C77BEF8E0F6AC71896""

test/Microsoft.Sbom.Tool.Tests/IntegrationTests.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void E2E_GenerateManifest_GeneratesManifest_ReturnsZeroExitCode()
9292
}
9393

9494
[TestMethod]
95-
public void E2E_GenerateAndValidateManifest_ValidationSucceeds_ReturnsZeroExitCode()
95+
public void E2E_GenerateAndValidateSPDX22Manifest_ValidationSucceeds_ReturnsZeroExitCode()
9696
{
9797
if (!IsWindows)
9898
{
@@ -113,6 +113,28 @@ public void E2E_GenerateAndValidateManifest_ValidationSucceeds_ReturnsZeroExitCo
113113
Assert.IsTrue(File.ReadAllText(outputFile).Contains("\"Result\":\"Success\"", StringComparison.OrdinalIgnoreCase));
114114
}
115115

116+
[TestMethod]
117+
public void E2E_GenerateAndValidateSPDX30Manifest_ValidationSucceeds_ReturnsZeroExitCode()
118+
{
119+
if (!IsWindows)
120+
{
121+
Assert.Inconclusive("This test is not (yet) supported on non-Windows platforms.");
122+
return;
123+
}
124+
125+
var testFolderPath = CreateTestFolder();
126+
GenerateManifestAndValidateSuccess(testFolderPath, manifestInfoSpdxVersion: "3.0");
127+
128+
var (arguments, outputFile) = GetValidateManifestArguments(testFolderPath, manifestInfoValue: "SPDX:3.0");
129+
130+
var (stdout, stderr, exitCode) = LaunchAndCaptureOutput(arguments);
131+
132+
Assert.AreEqual(stderr, string.Empty);
133+
Assert.AreEqual(0, exitCode.Value, $"Unexpected failure: stdout = {stdout}");
134+
Assert.IsTrue(File.Exists(outputFile), $"{outputFile} should have been created during validation");
135+
Assert.IsTrue(File.ReadAllText(outputFile).Contains("\"Result\":\"Success\"", StringComparison.OrdinalIgnoreCase));
136+
}
137+
116138
[TestMethod]
117139
public void E2E_GenerateAndRedactManifest_RedactedFileIsSmaller_ReturnsZeroExitCode()
118140
{
@@ -152,7 +174,7 @@ public void E2E_GenerateAndRedactSPDX30Manifest_ReturnsNonZeroExitCode()
152174
}
153175

154176
var testFolderPath = CreateTestFolder();
155-
GenerateManifestAndValidateSuccess(testFolderPath, manifestInfoValue: "3.0");
177+
GenerateManifestAndValidateSuccess(testFolderPath, manifestInfoSpdxVersion: "3.0");
156178

157179
var outputFolder = Path.Combine(TestContext.TestRunDirectory, TestContext.TestName, "redacted");
158180
var originalManifestFolderPath = AppendFullManifestFolderPath(testFolderPath, spdxVersion: "3.0");
@@ -311,15 +333,15 @@ public void E2E_Redact_WithManifestInfo_ReturnsNonZeroExitCode(string manifestIn
311333
Assert.AreNotEqual(0, exitCode.Value);
312334
}
313335

314-
private void GenerateManifestAndValidateSuccess(string testFolderPath, string manifestInfoValue = null)
336+
private void GenerateManifestAndValidateSuccess(string testFolderPath, string manifestInfoSpdxVersion = null)
315337
{
316-
var arguments = $"generate -ps IntegrationTests -pn IntegrationTests -pv 1.2.3 -m \"{testFolderPath}\" -b \"{testDropDirectory}\" -bc \"{GetSolutionFolderPath()}\"";
317-
arguments += string.IsNullOrEmpty(manifestInfoValue) ? string.Empty : $" -mi SPDX:{manifestInfoValue}";
318-
338+
var manifestInfoArg = string.IsNullOrEmpty(manifestInfoSpdxVersion) ? string.Empty : $"-mi SPDX:{manifestInfoSpdxVersion}";
339+
var arguments = $"generate -ps IntegrationTests -pn IntegrationTests -pv 1.2.3 -m \"{testFolderPath}\" -b \"{testDropDirectory}\" -bc \"{GetSolutionFolderPath()}\" {manifestInfoArg}";
340+
319341
var (stdout, stderr, exitCode) = LaunchAndCaptureOutput(arguments);
320342

321343
Assert.AreEqual(stderr, string.Empty);
322-
var manifestFolderPath = AppendFullManifestFolderPath(testFolderPath, spdxVersion: manifestInfoValue);
344+
var manifestFolderPath = AppendFullManifestFolderPath(testFolderPath, spdxVersion: manifestInfoSpdxVersion);
323345
var jsonFilePath = Path.Combine(manifestFolderPath, ManifestFileName);
324346
var shaFilePath = Path.Combine(manifestFolderPath, "manifest.spdx.json.sha256");
325347
Assert.IsTrue(File.Exists(jsonFilePath));

0 commit comments

Comments
 (0)