Repository navigation
Fix IndexOutOfRangeException in VSTestBridge BuildFilter UID escaping #9771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Amaury Levé (Evangelink)
merged 6 commits into
microsoft:main
from
azat-msft:fix/vstestbridge-buildfilter-uid-escaping
Jul 9, 2026
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e2871ae
Fix IndexOutOfRangeException in VSTestBridge BuildFilter UID escaping
azat-msft dbf0ee9
Add MatchTestCase round-trip tests for BuildFilter escaping
Evangelink 78405f9
Address review feedback: split operator/backslash tests, add single-n…
azat-msft 1c48163
Potential fix for pull request finding
azat-msft 0507068
Merge branch 'main' into fix/vstestbridge-buildfilter-uid-escaping
Evangelink 22d9268
Restore missing using directive to fix CS0246 build break
Evangelink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
293 changes: 293 additions & 0 deletions
293
...oft.Testing.Extensions.VSTestBridge.UnitTests/ObjectModel/RunContextAdapterFilterTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,293 @@ | ||
| // 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.VSTestBridge.CommandLine; | ||
| using Microsoft.Testing.Extensions.VSTestBridge.ObjectModel; | ||
| using Microsoft.Testing.Platform.CommandLine; | ||
| using Microsoft.Testing.Platform.Requests; | ||
|
Evangelink marked this conversation as resolved.
Resolved
|
||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; | ||
|
|
||
| using Moq; | ||
|
|
||
| namespace Microsoft.Testing.Extensions.VSTestBridge.UnitTests.ObjectModel; | ||
|
|
||
| /// <summary> | ||
| /// Tests for the filter building logic in <see cref="ContextAdapterBase"/> (exercised through | ||
| /// <see cref="RunContextAdapter"/>), most notably the private <c>BuildFilter</c> method that turns a | ||
| /// <see cref="TestNodeUidListFilter"/> coming from the server protocol into a VSTest | ||
| /// <c>TestCaseFilter</c> expression. | ||
| /// </summary> | ||
| [TestClass] | ||
| public sealed class RunContextAdapterFilterTests | ||
|
Evangelink marked this conversation as resolved.
Resolved
|
||
| { | ||
| private const string EmptyRunSettings = | ||
| """ | ||
| <RunSettings> | ||
| <RunConfiguration> | ||
| </RunConfiguration> | ||
| </RunSettings> | ||
| """; | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithSecondNodeContainingPipe_KeepsBothNodesAndEscapesPipe() | ||
| { | ||
| // A later node whose name contains '|' must be emitted as an escaped literal ('\|') so it | ||
| // stays part of its FullyQualifiedName value, while the '|' that joins the two clauses remains | ||
| // the OR operator. | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter("Namespace.MyClass.MyTest", "PrintArg(\"as|\")")); | ||
| string res = GetFilterValue(adapter); | ||
|
|
||
| Assert.AreEqual("(FullyQualifiedName=Namespace.MyClass.MyTest|FullyQualifiedName=PrintArg\\(\"as\\|\"\\))", res); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithNopFilter_AndNoRunSettingsOrCommandLineFilter_ReturnsNull() | ||
| { | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, new NopFilter()); | ||
|
|
||
| Assert.IsNull(adapter.GetTestCaseFilter(null, _ => null)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithSingleFullyQualifiedNameNode_BuildsFullyQualifiedNameFilter() | ||
| { | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter("Namespace.MyClass.MyTest")); | ||
|
|
||
| Assert.AreEqual("(FullyQualifiedName=Namespace.MyClass.MyTest)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithEmptyNodeList_ThrowsOnEmptyGroup() | ||
| { | ||
| // An empty UID list is constructible and builds the empty group "()", which the VSTest filter | ||
| // parser rejects, so GetTestCaseFilter surfaces a format error. Pinning this guards the edge | ||
| // case now that this suite owns the filter-building coverage. | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter()); | ||
|
|
||
| TestPlatformFormatException exception = Assert.ThrowsExactly<TestPlatformFormatException>(() => adapter.GetTestCaseFilter(null, _ => null)); | ||
| Assert.AreEqual("()", exception.FilterValue); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithMultipleNodes_JoinsWithOrOperator() | ||
| { | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter("A.B.Test1", "C.D.Test2")); | ||
|
|
||
| Assert.AreEqual("(FullyQualifiedName=A.B.Test1|FullyQualifiedName=C.D.Test2)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithGuidNode_BuildsIdFilter() | ||
| { | ||
| var guid = new Guid("12345678-1234-1234-1234-1234567890ab"); | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter(guid.ToString())); | ||
|
|
||
| Assert.AreEqual($"(Id={guid})", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithMixedGuidAndNameNodes_BuildsIdAndFullyQualifiedNameFilters() | ||
| { | ||
| var guid = new Guid("12345678-1234-1234-1234-1234567890ab"); | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter(guid.ToString(), "A.B.Test")); | ||
|
|
||
| Assert.AreEqual($"(Id={guid}|FullyQualifiedName=A.B.Test)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithSpecialCharactersInName_EscapesFilterOperators() | ||
| { | ||
| // The pipe, parentheses, ampersand, equals, bang, tilde and backslash are all TestCaseFilter | ||
| // operators and must be escaped so they are treated as literals (regression for tests whose | ||
| // display name contains such characters, e.g. NUnit [TestCase("as|")]). | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter("Ns.PrintArg(\"as|\")")); | ||
|
|
||
| Assert.AreEqual("(FullyQualifiedName=Ns.PrintArg\\(\"as\\|\"\\))", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithAllOperatorCharacters_EscapesEachOfThem() | ||
| { | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter("a\\b(c)d&e|f=g!h~i")); | ||
|
|
||
| Assert.AreEqual("(FullyQualifiedName=a\\\\b\\(c\\)d\\&e\\|f\\=g\\!h\\~i)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithRunSettingsTestCaseFilter_UsesRunSettingsFilter() | ||
| { | ||
| string runSettings = | ||
| """ | ||
| <RunSettings> | ||
| <RunConfiguration> | ||
| <TestCaseFilter>Category=Fast</TestCaseFilter> | ||
| </RunConfiguration> | ||
| </RunSettings> | ||
| """; | ||
| RunContextAdapter adapter = CreateAdapter(runSettings, new NopFilter()); | ||
|
|
||
| Assert.AreEqual("(Category=Fast)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithCommandLineFilter_UsesCommandLineFilter() | ||
| { | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, new NopFilter(), commandLineFilter: "Category=Slow"); | ||
|
|
||
| Assert.AreEqual("(Category=Slow)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithRunSettingsAndNodeFilter_CombinesWithAndOperator() | ||
| { | ||
| string runSettings = | ||
| """ | ||
| <RunSettings> | ||
| <RunConfiguration> | ||
| <TestCaseFilter>Category=Fast</TestCaseFilter> | ||
| </RunConfiguration> | ||
| </RunSettings> | ||
| """; | ||
| RunContextAdapter adapter = CreateAdapter(runSettings, CreateUidFilter("A.B.Test")); | ||
|
|
||
| Assert.AreEqual("(Category=Fast) & (FullyQualifiedName=A.B.Test)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithRunSettingsAndCommandLineFilter_CombinesWithAndOperator() | ||
| { | ||
| string runSettings = | ||
| """ | ||
| <RunSettings> | ||
| <RunConfiguration> | ||
| <TestCaseFilter>Category=Fast</TestCaseFilter> | ||
| </RunConfiguration> | ||
| </RunSettings> | ||
| """; | ||
| RunContextAdapter adapter = CreateAdapter(runSettings, new NopFilter(), commandLineFilter: "Priority=1"); | ||
|
|
||
| Assert.AreEqual("(Category=Fast) & (Priority=1)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithSingleNodeStartingWithSpecialCharacter_DoesNotThrowAndEscapes() | ||
| { | ||
| // Companion to the multi-node regression above, exhaustive over the index dimension: the | ||
| // single-node case (i == 0) never threw under the old code because the "i - 1 < 0" guard | ||
| // short-circuited before the bogus Value[k - 1] read. It must still escape the leading operator. | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter("(weird")); | ||
|
|
||
| Assert.AreEqual("(FullyQualifiedName=\\(weird)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithSecondNodeStartingWithSpecialCharacter_DoesNotThrowAndEscapes() | ||
|
azat-msft marked this conversation as resolved.
Resolved
|
||
| { | ||
| // Regression: the "already-escaped" guard in BuildFilter used the node index (i) instead of | ||
| // the character index (k) when looking back at the previous character. For any node after the | ||
| // first (i > 0) whose first character (k == 0) is a filter operator, this evaluated | ||
| // Value[k - 1] == Value[-1] and threw IndexOutOfRangeException. | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter("A.B.Test", "(weird")); | ||
|
|
||
| Assert.AreEqual("(FullyQualifiedName=A.B.Test|FullyQualifiedName=\\(weird)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithOperator_EscapesOperator() | ||
| { | ||
| // A bare operator ('|') in the name must be escaped so it stays a literal instead of being | ||
| // parsed as an OR that splits the clause. | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter("A.B|C")); | ||
|
|
||
| Assert.AreEqual("(FullyQualifiedName=A.B\\|C)", GetFilterValue(adapter)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithBackslashFollowedBySpecialCharacter_EscapesBoth() | ||
|
azat-msft marked this conversation as resolved.
Resolved
|
||
| { | ||
| // Regression: the buggy "already-escaped" guard treated the operator following a literal | ||
| // backslash as if it were already escaped, so it emitted the operator un-escaped. A raw | ||
| // backslash in the name must be escaped to "\\" AND the following operator must still be | ||
| // escaped, otherwise the operator (e.g. '|') is parsed as an OR and the clause is split. | ||
| RunContextAdapter adapterBackslash = CreateAdapter(EmptyRunSettings, CreateUidFilter("A.B\\|C")); | ||
|
|
||
| Assert.AreEqual("(FullyQualifiedName=A.B\\\\\\|C)", GetFilterValue(adapterBackslash)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithSpecialCharactersInName_RoundTripsAndMatchesOnlyExactName() | ||
| { | ||
| // Verify the full escape -> parse -> match path (not just the emitted string): a name full of | ||
| // filter operators must, once escaped, parse back to a filter that matches a test case whose | ||
| // FullyQualifiedName is exactly that name and rejects any other name. | ||
| const string name = "Ns.PrintArg(\"as|\")"; | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter(name)); | ||
|
|
||
| Assert.IsTrue(MatchesFullyQualifiedName(adapter, name)); | ||
| Assert.IsFalse(MatchesFullyQualifiedName(adapter, "Ns.PrintArg(\"as\")")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithBackslashFollowedBySpecialCharacter_RoundTripsAndMatchesOnlyExactName() | ||
| { | ||
| // Regression guard for the round trip: if the backslash-then-operator sequence were under-escaped | ||
| // the parser would split the value at the operator, so the filter would no longer match the exact | ||
| // name (and might match a truncated one). | ||
| const string name = "A.B\\|C"; | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter(name)); | ||
|
|
||
| Assert.IsTrue(MatchesFullyQualifiedName(adapter, name)); | ||
| Assert.IsFalse(MatchesFullyQualifiedName(adapter, "A.B")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetTestCaseFilter_WithMultipleNodes_MatchesEitherNodeButNotOthers() | ||
| { | ||
| // The '|' joining the two clauses must stay an OR operator while a '|' inside a name stays a | ||
| // literal, so the filter matches each node's exact name (including the special-character one) and | ||
| // nothing else. | ||
| RunContextAdapter adapter = CreateAdapter(EmptyRunSettings, CreateUidFilter("A.B.Test1", "PrintArg(\"as|\")")); | ||
|
|
||
| Assert.IsTrue(MatchesFullyQualifiedName(adapter, "A.B.Test1")); | ||
| Assert.IsTrue(MatchesFullyQualifiedName(adapter, "PrintArg(\"as|\")")); | ||
| Assert.IsFalse(MatchesFullyQualifiedName(adapter, "A.B.Test2")); | ||
| } | ||
|
|
||
| private static TestNodeUidListFilter CreateUidFilter(params string[] uids) | ||
| => new([.. uids.Select(uid => new TestNodeUid(uid))]); | ||
|
|
||
| private static string GetFilterValue(RunContextAdapter adapter) | ||
| { | ||
| ITestCaseFilterExpression? filterExpression = adapter.GetTestCaseFilter(null, _ => null); | ||
| Assert.IsNotNull(filterExpression); | ||
| return filterExpression.TestCaseFilterValue; | ||
| } | ||
|
|
||
| private static bool MatchesFullyQualifiedName(RunContextAdapter adapter, string fullyQualifiedName) | ||
| { | ||
| ITestCaseFilterExpression? filterExpression = adapter.GetTestCaseFilter(null, _ => null); | ||
| Assert.IsNotNull(filterExpression); | ||
|
|
||
| var testCase = new TestCase(fullyQualifiedName, new Uri("executor://mstest"), "source.dll"); | ||
| return filterExpression.MatchTestCase( | ||
| testCase, | ||
| propertyName => string.Equals(propertyName, "FullyQualifiedName", StringComparison.OrdinalIgnoreCase) | ||
| ? fullyQualifiedName | ||
| : null); | ||
| } | ||
|
|
||
| private static RunContextAdapter CreateAdapter(string runSettingsXml, ITestExecutionFilter filter, string? commandLineFilter = null) | ||
| { | ||
| var runSettings = new Mock<IRunSettings>(); | ||
| runSettings.Setup(x => x.SettingsXml).Returns(runSettingsXml); | ||
|
|
||
| var commandLineOptions = new Mock<ICommandLineOptions>(); | ||
| string[]? commandLineFilterArguments = commandLineFilter is null ? null : [commandLineFilter]; | ||
| commandLineOptions | ||
| .Setup(x => x.TryGetOptionArgumentList(TestCaseFilterCommandLineOptionsProvider.TestCaseFilterOptionName, out commandLineFilterArguments)) | ||
| .Returns(commandLineFilter is not null); | ||
|
|
||
| return new RunContextAdapter(commandLineOptions.Object, runSettings.Object, filter); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.