Skip to content

Commit 041ecd6

Browse files
authored
Use HTTP transport for browser dotnettestcli (#10143)
Copilot-Session: 9cf9bdca-8f9a-436e-a367-6689c3fe8fa3
1 parent 63df86e commit 041ecd6

43 files changed

Lines changed: 3183 additions & 114 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/mstest-runner-protocol/004-protocol-dotnet-test-pipe.md‎

Lines changed: 154 additions & 40 deletions
Large diffs are not rendered by default.

‎src/Platform/Microsoft.Testing.Extensions.HangDump/InternalAPI/InternalAPI.Unshipped.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ static Microsoft.Testing.Platform.IPC.Serializers.BaseSerializer.WriteExecutionS
1616
static Microsoft.Testing.Platform.IPC.Serializers.BaseSerializer.WriteListPayload<T>(System.IO.Stream! stream, ushort fieldId, T[]? list, System.Action<System.IO.Stream!, T>! writeItem) -> void
1717
const Microsoft.Testing.Platform.IPC.FailedTestResultMessageFieldsId.Expected = 10 -> ushort
1818
const Microsoft.Testing.Platform.IPC.FailedTestResultMessageFieldsId.Actual = 11 -> ushort
19+
Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.ReadNextMessageAsync(System.IO.Stream! stream, System.Threading.CancellationToken cancellationToken, int maximumFrameSize = 2147483647) -> System.Threading.Tasks.Task<object?>!
20+
Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.WriteMessageAsync(System.IO.Stream! stream, Microsoft.Testing.Platform.IPC.INamedPipeSerializer! serializer, object! message, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
21+
*REMOVED*Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.ReadNextMessageAsync(System.IO.Pipes.PipeStream! stream, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<object?>!
22+
*REMOVED*Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.WriteMessageAsync(System.IO.Pipes.PipeStream! stream, Microsoft.Testing.Platform.IPC.INamedPipeSerializer! serializer, object! message, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!

‎src/Platform/Microsoft.Testing.Extensions.MSBuild/InternalAPI.Unshipped.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ static Microsoft.Testing.Platform.IPC.Serializers.BaseSerializer.WriteExecutionS
1515
static Microsoft.Testing.Platform.IPC.Serializers.BaseSerializer.WriteListPayload<T>(System.IO.Stream! stream, ushort fieldId, T[]? list, System.Action<System.IO.Stream!, T>! writeItem) -> void
1616
const Microsoft.Testing.Platform.IPC.FailedTestResultMessageFieldsId.Expected = 10 -> ushort
1717
const Microsoft.Testing.Platform.IPC.FailedTestResultMessageFieldsId.Actual = 11 -> ushort
18+
Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.ReadNextMessageAsync(System.IO.Stream! stream, System.Threading.CancellationToken cancellationToken, int maximumFrameSize = 2147483647) -> System.Threading.Tasks.Task<object?>!
19+
Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.WriteMessageAsync(System.IO.Stream! stream, Microsoft.Testing.Platform.IPC.INamedPipeSerializer! serializer, object! message, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
20+
*REMOVED*Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.ReadNextMessageAsync(System.IO.Pipes.PipeStream! stream, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<object?>!
21+
*REMOVED*Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.WriteMessageAsync(System.IO.Pipes.PipeStream! stream, Microsoft.Testing.Platform.IPC.INamedPipeSerializer! serializer, object! message, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!

‎src/Platform/Microsoft.Testing.Extensions.Retry/InternalAPI/InternalAPI.Unshipped.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ static Microsoft.Testing.Platform.IPC.Serializers.BaseSerializer.WriteExecutionS
1919
static Microsoft.Testing.Platform.IPC.Serializers.BaseSerializer.WriteListPayload<T>(System.IO.Stream! stream, ushort fieldId, T[]? list, System.Action<System.IO.Stream!, T>! writeItem) -> void
2020
const Microsoft.Testing.Platform.IPC.FailedTestResultMessageFieldsId.Expected = 10 -> ushort
2121
const Microsoft.Testing.Platform.IPC.FailedTestResultMessageFieldsId.Actual = 11 -> ushort
22+
Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.ReadNextMessageAsync(System.IO.Stream! stream, System.Threading.CancellationToken cancellationToken, int maximumFrameSize = 2147483647) -> System.Threading.Tasks.Task<object?>!
23+
Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.WriteMessageAsync(System.IO.Stream! stream, Microsoft.Testing.Platform.IPC.INamedPipeSerializer! serializer, object! message, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
24+
*REMOVED*Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.ReadNextMessageAsync(System.IO.Pipes.PipeStream! stream, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<object?>!
25+
*REMOVED*Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.WriteMessageAsync(System.IO.Pipes.PipeStream! stream, Microsoft.Testing.Platform.IPC.INamedPipeSerializer! serializer, object! message, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!

‎src/Platform/Microsoft.Testing.Extensions.TrxReport/InternalAPI/InternalAPI.Unshipped.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ Microsoft.Testing.Extensions.MergeOutputFileHelper
6969
static Microsoft.Testing.Extensions.MergeOutputFileHelper.BuildCaseFoldedProbePath(string! directory, string! probeFileName) -> string!
7070
static Microsoft.Testing.Extensions.MergeOutputFileHelper.EnsureOutputDoesNotAliasInput(System.Collections.Generic.IReadOnlyList<string!>! inputPaths, string! outputPath) -> void
7171
static Microsoft.Testing.Extensions.MergeOutputFileHelper.WriteViaTemporarySiblingAsync(string! outputPath, System.Func<string!, System.Threading.Tasks.Task!>! writeToTempAsync) -> System.Threading.Tasks.Task!
72+
Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.ReadNextMessageAsync(System.IO.Stream! stream, System.Threading.CancellationToken cancellationToken, int maximumFrameSize = 2147483647) -> System.Threading.Tasks.Task<object?>!
73+
Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.WriteMessageAsync(System.IO.Stream! stream, Microsoft.Testing.Platform.IPC.INamedPipeSerializer! serializer, object! message, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
74+
*REMOVED*Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.ReadNextMessageAsync(System.IO.Pipes.PipeStream! stream, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<object?>!
75+
*REMOVED*Microsoft.Testing.Platform.IPC.NamedPipeConnectionBase.WriteMessageAsync(System.IO.Pipes.PipeStream! stream, Microsoft.Testing.Platform.IPC.INamedPipeSerializer! serializer, object! message, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!

‎src/Platform/Microsoft.Testing.Platform/Builder/TestApplication.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private static async Task LogInformationAsync(
186186

187187
string moduleName = testApplicationModuleInfo.GetDisplayName();
188188
await logger.LogInformationAsync($"Test module: {moduleName}").ConfigureAwait(false);
189-
await logger.LogInformationAsync($"Command line arguments: '{(args.Length == 0 ? string.Empty : args.Aggregate((a, b) => $"{a} {b}"))}'").ConfigureAwait(false);
189+
await logger.LogInformationAsync($"Command line arguments: '{CommandLineArgumentsRedactor.Redact(args)}'").ConfigureAwait(false);
190190

191191
StringBuilder machineInfo = new();
192192
#pragma warning disable RS0030 // Do not use banned APIs
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.Testing.Platform.CommandLine;
5+
6+
internal static class CommandLineArgumentsRedactor
7+
{
8+
private const string RedactedPlaceholder = "***REDACTED***";
9+
10+
public static string Redact(string[] args)
11+
=> string.Join(" ", RedactArguments(args));
12+
13+
public static string RedactArgument(string[] args, int index)
14+
=> RedactArguments(args)[index];
15+
16+
private static string[] RedactArguments(string[] args)
17+
{
18+
if (args.Length == 0)
19+
{
20+
return [];
21+
}
22+
23+
string[] redacted = new string[args.Length];
24+
bool redactNextValue = false;
25+
bool sanitizeNextEndpoint = false;
26+
bool sensitiveValueConsumed = false;
27+
28+
for (int i = 0; i < args.Length; i++)
29+
{
30+
string arg = args[i];
31+
if (TryParseOption(arg, out string? prefix, out string? name, out string? delimiter, out string? inlineValue))
32+
{
33+
bool isToken = IsTokenOption(name);
34+
bool isEndpoint = IsEndpointOption(name);
35+
if (isToken)
36+
{
37+
redacted[i] = inlineValue is null
38+
? arg
39+
: $"{prefix}{name}{delimiter}{RedactedPlaceholder}";
40+
redactNextValue = true;
41+
sanitizeNextEndpoint = false;
42+
sensitiveValueConsumed = inlineValue is not null;
43+
}
44+
else if (isEndpoint)
45+
{
46+
redacted[i] = inlineValue is null
47+
? arg
48+
: $"{prefix}{name}{delimiter}{SanitizeEndpoint(inlineValue)}";
49+
sanitizeNextEndpoint = true;
50+
redactNextValue = false;
51+
sensitiveValueConsumed = inlineValue is not null;
52+
}
53+
else if (redactNextValue && !sensitiveValueConsumed)
54+
{
55+
redacted[i] = RedactedPlaceholder;
56+
redactNextValue = false;
57+
sanitizeNextEndpoint = false;
58+
sensitiveValueConsumed = false;
59+
}
60+
else if (sanitizeNextEndpoint && !sensitiveValueConsumed)
61+
{
62+
redacted[i] = SanitizeEndpoint(arg);
63+
redactNextValue = false;
64+
sanitizeNextEndpoint = false;
65+
sensitiveValueConsumed = false;
66+
}
67+
else
68+
{
69+
redacted[i] = arg;
70+
redactNextValue = false;
71+
sanitizeNextEndpoint = false;
72+
sensitiveValueConsumed = false;
73+
}
74+
75+
continue;
76+
}
77+
78+
if (redactNextValue)
79+
{
80+
redacted[i] = RedactedPlaceholder;
81+
sensitiveValueConsumed = true;
82+
}
83+
else if (sanitizeNextEndpoint)
84+
{
85+
redacted[i] = SanitizeEndpoint(arg);
86+
sensitiveValueConsumed = true;
87+
}
88+
else
89+
{
90+
redacted[i] = arg;
91+
}
92+
}
93+
94+
return redacted;
95+
}
96+
97+
private static bool TryParseOption(
98+
string arg,
99+
[NotNullWhen(true)] out string? prefix,
100+
[NotNullWhen(true)] out string? name,
101+
out string? delimiter,
102+
out string? inlineValue)
103+
{
104+
prefix = null;
105+
name = null;
106+
delimiter = null;
107+
inlineValue = null;
108+
109+
int optionStart = 0;
110+
while (optionStart < arg.Length && char.IsWhiteSpace(arg[optionStart]))
111+
{
112+
optionStart++;
113+
}
114+
115+
int prefixLength = 0;
116+
while (optionStart + prefixLength < arg.Length && arg[optionStart + prefixLength] == '-')
117+
{
118+
prefixLength++;
119+
}
120+
121+
bool hasLeadingWhitespace = optionStart > 0;
122+
bool isValidPrefix = !hasLeadingWhitespace && prefixLength is 1 or 2;
123+
bool isMalformedPrefix = hasLeadingWhitespace || prefixLength >= 3;
124+
if ((!isValidPrefix && !isMalformedPrefix) || optionStart + prefixLength == arg.Length)
125+
{
126+
return false;
127+
}
128+
129+
prefix = arg[..(optionStart + prefixLength)];
130+
string withoutPrefix = arg[(optionStart + prefixLength)..];
131+
int delimiterIndex = withoutPrefix.IndexOfAny(['=', ':']);
132+
if (delimiterIndex == -1)
133+
{
134+
name = withoutPrefix;
135+
}
136+
else
137+
{
138+
name = withoutPrefix[..delimiterIndex];
139+
delimiter = withoutPrefix[delimiterIndex].ToString();
140+
inlineValue = withoutPrefix[(delimiterIndex + 1)..];
141+
}
142+
143+
if (!isMalformedPrefix || IsTokenOption(name) || IsEndpointOption(name))
144+
{
145+
return true;
146+
}
147+
148+
prefix = null;
149+
name = null;
150+
delimiter = null;
151+
inlineValue = null;
152+
return false;
153+
}
154+
155+
private static bool IsTokenOption(string name)
156+
=> PlatformCommandLineProvider.DotNetTestHttpTokenOptionKey.Equals(name, StringComparison.OrdinalIgnoreCase);
157+
158+
private static bool IsEndpointOption(string name)
159+
=> PlatformCommandLineProvider.DotNetTestHttpEndpointOptionKey.Equals(name, StringComparison.OrdinalIgnoreCase);
160+
161+
private static string SanitizeEndpoint(string endpoint)
162+
=> Uri.TryCreate(endpoint, UriKind.Absolute, out Uri? uri)
163+
&& uri.Scheme is "http" or "https"
164+
&& RoslynString.IsNullOrEmpty(uri.UserInfo)
165+
? uri.GetLeftPart(UriPartial.Authority)
166+
: RedactedPlaceholder;
167+
}

‎src/Platform/Microsoft.Testing.Platform/CommandLine/ParseResult.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal CommandLineParseResult(string? toolName, IReadOnlyList<CommandLineParse
3535
ToolName = toolName;
3636
Options = options;
3737
Errors = errors;
38-
CommandLine = string.Join(" ", arguments);
38+
CommandLine = CommandLineArgumentsRedactor.Redact([.. arguments]);
3939
}
4040

4141
/// <summary>

‎src/Platform/Microsoft.Testing.Platform/CommandLine/Parser.cs‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,18 @@ private static CommandLineParseResult Parse(List<string> args, IEnvironment envi
4747
{
4848
string? currentArg = args[i];
4949

50-
if (currentArg.StartsWith("@", StringComparison.Ordinal) && ResponseFileHelper.TryReadResponseFile(currentArg.Substring(1), errors, out string[]? newArguments))
50+
if (currentArg.StartsWith("@", StringComparison.Ordinal))
5151
{
52-
args.InsertRange(i + 1, newArguments);
53-
continue;
52+
string responseFilePath = currentArg.Substring(1);
53+
string redactedResponseFileArgument = CommandLineArgumentsRedactor.RedactArgument([.. args], i);
54+
string diagnosticPath = redactedResponseFileArgument.StartsWith("@", StringComparison.Ordinal)
55+
? redactedResponseFileArgument.Substring(1)
56+
: redactedResponseFileArgument;
57+
if (ResponseFileHelper.TryReadResponseFile(responseFilePath, diagnosticPath, errors, out string[]? newArguments))
58+
{
59+
args.InsertRange(i + 1, newArguments);
60+
continue;
61+
}
5462
}
5563

5664
// If it's the first argument and it doesn't start with - then it's the tool name
@@ -80,7 +88,11 @@ private static CommandLineParseResult Parse(List<string> args, IEnvironment envi
8088
{
8189
if (currentOption is null)
8290
{
83-
errors.Add(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineParserUnexpectedArgument, args[i]));
91+
errors.Add(
92+
string.Format(
93+
CultureInfo.InvariantCulture,
94+
PlatformResources.CommandLineParserUnexpectedArgument,
95+
CommandLineArgumentsRedactor.RedactArgument([.. args], i)));
8496
}
8597
else
8698
{
@@ -125,9 +137,15 @@ static bool TryUnescape(string input, string? option, IEnvironment environment,
125137
{
126138
if (input.IndexOf('\'', 1, input.Length - 2) != -1)
127139
{
140+
const string redactedPlaceholder = "***REDACTED***";
141+
string displayInput =
142+
PlatformCommandLineProvider.DotNetTestHttpTokenOptionKey.Equals(option, StringComparison.OrdinalIgnoreCase)
143+
|| PlatformCommandLineProvider.DotNetTestHttpEndpointOptionKey.Equals(option, StringComparison.OrdinalIgnoreCase)
144+
? redactedPlaceholder
145+
: input;
128146
error = option is null
129-
? string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineParserUnexpectedSingleQuoteInArgument, input)
130-
: string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineParserUnexpectedSingleQuoteInArgumentForOption, input, option);
147+
? string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineParserUnexpectedSingleQuoteInArgument, displayInput)
148+
: string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineParserUnexpectedSingleQuoteInArgumentForOption, displayInput, option);
131149
return false;
132150
}
133151

0 commit comments

Comments
 (0)