AssemblyTest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 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.Globalization;
  34. using System.IO;
  35. using System.Reflection;
  36. using System.Security;
  37. namespace MonoTests.System.Reflection
  38. {
  39. [TestFixture]
  40. public class AssemblyTest
  41. {
  42. [Test]
  43. public void CreateInstance()
  44. {
  45. Type type = typeof (AssemblyTest);
  46. Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  47. Assert.IsNotNull (obj, "#01");
  48. Assert.AreEqual (GetType (), obj.GetType (), "#02");
  49. }
  50. [Test]
  51. public void CreateInvalidInstance()
  52. {
  53. Type type = typeof (AssemblyTest);
  54. Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
  55. Assert.IsNull (obj, "#03");
  56. }
  57. [Test]
  58. #if NET_2_0
  59. [Category ("NotWorking")]
  60. [ExpectedException (typeof (ArgumentException))]
  61. #else
  62. [ExpectedException (typeof (TypeLoadException))]
  63. #endif
  64. public void TestGetType ()
  65. {
  66. // Bug #49114
  67. typeof (int).Assembly.GetType ("&blabla", true, true);
  68. }
  69. [Test][Category("NotWorking")]
  70. public void GetEntryAssembly ()
  71. {
  72. // note: only available in default appdomain
  73. // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
  74. //
  75. // Not sure we should emulate this behavior.
  76. Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  77. #if NET_2_0
  78. Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  79. #endif
  80. }
  81. #if NET_2_0
  82. [Category ("NotWorking")]
  83. #endif
  84. [Test]
  85. public void Corlib ()
  86. {
  87. Assembly corlib = typeof (int).Assembly;
  88. Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
  89. Assert.IsNull (corlib.EntryPoint, "EntryPoint");
  90. Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
  91. Assert.IsNotNull (corlib.Evidence, "Evidence");
  92. Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
  93. // corlib doesn't reference anything
  94. Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
  95. #if NET_2_0
  96. Assert.AreEqual ("mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
  97. // not really "true" but it's even more trusted so...
  98. Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
  99. Assert.AreEqual (0, corlib.HostContext, "HostContext");
  100. Assert.AreEqual ("v2.0.40607", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  101. Assert.AreEqual (PortableExecutableKind.ILOnly | PortableExecutableKind.Required32Bit, corlib.PortableExecutableKind, "PortableExecutableKind");
  102. Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
  103. Assert.AreEqual (0x20000001, corlib.MetadataToken);
  104. Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
  105. #elif NET_1_1
  106. Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
  107. Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
  108. Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  109. #endif
  110. }
  111. [Test]
  112. public void Corlib_test ()
  113. {
  114. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  115. Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
  116. Assert.IsNotNull (corlib_test.Evidence, "Evidence");
  117. Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
  118. Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
  119. #if NET_2_0
  120. Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
  121. Assert.AreEqual ("v2.0.40607", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  122. Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
  123. Assert.AreEqual (PortableExecutableKind.ILOnly, corlib_test.PortableExecutableKind, "PortableExecutableKind");
  124. Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
  125. #elif NET_1_1
  126. Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  127. #endif
  128. }
  129. [Test]
  130. public void GetAssembly ()
  131. {
  132. Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
  133. Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
  134. }
  135. [Test]
  136. [ExpectedException (typeof (ArgumentNullException))]
  137. public void GetFile_Null ()
  138. {
  139. Assembly.GetExecutingAssembly ().GetFile (null);
  140. }
  141. [Test]
  142. [ExpectedException (typeof (ArgumentException))]
  143. public void GetFile_Empty ()
  144. {
  145. Assembly.GetExecutingAssembly ().GetFile (String.Empty);
  146. }
  147. [Test]
  148. public void GetFiles_False ()
  149. {
  150. Assembly corlib = typeof (int).Assembly;
  151. FileStream[] fss = corlib.GetFiles ();
  152. Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
  153. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  154. fss = corlib_test.GetFiles ();
  155. Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
  156. }
  157. [Test]
  158. public void GetFiles_True ()
  159. {
  160. Assembly corlib = typeof (int).Assembly;
  161. FileStream[] fss = corlib.GetFiles ();
  162. Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
  163. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  164. fss = corlib_test.GetFiles ();
  165. Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
  166. }
  167. [Test]
  168. public void LoadWithPartialName ()
  169. {
  170. Assembly corlib = Assembly.LoadWithPartialName ("corlib_test_default");
  171. Assembly corlib2 = Assembly.LoadWithPartialName ("corlib_plattest");
  172. Assert.IsTrue (corlib != null || corlib2 != null);
  173. }
  174. #if NET_2_0
  175. [Test]
  176. public void ReflectionOnlyLoad ()
  177. {
  178. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  179. Assert.IsNotNull (assembly);
  180. Assert.IsTrue (assembly.ReflectionOnly);
  181. }
  182. [Test]
  183. public void ReflectionOnlyLoadFrom ()
  184. {
  185. string loc = typeof (AssemblyTest).Assembly.Location;
  186. string filename = Path.GetFileName (loc);
  187. Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
  188. Assert.IsNotNull (assembly);
  189. Assert.IsTrue (assembly.ReflectionOnly);
  190. }
  191. [Test]
  192. [ExpectedException (typeof (ArgumentException))]
  193. public void CreateInstanceOnRefOnly ()
  194. {
  195. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  196. assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  197. }
  198. #endif
  199. }
  200. }