From d31aba8b337b383d741a85794fd607efa4c13b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 15:22:00 +0200 Subject: [PATCH 01/14] Authorize extension pipes for sandboxed test hosts Share the validated test host controller authorization with TRX, HangDump, and Retry pipes, and publish each server's effective sandbox-qualified endpoint before launching the child process. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../HangDumpEnvironmentVariableProvider.cs | 5 +- .../HangDumpExtensions.cs | 10 ++- .../HangDumpProcessLifetimeHandler.cs | 24 ++++-- .../InternalAPI/InternalAPI.Unshipped.txt | 6 ++ .../RetryFailedTestsPipeServer.cs | 13 ++-- .../RetryOrchestrator.cs | 3 +- .../InternalAPI/InternalAPI.Unshipped.txt | 6 ++ .../TrxEnvironmentVariableProvider.cs | 5 +- .../TrxProcessLifetimeHandler.cs | 21 +++++- .../TrxReportExtensions.cs | 11 +-- .../Hosts/TestHostBuilder.Modes.cs | 14 ++++ .../Hosts/TestHostControllersTestHost.Ipc.cs | 75 ++----------------- ...ontrollersTestHost.ProcessConfiguration.cs | 7 ++ ...ostControllersTestHost.ProcessLifecycle.cs | 9 --- .../InternalAPI/InternalAPI.Unshipped.txt | 10 +++ .../Services/ServiceProvider.cs | 2 + .../Services/ServiceProviderExtensions.cs | 53 +++++++++++++ .../NamedPipeServerEndpoint.cs | 9 +++ .../TestHostControllersManager.cs | 2 +- ...ntrollerConnectionAuthorizationConsumer.cs | 6 ++ ...PipeNameEnvironmentVariableProviderBase.cs | 5 +- .../EnvironmentVariableProviderTests.cs | 11 ++- .../HangDumpTests.cs | 44 +++++++++++ .../RetryTests.cs | 30 ++++++++ .../TrxProcessLifetimeHandlerTests.cs | 53 ++++++++++--- .../TestApplicationBuilderTests.cs | 27 ++++++- 26 files changed, 340 insertions(+), 121 deletions(-) create mode 100644 src/Platform/Microsoft.Testing.Platform/TestHostControllers/NamedPipeServerEndpoint.cs create mode 100644 src/Platform/Microsoft.Testing.Platform/TestHostOrchestrator/ITestHostControllerConnectionAuthorizationConsumer.cs diff --git a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpEnvironmentVariableProvider.cs b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpEnvironmentVariableProvider.cs index 84db88d86e..0542a9641e 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpEnvironmentVariableProvider.cs +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpEnvironmentVariableProvider.cs @@ -3,6 +3,7 @@ using Microsoft.Testing.Extensions.Diagnostics.Resources; using Microsoft.Testing.Platform.CommandLine; +using Microsoft.Testing.Platform.TestHostControllers; namespace Microsoft.Testing.Extensions.Diagnostics; @@ -12,8 +13,8 @@ internal sealed class HangDumpEnvironmentVariableProvider : global::Microsoft.Te private readonly ICommandLineOptions _commandLineOptions; - public HangDumpEnvironmentVariableProvider(ICommandLineOptions commandLineOptions, string pipeName) - : base(pipeName, PipeNameEnvironmentVariableName) + public HangDumpEnvironmentVariableProvider(ICommandLineOptions commandLineOptions, NamedPipeServerEndpoint endpoint) + : base(endpoint, PipeNameEnvironmentVariableName) { _commandLineOptions = commandLineOptions; } diff --git a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpExtensions.cs b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpExtensions.cs index e61d8c3d94..f9067e9fe8 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpExtensions.cs +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpExtensions.cs @@ -6,6 +6,7 @@ using Microsoft.Testing.Platform.Extensions; using Microsoft.Testing.Platform.IPC; using Microsoft.Testing.Platform.Services; +using Microsoft.Testing.Platform.TestHostControllers; #if !NETCOREAPP using Polyfills; @@ -43,11 +44,11 @@ public static void AddHangDumpProvider(this ITestApplicationBuilder builder) throw new PlatformNotSupportedException("Hang dump extension is not available on wasi"); } - PipeNameDescription pipeNameDescription = NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N")); + var endpoint = new NamedPipeServerEndpoint(NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N")).Name); builder.TestHostControllers.AddProcessLifetimeHandler(serviceProvider => new HangDumpProcessLifetimeHandler( - pipeNameDescription, + endpoint, serviceProvider.GetMessageBus(), serviceProvider.GetOutputDevice(), serviceProvider.GetCommandLineOptions(), @@ -56,10 +57,11 @@ public static void AddHangDumpProvider(this ITestApplicationBuilder builder) serviceProvider.GetLoggerFactory(), serviceProvider.GetConfiguration(), serviceProvider.GetProcessHandler(), - serviceProvider.GetClock())); + serviceProvider.GetClock(), + serviceProvider)); builder.TestHostControllers.AddEnvironmentVariableProvider(serviceProvider - => new HangDumpEnvironmentVariableProvider(serviceProvider.GetCommandLineOptions(), pipeNameDescription.Name)); + => new HangDumpEnvironmentVariableProvider(serviceProvider.GetCommandLineOptions(), endpoint)); builder.CommandLine.AddProvider(() => new HangDumpCommandLineProvider()); diff --git a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs index 645d507f7b..9cdd2533e7 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs @@ -15,6 +15,8 @@ using Microsoft.Testing.Platform.Logging; using Microsoft.Testing.Platform.Messages; using Microsoft.Testing.Platform.OutputDevice; +using Microsoft.Testing.Platform.Services; +using Microsoft.Testing.Platform.TestHostControllers; namespace Microsoft.Testing.Extensions.Diagnostics; @@ -36,7 +38,8 @@ internal sealed partial class HangDumpProcessLifetimeHandler : ITestHostProcessL private readonly IConfiguration _configuration; private readonly IProcessHandler _processHandler; private readonly IClock _clock; - private readonly PipeNameDescription _pipeNameDescription; + private readonly IServiceProvider _serviceProvider; + private readonly NamedPipeServerEndpoint _endpoint; private readonly bool _traceEnabled; private readonly ILogger _logger; private readonly ManualResetEventSlim _waitConsumerPipeName = new(false); @@ -84,7 +87,7 @@ internal sealed partial class HangDumpProcessLifetimeHandler : ITestHostProcessL private CancellationTokenSource? _handshakeCancellationTokenSource; public HangDumpProcessLifetimeHandler( - PipeNameDescription pipeNameDescription, + NamedPipeServerEndpoint endpoint, IMessageBus messageBus, IOutputDevice outputDevice, ICommandLineOptions commandLineOptions, @@ -93,11 +96,12 @@ public HangDumpProcessLifetimeHandler( ILoggerFactory loggerFactory, IConfiguration configuration, IProcessHandler processHandler, - IClock clock) + IClock clock, + IServiceProvider serviceProvider) { _logger = loggerFactory.CreateLogger(); _traceEnabled = _logger.IsEnabled(LogLevel.Trace); - _pipeNameDescription = pipeNameDescription; + _endpoint = endpoint; _messageBus = messageBus; _outputDisplay = new OutputDeviceWriter(outputDevice, this); _commandLineOptions = commandLineOptions; @@ -106,6 +110,7 @@ public HangDumpProcessLifetimeHandler( _configuration = configuration; _processHandler = processHandler; _clock = clock; + _serviceProvider = serviceProvider; } public string Uid => nameof(HangDumpProcessLifetimeHandler); @@ -163,7 +168,16 @@ await _outputDisplay.DisplayAsync( await _logger.LogInformationAsync($"Hang dump deadline setup {_deadlineDumpAt:o}.").ConfigureAwait(false); } - _singleConnectionNamedPipeServer = new(_pipeNameDescription, CallbackAsync, _environment, _logger, _task, cancellationToken); + _singleConnectionNamedPipeServer = new( + new PipeNameDescription(_endpoint.PipeName), + CallbackAsync, + _environment, + _logger, + _task, + maxNumberOfServerInstances: 1, + _serviceProvider.GetTestHostControllerAuthorizedSecurityIdentities(), + cancellationToken); + _endpoint.PipeName = _singleConnectionNamedPipeServer.PipeName.Name; _singleConnectionNamedPipeServer.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); _singleConnectionNamedPipeServer.RegisterSerializer(new ConsumerPipeNameRequestSerializer(), typeof(ConsumerPipeNameRequest)); _singleConnectionNamedPipeServer.RegisterSerializer(new ActivitySignalRequestSerializer(), typeof(ActivitySignalRequest)); diff --git a/src/Platform/Microsoft.Testing.Extensions.HangDump/InternalAPI/InternalAPI.Unshipped.txt b/src/Platform/Microsoft.Testing.Extensions.HangDump/InternalAPI/InternalAPI.Unshipped.txt index 0a57dc1e23..8fe5137124 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/InternalAPI/InternalAPI.Unshipped.txt +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/InternalAPI/InternalAPI.Unshipped.txt @@ -1,4 +1,10 @@ #nullable enable +Microsoft.Testing.Extensions.Diagnostics.HangDumpEnvironmentVariableProvider.HangDumpEnvironmentVariableProvider(Microsoft.Testing.Platform.CommandLine.ICommandLineOptions! commandLineOptions, Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint! endpoint) -> void +Microsoft.Testing.Extensions.Diagnostics.HangDumpProcessLifetimeHandler.HangDumpProcessLifetimeHandler(Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint! endpoint, Microsoft.Testing.Platform.Messages.IMessageBus! messageBus, Microsoft.Testing.Platform.OutputDevice.IOutputDevice! outputDevice, Microsoft.Testing.Platform.CommandLine.ICommandLineOptions! commandLineOptions, Microsoft.Testing.Platform.Helpers.ITask! task, Microsoft.Testing.Platform.Helpers.IEnvironment! environment, Microsoft.Testing.Platform.Logging.ILoggerFactory! loggerFactory, Microsoft.Testing.Platform.Configurations.IConfiguration! configuration, Microsoft.Testing.Platform.Helpers.IProcessHandler! processHandler, Microsoft.Testing.Platform.Helpers.IClock! clock, System.IServiceProvider! serviceProvider) -> void +Microsoft.Testing.Extensions.PipeNameEnvironmentVariableProviderBase.PipeNameEnvironmentVariableProviderBase(Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint! endpoint, string! environmentVariableName) -> void +*REMOVED*Microsoft.Testing.Extensions.Diagnostics.HangDumpEnvironmentVariableProvider.HangDumpEnvironmentVariableProvider(Microsoft.Testing.Platform.CommandLine.ICommandLineOptions! commandLineOptions, string! pipeName) -> void +*REMOVED*Microsoft.Testing.Extensions.Diagnostics.HangDumpProcessLifetimeHandler.HangDumpProcessLifetimeHandler(Microsoft.Testing.Platform.IPC.PipeNameDescription! pipeNameDescription, Microsoft.Testing.Platform.Messages.IMessageBus! messageBus, Microsoft.Testing.Platform.OutputDevice.IOutputDevice! outputDevice, Microsoft.Testing.Platform.CommandLine.ICommandLineOptions! commandLineOptions, Microsoft.Testing.Platform.Helpers.ITask! task, Microsoft.Testing.Platform.Helpers.IEnvironment! environment, Microsoft.Testing.Platform.Logging.ILoggerFactory! loggerFactory, Microsoft.Testing.Platform.Configurations.IConfiguration! configuration, Microsoft.Testing.Platform.Helpers.IProcessHandler! processHandler, Microsoft.Testing.Platform.Helpers.IClock! clock) -> void +*REMOVED*Microsoft.Testing.Extensions.PipeNameEnvironmentVariableProviderBase.PipeNameEnvironmentVariableProviderBase(string! pipeName, string! environmentVariableName) -> void const Microsoft.Testing.Platform.Helpers.EnvironmentVariableConstants.TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER = "TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER" -> string! const Microsoft.Testing.Platform.IPC.FileArtifactMessageFieldsId.InputArtifactPaths = 8 -> ushort const Microsoft.Testing.Platform.IPC.FileArtifactMessageFieldsId.Kind = 7 -> ushort diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs index b85b31bebc..c341f17f7d 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs @@ -15,17 +15,20 @@ namespace Microsoft.Testing.Extensions.Policy; internal sealed class RetryFailedTestsPipeServer : IDisposable { private readonly NamedPipeServer _singleConnectionNamedPipeServer; - private readonly PipeNameDescription _pipeNameDescription; private readonly string[] _failedTests; public RetryFailedTestsPipeServer(IServiceProvider serviceProvider, string[] failedTests, ILogger logger) { - _pipeNameDescription = NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N")); - logger.LogTrace($"Retry server pipe name: '{_pipeNameDescription.Name}'"); - _singleConnectionNamedPipeServer = new NamedPipeServer(_pipeNameDescription, CallbackAsync, + PipeNameDescription pipeNameDescription = NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N")); + logger.LogTrace($"Retry server pipe name: '{pipeNameDescription.Name}'"); + _singleConnectionNamedPipeServer = new NamedPipeServer( + pipeNameDescription, + CallbackAsync, serviceProvider.GetEnvironment(), serviceProvider.GetLoggerFactory().CreateLogger(), serviceProvider.GetTask(), + maxNumberOfServerInstances: 1, + serviceProvider.GetTestHostControllerAuthorizedSecurityIdentities(), serviceProvider.GetTestApplicationCancellationTokenSource().CancellationToken); _singleConnectionNamedPipeServer.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); @@ -37,7 +40,7 @@ public RetryFailedTestsPipeServer(IServiceProvider serviceProvider, string[] fai _failedTests = failedTests; } - public string PipeName => _pipeNameDescription.Name; + public string PipeName => _singleConnectionNamedPipeServer.PipeName.Name; /// /// Gets the distinct uids of the tests that failed in this attempt, mapped to their display name. diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryOrchestrator.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryOrchestrator.cs index 63b1ab7fff..989fdecb05 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryOrchestrator.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryOrchestrator.cs @@ -13,11 +13,12 @@ using Microsoft.Testing.Platform.OutputDevice; using Microsoft.Testing.Platform.Services; using Microsoft.Testing.Platform.Telemetry; +using Microsoft.Testing.Platform.TestHostOrchestrator; namespace Microsoft.Testing.Extensions.Policy; [UnsupportedOSPlatform("browser")] -internal sealed class RetryOrchestrator : ITestHostExecutionOrchestrator, IOutputDeviceDataProducer +internal sealed class RetryOrchestrator : ITestHostExecutionOrchestrator, IOutputDeviceDataProducer, ITestHostControllerConnectionAuthorizationConsumer { private readonly IServiceProvider _serviceProvider; private readonly ICommandLineOptions _commandLineOptions; diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/InternalAPI/InternalAPI.Unshipped.txt b/src/Platform/Microsoft.Testing.Extensions.TrxReport/InternalAPI/InternalAPI.Unshipped.txt index bdac54e837..6972459f0a 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/InternalAPI/InternalAPI.Unshipped.txt +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/InternalAPI/InternalAPI.Unshipped.txt @@ -1,4 +1,10 @@ #nullable enable +Microsoft.Testing.Extensions.PipeNameEnvironmentVariableProviderBase.PipeNameEnvironmentVariableProviderBase(Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint! endpoint, string! environmentVariableName) -> void +Microsoft.Testing.Extensions.TrxReport.Abstractions.TrxEnvironmentVariableProvider.TrxEnvironmentVariableProvider(Microsoft.Testing.Platform.CommandLine.ICommandLineOptions! commandLineOptions, Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint! endpoint) -> void +Microsoft.Testing.Extensions.TrxReport.Abstractions.TrxProcessLifetimeHandler.TrxProcessLifetimeHandler(Microsoft.Testing.Platform.CommandLine.ICommandLineOptions! commandLineOptions, Microsoft.Testing.Platform.Helpers.IEnvironment! environment, Microsoft.Testing.Platform.Logging.ILoggerFactory! loggerFactory, Microsoft.Testing.Platform.Messages.IMessageBus! messageBus, Microsoft.Testing.Platform.Helpers.IFileSystem! fileSystem, Microsoft.Testing.Platform.Services.ITestApplicationModuleInfo! testApplicationModuleInfo, Microsoft.Testing.Platform.Configurations.IConfiguration! configuration, Microsoft.Testing.Platform.Helpers.IClock! clock, Microsoft.Testing.Platform.Helpers.ITask! task, Microsoft.Testing.Platform.OutputDevice.IOutputDevice! outputDevice, System.IServiceProvider! serviceProvider, Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint! endpoint) -> void +*REMOVED*Microsoft.Testing.Extensions.PipeNameEnvironmentVariableProviderBase.PipeNameEnvironmentVariableProviderBase(string! pipeName, string! environmentVariableName) -> void +*REMOVED*Microsoft.Testing.Extensions.TrxReport.Abstractions.TrxEnvironmentVariableProvider.TrxEnvironmentVariableProvider(Microsoft.Testing.Platform.CommandLine.ICommandLineOptions! commandLineOptions, string! pipeName) -> void +*REMOVED*Microsoft.Testing.Extensions.TrxReport.Abstractions.TrxProcessLifetimeHandler.TrxProcessLifetimeHandler(Microsoft.Testing.Platform.CommandLine.ICommandLineOptions! commandLineOptions, Microsoft.Testing.Platform.Helpers.IEnvironment! environment, Microsoft.Testing.Platform.Logging.ILoggerFactory! loggerFactory, Microsoft.Testing.Platform.Messages.IMessageBus! messageBus, Microsoft.Testing.Platform.Helpers.IFileSystem! fileSystem, Microsoft.Testing.Platform.Services.ITestApplicationModuleInfo! testApplicationModuleInfo, Microsoft.Testing.Platform.Configurations.IConfiguration! configuration, Microsoft.Testing.Platform.Helpers.IClock! clock, Microsoft.Testing.Platform.Helpers.ITask! task, Microsoft.Testing.Platform.OutputDevice.IOutputDevice! outputDevice, Microsoft.Testing.Platform.IPC.PipeNameDescription! pipeNameDescription) -> void Microsoft.Testing.Platform.Helpers.RuntimeFeatureHelper static Microsoft.Testing.Platform.Helpers.RuntimeFeatureHelper.IsMultiThreaded.get -> bool const Microsoft.Testing.Platform.Helpers.EnvironmentVariableConstants.TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER = "TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER" -> string! diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxEnvironmentVariableProvider.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxEnvironmentVariableProvider.cs index bfa5fefda1..669bfff47a 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxEnvironmentVariableProvider.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxEnvironmentVariableProvider.cs @@ -4,6 +4,7 @@ using Microsoft.Testing.Extensions.TrxReport.Resources; using Microsoft.Testing.Platform.CommandLine; using Microsoft.Testing.Platform.Helpers; +using Microsoft.Testing.Platform.TestHostControllers; namespace Microsoft.Testing.Extensions.TrxReport.Abstractions; @@ -13,8 +14,8 @@ internal sealed class TrxEnvironmentVariableProvider : global::Microsoft.Testing private readonly ICommandLineOptions _commandLineOptions; - public TrxEnvironmentVariableProvider(ICommandLineOptions commandLineOptions, string pipeName) - : base(pipeName, TRXNAMEDPIPENAME) + public TrxEnvironmentVariableProvider(ICommandLineOptions commandLineOptions, NamedPipeServerEndpoint endpoint) + : base(endpoint, TRXNAMEDPIPENAME) { _commandLineOptions = commandLineOptions; } diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs index d2112ecdc8..72c83392d5 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs @@ -20,6 +20,7 @@ using Microsoft.Testing.Platform.OutputDevice; using Microsoft.Testing.Platform.Services; using Microsoft.Testing.Platform.TestHost; +using Microsoft.Testing.Platform.TestHostControllers; namespace Microsoft.Testing.Extensions.TrxReport.Abstractions; @@ -43,7 +44,8 @@ internal sealed class TrxProcessLifetimeHandler : private readonly ITask _task; private readonly IOutputDevice _outputDevice; private readonly ILogger _logger; - private readonly PipeNameDescription _pipeNameDescription; + private readonly IServiceProvider _serviceProvider; + private readonly NamedPipeServerEndpoint _endpoint; private readonly Dictionary> _fileArtifacts = []; private readonly DateTimeOffset _startTime; @@ -64,7 +66,8 @@ public TrxProcessLifetimeHandler( IClock clock, ITask task, IOutputDevice outputDevice, - PipeNameDescription pipeNameDescription) + IServiceProvider serviceProvider, + NamedPipeServerEndpoint endpoint) { _commandLineOptions = commandLineOptions; _environment = environment; @@ -75,7 +78,8 @@ public TrxProcessLifetimeHandler( _clock = clock; _task = task; _outputDevice = outputDevice; - _pipeNameDescription = pipeNameDescription; + _serviceProvider = serviceProvider; + _endpoint = endpoint; _logger = loggerFactory.CreateLogger(); _startTime = _clock.UtcNow; } @@ -120,7 +124,16 @@ public Task BeforeTestHostProcessStartAsync(CancellationToken cancellationToken) [UnsupportedOSPlatform("BROWSER")] private void BeforeTestHostProcessStartCore(CancellationToken cancellationToken) { - _singleConnectionNamedPipeServer = new(_pipeNameDescription, CallbackAsync, _environment, _logger, _task, cancellationToken); + _singleConnectionNamedPipeServer = new( + new PipeNameDescription(_endpoint.PipeName), + CallbackAsync, + _environment, + _logger, + _task, + maxNumberOfServerInstances: 1, + _serviceProvider.GetTestHostControllerAuthorizedSecurityIdentities(), + cancellationToken); + _endpoint.PipeName = _singleConnectionNamedPipeServer.PipeName.Name; _singleConnectionNamedPipeServer.RegisterSerializer(new ReportFileNameRequestSerializer(), typeof(ReportFileNameRequest)); _singleConnectionNamedPipeServer.RegisterSerializer(new TestAdapterInformationRequestSerializer(), typeof(TestAdapterInformationRequest)); _singleConnectionNamedPipeServer.RegisterSerializer(new TrxStreamLocationRequestSerializer(), typeof(TrxStreamLocationRequest)); diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs index 08df022e7b..f87637714b 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs @@ -87,12 +87,12 @@ private static void NonBrowserRegistrations(ITestApplicationBuilder builder) serviceProvider.GetCommandLineOptions(), serviceProvider.GetEnvironment())); - PipeNameDescription pipeNameDescription = NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N")); + var endpoint = new NamedPipeServerEndpoint(NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N")).Name); var compositeLifeTimeHandler = new CompositeExtensionFactory(serviceProvider => { ILoggerFactory loggerFactory = serviceProvider.GetLoggerFactory(); - loggerFactory.CreateLogger().LogTrace($"TRX pipe name: '{pipeNameDescription.Name}"); + loggerFactory.CreateLogger().LogTrace($"TRX pipe name: '{endpoint.PipeName}"); return new TrxProcessLifetimeHandler( serviceProvider.GetCommandLineOptions(), serviceProvider.GetEnvironment(), @@ -104,14 +104,15 @@ private static void NonBrowserRegistrations(ITestApplicationBuilder builder) serviceProvider.GetSystemClock(), serviceProvider.GetTask(), serviceProvider.GetOutputDevice(), - pipeNameDescription); + serviceProvider, + endpoint); }); ((TestHostControllersManager)builder.TestHostControllers).AddDataConsumer(compositeLifeTimeHandler); builder.TestHostControllers.AddProcessLifetimeHandler(compositeLifeTimeHandler); builder.TestHostControllers.AddEnvironmentVariableProvider(serviceProvider => { - serviceProvider.GetLoggerFactory().CreateLogger().LogTrace($"TRX pipe name: '{pipeNameDescription.Name}"); - return new TrxEnvironmentVariableProvider(serviceProvider.GetCommandLineOptions(), pipeNameDescription.Name); + serviceProvider.GetLoggerFactory().CreateLogger().LogTrace($"TRX pipe name: '{endpoint.PipeName}"); + return new TrxEnvironmentVariableProvider(serviceProvider.GetCommandLineOptions(), endpoint); }); } } diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs index f4a1ec5a39..c52b3459d9 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs @@ -6,6 +6,7 @@ using Microsoft.Testing.Platform.Configurations; using Microsoft.Testing.Platform.Extensions.ArtifactPostProcessing; using Microsoft.Testing.Platform.Extensions.TestHost; +using Microsoft.Testing.Platform.Extensions.TestHostControllers; using Microsoft.Testing.Platform.Extensions.TestHostOrchestrator; using Microsoft.Testing.Platform.Helpers; using Microsoft.Testing.Platform.IPC; @@ -131,6 +132,19 @@ await LogTestHostCreatedAsync( return null; } + if (testHostOrchestratorConfiguration.TestHostOrchestrators.Any( + static orchestrator => orchestrator is ITestHostControllerConnectionAuthorizationConsumer)) + { + ITestHostLauncher? testHostLauncher = + await ((TestHostControllersManager)TestHostControllers).BuildTestHostLauncherAsync(context.ServiceProvider).ConfigureAwait(false); + ExecutableInfo executableInfo = context.ServiceProvider.GetTestApplicationModuleInfo().GetCurrentExecutableInfo(); + await context.ServiceProvider.ResolveTestHostControllerAuthorizedSecurityIdentitiesAsync( + testHostLauncher, + executableInfo.FilePath, + context.LoggerFactory.CreateLogger(), + context.TestApplicationCancellationTokenSource.CancellationToken).ConfigureAwait(false); + } + context.PoliciesService.ProcessRole = TestProcessRole.TestHostOrchestrator; await context.ProxyOutputDevice.HandleProcessRoleAsync(TestProcessRole.TestHostOrchestrator, context.TestApplicationCancellationTokenSource.CancellationToken).ConfigureAwait(false); diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.Ipc.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.Ipc.cs index ee0b9fa0b0..a3354cb010 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.Ipc.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.Ipc.cs @@ -1,12 +1,10 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using Microsoft.Testing.Platform.Extensions.TestHostControllers; using Microsoft.Testing.Platform.IPC; using Microsoft.Testing.Platform.IPC.Models; using Microsoft.Testing.Platform.IPC.Serializers; using Microsoft.Testing.Platform.Logging; -using Microsoft.Testing.Platform.Resources; using Microsoft.Testing.Platform.Services; namespace Microsoft.Testing.Platform.Hosts; @@ -15,83 +13,24 @@ internal sealed partial class TestHostControllersTestHost { private async Task CreateTestHostControllerIpcAsync(ExecutableInfo executableInfo, CancellationToken cancellationToken) { + IReadOnlyList? authorizedSecurityIdentities = await ServiceProvider.ResolveTestHostControllerAuthorizedSecurityIdentitiesAsync( + _testHostsInformation.TestHostLauncher, + executableInfo.FilePath, + _logger, + cancellationToken).ConfigureAwait(false); + NamedPipeServer testHostControllerIpc = new( $"MONITORTOHOST_{Guid.NewGuid():N}", HandleRequestAsync, _environment, _loggerFactory.CreateLogger(), ServiceProvider.GetTask(), - await GetAuthorizedSecurityIdentitiesAsync(_testHostsInformation.TestHostLauncher, executableInfo.FilePath, cancellationToken).ConfigureAwait(false), + authorizedSecurityIdentities, cancellationToken); testHostControllerIpc.RegisterAllSerializers(); return testHostControllerIpc; } - /// - /// Asks the registered launcher, when it implements - /// , for the security identities that must - /// additionally be authorized on the controller-to-host connection. - /// - /// - /// - /// This runs before the connection is created, which is the only point where its access control can - /// still be composed: it has to be listening before the host is launched, so the launcher cannot - /// contribute this from . - /// - /// - /// Every returned value is validated against the platform's least-privilege policy: only the identity of - /// a single sandboxed application may be authorized, never a user, a group, Everyone, or an - /// identity shared by every sandboxed application on the machine. An extension that asks for anything - /// else fails the run instead of silently getting a weaker connection, so a mistake in an extension - /// cannot degrade into an over-permissive access control list. - /// - /// - private async Task?> GetAuthorizedSecurityIdentitiesAsync( - ITestHostLauncher? testHostLauncher, - string testHostFileName, - CancellationToken cancellationToken) - { - if (testHostLauncher is not ITestHostControllerConnectionAuthorizer connectionAuthorizer) - { - return null; - } - - IReadOnlyList extensionResult = await connectionAuthorizer.GetAuthorizedSecurityIdentitiesAsync(testHostFileName, cancellationToken).ConfigureAwait(false); - if (extensionResult is null || extensionResult.Count == 0) - { - return null; - } - - // Snapshot immediately. Everything below — validation, logging, and ultimately the security - // descriptor — must operate on one fixed set of values; the sequence itself is extension-supplied - // and re-enumerating it is not guaranteed to yield what was validated. - string[] securityIdentities = [.. extensionResult]; - - if (!NamedPipeServerSecurity.IsSupported) - { - // Sandboxed-application identities and connection access control lists are expressible only on - // Windows. Anywhere else the request is meaningless, so it is ignored rather than failing an - // otherwise valid run. - await _logger.LogDebugAsync($"'{testHostLauncher.Uid}' requested {securityIdentities.Length} connection authorization(s), ignored on this operating system.").ConfigureAwait(false); - return null; - } - - foreach (string securityIdentity in securityIdentities.Where(static securityIdentity => - !NamedPipeServerSecurity.IsAuthorizableSandboxedApplicationIdentity(securityIdentity))) - { - throw new InvalidOperationException(string.Format( - CultureInfo.InvariantCulture, - PlatformResources.TestHostControllerConnectionInvalidAuthorizedSecurityIdentityErrorMessage, - testHostLauncher.DisplayName, - testHostLauncher.Uid, - securityIdentity ?? "", - NamedPipeServerSecurity.AllApplicationPackagesSid)); - } - - await _logger.LogDebugAsync($"'{testHostLauncher.Uid}' authorized the following security identity/identities on the test host controller connection: {string.Join(", ", securityIdentities)}").ConfigureAwait(false); - return securityIdentities; - } - private Task HandleRequestAsync(IRequest request) { try diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessConfiguration.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessConfiguration.cs index aef6527a66..070ceb380d 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessConfiguration.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessConfiguration.cs @@ -118,6 +118,13 @@ internal sealed partial class TestHostControllersTestHost await concreteMessageBusService.InitAsync().ConfigureAwait(false); ((MessageBusProxy)ServiceProvider.GetMessageBus()).SetBuiltMessageBus(concreteMessageBusService); + // Create controller-side extension connections before their environment-variable providers publish + // the effective pipe names to the test host. + foreach (ITestHostProcessLifetimeHandler lifetimeHandler in _testHostsInformation.LifetimeHandlers) + { + await lifetimeHandler.BeforeTestHostProcessStartAsync(cancellationToken).ConfigureAwait(false); + } + // Apply the ITestHostEnvironmentVariableProvider if (_testHostsInformation.EnvironmentVariableProviders.Length > 0) { diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessLifecycle.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessLifecycle.cs index d1bfb0a362..7f9b932389 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessLifecycle.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessLifecycle.cs @@ -29,15 +29,6 @@ internal sealed partial class TestHostControllersTestHost Stopwatch consoleRunStarted, CancellationToken applicationCancellationToken) { - // Apply the ITestHostProcessLifetimeHandler.BeforeTestHostProcessStartAsync - if (_testHostsInformation.LifetimeHandlers.Length > 0) - { - foreach (ITestHostProcessLifetimeHandler lifetimeHandler in _testHostsInformation.LifetimeHandlers) - { - await lifetimeHandler.BeforeTestHostProcessStartAsync(applicationCancellationToken).ConfigureAwait(false); - } - } - // Launch the test host process string testHostProcessStartupTime = _clock.UtcNow.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture); processStartInfo.EnvironmentVariables.Add($"{EnvironmentVariableConstants.TESTINGPLATFORM_TESTHOSTCONTROLLER_TESTHOSTPROCESSSTARTTIME}_{currentPid}", testHostProcessStartupTime); diff --git a/src/Platform/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt b/src/Platform/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt index f0eb42eb15..dff484af8c 100644 --- a/src/Platform/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt +++ b/src/Platform/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt @@ -189,6 +189,16 @@ Microsoft.Testing.Platform.Services.IStopPoliciesService.TryExecuteDeadlineStopF Microsoft.Testing.Platform.Services.StopPoliciesService.IsTestExecutionCompleted.get -> bool Microsoft.Testing.Platform.Services.StopPoliciesService.NotifyTestExecutionStarting() -> void Microsoft.Testing.Platform.Services.StopPoliciesService.NotifyTestExecutionCompleted() -> void +Microsoft.Testing.Platform.Services.ServiceProvider.TestHostControllerAuthorizedSecurityIdentities.get -> System.Collections.Generic.IReadOnlyList? +Microsoft.Testing.Platform.Services.ServiceProvider.TestHostControllerAuthorizedSecurityIdentities.set -> void +static Microsoft.Testing.Platform.Services.ServiceProviderExtensions.GetTestHostControllerAuthorizedSecurityIdentities(this System.IServiceProvider! serviceProvider) -> System.Collections.Generic.IReadOnlyList? +static Microsoft.Testing.Platform.Services.ServiceProviderExtensions.ResolveTestHostControllerAuthorizedSecurityIdentitiesAsync(this System.IServiceProvider! serviceProvider, Microsoft.Testing.Platform.Extensions.TestHostControllers.ITestHostLauncher? testHostLauncher, string! testHostFileName, Microsoft.Testing.Platform.Logging.ILogger! logger, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task?>! +Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint +Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint.NamedPipeServerEndpoint(string! pipeName) -> void +Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint.PipeName.get -> string! +Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint.PipeName.set -> void +Microsoft.Testing.Platform.TestHostControllers.TestHostControllersManager.BuildTestHostLauncherAsync(Microsoft.Testing.Platform.Services.ServiceProvider! serviceProvider) -> System.Threading.Tasks.Task! +Microsoft.Testing.Platform.TestHostOrchestrator.ITestHostControllerConnectionAuthorizationConsumer Microsoft.Testing.Platform.Services.StopPoliciesService.IsDeadlineTriggered.get -> bool Microsoft.Testing.Platform.Services.StopPoliciesService.RegisterDeadlineStopFallback(System.Func!>! callback) -> void Microsoft.Testing.Platform.Services.StopPoliciesService.Dispose() -> void diff --git a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs index 3395d98b42..3e07f96615 100644 --- a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs +++ b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProvider.cs @@ -17,6 +17,8 @@ internal sealed class ServiceProvider : IServiceProvider, ICloneable internal IReadOnlyCollection Services => _services; + internal IReadOnlyList? TestHostControllerAuthorizedSecurityIdentities { get; set; } + public bool AllowTestAdapterFrameworkRegistration { get; set; } #pragma warning disable CS0618 // Type or member is obsolete diff --git a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProviderExtensions.cs b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProviderExtensions.cs index da5e5a2e46..bb5935fa20 100644 --- a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProviderExtensions.cs +++ b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProviderExtensions.cs @@ -7,7 +7,9 @@ using Microsoft.Testing.Platform.CommandLine; using Microsoft.Testing.Platform.Configurations; using Microsoft.Testing.Platform.Extensions.TestFramework; +using Microsoft.Testing.Platform.Extensions.TestHostControllers; using Microsoft.Testing.Platform.Helpers; +using Microsoft.Testing.Platform.IPC; using Microsoft.Testing.Platform.Logging; using Microsoft.Testing.Platform.Messages; using Microsoft.Testing.Platform.OutputDevice; @@ -150,6 +152,57 @@ internal static IEnumerable GetServicesInternal(this IServic return ((ServiceProvider)provider).GetServicesInternal(typeof(TService)).Cast(); } + internal static IReadOnlyList? GetTestHostControllerAuthorizedSecurityIdentities(this IServiceProvider serviceProvider) + => ((ServiceProvider)serviceProvider).TestHostControllerAuthorizedSecurityIdentities; + + internal static async Task?> ResolveTestHostControllerAuthorizedSecurityIdentitiesAsync( + this IServiceProvider serviceProvider, + ITestHostLauncher? testHostLauncher, + string testHostFileName, + ILogger logger, + CancellationToken cancellationToken) + { + if (testHostLauncher is not ITestHostControllerConnectionAuthorizer connectionAuthorizer) + { + ((ServiceProvider)serviceProvider).TestHostControllerAuthorizedSecurityIdentities = null; + return null; + } + + IReadOnlyList extensionResult = await connectionAuthorizer.GetAuthorizedSecurityIdentitiesAsync(testHostFileName, cancellationToken).ConfigureAwait(false); + if (extensionResult is null || extensionResult.Count == 0) + { + ((ServiceProvider)serviceProvider).TestHostControllerAuthorizedSecurityIdentities = null; + return null; + } + + // Snapshot immediately. Validation, logging, and every controller-side pipe must operate on + // the same fixed set of extension-supplied values. + string[] securityIdentities = [.. extensionResult]; + + if (!NamedPipeServerSecurity.IsSupported) + { + await logger.LogDebugAsync($"'{testHostLauncher.Uid}' requested {securityIdentities.Length} connection authorization(s), ignored on this operating system.").ConfigureAwait(false); + ((ServiceProvider)serviceProvider).TestHostControllerAuthorizedSecurityIdentities = null; + return null; + } + + foreach (string securityIdentity in securityIdentities.Where(static securityIdentity => + !NamedPipeServerSecurity.IsAuthorizableSandboxedApplicationIdentity(securityIdentity))) + { + throw new InvalidOperationException(string.Format( + CultureInfo.InvariantCulture, + PlatformResources.TestHostControllerConnectionInvalidAuthorizedSecurityIdentityErrorMessage, + testHostLauncher.DisplayName, + testHostLauncher.Uid, + securityIdentity ?? "", + NamedPipeServerSecurity.AllApplicationPackagesSid)); + } + + await logger.LogDebugAsync($"'{testHostLauncher.Uid}' authorized the following security identity/identities on the test host controller connection: {string.Join(", ", securityIdentities)}").ConfigureAwait(false); + ((ServiceProvider)serviceProvider).TestHostControllerAuthorizedSecurityIdentities = securityIdentities; + return securityIdentities; + } + internal static ITestSessionContext GetTestSessionContext(this IServiceProvider serviceProvider) => serviceProvider.GetRequiredServiceInternal(); diff --git a/src/Platform/Microsoft.Testing.Platform/TestHostControllers/NamedPipeServerEndpoint.cs b/src/Platform/Microsoft.Testing.Platform/TestHostControllers/NamedPipeServerEndpoint.cs new file mode 100644 index 0000000000..fd41a0ecb2 --- /dev/null +++ b/src/Platform/Microsoft.Testing.Platform/TestHostControllers/NamedPipeServerEndpoint.cs @@ -0,0 +1,9 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Testing.Platform.TestHostControllers; + +internal sealed class NamedPipeServerEndpoint(string pipeName) +{ + public string PipeName { get; set; } = pipeName; +} diff --git a/src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostControllersManager.cs b/src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostControllersManager.cs index ee484a138f..8915061bd3 100644 --- a/src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostControllersManager.cs +++ b/src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostControllersManager.cs @@ -170,7 +170,7 @@ [.. dataConsumers.OrderBy(x => x.RegistrationOrder).Select(x => (IDataConsumer)x requireProcessRestart); } - private async Task BuildTestHostLauncherAsync(ServiceProvider serviceProvider) + internal async Task BuildTestHostLauncherAsync(ServiceProvider serviceProvider) { List launchers = []; await ExtensionBuilderHelper.BuildAndRegisterExtensionsAsync(_testHostLauncherFactories, serviceProvider, launchers, registerInServiceProvider: true).ConfigureAwait(false); diff --git a/src/Platform/Microsoft.Testing.Platform/TestHostOrchestrator/ITestHostControllerConnectionAuthorizationConsumer.cs b/src/Platform/Microsoft.Testing.Platform/TestHostOrchestrator/ITestHostControllerConnectionAuthorizationConsumer.cs new file mode 100644 index 0000000000..9d16d6e3e4 --- /dev/null +++ b/src/Platform/Microsoft.Testing.Platform/TestHostOrchestrator/ITestHostControllerConnectionAuthorizationConsumer.cs @@ -0,0 +1,6 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Testing.Platform.TestHostOrchestrator; + +internal interface ITestHostControllerConnectionAuthorizationConsumer; diff --git a/src/Platform/SharedExtensionHelpers/PipeNameEnvironmentVariableProviderBase.cs b/src/Platform/SharedExtensionHelpers/PipeNameEnvironmentVariableProviderBase.cs index b04e687832..0d94167b59 100644 --- a/src/Platform/SharedExtensionHelpers/PipeNameEnvironmentVariableProviderBase.cs +++ b/src/Platform/SharedExtensionHelpers/PipeNameEnvironmentVariableProviderBase.cs @@ -3,10 +3,11 @@ using Microsoft.Testing.Platform.Extensions; using Microsoft.Testing.Platform.Extensions.TestHostControllers; +using Microsoft.Testing.Platform.TestHostControllers; namespace Microsoft.Testing.Extensions; -internal abstract class PipeNameEnvironmentVariableProviderBase(string pipeName, string environmentVariableName) : ITestHostEnvironmentVariableProvider +internal abstract class PipeNameEnvironmentVariableProviderBase(NamedPipeServerEndpoint endpoint, string environmentVariableName) : ITestHostEnvironmentVariableProvider { public string Version => ExtensionVersion.DefaultSemVer; @@ -22,7 +23,7 @@ internal abstract class PipeNameEnvironmentVariableProviderBase(string pipeName, /// protected virtual bool ShouldValidatePipeNameValue => true; - protected string PipeName { get; } = pipeName; + protected string PipeName => endpoint.PipeName; protected string EnvironmentVariableName { get; } = environmentVariableName; diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/EnvironmentVariableProviderTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/EnvironmentVariableProviderTests.cs index bb498a85ad..799a3ceb59 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/EnvironmentVariableProviderTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/EnvironmentVariableProviderTests.cs @@ -6,6 +6,7 @@ using Microsoft.Testing.Extensions.UnitTests.Helpers; using Microsoft.Testing.Platform.Extensions; using Microsoft.Testing.Platform.Extensions.TestHostControllers; +using Microsoft.Testing.Platform.TestHostControllers; using Moq; @@ -22,7 +23,9 @@ public async Task HangDumpEnvironmentVariableProvider_UpdateAsync_SetsLockedPipe environmentVariables.Setup(x => x.SetVariable(It.IsAny())) .Callback(variable => setVariable = variable); - var provider = new HangDumpEnvironmentVariableProvider(new TestCommandLineOptions([]), "expected-pipe"); + var endpoint = new NamedPipeServerEndpoint("initial-pipe"); + var provider = new HangDumpEnvironmentVariableProvider(new TestCommandLineOptions([]), endpoint); + endpoint.PipeName = "expected-pipe"; await provider.UpdateAsync(environmentVariables.Object); @@ -47,7 +50,9 @@ public async Task HangDumpEnvironmentVariableProvider_ValidateTestHostEnvironmen .Setup(x => x.TryGetVariable(HangDumpEnvironmentVariableProvider.PipeNameEnvironmentVariableName, out existingVariable)) .Returns(true); - var provider = new HangDumpEnvironmentVariableProvider(new TestCommandLineOptions([]), "expected-pipe"); + var endpoint = new NamedPipeServerEndpoint("initial-pipe"); + var provider = new HangDumpEnvironmentVariableProvider(new TestCommandLineOptions([]), endpoint); + endpoint.PipeName = "expected-pipe"; ValidationResult result = await provider.ValidateTestHostEnvironmentVariablesAsync(readOnlyEnvironmentVariables.Object); @@ -71,7 +76,7 @@ public async Task TrxEnvironmentVariableProvider_ValidateTestHostEnvironmentVari .Setup(x => x.TryGetVariable(TrxEnvironmentVariableProvider.TRXNAMEDPIPENAME, out existingVariable)) .Returns(true); - var provider = new TrxEnvironmentVariableProvider(new TestCommandLineOptions([]), "expected-value"); + var provider = new TrxEnvironmentVariableProvider(new TestCommandLineOptions([]), new NamedPipeServerEndpoint("expected-value")); ValidationResult result = await provider.ValidateTestHostEnvironmentVariablesAsync(readOnlyEnvironmentVariables.Object); diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/HangDumpTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/HangDumpTests.cs index 64ec15a37b..d12779f2fd 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/HangDumpTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/HangDumpTests.cs @@ -5,12 +5,16 @@ using Microsoft.Testing.Extensions.Diagnostics.Helpers; using Microsoft.Testing.Extensions.Diagnostics.Resources; using Microsoft.Testing.Extensions.UnitTests.Helpers; +using Microsoft.Testing.Platform.Configurations; using Microsoft.Testing.Platform.Extensions.CommandLine; using Microsoft.Testing.Platform.Extensions.Messages; using Microsoft.Testing.Platform.Helpers; using Microsoft.Testing.Platform.Logging; +using Microsoft.Testing.Platform.Messages; +using Microsoft.Testing.Platform.OutputDevice; using Microsoft.Testing.Platform.Services; using Microsoft.Testing.Platform.TestHost; +using Microsoft.Testing.Platform.TestHostControllers; using Moq; @@ -19,6 +23,8 @@ namespace Microsoft.Testing.Extensions.UnitTests; [TestClass] public sealed class HangDumpTests { + private const string ContosoPackageSid = "S-1-15-2-1990679259-4123976751-842158434-3026549936-2944832882-252165955-409282942"; + public TestContext TestContext { get; set; } = null!; private HangDumpCommandLineProvider GetProvider() @@ -28,6 +34,44 @@ private HangDumpCommandLineProvider GetProvider() return new(); } + [TestMethod] + [OSCondition(ConditionMode.Include, OperatingSystems.Windows, IgnoreMessage = "AppContainer pipe authorization is Windows-only.")] + public async Task BeforeTestHostProcessStartAsync_UsesControllerAuthorizedSecurityIdentities() + { + var endpoint = new NamedPipeServerEndpoint($"hang_{Guid.NewGuid():N}"); + var options = new TestCommandLineOptions(new Dictionary + { + [HangDumpCommandLineProvider.HangDumpOptionName] = [], + }); + Mock loggerFactory = new(); + loggerFactory.Setup(x => x.CreateLogger(It.IsAny())).Returns(new Mock().Object); + Mock task = new(); + task.Setup(x => x.Run(It.IsAny>(), It.IsAny())).Returns(Task.CompletedTask); + Mock clock = new(); + clock.SetupGet(x => x.UtcNow).Returns(DateTimeOffset.UtcNow); + ServiceProvider serviceProvider = new() + { + TestHostControllerAuthorizedSecurityIdentities = [ContosoPackageSid], + }; + + using var handler = new HangDumpProcessLifetimeHandler( + endpoint, + new Mock().Object, + new Mock().Object, + options, + task.Object, + new Mock().Object, + loggerFactory.Object, + new Mock().Object, + new Mock().Object, + clock.Object, + serviceProvider); + + await handler.BeforeTestHostProcessStartAsync(TestContext.CancellationToken); + + Assert.IsTrue(endpoint.PipeName.StartsWith(@"LOCAL\", StringComparison.OrdinalIgnoreCase)); + } + [TestMethod] public async Task IsValid_If_Timeout_Value_Has_CorrectValue() { diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs index 83393a23b6..e63076eda5 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs @@ -13,6 +13,7 @@ using Microsoft.Testing.Platform.Logging; using Microsoft.Testing.Platform.OutputDevice; using Microsoft.Testing.Platform.Services; +using Microsoft.Testing.Platform.TestHostOrchestrator; using Moq; @@ -23,6 +24,35 @@ namespace Microsoft.Testing.Extensions.UnitTests; [TestClass] public class RetryTests { + private const string ContosoPackageSid = "S-1-15-2-1990679259-4123976751-842158434-3026549936-2944832882-252165955-409282942"; + + [TestMethod] + [OSCondition(ConditionMode.Include, OperatingSystems.Windows, IgnoreMessage = "AppContainer pipe authorization is Windows-only.")] + public void RetryPipeServer_UsesControllerAuthorizedSecurityIdentities() + { + ServiceProvider serviceProvider = new() + { + TestHostControllerAuthorizedSecurityIdentities = [ContosoPackageSid], + }; + serviceProvider.AddService(new Mock().Object); + Mock loggerFactory = new(); + loggerFactory.Setup(x => x.CreateLogger(It.IsAny())).Returns(new Mock().Object); + serviceProvider.AddService(loggerFactory.Object); + serviceProvider.AddService(new SystemTask()); + Mock cancellationTokenSource = new(); + cancellationTokenSource.SetupGet(x => x.CancellationToken).Returns(CancellationToken.None); + serviceProvider.AddService(cancellationTokenSource.Object); + serviceProvider.AddService(new TestCommandLineOptions([])); + serviceProvider.AddService(new Mock().Object); + + var orchestrator = new RetryOrchestrator(serviceProvider); + + using var server = new RetryFailedTestsPipeServer(serviceProvider, [], new Mock().Object); + + Assert.IsInstanceOfType(orchestrator); + Assert.IsTrue(server.PipeName.StartsWith(@"LOCAL\", StringComparison.OrdinalIgnoreCase)); + } + [TestMethod] public void SnapshotAttemptArtifacts_CopiesExternalArtifactAndPreservesDestination() { diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxProcessLifetimeHandlerTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxProcessLifetimeHandlerTests.cs index c983940e3c..fabee60a79 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxProcessLifetimeHandlerTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxProcessLifetimeHandlerTests.cs @@ -15,6 +15,7 @@ using Microsoft.Testing.Platform.Messages; using Microsoft.Testing.Platform.OutputDevice; using Microsoft.Testing.Platform.Services; +using Microsoft.Testing.Platform.TestHostControllers; using Moq; @@ -23,10 +24,46 @@ namespace Microsoft.Testing.Extensions.UnitTests; [TestClass] public sealed class TrxProcessLifetimeHandlerTests { + private const string ContosoPackageSid = "S-1-15-2-1990679259-4123976751-842158434-3026549936-2944832882-252165955-409282942"; + public TestContext TestContext { get; set; } = null!; [TestMethod] public async Task BeforeTestHostProcessStartAsync_CreatesPipeBeforeSchedulingConnectionWait() + { + (TrxProcessLifetimeHandler handler, NamedPipeServerEndpoint endpoint, _, DeferredTask task) = CreateHandler(); + using (handler) + { + await handler.BeforeTestHostProcessStartAsync(TestContext.CancellationToken); + + Assert.IsNotNull(task.DeferredFunction); + using var client = new NamedPipeClientStream(".", endpoint.PipeName, PipeDirection.InOut, PipeOptions.Asynchronous); + await client.ConnectAsync(timeout: 5_000, TestContext.CancellationToken); + Assert.IsTrue(client.IsConnected); + } + } + + [TestMethod] + [OSCondition(ConditionMode.Include, OperatingSystems.Windows, IgnoreMessage = "AppContainer pipe authorization is Windows-only.")] + public async Task BeforeTestHostProcessStartAsync_UsesRuntimeAuthorizationAndPublishesEffectivePipeName() + { + (TrxProcessLifetimeHandler handler, NamedPipeServerEndpoint endpoint, ServiceProvider serviceProvider, DeferredTask task) = CreateHandler(); + using (handler) + { + serviceProvider.TestHostControllerAuthorizedSecurityIdentities = [ContosoPackageSid]; + + await handler.BeforeTestHostProcessStartAsync(TestContext.CancellationToken); + + Assert.IsTrue(endpoint.PipeName.StartsWith(@"LOCAL\", StringComparison.OrdinalIgnoreCase)); + Assert.IsNotNull(task.DeferredFunction); + + using var client = new NamedPipeClientStream(".", endpoint.PipeName, PipeDirection.InOut, PipeOptions.Asynchronous); + await client.ConnectAsync(timeout: 5_000, TestContext.CancellationToken); + Assert.IsTrue(client.IsConnected); + } + } + + private static (TrxProcessLifetimeHandler Handler, NamedPipeServerEndpoint Endpoint, ServiceProvider ServiceProvider, DeferredTask Task) CreateHandler() { var commandLineOptions = new TestCommandLineOptions(new Dictionary { @@ -43,9 +80,12 @@ public async Task BeforeTestHostProcessStartAsync_CreatesPipeBeforeSchedulingCon Type namedPipeServerType = trxAssembly.GetType("Microsoft.Testing.Platform.IPC.NamedPipeServer", throwOnError: true)!; MethodInfo getPipeName = namedPipeServerType.GetMethod("GetPipeName", [typeof(string)])!; object pipeName = getPipeName.Invoke(null, [Guid.NewGuid().ToString("N")])!; + string pipeNameValue = (string)pipeName.GetType().GetProperty("Name")!.GetValue(pipeName)!; + var endpoint = new NamedPipeServerEndpoint(pipeNameValue); + ServiceProvider serviceProvider = new(); ConstructorInfo constructor = typeof(TrxProcessLifetimeHandler).GetConstructors().Single(); - using var handler = (TrxProcessLifetimeHandler)constructor.Invoke([ + var handler = (TrxProcessLifetimeHandler)constructor.Invoke([ commandLineOptions, environment.Object, loggerFactory.Object, @@ -56,16 +96,11 @@ public async Task BeforeTestHostProcessStartAsync_CreatesPipeBeforeSchedulingCon clock.Object, task, new Mock().Object, - pipeName, + serviceProvider, + endpoint, ]); - await handler.BeforeTestHostProcessStartAsync(TestContext.CancellationToken); - - Assert.IsNotNull(task.DeferredFunction); - string pipeNameValue = (string)pipeName.GetType().GetProperty("Name")!.GetValue(pipeName)!; - using var client = new NamedPipeClientStream(".", pipeNameValue, PipeDirection.InOut, PipeOptions.Asynchronous); - await client.ConnectAsync(timeout: 5_000, TestContext.CancellationToken); - Assert.IsTrue(client.IsConnected); + return (handler, endpoint, serviceProvider, task); } private sealed class DeferredTask : ITask diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestApplicationBuilderTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestApplicationBuilderTests.cs index 1a1a87bd46..55e9f8992f 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestApplicationBuilderTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestApplicationBuilderTests.cs @@ -22,6 +22,8 @@ namespace Microsoft.Testing.Platform.UnitTests; [UnsupportedOSPlatform("browser")] public sealed class TestApplicationBuilderTests { + private const string ContosoPackageSid = "S-1-15-2-1990679259-4123976751-842158434-3026549936-2944832882-252165955-409282942"; + private readonly ServiceProvider _serviceProvider = new(); public TestApplicationBuilderTests() @@ -373,6 +375,26 @@ public async Task TestHostLauncher_WhenRegistered_ForcesProcessRestartAndIsStore Assert.AreEqual((object)launcher, configuration.TestHostLauncher); } + [TestMethod] + [OSCondition(ConditionMode.Include, OperatingSystems.Windows, IgnoreMessage = "AppContainer pipe authorization is Windows-only.")] + public async Task TestHostLauncher_AuthorizationConsumerResolvesAndStoresIdentity() + { + TestHostControllersManager testHostControllerManager = new(); + TestHostLauncher launcher = new("launcher"); + testHostControllerManager.AddTestHostLauncher(_ => launcher); + + ITestHostLauncher? builtLauncher = await testHostControllerManager.BuildTestHostLauncherAsync(_serviceProvider); + IReadOnlyList? authorizedSecurityIdentities = + await _serviceProvider.ResolveTestHostControllerAuthorizedSecurityIdentitiesAsync( + builtLauncher, + "testhost.exe", + new NopLogger(), + CancellationToken.None); + + Assert.AreSequenceEqual([ContosoPackageSid], authorizedSecurityIdentities); + Assert.AreSame(authorizedSecurityIdentities, _serviceProvider.TestHostControllerAuthorizedSecurityIdentities); + } + [TestMethod] public async Task TestHostLauncher_MultipleRegistered_ShouldFail() { @@ -560,7 +582,7 @@ private sealed class TestHostProcessLifetimeHandler : ITestHostProcessLifetimeHa } #pragma warning disable TPEXP // Type is for evaluation purposes only and is subject to change or removal in future updates. - private sealed class TestHostLauncher : ITestHostLauncher + private sealed class TestHostLauncher : ITestHostLauncher, ITestHostControllerConnectionAuthorizer { public TestHostLauncher(string id) => Uid = id; @@ -575,6 +597,9 @@ private sealed class TestHostLauncher : ITestHostLauncher public Task IsEnabledAsync() => Task.FromResult(true); public Task LaunchTestHostAsync(TestHostLaunchContext context, CancellationToken cancellationToken) => throw new NotImplementedException(); + + public Task> GetAuthorizedSecurityIdentitiesAsync(string testHostFileName, CancellationToken cancellationToken) + => Task.FromResult>([ContosoPackageSid]); } #pragma warning restore TPEXP From cb97bf16a5ca655837bdcbfd40c8177e13a8083c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 15:46:34 +0200 Subject: [PATCH 02/14] Launch retry attempts through custom launcher Delegate retry child processes through the registered test host launcher and cover the packaged-app orchestration path with unit and acceptance tests. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../RetryTestHostRunner.cs | 33 ++++++- .../Helpers/AcceptanceTestBase.cs | 3 + .../PackagedAppDeploymentTests.cs | 30 ++++++- .../RetryTests.cs | 87 +++++++++++++++++++ 4 files changed, 150 insertions(+), 3 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs index b6f2519597..749504a3f9 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs @@ -2,7 +2,9 @@ // Licensed under dual-license. See LICENSE.PLATFORMTOOLS.txt file in the project root for full license information. using Microsoft.Testing.Extensions.Policy.Resources; +using Microsoft.Testing.Platform; using Microsoft.Testing.Platform.Extensions.OutputDevice; +using Microsoft.Testing.Platform.Extensions.TestHostControllers; using Microsoft.Testing.Platform.Helpers; using Microsoft.Testing.Platform.Logging; using Microsoft.Testing.Platform.OutputDevice; @@ -69,8 +71,11 @@ public static async Task RunAttemptAsync( attemptCount.ToString(CultureInfo.InvariantCulture); await logger.LogDebugAsync($"Starting test host process, attempt {attemptCount}/{userMaxRetryCount}").ConfigureAwait(false); - using IProcess testHostProcess = serviceProvider.GetProcessHandler().Start(processStartInfo) - ?? throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, ExtensionResources.RetryFailedTestsCannotStartProcessErrorMessage, processStartInfo.FileName)); + ITestHostLauncher? testHostLauncher = serviceProvider.GetServiceInternal(); + using IProcess testHostProcess = testHostLauncher is null + ? serviceProvider.GetProcessHandler().Start(processStartInfo) + ?? throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, ExtensionResources.RetryFailedTestsCannotStartProcessErrorMessage, processStartInfo.FileName)) + : await LaunchUsingCustomLauncherAsync(testHostLauncher, processStartInfo, finalArguments, logger, cancellationToken).ConfigureAwait(false); using var processExitedCancellationToken = new CancellationTokenSource(); EventHandler exitedHandler = (sender, e) => @@ -127,4 +132,28 @@ public static async Task RunAttemptAsync( return new AttemptResult { ExitCode = testHostProcess.ExitCode, ExitedBeforeConnect = false }; } + + private static async Task LaunchUsingCustomLauncherAsync( + ITestHostLauncher testHostLauncher, + ProcessStartInfo processStartInfo, + IReadOnlyList arguments, + ILogger logger, + CancellationToken cancellationToken) + { +#pragma warning disable IDE0028 // Collection initialization can be simplified — populated from a runtime loop. + Dictionary environmentVariables = new(StringComparer.Ordinal); +#pragma warning restore IDE0028 + foreach (string key in processStartInfo.EnvironmentVariables.Keys) + { + environmentVariables[key] = processStartInfo.EnvironmentVariables[key]; + } + + string? workingDirectory = RoslynString.IsNullOrEmpty(processStartInfo.WorkingDirectory) ? null : processStartInfo.WorkingDirectory; + TestHostLaunchContext context = new(processStartInfo.FileName, arguments, environmentVariables, workingDirectory); + + await logger.LogDebugAsync($"Delegating retry test host launch to '{testHostLauncher.DisplayName}' (UID: {testHostLauncher.Uid})").ConfigureAwait(false); + ITestHostHandle handle = await testHostLauncher.LaunchTestHostAsync(context, cancellationToken).ConfigureAwait(false); + await logger.LogDebugAsync($"Retry test host launched by '{testHostLauncher.Uid}' (Identifier: '{handle.Identifier ?? ""}')").ConfigureAwait(false); + return new TestHostHandleToProcessAdapter(handle); + } } diff --git a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/Helpers/AcceptanceTestBase.cs b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/Helpers/AcceptanceTestBase.cs index 1313cbb024..d6002b7f1b 100644 --- a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/Helpers/AcceptanceTestBase.cs +++ b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/Helpers/AcceptanceTestBase.cs @@ -34,6 +34,7 @@ static AcceptanceTestBase() MicrosoftTestingExtensionsJUnitReportVersion = ExtractVersionFromPackage(Constants.ArtifactsPackagesShipping, "Microsoft.Testing.Extensions.JUnitReport."); MicrosoftTestingExtensionsGitHubActionsReportVersion = ExtractVersionFromPackage(Constants.ArtifactsPackagesShipping, "Microsoft.Testing.Extensions.GitHubActionsReport."); MicrosoftTestingExtensionsPackagedAppVersion = ExtractVersionFromPackage(Constants.ArtifactsPackagesShipping, "Microsoft.Testing.Extensions.PackagedApp."); + MicrosoftTestingExtensionsRetryVersion = ExtractVersionFromPackage(Constants.ArtifactsPackagesShipping, "Microsoft.Testing.Extensions.Retry."); MicrosoftTestingExtensionsVideoRecorderVersion = ExtractVersionFromPackage(Constants.ArtifactsPackagesShipping, "Microsoft.Testing.Extensions.VideoRecorder."); MicrosoftTestingExtensionsAzureFoundryVersion = ExtractVersionFromPackage(Constants.ArtifactsPackagesNonShipping, "Microsoft.Testing.Extensions.AzureFoundry."); } @@ -109,6 +110,8 @@ public static bool IsWindowsApplicationModelTestEnvironment public static string MicrosoftTestingExtensionsPackagedAppVersion { get; private set; } + public static string MicrosoftTestingExtensionsRetryVersion { get; private set; } + public static string MicrosoftTestingExtensionsVideoRecorderVersion { get; private set; } public static string MicrosoftTestingExtensionsAzureFoundryVersion { get; private set; } diff --git a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/PackagedAppDeploymentTests.cs b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/PackagedAppDeploymentTests.cs index 20770fdd36..af110d8f58 100644 --- a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/PackagedAppDeploymentTests.cs +++ b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/PackagedAppDeploymentTests.cs @@ -54,6 +54,31 @@ public async Task PackagedAppDeployment_DeploysAndLaunchesTestHost_WithoutLocalP "The test host must have been deployed to and launched from a different directory."); } + [DynamicData(nameof(TargetFrameworks.NetForDynamicData), typeof(TargetFrameworks))] + [TestMethod] + [OSCondition(ConditionMode.Include, OperatingSystems.Windows, IgnoreMessage = "Packaged Windows apps (UWP/WinUI) are a Windows-only scenario.")] + public async Task PackagedAppDeployment_WithRetry_UsesLauncherForAttempt(string currentTfm) + { + var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, currentTfm); + string markerPath = Path.Combine(testHost.DirectoryName, "retry-deployment-basedir.txt"); + + TestHostResult testHostResult = await testHost.ExecuteAsync( + "--retry-failed-tests 1", + environmentVariables: new Dictionary + { + ["PACKAGEDAPP_BASEDIR_MARKER"] = markerPath, + [LauncherModeEnvironmentVariable] = "always", + }, + cancellationToken: TestContext.CancellationToken); + + testHostResult.AssertExitCodeIs(ExitCode.Success); + Assert.IsTrue(File.Exists(markerPath), $"Expected the retried host to write its base directory to '{markerPath}'."); + Assert.AreNotEqual( + NormalizeDirectory(testHost.DirectoryName), + ReadMarkedDirectory(markerPath), + "The retry orchestrator must delegate the attempt to the packaged-app launcher."); + } + [DynamicData(nameof(TargetFrameworks.NetForDynamicData), typeof(TargetFrameworks))] [TestMethod] [OSCondition(ConditionMode.Include, OperatingSystems.Windows, IgnoreMessage = "The launcher is only ever enabled on Windows, so there is nothing to opt out of elsewhere.")] @@ -101,6 +126,7 @@ public sealed class TestAssetFixture() : TestAssetFixtureBase() + @@ -134,6 +160,7 @@ public static async Task Main(string[] args) var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args); testApplicationBuilder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_,__) => new DummyTestFramework()); testApplicationBuilder.AddPackagedAppDeployment(); + testApplicationBuilder.AddRetryProvider(); using ITestApplication app = await testApplicationBuilder.BuildAsync(); return await app.RunAsync(); } @@ -179,7 +206,8 @@ public override (string ID, string Name, string Code) GetAssetsToGenerate() => ( Sources .PatchTargetFrameworks(TargetFrameworks.Net) .PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion) - .PatchCodeWithReplace("$MicrosoftTestingExtensionsPackagedAppVersion$", MicrosoftTestingExtensionsPackagedAppVersion)); + .PatchCodeWithReplace("$MicrosoftTestingExtensionsPackagedAppVersion$", MicrosoftTestingExtensionsPackagedAppVersion) + .PatchCodeWithReplace("$MicrosoftTestingExtensionsRetryVersion$", MicrosoftTestingExtensionsRetryVersion)); } public TestContext TestContext { get; set; } diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs index e63076eda5..6f554cadc0 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs @@ -3,12 +3,15 @@ // Licensed under dual-license. See LICENSE.PLATFORMTOOLS.txt file in the project root for full license information. #pragma warning restore IDE0073 // The file header does not match the required text +using System.IO.Pipes; + using Microsoft.Testing.Extensions.Policy; using Microsoft.Testing.Extensions.UnitTests.Helpers; using Microsoft.Testing.Platform.Extensions.ArtifactPostProcessing; using Microsoft.Testing.Platform.Extensions.CommandLine; using Microsoft.Testing.Platform.Extensions.OutputDevice; using Microsoft.Testing.Platform.Extensions.RetryFailedTests.Serializers; +using Microsoft.Testing.Platform.Extensions.TestHostControllers; using Microsoft.Testing.Platform.Helpers; using Microsoft.Testing.Platform.Logging; using Microsoft.Testing.Platform.OutputDevice; @@ -53,6 +56,49 @@ public void RetryPipeServer_UsesControllerAuthorizedSecurityIdentities() Assert.IsTrue(server.PipeName.StartsWith(@"LOCAL\", StringComparison.OrdinalIgnoreCase)); } + [TestMethod] + public async Task RunAttemptAsync_UsesRegisteredTestHostLauncher() + { + ServiceProvider serviceProvider = new(); + serviceProvider.AddService(new Mock().Object); + Mock loggerFactory = new(); + loggerFactory.Setup(x => x.CreateLogger(It.IsAny())).Returns(new Mock().Object); + serviceProvider.AddService(loggerFactory.Object); + serviceProvider.AddService(new SystemTask()); + Mock cancellationTokenSource = new(); + cancellationTokenSource.SetupGet(x => x.CancellationToken).Returns(CancellationToken.None); + serviceProvider.AddService(cancellationTokenSource.Object); + Mock processHandler = new(MockBehavior.Strict); + serviceProvider.AddService(processHandler.Object); + var launcher = new ConnectingTestHostLauncher(); + serviceProvider.AddService(launcher); + + using var server = new RetryFailedTestsPipeServer(serviceProvider, [], new Mock().Object); + List arguments = + [ + $"--{RetryCommandLineOptionsProvider.RetryFailedTestsPipeNameOptionName}", + server.PipeName, + ]; + + RetryTestHostRunner.AttemptResult result = await RetryTestHostRunner.RunAttemptAsync( + serviceProvider, + Mock.Of(), + Mock.Of(), + Mock.Of(), + server, + new ExecutableInfo("testhost.exe", [], string.Empty), + arguments, + attemptCount: 1, + userMaxRetryCount: 2, + CancellationToken.None); + + Assert.AreEqual(0, result.ExitCode); + Assert.IsFalse(result.ExitedBeforeConnect); + Assert.AreEqual("testhost.exe", launcher.Context!.FileName); + Assert.AreSequenceEqual(arguments, launcher.Context.Arguments); + processHandler.Verify(x => x.Start(It.IsAny()), Times.Never); + } + [TestMethod] public void SnapshotAttemptArtifacts_CopiesExternalArtifactAndPreservesDestination() { @@ -925,4 +971,45 @@ public TestArtifactPostProcessor( return _processAsync(inputs, outputDirectory, context, cancellationToken); } } + + private sealed class ConnectingTestHostLauncher : ITestHostLauncher + { + public TestHostLaunchContext? Context { get; private set; } + + public string Uid => nameof(ConnectingTestHostLauncher); + + public string Version => "1.0.0"; + + public string DisplayName => nameof(ConnectingTestHostLauncher); + + public string Description => nameof(ConnectingTestHostLauncher); + + public Task IsEnabledAsync() => Task.FromResult(true); + + public async Task LaunchTestHostAsync(TestHostLaunchContext context, CancellationToken cancellationToken) + { + Context = context; + int pipeNameIndex = context.Arguments.ToList().IndexOf($"--{RetryCommandLineOptionsProvider.RetryFailedTestsPipeNameOptionName}") + 1; + var pipeClient = new NamedPipeClientStream(".", context.Arguments[pipeNameIndex], PipeDirection.InOut, PipeOptions.Asynchronous); + await pipeClient.ConnectAsync(5_000, cancellationToken); + return new ConnectedTestHostHandle(pipeClient); + } + } + + private sealed class ConnectedTestHostHandle(NamedPipeClientStream pipeClient) : ITestHostHandle + { + public string Identifier => nameof(ConnectedTestHostHandle); + + public int ExitCode => 0; + + public bool HasExited => true; + + public Task WaitForExitAsync(CancellationToken cancellationToken) => Task.CompletedTask; + + public void Terminate() + { + } + + public void Dispose() => pipeClient.Dispose(); + } } From 25c359ce3b3476d79cafdb1a4d0443c66922aafe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 16:56:52 +0200 Subject: [PATCH 03/14] Preserve retry metadata across package activation Use retry pipe identities for packaged activation handoffs and carry the attempt marker while keeping unrelated environment values excluded. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../InternalAPI/InternalAPI.Unshipped.txt | 2 + .../PackagedAppConnectBackHandshake.cs | 28 ++++++++++++- .../PackagedAppConnectBackReader.cs | 12 +++--- .../PackagedAppTestHostLauncher.cs | 41 ++++++++++--------- .../PackagedAppConnectBackHandshakeTests.cs | 36 ++++++++++++++++ 5 files changed, 93 insertions(+), 26 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/InternalAPI/InternalAPI.Unshipped.txt b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/InternalAPI/InternalAPI.Unshipped.txt index 5bdbcee614..0c5ec80509 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/InternalAPI/InternalAPI.Unshipped.txt +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/InternalAPI/InternalAPI.Unshipped.txt @@ -37,6 +37,7 @@ static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake. static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.GetHandshakeFilePath(string! packageFamilyName, string! testHostControllerPid) -> string! static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.ReadAndDelete(string! filePath) -> System.Collections.Generic.IReadOnlyDictionary? static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.TryDelete(string! filePath) -> void +static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.TryGetHandshakeId(System.Collections.Generic.IReadOnlyList! arguments) -> string? static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.TryGetTestHostControllerPid(System.Collections.Generic.IReadOnlyList! arguments) -> string? static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.Write(string! filePath, System.Collections.Generic.IEnumerable>! entries) -> void static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackReader.TryApplyConnectBackEnvironment(System.Collections.Generic.IReadOnlyList! commandLineArguments) -> void @@ -47,6 +48,7 @@ static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackReader.Try Microsoft.Testing.Extensions.PackagedApp.PackagedAppTestHostLauncher.PackagedAppTestHostLauncher(string! testApplicationDirectory, System.Func! getEnvironmentVariable) -> void const Microsoft.Testing.Extensions.PackagedApp.PackagedAppTestHostLauncher.LauncherModeEnvironmentVariable = "TESTINGPLATFORM_PACKAGEDAPP_LAUNCHER" -> string! static Microsoft.Testing.Extensions.PackagedApp.PackagedAppTestHostLauncher.CreateActivationArguments(Microsoft.Testing.Extensions.PackagedApp.AppxApplicationInfo! application, System.Collections.Generic.IReadOnlyList! arguments, string! localStateDirectory) -> Microsoft.Testing.Extensions.PackagedApp.PackagedAppActivationData! +static Microsoft.Testing.Extensions.PackagedApp.PackagedAppTestHostLauncher.GetConnectBackEnvironment(Microsoft.Testing.Platform.Extensions.TestHostControllers.TestHostLaunchContext! context) -> System.Collections.Generic.IEnumerable>! Microsoft.Testing.Extensions.PackagedApp.AppContainerSecurityIdentifier static Microsoft.Testing.Extensions.PackagedApp.AppContainerSecurityIdentifier.TryDerive(string? packageFamilyName) -> string? Microsoft.Testing.Extensions.PackagedApp.PackagedAppTestHostLauncher.GetAuthorizedSecurityIdentitiesAsync(string! testHostFileName, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!>! diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackHandshake.cs b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackHandshake.cs index 9d9be236d1..78ae192eab 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackHandshake.cs +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackHandshake.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Security.Cryptography; + namespace Microsoft.Testing.Extensions.PackagedApp; /// @@ -33,6 +35,7 @@ internal static class PackagedAppConnectBackHandshake // because PlatformCommandLineProvider.TestHostControllerPIDOptionKey is internal to the platform // assembly; the two ship and version together in this repo. private const string TestHostControllerPidOptionKey = "internal-testhostcontroller-pid"; + private const string RetryPipeNameOptionKey = "internal-retry-pipename"; // Marker prefixes distinguishing a null value from a (possibly empty) string value on each line, // so an empty string round-trips as an empty string rather than as null. @@ -77,10 +80,33 @@ public static string GetHandshakeFilePath(string packageFamilyName, string testH /// test host waiting on a controller connect-back). /// public static string? TryGetTestHostControllerPid(IReadOnlyList arguments) + => TryGetOptionValue(arguments, TestHostControllerPidOptionKey); + + /// + /// Returns the stable identifier used to exchange environment variables for an activated host. + /// Controller-host runs use the controller PID; retry-orchestrated runs use a hash of their unique pipe name. + /// + public static string? TryGetHandshakeId(IReadOnlyList arguments) + { + if (TryGetTestHostControllerPid(arguments) is { } testHostControllerPid) + { + return testHostControllerPid; + } + + if (TryGetOptionValue(arguments, RetryPipeNameOptionKey) is not { } retryPipeName) + { + return null; + } + + byte[] hash = SHA256.HashData(Encoding.UTF8.GetBytes(retryPipeName)); + return $"retry-{Convert.ToHexString(hash)}"; + } + + private static string? TryGetOptionValue(IReadOnlyList arguments, string optionKey) { for (int i = 0; i < arguments.Count - 1; i++) { - if (string.Equals(arguments[i], $"--{TestHostControllerPidOptionKey}", StringComparison.Ordinal)) + if (string.Equals(arguments[i], $"--{optionKey}", StringComparison.Ordinal)) { return arguments[i + 1]; } diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackReader.cs b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackReader.cs index d764eca8d3..5641498500 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackReader.cs +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackReader.cs @@ -63,10 +63,10 @@ public static void TryApplyConnectBackEnvironment( Func getPackageLocalStateDirectory, Action setEnvironmentVariable) { - // Only the spawned test host leg carries the test host controller PID option; the controller - // process does not, so it never consumes a handshake. - string? testHostControllerPid = PackagedAppConnectBackHandshake.TryGetTestHostControllerPid(commandLineArguments); - if (testHostControllerPid is null) + // Controller-host and retry-orchestrated children both carry a unique handshake identifier in + // their command line. The parent process carries neither and never consumes a handshake. + string? handshakeId = PackagedAppConnectBackHandshake.TryGetHandshakeId(commandLineArguments); + if (handshakeId is null) { return; } @@ -95,11 +95,11 @@ public static void TryApplyConnectBackEnvironment( // package-aware ApplicationData.Current.LocalFolder is the correct, unredirected // %LOCALAPPDATA%\Packages\{PFN}\LocalState the (unpackaged) launcher wrote to; // Environment.SpecialFolder.LocalApplicationData is redirected here and would not find the file. - string fileName = PackagedAppConnectBackHandshake.GetHandshakeFileName(testHostControllerPid); + string fileName = PackagedAppConnectBackHandshake.GetHandshakeFileName(handshakeId); string? localStateDirectory = getPackageLocalStateDirectory(); string handshakePath = localStateDirectory is not null ? Path.Combine(localStateDirectory, fileName) - : PackagedAppConnectBackHandshake.GetHandshakeFilePath(packageFamilyName, testHostControllerPid); + : PackagedAppConnectBackHandshake.GetHandshakeFilePath(packageFamilyName, handshakeId); IReadOnlyDictionary? environment = PackagedAppConnectBackHandshake.ReadAndDelete(handshakePath); if (environment is null) diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs index f9971c3368..059e1a9085 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs @@ -85,14 +85,13 @@ internal sealed class PackagedAppTestHostLauncher : ITestHostLauncher, ITestHost /// internal const string PipeAuthorizationModeEnvironmentVariable = "TESTINGPLATFORM_PACKAGEDAPP_PIPEAUTHORIZATION"; -#if PACKAGEDAPP_WINRT // The prefix of the environment variables the platform uses for the controller-to-host connect-back // (pipe name, correlation id, parent PID, start time, skip-extension flag). Only these are handed off // to the activated packaged host: they are exactly what the host needs to reach its controller, they // contain no user-provided data (unlike broader TESTINGPLATFORM_* variables such as inline // runsettings, which can carry secrets), and they are read after this extension's builder hook runs. private const string ConnectBackEnvironmentVariablePrefix = "TESTINGPLATFORM_TESTHOSTCONTROLLER_"; -#endif + private const string RetryAttemptEnvironmentVariableName = "TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"; private readonly string _testApplicationDirectory; private readonly Func _getEnvironmentVariable; @@ -320,15 +319,17 @@ private static async Task LaunchPackagedAsync(TestHostLaunchCon // builder hook runs cannot be reproduced from here anyway. The activated host applies them // in-process before the platform's environment-variable-based connect-back runs (see // PackagedAppConnectBackReader). - string? testHostControllerPid = PackagedAppConnectBackHandshake.TryGetTestHostControllerPid(context.Arguments); + string? handshakeId = PackagedAppConnectBackHandshake.TryGetHandshakeId(context.Arguments); string? handshakePath = null; string? activationPayloadPath = null; try { - if (testHostControllerPid is not null) + if (handshakeId is not null) { - handshakePath = PackagedAppConnectBackHandshake.GetHandshakeFilePath(manifestInfo.PackageFamilyName, testHostControllerPid); - PackagedAppConnectBackHandshake.Write(handshakePath, GetConnectBackEnvironment(context)); + handshakePath = PackagedAppConnectBackHandshake.GetHandshakeFilePath(manifestInfo.PackageFamilyName, handshakeId); + PackagedAppConnectBackHandshake.Write( + handshakePath, + GetConnectBackEnvironment(context).Append(new(LauncherModeEnvironmentVariable, NeverMode))); } PackagedAppActivationData activationData = CreateActivationArguments( @@ -361,19 +362,6 @@ private static async Task LaunchPackagedAsync(TestHostLaunchCon } } - // Selects the controller-to-host connect-back variables (see ConnectBackEnvironmentVariablePrefix) - // from the platform-prepared environment. These are the variables an AUMID-activated host needs to - // reach its controller and that it would not otherwise inherit. - private static IEnumerable> GetConnectBackEnvironment(TestHostLaunchContext context) - { - foreach (KeyValuePair environmentVariable in context.EnvironmentVariables) - { - if (environmentVariable.Key.StartsWith(ConnectBackEnvironmentVariablePrefix, StringComparison.Ordinal)) - { - yield return environmentVariable; - } - } - } #else private static Task LaunchPackagedAsync(TestHostLaunchContext context, string manifestPath, CancellationToken cancellationToken) { @@ -394,6 +382,21 @@ private static Task LaunchPackagedAsync(TestHostLaunchContext c } #endif + // Selects the controller connect-back variables and the retry attempt marker from the + // platform-prepared environment. These are the values an AUMID-activated host needs and would not + // otherwise inherit; unrelated TESTINGPLATFORM_* values can contain user data and remain excluded. + internal static IEnumerable> GetConnectBackEnvironment(TestHostLaunchContext context) + { + foreach (KeyValuePair environmentVariable in context.EnvironmentVariables) + { + if (environmentVariable.Key.StartsWith(ConnectBackEnvironmentVariablePrefix, StringComparison.Ordinal) + || environmentVariable.Key == RetryAttemptEnvironmentVariableName) + { + yield return environmentVariable; + } + } + } + private static ITestHostHandle LaunchLooseLayout(TestHostLaunchContext context, string sourceDirectory, CancellationToken cancellationToken) { // 1. Copy the app's loose layout into an isolated directory. diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs index 3054bd5848..06328f2028 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs @@ -5,6 +5,7 @@ #if !NETFRAMEWORK using Microsoft.Testing.Extensions.PackagedApp; +using Microsoft.Testing.Platform.Extensions.TestHostControllers; namespace Microsoft.Testing.Extensions.UnitTests; @@ -123,6 +124,41 @@ public void TryGetTestHostControllerPid_ReturnsNull_WhenOptionIsLastWithoutValue Assert.IsNull(PackagedAppConnectBackHandshake.TryGetTestHostControllerPid(arguments)); } + + [TestMethod] + public void TryGetHandshakeId_UsesRetryPipeName_WhenControllerPidIsAbsent() + { + string[] arguments = ["--internal-retry-pipename", @"LOCAL\testingplatform.pipe.retry"]; + + string? handshakeId = PackagedAppConnectBackHandshake.TryGetHandshakeId(arguments); + + Assert.IsNotNull(handshakeId); + Assert.StartsWith("retry-", handshakeId); + Assert.AreEqual(handshakeId, PackagedAppConnectBackHandshake.TryGetHandshakeId(arguments)); + Assert.DoesNotContain(@"\", handshakeId); + } + + [TestMethod] + public void GetConnectBackEnvironment_IncludesRetryAttemptAndExcludesUnrelatedValues() + { + var context = new TestHostLaunchContext( + "testhost.exe", + [], + new Dictionary + { + ["TESTINGPLATFORM_TESTHOSTCONTROLLER_PIPENAME_1234"] = "controller-pipe", + ["TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"] = "2", + ["TESTINGPLATFORM_TESTCONFIGURATION"] = "secret", + }, + workingDirectory: null); + + var environment = PackagedAppTestHostLauncher.GetConnectBackEnvironment(context).ToDictionary(); + + Assert.HasCount(2, environment); + Assert.AreEqual("controller-pipe", environment["TESTINGPLATFORM_TESTHOSTCONTROLLER_PIPENAME_1234"]); + Assert.AreEqual("2", environment["TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"]); + Assert.IsFalse(environment.ContainsKey("TESTINGPLATFORM_TESTCONFIGURATION")); + } } #endif From 19cc5d97fd74d908be915ce2c527f99b56ce69a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 17:27:14 +0200 Subject: [PATCH 04/14] Complete packaged retry environment handoff Allow TRX and HangDump endpoints across AUMID activation and disable recursive packaged-launcher activation for loose retry children. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../PackagedAppTestHostLauncher.cs | 10 +++++++++- .../PackagedAppDeploymentTests.cs | 13 +++++++++++++ .../PackagedAppConnectBackHandshakeTests.cs | 6 +++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs index 059e1a9085..5788d6ba0c 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs @@ -91,7 +91,9 @@ internal sealed class PackagedAppTestHostLauncher : ITestHostLauncher, ITestHost // contain no user-provided data (unlike broader TESTINGPLATFORM_* variables such as inline // runsettings, which can carry secrets), and they are read after this extension's builder hook runs. private const string ConnectBackEnvironmentVariablePrefix = "TESTINGPLATFORM_TESTHOSTCONTROLLER_"; + private const string HangDumpPipeEnvironmentVariableName = "TESTINGPLATFORM_HANGDUMP_PIPENAME"; private const string RetryAttemptEnvironmentVariableName = "TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"; + private const string TrxPipeEnvironmentVariableName = "TRXNAMEDPIPENAME"; private readonly string _testApplicationDirectory; private readonly Func _getEnvironmentVariable; @@ -390,7 +392,9 @@ private static Task LaunchPackagedAsync(TestHostLaunchContext c foreach (KeyValuePair environmentVariable in context.EnvironmentVariables) { if (environmentVariable.Key.StartsWith(ConnectBackEnvironmentVariablePrefix, StringComparison.Ordinal) - || environmentVariable.Key == RetryAttemptEnvironmentVariableName) + || environmentVariable.Key is RetryAttemptEnvironmentVariableName + or TrxPipeEnvironmentVariableName + or HangDumpPipeEnvironmentVariableName) { yield return environmentVariable; } @@ -423,6 +427,10 @@ private static ITestHostHandle LaunchLooseLayout(TestHostLaunchContext context, startInfo.Environment[environmentVariable.Key] = environmentVariable.Value; } + // The deployed child is the retry attempt itself. Prevent it from enabling this launcher again and + // recursively creating another controller/deployment layer when the parent was opted in with "always". + startInfo.Environment[LauncherModeEnvironmentVariable] = NeverMode; + Process process = Process.Start(startInfo) ?? throw new InvalidOperationException($"Failed to start deployed packaged-app test host '{deployedFileName}'."); diff --git a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/PackagedAppDeploymentTests.cs b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/PackagedAppDeploymentTests.cs index af110d8f58..814391a434 100644 --- a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/PackagedAppDeploymentTests.cs +++ b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/PackagedAppDeploymentTests.cs @@ -61,12 +61,14 @@ public async Task PackagedAppDeployment_WithRetry_UsesLauncherForAttempt(string { var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, currentTfm); string markerPath = Path.Combine(testHost.DirectoryName, "retry-deployment-basedir.txt"); + string launcherModeMarkerPath = Path.Combine(testHost.DirectoryName, "retry-launcher-mode.txt"); TestHostResult testHostResult = await testHost.ExecuteAsync( "--retry-failed-tests 1", environmentVariables: new Dictionary { ["PACKAGEDAPP_BASEDIR_MARKER"] = markerPath, + ["PACKAGEDAPP_LAUNCHER_MODE_MARKER"] = launcherModeMarkerPath, [LauncherModeEnvironmentVariable] = "always", }, cancellationToken: TestContext.CancellationToken); @@ -77,6 +79,10 @@ public async Task PackagedAppDeployment_WithRetry_UsesLauncherForAttempt(string NormalizeDirectory(testHost.DirectoryName), ReadMarkedDirectory(markerPath), "The retry orchestrator must delegate the attempt to the packaged-app launcher."); + Assert.AreEqual( + "never", + File.ReadAllText(launcherModeMarkerPath), + "The deployed retry child must not recursively enable the packaged-app launcher."); } [DynamicData(nameof(TargetFrameworks.NetForDynamicData), typeof(TargetFrameworks))] @@ -156,6 +162,13 @@ public static async Task Main(string[] args) { System.IO.File.WriteAllText(markerPath, AppContext.BaseDirectory); } + string? launcherModeMarkerPath = Environment.GetEnvironmentVariable("PACKAGEDAPP_LAUNCHER_MODE_MARKER"); + if (!string.IsNullOrEmpty(launcherModeMarkerPath)) + { + System.IO.File.WriteAllText( + launcherModeMarkerPath, + Environment.GetEnvironmentVariable("TESTINGPLATFORM_PACKAGEDAPP_LAUNCHER") ?? string.Empty); + } var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args); testApplicationBuilder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_,__) => new DummyTestFramework()); diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs index 06328f2028..4746d42e0d 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs @@ -148,15 +148,19 @@ public void GetConnectBackEnvironment_IncludesRetryAttemptAndExcludesUnrelatedVa { ["TESTINGPLATFORM_TESTHOSTCONTROLLER_PIPENAME_1234"] = "controller-pipe", ["TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"] = "2", + ["TRXNAMEDPIPENAME"] = @"LOCAL\trx-pipe", + ["TESTINGPLATFORM_HANGDUMP_PIPENAME"] = @"LOCAL\hangdump-pipe", ["TESTINGPLATFORM_TESTCONFIGURATION"] = "secret", }, workingDirectory: null); var environment = PackagedAppTestHostLauncher.GetConnectBackEnvironment(context).ToDictionary(); - Assert.HasCount(2, environment); + Assert.HasCount(4, environment); Assert.AreEqual("controller-pipe", environment["TESTINGPLATFORM_TESTHOSTCONTROLLER_PIPENAME_1234"]); Assert.AreEqual("2", environment["TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"]); + Assert.AreEqual(@"LOCAL\trx-pipe", environment["TRXNAMEDPIPENAME"]); + Assert.AreEqual(@"LOCAL\hangdump-pipe", environment["TESTINGPLATFORM_HANGDUMP_PIPENAME"]); Assert.IsFalse(environment.ContainsKey("TESTINGPLATFORM_TESTCONFIGURATION")); } } From 57cc6d96c6c19edc1a5bd450f277ae21289ba6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 17:53:17 +0200 Subject: [PATCH 05/14] Cover controller endpoint publication ordering Exercise the controller pre-launch handler/provider ordering and retry-derived packaged handshake consumption end to end. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- ...ontrollersTestHost.ProcessConfiguration.cs | 48 ++++++++++++------- .../InternalAPI/InternalAPI.Unshipped.txt | 1 + .../PackagedAppActivationArgumentsTests.cs | 42 ++++++++++++++++ .../TestHostBuilderTests.cs | 26 ++++++++++ 4 files changed, 99 insertions(+), 18 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessConfiguration.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessConfiguration.cs index 070ceb380d..6c13336c91 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessConfiguration.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.ProcessConfiguration.cs @@ -118,29 +118,21 @@ internal sealed partial class TestHostControllersTestHost await concreteMessageBusService.InitAsync().ConfigureAwait(false); ((MessageBusProxy)ServiceProvider.GetMessageBus()).SetBuiltMessageBus(concreteMessageBusService); - // Create controller-side extension connections before their environment-variable providers publish - // the effective pipe names to the test host. - foreach (ITestHostProcessLifetimeHandler lifetimeHandler in _testHostsInformation.LifetimeHandlers) + SystemEnvironmentVariableProvider systemEnvironmentVariableProvider = new(environment); + EnvironmentVariables environmentVariables = new(_loggerFactory) { - await lifetimeHandler.BeforeTestHostProcessStartAsync(cancellationToken).ConfigureAwait(false); - } + CurrentProvider = systemEnvironmentVariableProvider, + }; + await systemEnvironmentVariableProvider.UpdateAsync(environmentVariables).ConfigureAwait(false); + await ApplyControllerExtensionPreLaunchAsync( + _testHostsInformation.LifetimeHandlers, + _testHostsInformation.EnvironmentVariableProviders, + environmentVariables, + cancellationToken).ConfigureAwait(false); // Apply the ITestHostEnvironmentVariableProvider if (_testHostsInformation.EnvironmentVariableProviders.Length > 0) { - SystemEnvironmentVariableProvider systemEnvironmentVariableProvider = new(environment); - EnvironmentVariables environmentVariables = new(_loggerFactory) - { - CurrentProvider = systemEnvironmentVariableProvider, - }; - await systemEnvironmentVariableProvider.UpdateAsync(environmentVariables).ConfigureAwait(false); - - foreach (ITestHostEnvironmentVariableProvider environmentVariableProvider in _testHostsInformation.EnvironmentVariableProviders) - { - environmentVariables.CurrentProvider = environmentVariableProvider; - await environmentVariableProvider.UpdateAsync(environmentVariables).ConfigureAwait(false); - } - environmentVariables.CurrentProvider = null; List<(IExtension, string)> failedValidations = []; @@ -178,4 +170,24 @@ internal sealed partial class TestHostControllersTestHost return (processStartInfo, partialCommandLine); } + + internal static async Task ApplyControllerExtensionPreLaunchAsync( + IReadOnlyList lifetimeHandlers, + IReadOnlyList environmentVariableProviders, + EnvironmentVariables environmentVariables, + CancellationToken cancellationToken) + { + // Servers can qualify their endpoint while starting, so handlers must run before providers publish + // those endpoints to the child process. + foreach (ITestHostProcessLifetimeHandler lifetimeHandler in lifetimeHandlers) + { + await lifetimeHandler.BeforeTestHostProcessStartAsync(cancellationToken).ConfigureAwait(false); + } + + foreach (ITestHostEnvironmentVariableProvider environmentVariableProvider in environmentVariableProviders) + { + environmentVariables.CurrentProvider = environmentVariableProvider; + await environmentVariableProvider.UpdateAsync(environmentVariables).ConfigureAwait(false); + } + } } diff --git a/src/Platform/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt b/src/Platform/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt index dff484af8c..ac02d60277 100644 --- a/src/Platform/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt +++ b/src/Platform/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt @@ -199,6 +199,7 @@ Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint.PipeName. Microsoft.Testing.Platform.TestHostControllers.NamedPipeServerEndpoint.PipeName.set -> void Microsoft.Testing.Platform.TestHostControllers.TestHostControllersManager.BuildTestHostLauncherAsync(Microsoft.Testing.Platform.Services.ServiceProvider! serviceProvider) -> System.Threading.Tasks.Task! Microsoft.Testing.Platform.TestHostOrchestrator.ITestHostControllerConnectionAuthorizationConsumer +static Microsoft.Testing.Platform.Hosts.TestHostControllersTestHost.ApplyControllerExtensionPreLaunchAsync(System.Collections.Generic.IReadOnlyList! lifetimeHandlers, System.Collections.Generic.IReadOnlyList! environmentVariableProviders, Microsoft.Testing.Platform.TestHostControllers.EnvironmentVariables! environmentVariables, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task! Microsoft.Testing.Platform.Services.StopPoliciesService.IsDeadlineTriggered.get -> bool Microsoft.Testing.Platform.Services.StopPoliciesService.RegisterDeadlineStopFallback(System.Func!>! callback) -> void Microsoft.Testing.Platform.Services.StopPoliciesService.Dispose() -> void diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppActivationArgumentsTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppActivationArgumentsTests.cs index ef8648f612..499390e2c3 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppActivationArgumentsTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppActivationArgumentsTests.cs @@ -278,6 +278,48 @@ public void ReadActivationArgumentsAndApplyConnectBack_PackageShapedHost_Restore } } + [TestMethod] + public void ReadActivationArgumentsAndApplyConnectBack_RetryHandshake_RestoresEnvironmentAndConsumesHandshake() + { + string directory = CreateTemporaryDirectory(); + try + { + File.WriteAllText( + Path.Combine(directory, AppxManifestInfo.AppxManifestFileName), + """ + + + + + + """); + + string[] expectedArguments = ["--internal-retry-pipename", @"LOCAL\retry-pipe", "--help"]; + string handshakeId = PackagedAppConnectBackHandshake.TryGetHandshakeId(expectedArguments)!; + string handshakePath = Path.Combine(directory, PackagedAppConnectBackHandshake.GetHandshakeFileName(handshakeId)); + PackagedAppConnectBackHandshake.Write( + handshakePath, + new Dictionary { ["TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"] = "2" }); + PackagedAppActivationData activation = PackagedAppActivationArguments.Create(expectedArguments, directory); + var restoredEnvironment = new Dictionary(StringComparer.Ordinal); + + string[] actualArguments = PackagedAppConnectBackReader.ReadActivationArgumentsAndApplyConnectBack( + activation.Arguments, + directory, + directory, + () => directory, + (name, value) => restoredEnvironment[name] = value); + + AssertArgumentsAreEqual(expectedArguments, actualArguments); + Assert.AreEqual("2", restoredEnvironment["TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"]); + Assert.IsFalse(File.Exists(handshakePath)); + } + finally + { + Directory.Delete(directory, recursive: true); + } + } + private static string CreateTemporaryDirectory() { string directory = Path.Combine(Path.GetTempPath(), "PackagedAppActivationArgumentsTests", Guid.NewGuid().ToString("N")); diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestHostBuilderTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestHostBuilderTests.cs index d84ba78f6d..efa6ff5195 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestHostBuilderTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/TestHostBuilderTests.cs @@ -5,18 +5,44 @@ using Microsoft.Testing.Platform.CommandLine; using Microsoft.Testing.Platform.Configurations; +using Microsoft.Testing.Platform.Extensions.TestHostControllers; using Microsoft.Testing.Platform.Helpers; using Microsoft.Testing.Platform.Hosts; using Microsoft.Testing.Platform.Logging; using Microsoft.Testing.Platform.Services; using Microsoft.Testing.Platform.TestHostControllers; +using Moq; + namespace Microsoft.Testing.Platform.UnitTests; [TestClass] [UnsupportedOSPlatform("browser")] public sealed class TestHostBuilderTests { + [TestMethod] + public async Task ControllerPreLaunch_StartsLifetimeHandlersBeforePublishingEnvironment() + { + string endpoint = "unqualified"; + string? publishedEndpoint = null; + Mock handler = new(); + handler.Setup(x => x.BeforeTestHostProcessStartAsync(It.IsAny())) + .Callback(() => endpoint = @"LOCAL\qualified") + .Returns(Task.CompletedTask); + Mock provider = new(); + provider.Setup(x => x.UpdateAsync(It.IsAny())) + .Callback(() => publishedEndpoint = endpoint) + .Returns(Task.CompletedTask); + + await TestHostControllersTestHost.ApplyControllerExtensionPreLaunchAsync( + [handler.Object], + [provider.Object], + new EnvironmentVariables(new Mock().Object), + CancellationToken.None); + + Assert.AreEqual(@"LOCAL\qualified", publishedEndpoint); + } + [TestMethod] public async Task ConnectToTestHostProcessMonitorIfAvailableAsync_MissingPipeName_ReportsPidQualifiedEnvironmentVariable() { From 03243a539f1542241d4d28d2c089f9c784f11bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 18:10:16 +0200 Subject: [PATCH 06/14] Observe retry hosts that exit before subscription Check custom host state after attaching the exit handler so already-completed launches cannot wait for the pipe timeout. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../RetryTestHostRunner.cs | 9 +++ .../RetryTests.cs | 80 +++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs index 749504a3f9..b41347824e 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs @@ -98,6 +98,15 @@ public static async Task RunAttemptAsync( }; testHostProcess.Exited += exitedHandler; + if (testHostProcess.HasExited) + { +#if NET8_0_OR_GREATER + await processExitedCancellationToken.CancelAsync().ConfigureAwait(false); +#else + processExitedCancellationToken.Cancel(); +#endif + } + try { using var timeout = new CancellationTokenSource(TimeoutHelper.DefaultHangTimeSpanTimeout); diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs index 6f554cadc0..3e141d8a2b 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs @@ -99,6 +99,51 @@ public async Task RunAttemptAsync_UsesRegisteredTestHostLauncher() processHandler.Verify(x => x.Start(It.IsAny()), Times.Never); } + [TestMethod] + public async Task RunAttemptAsync_AlreadyExitedCustomHandle_DoesNotWaitForPipeTimeout() + { + ServiceProvider serviceProvider = new(); + serviceProvider.AddService(new Mock().Object); + Mock loggerFactory = new(); + loggerFactory.Setup(x => x.CreateLogger(It.IsAny())).Returns(new Mock().Object); + serviceProvider.AddService(loggerFactory.Object); + serviceProvider.AddService(new SystemTask()); + Mock cancellationTokenSource = new(); + cancellationTokenSource.SetupGet(x => x.CancellationToken).Returns(CancellationToken.None); + serviceProvider.AddService(cancellationTokenSource.Object); + serviceProvider.AddService(new Mock(MockBehavior.Strict).Object); + serviceProvider.AddService(new AlreadyExitedTestHostLauncher(exitCode: 7)); + Mock outputDevice = new(); + outputDevice.Setup(x => x.DisplayAsync( + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(Task.CompletedTask); + + using var server = new RetryFailedTestsPipeServer(serviceProvider, [], new Mock().Object); + + RetryTestHostRunner.AttemptResult result = await RetryTestHostRunner.RunAttemptAsync( + serviceProvider, + Mock.Of(), + outputDevice.Object, + Mock.Of(), + server, + new ExecutableInfo("testhost.exe", [], string.Empty), + [], + attemptCount: 1, + userMaxRetryCount: 2, + CancellationToken.None); + + Assert.AreEqual(7, result.ExitCode); + Assert.IsTrue(result.ExitedBeforeConnect); + outputDevice.Verify( + x => x.DisplayAsync( + It.IsAny(), + It.IsAny(), + It.IsAny()), + Times.Once); + } + [TestMethod] public void SnapshotAttemptArtifacts_CopiesExternalArtifactAndPreservesDestination() { @@ -1012,4 +1057,39 @@ public void Terminate() public void Dispose() => pipeClient.Dispose(); } + + private sealed class AlreadyExitedTestHostLauncher(int exitCode) : ITestHostLauncher + { + public string Uid => nameof(AlreadyExitedTestHostLauncher); + + public string Version => "1.0.0"; + + public string DisplayName => nameof(AlreadyExitedTestHostLauncher); + + public string Description => nameof(AlreadyExitedTestHostLauncher); + + public Task IsEnabledAsync() => Task.FromResult(true); + + public Task LaunchTestHostAsync(TestHostLaunchContext context, CancellationToken cancellationToken) + => Task.FromResult(new AlreadyExitedTestHostHandle(exitCode)); + } + + private sealed class AlreadyExitedTestHostHandle(int exitCode) : ITestHostHandle + { + public string Identifier => nameof(AlreadyExitedTestHostHandle); + + public int ExitCode => exitCode; + + public bool HasExited => true; + + public Task WaitForExitAsync(CancellationToken cancellationToken) => Task.CompletedTask; + + public void Terminate() + { + } + + public void Dispose() + { + } + } } From 58338807a5cdd864cd9bf5522226112f39068f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 18:24:23 +0200 Subject: [PATCH 07/14] Document generalized package handshake IDs Update the packaged activation handshake contract and parameter names to cover both controller PID and retry pipe derived identifiers. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../InternalAPI/InternalAPI.Unshipped.txt | 4 +-- .../PackagedAppConnectBackHandshake.cs | 27 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/InternalAPI/InternalAPI.Unshipped.txt b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/InternalAPI/InternalAPI.Unshipped.txt index 0c5ec80509..73942f97be 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/InternalAPI/InternalAPI.Unshipped.txt +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/InternalAPI/InternalAPI.Unshipped.txt @@ -33,8 +33,8 @@ static Microsoft.Testing.Extensions.PackagedApp.PackagedAppActivationArguments.R static Microsoft.Testing.Extensions.PackagedApp.PackagedAppActivationArguments.TryDeletePayload(string? payloadPath) -> void static Microsoft.Testing.Extensions.PackagedApp.PackagedAppActivationArguments.TryDeleteStalePayloads(string! localStateDirectory, System.DateTime utcNow) -> void static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.GetHandshakeDirectory(string! packageFamilyName) -> string! -static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.GetHandshakeFileName(string! testHostControllerPid) -> string! -static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.GetHandshakeFilePath(string! packageFamilyName, string! testHostControllerPid) -> string! +static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.GetHandshakeFileName(string! handshakeId) -> string! +static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.GetHandshakeFilePath(string! packageFamilyName, string! handshakeId) -> string! static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.ReadAndDelete(string! filePath) -> System.Collections.Generic.IReadOnlyDictionary? static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.TryDelete(string! filePath) -> void static Microsoft.Testing.Extensions.PackagedApp.PackagedAppConnectBackHandshake.TryGetHandshakeId(System.Collections.Generic.IReadOnlyList! arguments) -> string? diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackHandshake.cs b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackHandshake.cs index 78ae192eab..6b1db19943 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackHandshake.cs +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackHandshake.cs @@ -6,8 +6,8 @@ namespace Microsoft.Testing.Extensions.PackagedApp; /// -/// The out-of-band hand-off that lets a packaged (MSIX) test host receive the controller-to-host -/// connect-back environment variables that a plain Process.Start would have inherited. +/// The out-of-band hand-off that lets a packaged (MSIX) test host receive the controller and retry +/// environment variables that a plain Process.Start would have inherited. /// /// /// @@ -23,9 +23,9 @@ namespace Microsoft.Testing.Extensions.PackagedApp; /// . /// /// -/// The file is named with the test host controller PID (the value the platform passes on the command -/// line as --internal-testhostcontroller-pid) so that concurrent runs of the same package do -/// not collide, and it is deleted by the host as soon as it has been consumed. +/// The file is named with a stable handshake ID so concurrent runs of the same package do not collide. +/// Controller-host runs use the controller PID; retry runs use a hash of their unique pipe name. The +/// host deletes the file as soon as it has been consumed. /// /// internal static class PackagedAppConnectBackHandshake @@ -58,21 +58,20 @@ public static string GetHandshakeDirectory(string packageFamilyName) "LocalState"); /// - /// Returns the file name (without directory) of the handshake file for a given test host controller - /// PID. The directory differs between the two sides (see and the - /// activated host, which resolves its own package LocalState), but the file name is shared. + /// Returns the file name (without directory) for a handshake ID. The directory differs between the + /// two sides (see and the activated host, which resolves its own + /// package LocalState), but the file name is shared. /// - public static string GetHandshakeFileName(string testHostControllerPid) - => $"mtp-testhostcontroller-{testHostControllerPid}.handshake"; + public static string GetHandshakeFileName(string handshakeId) + => $"mtp-testhostcontroller-{handshakeId}.handshake"; /// - /// Returns the full path of the handshake file for a given package family name and test host - /// controller PID. + /// Returns the full path of the handshake file for a package family name and handshake ID. /// - public static string GetHandshakeFilePath(string packageFamilyName, string testHostControllerPid) + public static string GetHandshakeFilePath(string packageFamilyName, string handshakeId) => Path.Combine( GetHandshakeDirectory(packageFamilyName), - GetHandshakeFileName(testHostControllerPid)); + GetHandshakeFileName(handshakeId)); /// /// Extracts the value of the --internal-testhostcontroller-pid option from a command line, From 1028ef43f0fa9987c31938cd60a90e98e579c04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 18:39:11 +0200 Subject: [PATCH 08/14] Complete packaged activation correlation handoff Preserve retry run identifiers across AUMID activation, document the explicit safe allowlist, and correct TRX pipe diagnostics. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../PackagedAppTestHostLauncher.cs | 25 ++++++++----------- .../TrxReportExtensions.cs | 4 +-- .../PackagedAppConnectBackHandshakeTests.cs | 6 ++++- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs index 5788d6ba0c..653fe195ce 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs @@ -85,14 +85,14 @@ internal sealed class PackagedAppTestHostLauncher : ITestHostLauncher, ITestHost /// internal const string PipeAuthorizationModeEnvironmentVariable = "TESTINGPLATFORM_PACKAGEDAPP_PIPEAUTHORIZATION"; - // The prefix of the environment variables the platform uses for the controller-to-host connect-back - // (pipe name, correlation id, parent PID, start time, skip-extension flag). Only these are handed off - // to the activated packaged host: they are exactly what the host needs to reach its controller, they - // contain no user-provided data (unlike broader TESTINGPLATFORM_* variables such as inline - // runsettings, which can carry secrets), and they are read after this extension's builder hook runs. + // The handoff is an explicit allowlist: controller connect-back values, TRX/HangDump pipe endpoints, + // and Retry attempt/run correlation. These contain no user-provided data (unlike broader + // TESTINGPLATFORM_* values such as inline runsettings, which can carry secrets). private const string ConnectBackEnvironmentVariablePrefix = "TESTINGPLATFORM_TESTHOSTCONTROLLER_"; private const string HangDumpPipeEnvironmentVariableName = "TESTINGPLATFORM_HANGDUMP_PIPENAME"; + private const string LogicalRunIdEnvironmentVariableName = "TESTINGPLATFORM_LOGICAL_RUN_ID"; private const string RetryAttemptEnvironmentVariableName = "TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"; + private const string TrxTestRunIdEnvironmentVariableName = "TESTINGPLATFORM_TRX_TESTRUN_ID"; private const string TrxPipeEnvironmentVariableName = "TRXNAMEDPIPENAME"; private readonly string _testApplicationDirectory; @@ -312,15 +312,10 @@ private static async Task LaunchPackagedAsync(TestHostLaunchCon // controller first would give it the controller's ACL and make it unreadable by the activated app. await PackageDeployer.RegisterAsync(manifestPath, cancellationToken).ConfigureAwait(false); - // Hand off the controller-to-host connect-back environment variables through the package's - // LocalState, keyed by the test host controller PID so concurrent runs of the same package do not - // collide. An AUMID-activated process is created by the Windows activation infrastructure and does - // not inherit the controller's environment, so these variables must be reproduced out-of-band. The - // hand-off is deliberately limited to the platform connect-back variables (not the full prepared - // environment): they carry no user secrets, and environment consumed before this extension's - // builder hook runs cannot be reproduced from here anyway. The activated host applies them - // in-process before the platform's environment-variable-based connect-back runs (see - // PackagedAppConnectBackReader). + // Hand off the explicit safe environment allowlist through package LocalState. Controller-host runs + // key the file by controller PID; retry runs key it by a hash of their unique pipe name. An + // AUMID-activated process does not inherit the controller's environment, so the activated host + // applies these values before platform and extension connect-back initialization. string? handshakeId = PackagedAppConnectBackHandshake.TryGetHandshakeId(context.Arguments); string? handshakePath = null; string? activationPayloadPath = null; @@ -393,6 +388,8 @@ private static Task LaunchPackagedAsync(TestHostLaunchContext c { if (environmentVariable.Key.StartsWith(ConnectBackEnvironmentVariablePrefix, StringComparison.Ordinal) || environmentVariable.Key is RetryAttemptEnvironmentVariableName + or LogicalRunIdEnvironmentVariableName + or TrxTestRunIdEnvironmentVariableName or TrxPipeEnvironmentVariableName or HangDumpPipeEnvironmentVariableName) { diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs index f87637714b..9eedbc1bf5 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxReportExtensions.cs @@ -92,7 +92,7 @@ private static void NonBrowserRegistrations(ITestApplicationBuilder builder) new CompositeExtensionFactory(serviceProvider => { ILoggerFactory loggerFactory = serviceProvider.GetLoggerFactory(); - loggerFactory.CreateLogger().LogTrace($"TRX pipe name: '{endpoint.PipeName}"); + loggerFactory.CreateLogger().LogTrace($"TRX pipe name: '{endpoint.PipeName}'"); return new TrxProcessLifetimeHandler( serviceProvider.GetCommandLineOptions(), serviceProvider.GetEnvironment(), @@ -111,7 +111,7 @@ private static void NonBrowserRegistrations(ITestApplicationBuilder builder) builder.TestHostControllers.AddProcessLifetimeHandler(compositeLifeTimeHandler); builder.TestHostControllers.AddEnvironmentVariableProvider(serviceProvider => { - serviceProvider.GetLoggerFactory().CreateLogger().LogTrace($"TRX pipe name: '{endpoint.PipeName}"); + serviceProvider.GetLoggerFactory().CreateLogger().LogTrace($"TRX pipe name: '{endpoint.PipeName}'"); return new TrxEnvironmentVariableProvider(serviceProvider.GetCommandLineOptions(), endpoint); }); } diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs index 4746d42e0d..3f4f410046 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs @@ -150,17 +150,21 @@ public void GetConnectBackEnvironment_IncludesRetryAttemptAndExcludesUnrelatedVa ["TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"] = "2", ["TRXNAMEDPIPENAME"] = @"LOCAL\trx-pipe", ["TESTINGPLATFORM_HANGDUMP_PIPENAME"] = @"LOCAL\hangdump-pipe", + ["TESTINGPLATFORM_TRX_TESTRUN_ID"] = "trx-run", + ["TESTINGPLATFORM_LOGICAL_RUN_ID"] = "logical-run", ["TESTINGPLATFORM_TESTCONFIGURATION"] = "secret", }, workingDirectory: null); var environment = PackagedAppTestHostLauncher.GetConnectBackEnvironment(context).ToDictionary(); - Assert.HasCount(4, environment); + Assert.HasCount(6, environment); Assert.AreEqual("controller-pipe", environment["TESTINGPLATFORM_TESTHOSTCONTROLLER_PIPENAME_1234"]); Assert.AreEqual("2", environment["TESTINGPLATFORM_DOTNETTEST_ATTEMPTNUMBER"]); Assert.AreEqual(@"LOCAL\trx-pipe", environment["TRXNAMEDPIPENAME"]); Assert.AreEqual(@"LOCAL\hangdump-pipe", environment["TESTINGPLATFORM_HANGDUMP_PIPENAME"]); + Assert.AreEqual("trx-run", environment["TESTINGPLATFORM_TRX_TESTRUN_ID"]); + Assert.AreEqual("logical-run", environment["TESTINGPLATFORM_LOGICAL_RUN_ID"]); Assert.IsFalse(environment.ContainsKey("TESTINGPLATFORM_TESTCONFIGURATION")); } } From 6ea24811ba83466e7fc6926b3f7383a2e6c4abdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 18:59:48 +0200 Subject: [PATCH 09/14] Terminate canceled custom retry hosts Bound custom launcher teardown before disposing its handle and defer disposal if termination does not complete. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../RetryTestHostRunner.cs | 35 ++++++++ .../RetryTests.cs | 80 +++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs index b41347824e..c245af9100 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs @@ -19,6 +19,8 @@ namespace Microsoft.Testing.Extensions.Policy; [UnsupportedOSPlatform("browser")] internal static class RetryTestHostRunner { + private static readonly TimeSpan TestHostTerminationTimeout = TimeSpan.FromSeconds(30); + /// /// Result of running one attempt. When is , the child /// process died before it could connect to the retry pipe, and the orchestrator should stop with a generic failure. @@ -131,6 +133,11 @@ public static async Task RunAttemptAsync( await outputDevice.DisplayAsync(producer, new ErrorMessageOutputDeviceData(string.Format(CultureInfo.InvariantCulture, ExtensionResources.TestHostProcessExitedBeforeRetryCouldConnect, testHostProcess.ExitCode)), cancellationToken).ConfigureAwait(false); return new AttemptResult { ExitCode = testHostProcess.ExitCode, ExitedBeforeConnect = true }; } + catch (OperationCanceledException) + { + await TerminateAndWaitForExitAsync(testHostProcess, logger).ConfigureAwait(false); + throw; + } } finally { @@ -142,6 +149,34 @@ public static async Task RunAttemptAsync( return new AttemptResult { ExitCode = testHostProcess.ExitCode, ExitedBeforeConnect = false }; } + private static async Task TerminateAndWaitForExitAsync(IProcess testHostProcess, ILogger logger) + { + try + { + testHostProcess.Kill(); + } + catch (Exception ex) + { + await logger.LogDebugAsync($"Ignoring failure while terminating the retry test host: {ex}").ConfigureAwait(false); + } + + using var timeout = new CancellationTokenSource(TestHostTerminationTimeout); + try + { + await testHostProcess.WaitForExitAsync(timeout.Token).ConfigureAwait(false); + } + catch (OperationCanceledException) when (timeout.IsCancellationRequested) + { + if (testHostProcess is TestHostHandleToProcessAdapter adapter) + { + adapter.DeferDisposalUntilExit(); + } + + await logger.LogWarningAsync( + $"Retry test host did not exit within {TestHostTerminationTimeout} after termination was requested; continuing cancellation.").ConfigureAwait(false); + } + } + private static async Task LaunchUsingCustomLauncherAsync( ITestHostLauncher testHostLauncher, ProcessStartInfo processStartInfo, diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs index 3e141d8a2b..859f2ab45c 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/RetryTests.cs @@ -144,6 +144,43 @@ public async Task RunAttemptAsync_AlreadyExitedCustomHandle_DoesNotWaitForPipeTi Times.Once); } + [TestMethod] + public async Task RunAttemptAsync_CanceledConnection_TerminatesCustomHandleBeforeDisposal() + { + ServiceProvider serviceProvider = new(); + serviceProvider.AddService(new Mock().Object); + Mock loggerFactory = new(); + loggerFactory.Setup(x => x.CreateLogger(It.IsAny())).Returns(new Mock().Object); + serviceProvider.AddService(loggerFactory.Object); + serviceProvider.AddService(new SystemTask()); + Mock applicationCancellation = new(); + applicationCancellation.SetupGet(x => x.CancellationToken).Returns(CancellationToken.None); + serviceProvider.AddService(applicationCancellation.Object); + serviceProvider.AddService(new Mock(MockBehavior.Strict).Object); + var launcher = new TerminatedTestHostLauncher(); + serviceProvider.AddService(launcher); + using var server = new RetryFailedTestsPipeServer(serviceProvider, [], new Mock().Object); + using var cancellation = new CancellationTokenSource(); +#pragma warning disable VSTHRD103 // CancelAsync is unavailable on .NET Framework. + cancellation.Cancel(); +#pragma warning restore VSTHRD103 + + await Assert.ThrowsAsync(() => RetryTestHostRunner.RunAttemptAsync( + serviceProvider, + Mock.Of(), + Mock.Of(), + Mock.Of(), + server, + new ExecutableInfo("testhost.exe", [], string.Empty), + [], + attemptCount: 1, + userMaxRetryCount: 2, + cancellation.Token)); + + Assert.IsTrue(launcher.Handle.TerminateCalled); + Assert.IsTrue(launcher.Handle.Disposed); + } + [TestMethod] public void SnapshotAttemptArtifacts_CopiesExternalArtifactAndPreservesDestination() { @@ -1092,4 +1129,47 @@ public void Dispose() { } } + + private sealed class TerminatedTestHostLauncher : ITestHostLauncher + { + public TerminatedTestHostHandle Handle { get; } = new(); + + public string Uid => nameof(TerminatedTestHostLauncher); + + public string Version => "1.0.0"; + + public string DisplayName => Uid; + + public string Description => Uid; + + public Task IsEnabledAsync() => Task.FromResult(true); + + public Task LaunchTestHostAsync(TestHostLaunchContext context, CancellationToken cancellationToken) + => Task.FromResult(Handle); + } + + private sealed class TerminatedTestHostHandle : ITestHostHandle + { + private readonly TaskCompletionSource _exited = new(TaskCreationOptions.RunContinuationsAsynchronously); + + public bool TerminateCalled { get; private set; } + + public bool Disposed { get; private set; } + + public string Identifier => nameof(TerminatedTestHostHandle); + + public int ExitCode => 1; + + public bool HasExited => _exited.Task.IsCompleted; + + public Task WaitForExitAsync(CancellationToken cancellationToken) => _exited.Task.WithCancellationAsync(cancellationToken); + + public void Terminate() + { + TerminateCalled = true; + _exited.TrySetResult(true); + } + + public void Dispose() => Disposed = true; + } } From 02bd10489c161ae55193f2dc49e4a66a06bb3642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 19:14:54 +0200 Subject: [PATCH 10/14] Clarify packaged handoff security contract Describe the activation allowlist as intentional protocol metadata while retaining the exclusion of arbitrary user-bearing environment values. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../PackagedAppTestHostLauncher.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs index 653fe195ce..0648ff06e1 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs @@ -85,9 +85,10 @@ internal sealed class PackagedAppTestHostLauncher : ITestHostLauncher, ITestHost /// internal const string PipeAuthorizationModeEnvironmentVariable = "TESTINGPLATFORM_PACKAGEDAPP_PIPEAUTHORIZATION"; - // The handoff is an explicit allowlist: controller connect-back values, TRX/HangDump pipe endpoints, - // and Retry attempt/run correlation. These contain no user-provided data (unlike broader - // TESTINGPLATFORM_* values such as inline runsettings, which can carry secrets). + // The handoff is an explicit allowlist of protocol metadata: controller connect-back values, + // TRX/HangDump pipe endpoints, and Retry attempt/run correlation. Some correlation values may be + // supplied by CI or the user, but arbitrary environment values remain excluded because broader + // TESTINGPLATFORM_* values such as inline runsettings can carry secrets. private const string ConnectBackEnvironmentVariablePrefix = "TESTINGPLATFORM_TESTHOSTCONTROLLER_"; private const string HangDumpPipeEnvironmentVariableName = "TESTINGPLATFORM_HANGDUMP_PIPENAME"; private const string LogicalRunIdEnvironmentVariableName = "TESTINGPLATFORM_LOGICAL_RUN_ID"; @@ -379,9 +380,9 @@ private static Task LaunchPackagedAsync(TestHostLaunchContext c } #endif - // Selects the controller connect-back variables and the retry attempt marker from the - // platform-prepared environment. These are the values an AUMID-activated host needs and would not - // otherwise inherit; unrelated TESTINGPLATFORM_* values can contain user data and remain excluded. + // Selects the controller connect-back values, TRX/HangDump endpoints, and Retry attempt/run + // correlation metadata that an AUMID-activated host would not otherwise inherit. Unrelated + // environment values remain excluded because they can contain user data or secrets. internal static IEnumerable> GetConnectBackEnvironment(TestHostLaunchContext context) { foreach (KeyValuePair environmentVariable in context.EnvironmentVariables) From ed8b66153fac795d66dd9aa2eba191efc2ec6057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 21:44:58 +0200 Subject: [PATCH 11/14] Prefer connected retry hosts over exit races Race connection and process exit explicitly, allow a pending server completion to win briefly, and retain bounded cancellation teardown. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../RetryTestHostRunner.cs | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs index c245af9100..1a7e5fb968 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs @@ -113,31 +113,43 @@ public static async Task RunAttemptAsync( { using var timeout = new CancellationTokenSource(TimeoutHelper.DefaultHangTimeSpanTimeout); using var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, cancellationToken); - using var linkedToken2 = CancellationTokenSource.CreateLinkedTokenSource(linkedToken.Token, processExitedCancellationToken.Token); await logger.LogDebugAsync("Wait connection from the test host process").ConfigureAwait(false); - try + Task waitForConnectionTask = retryFailedTestsPipeServer.WaitForConnectionAsync(linkedToken.Token); + var processExitedTask = Task.Delay(Timeout.InfiniteTimeSpan, processExitedCancellationToken.Token); + Task completedTask = await Task.WhenAny(waitForConnectionTask, processExitedTask).ConfigureAwait(false); + + // A launcher can return an already-exited handle after the child successfully connected. Prefer + // that completed connection over the exit notification so the attempt results are still consumed. + if (completedTask != waitForConnectionTask && !waitForConnectionTask.IsCompleted) + { + // ConnectAsync on the client can complete just before the server-side completion is scheduled. + // Give that already-established connection a brief chance to win over the exit notification. + Task connectionOrGracePeriod = await Task.WhenAny( + waitForConnectionTask, + Task.Delay(TimeSpan.FromSeconds(1), linkedToken.Token)).ConfigureAwait(false); + linkedToken.Token.ThrowIfCancellationRequested(); + completedTask = connectionOrGracePeriod; + } + + if (completedTask == waitForConnectionTask || waitForConnectionTask.IsCompleted) { #if NETCOREAPP - await retryFailedTestsPipeServer.WaitForConnectionAsync(linkedToken2.Token).ConfigureAwait(false); + await waitForConnectionTask.ConfigureAwait(false); #else - // We don't know why but if the cancellation is called quickly in `testHostProcess.Exited`: `processExitedCancellationToken.Cancel();` for netfx we stuck sometime here, like if - // the token we pass to the named pipe is not "correctly" verified inside the pipe implementation self. - // We fallback with our custom agnostic cancellation mechanism in that case. - // We see it happen only in .NET FX and not in .NET Core so for now we don't do it for core. - await retryFailedTestsPipeServer.WaitForConnectionAsync(linkedToken2.Token).WithCancellationAsync(linkedToken2.Token).ConfigureAwait(false); + await waitForConnectionTask.WithCancellationAsync(linkedToken.Token).ConfigureAwait(false); #endif } - catch (OperationCanceledException) when (processExitedCancellationToken.IsCancellationRequested) + else { await outputDevice.DisplayAsync(producer, new ErrorMessageOutputDeviceData(string.Format(CultureInfo.InvariantCulture, ExtensionResources.TestHostProcessExitedBeforeRetryCouldConnect, testHostProcess.ExitCode)), cancellationToken).ConfigureAwait(false); return new AttemptResult { ExitCode = testHostProcess.ExitCode, ExitedBeforeConnect = true }; } - catch (OperationCanceledException) - { - await TerminateAndWaitForExitAsync(testHostProcess, logger).ConfigureAwait(false); - throw; - } + } + catch (OperationCanceledException) + { + await TerminateAndWaitForExitAsync(testHostProcess, logger).ConfigureAwait(false); + throw; } finally { From 95d2cdbcc10d38233090b4b2abd6ef97cdb59dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 21:58:02 +0200 Subject: [PATCH 12/14] Clarify controller connection authorization contract Document one-time identity resolution and reuse across platform, extension, and orchestrator-side connections. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../ITestHostControllerConnectionAuthorizer.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Platform/TestHostControllers/ITestHostControllerConnectionAuthorizer.cs b/src/Platform/Microsoft.Testing.Platform/TestHostControllers/ITestHostControllerConnectionAuthorizer.cs index 32bc6470ba..496943dfee 100644 --- a/src/Platform/Microsoft.Testing.Platform/TestHostControllers/ITestHostControllerConnectionAuthorizer.cs +++ b/src/Platform/Microsoft.Testing.Platform/TestHostControllers/ITestHostControllerConnectionAuthorizer.cs @@ -5,7 +5,7 @@ namespace Microsoft.Testing.Platform.Extensions.TestHostControllers; /// /// Implemented by an that starts the test host under an operating-system -/// security identity of its own, to authorize that identity on the controller-to-host connection. +/// security identity of its own, to authorize that identity on controller-side connections. /// /// /// @@ -20,11 +20,12 @@ namespace Microsoft.Testing.Platform.Extensions.TestHostControllers; /// be allowed through. /// /// -/// The connection is established before the launcher runs (it has to be listening before the host is -/// started), so a launcher that needs this cannot supply the identity from -/// . The platform instead calls this interface just -/// before creating the connection, which is why it is a separate interface an -/// opts into rather than a member of that interface. +/// Controller-side connections must listen before the launcher runs, so a launcher cannot supply the +/// identity from . The platform resolves this interface +/// once before creating those connections, either in controller mode or for an orchestrator such as Retry, +/// and reuses the validated result for the platform connection and controller-side extension pipes. This is +/// why authorization is a separate interface an opts into rather than a +/// member of that interface. /// /// /// Security. The platform does not grant whatever it is handed. It validates every value @@ -44,8 +45,8 @@ namespace Microsoft.Testing.Platform.Extensions.TestHostControllers; public interface ITestHostControllerConnectionAuthorizer { /// - /// Returns the operating-system security identities that must be able to reach the controller-to-host - /// connection in addition to the current user. + /// Returns the operating-system security identities that must be able to reach controller-side + /// connections in addition to the current user. /// /// /// On Windows an identity is a security identifier (SID) in SDDL string form, and only the SID of a From 7588450b580ea6a8371cbb17e60c7441a136844e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 22:12:46 +0200 Subject: [PATCH 13/14] Restore UTF-8 BOM on changed C# files Normalize every changed C# file that lacked the repository-required UTF-8 BOM. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs | 2 +- .../PipeNameEnvironmentVariableProviderBase.cs | 2 +- .../EnvironmentVariableProviderTests.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs index 1a7e5fb968..fc67296d4a 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryTestHostRunner.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under dual-license. See LICENSE.PLATFORMTOOLS.txt file in the project root for full license information. using Microsoft.Testing.Extensions.Policy.Resources; diff --git a/src/Platform/SharedExtensionHelpers/PipeNameEnvironmentVariableProviderBase.cs b/src/Platform/SharedExtensionHelpers/PipeNameEnvironmentVariableProviderBase.cs index 0d94167b59..b8a8ed7313 100644 --- a/src/Platform/SharedExtensionHelpers/PipeNameEnvironmentVariableProviderBase.cs +++ b/src/Platform/SharedExtensionHelpers/PipeNameEnvironmentVariableProviderBase.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using Microsoft.Testing.Platform.Extensions; diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/EnvironmentVariableProviderTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/EnvironmentVariableProviderTests.cs index 799a3ceb59..c20738f7d5 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/EnvironmentVariableProviderTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/EnvironmentVariableProviderTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using Microsoft.Testing.Extensions.Diagnostics; From a2ba458f4628b42abd4a296b14e8433f541ce045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 28 Aug 2026 22:29:07 +0200 Subject: [PATCH 14/14] Generalize custom launcher invocation contract Document controller and orchestrator launch contexts, make Windows handoff matching case-insensitive, and generalize authorization diagnostics. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9da6d542-9af8-408d-bd78-ef207c26bc23 --- .../PackagedAppTestHostLauncher.cs | 12 ++++++------ .../Services/ServiceProviderExtensions.cs | 2 +- .../TestHostControllers/ITestHostLauncher.cs | 18 ++++++++---------- .../TestHostLaunchContext.cs | 13 ++++++------- .../PackagedAppConnectBackHandshakeTests.cs | 4 ++-- 5 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs index 0648ff06e1..0ededee553 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs @@ -387,12 +387,12 @@ private static Task LaunchPackagedAsync(TestHostLaunchContext c { foreach (KeyValuePair environmentVariable in context.EnvironmentVariables) { - if (environmentVariable.Key.StartsWith(ConnectBackEnvironmentVariablePrefix, StringComparison.Ordinal) - || environmentVariable.Key is RetryAttemptEnvironmentVariableName - or LogicalRunIdEnvironmentVariableName - or TrxTestRunIdEnvironmentVariableName - or TrxPipeEnvironmentVariableName - or HangDumpPipeEnvironmentVariableName) + if (environmentVariable.Key.StartsWith(ConnectBackEnvironmentVariablePrefix, StringComparison.OrdinalIgnoreCase) + || string.Equals(environmentVariable.Key, RetryAttemptEnvironmentVariableName, StringComparison.OrdinalIgnoreCase) + || string.Equals(environmentVariable.Key, LogicalRunIdEnvironmentVariableName, StringComparison.OrdinalIgnoreCase) + || string.Equals(environmentVariable.Key, TrxTestRunIdEnvironmentVariableName, StringComparison.OrdinalIgnoreCase) + || string.Equals(environmentVariable.Key, TrxPipeEnvironmentVariableName, StringComparison.OrdinalIgnoreCase) + || string.Equals(environmentVariable.Key, HangDumpPipeEnvironmentVariableName, StringComparison.OrdinalIgnoreCase)) { yield return environmentVariable; } diff --git a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProviderExtensions.cs b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProviderExtensions.cs index bb5935fa20..4080af534b 100644 --- a/src/Platform/Microsoft.Testing.Platform/Services/ServiceProviderExtensions.cs +++ b/src/Platform/Microsoft.Testing.Platform/Services/ServiceProviderExtensions.cs @@ -198,7 +198,7 @@ internal static IEnumerable GetServicesInternal(this IServic NamedPipeServerSecurity.AllApplicationPackagesSid)); } - await logger.LogDebugAsync($"'{testHostLauncher.Uid}' authorized the following security identity/identities on the test host controller connection: {string.Join(", ", securityIdentities)}").ConfigureAwait(false); + await logger.LogDebugAsync($"'{testHostLauncher.Uid}' requested the following validated security identity/identities for controller-side connections: {string.Join(", ", securityIdentities)}").ConfigureAwait(false); ((ServiceProvider)serviceProvider).TestHostControllerAuthorizedSecurityIdentities = securityIdentities; return securityIdentities; } diff --git a/src/Platform/Microsoft.Testing.Platform/TestHostControllers/ITestHostLauncher.cs b/src/Platform/Microsoft.Testing.Platform/TestHostControllers/ITestHostLauncher.cs index 6dc90cb713..62ec4c7241 100644 --- a/src/Platform/Microsoft.Testing.Platform/TestHostControllers/ITestHostLauncher.cs +++ b/src/Platform/Microsoft.Testing.Platform/TestHostControllers/ITestHostLauncher.cs @@ -8,22 +8,20 @@ namespace Microsoft.Testing.Platform.Extensions.TestHostControllers; /// platform's default Process.Start behavior. /// /// -/// The platform keeps owning everything around the launch — argument and environment preparation, -/// the controller-to-host IPC pipe, the PID handshake, -/// callbacks, and exit-code reconciliation — and delegates only the single "create and start the -/// test host" step to the registered launcher. The launcher does not have to start a local OS -/// process: it can deploy and activate a packaged application, launch a container, or start the -/// host on a remote machine, as long as it returns an the platform -/// can monitor. +/// The platform keeps owning argument and environment preparation, connection handshakes, lifecycle +/// callbacks, and exit-code reconciliation, and delegates only the single "create and start the test host" +/// step. A controller launch carries the controller IPC/PID handshake; an orchestrator launch can instead +/// carry extension-specific connection metadata such as Retry. Launchers must forward the supplied context +/// opaquely rather than require one specific handshake. The launcher does not have to start a local OS +/// process as long as it returns an the platform can monitor. /// [Experimental("TPEXP", UrlFormat = "https://aka.ms/testingplatform/diagnostics#{0}")] public interface ITestHostLauncher : ITestHostControllersExtension { /// /// Creates and starts the test host. The platform has already prepared the file name, - /// arguments, and environment variables (including the controller IPC pipe name) carried by - /// . The implementation must return a handle the platform can - /// monitor for completion. + /// arguments, and environment variables carried by for the active controller + /// or orchestrator mode. The implementation must return a handle the platform can monitor for completion. /// /// The fully prepared launch information. /// The cancellation token. diff --git a/src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostLaunchContext.cs b/src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostLaunchContext.cs index d7bcffd228..7081677fa4 100644 --- a/src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostLaunchContext.cs +++ b/src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostLaunchContext.cs @@ -14,10 +14,10 @@ public sealed class TestHostLaunchContext /// Initializes a new instance of the class. /// /// The test host executable path the platform would have started. - /// The arguments, already including the test host controller PID option. + /// The fully prepared arguments for the active launch mode. /// - /// The final environment for the test host, including the controller-to-host IPC pipe name the - /// host must connect back on. + /// The final environment for the test host, including any controller or orchestrator connection + /// metadata the host must consume. /// /// The working directory, or to inherit the current one. public TestHostLaunchContext( @@ -38,14 +38,13 @@ public TestHostLaunchContext( public string FileName { get; } /// - /// Gets the arguments, already including the test host controller PID option. + /// Gets the fully prepared arguments for the active launch mode. /// public IReadOnlyList Arguments { get; } /// - /// Gets the final environment for the test host, after all - /// ran. Includes the controller-to-host IPC - /// pipe name the host must connect back on. + /// Gets the final environment for the test host, including any controller or orchestrator connection + /// metadata required by the active launch mode. /// public IReadOnlyDictionary EnvironmentVariables { get; } diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs index 3f4f410046..d895b1938d 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/PackagedAppConnectBackHandshakeTests.cs @@ -151,7 +151,7 @@ public void GetConnectBackEnvironment_IncludesRetryAttemptAndExcludesUnrelatedVa ["TRXNAMEDPIPENAME"] = @"LOCAL\trx-pipe", ["TESTINGPLATFORM_HANGDUMP_PIPENAME"] = @"LOCAL\hangdump-pipe", ["TESTINGPLATFORM_TRX_TESTRUN_ID"] = "trx-run", - ["TESTINGPLATFORM_LOGICAL_RUN_ID"] = "logical-run", + ["testingplatform_logical_run_id"] = "logical-run", ["TESTINGPLATFORM_TESTCONFIGURATION"] = "secret", }, workingDirectory: null); @@ -164,7 +164,7 @@ public void GetConnectBackEnvironment_IncludesRetryAttemptAndExcludesUnrelatedVa Assert.AreEqual(@"LOCAL\trx-pipe", environment["TRXNAMEDPIPENAME"]); Assert.AreEqual(@"LOCAL\hangdump-pipe", environment["TESTINGPLATFORM_HANGDUMP_PIPENAME"]); Assert.AreEqual("trx-run", environment["TESTINGPLATFORM_TRX_TESTRUN_ID"]); - Assert.AreEqual("logical-run", environment["TESTINGPLATFORM_LOGICAL_RUN_ID"]); + Assert.AreEqual("logical-run", environment["testingplatform_logical_run_id"]); Assert.IsFalse(environment.ContainsKey("TESTINGPLATFORM_TESTCONFIGURATION")); } }