From 953f59bdcaf255c87eff09c18658088e4d4cacf9 Mon Sep 17 00:00:00 2001 From: Michael Tsfoni <80639729+mtsfoni@users.noreply.github.com> Date: Fri, 7 Jun 2024 10:14:52 +0200 Subject: [PATCH] fix: CycloneDX should fail when the provided file was not found * CycloneDX should fail when the provided file was not found https://github.com/CycloneDX/cyclonedx-dotnet/issues/882 Signed-off-by: Michael Tsfoni <80639729+mtsfoni@users.noreply.github.com> --- CycloneDX.Tests/ProgramTests.cs | 28 ++++++++++++++++++++++++++++ CycloneDX/Runner.cs | 24 ++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/CycloneDX.Tests/ProgramTests.cs b/CycloneDX.Tests/ProgramTests.cs index 7817fffd..0e1ca4a3 100755 --- a/CycloneDX.Tests/ProgramTests.cs +++ b/CycloneDX.Tests/ProgramTests.cs @@ -102,5 +102,33 @@ public void CheckMetaDataTemplate() Assert.Matches("CycloneDX", bom.Metadata.Tools.Tools[0].Vendor); Assert.Matches("1.2.0", bom.Metadata.Tools.Tools[0].Version); } + + [Theory] + [InlineData(@"c:\SolutionPath\SolutionFile.sln", false)] + [InlineData(@"c:\SolutionPath\ProjectFile.csproj", false)] + [InlineData(@"c:\SolutionPath\ProjectFile.csproj", true)] + [InlineData(@"c:\SolutionPath\packages.config", false)] + public async Task CallingCycloneDX_WithSolutionOrProjectFileThatDoesntExistsReturnAnythingButZero(string path, bool rs) + { + var mockFileSystem = new MockFileSystem(new Dictionary()); + var mockSolutionFileService = new Mock(); + mockSolutionFileService + .Setup(s => s.GetSolutionDotnetDependencys(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(new HashSet()); + + Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, null, solutionFileService: mockSolutionFileService.Object, null); + + RunOptions runOptions = new RunOptions + { + SolutionOrProjectFile = XFS.Path(path), + scanProjectReferences = rs, + outputDirectory = XFS.Path(@"c:\NewDirectory"), + outputFilename = XFS.Path(@"my_bom.xml") + }; + + var exitCode = await runner.HandleCommandAsync(runOptions); + + Assert.NotEqual((int)ExitCode.OK, exitCode); + } } } diff --git a/CycloneDX/Runner.cs b/CycloneDX/Runner.cs index eaef36be..890278a2 100644 --- a/CycloneDX/Runner.cs +++ b/CycloneDX/Runner.cs @@ -164,21 +164,41 @@ public async Task HandleCommandAsync(RunOptions options) { if (SolutionOrProjectFile.ToLowerInvariant().EndsWith(".sln", StringComparison.OrdinalIgnoreCase)) { + if (!fileSystem.File.Exists(SolutionOrProjectFile)) + { + Console.Error.WriteLine($"No file found at path {SolutionOrProjectFile}"); + return (int)ExitCode.InvalidOptions; + } packages = await solutionFileService.GetSolutionDotnetDependencys(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false); topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile); } else if (Utils.IsSupportedProjectType(SolutionOrProjectFile) && scanProjectReferences) { + if(!fileSystem.File.Exists(SolutionOrProjectFile)) + { + Console.Error.WriteLine($"No file found at path {SolutionOrProjectFile}"); + return (int)ExitCode.InvalidOptions; + } packages = await projectFileService.RecursivelyGetProjectDotnetDependencysAsync(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false); topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile); } else if (Utils.IsSupportedProjectType(SolutionOrProjectFile)) - { + { + if(!fileSystem.File.Exists(SolutionOrProjectFile)) + { + Console.Error.WriteLine($"No file found at path {SolutionOrProjectFile}"); + return (int)ExitCode.InvalidOptions; + } packages = await projectFileService.GetProjectDotnetDependencysAsync(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false); topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile); } - else if (this.fileSystem.Path.GetFileName(SolutionOrProjectFile).ToLowerInvariant().Equals("packages.config", StringComparison.OrdinalIgnoreCase)) + else if (fileSystem.Path.GetFileName(SolutionOrProjectFile).ToLowerInvariant().Equals("packages.config", StringComparison.OrdinalIgnoreCase)) { + if (!fileSystem.File.Exists(SolutionOrProjectFile)) + { + Console.Error.WriteLine($"No file found at path {SolutionOrProjectFile}"); + return (int)ExitCode.InvalidOptions; + } packages = await packagesFileService.GetDotnetDependencysAsync(fullSolutionOrProjectFilePath).ConfigureAwait(false); topLevelComponent.Name = fileSystem.Path.GetDirectoryName(fullSolutionOrProjectFilePath); }