Skip to content
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

Fix Excessive Encoding in Test Logs #92286

Merged
merged 4 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/tests/Common/XUnitWrapperLibrary/TestSummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace XUnitWrapperLibrary;

public class TestSummary
Expand Down Expand Up @@ -43,8 +44,14 @@ public string ToXmlString()
testResultSb.Append($@"<test name=""{Name}"" type=""{ContainingTypeName}"""
+ $@" method=""{MethodName}"" time=""{Duration.TotalSeconds:F6}""");

// GH dotnet/runtime issue #92092: It is possible for tests to have
// illegal XML characters in their output. So, we have to sanitize said
// output before writing it down to the XML log because otherwise, the
// python XML parser crashes. The way we clean it is by replacing said
// characters with their hexadecimal notation in SanitizeOutput().

string outputElement = !string.IsNullOrWhiteSpace(Output)
? $"<output><![CDATA[{XmlConvert.EncodeName(Output)}]]></output>"
? $"<output><![CDATA[{SanitizeOutput(Output)}]]></output>"
: string.Empty;

if (Exception is not null)
Expand Down Expand Up @@ -95,12 +102,36 @@ public string ToXmlString()

return testResultSb.ToString();
}

private string SanitizeOutput(string output)
{
StringBuilder sanitizedOutput = new StringBuilder();

// Check each character in the given test's output:
// * If it's a legal XML character, then write it as is.
// * Otherwise, write it in its hexadecimal notation.

foreach (char ch in output)
{
if (XmlConvert.IsXmlChar(ch))
{
sanitizedOutput.Append(ch);
}
else
{
int charCode = Convert.ToInt32(ch);
sanitizedOutput.AppendFormat("_0x{0}_", charCode.ToString("X"));
}
}

return sanitizedOutput.ToString();
}
}

public int PassedTests { get; private set; }
public int FailedTests { get; private set; }
public int SkippedTests { get; private set; }
public int TotalTests { get; private set; }
public int PassedTests { get; private set; } = 0;
public int FailedTests { get; private set; } = 0;
public int SkippedTests { get; private set; } = 0;
public int TotalTests { get; private set; } = 0;

private readonly List<TestResult> _testResults = new();
private DateTime _testRunStart = DateTime.Now;
Expand Down
37 changes: 28 additions & 9 deletions src/tests/JIT/Regression/VS-ia64-JIT/M00/b119026/bug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,50 @@
// The .NET Foundation licenses this file to you under the MIT license.
//

using System;
using Xunit;

public class test
{
static short si16;
static uint su32;

[Fact]
public static void TestEntryPoint()
public static int TestEntryPoint()
{
si16 = -1;
su32 = (uint)si16;
System.Console.WriteLine(su32);
if (su32 == uint.MaxValue)
int exitCode = -1;
short i16 = -1;
uint u32 = (uint) i16;

Console.WriteLine(u32);

if (u32 == uint.MaxValue)
{
System.Console.WriteLine("Pass");
exitCode = 100;
}
else
{
System.Console.WriteLine("Fail");
short i16 = -1;
uint u32 = (uint)i16;
System.Console.WriteLine(u32);
if (u32 == uint.MaxValue)
exitCode = 101;
}

si16 = -1;
su32 = (uint) si16;

Console.WriteLine(su32);

if (su32 == uint.MaxValue)
{
System.Console.WriteLine("Pass");
}
else
{
System.Console.WriteLine("Fail");
if (exitCode == 100)
exitCode = 101;
}

return exitCode;
}
}
31 changes: 24 additions & 7 deletions src/tests/JIT/Regression/VS-ia64-JIT/M00/b119026/charbug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,50 @@
// The .NET Foundation licenses this file to you under the MIT license.
//

using System;
using Xunit;

public class test
{
static sbyte si8;
static char sc;

[Fact]
public static int TestEntryPoint()
{
int exitCode = -1;
sbyte i8 = -1;
char c = (char)i8;
System.Console.WriteLine("{0}: {1}", c, ((ushort)c));
char c = (char) i8;

Console.WriteLine("{0:X}: {1}", Convert.ToUInt32(c), ((ushort)c));

if (c == char.MaxValue)
System.Console.WriteLine("Pass");
{
Console.WriteLine("Pass");
exitCode = 100;
}
else
System.Console.WriteLine("Fail");
{
Console.WriteLine("Fail");
exitCode = 101;
}

si8 = -1;
sc = (char)si8;
System.Console.WriteLine("{0}: {1}", sc, ((ushort)sc));

Console.WriteLine("{0:X}: {1}", Convert.ToUInt32(sc), ((ushort)sc));

if (sc == char.MaxValue)
{
System.Console.WriteLine("Pass");
return 100;
}
else
{
System.Console.WriteLine("Fail");
return 1;
if (exitCode == 100)
exitCode = 101;
}

return exitCode;
}
}
Loading