[corlib] Fix tests for watchOS. (#3131)
[mono.git] / mcs / class / corlib / Test / System.Reflection / AssemblyTest.cs
index fe18b8ee8db18209e2873aec02212b3bcb1c8f1d..bc3416441cfad03b93e428301df40573aaff264e 100644 (file)
@@ -5,9 +5,11 @@
 //     Gonzalo Paniagua Javier (gonzalo@ximian.com)
 //     Philippe Lavoie (philippe.lavoie@cactus.ca)
 //     Sebastien Pouliot (sebastien@ximian.com)
+//      Aleksey Kliger (aleksey@xamarin.com)
 //
 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2015 Xamarin, Inc. (http://www.xamarin.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -35,15 +37,19 @@ using System.Configuration.Assemblies;
 using System.Globalization;
 using System.IO;
 using System.Reflection;
-#if !MONOTOUCH
+#if !MONOTOUCH && !MOBILE_STATIC
 using System.Reflection.Emit;
 #endif
 using System.Threading;
 using System.Runtime.Serialization;
+using System.Runtime.CompilerServices;
 using System.Security;
 using System.Linq;
 using System.Resources;
 
+// Used by GetType_TypeForwarder_Nested ()
+[assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(System.Globalization.CultureInfo))]
+
 namespace MonoTests.System.Reflection
 {
        [TestFixture]
@@ -89,13 +95,58 @@ namespace MonoTests.System.Reflection
                }
 
                [Test] // bug #49114
-               [Category ("NotWorking")]
                [ExpectedException (typeof (ArgumentException))]
                public void GetType_TypeName_Invalid () 
                {
                        typeof (int).Assembly.GetType ("&blabla", true, true);
                }
 
+               [Test] // bug #17571
+               [ExpectedException (typeof (ArgumentException))]
+               public void GetType_Invalid_RefPtr () {
+                       typeof (int).Assembly.GetType ("System.Int32&*", true, true);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void GetType_Invalid_RefArray () {
+                       typeof (int).Assembly.GetType ("System.Int32&[]", true, true);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void GetType_Invalid_RefGeneric () {
+                       typeof (int).Assembly.GetType ("System.Tuple`1&[System.Int32]", true, true);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void GetType_Invalid_PtrGeneric () {
+                       typeof (int).Assembly.GetType ("System.Tuple`1*[System.Int32]", true, true);
+               }
+
+               [Test]
+               public void GetType_ComposeModifiers () {
+                       var a = typeof(int).Assembly;
+
+                       var e1 = typeof (Int32).MakePointerType().MakeByRefType();
+                       var t1 = a.GetType ("System.Int32*&", true, true);
+                       Assert.AreEqual (e1, t1, "#1");
+
+                       var e2 = typeof (Int32).MakeArrayType(2).MakeByRefType();
+                       var t2 = a.GetType ("System.Int32[,]&", true, true);
+                       Assert.AreEqual (e2, t2, "#2");
+
+                       var e3 = typeof (Int32).MakePointerType().MakeArrayType();
+                       var t3 = a.GetType ("System.Int32*[]", true, true);
+                       Assert.AreEqual (e3, t3, "#3");
+
+                       var e4 = typeof (Int32).MakeArrayType().MakePointerType().MakePointerType().MakeArrayType().MakePointerType().MakeByRefType();
+                       var t4 = a.GetType ("System.Int32[]**[]*&", true, true);
+                       Assert.AreEqual (e4, t4, "#4");
+                               
+               }
+               
                [Test] // bug #334203
                public void GetType_TypeName_AssemblyName ()
                {
@@ -117,13 +168,24 @@ namespace MonoTests.System.Reflection
                        Assert.IsNull (type, "#B2");
                }
 
+               [Test]
+               public void GetType_TypeForwarder_Nested () {
+                       // System.Globalization is a PCL assembly
+                       Type t = typeof (AssemblyTest).Assembly.GetType ("System.Globalization.CultureInfo/Data");
+                       Assert.IsNotNull (t);
+                       Assert.AreEqual ("System.Globalization.CultureInfo+Data", t.FullName);
+               }
+
                [Test]
                public void GetEntryAssembly ()
                {
                        // note: only available in default appdomain
                        // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
                        // Not sure we should emulate this behavior.
-#if !MONODROID
+#if __WATCHOS__
+                       Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
+                       Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
+#elif !MONODROID
                        string fname = AppDomain.CurrentDomain.FriendlyName;
                        if (fname.EndsWith (".dll")) { // nunit-console
                                Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
@@ -138,7 +200,7 @@ namespace MonoTests.System.Reflection
 #endif
                }
 
-#if !MONOTOUCH // Reflection.Emit is not supported.
+#if !MONOTOUCH && !MOBILE_STATIC // Reflection.Emit is not supported.
                [Test]
                [Category("AndroidNotWorking")] // Missing Mono.CompilerServices.SymbolWriter
                public void GetModules_MissingFile ()
@@ -190,7 +252,7 @@ namespace MonoTests.System.Reflection
                public void Corlib_test ()
                {
                        Assembly corlib_test = Assembly.GetExecutingAssembly ();
-#if MONODROID
+#if MONODROID || MOBILE_STATIC || __WATCHOS__
                        Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
                        Assert.IsNull (corlib_test.Evidence, "Evidence");
 #elif MOBILE
@@ -398,7 +460,18 @@ namespace MonoTests.System.Reflection
                [Test]
                public void LoadWithPartialName ()
                {
-                       string [] names = { "corlib_test_net_1_1", "corlib_test_net_2_0", "corlib_test_net_4_0", "corlib_test_net_4_5", "corlib_test_net_4_x", "corlib_plattest", "mscorlibtests", "BclTests" };
+// FIXME?
+// So the problem here is that if we load an assembly
+// in a fully aot mode and then cannot load the
+// dylib, we will assert. This test is incompatible
+// with the semantics of aot'ed assembly loading, as
+// aot may assert when loading. This assumes that it's
+// safe to greedly load everything.
+#if MOBILE_STATIC
+                       string [] names = { "mobile_static_corlib_test" };
+#else
+                       string [] names = { "corlib_test_net_1_1", "corlib_test_net_2_0", "corlib_test_net_4_0", "corlib_test_net_4_5", "net_4_x_corlib_test", "corlib_plattest", "mscorlibtests", "BclTests" };
+#endif
 
                        foreach (string s in names)
                                if (Assembly.LoadWithPartialName (s) != null)
@@ -443,7 +516,7 @@ namespace MonoTests.System.Reflection
                        }
                }
 
-#if !MONOTOUCH // Reflection.Emit is not supported.
+#if !MONOTOUCH && !MOBILE_STATIC // Reflection.Emit is not supported.
                [Test]
                public void Location_Empty() {
                        string assemblyFileName = Path.Combine (
@@ -1116,7 +1189,7 @@ namespace MonoTests.System.Reflection
 
                        Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
 
-#if !MONOTOUCH
+#if !MONOTOUCH && !MOBILE_STATIC
                        Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
 #endif
                        Assert.IsFalse (module.IsResource (), "#4");
@@ -1186,6 +1259,67 @@ namespace MonoTests.System.Reflection
                        } catch (ArgumentException) {}
                }
 
+               class GetCallingAssemblyCallee {
+                       static int _dummy;
+
+                       static void sideEffect () {
+                               _dummy++;
+                       }
+
+                       // GetCallingAssembly may see an unpredictable
+                       // view of the stack if it's called in tail
+                       // position, or if its caller or the caller's
+                       // caller is inlined.  So we put in a side
+                       // effect to get out of tail position, and we
+                       // tag the methods NoInlining to discourage
+                       // the inliner.
+                       [MethodImplAttribute (MethodImplOptions.NoInlining)]
+                       public static Assembly Leaf () {
+                               var a = Assembly.GetCallingAssembly ();
+                               sideEffect();
+                               return a;
+                       }
+
+                       [MethodImplAttribute (MethodImplOptions.NoInlining)]
+                       public static Assembly DirectCall () {
+                               var a = Leaf();
+                               sideEffect();
+                               return a;
+                       }
+
+                       [MethodImplAttribute (MethodImplOptions.NoInlining)]
+                       public static Assembly InvokeCall () {
+                               var ty = typeof (GetCallingAssemblyCallee);
+                               var mi = ty.GetMethod("Leaf");
+                               var o = mi.Invoke(null, null);
+                               sideEffect();
+                               return (Assembly)o;
+                       }
+               }
+
+               [Test]
+               public void GetCallingAssembly_Direct() {
+                       var a = GetCallingAssemblyCallee.DirectCall ();
+                       Assert.IsNotNull (a);
+
+                       Assert.AreEqual (GetType().Assembly, a);
+               }
+
+               [Test]
+               public void GetCallingAssembly_SkipsReflection () {
+                       // check that the calling assembly is this
+                       // one, not mscorlib (aka, the reflection
+                       // API).
+                       var a = GetCallingAssemblyCallee.InvokeCall ();
+                       Assert.IsNotNull (a);
+
+                       var invokeAssembly =
+                               typeof (MethodInfo).Assembly;
+                       Assert.AreNotEqual (invokeAssembly, a);
+
+                       Assert.AreEqual (GetType().Assembly, a);
+               }
+
 #if NET_4_5
                [Test]
                public void DefinedTypes_Equality ()
@@ -1196,6 +1330,19 @@ namespace MonoTests.System.Reflection
                        Assert.AreSame (x1, x2, "#1");
                }
 #endif
+
+               class MyAssembly : Assembly { }
+
+               [Test]
+               public void CustomAssemblyImplThrows ()
+               {
+                       var ma = new MyAssembly();
+                       try {
+                               ma.GetName ();
+                               Assert.Fail ("must throw");
+                       } catch (NotImplementedException){
+                       }
+               }
        }
 
        public class TestDefinedTypes