AssemblyTest.cs 8.9 KB

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