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

SCAN4NET-227 Use system trusted certificate or JVM certificate store #2330

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
164 changes: 100 additions & 64 deletions Tests/SonarScanner.MSBuild.Common.Test/ProcessRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestUtilities;
using System.Security;

namespace SonarScanner.MSBuild.Common.Test;

Expand Down Expand Up @@ -54,7 +47,7 @@ public void ProcRunner_ExecutionFailed()

var logger = new TestLogger();
var args = new ProcessRunnerArguments(exeName, true);
var runner = new ProcessRunner(logger);
using var runner = new ProcessRunner(logger);

// Act
var success = runner.Execute(args);
Expand All @@ -69,14 +62,16 @@ public void ProcRunner_ExecutionSucceeded()
{
// Arrange
var exeName = TestUtils.WriteBatchFileForTest(TestContext,
@"@echo Hello world
xxx yyy
@echo Testing 1,2,3...>&2
");
"""
@echo off
@echo Hello world
xxx yyy
@echo Testing 1,2,3...>&2
""");

var logger = new TestLogger();
var args = new ProcessRunnerArguments(exeName, true);
var runner = new ProcessRunner(logger);
using var runner = new ProcessRunner(logger);

// Act
var success = runner.Execute(args);
Expand All @@ -87,6 +82,50 @@ xxx yyy

logger.AssertInfoLogged("Hello world"); // Check output message are passed to the logger
logger.AssertErrorLogged("Testing 1,2,3..."); // Check error messages are passed to the logger
runner.StandardOutput.ReadToEnd().Should().Be("Hello world" + Environment.NewLine);
runner.ErrorOutput.ReadToEnd().Should().Be("""
'xxx' is not recognized as an internal or external command,
operable program or batch file.
Testing 1,2,3...

""");
}

[TestMethod]
public void ProcRunner_LogOutputFalse_ExecutionSucceeded()
{
// Arrange
var exeName = TestUtils.WriteBatchFileForTest(TestContext,
"""
@echo off
@echo Hello world
xxx yyy
@echo Testing 1,2,3...>&2
""");

var logger = new TestLogger();
var args = new ProcessRunnerArguments(exeName, true)
{
LogOutput = false
};
using var runner = new ProcessRunner(logger);

// Act
var success = runner.Execute(args);

// Assert
success.Should().BeTrue("Expecting the process to have succeeded");
runner.ExitCode.Should().Be(0, "Unexpected exit code");

logger.AssertMessageNotLogged("Hello world");
logger.AssertErrorNotLogged("Testing 1,2,3...");
runner.StandardOutput.ReadToEnd().Should().Be("Hello world" + Environment.NewLine);
runner.ErrorOutput.ReadToEnd().Should().Be("""
'xxx' is not recognized as an internal or external command,
operable program or batch file.
Testing 1,2,3...

""");
}

[TestMethod]
Expand All @@ -99,16 +138,14 @@ public void ProcRunner_FailsOnTimeout()
// Alternatives such as
// pinging a non-existent address with a timeout were not reliable.
var exeName = TestUtils.WriteBatchFileForTest(TestContext,
@"waitfor /t 2 somethingThatNeverHappen
@echo Hello world
");
"""
waitfor /t 2 somethingThatNeverHappen
@echo Hello world
""");

var logger = new TestLogger();
var args = new ProcessRunnerArguments(exeName, true)
{
TimeoutInMilliseconds = 100
};
var runner = new ProcessRunner(logger);
var args = new ProcessRunnerArguments(exeName, true) { TimeoutInMilliseconds = 100 };
using var runner = new ProcessRunner(logger);

var timer = Stopwatch.StartNew();

Expand All @@ -133,22 +170,17 @@ public void ProcRunner_PassesEnvVariables()
{
// Arrange
var logger = new TestLogger();
var runner = new ProcessRunner(logger);
using var runner = new ProcessRunner(logger);

var exeName = TestUtils.WriteBatchFileForTest(TestContext,
@"echo %PROCESS_VAR%
@echo %PROCESS_VAR2%
@echo %PROCESS_VAR3%
");
var envVariables = new Dictionary<string, string>() {
{ "PROCESS_VAR", "PROCESS_VAR value" },
{ "PROCESS_VAR2", "PROCESS_VAR2 value" },
{ "PROCESS_VAR3", "PROCESS_VAR3 value" } };
"""
echo %PROCESS_VAR%
@echo %PROCESS_VAR2%
@echo %PROCESS_VAR3%
""");
var envVariables = new Dictionary<string, string> { { "PROCESS_VAR", "PROCESS_VAR value" }, { "PROCESS_VAR2", "PROCESS_VAR2 value" }, { "PROCESS_VAR3", "PROCESS_VAR3 value" } };

var args = new ProcessRunnerArguments(exeName, true)
{
EnvironmentVariables = envVariables
};
var args = new ProcessRunnerArguments(exeName, true) { EnvironmentVariables = envVariables };

// Act
var success = runner.Execute(args);
Expand All @@ -169,7 +201,7 @@ public void ProcRunner_PassesEnvVariables_OverrideExisting()

// Arrange
var logger = new TestLogger();
var runner = new ProcessRunner(logger);
using var runner = new ProcessRunner(logger);

try
{
Expand All @@ -180,21 +212,21 @@ public void ProcRunner_PassesEnvVariables_OverrideExisting()
Environment.SetEnvironmentVariable("proc.runner.test.user", "existing user value", EnvironmentVariableTarget.User);

var exeName = TestUtils.WriteBatchFileForTest(TestContext,
@"@echo file: %proc.runner.test.machine%
@echo file: %proc.runner.test.process%
@echo file: %proc.runner.test.user%
");
"""
@echo file: %proc.runner.test.machine%
@echo file: %proc.runner.test.process%
@echo file: %proc.runner.test.user%
""");

var envVariables = new Dictionary<string, string>() {
var envVariables = new Dictionary<string, string>
{
{ "proc.runner.test.machine", "machine override" },
{ "proc.runner.test.process", "process override" },
{ "proc.runner.test.user", "user override" } };

var args = new ProcessRunnerArguments(exeName, true)
{
EnvironmentVariables = envVariables
{ "proc.runner.test.user", "user override" }
};

var args = new ProcessRunnerArguments(exeName, true) { EnvironmentVariables = envVariables };

// Act
var success = runner.Execute(args);

Expand Down Expand Up @@ -228,7 +260,7 @@ public void ProcRunner_MissingExe()
// Arrange
var logger = new TestLogger();
var args = new ProcessRunnerArguments("missingExe.foo", false);
var runner = new ProcessRunner(logger);
using var runner = new ProcessRunner(logger);

// Act
var success = runner.Execute(args);
Expand All @@ -244,8 +276,9 @@ public void ProcRunner_ArgumentQuoting()
{
// Checks arguments passed to the child process are correctly quoted
var testDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);
var runner = new ProcessRunner(new TestLogger());
var expected = new[] {
using var runner = new ProcessRunner(new TestLogger());
var expected = new[]
{
"unquoted",
"\"quoted\"",
"\"quoted with spaces\"",
Expand Down Expand Up @@ -275,8 +308,9 @@ public void ProcRunner_ArgumentQuotingForwardedByBatchScript()
// Checks arguments passed to a batch script which itself passes them on are correctly escaped
var testDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);
var batchName = TestUtils.WriteBatchFileForTest(TestContext, "\"" + LogArgsPath() + "\" %*");
var runner = new ProcessRunner(new TestLogger());
var expected = new[] {
using var runner = new ProcessRunner(new TestLogger());
var expected = new[]
{
"unquoted",
"\"quoted\"",
"\"quoted with spaces\"",
Expand Down Expand Up @@ -307,25 +341,26 @@ public void ProcRunner_ArgumentQuotingScanner()
// Checks arguments passed to a batch script which itself passes them on are correctly escaped
var testDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);
var batchName = TestUtils.WriteBatchFileForTest(TestContext,
@"@echo off
REM The sonar-scanner.bat uses %* to pass the argument to javac.exe
echo %*
REM Because of the escaping, the single arguments are somewhat broken on echo. A workaround is to add some new lines for some reason.
echo %1
"""
@echo off
REM The sonar-scanner.bat uses %* to pass the argument to javac.exe
echo %*
REM Because of the escaping, the single arguments are somewhat broken on echo. A workaround is to add some new lines for some reason.
echo %1


echo %2
echo %2


echo %3
echo %3


echo %4
echo %4


");
""");
var logger = new TestLogger();
var runner = new ProcessRunner(logger);
using var runner = new ProcessRunner(logger);
var expected = new[]
{
@"-Dsonar.scanAllFiles=true",
Expand Down Expand Up @@ -356,15 +391,16 @@ public void ProcRunner_DoNotLogSensitiveData()
{
var testDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);
var logger = new TestLogger();
var runner = new ProcessRunner(logger);
using var runner = new ProcessRunner(logger);
// Public args - should appear in the log
var publicArgs = new[]
{
"public1",
"public2",
"/d:sonar.projectKey=my.key"
};
var sensitiveArgs = new[] {
var sensitiveArgs = new[]
{
// Public args - should appear in the log
"public1", "public2", "/dmy.key=value",

Expand Down Expand Up @@ -408,7 +444,7 @@ private static void SafeSetEnvironmentVariable(string key, string value, Environ
{
Environment.SetEnvironmentVariable(key, value, target);
}
catch (System.Security.SecurityException)
catch (SecurityException)
{
logger.LogWarning("Test setup error: user running the test doesn't have the permissions to set the environment variable. Key: {0}, value: {1}, target: {2}",
key, value, target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ public void GenerateFile_TrustStoreProperties_Mapped()
var analysisDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);
var settings = BuildSettings.CreateNonTeamBuildSettingsForTesting(analysisDir);
var propertiesProvider = new ListPropertiesProvider();
AddIfNotEmpty(propertiesProvider, SonarProperties.HostUrl, "https://localhost:9000");
AddIfNotEmpty(propertiesProvider, "sonar.scanner.truststorePath", "C:\\path\\to\\truststore.pfx");
AddIfNotEmpty(propertiesProvider, "sonar.scanner.truststorePassword", "password");
var args = CreateProcessedArgs(propertiesProvider);
Expand Down
Loading
Loading