2 // System.Runtime.InteropServices.Marshal Test Cases
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
11 using NUnit.Framework;
13 using System.Runtime.InteropServices;
14 using System.Security;
16 namespace MonoTests.System.Runtime.InteropServices
19 public class MarshalTest
21 [StructLayout (LayoutKind.Sequential)]
30 [StructLayout (LayoutKind.Explicit)]
32 [FieldOffset (0)] public int field;
35 [StructLayout (LayoutKind.Sequential)]
36 struct StrSequential {
44 [StructLayout (LayoutKind.Explicit)]
46 [FieldOffset (0)] public int field;
50 public void ClassSequential ()
52 Marshal.SizeOf (typeof (ClsSequential));
56 [ExpectedException (typeof (ArgumentException))]
57 public void ClassNoLayout ()
59 Marshal.SizeOf (typeof (ClsNoLayout));
63 public void ClassExplicit ()
65 Marshal.SizeOf (typeof (ClsExplicit));
69 public void StructSequential ()
71 Marshal.SizeOf (typeof (StrSequential));
75 public void StructNoLayout ()
77 Marshal.SizeOf (typeof (StrNoLayout));
81 public void StructExplicit ()
83 Marshal.SizeOf (typeof (StrExplicit));
87 [ExpectedException (typeof (ArgumentException))]
88 public void ArrayType ()
90 Marshal.SizeOf (typeof (string[]));
94 public void PtrToStringWithNull ()
96 Assert.IsNull (Marshal.PtrToStringAnsi (IntPtr.Zero), "A");
97 Assert.IsNull (Marshal.PtrToStringUni (IntPtr.Zero), "C");
101 [ExpectedException (typeof (ArgumentNullException))]
102 public void PtrToStringWithNullThrow ()
104 Assert.IsNull (Marshal.PtrToStringAnsi (IntPtr.Zero, 0), "B");
108 [ExpectedException (typeof (ArgumentNullException))]
109 public void PtrToStringWithNullThrow2 ()
111 Assert.IsNull (Marshal.PtrToStringUni (IntPtr.Zero, 0), "D");
115 public unsafe void UnsafeAddrOfPinnedArrayElement () {
116 short[] sarr = new short [5];
119 IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement (sarr, 2);
120 Assert.AreEqual (3, *(short*)ptr.ToPointer ());
124 public void AllocHGlobalZeroSize () {
125 IntPtr ptr = Marshal.AllocHGlobal (0);
126 Assert.IsTrue (ptr != IntPtr.Zero);
127 Marshal.FreeHGlobal (ptr);
139 [ExpectedException (typeof (ArgumentException))]
140 public void OffsetOfStatic () {
141 Marshal.OffsetOf (typeof (Foo), "b");
146 public void StringToHGlobalUni () {
147 IntPtr handle = Marshal.StringToHGlobalUni ("unicode data");
148 string s = Marshal.PtrToStringUni (handle);
149 Assert.AreEqual (12, s.Length, "#1");
151 handle = Marshal.StringToHGlobalUni ("unicode data string");
152 s = Marshal.PtrToStringUni (handle);
153 Assert.AreEqual (19, s.Length, "#2");
157 public void ReadInt32_Endian ()
159 IntPtr ptr = Marshal.AllocHGlobal (4);
161 Marshal.WriteByte (ptr, 0, 0x01);
162 Marshal.WriteByte (ptr, 1, 0x02);
163 Marshal.WriteByte (ptr, 2, 0x03);
164 Marshal.WriteByte (ptr, 3, 0x04);
165 // Marshal MUST use the native CPU data
166 if (BitConverter.IsLittleEndian){
167 Assert.AreEqual (0x04030201, Marshal.ReadInt32 (ptr), "ReadInt32");
169 Assert.AreEqual (0x01020304, Marshal.ReadInt32 (ptr), "ReadInt32");
173 Marshal.FreeHGlobal (ptr);
178 private const string NotSupported = "Not supported before Windows 2000 Service Pack 3";
179 private static char[] PlainText = new char[] { 'a', 'b', 'c' };
180 private static byte[] AsciiPlainText = new byte[] { (byte) 'a', (byte) 'b', (byte) 'c' };
182 private unsafe SecureString GetSecureString ()
184 fixed (char* p = &PlainText[0]) {
185 return new SecureString (p, PlainText.Length);
190 [ExpectedException (typeof (ArgumentNullException))]
191 public void SecureStringToBSTR_Null ()
193 Marshal.SecureStringToBSTR (null);
197 public void SecureStringToBSTR ()
200 SecureString ss = GetSecureString ();
201 IntPtr p = Marshal.SecureStringToBSTR (ss);
203 char[] decrypted = new char[ss.Length];
204 Marshal.Copy (p, decrypted, 0, decrypted.Length);
205 Assert.AreEqual (PlainText, decrypted, "Decrypted");
207 Marshal.ZeroFreeBSTR (p);
209 catch (NotSupportedException) {
210 Assert.Ignore (NotSupported);
215 [ExpectedException (typeof (ArgumentNullException))]
216 public void SecureStringToCoTaskMemAnsi_Null ()
218 Marshal.SecureStringToCoTaskMemAnsi (null);
222 public void SecureStringToCoTaskMemAnsi ()
225 SecureString ss = GetSecureString ();
226 IntPtr p = Marshal.SecureStringToCoTaskMemAnsi (ss);
228 byte[] decrypted = new byte[ss.Length];
229 Marshal.Copy (p, decrypted, 0, decrypted.Length);
230 Assert.AreEqual (AsciiPlainText, decrypted, "Decrypted");
232 Marshal.ZeroFreeCoTaskMemAnsi (p);
234 catch (NotSupportedException) {
235 Assert.Ignore (NotSupported);
240 [ExpectedException (typeof (ArgumentNullException))]
241 public void SecureStringToCoTaskMemUnicode_Null ()
243 Marshal.SecureStringToCoTaskMemUnicode (null);
247 public void SecureStringToCoTaskMemUnicode ()
250 SecureString ss = GetSecureString ();
251 IntPtr p = Marshal.SecureStringToCoTaskMemUnicode (ss);
253 char[] decrypted = new char[ss.Length];
254 Marshal.Copy (p, decrypted, 0, decrypted.Length);
255 Assert.AreEqual (PlainText, decrypted, "Decrypted");
257 Marshal.ZeroFreeCoTaskMemUnicode (p);
259 catch (NotSupportedException) {
260 Assert.Ignore (NotSupported);
265 [ExpectedException (typeof (ArgumentNullException))]
266 public void SecureStringToGlobalAllocAnsi_Null ()
268 Marshal.SecureStringToGlobalAllocAnsi (null);
272 public void SecureStringToGlobalAllocAnsi ()
275 SecureString ss = GetSecureString ();
276 IntPtr p = Marshal.SecureStringToGlobalAllocAnsi (ss);
278 byte[] decrypted = new byte[ss.Length];
279 Marshal.Copy (p, decrypted, 0, decrypted.Length);
280 Assert.AreEqual (AsciiPlainText, decrypted, "Decrypted");
282 Marshal.ZeroFreeGlobalAllocAnsi (p);
284 catch (NotSupportedException) {
285 Assert.Ignore (NotSupported);
290 [ExpectedException (typeof (ArgumentNullException))]
291 public void SecureStringToGlobalAllocUnicode_Null ()
293 Marshal.SecureStringToGlobalAllocUnicode (null);
297 public void SecureStringToGlobalAllocUnicode ()
300 SecureString ss = GetSecureString ();
301 IntPtr p = Marshal.SecureStringToGlobalAllocUnicode (ss);
303 char[] decrypted = new char[ss.Length];
304 Marshal.Copy (p, decrypted, 0, decrypted.Length);
305 Assert.AreEqual (PlainText, decrypted, "Decrypted");
307 Marshal.ZeroFreeGlobalAllocUnicode (p);
309 catch (NotSupportedException) {
310 Assert.Ignore (NotSupported);
316 public void TestGetComSlotForMethodInfo ()
318 Assert.AreEqual (7, Marshal.GetComSlotForMethodInfo(typeof(ITestDefault).GetMethod("DoNothing")));
319 Assert.AreEqual (7, Marshal.GetComSlotForMethodInfo(typeof(ITestDual).GetMethod("DoNothing")));
320 Assert.AreEqual (7, Marshal.GetComSlotForMethodInfo (typeof(ITestDefault).GetMethod ("DoNothing")));
321 Assert.AreEqual (3, Marshal.GetComSlotForMethodInfo (typeof(ITestUnknown).GetMethod ("DoNothing")));
323 for (int i = 0; i < 10; i++)
324 Assert.AreEqual (7+i, Marshal.GetComSlotForMethodInfo(typeof(ITestInterface).GetMethod ("Method"+i.ToString())));
328 [ExpectedException(typeof(ArgumentNullException))]
329 public void TestGetComSlotForMethodInfoNullException()
331 Marshal.GetComSlotForMethodInfo (null);
335 [ExpectedException(typeof(ArgumentException))]
336 public void TestGetComSlotForMethodInfoArgumentException2 ()
338 Marshal.GetComSlotForMethodInfo (typeof(TestCoClass).GetMethod ("DoNothing"));
342 public void TestPtrToStringAuto ()
344 string input = Guid.NewGuid ().ToString ();
350 if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
352 ptr = Marshal.StringToHGlobalAuto (input);
353 output = Marshal.PtrToStringUni (ptr);
354 output2 = Marshal.PtrToStringUni (ptr, len);
357 ptr = Marshal.StringToHGlobalAuto (input);
358 output = Marshal.PtrToStringAnsi (ptr);
359 output2 = Marshal.PtrToStringAnsi (ptr, len);
363 Assert.AreEqual (input, output, "#1");
364 Assert.AreEqual (input.Substring (0, len), output2, "#2");
366 Marshal.FreeHGlobal (ptr);
372 [Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")]
373 interface ITestDefault
379 [Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")]
380 [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
381 interface ITestDispatch
387 [Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")]
388 [InterfaceType(ComInterfaceType.InterfaceIsDual)]
395 [Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")]
396 [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
397 interface ITestUnknown
403 [Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")]
404 interface ITestInterface
418 public class TestCoClass : ITestDispatch
420 public void DoNothing ()