AssemblyTest.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.0.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. string [] names = { "corlib_test_default", "corlib_test_net_2_0", "corlib_plattest" };
  179. foreach (string s in names)
  180. if (Assembly.LoadWithPartialName (s) != null)
  181. return;
  182. Assertion.Fail ("Was not able to load any corlib test");
  183. }
  184. [Test]
  185. [ExpectedException (typeof (ArgumentNullException))]
  186. public void GetObjectData_Null ()
  187. {
  188. Assembly corlib = typeof (int).Assembly;
  189. corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
  190. }
  191. [Test]
  192. public void GetReferencedAssemblies ()
  193. {
  194. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  195. AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
  196. foreach (AssemblyName an in names) {
  197. Assert.IsNull (an.CodeBase, "CodeBase");
  198. Assert.IsNotNull (an.CultureInfo, "CultureInfo");
  199. Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
  200. Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
  201. Assert.IsNotNull (an.FullName, "FullName");
  202. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
  203. Assert.IsNull (an.KeyPair, "KeyPair");
  204. Assert.IsNotNull (an.Name, "Name");
  205. Assert.IsNotNull (an.Version, "Version");
  206. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  207. an.VersionCompatibility, "VersionCompatibility");
  208. }
  209. }
  210. [Test]
  211. public void Location_Empty() {
  212. string assemblyFileName = Path.Combine (
  213. Path.GetTempPath (), "AssemblyLocation.dll");
  214. try {
  215. AssemblyName assemblyName = new AssemblyName ();
  216. assemblyName.Name = "AssemblyLocation";
  217. AssemblyBuilder ab = AppDomain.CurrentDomain
  218. .DefineDynamicAssembly (assemblyName,
  219. AssemblyBuilderAccess.Save,
  220. Path.GetTempPath (),
  221. AppDomain.CurrentDomain.Evidence);
  222. ab.Save (Path.GetFileName (assemblyFileName));
  223. using (FileStream fs = File.OpenRead(assemblyFileName)) {
  224. byte[] buffer = new byte[fs.Length];
  225. fs.Read(buffer, 0, buffer.Length);
  226. Assembly assembly = Assembly.Load(buffer);
  227. Assert.AreEqual(string.Empty, assembly.Location);
  228. fs.Close();
  229. }
  230. } finally {
  231. File.Delete (assemblyFileName);
  232. }
  233. }
  234. #if NET_2_0
  235. [Test]
  236. [Category ("NotWorking")]
  237. public void ReflectionOnlyLoad ()
  238. {
  239. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  240. Assert.IsNotNull (assembly);
  241. Assert.IsTrue (assembly.ReflectionOnly);
  242. }
  243. [Test]
  244. [Category ("NotWorking")]
  245. public void ReflectionOnlyLoadFrom ()
  246. {
  247. string loc = typeof (AssemblyTest).Assembly.Location;
  248. string filename = Path.GetFileName (loc);
  249. Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
  250. Assert.IsNotNull (assembly);
  251. Assert.IsTrue (assembly.ReflectionOnly);
  252. }
  253. [Test]
  254. [ExpectedException (typeof (ArgumentException))]
  255. [Category ("NotWorking")]
  256. public void CreateInstanceOnRefOnly ()
  257. {
  258. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  259. assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  260. }
  261. #endif
  262. }
  263. }