2005-04-28 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Reflection / AssemblyCas.cs
1 //
2 // AssemblyCas.cs - CAS unit tests for System.Reflection.Assembly
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
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
29 using NUnit.Framework;
30
31 using System;
32 using System.IO;
33 using System.Reflection;
34 using System.Runtime.Serialization;
35 using System.Security;
36 using System.Security.Permissions;
37
38 namespace MonoCasTests.System.Reflection {
39
40         [TestFixture]
41         [Category ("CAS")]
42         public class AssemblyCas {
43
44                 private MonoTests.System.Reflection.AssemblyTest at;
45                 private Assembly corlib;
46                 private Assembly corlib_test;
47
48                 [TestFixtureSetUp]
49                 public void FixtureSetUp ()
50                 {
51                         at = new MonoTests.System.Reflection.AssemblyTest ();
52                         corlib = typeof (int).Assembly;
53                         corlib_test = Assembly.GetExecutingAssembly ();
54                 }
55
56                 [SetUp]
57                 public void SetUp ()
58                 {
59                         if (!SecurityManager.SecurityEnabled)
60                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
61                 }
62
63                 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
64
65                 [Test]
66                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
67                 public void PartialTrust_Deny_Unrestricted ()
68                 {
69                         at.CreateInstance ();
70                         at.CreateInvalidInstance ();
71                         at.GetAssembly ();
72                         at.GetReferencedAssemblies ();
73
74                         Assert.IsNotNull (Assembly.GetCallingAssembly (), "GetCallingAssembly");
75                         Assembly.GetEntryAssembly (); // null for MS, non-null with Mono
76
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");
81
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");
87
88                         corlib.GetManifestResourceNames ();
89
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)");
97
98                         Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
99                 }
100
101                 [Test]
102                 [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
103                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
104                 public void PartialTrust_PermitOnly_ControlEvidenceFileIOPermission ()
105                 {
106                         at.Corlib_test ();
107 #if NET_2_0
108 #elif NET_1_1
109                         at.Corlib ();
110 #endif
111                 }
112
113                 [Test]
114                 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
115                 [ExpectedException (typeof (SecurityException))]
116                 public void PartialTrust_Deny_ControlEvidence ()
117                 {
118                         Assert.IsNotNull (corlib_test.Evidence, "Evidence");
119                 }
120
121                 [Test]
122                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
123                 [ExpectedException (typeof (SecurityException))]
124                 public void CodeBase_Deny_FileIOPermission ()
125                 {
126                         Assert.IsNotNull (corlib_test.CodeBase, "CodeBase");
127                 }
128
129                 [Test]
130                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
131                 [ExpectedException (typeof (SecurityException))]
132                 public void EscapedCodeBase_Deny_FileIOPermission ()
133                 {
134                         Assert.IsNotNull (corlib_test.EscapedCodeBase, "EscapedCodeBase");
135                 }
136
137                 [Test]
138                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
139                 [ExpectedException (typeof (SecurityException))]
140                 public void Location_Deny_FileIOPermission ()
141                 {
142                         Assert.IsNotNull (corlib_test.Location, "Location");
143                 }
144
145                 [Test]
146                 public void GetFile_PermitOnly_FileIOPermission ()
147                 {
148                         FileStream[] fss = corlib.GetFiles (false);
149                         if (fss.Length > 0) {
150                                 foreach (FileStream fs in fss) {
151                                         GetFile_PermitOnly (fs.Name);
152                                 }
153                         }
154                 }
155
156                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
157                 private void GetFile_PermitOnly (string filename)
158                 {
159                         corlib.GetFile (filename);
160                 }
161
162                 [Test]
163                 public void GetFile_Deny_FileIOPermission ()
164                 {
165                         FileStream[] fss = corlib.GetFiles (false);
166                         if (fss.Length > 0) {
167                                 foreach (FileStream fs in fss) {
168                                         GetFile_Deny (fs.Name);
169                                 }
170                         }
171                         // note: we already know the name
172                 }
173
174                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
175                 private void GetFile_Deny (string filename)
176                 {
177                         corlib.GetFile (filename);
178                 }
179
180                 [Test]
181                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
182                 public void GetFile_Unexisting_Deny ()
183                 {
184                         corlib.GetFile ("TOTO");
185                 }
186
187                 [Test]
188                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
189                 public void GetFiles_PermitOnly_FileIOPermission ()
190                 {
191                         at.GetFiles_False ();
192                         at.GetFiles_True ();
193                 }
194
195                 [Test]
196                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
197                 public void GetFilesFalse_Deny_FileIOPermission ()
198                 {
199                         try {
200                                 FileStream[] fss = corlib.GetFiles (false);
201                                 if (fss.Length != 0)
202                                         Assert.Fail ("Expected SecurityException");
203                         }
204                         catch (SecurityException) {
205                                 // so there was at least one (like on MS runtime)
206                         }
207                 }
208
209                 [Test]
210                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
211                 public void GetFilesTrue_Deny_FileIOPermission ()
212                 {
213                         try {
214                                 FileStream[] fss = corlib.GetFiles (true);
215                                 if (fss.Length != 0)
216                                         Assert.Fail ("Expected SecurityException");
217                         }
218                         catch (SecurityException) {
219                                 // so there was at least one (like on MS runtime)
220                         }
221                 }
222
223                 [Test]
224                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
225                 [ExpectedException (typeof (SecurityException))]
226                 public void GetName_Deny_FileIOPermission ()
227                 {
228                         corlib.GetName ();
229                 }
230
231                 [Test]
232                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
233                 public void GetName_PermitOnly_FileIOPermission ()
234                 {
235                         corlib.GetName ();
236                 }
237
238                 [Test]
239                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
240                 public void LoadWithPartialName_Deny_FileIOPermission ()
241                 {
242                         // FileIOPermission isn't (always) required for LoadWithPartialName
243                         // e.g. in this case both assemblies are already loaded in memory
244                         at.LoadWithPartialName ();
245                 }
246 #if !NET_2_0
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")]
250                 [Test]
251                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
252                 [ExpectedException (typeof (SecurityException))]
253                 public void LoadWithPartialName_Deny_SecurityPermission ()
254                 {
255                         at.LoadWithPartialName ();
256                 }
257 #endif
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).
261
262                 [Test]
263                 [SecurityPermission (SecurityAction.Deny, SerializationFormatter = true)]
264                 [ExpectedException (typeof (SecurityException))]
265                 public void GetObjectData ()
266                 {
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 });
272                 }
273
274                 [Test]
275                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
276                 [ExpectedException (typeof (SecurityException))]
277                 public void AddModuleResolve ()
278                 {
279                         Assembly a = Assembly.GetExecutingAssembly ();
280                         MethodInfo mi = typeof (Assembly).GetMethod ("add_ModuleResolve");
281                         mi.Invoke (a, new object [1] { null });
282                 }
283
284                 [Test]
285                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
286                 [ExpectedException (typeof (SecurityException))]
287                 public void RemoveModuleResolve ()
288                 {
289                         Assembly a = Assembly.GetExecutingAssembly ();
290                         MethodInfo mi = typeof (Assembly).GetMethod ("remove_ModuleResolve");
291                         mi.Invoke (a, new object [1] { null });
292                 }
293         }
294 }