Skip to content

Fix IndexOutOfRangeException in VSTestBridge BuildFilter UID escaping - #9771

Merged
Amaury Levé (Evangelink) merged 6 commits into
microsoft:mainfrom
azat-msft:fix/vstestbridge-buildfilter-uid-escaping
Jul 9, 2026
Merged

Amaury Levé (Evangelink) merged 6 commits into
microsoft:mainfrom
azat-msft:fix/vstestbridge-buildfilter-uid-escaping

Conversation

@azat-msft

@azat-msft Azat Mukhametshin (azat-msft) commented Jul 9, 2026 •

Copy link
Copy Markdown
Member

Summary

ContextAdapterBase.BuildFilter reconstructs a VSTest TestCaseFilter string from a TestNodeUidListFilter — the by-UID test selection that IDEs (e.g. Visual Studio Test Explorer) send over the Microsoft.Testing.Platform server protocol. Its hand-rolled escaper had an index bug: the "is this character already escaped?" guard used the node index i instead of the character index k:

if (i - 1 < 0 || currentTestNodeUid.Value[k - 1] != '\\')

It reads Value[k - 1] but tests i - 1 < 0. So for any node after the first (i > 0) whose first character (k == 0) is a filter operator (\ ( ) & | = ! ~), it evaluates Value[-1] and throws IndexOutOfRangeException, aborting the whole run. The same index confusion could also under-escape an operator that follows a literal backslash, splitting a FullyQualifiedName value at what should be an escaped operator.

Fix

Replace the hand-rolled loop with VSTest's canonical FilterHelper.Escape, which escapes every filter operator unconditionally — exactly what a raw (un-escaped) test-node UID needs — removing the index bug entirely.

Tests

Adds RunContextAdapterFilterTests covering BuildFilter output through the public RunContextAdapter.GetTestCaseFilter(...):

  • FullyQualifiedName= vs Id=<guid> selection (and mixed), multi-node | (OR) joining.
  • Runsettings TestCaseFilter and command-line --filter, and their & combination with a UID selection.
  • Special-character escaping for all operators (each operator escaped individually).
  • Index-dimension coverage: a single node (i == 0) and a later node (i > 0) whose name starts with a filter operator both escape and no longer throw.
  • Backslash-then-operator escapes both characters.
  • Empty TestNodeUidListFilter([]) behavior pinned.
  • Round-trip MatchTestCase tests that assert the built filter actually matches the exact test case and rejects near-misses, guarding the full escape → parse → match path.

All 20 tests pass (net8.0, net9.0, net462).

ContextAdapterBase.BuildFilter reconstructs a VSTest TestCaseFilter from a
TestNodeUidListFilter (the by-UID selection IDEs send over the server protocol).
Its hand-rolled escaper guarded the "is this char already escaped?" check with the
node index (i) instead of the character index (k): it read
currentTestNodeUid.Value[k - 1] while testing i - 1 < 0. For any node after the
first (i > 0) whose first character (k == 0) is a filter operator, this evaluated
Value[-1] and threw IndexOutOfRangeException, aborting the whole run. The same
index confusion could also under-escape an operator that follows a literal
backslash.

Replace the loop with VSTest's canonical FilterHelper.Escape, which escapes every
filter operator ('\ ( ) & | = ! ~') unconditionally -- exactly what a raw
test-node UID needs -- removing the index bug.

Add RunContextAdapterFilterTests covering BuildFilter output: FullyQualifiedName
vs Id=<guid> selection, multi-node OR joining, runsettings/command-line filters,
operator escaping, and the two regressions above (a later node starting with an
operator no longer throws; backslash-then-operator escapes both characters).

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings July 9, 2026 08:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an IndexOutOfRangeException in ContextAdapterBase.BuildFilter (in the VSTestBridge), which reconstructs a VSTest TestCaseFilter string from a by-UID TestNodeUidListFilter sent by IDEs over the Microsoft.Testing.Platform server protocol. The hand-rolled escaper's "already escaped?" guard mistakenly tested the node index i while indexing with the character index k (if (i - 1 < 0 || currentTestNodeUid.Value[k - 1] != '\\')), so any node after the first whose first character was a filter operator evaluated Value[-1] and threw, aborting the whole run. The same index confusion could also under-escape an operator following a literal backslash, splitting a FullyQualifiedName value. The fix replaces the loop with VSTest's canonical FilterHelper.Escape, which unconditionally escapes every filter operator — the correct behavior for a raw, un-escaped test-node UID.

Changes:

  • Replaced the buggy hand-rolled per-character escaper in BuildFilter with FilterHelper.Escape (already available via the existing Microsoft.TestPlatform.Filter.Source package reference and Microsoft.VisualStudio.TestPlatform.Common.Filtering using directive).
  • Added RunContextAdapterFilterTests with 14 tests exercising BuildFilter through the public RunContextAdapter.GetTestCaseFilter, covering FQN vs Id= selection, multi-node OR joining, runsettings/command-line filter combination, full operator escaping, and explicit regressions for both bug variants.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/ContextAdapterBase.cs Removes the off-by-index hand-rolled escaper and delegates to FilterHelper.Escape, eliminating the IndexOutOfRangeException and under-escaping bug.
test/UnitTests/Microsoft.Testing.Extensions.VSTestBridge.UnitTests/ObjectModel/RunContextAdapterFilterTests.cs New MSTest-based coverage for the filter-building logic, including targeted regression tests for the fixed index bug.

@Evangelink Amaury Levé (Evangelink) left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed in depth (manual pass + a second expert-review pass) and built/ran the test project locally — all 14 tests pass on net8.0, net9.0 and net462, 0 warnings.

Verdict: LGTM. The root cause is correctly diagnosed: the old escaper guard bounds-checked the node index i but indexed with the char index k (Value[k - 1] while testing i - 1 < 0), so any node after the first whose first char is a filter operator read Value[-1] and threw IndexOutOfRangeException; it also under-escaped an operator following a literal backslash. Replacing it with FilterHelper.Escape (VSTest canonical escaper, already reachable via the imported ...Common.Filtering namespace) is the right fix — MTP sends raw, unescaped UIDs, so escaping every operator unconditionally is exactly correct, and the Escape -> FilterExpressionWrapper/Condition tokenizer -> Unescape round-trip is lossless. I independently decoded every expected string in the tests against the actual output of Escape and they all match, including the subtle A.B\\\|C triple-backslash case.

Only a few optional, non-blocking follow-ups below.

  • (Low) An empty UID list (new TestNodeUidListFilter([])) is constructible and yields the filter string "()" (pre-existing behavior, not introduced here). Since this PR adds the definitive filter-building suite, a test pinning that outcome would be nice to have.

No public API added; assertion style and conventions are compliant. Thanks for the thorough tests!

Assert the built filter not only produces the expected escaped string but also
actually matches (via ITestCaseFilterExpression.MatchTestCase) a test case whose
FullyQualifiedName is exactly the escaped name and rejects others, guarding the
full escape -> parse -> match path against a future parser-side regression.

Co-authored-by: Copilot App <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Low

Copilot AI review requested due to automatic review settings July 9, 2026 12:59
@azat-msft

Copy link
Copy Markdown
Member Author

Addressed the follow-ups from your review:

  • Split the operator vs. backslash+operator case into two focused [TestMethod]s (GetTestCaseFilter_WithOperator_EscapesOperator and GetTestCaseFilter_WithBackslashFollowedBySpecialCharacter_EscapesBoth).
  • Added the single-node "(weird" case (GetTestCaseFilter_WithSingleNodeStartingWithSpecialCharacter_DoesNotThrowAndEscapes) so the index dimension is exhaustively covered.
  • Added GetTestCaseFilter_WithEmptyNodeList_ThrowsOnEmptyGroup for the empty TestNodeUidListFilter([]). Note: while BuildFilter does produce the () string, going through the public GetTestCaseFilter the VSTest parser rejects the empty parenthesis and throws TestPlatformFormatException (with FilterValue == "()"), so the test pins that actual observable behavior.

All tests pass locally on net8.0 (net9.0 SDK is not installed on my box, but the change is test-only).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings July 9, 2026 13:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Copilot AI review requested due to automatic review settings July 9, 2026 16:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Medium

TestNodeUid lives in Microsoft.Testing.Platform.Extensions.Messages; that using
was removed as 'unused' but CreateUidFilter constructs TestNodeUid, so all TFMs
failed to compile with CS0246. Re-add the directive in alphabetical order.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 9, 2026 17:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Medium

@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) July 9, 2026 17:32
@Evangelink
Amaury Levé (Evangelink) merged commit 20a45c2 into microsoft:main Jul 9, 2026
51 checks passed
@Evangelink

Copy link
Copy Markdown
Member

/backport to rel/4.3

@github-actions

Copy link
Copy Markdown
Contributor

Amaury Levé (Evangelink) added a commit that referenced this pull request Jul 21, 2026
… by @azat-msft in #9771 (backport to rel/4.3) (#10112)

Co-authored-by: Azat Mukhametshin <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Amaury Levé <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants