Merge pull request #194 from QuickJack/master
[mono.git] / mcs / class / System / Test / System.CodeDom.Compiler / ExecutorCas.cs
1 //
2 // ExecutorCas.cs 
3 //      - CAS unit tests for System.CodeDom.Compiler.Executor
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31
32 using System;
33 using System.CodeDom.Compiler;
34 using System.IO;
35 using System.Reflection;
36 using System.Security;
37 using System.Security.Permissions;
38 using System.Security.Policy;
39
40 using MonoTests.System.CodeDom.Compiler;
41
42 namespace MonoCasTests.System.CodeDom.Compiler {
43
44         [TestFixture]
45         [Category ("CAS")]
46         public class ExecutorCas {
47
48                 [SetUp]
49                 public void SetUp ()
50                 {
51                         if (!SecurityManager.SecurityEnabled)
52                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
53                 }
54
55                 private string cmd;
56                 private TempFileCollection tfc;
57                 private ExecutorTest unit;
58                 private MethodInfo execWaitWithCapture;
59
60                 [TestFixtureSetUp]
61                 public void FixtureSetUp ()
62                 {
63                         // at fulltrust
64                         tfc = new TempFileCollection ();
65                         cmd = "ping"; // available everywhere
66
67                         unit = new ExecutorTest ();
68                         unit.FixtureSetUp ();
69
70                         // for linkdemands tests
71                         MethodInfo[] methods = typeof (Executor).GetMethods ();
72                         foreach (MethodInfo mi in methods) {
73                                 if ((mi.Name == "ExecWaitWithCapture") && (mi.GetParameters ().Length == 4)) {
74                                         execWaitWithCapture = mi;
75                                         break;
76                                 }
77                         }
78                 }
79
80                 [Test]
81                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
82                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
83                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
84                 public void ReuseUnitTests_PermitOnly ()
85                 {
86                         unit.ExecWaitWithCapture ();
87                         unit.ExecWaitWithCapture_CurrentDir ();
88                         unit.ExecWaitWithCapture_Token ();
89                         unit.ExecWaitWithCapture_Token_CurrentDir ();
90                 }
91                 
92                 [Test]
93                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
94                 [ExpectedException (typeof (SecurityException))]
95                 public void ExecWaitWithCapture_Deny_FileIO ()
96                 {
97                         unit.ExecWaitWithCapture ();
98                 }
99
100                 [Test]
101                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
102                 [ExpectedException (typeof (SecurityException))]
103                 public void ExecWaitWithCapture_Deny_UnmanagedCode ()
104                 {
105                         unit.ExecWaitWithCapture ();
106                 }
107
108                 [Test]
109                 [EnvironmentPermission (SecurityAction.Deny, Unrestricted = true)]
110                 [ExpectedException (typeof (SecurityException))]
111                 public void ExecWaitWithCapture_Deny_Environment ()
112                 {
113                         unit.ExecWaitWithCapture ();
114                 }
115
116                 [Test]
117                 public void LinkDemand_No_Restriction ()
118                 {
119                         Assert.IsNotNull (execWaitWithCapture, "ExecWaitWithCapture");
120                         string output = null;
121                         string error = null;
122                         Assert.IsNotNull (execWaitWithCapture.Invoke (null, new object[4] { cmd, tfc, output, error }), "invoke");
123                 }
124
125                 [Test]
126                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
127                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
128                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
129                 [ExpectedException (typeof (SecurityException))]
130                 public void LinkDemand_PermitOnly ()
131                 {
132                         Assert.IsNotNull (execWaitWithCapture, "ExecWaitWithCapture");
133                         string output = null;
134                         string error = null;
135                         Assert.IsNotNull (execWaitWithCapture.Invoke (null, new object[4] { cmd, tfc, output, error }), "invoke");
136                         // it's not enough, so there's a LinkDemand on the class
137                 }
138
139                 [Test]
140                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
141                 [ExpectedException (typeof (SecurityException))]
142                 public void LinkDemand_Deny_UnmanagedCode ()
143                 {
144                         // denying anything results in a non unrestricted permission set
145                         Assert.IsNotNull (execWaitWithCapture, "ExecWaitWithCapture");
146                         string output = null;
147                         string error = null;
148                         Assert.IsNotNull (execWaitWithCapture.Invoke (null, new object[4] { cmd, tfc, output, error }), "invoke");
149                 }
150
151                 [Test]
152                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
153                 [ExpectedException (typeof (SecurityException))]
154                 public void LinkDemand_Deny_FileIO ()
155                 {
156                         Assert.IsNotNull (execWaitWithCapture, "ExecWaitWithCapture");
157                         string output = null;
158                         string error = null;
159                         Assert.IsNotNull (execWaitWithCapture.Invoke (null, new object[4] { cmd, tfc, output, error }), "invoke");
160                 }
161
162                 [Test]
163                 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
164                 [ExpectedException (typeof (SecurityException))]
165                 public void LinkDemand_Deny_Environment ()
166                 {
167                         // denying anything results in a non unrestricted permission set
168                         Assert.IsNotNull (execWaitWithCapture, "ExecWaitWithCapture");
169                         string output = null;
170                         string error = null;
171                         Assert.IsNotNull (execWaitWithCapture.Invoke (null, new object[4] { cmd, tfc, output, error }), "invoke");
172                 }
173         }
174 }