Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GCHandleTest.cs « System.Runtime.InteropServices « Test « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c7cb95ad325bdb1751c71ac40687a0ccc856533 (plain)
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
//
// System.Runtime.InteropServices.GCHandle Test Cases
//
// Authors:
// 	Paolo Molaro (lupus@ximian.com)
//
// Copyright (C) 2005, 2009 Novell, Inc (http://www.novell.com)
//

using NUnit.Framework;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Globalization;

namespace MonoTests.System.Runtime.InteropServices
{
	[TestFixture]
	public class GCHandleTest
	{
		// Expected warning, the tests that reference this handle are testing for the default values of the object
		#pragma warning disable 649
		static GCHandle handle;
		#pragma warning restore 649
		
		[Test]
		public void DefaultZeroValue_Allocated ()
		{
			Assert.IsFalse (handle.IsAllocated, "IsAllocated");
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void DefaultZeroValue_Target ()
		{
			Assert.IsNull (handle.Target, "Target");
		}

		[Test]
		public void AllocNull ()
		{
			IntPtr ptr = (IntPtr) GCHandle.Alloc (null);
			Assert.IsFalse (ptr == IntPtr.Zero, "ptr");
			GCHandle gch = (GCHandle) ptr;
			Assert.IsTrue (gch.IsAllocated, "IsAllocated");
			Assert.IsNull (gch.Target, "Target");
		}

		[Test]
		public void AllocNullWeakTrack ()
		{
			GCHandle gch = GCHandle.Alloc (null, GCHandleType.WeakTrackResurrection);
			Assert.IsTrue (gch.IsAllocated, "IsAllocated");
			Assert.IsNull (gch.Target, "Target");
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void AddrOfPinnedObjectNormal ()
		{
			GCHandle handle = GCHandle.Alloc (new Object (), GCHandleType.Normal);
			try {
				IntPtr ptr = handle.AddrOfPinnedObject();
			}
			finally {
				handle.Free();
			}
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void AddrOfPinnedObjectWeak ()
		{
			GCHandle handle = GCHandle.Alloc (new Object (), GCHandleType.Weak);
			try {
				IntPtr ptr = handle.AddrOfPinnedObject();
			}
			finally {
				handle.Free();
			}
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void AddrOfPinnedObjectWeakTrackResurrection ()
		{
			GCHandle handle = GCHandle.Alloc (new Object (), GCHandleType.WeakTrackResurrection);
			try {
				IntPtr ptr = handle.AddrOfPinnedObject();
			}
			finally {
				handle.Free();
			}
		}

		[Test]
		public void AddrOfPinnedObjectNull ()
		{
			GCHandle handle = GCHandle.Alloc (null, GCHandleType.Pinned);
			try {
				IntPtr ptr = handle.AddrOfPinnedObject();
				Assert.AreEqual (new IntPtr (0), ptr);
			}
			finally {
				handle.Free();
			}
		}

		[Test]
		[Ignore ("throw non-catchable ExecutionEngineException")]
		[ExpectedException (typeof (ExecutionEngineException))]
		public void AllocMinusOne ()
		{
			// -1 is a special value used by the mono runtime
			// looks like it's special too in MS CLR (since it will crash)
			GCHandle.Alloc (null, (GCHandleType) (-1));
		}

		[Test]
		public void AllocInvalidType ()
		{
			GCHandle gch = GCHandle.Alloc (null, (GCHandleType) Int32.MinValue);
			try {
				Assert.IsTrue (gch.IsAllocated, "IsAllocated");
				Assert.IsNull (gch.Target, "Target");
			}
			finally {
				gch.Free ();
			}
		}
#if !MONOTOUCH && !MOBILE_STATIC
		[Test]
		[Category("MobileNotWorking")] // SIGSEGV, probably on AppDomain.Unload
		public void WeakHandleWorksOnNonRootDomain ()
		{
			//Console.WriteLine("current app domain: " + AppDomain.CurrentDomain.Id);
			AppDomain domain = AppDomain.CreateDomain("testdomain");

			Assembly ea = Assembly.GetExecutingAssembly ();
			domain.CreateInstanceFrom (ea.CodeBase,
				typeof (AssemblyResolveHandler).FullName,
				false,
				BindingFlags.Public | BindingFlags.Instance,
				null,
				new object [] { ea.Location, ea.FullName },
				CultureInfo.InvariantCulture,
				null,
				null);


			var testerType = typeof (CrossDomainGCHandleRunner);
			var r = (CrossDomainGCHandleRunner)domain.CreateInstanceAndUnwrap (
				testerType.Assembly.FullName, testerType.FullName, false,
				BindingFlags.Public | BindingFlags.Instance, null, new object [0],
				CultureInfo.InvariantCulture, new object [0], null);


			Assert.IsTrue (r.RunTest (), "#1");
			AppDomain.Unload (domain);
		}

		public class CrossDomainGCHandleRunner : MarshalByRefObject {
			public bool RunTest () {
				object o = new object();
				GCHandle gcHandle = GCHandle.Alloc (o, GCHandleType.Weak);
				IntPtr intPtr = (IntPtr)gcHandle;
				
				try {
					object target = GCHandle.FromIntPtr(intPtr).Target;
					return true;
				} catch (Exception) {}
				return false;
			}
		}
		
		[Serializable ()]
		class AssemblyResolveHandler
		{
			public AssemblyResolveHandler (string assemblyFile, string assemblyName)
			{
				_assemblyFile = assemblyFile;
				_assemblyName = assemblyName;

				AppDomain.CurrentDomain.AssemblyResolve +=
					new ResolveEventHandler (ResolveAssembly);
			}

			private Assembly ResolveAssembly (object sender, ResolveEventArgs args)
			{
				if (args.Name == _assemblyName)
					return Assembly.LoadFrom (_assemblyFile);

				return null;
			}

			private readonly string _assemblyFile;
			private readonly string _assemblyName;
		}
#endif
	}

}