AssemblyCas.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // AssemblyCas.cs - CAS unit tests for System.Reflection.Assembly
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using NUnit.Framework;
  29. using System;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Runtime.Serialization;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. namespace MonoCasTests.System.Reflection {
  36. [TestFixture]
  37. [Category ("CAS")]
  38. public class AssemblyCas {
  39. private MonoTests.System.Reflection.AssemblyTest at;
  40. private Assembly corlib;
  41. private Assembly corlib_test;
  42. [TestFixtureSetUp]
  43. public void FixtureSetUp ()
  44. {
  45. at = new MonoTests.System.Reflection.AssemblyTest ();
  46. corlib = typeof (int).Assembly;
  47. corlib_test = Assembly.GetExecutingAssembly ();
  48. }
  49. [SetUp]
  50. public void SetUp ()
  51. {
  52. if (!SecurityManager.SecurityEnabled)
  53. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  54. }
  55. // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
  56. [Test]
  57. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  58. public void PartialTrust_Deny_Unrestricted ()
  59. {
  60. at.CreateInstance ();
  61. at.CreateInvalidInstance ();
  62. at.GetAssembly ();
  63. at.GetReferencedAssemblies ();
  64. Assert.IsNotNull (Assembly.GetCallingAssembly (), "GetCallingAssembly");
  65. Assembly.GetEntryAssembly (); // null for MS, non-null with Mono
  66. Assert.IsTrue (corlib.GetCustomAttributes (true).Length > 0, "GetCustomAttribute");
  67. Assert.IsTrue (corlib.GetExportedTypes ().Length > 0, "GetExportedTypes");
  68. Assert.IsTrue (corlib.GetLoadedModules (true).Length > 0, "GetLoadedModules(true)");
  69. Assert.IsNotNull (corlib.ToString (), "ToString");
  70. Module[] ms = corlib.GetModules (true);
  71. Assert.IsTrue (ms.Length > 0, "GetModules(true)");
  72. // can't use ms [0].Name as this requires PathDiscovery
  73. // but ToString return the same value without the check
  74. Assert.IsNotNull (corlib.GetModule (ms [0].ToString ()), "GetModule");
  75. corlib.GetManifestResourceNames ();
  76. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  77. Assert.AreEqual (corlib_test.GetCustomAttributes (true).Length,
  78. corlib_test.GetCustomAttributes (false).Length, "GetCustomAttribute true==false");
  79. Assert.AreEqual (corlib_test.GetLoadedModules ().Length,
  80. corlib_test.GetLoadedModules (false).Length, "GetLoadedModules()==(false)");
  81. Assert.AreEqual (corlib_test.GetModules ().Length,
  82. corlib_test.GetModules (false).Length, "GetModules()==(false)");
  83. Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
  84. }
  85. [Test]
  86. [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
  87. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  88. public void PartialTrust_PermitOnly_ControlEvidenceFileIOPermission ()
  89. {
  90. at.Corlib_test ();
  91. }
  92. [Test]
  93. [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
  94. [ExpectedException (typeof (SecurityException))]
  95. public void PartialTrust_Deny_ControlEvidence ()
  96. {
  97. Assert.IsNotNull (corlib_test.Evidence, "Evidence");
  98. }
  99. [Test]
  100. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  101. [ExpectedException (typeof (SecurityException))]
  102. public void CodeBase_Deny_FileIOPermission ()
  103. {
  104. Assert.IsNotNull (corlib_test.CodeBase, "CodeBase");
  105. }
  106. [Test]
  107. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  108. [ExpectedException (typeof (SecurityException))]
  109. public void EscapedCodeBase_Deny_FileIOPermission ()
  110. {
  111. Assert.IsNotNull (corlib_test.EscapedCodeBase, "EscapedCodeBase");
  112. }
  113. [Test]
  114. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  115. [ExpectedException (typeof (SecurityException))]
  116. public void Location_Deny_FileIOPermission ()
  117. {
  118. Assert.IsNotNull (corlib_test.Location, "Location");
  119. }
  120. [Test]
  121. public void GetFile_PermitOnly_FileIOPermission ()
  122. {
  123. FileStream[] fss = corlib.GetFiles (false);
  124. if (fss.Length > 0) {
  125. foreach (FileStream fs in fss) {
  126. GetFile_PermitOnly (fs.Name);
  127. }
  128. }
  129. }
  130. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  131. private void GetFile_PermitOnly (string filename)
  132. {
  133. corlib.GetFile (filename);
  134. }
  135. [Test]
  136. public void GetFile_Deny_FileIOPermission ()
  137. {
  138. FileStream[] fss = corlib.GetFiles (false);
  139. if (fss.Length > 0) {
  140. foreach (FileStream fs in fss) {
  141. GetFile_Deny (fs.Name);
  142. }
  143. }
  144. // note: we already know the name
  145. }
  146. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  147. private void GetFile_Deny (string filename)
  148. {
  149. corlib.GetFile (filename);
  150. }
  151. [Test]
  152. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  153. public void GetFile_Unexisting_Deny ()
  154. {
  155. corlib.GetFile ("TOTO");
  156. }
  157. [Test]
  158. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  159. public void GetFiles_PermitOnly_FileIOPermission ()
  160. {
  161. at.GetFiles_False ();
  162. at.GetFiles_True ();
  163. }
  164. [Test]
  165. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  166. public void GetFilesFalse_Deny_FileIOPermission ()
  167. {
  168. try {
  169. FileStream[] fss = corlib.GetFiles (false);
  170. if (fss.Length != 0)
  171. Assert.Fail ("Expected SecurityException");
  172. }
  173. catch (SecurityException) {
  174. // so there was at least one (like on MS runtime)
  175. }
  176. }
  177. [Test]
  178. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  179. public void GetFilesTrue_Deny_FileIOPermission ()
  180. {
  181. try {
  182. FileStream[] fss = corlib.GetFiles (true);
  183. if (fss.Length != 0)
  184. Assert.Fail ("Expected SecurityException");
  185. }
  186. catch (SecurityException) {
  187. // so there was at least one (like on MS runtime)
  188. }
  189. }
  190. [Test]
  191. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  192. [ExpectedException (typeof (SecurityException))]
  193. public void GetName_Deny_FileIOPermission ()
  194. {
  195. corlib.GetName ();
  196. }
  197. [Test]
  198. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  199. public void GetName_PermitOnly_FileIOPermission ()
  200. {
  201. corlib.GetName ();
  202. }
  203. [Test]
  204. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  205. public void LoadWithPartialName_Deny_FileIOPermission ()
  206. {
  207. // FileIOPermission isn't (always) required for LoadWithPartialName
  208. // e.g. in this case both assemblies are already loaded in memory
  209. at.LoadWithPartialName ();
  210. }
  211. #if !NET_2_0
  212. // that one is unclear (undocumented) and doesn't happen in 2.0
  213. // will not be implemented in Mono unless if find out why...
  214. [Category ("NotWorking")]
  215. [Test]
  216. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  217. [ExpectedException (typeof (SecurityException))]
  218. public void LoadWithPartialName_Deny_SecurityPermission ()
  219. {
  220. at.LoadWithPartialName ();
  221. }
  222. #endif
  223. // we use reflection to call Assembly as some methods and events are protected
  224. // by LinkDemand (which will be converted into full demand, i.e. a stack walk)
  225. // when reflection is used (i.e. it gets testable).
  226. [Test]
  227. [SecurityPermission (SecurityAction.Deny, SerializationFormatter = true)]
  228. [ExpectedException (typeof (SecurityException))]
  229. public void GetObjectData ()
  230. {
  231. SerializationInfo info = null;
  232. StreamingContext context = new StreamingContext (StreamingContextStates.All);
  233. Assembly a = Assembly.GetExecutingAssembly ();
  234. MethodInfo mi = typeof (Assembly).GetMethod ("GetObjectData");
  235. mi.Invoke (a, new object [2] { info, context });
  236. }
  237. [Test]
  238. [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
  239. [ExpectedException (typeof (SecurityException))]
  240. public void AddModuleResolve ()
  241. {
  242. Assembly a = Assembly.GetExecutingAssembly ();
  243. MethodInfo mi = typeof (Assembly).GetMethod ("add_ModuleResolve");
  244. mi.Invoke (a, new object [1] { null });
  245. }
  246. [Test]
  247. [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
  248. [ExpectedException (typeof (SecurityException))]
  249. public void RemoveModuleResolve ()
  250. {
  251. Assembly a = Assembly.GetExecutingAssembly ();
  252. MethodInfo mi = typeof (Assembly).GetMethod ("remove_ModuleResolve");
  253. mi.Invoke (a, new object [1] { null });
  254. }
  255. }
  256. }