[bcl] Remove more NET_2_0 checks from class libs
[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                 }
108
109                 [Test]
110                 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
111                 [ExpectedException (typeof (SecurityException))]
112                 public void PartialTrust_Deny_ControlEvidence ()
113                 {
114                         Assert.IsNotNull (corlib_test.Evidence, "Evidence");
115                 }
116
117                 [Test]
118                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
119                 [ExpectedException (typeof (SecurityException))]
120                 public void CodeBase_Deny_FileIOPermission ()
121                 {
122                         Assert.IsNotNull (corlib_test.CodeBase, "CodeBase");
123                 }
124
125                 [Test]
126                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
127                 [ExpectedException (typeof (SecurityException))]
128                 public void EscapedCodeBase_Deny_FileIOPermission ()
129                 {
130                         Assert.IsNotNull (corlib_test.EscapedCodeBase, "EscapedCodeBase");
131                 }
132
133                 [Test]
134                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
135                 [ExpectedException (typeof (SecurityException))]
136                 public void Location_Deny_FileIOPermission ()
137                 {
138                         Assert.IsNotNull (corlib_test.Location, "Location");
139                 }
140
141                 [Test]
142                 public void GetFile_PermitOnly_FileIOPermission ()
143                 {
144                         FileStream[] fss = corlib.GetFiles (false);
145                         if (fss.Length > 0) {
146                                 foreach (FileStream fs in fss) {
147                                         GetFile_PermitOnly (fs.Name);
148                                 }
149                         }
150                 }
151
152                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
153                 private void GetFile_PermitOnly (string filename)
154                 {
155                         corlib.GetFile (filename);
156                 }
157
158                 [Test]
159                 public void GetFile_Deny_FileIOPermission ()
160                 {
161                         FileStream[] fss = corlib.GetFiles (false);
162                         if (fss.Length > 0) {
163                                 foreach (FileStream fs in fss) {
164                                         GetFile_Deny (fs.Name);
165                                 }
166                         }
167                         // note: we already know the name
168                 }
169
170                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
171                 private void GetFile_Deny (string filename)
172                 {
173                         corlib.GetFile (filename);
174                 }
175
176                 [Test]
177                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
178                 public void GetFile_Unexisting_Deny ()
179                 {
180                         corlib.GetFile ("TOTO");
181                 }
182
183                 [Test]
184                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
185                 public void GetFiles_PermitOnly_FileIOPermission ()
186                 {
187                         at.GetFiles_False ();
188                         at.GetFiles_True ();
189                 }
190
191                 [Test]
192                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
193                 public void GetFilesFalse_Deny_FileIOPermission ()
194                 {
195                         try {
196                                 FileStream[] fss = corlib.GetFiles (false);
197                                 if (fss.Length != 0)
198                                         Assert.Fail ("Expected SecurityException");
199                         }
200                         catch (SecurityException) {
201                                 // so there was at least one (like on MS runtime)
202                         }
203                 }
204
205                 [Test]
206                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
207                 public void GetFilesTrue_Deny_FileIOPermission ()
208                 {
209                         try {
210                                 FileStream[] fss = corlib.GetFiles (true);
211                                 if (fss.Length != 0)
212                                         Assert.Fail ("Expected SecurityException");
213                         }
214                         catch (SecurityException) {
215                                 // so there was at least one (like on MS runtime)
216                         }
217                 }
218
219                 [Test]
220                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
221                 [ExpectedException (typeof (SecurityException))]
222                 public void GetName_Deny_FileIOPermission ()
223                 {
224                         corlib.GetName ();
225                 }
226
227                 [Test]
228                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
229                 public void GetName_PermitOnly_FileIOPermission ()
230                 {
231                         corlib.GetName ();
232                 }
233
234                 [Test]
235                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
236                 public void LoadWithPartialName_Deny_FileIOPermission ()
237                 {
238                         // FileIOPermission isn't (always) required for LoadWithPartialName
239                         // e.g. in this case both assemblies are already loaded in memory
240                         at.LoadWithPartialName ();
241                 }
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).
245
246                 [Test]
247                 [SecurityPermission (SecurityAction.Deny, SerializationFormatter = true)]
248                 [ExpectedException (typeof (SecurityException))]
249                 public void GetObjectData ()
250                 {
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 });
256                 }
257
258                 [Test]
259                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
260                 [ExpectedException (typeof (SecurityException))]
261                 public void AddModuleResolve ()
262                 {
263                         Assembly a = Assembly.GetExecutingAssembly ();
264                         MethodInfo mi = typeof (Assembly).GetMethod ("add_ModuleResolve");
265                         mi.Invoke (a, new object [1] { null });
266                 }
267
268                 [Test]
269                 [SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
270                 [ExpectedException (typeof (SecurityException))]
271                 public void RemoveModuleResolve ()
272                 {
273                         Assembly a = Assembly.GetExecutingAssembly ();
274                         MethodInfo mi = typeof (Assembly).GetMethod ("remove_ModuleResolve");
275                         mi.Invoke (a, new object [1] { null });
276                 }
277         }
278 }