[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[mono.git] / mcs / class / corlib / Test / System / ActivatorTest.cs
1 //
2 // ActivatorTest.cs - NUnit Test Cases for System.Activator
3 //
4 // Authors:
5 //      Nick Drochak <ndrochak@gol.com>
6 //      Gert Driesen <drieseng@users.sourceforge.net>
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11
12 using System;
13 using System.Globalization;
14 using System.IO;
15 using System.Reflection;
16 #if !MONOTOUCH
17 using System.Reflection.Emit;
18 #endif
19 using System.Runtime.InteropServices;
20 using System.Runtime.Remoting;
21 using System.Runtime.Remoting.Channels;
22 using System.Security;
23 using System.Security.Permissions;
24
25 using NUnit.Framework;
26
27 // The class in this namespace is used by the main test class
28 namespace MonoTests.System.ActivatorTestInternal {
29
30         // We need a COM class to test the Activator class
31         [ComVisible (true)]
32         public class COMTest : MarshalByRefObject {
33
34                 private int id;
35                 public bool constructorFlag = false;
36
37                 public COMTest ()
38                 {
39                         id = 0;
40                 }
41
42                 public COMTest (int id)
43                 {
44                         this.id = id;
45                 }
46
47                 // This property is visible
48                 [ComVisible (true)]
49                 public int Id {
50                         get { return id; }
51                         set { id = value; }
52                 }
53         }
54
55         [ComVisible (false)]
56         public class NonCOMTest : COMTest {
57         }
58 }
59
60 namespace MonoTests.System {
61
62         using MonoTests.System.ActivatorTestInternal;
63
64         class CustomUserType : Type
65         {
66                 public override Assembly Assembly
67                 {
68                         get { throw new NotImplementedException (); }
69                 }
70
71                 public override string AssemblyQualifiedName
72                 {
73                         get { throw new NotImplementedException (); }
74                 }
75
76                 public override Type BaseType
77                 {
78                         get { throw new NotImplementedException (); }
79                 }
80
81                 public override string FullName
82                 {
83                         get { throw new NotImplementedException (); }
84                 }
85
86                 public override Guid GUID
87                 {
88                         get { throw new NotImplementedException (); }
89                 }
90
91                 protected override TypeAttributes GetAttributeFlagsImpl ()
92                 {
93                         throw new NotImplementedException ();
94                 }
95
96                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
97                 {
98                         throw new NotImplementedException ();
99                 }
100
101                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
102                 {
103                         throw new NotImplementedException ();
104                 }
105
106                 public override Type GetElementType ()
107                 {
108                         throw new NotImplementedException ();
109                 }
110
111                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
112                 {
113                         throw new NotImplementedException ();
114                 }
115
116                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
117                 {
118                         throw new NotImplementedException ();
119                 }
120
121                 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
122                 {
123                         throw new NotImplementedException ();
124                 }
125
126                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
127                 {
128                         throw new NotImplementedException ();
129                 }
130
131                 public override Type GetInterface (string name, bool ignoreCase)
132                 {
133                         throw new NotImplementedException ();
134                 }
135
136                 public override Type[] GetInterfaces ()
137                 {
138                         throw new NotImplementedException ();
139                 }
140
141                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
142                 {
143                         throw new NotImplementedException ();
144                 }
145
146                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
147                 {
148                         throw new NotImplementedException ();
149                 }
150
151                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
152                 {
153                         throw new NotImplementedException ();
154                 }
155
156                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
157                 {
158                         throw new NotImplementedException ();
159                 }
160
161                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
162                 {
163                         throw new NotImplementedException ();
164                 }
165
166                 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
167                 {
168                         throw new NotImplementedException ();
169                 }
170
171                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
172                 {
173                         throw new NotImplementedException ();
174                 }
175
176                 protected override bool HasElementTypeImpl ()
177                 {
178                         throw new NotImplementedException ();
179                 }
180
181                 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
182                 {
183                         throw new NotImplementedException ();
184                 }
185
186                 protected override bool IsArrayImpl ()
187                 {
188                         throw new NotImplementedException ();
189                 }
190
191                 protected override bool IsByRefImpl ()
192                 {
193                         throw new NotImplementedException ();
194                 }
195
196                 protected override bool IsCOMObjectImpl ()
197                 {
198                         throw new NotImplementedException ();
199                 }
200
201                 protected override bool IsPointerImpl ()
202                 {
203                         throw new NotImplementedException ();
204                 }
205
206                 protected override bool IsPrimitiveImpl ()
207                 {
208                         throw new NotImplementedException ();
209                 }
210
211                 public override Module Module
212                 {
213                         get { throw new NotImplementedException (); }
214                 }
215
216                 public override string Namespace
217                 {
218                         get { throw new NotImplementedException (); }
219                 }
220
221                 public override Type UnderlyingSystemType
222                 {
223                         get {
224                                 return this;
225                         }
226                 }
227
228                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
229                 {
230                         throw new NotImplementedException ();
231                 }
232
233                 public override object[] GetCustomAttributes (bool inherit)
234                 {
235                         throw new NotImplementedException ();
236                 }
237
238                 public override bool IsDefined (Type attributeType, bool inherit)
239                 {
240                         throw new NotImplementedException ();
241                 }
242
243                 public override string Name
244                 {
245                         get { throw new NotImplementedException (); }
246                 }
247         }
248
249
250         [TestFixture]
251         public class ActivatorTest {
252
253                 private string testLocation = typeof (ActivatorTest).Assembly.Location;
254
255                 [Test]
256                 public void CreateInstance_Type()
257                 {
258                         COMTest objCOMTest = (COMTest) Activator.CreateInstance (typeof (COMTest));
259                         Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", (objCOMTest.GetType ()).ToString (), "#A02");
260                 }
261
262                 [Test]
263                 [ExpectedException (typeof (ArgumentNullException))]
264                 public void CreateInstance_TypeNull ()
265                 {
266                         Activator.CreateInstance ((Type)null);
267                 }
268
269                 [Test]
270                 [ExpectedException (typeof (ArgumentException))]
271                 public void CreateInstance_CustomType ()
272                 {
273                         Activator.CreateInstance (new CustomUserType ());
274                 }
275
276                 [Test]
277                 public void CreateInstance_StringString ()
278                 {
279                         ObjectHandle objHandle = Activator.CreateInstance (null, "MonoTests.System.ActivatorTestInternal.COMTest");
280                         COMTest objCOMTest = (COMTest)objHandle.Unwrap ();
281                         objCOMTest.Id = 2;
282                         Assert.AreEqual (2, objCOMTest.Id, "#A03");
283                 }
284
285                 [Test]
286                 [ExpectedException (typeof (ArgumentNullException))]
287                 public void CreateInstance_StringNull ()
288                 {
289                         Activator.CreateInstance ((string)null, null);
290                 }
291
292                 [Test]
293                 [ExpectedException (typeof (TypeLoadException))]
294                 public void CreateInstance_StringTypeNameDoesNotExists ()
295                 {
296                         Activator.CreateInstance ((string)null, "MonoTests.System.ActivatorTestInternal.DoesntExistsCOMTest");
297                 }
298
299                 [Test]
300                 public void CreateInstance_TypeBool ()
301                 {
302                         COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), false);
303                         Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", objCOMTest.GetType ().ToString (), "#A04");
304                 }
305
306                 [Test]
307                 public void CreateInstance_TypeObjectArray ()
308                 {
309                         object[] objArray = new object[1] { 7 };
310                         COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), objArray);
311                         Assert.AreEqual (7, objCOMTest.Id, "#A05");
312                 }
313
314 #if !MONOTOUCH
315                 [Test]
316                 [ExpectedException (typeof (MissingMethodException))]
317                 public void CreateInstance_TypeBuilder ()
318                 {
319                         Type tb = typeof (TypeBuilder); // no public ctor - but why is it documented as NotSupportedException ?
320                         ConstructorInfo[] ctors = tb.GetConstructors (BindingFlags.Instance | BindingFlags.NonPublic);
321                         Activator.CreateInstance (tb, new object [ctors [0].GetParameters ().Length]);
322                 }
323
324                 [Test]
325                 [ExpectedException (typeof (NotSupportedException))]
326                 public void CreateInstance_TypedReference ()
327                 {
328                         Activator.CreateInstance (typeof (TypedReference), null);
329                 }
330
331                 [Test]
332                 [ExpectedException (typeof (NotSupportedException))]
333                 public void CreateInstance_ArgIterator ()
334                 {
335                         Activator.CreateInstance (typeof (ArgIterator), null);
336                 }
337 #endif
338
339                 [Test]
340                 [ExpectedException (typeof (NotSupportedException))]
341                 public void CreateInstance_Void ()
342                 {
343                         Activator.CreateInstance (typeof (void), null);
344                 }
345
346                 [Test]
347                 [ExpectedException (typeof (NotSupportedException))]
348                 public void CreateInstance_RuntimeArgumentHandle ()
349                 {
350                         Activator.CreateInstance (typeof (RuntimeArgumentHandle), null);
351                 }
352
353                 [Test]
354                 [ExpectedException (typeof (NotSupportedException))]
355                 public void CreateInstance_NotMarshalByReferenceWithActivationAttributes ()
356                 {
357                         Activator.CreateInstance (typeof (object), null, new object[1] { null });
358                 }
359
360                 // TODO: Implemente the test methods for all the overriden functions using activationAttribute
361
362                 [Test]
363                 [ExpectedException(typeof(MissingMethodException))]
364                 public void CreateInstanceAbstract1 () 
365                 {
366                         Activator.CreateInstance (typeof (Type));
367                 }
368
369                 [Test]
370                 [ExpectedException(typeof(MissingMethodException))]
371                 public void CreateInstanceAbstract2 () 
372                 {
373                         Activator.CreateInstance (typeof (Type), true);
374                 }
375
376                 [Test]
377                 [ExpectedException(typeof(MissingMethodException))]
378                 public void CreateInstanceAbstract3 () 
379                 {
380                         Activator.CreateInstance (typeof (Type), null, null);
381                 }
382
383                 [Test]
384                 [ExpectedException(typeof(MissingMethodException))]
385                 public void CreateInstanceAbstract4() 
386                 {
387                         Activator.CreateInstance (typeof (Type), BindingFlags.CreateInstance | (BindingFlags.Public | BindingFlags.Instance), null, null, CultureInfo.InvariantCulture, null);
388                 }
389
390                 [Test]
391                 [ExpectedException (typeof (MissingMethodException))]
392                 public void CreateInstanceAbstract5 () 
393                 {
394                         Activator.CreateInstance (typeof (Type), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);
395                 }
396
397                 [Test]
398                 public void CreateInstance_Nullable ()
399                 {
400                         Assert.AreEqual (5, Activator.CreateInstance (typeof (Nullable<int>), new object [] { 5 }));
401                         Assert.AreEqual (typeof (int), Activator.CreateInstance (typeof (Nullable<int>), new object [] { 5 }).GetType ());
402                         Assert.AreEqual (0, Activator.CreateInstance (typeof (Nullable<int>), new object [] { null }));
403                         Assert.AreEqual (typeof (int), Activator.CreateInstance (typeof (Nullable<int>), new object [] { null }).GetType ());
404                         Assert.AreEqual (null, Activator.CreateInstance (typeof (Nullable<int>)));
405                 }
406
407                 [Test]
408                 [ExpectedException (typeof (ArgumentNullException))]
409                 public void GetObject_TypeNull ()
410                 {
411                         Activator.GetObject (null, "tcp://localhost:1234/COMTestUri");
412                 }
413
414 #if !MOBILE
415                 [Test]
416                 [ExpectedException (typeof (ArgumentNullException))]
417                 public void GetObject_UrlNull ()
418                 {
419                         Activator.GetObject (typeof (COMTest), null);
420                 }
421 #endif
422
423 /* This test is now executed in System.Runtime.Remoting unit tests 
424                 [Test]
425                 public void GetObject ()
426                 {
427                         // This will provide a COMTest object on  tcp://localhost:1234/COMTestUri
428                         COMTest objCOMTest = new COMTest (8);
429                         TcpChannel chnServer = new TcpChannel (1234);
430                         ChannelServices.RegisterChannel (chnServer);
431                         RemotingServices.SetObjectUriForMarshal (objCOMTest, "COMTestUri");
432                         RemotingServices.Marshal (objCOMTest);
433
434                         // This will get the remoting object
435                         object objRem = Activator.GetObject (typeof (COMTest), "tcp://localhost:1234/COMTestUri");
436                         Assert.IsNotNull (objRem, "#A07");
437                         COMTest remCOMTest = (COMTest) objRem;
438                         Assert.AreEqual (8, remCOMTest.Id, "#A08");
439
440                         ChannelServices.UnregisterChannel(chnServer);
441                 }
442 */
443                 // TODO: Implemente the test methods for all the overriden function using activationAttribute
444
445                 [Test]
446                 public void CreateInstanceFrom ()
447                 {
448                         ObjectHandle objHandle = Activator.CreateInstanceFrom (testLocation, "MonoTests.System.ActivatorTestInternal.COMTest");
449                         Assert.IsNotNull (objHandle, "#A09");
450                         objHandle.Unwrap ();
451                         // TODO: Implement the test methods for all the overriden function using activationAttribute
452                 }
453
454 #if !MOBILE
455
456                 // note: this only ensure that the ECMA key support unification (more test required, outside corlib, for other keys, like MS final).
457                 private const string CorlibPermissionPattern = "System.Security.Permissions.FileDialogPermission, mscorlib, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";
458                 private const string SystemPermissionPattern = "System.Net.DnsPermission, System, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";
459                 private const string fx10version = "1.0.3300.0";
460                 private const string fx11version = "1.0.5000.0";
461                 private const string fx20version = "2.0.0.0";
462
463                 private static object[] psNone = new object [1] { PermissionState.None };
464
465                 private void Unification (string fullname)
466                 {
467                         Type t = Type.GetType (fullname);
468                         IPermission p = (IPermission)Activator.CreateInstance (t, psNone);
469                         string currentVersion = typeof (string).Assembly.GetName ().Version.ToString ();
470                         Assert.IsTrue ((p.ToString ().IndexOf (currentVersion) > 0), currentVersion);
471                 }
472
473                 [Test]
474                 public void Unification_FromFx10 ()
475                 {
476                         Unification (String.Format (CorlibPermissionPattern, fx10version));
477                         Unification (String.Format (SystemPermissionPattern, fx10version));
478                 }
479
480                 [Test]
481                 public void Unification_FromFx11 ()
482                 {
483                         Unification (String.Format (CorlibPermissionPattern, fx11version));
484                         Unification (String.Format (SystemPermissionPattern, fx11version));
485                 }
486
487                 [Test]
488                 public void Unification_FromFx20 ()
489                 {
490                         Unification (String.Format (CorlibPermissionPattern, fx20version));
491                         Unification (String.Format (SystemPermissionPattern, fx20version));
492                 }
493
494                 [Test]
495                 public void Unification_FromFx99_Corlib ()
496                 {
497                         Unification (String.Format (CorlibPermissionPattern, "9.99.999.9999"));
498                 }
499
500                 [Test]
501                 [Category ("NotWorking")]
502                 public void Unification_FromFx99_System ()
503                 {
504                         Assert.IsNull (Type.GetType (String.Format (SystemPermissionPattern, "9.99.999.9999")));
505                 }
506 #endif
507                 class foo2<T, U> {}
508                 class foo1<T> : foo2<T, int> {}
509
510                 [Test, ExpectedException (typeof (ArgumentException))]
511                 public void GenericType_Open1 ()
512                 {
513                         Activator.CreateInstance (typeof (foo2<,>));
514                 }
515                 [Test, ExpectedException (typeof (ArgumentException))]
516                 public void GenericType_Open2 ()
517                 {
518                         Activator.CreateInstance (typeof (foo1<>));
519                 }
520                 [Test]
521                 public void GenericTypes_Closed ()
522                 {
523                         Assert.IsNotNull (Activator.CreateInstance (typeof (foo1<int>)), "foo1<int>");
524                         Assert.IsNotNull (Activator.CreateInstance (typeof (foo2<long, int>)), "foo2<long, int>");
525                 }
526
527                 [Test]
528                 public void CreateInstanceCrossDomain ()
529                 {
530                         Activator.CreateInstance (AppDomain.CurrentDomain, "mscorlib.dll", "System.Object");
531                         Activator.CreateInstance (AppDomain.CurrentDomain, "mscorlib.dll", "System.Object", false,
532                                                   BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture,
533                                                   null, null);
534                 }
535
536 #if !MONOTOUCH
537                 [Test]
538                 public void CreateInstanceCustomDomain ()
539                 {
540                         // FIXME: below works as a standalone case, but does not as a unit test (causes JIT error).
541                         Activator.CreateInstance (AppDomain.CreateDomain ("foo"), "mscorlib.dll", "System.Object", false,
542                                                   BindingFlags.Public | BindingFlags.Instance, null, null, null,
543                                                   null, null);
544                 }
545 #endif
546                 [Test]
547                 public void CreateInstanceCrossDomainNonSerializableArgs ()
548                 {
549                         // I'm not sure why this is possible ...
550                         Activator.CreateInstance (AppDomain.CurrentDomain, "mscorlib.dll", "System.WeakReference", false,
551                                                   BindingFlags.Public | BindingFlags.Instance, null, new object [] {ModuleHandle.EmptyHandle}, null, null, null);
552                 }
553
554                 [Test]
555                 [ExpectedException (typeof (NotSupportedException))]
556                 public void CreateInstanceNonSerializableAtts ()
557                 {
558                         // even on invalid success it causes different exception though.
559                         Activator.CreateInstance ("mscorlib.dll", "System.Object", false,
560                                                   BindingFlags.Public | BindingFlags.Instance, null, null, null,
561                                                   new object [] {ModuleHandle.EmptyHandle}, null);
562                 }
563
564                 [Test]
565                 [ExpectedException (typeof (NotSupportedException))]
566                 public void CreateInstanceCrossDomainNonSerializableAtts ()
567                 {
568                         // even on invalid success it causes different exception though.
569                         Activator.CreateInstance (AppDomain.CurrentDomain, "mscorlib.dll", "System.Object", false,
570                                                   BindingFlags.Public | BindingFlags.Instance, null, null, null,
571                                                   new object [] {ModuleHandle.EmptyHandle}, null);
572                 }
573
574                 public class ParamsConstructor {
575
576                         public int A;
577                         public string X;
578                         public string Y;
579
580                         public ParamsConstructor (int a, params string [] s)
581                         {
582                                 A = a;
583
584                                 Assert.IsNotNull (s);
585
586                                 if (s.Length == 0)
587                                         return;
588
589                                 X = s [0];
590
591                                 if (s.Length == 1)
592                                         return;
593
594                                 Y = s [1];
595                         }
596                 }
597
598                 [Test]
599                 public void CreateInstanceParamsConstructor ()
600                 {
601                         var a = (ParamsConstructor) Activator.CreateInstance (
602                                 typeof (ParamsConstructor), new object [] { 42, "foo", "bar" });
603
604                         Assert.AreEqual (42, a.A);
605                         Assert.AreEqual ("foo", a.X);
606                         Assert.AreEqual ("bar", a.Y);
607
608                         a = (ParamsConstructor) Activator.CreateInstance (
609                                 typeof (ParamsConstructor), new object [] { 42, "foo" });
610
611                         Assert.AreEqual (42, a.A);
612                         Assert.AreEqual ("foo", a.X);
613                         Assert.AreEqual (null, a.Y);
614
615                         a = (ParamsConstructor) Activator.CreateInstance (
616                                 typeof (ParamsConstructor), new object [] { 42 });
617
618                         Assert.AreEqual (42, a.A);
619                         Assert.AreEqual (null, a.X);
620                         Assert.AreEqual (null, a.Y);
621
622                         var b = Activator.CreateInstance (typeof (SimpleParamsObjectConstructor), 1, 2, 3, 4, 5);
623                         Assert.IsNotNull (b);
624                 }
625
626                 class SimpleParamsObjectConstructor
627                 {
628                         public SimpleParamsObjectConstructor (params object[] parameters)
629                         {
630                         }
631                 }
632
633                 class SimpleParamsConstructor {
634
635                         public string X;
636                         public string Y;
637
638                         public SimpleParamsConstructor (params string [] s)
639                         {
640                                 Assert.IsNotNull (s);
641
642                                 if (s.Length == 0)
643                                         return;
644
645                                 X = s [0];
646
647                                 if (s.Length == 1)
648                                         return;
649
650                                 Y = s [1];
651                         }
652                 }
653
654                 [Test]
655                 public void CreateInstanceSimpleParamsConstructor ()
656                 {
657                         var a = (SimpleParamsConstructor) Activator.CreateInstance (
658                                 typeof (SimpleParamsConstructor), new object [] { "foo", "bar" });
659
660                         Assert.AreEqual ("foo", a.X);
661                         Assert.AreEqual ("bar", a.Y);
662
663                         a = (SimpleParamsConstructor) Activator.CreateInstance (
664                                 typeof (SimpleParamsConstructor), new object [0]);
665
666                         Assert.AreEqual (null, a.X);
667                         Assert.AreEqual (null, a.Y);
668                 }
669
670                 class ParamsConstructorWithObjectConversion
671                 {
672                         public ParamsConstructorWithObjectConversion (params int[] x)
673                         {
674                         }
675                 }
676
677                 [Test]
678                 public void CreateInstanceParamsConstructorWithObjectConversion ()
679                 {
680                         var a = Activator.CreateInstance (typeof(ParamsConstructorWithObjectConversion), new object[] { (object) 2 });
681                         Assert.IsNotNull (a);
682                 }
683         }
684 }