2005-04-14 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Reflection / AssemblyTest.cs
1 //
2 // System.Reflection.Assembly Test Cases
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Philippe Lavoie (philippe.lavoie@cactus.ca)
7 //      Sebastien Pouliot (sebastien@ximian.com)
8 //
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using NUnit.Framework;
33 using System;
34 using System.Configuration.Assemblies;
35 using System.Globalization;
36 using System.IO;
37 using System.Reflection;
38 using System.Runtime.Serialization;
39 using System.Security;
40
41 namespace MonoTests.System.Reflection
42 {
43         [TestFixture]
44         public class AssemblyTest
45         {
46                 [Test] 
47                 public void CreateInstance() 
48                 {
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");
53                 } 
54
55                 [Test] 
56                 public void CreateInvalidInstance() 
57                 { 
58                         Type type = typeof (AssemblyTest);
59                         Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
60                         Assert.IsNull (obj, "#03");
61                 } 
62
63                 [Test]
64 #if NET_2_0
65                 [Category ("NotWorking")]
66                 [ExpectedException (typeof (ArgumentException))]
67 #else
68                 [ExpectedException (typeof (TypeLoadException))]
69 #endif
70                 public void TestGetType () 
71                 {
72                         // Bug #49114
73                         typeof (int).Assembly.GetType ("&blabla", true, true);
74                 }
75
76                 [Test][Category("NotWorking")]
77                 public void GetEntryAssembly ()
78                 {
79                         // note: only available in default appdomain
80                         // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
81                         //
82                         // Not sure we should emulate this behavior.
83                         Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
84 #if NET_2_0
85                         Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
86 #endif
87                 }
88
89 #if NET_2_0
90                 [Category ("NotWorking")]
91 #endif
92                 [Test]
93                 public void Corlib () 
94                 {
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");
101
102                         // corlib doesn't reference anything
103                         Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
104 #if NET_2_0
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);
114 #elif NET_1_1
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");
118 #endif
119                 }
120
121                 [Test]
122                 public void Corlib_test ()
123                 {
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");
128
129                         Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
130 #if NET_2_0
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");
136 #elif NET_1_1
137                         Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
138 #endif
139                 }
140
141                 [Test]
142                 public void GetAssembly ()
143                 {
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)");
146                 }
147
148                 [Test]
149                 [ExpectedException (typeof (ArgumentNullException))]
150                 public void GetFile_Null ()
151                 {
152                         Assembly.GetExecutingAssembly ().GetFile (null);
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (ArgumentException))]
157                 public void GetFile_Empty ()
158                 {
159                         Assembly.GetExecutingAssembly ().GetFile (String.Empty);
160                 }
161
162                 [Test]
163                 public void GetFiles_False ()
164                 {
165                         Assembly corlib = typeof (int).Assembly;
166                         FileStream[] fss = corlib.GetFiles ();
167                         Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
168
169                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
170                         fss = corlib_test.GetFiles ();
171                         Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
172                 }
173
174                 [Test]
175                 public void GetFiles_True ()
176                 {
177                         Assembly corlib = typeof (int).Assembly;
178                         FileStream[] fss = corlib.GetFiles ();
179                         Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
180
181                         Assembly corlib_test = Assembly.GetExecutingAssembly ();
182                         fss = corlib_test.GetFiles ();
183                         Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
184                 }
185
186                 [Test]
187                 public void LoadWithPartialName ()
188                 {
189                         Assembly corlib = Assembly.LoadWithPartialName ("corlib_test_default");
190                         Assembly corlib2 = Assembly.LoadWithPartialName ("corlib_plattest");
191                         Assert.IsTrue (corlib != null || corlib2 != null);
192                 }
193
194                 [Test]
195                 [ExpectedException (typeof (ArgumentNullException))]
196                 public void GetObjectData_Null ()
197                 {
198                         Assembly corlib = typeof (int).Assembly;
199                         corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));\r
200                 }
201
202                 [Test]
203                 public void GetReferencedAssemblies ()
204                 {
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");
219                         }
220                 }
221 #if NET_2_0
222                 [Test]
223                 public void ReflectionOnlyLoad ()
224                 {
225                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
226                         
227                         Assert.IsNotNull (assembly);
228                         Assert.IsTrue (assembly.ReflectionOnly);
229                 }
230
231                 [Test]
232                 public void ReflectionOnlyLoadFrom ()
233                 {
234                         string loc = typeof (AssemblyTest).Assembly.Location;
235                         string filename = Path.GetFileName (loc);
236                         Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
237
238                         Assert.IsNotNull (assembly);
239                         Assert.IsTrue (assembly.ReflectionOnly);
240                 }
241
242                 [Test]
243                 [ExpectedException (typeof (ArgumentException))]
244                 public void CreateInstanceOnRefOnly ()
245                 {
246                         Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
247                         assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
248                 }
249 #endif
250         }
251 }
252