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

Add test for parameter passing to batch -> java application #1765

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
97 changes: 97 additions & 0 deletions Tests/SonarScanner.MSBuild.Common.Test/ProcessRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,103 @@ REM The sonar-scanner.bat uses %* to pass the argument to javac.exe
@"""--debug""");
}

[DataTestMethod]
[DataRow(@"")] // in CI: one empty string
[DataRow(@"unquoted", @"unquoted")]
[DataRow(@"""quoted""", @"""quoted""")]
[DataRow(@"""quoted with spaces""", @"""quoted with spaces""")]
[DataRow(@"/test:1", @"/test:1")]
[DataRow(@"/test:""quoted arg""", @"/test:""quoted arg""")]
[DataRow(@"unquoted with spaces", @"unquoted with spaces")]
[DataRow(@"quote in ""the middle", @"quote in ""the middle")]
[DataRow(@"quote""name", @"quote""name")]
[DataRow(@"quotes ""& ampersands", @"quotes ""& ampersands")]
[DataRow(@"""multiple """""" quotes "" ", @"""multiple """""" quotes "" ")]
[DataRow(@"trailing backslash \", @"trailing backslash """)] // https://github.com/SonarSource/sonar-scanner-msbuild/issues/1706 - in CI: trailing backslash \
[DataRow(@"trailing backslash \""", @"trailing backslash \""")]
[DataRow(@"trailing\\backslash\\", @"trailing\\backslash\\")] // https://github.com/SonarSource/sonar-scanner-msbuild/issues/1706 - in CI: trailing \\backslash\\
[DataRow(@"trailing \\backslash\\", @"trailing \\backslash\")] // https://github.com/SonarSource/sonar-scanner-msbuild/issues/1706
[DataRow(@"trailing \""""\ backslash""\\""", @"trailing \""\", @"backslash""\""""")] // https://github.com/SonarSource/sonar-scanner-msbuild/issues/1706 - in CI: trailing \""\ backslash"\\"
[DataRow(@"all special chars: \ / : * ? "" < > | %", @"all special chars: \ / : * ? "" < > | %")]
[DataRow(@"injection "" > foo.txt", @"injection "" > foo.txt")]
[DataRow(@"injection "" & echo haha", @"injection "" & echo haha")]
[DataRow(@"double escaping \"" > foo.txt", @"double escaping \", @">", @"foo.txt")] // https://github.com/SonarSource/sonar-scanner-msbuild/issues/1706 - in CI: double escaping \" > foo.txt
[DataRow(@"^", @"^")]
[DataRow(@"a^", @"a^")]
[DataRow(@"a^b^c", @"a^b^c")]
[DataRow(@"a^^b", @"a^^b")]
[DataRow(@">Test.txt", @">Test.txt")]
[DataRow(@"a>Test.txt", @"a>Test.txt")]
[DataRow(@"a>>Test.txt", @"a>>Test.txt")]
[DataRow(@"<Test.txt", @"<Test.txt")]
[DataRow(@"a<Test.txt", @"a<Test.txt")]
[DataRow(@"a<<Test.txt", @"a<<Test.txt")]
[DataRow(@"&Test.txt", @"&Test.txt")]
[DataRow(@"a&Test.txt", @"a&Test.txt")]
[DataRow(@"a&&Test.txt", @"a&&Test.txt")]
[DataRow(@"|Test.txt", @"|Test.txt")]
[DataRow(@"a|Test.txt", @"a|Test.txt")]
[DataRow(@"a||Test.txt", @"a||Test.txt")]
[DataRow(@"a|b^c>d<e", @"a|b^c>d<e")]
[DataRow(@"%", @"%")]
[DataRow(@"'", @"'")]
[DataRow(@"`", @"`")]
[DataRow(@"\", @"\")]
[DataRow(@"(", @"(")]
[DataRow(@")", @")")]
[DataRow(@"[", @"[")]
[DataRow(@"]", @"]")]
[DataRow(@"!", @"!")]
[DataRow(@".", @".")]
[DataRow(@"*", @"LogArgs.class", @"LogArgs.java", @"ProcRunner_ArgumentQuotingForwardedByBatchScriptToJava.bat")] // Expected behavior - in CI: *
[DataRow(@"*.*", @"LogArgs.class", @"LogArgs.java", @"ProcRunner_ArgumentQuotingForwardedByBatchScriptToJava.bat")] // Expected behavior - in CI: *.*
[DataRow(@"""C:\*.*""", @"""C:\*.*""")]
[DataRow(@"?", @"?")]
[DataRow(@"=", @"=")]
[DataRow(@"a=b", @"a=b")]
[DataRow(@"äöüß", @"äöüß")] // in CI: ����
[DataRow(@"Σὲ γνωρίζω ἀπὸ τὴν κόψη", @"S? ??????? ?p? t?? ????")]
public void ProcRunner_ArgumentQuotingForwardedByBatchScriptToJava(string parameter, params string[] expected)
{
// Checks arguments passed to a batch script which itself passes them on are correctly escaped
var testDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);
File.WriteAllText($@"{testDir}\LogArgs.java", @"
import java.io.*;

class LogArgs {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter(""LogArgs.log""));
for (String arg : args) {
pw.println(arg);
}
pw.close();
}
}");
// This simulates the %* behavior of sonar-scanner.bat
// https://github.com/SonarSource/sonar-scanner-cli/blob/5a8476b77a7a679d8adebdfe69fa4c9fda4a96ff/src/main/assembly/bin/sonar-scanner.bat#L72
var batchName = TestUtils.WriteBatchFileForTest(TestContext, @"
if not exist LogArgs.class (
javac LogArgs.java
)
java LogArgs %*");
var logger = new TestLogger();
var runner = new ProcessRunner(logger);
var args = new ProcessRunnerArguments(batchName, isBatchScript: true) { CmdLineArgs = new[] { parameter }, WorkingDirectory = testDir };
try
{
var success = runner.Execute(args);

success.Should().BeTrue("Expecting the process to have succeeded");
runner.ExitCode.Should().Be(0, "Unexpected exit code");
AssertExpectedLogContents(testDir, expected);
}
finally
{
File.Delete(batchName);
File.Delete($@"{testDir}\LogArgs.log");
}
}

[TestMethod]
[WorkItem(126)] // Exclude secrets from log data: http://jira.sonarsource.com/browse/SONARMSBRU-126
public void ProcRunner_DoNotLogSensitiveData()
Expand Down
Loading