From: Hugh Bellamy Date: Mon, 23 Jul 2018 06:04:08 +0000 (+0100) Subject: Add/cleanup Free/PtrTo tests (dotnet/corefx#31151) X-Git-Tag: submit/tizen/20210909.063632~11031^2~3893 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=25f1b31351222ae84dca6fa34803fe05148c866d;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Add/cleanup Free/PtrTo tests (dotnet/corefx#31151) Add/cleanup Free/PtrTo tests Commit migrated from https://github.com/dotnet/corefx/commit/2a86a4a72bc4746f4df97b65955b3315ea5774c4 --- diff --git a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.Tests.csproj b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.Tests.csproj index b4cd584..0d2cf11 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.Tests.csproj +++ b/src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.Tests.csproj @@ -59,6 +59,9 @@ + + + @@ -77,7 +80,10 @@ - + + + + @@ -98,6 +104,11 @@ + + + + + diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeBSTRTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeBSTRTests.cs new file mode 100644 index 0000000..e178fb7 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeBSTRTests.cs @@ -0,0 +1,33 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class FreeBSTRTests + { + [Fact] + public void FreeBSTR_ValidPointer_Success() + { + IntPtr ptr = Marshal.StringToBSTR("hello"); + Marshal.FreeBSTR(ptr); + } + + [Fact] + public void FreeBSTR_Zero_Nop() + { + Marshal.FreeBSTR(IntPtr.Zero); + } + + [Fact] + [PlatformSpecific(TestPlatforms.Windows)] + public void FreeBSTR_Win32Atom_Nop() + { + // Windows Marshal has specific checks that do not free + // anything if the ptr is less than 64K. + Marshal.FreeBSTR((IntPtr)1); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeCoTaskMemTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeCoTaskMemTests.cs new file mode 100644 index 0000000..360c464 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeCoTaskMemTests.cs @@ -0,0 +1,33 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class FreeCoTaskMemTests + { + [Fact] + public void FreeCoTaskMem_ValidPointer_Success() + { + IntPtr mem = Marshal.AllocCoTaskMem(10); + Marshal.FreeCoTaskMem(mem); + } + + [Fact] + public void FreeCoTaskMem_Zero_Nop() + { + Marshal.FreeCoTaskMem(IntPtr.Zero); + } + + [Fact] + [PlatformSpecific(TestPlatforms.Windows)] + public void FreeCoTaskMem_Win32Atom_Nop() + { + // Windows Marshal has specific checks that do not free + // anything if the ptr is less than 64K. + Marshal.FreeCoTaskMem((IntPtr)1); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeHGlobalTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeHGlobalTests.cs new file mode 100644 index 0000000..cc701a2 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeHGlobalTests.cs @@ -0,0 +1,33 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class FreeHGlobalTests + { + [Fact] + public void FreeHGlobal_ValidPointer_Success() + { + IntPtr mem = Marshal.AllocHGlobal(10); + Marshal.FreeHGlobal(mem); + } + + [Fact] + public void FreeHGlobal_Zero_Nop() + { + Marshal.FreeHGlobal(IntPtr.Zero); + } + + [Fact] + [PlatformSpecific(TestPlatforms.Windows)] + public void FreeHGlobal_Win32Atom_Nop() + { + // Windows Marshal has specific checks that do not free + // anything if the ptr is less than 64K. + Marshal.FreeHGlobal((IntPtr)1); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringAnsiTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringAnsiTests.cs new file mode 100644 index 0000000..a5aedaf --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringAnsiTests.cs @@ -0,0 +1,57 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class PtrToStringAnsiTests + { + [Theory] + [InlineData("", 0)] + [InlineData("hello", 0)] + [InlineData("hello", 1)] + [InlineData("hello", 4)] + public void PtrToStringAnsi_Length_Success(string s, int len) + { + IntPtr ptr = Marshal.StringToCoTaskMemAnsi(s); + try + { + string result = Marshal.PtrToStringAnsi(ptr, len); + Assert.Equal(s.Substring(0, len), result); + } + finally + { + Marshal.FreeCoTaskMem(ptr); + } + } + + [Fact] + public void PtrToStringAnsi_ZeroPointer_ReturnsNull() + { + Assert.Null(Marshal.PtrToStringAnsi(IntPtr.Zero)); + } + + [Fact] + [PlatformSpecific(TestPlatforms.Windows)] + public void PtrToStringAnsi_Win32AtomPointer_ReturnsNull() + { + // Windows Marshal has specific checks that does not do + // anything if the ptr is less than 64K. + Assert.Null(Marshal.PtrToStringAnsi((IntPtr)1)); + } + + [Fact] + public void PtrToStringAnsi_ZeroPointer_ThrowsArgumentNullException() + { + AssertExtensions.Throws("ptr", () => Marshal.PtrToStringAnsi(IntPtr.Zero, 123)); + } + + [Fact] + public void PtrToStringAnsi_NegativeLength_ThrowsArgumentExeption() + { + AssertExtensions.Throws("len", null, () => Marshal.PtrToStringAnsi(new IntPtr(123), -77)); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringBSTR.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringBSTR.cs new file mode 100644 index 0000000..5bb47c5 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringBSTR.cs @@ -0,0 +1,17 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class PtrToStringBSTRTests + { + [Fact] + public void PtrToStringBSTR_ZeroPointer_ThrowsArgumentNullException() + { + AssertExtensions.Throws("ptr", () => Marshal.PtrToStringBSTR(IntPtr.Zero)); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringTests.cs deleted file mode 100644 index 9aabe46..0000000 --- a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringTests.cs +++ /dev/null @@ -1,40 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Xunit; - -namespace System.Runtime.InteropServices.Tests -{ - public class PtrToStringTests - { - [Fact] - public void PtrToStringAnsi() - { - AssertExtensions.Throws("ptr", () => Marshal.PtrToStringAnsi(IntPtr.Zero, 123)); - Assert.Throws(() => Marshal.PtrToStringAnsi(new IntPtr(123), -77)); - } - - [Fact] - public void PtrToStringUni() - { - AssertExtensions.Throws("ptr", () => Marshal.PtrToStringUni(IntPtr.Zero, 123)); - Assert.Throws(() => Marshal.PtrToStringUni(new IntPtr(123), -77)); - } - - [Fact] - public void PtrToStringBSTR() - { - AssertExtensions.Throws("ptr", () => Marshal.PtrToStringBSTR(IntPtr.Zero)); - } - -#if netcoreapp - [Fact] - public void PtrToStringUTF8() - { - Assert.Null(Marshal.PtrToStringUTF8(IntPtr.Zero, 123)); - Assert.Throws(() => Marshal.PtrToStringUTF8(new IntPtr(123), -77)); - } -#endif - } -} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringUTF8Tests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringUTF8Tests.cs new file mode 100644 index 0000000..bf72a55 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringUTF8Tests.cs @@ -0,0 +1,54 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class PtrToStringUTF8 + { + [Theory] + [InlineData("", 0)] + [InlineData("hello", 0)] + [InlineData("hello", 1)] + [InlineData("hello", 4)] + public void PtrToStringUTF8_Length_Success(string s, int len) + { + IntPtr ptr = Marshal.StringToCoTaskMemUTF8(s); + try + { + string result = Marshal.PtrToStringUTF8(ptr, len); + Assert.Equal(s.Substring(0, len), result); + } + finally + { + Marshal.FreeCoTaskMem(ptr); + } + } + + [Fact] + public void PtrToStringUTF8_ZeroPointer_ReturnsNull() + { + Assert.Null(Marshal.PtrToStringUTF8(IntPtr.Zero)); + Assert.Null(Marshal.PtrToStringUTF8(IntPtr.Zero, 0)); + Assert.Null(Marshal.PtrToStringUTF8(IntPtr.Zero, 1)); + } + + [Fact] + [PlatformSpecific(TestPlatforms.Windows)] + public void PtrToStringUTF8_Win32AtomPointer_ReturnsNull() + { + // Windows Marshal has specific checks that does not do + // anything if the ptr is less than 64K. + Assert.Null(Marshal.PtrToStringUTF8((IntPtr)1, 10)); + } + + [Fact] + public void PtrToStringUTF8_NegativeLength_ThrowsArgumentOutOfRangeExeption() + { + AssertExtensions.Throws("byteLen", null, () => Marshal.PtrToStringUTF8(new IntPtr(123), -77)); + AssertExtensions.Throws("byteLen", null, () => Marshal.PtrToStringUTF8(IntPtr.Zero, -77)); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringUniTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringUniTests.cs new file mode 100644 index 0000000..4d4be3d --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringUniTests.cs @@ -0,0 +1,57 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class PtrToStringUniTests + { + [Theory] + [InlineData("", 0)] + [InlineData("hello", 0)] + [InlineData("hello", 1)] + [InlineData("hello", 4)] + public void PtrToStringUni_Length_Success(string s, int len) + { + IntPtr ptr = Marshal.StringToCoTaskMemUni(s); + try + { + string result = Marshal.PtrToStringUni(ptr, len); + Assert.Equal(s.Substring(0, len), result); + } + finally + { + Marshal.FreeCoTaskMem(ptr); + } + } + + [Fact] + public void PtrToStringUni_ZeroPointer_ReturnsNull() + { + Assert.Null(Marshal.PtrToStringUni(IntPtr.Zero)); + } + + [Fact] + [PlatformSpecific(TestPlatforms.Windows)] + public void PtrToStringUni_Win32AtomPointer_ReturnsNull() + { + // Windows Marshal has specific checks that does not do + // anything if the ptr is less than 64K. + Assert.Null(Marshal.PtrToStringUni((IntPtr)1)); + } + + [Fact] + public void PtrToStringUni_ZeroPointer_ThrowsArgumentNullException() + { + AssertExtensions.Throws("ptr", () => Marshal.PtrToStringUni(IntPtr.Zero, 123)); + } + + [Fact] + public void PtrToStringUni_NegativeLength_ThrowsArgumentExeption() + { + AssertExtensions.Throws("len", null, () => Marshal.PtrToStringUni(new IntPtr(123), -77)); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemAnsiTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemAnsiTests.cs new file mode 100644 index 0000000..72ffb06 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemAnsiTests.cs @@ -0,0 +1,39 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Security; +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class ZeroFreeCoTaskMemAnsiTests + { + [Fact] + public void ZeroFreeCoTaskMemAnsi_ValidPointer_Success() + { + using (SecureString secureString = ToSecureString("hello")) + { + IntPtr ptr = Marshal.SecureStringToCoTaskMemAnsi(secureString); + Marshal.ZeroFreeCoTaskMemAnsi(ptr); + } + } + + [Fact] + public void ZeroFreeCoTaskMemAnsi_Zero_Nop() + { + Marshal.ZeroFreeCoTaskMemAnsi(IntPtr.Zero); + } + + private static SecureString ToSecureString(string data) + { + var str = new SecureString(); + foreach (char c in data) + { + str.AppendChar(c); + } + str.MakeReadOnly(); + return str; + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemUTF8Tests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemUTF8Tests.cs new file mode 100644 index 0000000..f1712d3 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemUTF8Tests.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class ZeroFreeCoTaskMemUTF8Tests + { + [Fact] + public void ZeroFreeCoTaskMemUTF8_ValidPointer_Success() + { + IntPtr ptr = Marshal.StringToCoTaskMemUTF8("hello"); + Marshal.ZeroFreeCoTaskMemUTF8(ptr); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemUnicodeTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemUnicodeTests.cs new file mode 100644 index 0000000..71d46f6 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemUnicodeTests.cs @@ -0,0 +1,39 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Security; +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class ZeroFreeCoTaskMemUnicodeTests + { + [Fact] + public void ZeroFreeCoTaskMemUnicode_ValidPointer_Success() + { + using (SecureString secureString = ToSecureString("hello")) + { + IntPtr ptr = Marshal.SecureStringToCoTaskMemUnicode(secureString); + Marshal.ZeroFreeCoTaskMemUnicode(ptr); + } + } + + [Fact] + public void ZeroFreeCoTaskMemUnicode_Zero_Nop() + { + Marshal.ZeroFreeCoTaskMemUnicode(IntPtr.Zero); + } + + private static SecureString ToSecureString(string data) + { + var str = new SecureString(); + foreach (char c in data) + { + str.AppendChar(c); + } + str.MakeReadOnly(); + return str; + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeGlobalAllocAnsiTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeGlobalAllocAnsiTests.cs new file mode 100644 index 0000000..bc96677 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeGlobalAllocAnsiTests.cs @@ -0,0 +1,39 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Security; +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class ZeroFreeGlobalAllocAnsiTests + { + [Fact] + public void ZeroFreeGlobalAllocAnsi_ValidPointer_Success() + { + using (SecureString secureString = ToSecureString("hello")) + { + IntPtr ptr = Marshal.SecureStringToGlobalAllocAnsi(secureString); + Marshal.ZeroFreeGlobalAllocAnsi(ptr); + } + } + + [Fact] + public void ZeroFreeGlobalAllocAnsi_Zero_Nop() + { + Marshal.ZeroFreeGlobalAllocAnsi(IntPtr.Zero); + } + + private static SecureString ToSecureString(string data) + { + var str = new SecureString(); + foreach (char c in data) + { + str.AppendChar(c); + } + str.MakeReadOnly(); + return str; + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeGlobalAllocUnicodeTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeGlobalAllocUnicodeTests.cs new file mode 100644 index 0000000..012e856 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeGlobalAllocUnicodeTests.cs @@ -0,0 +1,39 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Security; +using Xunit; + +namespace System.Runtime.InteropServices.Tests +{ + public class ZeroFreeGlobalAllocUnicodeTests + { + [Fact] + public void ZeroFreeGlobalAllocUnicode_ValidPointer_Success() + { + using (SecureString secureString = ToSecureString("hello")) + { + IntPtr ptr = Marshal.SecureStringToGlobalAllocUnicode(secureString); + Marshal.ZeroFreeGlobalAllocUnicode(ptr); + } + } + + [Fact] + public void ZeroFreeGlobalAllocUnicode_Zero_Nop() + { + Marshal.ZeroFreeGlobalAllocUnicode(IntPtr.Zero); + } + + private static SecureString ToSecureString(string data) + { + var str = new SecureString(); + foreach (char c in data) + { + str.AppendChar(c); + } + str.MakeReadOnly(); + return str; + } + } +}