<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" />
--- /dev/null
+// 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);
+ }
+ }
+}
--- /dev/null
+// 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);
+ }
+ }
+}
--- /dev/null
+// 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);
+ }
+ }
+}
--- /dev/null
+// 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));
+ }
+ }
+}
--- /dev/null
+// 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));
+ }
+ }
+}
+++ /dev/null
-// 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
- }
-}
--- /dev/null
+// 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));
+ }
+ }
+}
--- /dev/null
+// 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));
+ }
+ }
+}
--- /dev/null
+// 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;
+ }
+ }
+}
--- /dev/null
+// 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);
+ }
+ }
+}
--- /dev/null
+// 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;
+ }
+ }
+}
--- /dev/null
+// 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;
+ }
+ }
+}
--- /dev/null
+// 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;
+ }
+ }
+}