AssemblyTest.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // System.Reflection.Assembly Test Cases
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Philippe Lavoie ([email protected])
  7. // Sebastien Pouliot ([email protected])
  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. using NUnit.Framework;
  32. using System;
  33. using System.Configuration.Assemblies;
  34. using System.Globalization;
  35. using System.IO;
  36. using System.Reflection;
  37. using System.Reflection.Emit;
  38. using System.Runtime.Serialization;
  39. using System.Security;
  40. namespace MonoTests.System.Reflection
  41. {
  42. [TestFixture]
  43. public class AssemblyTest
  44. {
  45. [Test]
  46. public void CreateInstance()
  47. {
  48. Type type = typeof (AssemblyTest);
  49. Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  50. Assert.IsNotNull (obj, "#01");
  51. Assert.AreEqual (GetType (), obj.GetType (), "#02");
  52. }
  53. [Test]
  54. public void CreateInvalidInstance()
  55. {
  56. Type type = typeof (AssemblyTest);
  57. Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
  58. Assert.IsNull (obj, "#03");
  59. }
  60. [Test]
  61. #if NET_2_0
  62. [Category ("NotWorking")]
  63. [ExpectedException (typeof (ArgumentException))]
  64. #else
  65. [ExpectedException (typeof (TypeLoadException))]
  66. #endif
  67. public void TestGetType ()
  68. {
  69. // Bug #49114
  70. typeof (int).Assembly.GetType ("&blabla", true, true);
  71. }
  72. [Test]
  73. public void GetEntryAssembly ()
  74. {
  75. // note: only available in default appdomain
  76. // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
  77. // Not sure we should emulate this behavior.
  78. string fname = AppDomain.CurrentDomain.FriendlyName;
  79. if (fname.EndsWith (".dll")) { // nunit-console
  80. Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  81. #if NET_2_0
  82. Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  83. #endif
  84. } else { // gnunit
  85. Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  86. #if NET_2_0
  87. Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  88. #endif
  89. }
  90. }
  91. #if NET_2_0
  92. [Category ("NotWorking")]
  93. #endif
  94. [Test]
  95. public void Corlib ()
  96. {
  97. Assembly corlib = typeof (int).Assembly;
  98. Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
  99. Assert.IsNull (corlib.EntryPoint, "EntryPoint");
  100. Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
  101. Assert.IsNotNull (corlib.Evidence, "Evidence");
  102. Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
  103. // corlib doesn't reference anything
  104. Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
  105. #if NET_2_0
  106. Assert.AreEqual ("mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
  107. // not really "true" but it's even more trusted so...
  108. Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
  109. Assert.AreEqual (0, corlib.HostContext, "HostContext");
  110. Assert.AreEqual ("v2.0.50215", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  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. [Test]
  121. public void Corlib_test ()
  122. {
  123. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  124. Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
  125. Assert.IsNotNull (corlib_test.Evidence, "Evidence");
  126. Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
  127. Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
  128. #if NET_2_0
  129. Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
  130. Assert.AreEqual ("v2.0.50215", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  131. Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
  132. Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
  133. #elif NET_1_1
  134. Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  135. #endif
  136. }
  137. [Test]
  138. public void GetAssembly ()
  139. {
  140. Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
  141. Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
  142. }
  143. [Test]
  144. [ExpectedException (typeof (ArgumentNullException))]
  145. public void GetFile_Null ()
  146. {
  147. Assembly.GetExecutingAssembly ().GetFile (null);
  148. }
  149. [Test]
  150. [ExpectedException (typeof (ArgumentException))]
  151. public void GetFile_Empty ()
  152. {
  153. Assembly.GetExecutingAssembly ().GetFile (String.Empty);
  154. }
  155. [Test]
  156. public void GetFiles_False ()
  157. {
  158. Assembly corlib = typeof (int).Assembly;
  159. FileStream[] fss = corlib.GetFiles ();
  160. Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
  161. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  162. fss = corlib_test.GetFiles ();
  163. Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
  164. }
  165. [Test]
  166. public void GetFiles_True ()
  167. {
  168. Assembly corlib = typeof (int).Assembly;
  169. FileStream[] fss = corlib.GetFiles ();
  170. Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
  171. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  172. fss = corlib_test.GetFiles ();
  173. Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
  174. }
  175. [Test]
  176. public void LoadWithPartialName ()
  177. {
  178. Assembly corlib = Assembly.LoadWithPartialName ("corlib_test_default");
  179. Assembly corlib2 = Assembly.LoadWithPartialName ("corlib_plattest");
  180. Assert.IsTrue (corlib != null || corlib2 != null);
  181. }
  182. [Test]
  183. [ExpectedException (typeof (ArgumentNullException))]
  184. public void GetObjectData_Null ()
  185. {
  186. Assembly corlib = typeof (int).Assembly;
  187. corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
  188. }
  189. [Test]
  190. public void GetReferencedAssemblies ()
  191. {
  192. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  193. AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
  194. foreach (AssemblyName an in names) {
  195. Assert.IsNull (an.CodeBase, "CodeBase");
  196. Assert.IsNotNull (an.CultureInfo, "CultureInfo");
  197. Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
  198. Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
  199. Assert.IsNotNull (an.FullName, "FullName");
  200. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
  201. Assert.IsNull (an.KeyPair, "KeyPair");
  202. Assert.IsNotNull (an.Name, "Name");
  203. Assert.IsNotNull (an.Version, "Version");
  204. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  205. an.VersionCompatibility, "VersionCompatibility");
  206. }
  207. }
  208. [Test]
  209. public void Location_Empty() {
  210. string assemblyFileName = Path.Combine (
  211. Path.GetTempPath (), "AssemblyLocation.dll");
  212. try {
  213. AssemblyName assemblyName = new AssemblyName ();
  214. assemblyName.Name = "AssemblyLocation";
  215. AssemblyBuilder ab = AppDomain.CurrentDomain
  216. .DefineDynamicAssembly (assemblyName,
  217. AssemblyBuilderAccess.Save,
  218. Path.GetTempPath (),
  219. AppDomain.CurrentDomain.Evidence);
  220. ab.Save (Path.GetFileName (assemblyFileName));
  221. using (FileStream fs = File.OpenRead(assemblyFileName)) {
  222. byte[] buffer = new byte[fs.Length];
  223. fs.Read(buffer, 0, buffer.Length);
  224. Assembly assembly = Assembly.Load(buffer);
  225. Assert.AreEqual(string.Empty, assembly.Location);
  226. fs.Close();
  227. }
  228. } finally {
  229. File.Delete (assemblyFileName);
  230. }
  231. }
  232. #if NET_2_0
  233. [Test]
  234. [Category ("NotWorking")]
  235. public void ReflectionOnlyLoad ()
  236. {
  237. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  238. Assert.IsNotNull (assembly);
  239. Assert.IsTrue (assembly.ReflectionOnly);
  240. }
  241. [Test]
  242. [Category ("NotWorking")]
  243. public void ReflectionOnlyLoadFrom ()
  244. {
  245. string loc = typeof (AssemblyTest).Assembly.Location;
  246. string filename = Path.GetFileName (loc);
  247. Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
  248. Assert.IsNotNull (assembly);
  249. Assert.IsTrue (assembly.ReflectionOnly);
  250. }
  251. [Test]
  252. [ExpectedException (typeof (ArgumentException))]
  253. [Category ("NotWorking")]
  254. public void CreateInstanceOnRefOnly ()
  255. {
  256. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  257. assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  258. }
  259. #endif
  260. }
  261. }