1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
//
// System.Runtime.InteropServices.Marshal Test Cases
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
//
using NUnit.Framework;
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace MonoTests.System.Runtime.InteropServices
{
[TestFixture]
public class MarshalTest
{
[StructLayout (LayoutKind.Sequential)]
class ClsSequential {
public int field;
}
class ClsNoLayout {
public int field;
}
[StructLayout (LayoutKind.Explicit)]
class ClsExplicit {
[FieldOffset (0)] public int field;
}
[StructLayout (LayoutKind.Sequential)]
struct StrSequential {
public int field;
}
struct StrNoLayout {
public int field;
}
[StructLayout (LayoutKind.Explicit)]
struct StrExplicit {
[FieldOffset (0)] public int field;
}
[Test]
public void ClassSequential ()
{
Marshal.SizeOf (typeof (ClsSequential));
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ClassNoLayout ()
{
Marshal.SizeOf (typeof (ClsNoLayout));
}
[Test]
public void ClassExplicit ()
{
Marshal.SizeOf (typeof (ClsExplicit));
}
[Test]
public void StructSequential ()
{
Marshal.SizeOf (typeof (StrSequential));
}
[Test]
public void StructNoLayout ()
{
Marshal.SizeOf (typeof (StrNoLayout));
}
[Test]
public void StructExplicit ()
{
Marshal.SizeOf (typeof (StrExplicit));
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ArrayType ()
{
Marshal.SizeOf (typeof (string[]));
}
[Test]
public void PtrToStringWithNull ()
{
Assert.IsNull (Marshal.PtrToStringAnsi (IntPtr.Zero), "A");
Assert.IsNull (Marshal.PtrToStringUni (IntPtr.Zero), "C");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void PtrToStringWithNullThrow ()
{
Assert.IsNull (Marshal.PtrToStringAnsi (IntPtr.Zero, 0), "B");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void PtrToStringWithNullThrow2 ()
{
Assert.IsNull (Marshal.PtrToStringUni (IntPtr.Zero, 0), "D");
}
[Test]
public unsafe void UnsafeAddrOfPinnedArrayElement () {
short[] sarr = new short [5];
sarr [2] = 3;
IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement (sarr, 2);
Assert.AreEqual (3, *(short*)ptr.ToPointer ());
}
[Test]
public void AllocHGlobalZeroSize () {
IntPtr ptr = Marshal.AllocHGlobal (0);
Assert.IsTrue (ptr != IntPtr.Zero);
Marshal.FreeHGlobal (ptr);
}
struct Foo {
int a;
static int b;
long c;
static char d;
int e;
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void OffsetOfStatic () {
Marshal.OffsetOf (typeof (Foo), "b");
}
// bug #76123
[Test]
public void StringToHGlobalUni () {
IntPtr handle = Marshal.StringToHGlobalUni ("unicode data");
string s = Marshal.PtrToStringUni (handle);
Assert.AreEqual (12, s.Length, "#1");
handle = Marshal.StringToHGlobalUni ("unicode data string");
s = Marshal.PtrToStringUni (handle);
Assert.AreEqual (19, s.Length, "#2");
}
[Test]
public void ReadInt32_Endian ()
{
IntPtr ptr = Marshal.AllocHGlobal (4);
try {
Marshal.WriteByte (ptr, 0, 0x01);
Marshal.WriteByte (ptr, 1, 0x02);
Marshal.WriteByte (ptr, 2, 0x03);
Marshal.WriteByte (ptr, 3, 0x04);
// Marshal MUST use the native CPU data
if (BitConverter.IsLittleEndian){
Assert.AreEqual (0x04030201, Marshal.ReadInt32 (ptr), "ReadInt32");
} else {
Assert.AreEqual (0x01020304, Marshal.ReadInt32 (ptr), "ReadInt32");
}
}
finally {
Marshal.FreeHGlobal (ptr);
}
}
#if NET_2_0
private const string NotSupported = "Not supported before Windows 2000 Service Pack 3";
private static char[] PlainText = new char[] { 'a', 'b', 'c' };
private static byte[] AsciiPlainText = new byte[] { (byte) 'a', (byte) 'b', (byte) 'c' };
private unsafe SecureString GetSecureString ()
{
fixed (char* p = &PlainText[0]) {
return new SecureString (p, PlainText.Length);
}
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void SecureStringToBSTR_Null ()
{
Marshal.SecureStringToBSTR (null);
}
[Test]
public void SecureStringToBSTR ()
{
try {
SecureString ss = GetSecureString ();
IntPtr p = Marshal.SecureStringToBSTR (ss);
char[] decrypted = new char[ss.Length];
Marshal.Copy (p, decrypted, 0, decrypted.Length);
Assert.AreEqual (PlainText, decrypted, "Decrypted");
Marshal.ZeroFreeBSTR (p);
}
catch (NotSupportedException) {
Assert.Ignore (NotSupported);
}
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void SecureStringToCoTaskMemAnsi_Null ()
{
Marshal.SecureStringToCoTaskMemAnsi (null);
}
[Test]
public void SecureStringToCoTaskMemAnsi ()
{
try {
SecureString ss = GetSecureString ();
IntPtr p = Marshal.SecureStringToCoTaskMemAnsi (ss);
byte[] decrypted = new byte[ss.Length];
Marshal.Copy (p, decrypted, 0, decrypted.Length);
Assert.AreEqual (AsciiPlainText, decrypted, "Decrypted");
Marshal.ZeroFreeCoTaskMemAnsi (p);
}
catch (NotSupportedException) {
Assert.Ignore (NotSupported);
}
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void SecureStringToCoTaskMemUnicode_Null ()
{
Marshal.SecureStringToCoTaskMemUnicode (null);
}
[Test]
public void SecureStringToCoTaskMemUnicode ()
{
try {
SecureString ss = GetSecureString ();
IntPtr p = Marshal.SecureStringToCoTaskMemUnicode (ss);
char[] decrypted = new char[ss.Length];
Marshal.Copy (p, decrypted, 0, decrypted.Length);
Assert.AreEqual (PlainText, decrypted, "Decrypted");
Marshal.ZeroFreeCoTaskMemUnicode (p);
}
catch (NotSupportedException) {
Assert.Ignore (NotSupported);
}
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void SecureStringToGlobalAllocAnsi_Null ()
{
Marshal.SecureStringToGlobalAllocAnsi (null);
}
[Test]
public void SecureStringToGlobalAllocAnsi ()
{
try {
SecureString ss = GetSecureString ();
IntPtr p = Marshal.SecureStringToGlobalAllocAnsi (ss);
byte[] decrypted = new byte[ss.Length];
Marshal.Copy (p, decrypted, 0, decrypted.Length);
Assert.AreEqual (AsciiPlainText, decrypted, "Decrypted");
Marshal.ZeroFreeGlobalAllocAnsi (p);
}
catch (NotSupportedException) {
Assert.Ignore (NotSupported);
}
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void SecureStringToGlobalAllocUnicode_Null ()
{
Marshal.SecureStringToGlobalAllocUnicode (null);
}
[Test]
public void SecureStringToGlobalAllocUnicode ()
{
try {
SecureString ss = GetSecureString ();
IntPtr p = Marshal.SecureStringToGlobalAllocUnicode (ss);
char[] decrypted = new char[ss.Length];
Marshal.Copy (p, decrypted, 0, decrypted.Length);
Assert.AreEqual (PlainText, decrypted, "Decrypted");
Marshal.ZeroFreeGlobalAllocUnicode (p);
}
catch (NotSupportedException) {
Assert.Ignore (NotSupported);
}
}
#endif
[Test]
public void TestGetComSlotForMethodInfo ()
{
Assert.AreEqual (7, Marshal.GetComSlotForMethodInfo(typeof(ITestDefault).GetMethod("DoNothing")));
Assert.AreEqual (7, Marshal.GetComSlotForMethodInfo(typeof(ITestDual).GetMethod("DoNothing")));
Assert.AreEqual (7, Marshal.GetComSlotForMethodInfo (typeof(ITestDefault).GetMethod ("DoNothing")));
Assert.AreEqual (3, Marshal.GetComSlotForMethodInfo (typeof(ITestUnknown).GetMethod ("DoNothing")));
for (int i = 0; i < 10; i++)
Assert.AreEqual (7+i, Marshal.GetComSlotForMethodInfo(typeof(ITestInterface).GetMethod ("Method"+i.ToString())));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestGetComSlotForMethodInfoNullException()
{
Marshal.GetComSlotForMethodInfo (null);
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void TestGetComSlotForMethodInfoArgumentException2 ()
{
Marshal.GetComSlotForMethodInfo (typeof(TestCoClass).GetMethod ("DoNothing"));
}
[Test]
public void TestPtrToStringAuto ()
{
string input = Guid.NewGuid ().ToString ();
string output;
string output2;
int len = 4;
IntPtr ptr;
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
// Auto -> Uni
ptr = Marshal.StringToHGlobalAuto (input);
output = Marshal.PtrToStringUni (ptr);
output2 = Marshal.PtrToStringUni (ptr, len);
} else {
// Auto -> Ansi
ptr = Marshal.StringToHGlobalAuto (input);
output = Marshal.PtrToStringAnsi (ptr);
output2 = Marshal.PtrToStringAnsi (ptr, len);
}
try {
Assert.AreEqual (input, output, "#1");
Assert.AreEqual (input.Substring (0, len), output2, "#2");
} finally {
Marshal.FreeHGlobal (ptr);
}
}
}
[ComImport()]
[Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")]
interface ITestDefault
{
void DoNothing ();
}
[ComImport()]
[Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
interface ITestDispatch
{
void DoNothing ();
}
[ComImport()]
[Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
interface ITestDual
{
void DoNothing ();
}
[ComImport()]
[Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ITestUnknown
{
void DoNothing ();
}
[ComImport()]
[Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA")]
interface ITestInterface
{
void Method0 ();
void Method1 ();
void Method2 ();
void Method3 ();
void Method4 ();
void Method5 ();
void Method6 ();
void Method7 ();
void Method8 ();
void Method9 ();
}
public class TestCoClass : ITestDispatch
{
public void DoNothing ()
{
}
}
}
|