Skip to content

Commit

Permalink
Merge pull request #1477 from nunit/issue-1469
Browse files Browse the repository at this point in the history
Handle invalid chars sent from the framework
  • Loading branch information
CharliePoole authored Sep 14, 2024
2 parents 0842fd5 + 359e19c commit 5056d41
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 8 deletions.
7 changes: 7 additions & 0 deletions NUnitConsole.sln
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "notest-assembly", "src\Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfApp", "src\TestData\WpfApp\WpfApp.csproj", "{6B550F25-1CA5-4F3E-B631-1ECCD4CB94E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InvalidTestNames", "src\TestData\InvalidTestNames\InvalidTestNames.csproj", "{58E18ACC-1F7E-4395-817E-E7EF943E0C77}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -212,6 +214,10 @@ Global
{6B550F25-1CA5-4F3E-B631-1ECCD4CB94E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B550F25-1CA5-4F3E-B631-1ECCD4CB94E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B550F25-1CA5-4F3E-B631-1ECCD4CB94E4}.Release|Any CPU.Build.0 = Release|Any CPU
{58E18ACC-1F7E-4395-817E-E7EF943E0C77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58E18ACC-1F7E-4395-817E-E7EF943E0C77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58E18ACC-1F7E-4395-817E-E7EF943E0C77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58E18ACC-1F7E-4395-817E-E7EF943E0C77}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -244,6 +250,7 @@ Global
{8FE8378E-5A8B-4708-8F86-35BE0DE121F7} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F}
{81E63A90-3191-4E99-92FF-01F9B1D3E3C5} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F}
{6B550F25-1CA5-4F3E-B631-1ECCD4CB94E4} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F}
{58E18ACC-1F7E-4395-817E-E7EF943E0C77} = {2ECE1CFB-9436-4149-B7E4-1FB1786FDE9F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D8E4FC26-5422-4C51-8BBC-D1AC0A578711}
Expand Down
40 changes: 39 additions & 1 deletion package-tests.cake
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ StandardRunnerTests.Add(new PackageTest(

StandardRunnerTests.Add(new PackageTest(
1, "NUnitProjectTest",
"Run project with both copies of mock-assembly",
"Run NUnit project with mock-assembly.dll built for .NET 4.6.2 and 6.0",
"../../NetFXTests.nunit --config=Release --trace=Debug",
new MockAssemblyExpectedResult("net-4.6.2", "netcore-6.0"),
KnownExtensions.NUnitProjectLoader.SetVersion("3.8.0")));
Expand Down Expand Up @@ -249,3 +249,41 @@ StandardRunnerTests.Add(new PackageTest(
"../../src/TestData/TestData.sln --config=Release --trace=Debug",
MockAssemblySolutionResult,
KnownExtensions.VSProjectLoader.SetVersion("3.9.0")));

// Special Cases

StandardRunnerTests.Add(new PackageTest(
1, "InvalidTestNameTest_Net462",
"Ensure we handle invalid test names correctly under .NET 4.6.2",
"net462/InvalidTestNames.dll --trace:Debug",
new ExpectedResult("Passed")
{
Assemblies = new ExpectedAssemblyResult[]
{
new ExpectedAssemblyResult("InvalidTestNames.dll", "net-4.6.2")
}
}));

AddToBothLists(new PackageTest(
1, "InvalidTestNameTest_Net60",
"Ensure we handle invalid test names correctly under .NET 6.0",
"net6.0/InvalidTestNames.dll --trace:Debug",
new ExpectedResult("Passed")
{
Assemblies = new ExpectedAssemblyResult[]
{
new ExpectedAssemblyResult("InvalidTestNames.dll", "netcore-6.0")
}
}));

AddToBothLists(new PackageTest(
1, "InvalidTestNameTest_Net80",
"Ensure we handle invalid test names correctly under .NET 8.0",
"net8.0/InvalidTestNames.dll --trace:Debug",
new ExpectedResult("Passed")
{
Assemblies = new ExpectedAssemblyResult[]
{
new ExpectedAssemblyResult("InvalidTestNames.dll", "netcore-8.0")
}
}));
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public void CreateWriter()
_writer = new ExtendedTextWrapper(new StringWriter(_output));
}

[TestCase(char.MaxValue)]
public void TestNameContainsInvalidChar(char c)
{
Console.WriteLine($"Test for char {c}");
}

[TestCaseSource("SingleEventData")]
public void SingleEventsWriteExpectedOutput(string report, string labels, string expected)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

<ItemGroup Condition="'$(TargetFramework)'!='net462'">
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<!--<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />-->
</ItemGroup>

<ItemGroup>
Expand All @@ -45,9 +45,7 @@
<ItemGroup>
<ProjectReference Include="..\..\TestData\mock-assembly\mock-assembly.csproj" />
<ProjectReference Include="..\..\NUnitEngine\nunit.engine\nunit.engine.csproj" />
<ProjectReference Include="..\nunit3-console\nunit3-console.csproj">
<Aliases></Aliases>
</ProjectReference>
<ProjectReference Include="..\nunit3-console\nunit3-console.csproj" />
<ProjectReference Include="..\..\NUnitEngine\nunit.engine.api\nunit.engine.api.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IDriverFactory
/// <param name="reference">An AssemblyName referring to the possible test framework.</param>
bool IsSupportedTestFramework(AssemblyName reference);

#if NETSTANDARD2_0
#if NETSTANDARD || NETCOREAPP
/// <summary>
/// Gets a driver for a given test assembly and a framework
/// which the assembly is already known to reference.
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitEngine/nunit.engine.api/TestEngineActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class TestEngineActivator
private const string DefaultAssemblyName = "nunit.engine.dll";
internal const string DefaultTypeName = "NUnit.Engine.TestEngine";

#if NETSTANDARD2_0
#if NETSTANDARD || NETCOREAPP
/// <summary>
/// Create an instance of the test engine.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/NUnitEngine/nunit.engine/Runners/TestEventDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ public TestEventDispatcher()

public void OnTestEvent(string report)
{
const string badChar = "\xffff";

lock (_eventLock)
{
// TODO: Review whether we need more checks
report = report.Replace(badChar, "?");

foreach (var listener in Listeners)
listener.OnTestEvent(report);
}
Expand Down
22 changes: 22 additions & 0 deletions src/TestData/InvalidTestNames/InvalidTestNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace InvalidTestNames
{
// Issue 1469 showed that the NUnit framework sometimes sends char
// '\uffff' in test names, if the user provides it as an argument.
// Therefore, the runner has to protect itself. If additional bad
// characters are detected, we can add more tests here.
public class InvalidTestNames
{
// Generates test name TestContainsInvalidCharacter("\uffff");
[TestCase(char.MaxValue)]
public void TestNameContainsInvalidChar(char c)
{
Console.WriteLine($"Test for char {c}");
}
}
}
12 changes: 12 additions & 0 deletions src/TestData/InvalidTestNames/InvalidTestNames.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;net6.0;net8.0</TargetFrameworks>
<OutputType>Library</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include = "nunit" Version="3.13.3" />
</ItemGroup>

</Project>

0 comments on commit 5056d41

Please sign in to comment.