summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLuqun Lou <luqunl@users.noreply.github.com>2018-03-29 13:59:18 -0700
committerGitHub <noreply@github.com>2018-03-29 13:59:18 -0700
commit1b0e8e3f76530efb68e40c0c47d1c568b4029f65 (patch)
tree68613608a795bd174a8a6fb7c6d3a649b18d9fc7 /tests
parent54c2b5bcaccefb4259aba2d0aca8cef89b6b5b6b (diff)
downloadcoreclr-1b0e8e3f76530efb68e40c0c47d1c568b4029f65.tar.gz
coreclr-1b0e8e3f76530efb68e40c0c47d1c568b4029f65.tar.bz2
coreclr-1b0e8e3f76530efb68e40c0c47d1c568b4029f65.zip
Fix for incorrectly handle invalid UTF8 characters issue (#17302)
There are some behavior difference between C/C++ UTF8 encoder/decoder and Encoding.UTF8 as mentioned by #16786.
Diffstat (limited to 'tests')
-rw-r--r--tests/src/Interop/StringMarshalling/UTF8/UTF8Test.cs52
1 files changed, 50 insertions, 2 deletions
diff --git a/tests/src/Interop/StringMarshalling/UTF8/UTF8Test.cs b/tests/src/Interop/StringMarshalling/UTF8/UTF8Test.cs
index 7bfe19fdac..2e9e6858f8 100644
--- a/tests/src/Interop/StringMarshalling/UTF8/UTF8Test.cs
+++ b/tests/src/Interop/StringMarshalling/UTF8/UTF8Test.cs
@@ -126,8 +126,21 @@ class UTF8StructMarshalling
public int index;
}
+ unsafe struct UnmanagedStruct
+ {
+ public fixed byte psz[8];
+ }
+
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
+ struct ManagedStruct
+ {
+ [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
+ public string str;
+ }
+
[DllImport("UTF8TestNative", CallingConvention = CallingConvention.Cdecl)]
public static extern void TestStructWithUtf8Field(Utf8Struct utfStruct);
+
public static void TestUTF8StructMarshalling(string[] utf8Strings)
{
Utf8Struct utf8Struct = new Utf8Struct();
@@ -137,7 +150,40 @@ class UTF8StructMarshalling
utf8Struct.index = i;
TestStructWithUtf8Field(utf8Struct);
}
- }
+ if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ CompareWithUTF8Encoding();
+ }
+
+ unsafe static void CompareWithUTF8Encoding()
+ {
+ // Compare results with UTF8Encoding
+ UnmanagedStruct ums;
+ ums.psz[0] = 0xFF;
+ ums.psz[1] = (byte)'a';
+ ums.psz[2] = (byte)'b';
+ ums.psz[3] = (byte)'c';
+ ums.psz[4] = (byte)'d';
+ ums.psz[5] = 0;
+
+ IntPtr ptr = (IntPtr)(&ums);
+ ManagedStruct ms = Marshal.PtrToStructure<ManagedStruct>(ptr);
+ string actual = ms.str;
+
+ UTF8Encoding uTF8Encoding = new UTF8Encoding();
+ byte [] b = new byte[5];
+ b[0] = 0xFF;
+ b[1] = (byte)'a';
+ b[2] = (byte)'b';
+ b[3] = (byte)'c';
+ b[4] = (byte)'d';
+ string expected = uTF8Encoding.GetString(b);
+ if (actual != expected)
+ {
+ Console.WriteLine("Actual:" + actual + " Length:" + actual.Length);
+ Console.WriteLine("Expected:" + expected + " Length:" + expected.Length);
+ throw new Exception("UTF8Encoding.GetString doesn't match with Utf8 String Marshaller result");
+ }
+ }
}
// UTF8 string as delegate parameter
@@ -165,6 +211,7 @@ class UTF8DelegateMarshalling
}
}
+
class Test
{
//test strings
@@ -216,6 +263,7 @@ class Test
// String.Empty tests
UTF8StringTests.EmptyStringTest();
+
return 100;
}
-} \ No newline at end of file
+}