AssemblyTest.cs 9.8 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.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.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  111. Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
  112. Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
  113. #elif NET_1_1
  114. Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
  115. Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
  116. Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  117. #endif
  118. }
  119. [Test]
  120. public void Corlib_test ()
  121. {
  122. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  123. Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
  124. Assert.IsNotNull (corlib_test.Evidence, "Evidence");
  125. Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
  126. Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
  127. #if NET_2_0
  128. Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
  129. Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  130. Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
  131. Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
  132. #elif NET_1_1
  133. Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  134. #endif
  135. }
  136. [Test]
  137. public void GetAssembly ()
  138. {
  139. Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
  140. Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
  141. }
  142. [Test]
  143. [ExpectedException (typeof (ArgumentNullException))]
  144. public void GetFile_Null ()
  145. {
  146. Assembly.GetExecutingAssembly ().GetFile (null);
  147. }
  148. [Test]
  149. [ExpectedException (typeof (ArgumentException))]
  150. public void GetFile_Empty ()
  151. {
  152. Assembly.GetExecutingAssembly ().GetFile (String.Empty);
  153. }
  154. [Test]
  155. public void GetFiles_False ()
  156. {
  157. Assembly corlib = typeof (int).Assembly;
  158. FileStream[] fss = corlib.GetFiles ();
  159. Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
  160. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  161. fss = corlib_test.GetFiles ();
  162. Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
  163. }
  164. [Test]
  165. public void GetFiles_True ()
  166. {
  167. Assembly corlib = typeof (int).Assembly;
  168. FileStream[] fss = corlib.GetFiles ();
  169. Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
  170. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  171. fss = corlib_test.GetFiles ();
  172. Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
  173. }
  174. [Test]
  175. public void LoadWithPartialName ()
  176. {
  177. string [] names = { "corlib_test_default", "corlib_test_net_2_0", "corlib_plattest" };
  178. foreach (string s in names)
  179. if (Assembly.LoadWithPartialName (s) != null)
  180. return;
  181. Assertion.Fail ("Was not able to load any corlib test");
  182. }
  183. [Test]
  184. [ExpectedException (typeof (ArgumentNullException))]
  185. public void GetObjectData_Null ()
  186. {
  187. Assembly corlib = typeof (int).Assembly;
  188. corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
  189. }
  190. [Test]
  191. public void GetReferencedAssemblies ()
  192. {
  193. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  194. AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
  195. foreach (AssemblyName an in names) {
  196. Assert.IsNull (an.CodeBase, "CodeBase");
  197. Assert.IsNotNull (an.CultureInfo, "CultureInfo");
  198. Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
  199. Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
  200. Assert.IsNotNull (an.FullName, "FullName");
  201. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
  202. Assert.IsNull (an.KeyPair, "KeyPair");
  203. Assert.IsNotNull (an.Name, "Name");
  204. Assert.IsNotNull (an.Version, "Version");
  205. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  206. an.VersionCompatibility, "VersionCompatibility");
  207. }
  208. }
  209. [Test]
  210. public void Location_Empty() {
  211. string assemblyFileName = Path.Combine (
  212. Path.GetTempPath (), "AssemblyLocation.dll");
  213. try {
  214. AssemblyName assemblyName = new AssemblyName ();
  215. assemblyName.Name = "AssemblyLocation";
  216. AssemblyBuilder ab = AppDomain.CurrentDomain
  217. .DefineDynamicAssembly (assemblyName,
  218. AssemblyBuilderAccess.Save,
  219. Path.GetTempPath (),
  220. AppDomain.CurrentDomain.Evidence);
  221. ab.Save (Path.GetFileName (assemblyFileName));
  222. using (FileStream fs = File.OpenRead(assemblyFileName)) {
  223. byte[] buffer = new byte[fs.Length];
  224. fs.Read(buffer, 0, buffer.Length);
  225. Assembly assembly = Assembly.Load(buffer);
  226. Assert.AreEqual(string.Empty, assembly.Location);
  227. fs.Close();
  228. }
  229. } finally {
  230. File.Delete (assemblyFileName);
  231. }
  232. }
  233. #if NET_2_0
  234. [Test]
  235. [Category ("NotWorking")]
  236. public void ReflectionOnlyLoad ()
  237. {
  238. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  239. Assert.IsNotNull (assembly);
  240. Assert.IsTrue (assembly.ReflectionOnly);
  241. }
  242. [Test]
  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. public void CreateInstanceOnRefOnly ()
  254. {
  255. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  256. assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  257. }
  258. #endif
  259. }
  260. }