Add/cleanup Free/PtrTo tests (dotnet/corefx#31151)
authorHugh Bellamy <hughbellars@gmail.com>
Mon, 23 Jul 2018 06:04:08 +0000 (07:04 +0100)
committerAaron Robinson <arobins@microsoft.com>
Mon, 23 Jul 2018 06:04:08 +0000 (23:04 -0700)
Add/cleanup Free/PtrTo tests

Commit migrated from https://github.com/dotnet/corefx/commit/2a86a4a72bc4746f4df97b65955b3315ea5774c4

14 files changed:
src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.Tests.csproj
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeBSTRTests.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeCoTaskMemTests.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/FreeHGlobalTests.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringAnsiTests.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringBSTR.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringTests.cs [deleted file]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringUTF8Tests.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/PtrToStringUniTests.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemAnsiTests.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemUTF8Tests.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeCoTaskMemUnicodeTests.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeGlobalAllocAnsiTests.cs [new file with mode: 0644]
src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/ZeroFreeGlobalAllocUnicodeTests.cs [new file with mode: 0644]

index b4cd584..0d2cf11 100644 (file)
@@ -59,6 +59,9 @@
     <Compile Include="System\Runtime\InteropServices\Marshal\Common\CommonTypes.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\Common\CommonTypes.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
     <Compile Include="System\Runtime\InteropServices\Marshal\DestroyStructureTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\FreeBSTRTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\FreeCoTaskMemTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\FreeHGlobalTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\GenerateGuidForTypeTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\GenerateGuidForTypeTests.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
     <Compile Include="System\Runtime\InteropServices\Marshal\GenerateProgIdForTypeTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\IsComObjectTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\IsComObjectTests.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
     <Compile Include="System\Runtime\InteropServices\Marshal\PtrToStructureTests.cs" />
-    <Compile Include="System\Runtime\InteropServices\Marshal\PtrToStringTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\PtrToStringAnsiTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\PtrToStringBSTR.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\PtrToStringUniTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\PtrToStringUTF8Tests.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
     <Compile Include="System\Runtime\InteropServices\Marshal\ReAllocHGlobalTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\ReAllocCoTaskMemTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\SecureStringToBSTRTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\OffsetOfTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\PrelinkTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\PrelinkAllTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\ZeroFreeCoTaskMemAnsiTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\ZeroFreeCoTaskMemUnicodeTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\ZeroFreeCoTaskMemUTF8Tests.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\ZeroFreeGlobalAllocAnsiTests.cs" />
+    <Compile Include="System\Runtime\InteropServices\Marshal\ZeroFreeGlobalAllocUnicodeTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\ReadWrite\ByteTests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\ReadWrite\Int16Tests.cs" />
     <Compile Include="System\Runtime\InteropServices\Marshal\ReadWrite\Int32Tests.cs" />
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 (file)
index 0000000..e178fb7
--- /dev/null
@@ -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 (file)
index 0000000..360c464
--- /dev/null
@@ -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 (file)
index 0000000..cc701a2
--- /dev/null
@@ -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 (file)
index 0000000..a5aedaf
--- /dev/null
@@ -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<ArgumentNullException>("ptr", () => Marshal.PtrToStringAnsi(IntPtr.Zero, 123));
+        }
+
+        [Fact]
+        public void PtrToStringAnsi_NegativeLength_ThrowsArgumentExeption()
+        {
+            AssertExtensions.Throws<ArgumentException>("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 (file)
index 0000000..5bb47c5
--- /dev/null
@@ -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<ArgumentNullException>("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 (file)
index 9aabe46..0000000
+++ /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<ArgumentNullException>("ptr", () => Marshal.PtrToStringAnsi(IntPtr.Zero, 123));
-            Assert.Throws<ArgumentException>(() => Marshal.PtrToStringAnsi(new IntPtr(123), -77));
-        }
-
-        [Fact]
-        public void PtrToStringUni()
-        {
-            AssertExtensions.Throws<ArgumentNullException>("ptr", () => Marshal.PtrToStringUni(IntPtr.Zero, 123));
-            Assert.Throws<ArgumentException>(() => Marshal.PtrToStringUni(new IntPtr(123), -77));
-        }
-
-        [Fact]
-        public void PtrToStringBSTR()
-        {
-            AssertExtensions.Throws<ArgumentNullException>("ptr", () => Marshal.PtrToStringBSTR(IntPtr.Zero));
-        }
-
-#if netcoreapp
-        [Fact]
-        public void PtrToStringUTF8()
-        {
-            Assert.Null(Marshal.PtrToStringUTF8(IntPtr.Zero, 123));
-            Assert.Throws<ArgumentOutOfRangeException>(() => 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 (file)
index 0000000..bf72a55
--- /dev/null
@@ -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<ArgumentOutOfRangeException>("byteLen", null, () => Marshal.PtrToStringUTF8(new IntPtr(123), -77));
+            AssertExtensions.Throws<ArgumentOutOfRangeException>("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 (file)
index 0000000..4d4be3d
--- /dev/null
@@ -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<ArgumentNullException>("ptr", () => Marshal.PtrToStringUni(IntPtr.Zero, 123));
+        }
+
+        [Fact]
+        public void PtrToStringUni_NegativeLength_ThrowsArgumentExeption()
+        {
+            AssertExtensions.Throws<ArgumentException>("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 (file)
index 0000000..72ffb06
--- /dev/null
@@ -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 (file)
index 0000000..f1712d3
--- /dev/null
@@ -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 (file)
index 0000000..71d46f6
--- /dev/null
@@ -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 (file)
index 0000000..bc96677
--- /dev/null
@@ -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 (file)
index 0000000..012e856
--- /dev/null
@@ -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;
+        }
+    }
+}