Esempio n. 1
0
        public static void ResolveEventMultipleCalls()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                int resolveHandlerCallCount = 0;

                Assembly basesAssembly = tl.LoadFromByteArray(TestData.s_BaseClassesImage);

                tl.Resolving +=
                    delegate(TypeLoader sender, AssemblyName name)
                {
                    resolveHandlerCallCount++;
                    return(basesAssembly);
                };

                Assembly derived = tl.LoadFromByteArray(TestData.s_DerivedClassWithVariationsOnFooImage);

                int expectedCount = 1;
                foreach (string typeName in new string[] { "Derived1", "Derived3", "Derived4", "Derived5", "Derived6" })
                {
                    Type t  = derived.GetType(typeName, throwOnError: true);
                    Type bt = t.BaseType;
                    Assert.Equal(basesAssembly, bt.Assembly);
                    Assert.Equal(expectedCount++, resolveHandlerCallCount);
                }
            }
        }
Esempio n. 2
0
 public static void CrossAssemblyTypeRefToNestedType()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a        = tl.LoadFromByteArray(TestData.s_AssemblyWithNestedTypeImage);
         Assembly n        = tl.LoadFromByteArray(TestData.s_AssemblyWithTypeRefToNestedTypeImage);
         Type     nt       = n.GetType("N", throwOnError: true);
         Type     bt       = nt.BaseType;
         Type     expected = a.GetType("Outer+Inner+ReallyInner", throwOnError: true);
         Assert.Equal(expected, bt);
     }
 }
Esempio n. 3
0
 public static void GetAssemblies_SnapshotIsAtomic()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a1 = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         IEnumerable <Assembly> loadedAssembliesSnapshot = tl.GetAssemblies();
         Assembly   a2 = tl.LoadFromByteArray(TestData.s_SimpleNameOnlyImage);
         Assembly[] loadedAssemblies = loadedAssembliesSnapshot.ToArray();
         Assert.Equal(1, loadedAssemblies.Length);
         Assert.Equal(a1, loadedAssemblies[0]);
     }
 }
Esempio n. 4
0
        public static void AssemblyGetForwardedTypes1()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                Assembly upper  = tl.LoadFromByteArray(TestData.s_UpperImage);
                Assembly middle = tl.LoadFromByteArray(TestData.s_MiddleImage);
                Assembly lower  = tl.LoadFromByteArray(TestData.s_LowerImage);

                Type[]   types     = upper.GetForwardedTypesThunk().OrderBy(t => t.FullName).ToArray();
                string[] fullNames = types.Select(t => t.FullName).ToArray();

                string[] expected = { "Middle2", "Upper2", "Upper3", "Upper3+Upper3a" };
                Assert.Equal <string>(expected, fullNames);
            }
        }
Esempio n. 5
0
        public static void GetAssemblies()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                Assembly[] loadedAssemblies = tl.GetAssemblies().ToArray();
                Assert.Equal(0, loadedAssemblies.Length);

                Assembly a1 = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
                Assembly a2 = tl.LoadFromByteArray(TestData.s_SimpleNameOnlyImage);
                loadedAssemblies = tl.GetAssemblies().ToArray();
                Assert.Equal(2, loadedAssemblies.Length);
                Assert.Contains <Assembly>(a1, loadedAssemblies);
                Assert.Contains <Assembly>(a2, loadedAssemblies);
            }
        }
Esempio n. 6
0
        public static void ModuleResolveEvent()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                Module moduleReturnedFromEventHandler = null;

                Assembly a = tl.LoadFromByteArray(TestData.s_MultiModuleDllImage);
                a.ModuleResolve +=
                    delegate(object sender, ResolveEventArgs e)
                {
                    Assert.Same(a, sender);
                    Assert.Null(moduleReturnedFromEventHandler);     // We're not doing anything to cause this to trigger twice!
                    Assert.Equal("Bob.netmodule", e.Name);
                    moduleReturnedFromEventHandler = a.LoadModule("Bob.netmodule", TestData.s_JoeNetModuleImage);
                    return(moduleReturnedFromEventHandler);
                };

                Module m = a.GetModule("Bob.netmodule");
                Assert.NotNull(m);
                Assert.Equal(moduleReturnedFromEventHandler, m);

                // Make sure the event doesn't get raised twice. For a single-threaded case like this, that's a reasonable assumption.
                Module m1 = a.GetModule("Bob.netmodule");
            }
        }
Esempio n. 7
0
        public static void AssemblyWithEmbeddedResources()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                Assembly a = tl.LoadFromByteArray(TestData.s_AssemblyWithEmbeddedResourcesImage);

                string[] names = a.GetManifestResourceNames().OrderBy(s => s).ToArray();
                Assert.Equal <string>(new string[] { "MyRes1", "MyRes2", "MyRes3" }, names);
                foreach (string name in names)
                {
                    ManifestResourceInfo mri = a.GetManifestResourceInfo(name);
                    Assert.Equal(ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile, mri.ResourceLocation);
                    Assert.Null(mri.FileName);
                    Assert.Null(mri.ReferencedAssembly);
                }

                using (Stream s = a.GetManifestResourceStream("MyRes1"))
                {
                    byte[] res = s.ToArray();
                    Assert.Equal <byte>(TestData.s_MyRes1, res);
                }

                using (Stream s = a.GetManifestResourceStream("MyRes2"))
                {
                    byte[] res = s.ToArray();
                    Assert.Equal <byte>(TestData.s_MyRes2, res);
                }

                using (Stream s = a.GetManifestResourceStream("MyRes3"))
                {
                    byte[] res = s.ToArray();
                    Assert.Equal <byte>(TestData.s_MyRes3, res);
                }
            }
        }
Esempio n. 8
0
        public static void ResolveEventReturnsSomething()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                bool     resolveHandlerCalled      = false;
                Assembly resolveEventHandlerResult = null;

                tl.Resolving +=
                    delegate(TypeLoader sender, AssemblyName name)
                {
                    Assert.Same(tl, sender);
                    Assert.Equal(name.Name, "Foo");
                    resolveHandlerCalled      = true;
                    resolveEventHandlerResult = sender.LoadFromByteArray(TestData.s_BaseClassesImage);
                    return(resolveEventHandlerResult);
                };

                Assembly derived = tl.LoadFromByteArray(TestData.s_DerivedClassWithVariationsOnFooImage);
                Type     t       = derived.GetType("Derived1", throwOnError: true);
                Type     bt      = t.BaseType;
                Assembly a       = bt.Assembly;
                Assert.True(resolveHandlerCalled);
                Assert.Equal(a, resolveEventHandlerResult);
            }
        }
Esempio n. 9
0
 public static void LoadFromByteArrayNull()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assert.Throws <ArgumentNullException>(() => tl.LoadFromByteArray(null));
     }
 }
Esempio n. 10
0
        public static void AssemblyName_GetName_SimpleNameOnly()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                Assembly     a  = tl.LoadFromByteArray(TestData.s_SimpleNameOnlyImage);
                AssemblyName an = a.GetName(copiedName: false);

                Assert.Equal("SimpleNameOnly", an.Name);

                Assert.Equal(AssemblyNameFlags.PublicKey, an.Flags);

                Version v = an.Version;
                Assert.NotNull(v);
                Assert.Equal(0, v.Major);
                Assert.Equal(0, v.Minor);
                Assert.Equal(0, v.Build);
                Assert.Equal(0, v.Revision);

                string cultureName = an.CultureName;
                Assert.Equal(string.Empty, cultureName);

                byte[] publicKey = an.GetPublicKey();
                Assert.Equal(0, publicKey.Length);

                Assert.Equal(AssemblyContentType.Default, an.ContentType);
                Assert.Equal(AssemblyHashAlgorithm.SHA1, an.HashAlgorithm);

                Assembly aAgain = tl.LoadFromAssemblyName(an);
                Assert.Equal(a, aAgain);
            }
        }
Esempio n. 11
0
        public unsafe static void TestCustomModifiers1()
        {
            using (TypeLoader tl = new TypeLoader("mscorlib"))
            {
                tl.Resolving +=
                    delegate(TypeLoader sender, AssemblyName name)
                {
                    if (name.Name == "mscorlib")
                    {
                        return(tl.LoadFromStream(TestUtils.CreateStreamForCoreAssembly()));
                    }
                    return(null);
                };

                Assembly a    = tl.LoadFromByteArray(TestData.s_CustomModifiersImage);
                Type     t    = a.GetType("N", throwOnError: true);
                Type     reqA = a.GetType("ReqA", throwOnError: true);
                Type     reqB = a.GetType("ReqB", throwOnError: true);
                Type     reqC = a.GetType("ReqC", throwOnError: true);
                Type     optA = a.GetType("OptA", throwOnError: true);
                Type     optB = a.GetType("OptB", throwOnError: true);
                Type     optC = a.GetType("OptC", throwOnError: true);

                MethodInfo    m   = t.GetMethod("MyMethod");
                ParameterInfo p   = m.GetParameters()[0];
                Type[]        req = p.GetRequiredCustomModifiers();
                Type[]        opt = p.GetOptionalCustomModifiers();

                Assert.Equal <Type>(new Type[] { reqA, reqB, reqC }, req);
                Assert.Equal <Type>(new Type[] { optA, optB, optC }, opt);

                TestUtils.AssertNewObjectReturnedEachTime(() => p.GetRequiredCustomModifiers());
                TestUtils.AssertNewObjectReturnedEachTime(() => p.GetOptionalCustomModifiers());
            }
        }
Esempio n. 12
0
        public static void ResolveEventFromReferencedAssembliesUsingFullPublicKeyReference()
        {
            // Ecma-335 allows an assembly reference to specify a full public key rather than the token. Ensure that those references
            // still hand out usable AssemblyNames to resolve handlers.
            using (TypeLoader tl = new TypeLoader())
            {
                AssemblyName assemblyNameReceivedByHandler = null;
                tl.Resolving +=
                    delegate(TypeLoader sender, AssemblyName name)
                {
                    assemblyNameReceivedByHandler = name;
                    return(null);
                };


                Assembly a = tl.LoadFromByteArray(TestData.s_AssemblyRefUsingFullPublicKeyImage);
                Type     t = a.GetType("C", throwOnError: true);

                // We expect this next to call to throw since it asks the TypeLoader to resolve [mscorlib]System.Object and our
                // resolve handler doesn't return anything for that.
                Assert.Throws <FileNotFoundException>(() => t.BaseType);

                // But it did get called with a request to resolve "mscorlib" and we got the correct PKT calculated from the PK.
                // Note that the original PK is not made available (which follows prior precedent with these apis.) It's not like
                // anyone binds with the full PK...
                Assert.NotNull(assemblyNameReceivedByHandler);
                byte[] expectedPkt = "b77a5c561934e089".HexToByteArray();
                byte[] actualPkt   = assemblyNameReceivedByHandler.GetPublicKeyToken();
                Assert.Equal <byte>(expectedPkt, actualPkt);
            }
        }
Esempio n. 13
0
        public static void GetLoadModules1()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                Assembly a = tl.LoadFromByteArray(TestData.s_MultiModuleDllImage);

                {
                    Module[] loadedModules = a.GetLoadedModules(getResourceModules: true);
                    Assert.Equal(1, loadedModules.Length);
                    Assert.Equal(a.ManifestModule, loadedModules[0]);
                }

                {
                    Module[] loadedModules = a.GetLoadedModules(getResourceModules: false);
                    Assert.Equal(1, loadedModules.Length);
                    Assert.Equal(a.ManifestModule, loadedModules[0]);
                }

                Module m1 = a.LoadModule("Bob.netmodule", TestData.s_JoeNetModuleImage);

                {
                    Module[] loadedModules = a.GetLoadedModules(getResourceModules: true);
                    Assert.Equal(2, loadedModules.Length);
                    Assert.Contains <Module>(a.ManifestModule, loadedModules);
                    Assert.Contains <Module>(m1, loadedModules);
                }

                {
                    Module[] loadedModules = a.GetLoadedModules(getResourceModules: false);
                    Assert.Equal(2, loadedModules.Length);
                    Assert.Contains <Module>(a.ManifestModule, loadedModules);
                    Assert.Contains <Module>(m1, loadedModules);
                }
            }
        }
Esempio n. 14
0
        public static void ResolveEventNoUnnecessaryCalls()
        {
            // In a single-threaded scenario at least, TypeLoaders shouldn't ask the event to bind the same name twice.
            using (TypeLoader tl = new TypeLoader())
            {
                int      resolveHandlerCallCount   = 0;
                Assembly resolveEventHandlerResult = null;

                tl.Resolving +=
                    delegate(TypeLoader sender, AssemblyName name)
                {
                    if (name.Name == "Foo")
                    {
                        resolveHandlerCallCount++;
                        resolveEventHandlerResult = sender.LoadFromByteArray(TestData.s_BaseClassesImage);
                        return(resolveEventHandlerResult);
                    }
                    return(null);
                };

                Assembly derived = tl.LoadFromByteArray(TestData.s_DerivedClassWithVariationsOnFooImage);
                Type     t1      = derived.GetType("Derived1", throwOnError: true);
                Type     bt1     = t1.BaseType;
                Type     t2      = derived.GetType("Derived2", throwOnError: true);
                Type     bt2     = t2.BaseType;
                Assert.Equal(1, resolveHandlerCallCount);
                Assert.Equal(resolveEventHandlerResult, bt1.Assembly);
                Assert.Equal(resolveEventHandlerResult, bt2.Assembly);
            }
        }
Esempio n. 15
0
 public static void LoadModule_CannotLoadModuleNotInManifest()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_MultiModuleDllImage);
         Assert.Throws <ArgumentException>(() => a.LoadModule("NotInManifest.dll", TestData.s_JoeNetModuleImage));
     }
 }
Esempio n. 16
0
 public static void LoadMultiModuleFromByteArray_GetModule()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_MultiModuleDllImage);
         Assert.Throws <FileNotFoundException>(() => a.GetModule("Bob.netmodule"));
     }
 }
Esempio n. 17
0
 public static void AssemblyIsDynamic()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Assert.False(a.IsDynamic);
     }
 }
Esempio n. 18
0
 public static void AssemblyHostContext()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Assert.Equal(0L, a.HostContext);  // This property is meaningless for TypeLoaders and always returns 0.
     }
 }
Esempio n. 19
0
 public static void AssemblyGlobalAssemblyCache()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Assert.False(a.GlobalAssemblyCache);  // This property is meaningless for TypeLoaders and always returns false.
     }
 }
Esempio n. 20
0
 public static void AssemblyReflectionOnly()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Assert.True(a.ReflectionOnly);
     }
 }
Esempio n. 21
0
 public static void ModuleIsResource()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Module   m = a.ManifestModule;
         Assert.False(m.IsResource());
     }
 }
Esempio n. 22
0
 public static void AssemblyMetadataVersion2_0()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_NetFx20AssemblyImage);
         string   metadataVersion = a.ImageRuntimeVersion;
         Assert.Equal("v2.0.50727", metadataVersion);
     }
 }
Esempio n. 23
0
 public static void AssemblyMetadataVersion4_0()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         string   metadataVersion = a.ImageRuntimeVersion;
         Assert.Equal("v4.0.30319", metadataVersion);
     }
 }
Esempio n. 24
0
 public static void AssemblyLocationMemory()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a        = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         string   location = a.Location;
         Assert.Equal(string.Empty, location);
     }
 }
Esempio n. 25
0
        public static void ExtraDisposesIgnored()
        {
            TypeLoader tl = new TypeLoader();

            tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
            tl.Dispose();
            tl.Dispose();
            tl.Dispose();
        }
Esempio n. 26
0
        public static void AssemblyGetForwardedTypes2()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                Assembly upper  = tl.LoadFromByteArray(TestData.s_UpperImage);
                Assembly middle = tl.LoadFromByteArray(TestData.s_MiddleImage);

                ReflectionTypeLoadException re = Assert.Throws <ReflectionTypeLoadException>(() => upper.GetForwardedTypesThunk());
                Assert.Equal(3, re.Types.Length);
                Assert.Equal(3, re.LoaderExceptions.Length);

                Assert.Equal(2, re.Types.Count((t) => t == null));
                Assert.Contains <Type>(middle.GetType("Upper2", throwOnError: true), re.Types);

                Assert.Equal(1, re.LoaderExceptions.Count((t) => t == null));
                Assert.True(re.LoaderExceptions.All((t) => t == null || t is FileNotFoundException));
            }
        }
Esempio n. 27
0
 public static void AssemblyFullName()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a        = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         string   fullName = a.FullName;
         Assert.Equal(TestData.s_SimpleAssemblyName, fullName);
     }
 }
Esempio n. 28
0
 public static void AssemblyEntryPoint2()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly   a = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         MethodInfo m = a.EntryPoint;
         Assert.Null(m);
     }
 }
Esempio n. 29
0
 public static void ModuleGetNameFromByteArray()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_SimpleAssemblyImage);
         Module   m = a.ManifestModule;
         Assert.Equal(string.Empty, m.Name);
     }
 }
Esempio n. 30
0
 public static void LoadModule_Null()
 {
     using (TypeLoader tl = new TypeLoader())
     {
         Assembly a = tl.LoadFromByteArray(TestData.s_MultiModuleDllImage);
         Assert.Throws <ArgumentNullException>(() => a.LoadModule(null, TestData.s_JoeNetModuleImage));
         Assert.Throws <ArgumentNullException>(() => a.LoadModule("Bob.netmodule", null));
     }
 }