2 // System.Reflection.Assembly Test Cases
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Philippe Lavoie (philippe.lavoie@cactus.ca)
7 // Sebastien Pouliot (sebastien@ximian.com)
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
34 using System.Configuration.Assemblies;
35 using System.Globalization;
37 using System.Reflection;
38 using System.Runtime.Serialization;
39 using System.Security;
41 namespace MonoTests.System.Reflection
44 public class AssemblyTest
47 public void CreateInstance()
49 Type type = typeof (AssemblyTest);
50 Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
51 Assert.IsNotNull (obj, "#01");
52 Assert.AreEqual (GetType (), obj.GetType (), "#02");
56 public void CreateInvalidInstance()
58 Type type = typeof (AssemblyTest);
59 Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
60 Assert.IsNull (obj, "#03");
65 [Category ("NotWorking")]
66 [ExpectedException (typeof (ArgumentException))]
68 [ExpectedException (typeof (TypeLoadException))]
70 public void TestGetType ()
73 typeof (int).Assembly.GetType ("&blabla", true, true);
76 [Test][Category("NotWorking")]
77 public void GetEntryAssembly ()
79 // note: only available in default appdomain
80 // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
82 // Not sure we should emulate this behavior.
83 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
85 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
90 [Category ("NotWorking")]
95 Assembly corlib = typeof (int).Assembly;
96 Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
97 Assert.IsNull (corlib.EntryPoint, "EntryPoint");
98 Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
99 Assert.IsNotNull (corlib.Evidence, "Evidence");
100 Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
102 // corlib doesn't reference anything
103 Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
105 Assert.AreEqual ("mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
106 // not really "true" but it's even more trusted so...
107 Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
108 Assert.AreEqual (0, corlib.HostContext, "HostContext");
109 Assert.AreEqual ("v2.0.40607", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
110 Assert.AreEqual (PortableExecutableKind.ILOnly | PortableExecutableKind.Required32Bit, corlib.PortableExecutableKind, "PortableExecutableKind");
111 Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
112 Assert.AreEqual (0x20000001, corlib.MetadataToken);
113 Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
115 Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
116 Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
117 Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
122 public void Corlib_test ()
124 Assembly corlib_test = Assembly.GetExecutingAssembly ();
125 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
126 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
127 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
129 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
131 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
132 Assert.AreEqual ("v2.0.40607", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
133 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
134 Assert.AreEqual (PortableExecutableKind.ILOnly, corlib_test.PortableExecutableKind, "PortableExecutableKind");
135 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
137 Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
142 public void GetAssembly ()
144 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
145 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
149 [ExpectedException (typeof (ArgumentNullException))]
150 public void GetFile_Null ()
152 Assembly.GetExecutingAssembly ().GetFile (null);
156 [ExpectedException (typeof (ArgumentException))]
157 public void GetFile_Empty ()
159 Assembly.GetExecutingAssembly ().GetFile (String.Empty);
163 public void GetFiles_False ()
165 Assembly corlib = typeof (int).Assembly;
166 FileStream[] fss = corlib.GetFiles ();
167 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
169 Assembly corlib_test = Assembly.GetExecutingAssembly ();
170 fss = corlib_test.GetFiles ();
171 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
175 public void GetFiles_True ()
177 Assembly corlib = typeof (int).Assembly;
178 FileStream[] fss = corlib.GetFiles ();
179 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
181 Assembly corlib_test = Assembly.GetExecutingAssembly ();
182 fss = corlib_test.GetFiles ();
183 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
187 public void LoadWithPartialName ()
189 Assembly corlib = Assembly.LoadWithPartialName ("corlib_test_default");
190 Assembly corlib2 = Assembly.LoadWithPartialName ("corlib_plattest");
191 Assert.IsTrue (corlib != null || corlib2 != null);
195 [ExpectedException (typeof (ArgumentNullException))]
196 public void GetObjectData_Null ()
198 Assembly corlib = typeof (int).Assembly;
199 corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
\r
203 public void GetReferencedAssemblies ()
205 Assembly corlib_test = Assembly.GetExecutingAssembly ();
206 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
207 foreach (AssemblyName an in names) {
208 Assert.IsNull (an.CodeBase, "CodeBase");
209 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
210 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
211 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
212 Assert.IsNotNull (an.FullName, "FullName");
213 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
214 Assert.IsNull (an.KeyPair, "KeyPair");
215 Assert.IsNotNull (an.Name, "Name");
216 Assert.IsNotNull (an.Version, "Version");
217 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
218 an.VersionCompatibility, "VersionCompatibility");
223 public void ReflectionOnlyLoad ()
225 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
227 Assert.IsNotNull (assembly);
228 Assert.IsTrue (assembly.ReflectionOnly);
232 public void ReflectionOnlyLoadFrom ()
234 string loc = typeof (AssemblyTest).Assembly.Location;
235 string filename = Path.GetFileName (loc);
236 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
238 Assert.IsNotNull (assembly);
239 Assert.IsTrue (assembly.ReflectionOnly);
243 [ExpectedException (typeof (ArgumentException))]
244 public void CreateInstanceOnRefOnly ()
246 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
247 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");