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 ()
110 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
111 [ExpectedException (typeof (SecurityException))]
112 public void PartialTrust_Deny_ControlEvidence ()
114 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
118 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
119 [ExpectedException (typeof (SecurityException))]
120 public void CodeBase_Deny_FileIOPermission ()
122 Assert.IsNotNull (corlib_test.CodeBase, "CodeBase");
126 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
127 [ExpectedException (typeof (SecurityException))]
128 public void EscapedCodeBase_Deny_FileIOPermission ()
130 Assert.IsNotNull (corlib_test.EscapedCodeBase, "EscapedCodeBase");
134 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
135 [ExpectedException (typeof (SecurityException))]
136 public void Location_Deny_FileIOPermission ()
138 Assert.IsNotNull (corlib_test.Location, "Location");
142 public void GetFile_PermitOnly_FileIOPermission ()
144 FileStream[] fss = corlib.GetFiles (false);
145 if (fss.Length > 0) {
146 foreach (FileStream fs in fss) {
147 GetFile_PermitOnly (fs.Name);
152 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
153 private void GetFile_PermitOnly (string filename)
155 corlib.GetFile (filename);
159 public void GetFile_Deny_FileIOPermission ()
161 FileStream[] fss = corlib.GetFiles (false);
162 if (fss.Length > 0) {
163 foreach (FileStream fs in fss) {
164 GetFile_Deny (fs.Name);
167 // note: we already know the name
170 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
171 private void GetFile_Deny (string filename)
173 corlib.GetFile (filename);
177 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
178 public void GetFile_Unexisting_Deny ()
180 corlib.GetFile ("TOTO");
184 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
185 public void GetFiles_PermitOnly_FileIOPermission ()
187 at.GetFiles_False ();
192 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
193 public void GetFilesFalse_Deny_FileIOPermission ()
196 FileStream[] fss = corlib.GetFiles (false);
198 Assert.Fail ("Expected SecurityException");
200 catch (SecurityException) {
201 // so there was at least one (like on MS runtime)
206 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
207 public void GetFilesTrue_Deny_FileIOPermission ()
210 FileStream[] fss = corlib.GetFiles (true);
212 Assert.Fail ("Expected SecurityException");
214 catch (SecurityException) {
215 // so there was at least one (like on MS runtime)
220 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
221 [ExpectedException (typeof (SecurityException))]
222 public void GetName_Deny_FileIOPermission ()
228 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
229 public void GetName_PermitOnly_FileIOPermission ()
235 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
236 public void LoadWithPartialName_Deny_FileIOPermission ()
238 // FileIOPermission isn't (always) required for LoadWithPartialName
239 // e.g. in this case both assemblies are already loaded in memory
240 at.LoadWithPartialName ();
242 // we use reflection to call Assembly as some methods and events are protected
243 // by LinkDemand (which will be converted into full demand, i.e. a stack walk)
244 // when reflection is used (i.e. it gets testable).
247 [SecurityPermission (SecurityAction.Deny, SerializationFormatter = true)]
248 [ExpectedException (typeof (SecurityException))]
249 public void GetObjectData ()
251 SerializationInfo info = null;
252 StreamingContext context = new StreamingContext (StreamingContextStates.All);
253 Assembly a = Assembly.GetExecutingAssembly ();
254 MethodInfo mi = typeof (Assembly).GetMethod ("GetObjectData");
255 mi.Invoke (a, new object [2] { info, context });
259 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
260 [ExpectedException (typeof (SecurityException))]
261 public void AddModuleResolve ()
263 Assembly a = Assembly.GetExecutingAssembly ();
264 MethodInfo mi = typeof (Assembly).GetMethod ("add_ModuleResolve");
265 mi.Invoke (a, new object [1] { null });
269 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
270 [ExpectedException (typeof (SecurityException))]
271 public void RemoveModuleResolve ()
273 Assembly a = Assembly.GetExecutingAssembly ();
274 MethodInfo mi = typeof (Assembly).GetMethod ("remove_ModuleResolve");
275 mi.Invoke (a, new object [1] { null });