-
Notifications
You must be signed in to change notification settings - Fork 177
Fix SbomValidator.ValidateSbomAsync false positive when outputPath is directory #1131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
13
commits into
main
Choose a base branch
from
copilot/fix-1093
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
09406d2
Initial plan
Copilot bb2706b
Add Exceptions property to IRecorder interface and update SbomValidat…
Copilot 4bfb55d
Merge branch 'main' of https://github.com/microsoft/sbom-tool into co…
JoseRenan ec28a81
Cleanup
JoseRenan 09bf8fd
Merge branch 'main' into copilot/fix-1093
JoseRenan 271cf31
Refactor SbomValidatorTests to use private fields for common test data
Copilot 399220a
Merge branch 'main' into copilot/fix-1093
JoseRenan 7e118b0
Refactor SbomValidatorTests to use test fixture pattern for sbomValid…
Copilot 76b439c
Refactor test boilerplate into helper method as requested
Copilot c97f2d1
Implement @DaveTryon's suggestion using Result property approach
Copilot 19deec3
Remove Exceptions property from IRecorder interface as suggested
Copilot d53a3bc
Fix test assertions to check specific indices instead of just contain…
Copilot 3f1cae3
Remove Exceptions property from InterfaceConcretionTests as requested
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Sbom.Api.Entities; | ||
| using Microsoft.Sbom.Api.Output.Telemetry; | ||
| using Microsoft.Sbom.Api.Workflows; | ||
| using Microsoft.Sbom.Common; | ||
| using Microsoft.Sbom.Common.Config; | ||
| using Microsoft.Sbom.Common.Config.Validators; | ||
| using Microsoft.Sbom.Contracts; | ||
| using Microsoft.Sbom.Extensions; | ||
| using Microsoft.Sbom.Extensions.Entities; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Moq; | ||
| using Constants = Microsoft.Sbom.Api.Utils.Constants; | ||
|
|
||
| namespace Microsoft.Sbom.Api.Tests; | ||
|
|
||
| [TestClass] | ||
| public class SbomValidatorTests | ||
| { | ||
| private Mock<IWorkflow<SbomParserBasedValidationWorkflow>> workflowMock; | ||
| private Mock<IRecorder> recorderMock; | ||
| private Mock<IEnumerable<ConfigValidator>> configValidatorsMock; | ||
| private Mock<IConfiguration> configurationMock; | ||
| private Mock<ISbomConfigProvider> sbomConfigProviderMock; | ||
| private Mock<IFileSystemUtils> fileSystemUtilsMock; | ||
| private Mock<ISbomConfig> sbomConfigMock; | ||
|
|
||
| // Common test data | ||
| private readonly string buildDropPath = "/test/drop"; | ||
| private readonly string outputPathFile = "/test/output.json"; | ||
| private readonly string outputPathDirectory = "/test/output"; | ||
| private readonly List<SbomSpecification> specifications = new List<SbomSpecification> { new SbomSpecification("SPDX", "2.2") }; | ||
| private readonly string manifestDirPath = "/test/manifest"; | ||
| private readonly ManifestInfo manifestInfo = Constants.TestManifestInfo; | ||
| private readonly string manifestJsonPath = "/test/manifest/manifest.json"; | ||
|
|
||
| [TestInitialize] | ||
| public void Init() | ||
| { | ||
| workflowMock = new Mock<IWorkflow<SbomParserBasedValidationWorkflow>>(MockBehavior.Strict); | ||
| recorderMock = new Mock<IRecorder>(MockBehavior.Strict); | ||
| configValidatorsMock = new Mock<IEnumerable<ConfigValidator>>(MockBehavior.Strict); | ||
| configurationMock = new Mock<IConfiguration>(MockBehavior.Strict); | ||
| sbomConfigProviderMock = new Mock<ISbomConfigProvider>(MockBehavior.Strict); | ||
| fileSystemUtilsMock = new Mock<IFileSystemUtils>(MockBehavior.Strict); | ||
| sbomConfigMock = new Mock<ISbomConfig>(MockBehavior.Strict); | ||
| } | ||
DaveTryon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [TestCleanup] | ||
| public void AfterEachTest() | ||
| { | ||
| workflowMock.VerifyAll(); | ||
| recorderMock.VerifyAll(); | ||
| configValidatorsMock.VerifyAll(); | ||
| configurationMock.VerifyAll(); | ||
| sbomConfigProviderMock.VerifyAll(); | ||
| fileSystemUtilsMock.VerifyAll(); | ||
| sbomConfigMock.VerifyAll(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task ValidateSbomAsync_WithNoErrorsAndNoExceptions_ReturnsTrue() | ||
| { | ||
| var errors = new List<FileValidationResult>(); | ||
| var exceptions = new List<Exception>(); | ||
|
|
||
| configValidatorsMock.Setup(cv => cv.GetEnumerator()).Returns(new List<ConfigValidator>().GetEnumerator()); | ||
|
|
||
| configurationMock.Setup(c => c.ManifestInfo).Returns(new ConfigurationSetting<IList<ManifestInfo>> | ||
DaveTryon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| Value = new List<ManifestInfo> { manifestInfo } | ||
| }); | ||
|
|
||
| sbomConfigProviderMock.Setup(scp => scp.Get(manifestInfo)).Returns(sbomConfigMock.Object); | ||
| sbomConfigMock.Setup(sc => sc.ManifestJsonFilePath).Returns(manifestJsonPath); | ||
|
|
||
| fileSystemUtilsMock.Setup(fs => fs.FileExists(manifestJsonPath)).Returns(true); | ||
| workflowMock.Setup(w => w.RunAsync()).ReturnsAsync(true); | ||
|
|
||
| recorderMock.Setup(r => r.FinalizeAndLogTelemetryAsync()).Returns(Task.CompletedTask); | ||
| recorderMock.Setup(r => r.Errors).Returns(errors); | ||
| recorderMock.Setup(r => r.Exceptions).Returns(exceptions); | ||
|
|
||
| var validator = new SbomValidator( | ||
| workflowMock.Object, | ||
| recorderMock.Object, | ||
| configValidatorsMock.Object, | ||
| configurationMock.Object, | ||
| sbomConfigProviderMock.Object, | ||
| fileSystemUtilsMock.Object); | ||
|
|
||
| var result = await validator.ValidateSbomAsync(buildDropPath, outputPathFile, specifications, manifestDirPath); | ||
|
|
||
| Assert.IsTrue(result.IsSuccess); | ||
| Assert.AreEqual(0, result.Errors.Count); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task ValidateSbomAsync_WithErrorsButNoExceptions_ReturnsFalse() | ||
| { | ||
| var errors = new List<FileValidationResult> | ||
| { | ||
| new FileValidationResult { ErrorType = ErrorType.MissingFile, Path = "/test/missing.txt" } | ||
| }; | ||
| var exceptions = new List<Exception>(); | ||
|
|
||
| configValidatorsMock.Setup(cv => cv.GetEnumerator()).Returns(new List<ConfigValidator>().GetEnumerator()); | ||
|
|
||
| configurationMock.Setup(c => c.ManifestInfo).Returns(new ConfigurationSetting<IList<ManifestInfo>> | ||
| { | ||
| Value = new List<ManifestInfo> { manifestInfo } | ||
| }); | ||
|
|
||
| sbomConfigProviderMock.Setup(scp => scp.Get(manifestInfo)).Returns(sbomConfigMock.Object); | ||
| sbomConfigMock.Setup(sc => sc.ManifestJsonFilePath).Returns(manifestJsonPath); | ||
|
|
||
| fileSystemUtilsMock.Setup(fs => fs.FileExists(manifestJsonPath)).Returns(true); | ||
| workflowMock.Setup(w => w.RunAsync()).ReturnsAsync(true); | ||
|
|
||
| recorderMock.Setup(r => r.FinalizeAndLogTelemetryAsync()).Returns(Task.CompletedTask); | ||
| recorderMock.Setup(r => r.Errors).Returns(errors); | ||
| recorderMock.Setup(r => r.Exceptions).Returns(exceptions); | ||
|
|
||
| var validator = new SbomValidator( | ||
| workflowMock.Object, | ||
| recorderMock.Object, | ||
| configValidatorsMock.Object, | ||
| configurationMock.Object, | ||
| sbomConfigProviderMock.Object, | ||
| fileSystemUtilsMock.Object); | ||
|
|
||
| var result = await validator.ValidateSbomAsync(buildDropPath, outputPathFile, specifications, manifestDirPath); | ||
|
|
||
| Assert.IsFalse(result.IsSuccess); | ||
| Assert.AreEqual(1, result.Errors.Count); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task ValidateSbomAsync_WithNoErrorsButWithExceptions_ReturnsFalse() | ||
| { | ||
| var errors = new List<FileValidationResult>(); | ||
| var exceptions = new List<Exception> | ||
| { | ||
| new InvalidOperationException("Cannot write to directory path") | ||
| }; | ||
|
|
||
| configValidatorsMock.Setup(cv => cv.GetEnumerator()).Returns(new List<ConfigValidator>().GetEnumerator()); | ||
|
|
||
| configurationMock.Setup(c => c.ManifestInfo).Returns(new ConfigurationSetting<IList<ManifestInfo>> | ||
| { | ||
| Value = new List<ManifestInfo> { manifestInfo } | ||
| }); | ||
|
|
||
| sbomConfigProviderMock.Setup(scp => scp.Get(manifestInfo)).Returns(sbomConfigMock.Object); | ||
| sbomConfigMock.Setup(sc => sc.ManifestJsonFilePath).Returns(manifestJsonPath); | ||
|
|
||
| fileSystemUtilsMock.Setup(fs => fs.FileExists(manifestJsonPath)).Returns(true); | ||
| workflowMock.Setup(w => w.RunAsync()).ReturnsAsync(true); | ||
|
|
||
| recorderMock.Setup(r => r.FinalizeAndLogTelemetryAsync()).Returns(Task.CompletedTask); | ||
| recorderMock.Setup(r => r.Errors).Returns(errors); | ||
| recorderMock.Setup(r => r.Exceptions).Returns(exceptions); | ||
|
|
||
| var validator = new SbomValidator( | ||
| workflowMock.Object, | ||
| recorderMock.Object, | ||
| configValidatorsMock.Object, | ||
| configurationMock.Object, | ||
| sbomConfigProviderMock.Object, | ||
| fileSystemUtilsMock.Object); | ||
|
|
||
| var result = await validator.ValidateSbomAsync(buildDropPath, outputPathDirectory, specifications, manifestDirPath); | ||
|
|
||
| Assert.IsFalse(result.IsSuccess); | ||
| Assert.AreEqual(0, result.Errors.Count); // No validation errors, but should still fail due to exception | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task ValidateSbomAsync_WithBothErrorsAndExceptions_ReturnsFalse() | ||
| { | ||
| var errors = new List<FileValidationResult> | ||
| { | ||
| new FileValidationResult { ErrorType = ErrorType.MissingFile, Path = "/test/missing.txt" } | ||
| }; | ||
| var exceptions = new List<Exception> | ||
| { | ||
| new InvalidOperationException("Cannot write to directory path") | ||
| }; | ||
|
|
||
| configValidatorsMock.Setup(cv => cv.GetEnumerator()).Returns(new List<ConfigValidator>().GetEnumerator()); | ||
|
|
||
| configurationMock.Setup(c => c.ManifestInfo).Returns(new ConfigurationSetting<IList<ManifestInfo>> | ||
| { | ||
| Value = new List<ManifestInfo> { manifestInfo } | ||
| }); | ||
|
|
||
| sbomConfigProviderMock.Setup(scp => scp.Get(manifestInfo)).Returns(sbomConfigMock.Object); | ||
| sbomConfigMock.Setup(sc => sc.ManifestJsonFilePath).Returns(manifestJsonPath); | ||
|
|
||
| fileSystemUtilsMock.Setup(fs => fs.FileExists(manifestJsonPath)).Returns(true); | ||
| workflowMock.Setup(w => w.RunAsync()).ReturnsAsync(true); | ||
|
|
||
| recorderMock.Setup(r => r.FinalizeAndLogTelemetryAsync()).Returns(Task.CompletedTask); | ||
| recorderMock.Setup(r => r.Errors).Returns(errors); | ||
| recorderMock.Setup(r => r.Exceptions).Returns(exceptions); | ||
|
|
||
| var validator = new SbomValidator( | ||
| workflowMock.Object, | ||
| recorderMock.Object, | ||
| configValidatorsMock.Object, | ||
| configurationMock.Object, | ||
| sbomConfigProviderMock.Object, | ||
| fileSystemUtilsMock.Object); | ||
|
|
||
| var result = await validator.ValidateSbomAsync(buildDropPath, outputPathDirectory, specifications, manifestDirPath); | ||
|
|
||
| Assert.IsFalse(result.IsSuccess); | ||
| Assert.AreEqual(1, result.Errors.Count); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,6 +128,8 @@ private class PinnedIRecorder : IRecorder | |
| { | ||
| public IList<FileValidationResult> Errors => throw new NotImplementedException(); | ||
|
|
||
| public IList<Exception> Exceptions => throw new NotImplementedException(); | ||
|
||
|
|
||
| public void AddResult(string propertyName, string value) => throw new NotImplementedException(); | ||
| public void AddToTotalCountOfLicenses(int count) => throw new NotImplementedException(); | ||
| public void AddToTotalNumberOfPackageDetailsEntries(int count) => throw new NotImplementedException(); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.