2 // AssemblyCas.cs - CAS unit tests for System.Reflection.Assembly
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using NUnit.Framework;
33 using System.Reflection;
34 using System.Runtime.Serialization;
35 using System.Security;
36 using System.Security.Permissions;
38 namespace MonoCasTests.System.Reflection {
42 public class AssemblyCas {
44 private MonoTests.System.Reflection.AssemblyTest at;
45 private Assembly corlib;
46 private Assembly corlib_test;
49 public void FixtureSetUp ()
51 at = new MonoTests.System.Reflection.AssemblyTest ();
52 corlib = typeof (int).Assembly;
53 corlib_test = Assembly.GetExecutingAssembly ();
59 if (!SecurityManager.SecurityEnabled)
60 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
63 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
66 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
67 public void PartialTrust_Deny_Unrestricted ()
70 at.CreateInvalidInstance ();
72 at.GetReferencedAssemblies ();
74 Assert.IsNotNull (Assembly.GetCallingAssembly (), "GetCallingAssembly");
75 Assembly.GetEntryAssembly (); // null for MS, non-null with Mono
77 Assert.IsTrue (corlib.GetCustomAttributes (true).Length > 0, "GetCustomAttribute");
78 Assert.IsTrue (corlib.GetExportedTypes ().Length > 0, "GetExportedTypes");
79 Assert.IsTrue (corlib.GetLoadedModules (true).Length > 0, "GetLoadedModules(true)");
80 Assert.IsNotNull (corlib.ToString (), "ToString");
82 Module[] ms = corlib.GetModules (true);
83 Assert.IsTrue (ms.Length > 0, "GetModules(true)");
84 // can't use ms [0].Name as this requires PathDiscovery
85 // but ToString return the same value without the check
86 Assert.IsNotNull (corlib.GetModule (ms [0].ToString ()), "GetModule");
88 corlib.GetManifestResourceNames ();
90 Assembly corlib_test = Assembly.GetExecutingAssembly ();
91 Assert.AreEqual (corlib_test.GetCustomAttributes (true).Length,
92 corlib_test.GetCustomAttributes (false).Length, "GetCustomAttribute true==false");
93 Assert.AreEqual (corlib_test.GetLoadedModules ().Length,
94 corlib_test.GetLoadedModules (false).Length, "GetLoadedModules()==(false)");
95 Assert.AreEqual (corlib_test.GetModules ().Length,
96 corlib_test.GetModules (false).Length, "GetModules()==(false)");
98 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
102 [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
103 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
104 public void PartialTrust_PermitOnly_ControlEvidenceFileIOPermission ()
114 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
115 [ExpectedException (typeof (SecurityException))]
116 public void PartialTrust_Deny_ControlEvidence ()
118 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
122 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
123 [ExpectedException (typeof (SecurityException))]
124 public void CodeBase_Deny_FileIOPermission ()
126 Assert.IsNotNull (corlib_test.CodeBase, "CodeBase");
130 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
131 [ExpectedException (typeof (SecurityException))]
132 public void EscapedCodeBase_Deny_FileIOPermission ()
134 Assert.IsNotNull (corlib_test.EscapedCodeBase, "EscapedCodeBase");
138 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
139 [ExpectedException (typeof (SecurityException))]
140 public void Location_Deny_FileIOPermission ()
142 Assert.IsNotNull (corlib_test.Location, "Location");
146 public void GetFile_PermitOnly_FileIOPermission ()
148 FileStream[] fss = corlib.GetFiles (false);
149 if (fss.Length > 0) {
150 foreach (FileStream fs in fss) {
151 GetFile_PermitOnly (fs.Name);
156 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
157 private void GetFile_PermitOnly (string filename)
159 corlib.GetFile (filename);
163 public void GetFile_Deny_FileIOPermission ()
165 FileStream[] fss = corlib.GetFiles (false);
166 if (fss.Length > 0) {
167 foreach (FileStream fs in fss) {
168 GetFile_Deny (fs.Name);
171 // note: we already know the name
174 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
175 private void GetFile_Deny (string filename)
177 corlib.GetFile (filename);
181 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
182 public void GetFile_Unexisting_Deny ()
184 corlib.GetFile ("TOTO");
188 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
189 public void GetFiles_PermitOnly_FileIOPermission ()
191 at.GetFiles_False ();
196 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
197 public void GetFilesFalse_Deny_FileIOPermission ()
200 FileStream[] fss = corlib.GetFiles (false);
202 Assert.Fail ("Expected SecurityException");
204 catch (SecurityException) {
205 // so there was at least one (like on MS runtime)
210 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
211 public void GetFilesTrue_Deny_FileIOPermission ()
214 FileStream[] fss = corlib.GetFiles (true);
216 Assert.Fail ("Expected SecurityException");
218 catch (SecurityException) {
219 // so there was at least one (like on MS runtime)
224 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
225 [ExpectedException (typeof (SecurityException))]
226 public void GetName_Deny_FileIOPermission ()
232 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
233 public void GetName_PermitOnly_FileIOPermission ()
239 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
240 public void LoadWithPartialName_Deny_FileIOPermission ()
242 // FileIOPermission isn't (always) required for LoadWithPartialName
243 // e.g. in this case both assemblies are already loaded in memory
244 at.LoadWithPartialName ();
247 // that one is unclear (undocumented) and doesn't happen in 2.0
248 // will not be implemented in Mono unless if find out why...
249 [Category ("NotWorking")]
251 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
252 [ExpectedException (typeof (SecurityException))]
253 public void LoadWithPartialName_Deny_SecurityPermission ()
255 at.LoadWithPartialName ();
258 // we use reflection to call Assembly as some methods and events are protected
259 // by LinkDemand (which will be converted into full demand, i.e. a stack walk)
260 // when reflection is used (i.e. it gets testable).
263 [SecurityPermission (SecurityAction.Deny, SerializationFormatter = true)]
264 [ExpectedException (typeof (SecurityException))]
265 public void GetObjectData ()
267 SerializationInfo info = null;
268 StreamingContext context = new StreamingContext (StreamingContextStates.All);
269 Assembly a = Assembly.GetExecutingAssembly ();
270 MethodInfo mi = typeof (Assembly).GetMethod ("GetObjectData");
271 mi.Invoke (a, new object [2] { info, context });
275 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
276 [ExpectedException (typeof (SecurityException))]
277 public void AddModuleResolve ()
279 Assembly a = Assembly.GetExecutingAssembly ();
280 MethodInfo mi = typeof (Assembly).GetMethod ("add_ModuleResolve");
281 mi.Invoke (a, new object [1] { null });
285 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
286 [ExpectedException (typeof (SecurityException))]
287 public void RemoveModuleResolve ()
289 Assembly a = Assembly.GetExecutingAssembly ();
290 MethodInfo mi = typeof (Assembly).GetMethod ("remove_ModuleResolve");
291 mi.Invoke (a, new object [1] { null });