Skip to content
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

Cross-Platform .rsp File Support #1016

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BinSkim.Driver/BinSkim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static class BinSkim
{
private static int Main(string[] args)
{
args = EntryPointUtilities.GenerateArguments(args, new FileSystem(), new EnvironmentVariables());
args = ExpandArguments.GenerateArguments(args, new FileSystem(), new EnvironmentVariables());
args = RewriteArgs(args);

var rewrittenArgs = new List<string>(args);
Expand Down
67 changes: 67 additions & 0 deletions src/BinSkim.Driver/ExpandArguments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 Microsoft.CodeAnalysis.Sarif;
using Microsoft.CodeAnalysis.Sarif.Driver;

namespace Microsoft.CodeAnalysis.IL
{
public static class ExpandArguments
{
public static string[] GenerateArguments(
string[] args,
IFileSystem fileSystem,
IEnvironmentVariables environmentVariables)
{
var expandedArguments = new List<string>();

foreach (string argument in args)
{
string trimArgument = argument.Trim('"');
if (!IsResponseFileArgument(trimArgument))
{
expandedArguments.Add(trimArgument);
continue;
}

string responseFile = trimArgument.Substring(1);

responseFile = environmentVariables.ExpandEnvironmentVariables(responseFile);
responseFile = fileSystem.PathGetFullPath(responseFile);

string[] responseFileLines = fileSystem.FileReadAllLines(responseFile);
ExpandResponseFile(responseFileLines, expandedArguments);
}
Dismissed Show dismissed Hide dismissed

return expandedArguments.ToArray();
}

private static bool IsResponseFileArgument(string argument)
{
return argument.Length > 1 && argument[0] == '@';
AllDwarf marked this conversation as resolved.
Show resolved Hide resolved
}

private static void ExpandResponseFile(string[] responseFileLines, List<string> expandedArguments)
lukaskohl-msft marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (string responseFileLine in responseFileLines)
{
string responseFilePath = responseFileLine;

// Ignore comments from response file lines
int commentIndex = responseFileLine.IndexOf('#');
if (commentIndex >= 0)
lukaskohl-msft marked this conversation as resolved.
Show resolved Hide resolved
{
responseFilePath = responseFileLine.Substring(0, commentIndex);
}

List<string> fileList = ArgumentSplitter.CommandLineToArgvW(responseFilePath.Trim()) ??
throw new InvalidOperationException("Could not parse response file line:" + responseFileLine);

expandedArguments.AddRange(fileList);
}
}
}
}
180 changes: 180 additions & 0 deletions src/Test.UnitTests.BinSkim.Driver/ExpandArgumentsUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// 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.Globalization;
using System.IO;

using FluentAssertions;

using Microsoft.CodeAnalysis.IL;

using Microsoft.CodeAnalysis.Sarif;
using Microsoft.CodeAnalysis.Sarif.Driver;

using Moq;

using Xunit;


namespace Microsoft.CodeAnalysis.BinSkim.Rules
{
public class ExpandArgumentsUnitTests
{
private static void SetupTestMocks(
string responseFileName,
string[] responseFileContents,
out Mock<IFileSystem> fileSystemMock,
out Mock<IEnvironmentVariables> environmentVariablesMock)
{
fileSystemMock = new Mock<IFileSystem>();
environmentVariablesMock = new Mock<IEnvironmentVariables>();

fileSystemMock.Setup(fs => fs.PathGetFullPath(responseFileName)).Returns(responseFileName);
fileSystemMock.Setup(fs => fs.FileReadAllLines(responseFileName)).Returns(responseFileContents);
environmentVariablesMock.Setup(ev => ev.ExpandEnvironmentVariables(responseFileName)).Returns(responseFileName);
}

[Fact]
public void GenerateArguments_SucceedsWithEmptyArgumentList()
{
string[] result = ExpandArguments.GenerateArguments(Array.Empty<string>(), null, null);

result.Should().BeEmpty();
}

[Fact]
public void GenerateArguments_SucceedsWithNormalArguments()
{
string[] args = new[] { "/y:z", "/x" };

string[] result = ExpandArguments.GenerateArguments(args, null, null);

result.Length.Should().Be(2);
result.Should().ContainInOrder(args);
}

[Fact]
public void GenerateArguments_ExceptionIfResponseFileDoesNotExist()
{
string NonexistentResponseFile = Guid.NewGuid().ToString() + ".rsp";
string[] args = new[] { "/a", "@" + NonexistentResponseFile, "/f" };

Assert.Throws<FileNotFoundException>(
() => ExpandArguments.GenerateArguments(args, new FileSystem(), new EnvironmentVariables())
);
}

[Theory]
[InlineData(new[] { "/b", "/c:val /d", " /e " }, new[] { "/a", "/b", "/c:val", "/d", "/e", "/f" })]
public void GenerateArguments_ExpandsResponseFileContents(string[] rspContent, string[] expected)
{
const string ResponseFileName = "Mocked.rsp";
string[] args = new[] { "/a", "@" + ResponseFileName, "/f" };

SetupTestMocks(
ResponseFileName,
rspContent,
out Mock<IFileSystem> fileSystemMock,
out Mock<IEnvironmentVariables> environmentVariablesMock);

IFileSystem fileSystem = fileSystemMock.Object;
IEnvironmentVariables environmentVariables = environmentVariablesMock.Object;

string[] result = ExpandArguments.GenerateArguments(args, fileSystem, environmentVariables);

result.Should().ContainInOrder(expected);

fileSystemMock.Verify(fs => fs.PathGetFullPath(ResponseFileName), Times.Once);
fileSystemMock.Verify(fs => fs.FileReadAllLines(ResponseFileName), Times.Once);
environmentVariablesMock.Verify(ev => ev.ExpandEnvironmentVariables(ResponseFileName), Times.Once);
}

[Theory]
[InlineData(new[] { "/b", "/c:val /d", "# Random Comment", " /e " }, new[] { "/a", "/b", "/c:val", "/d", "/e", "/f" })]
[InlineData(new[] { "/b", "/c:val /d#Another Comment", " /e " }, new[] { "/a", "/b", "/c:val", "/d", "/e", "/f" })]
public void GenerateArguments_TrimCommentsFromResponseFileContents(string[] rspContent, string[] expected)
{
const string ResponseFileName = "Mocked.rsp";
string[] args = new[] { "/a", "@" + ResponseFileName, "/f" };

SetupTestMocks(
ResponseFileName,
rspContent,
out Mock<IFileSystem> fileSystemMock,
out Mock<IEnvironmentVariables> environmentVariablesMock);

IFileSystem fileSystem = fileSystemMock.Object;
IEnvironmentVariables environmentVariables = environmentVariablesMock.Object;

string[] result = ExpandArguments.GenerateArguments(args, fileSystem, environmentVariables);

result.Should().ContainInOrder(expected);

fileSystemMock.Verify(fs => fs.PathGetFullPath(ResponseFileName), Times.Once);
fileSystemMock.Verify(fs => fs.FileReadAllLines(ResponseFileName), Times.Once);
environmentVariablesMock.Verify(ev => ev.ExpandEnvironmentVariables(ResponseFileName), Times.Once);
}

[Theory]
[InlineData(new[] { "a \"one two\" b" }, new[] { "a", "one two", "b" })]
public void GenerateArguments_StripsQuotesFromAroundArgsWithSpacesInResponseFiles(string[] rspContent, string[] expected)
{
const string ResponseFileName = "Mocked.rsp";
string[] args = new[] { "@" + ResponseFileName };

SetupTestMocks(
ResponseFileName,
rspContent,
out Mock<IFileSystem> fileSystemMock,
out Mock<IEnvironmentVariables> environmentVariablesMock);

IFileSystem fileSystem = fileSystemMock.Object;
IEnvironmentVariables environmentVariables = environmentVariablesMock.Object;

string[] result = ExpandArguments.GenerateArguments(args, fileSystem, environmentVariables);

result.Length.Should().Be(3);
result.Should().ContainInOrder(expected);

fileSystemMock.Verify(fs => fs.PathGetFullPath(ResponseFileName), Times.Once);
fileSystemMock.Verify(fs => fs.FileReadAllLines(ResponseFileName), Times.Once);
environmentVariablesMock.Verify(ev => ev.ExpandEnvironmentVariables(ResponseFileName), Times.Once);
}

[Theory]
[InlineData(new[] { "a \"one two\" b" }, new[] { "a", "one two", "b" })]
public void GenerateArguments_ExpandsEnvironmentVariablesInResponseFilePathName(string[] rspContent, string[] expected)
{
const string DirectoryVariableName = "InstallationDirectory";
const string ResponseFileName = "Mocked.rsp";

string responseFileNameArgument = string.Format(
CultureInfo.InvariantCulture,
@"%{0}%\{1}",
DirectoryVariableName,
ResponseFileName
);

string[] args = new[] { "@" + responseFileNameArgument };

SetupTestMocks(
responseFileNameArgument,
rspContent,
out Mock<IFileSystem> fileSystemMock,
out Mock<IEnvironmentVariables> environmentVariablesMock);

IFileSystem fileSystem = fileSystemMock.Object;
IEnvironmentVariables environmentVariables = environmentVariablesMock.Object;

string[] result = ExpandArguments.GenerateArguments(args, fileSystem, environmentVariables);

result.Length.Should().Be(3);
result.Should().ContainInOrder(expected);

fileSystemMock.Verify(fs => fs.PathGetFullPath(responseFileNameArgument), Times.Once);
fileSystemMock.Verify(fs => fs.FileReadAllLines(responseFileNameArgument), Times.Once);
environmentVariablesMock.Verify(ev => ev.ExpandEnvironmentVariables(responseFileNameArgument), Times.Once);
}
}
}
Loading