Merge pull request #1219 from panzone/los_partial_marking
[mono.git] / mcs / class / corlib / Test / System / EnvironmentCas.cs
1 //
2 // EnvironmentCas.cs - CAS unit tests for System.Environment
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 #if !MOBILE
30 using NUnit.Framework;
31
32 using System;
33 using System.Collections;
34 using System.IO;
35 using System.Security;
36 using System.Security.Permissions;
37
38 namespace MonoCasTests.System {
39
40         [TestFixture]
41         [Category ("CAS")]
42         public class EnvironmentCas {
43
44                 [SetUp]
45                 public void SetUp ()
46                 {
47                         if (!SecurityManager.SecurityEnabled)
48                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
49                 }
50
51                 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
52
53                 [Test]
54                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
55                 public void PartialTrust_PermitOnly_Environment ()
56                 {
57                         MonoTests.System.EnvironmentTest et = new MonoTests.System.EnvironmentTest ();
58                         // call most (all but arguments checking) unit tests from EnvironmentTest
59                         et.ExpandEnvironmentVariables_UnknownVariable ();
60                         et.ExpandEnvironmentVariables_KnownVariable ();
61                         et.ExpandEnvironmentVariables_NotVariable ();
62                         et.ExpandEnvironmentVariables_Alone ();
63                         et.ExpandEnvironmentVariables_End ();
64                         et.ExpandEnvironmentVariables_None ();
65                         et.ExpandEnvironmentVariables_EmptyVariable ();
66                         et.ExpandEnvironmentVariables_Double ();
67                         et.ExpandEnvironmentVariables_ComplexExpandable ();
68                         et.ExpandEnvironmentVariables_ExpandableAndNonExpandable ();
69                         et.ExpandEnvironmentVariables_ExpandableWithTrailingPercent ();
70                         et.ExpandEnvironmentVariables_ComplexExpandable2 ();
71                         et.GetEnvironmentVariables ();
72                         et.GetCommandLineArgs ();
73                 }               
74
75                 // test Demand by denying it's caller from the required privileges
76
77                 [Test]
78                 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
79                 [ExpectedException (typeof (SecurityException))]
80                 public void CommandLine ()
81                 {
82                         // now that the stack is set, call the method
83                         string s = Environment.CommandLine;
84                 }
85
86                 [Test]
87                 [EnvironmentPermission (SecurityAction.Deny, Unrestricted = true)]
88                 public void CurrentDirectory_EnvironmentPermission ()
89                 {
90                         // now that the stack is set, call the methods
91                         string cd = Environment.CurrentDirectory;
92                         Environment.CurrentDirectory = cd;
93                         // nothing to do with EnvironmentPermission
94                 }
95
96                 [Test]
97                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
98                 [ExpectedException (typeof (SecurityException))]
99                 public void CurrentDirectory_SecurityPermission ()
100                 {
101                         string cd = null;
102                         try {
103                                 cd = Environment.CurrentDirectory;
104                         }
105                         catch (SecurityException) {
106                                 // this isn't the part that should fail
107                                 Assert.Fail ("shouldn't fail when getting current dir");
108                         }
109                         // now that the stack is set, call the method
110                         Environment.CurrentDirectory = cd;
111                 }
112
113 #if !RUN_ONDOTNET || NET_4_0 // Disabled because .net 2 fails to load dll with "Failure decoding embedded permission set object" due to "/" path
114
115                 [Test]
116                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
117                 [ExpectedException (typeof (SecurityException))]
118                 public void CurrentDirectory_Get_FileIOPermission ()
119                 {
120                         // now that the stack is set, call the method
121                         string cd = Environment.CurrentDirectory;
122                 }
123                 
124                 [Test]
125                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
126                 [ExpectedException (typeof (SecurityException))]
127                 public void GetFolderPath ()
128                 {
129                         // now that the stack is set, call the method
130                         string s = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
131                 }               
132 #endif
133
134                 [Test]
135                 public void CurrentDirectory_Set_FileIOPermission ()
136                 {
137                         // this test will change the current directory
138                         // and cause tests failures elsewhere...
139                         string cd = Environment.CurrentDirectory;
140                         try {
141                                 CurrentDirectory_Set_FileIOPermission_Restricted ();
142                         }
143                         finally {
144                                 // ... unless we return to the original directory
145                                 Environment.CurrentDirectory = cd;
146                         }
147                 }
148
149                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
150                 private void CurrentDirectory_Set_FileIOPermission_Restricted ()
151                 {
152                         // now that the stack is set, call the method
153 #if RUN_ONDOTNET
154                         Environment.CurrentDirectory = "C:\\";
155 #else
156                         Environment.CurrentDirectory = "/";
157 #endif
158                         // no rights are required (as we already know the path)
159                 }
160
161                 [Test]
162                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
163                 [ExpectedException (typeof (SecurityException))]
164                 public void Exit ()
165                 {
166                         // now that the stack is set, call the method
167                         Environment.Exit (1);
168                 }
169
170                 [Test]
171                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
172                 public void ExitCode ()
173                 {
174                         // now that the stack is set, call the method
175                         int ec = Environment.ExitCode;
176                         Environment.ExitCode = ec;
177                 }
178
179                 [Test]
180                 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
181                 [ExpectedException (typeof (SecurityException))]
182                 public void ExpandEnvironmentVariables ()
183                 {
184                         // now that the stack is set, call the method
185                         string s = Environment.ExpandEnvironmentVariables ("%PATH%");
186                 }
187
188                 [Test]
189                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
190                 [Ignore ("Protected by a LinkDemand, will throw an ExecutionEngineException to stop process")]
191                 public void FailFast ()
192                 {
193                         // now that the stack is set, call the method
194                         Environment.FailFast ("bye bye");
195                 }
196
197                 [Test]
198                 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
199                 [ExpectedException (typeof (SecurityException))]
200                 public void GetCommandLineArgs ()
201                 {
202                         // now that the stack is set, call the method
203                         string[] s = Environment.GetCommandLineArgs ();
204                 }
205
206                 [Test]
207                 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
208                 [ExpectedException (typeof (SecurityException))]
209                 public void GetEnvironmentVariable ()
210                 {
211                         // now that the stack is set, call the method
212                         string s = Environment.GetEnvironmentVariable ("PATH");
213                 }
214
215                 [Test]
216                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
217                 public void GetEnvironmentVariable_Target_Process ()
218                 {
219                         // now that the stack is set, call the method
220                         string s = Environment.GetEnvironmentVariable ("PATH",
221                                 EnvironmentVariableTarget.Process);
222                         // it doesn't takes Unrestricted access to read from 
223                         // Process environment variables (like older API)
224                 }
225
226                 [Test]
227                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
228                 [ExpectedException (typeof (SecurityException))]
229                 public void GetEnvironmentVariable_Target ()
230                 {
231                         // now that the stack is set, call the method
232                         string s = Environment.GetEnvironmentVariable ("PATH",
233                                 EnvironmentVariableTarget.Machine);
234                         // it takes Unrestricted access to read from Machine
235                         // and User environment variables
236                 }
237
238                 [Test]
239                 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
240                 [ExpectedException (typeof (SecurityException))]
241                 public void GetEnvironmentVariable_Target2 ()
242                 {
243                         // note that be removing write access we're no longer
244                         // unrestricted, so this should fail even for a read
245                         string s = Environment.GetEnvironmentVariable ("PATH",
246                                 EnvironmentVariableTarget.Machine);
247                         // it takes Unrestricted access to read from Machine
248                         // and User environment variables
249                 }
250
251                 [Test]
252                 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
253                 [ExpectedException (typeof (SecurityException))]
254                 public void GetEnvironmentVariables ()
255                 {
256                         // note that whis wouldn't fail if no PATH variable
257                         // was part of the environment variables
258                         IDictionary d = Environment.GetEnvironmentVariables ();
259                 }
260
261                 [Test]
262                 [EnvironmentPermission (SecurityAction.Deny, Read = "PLEASE_NEVER_DEFINE_THIS_VARIABLE")]
263                 // Before 2.0 this required Unrestricted EnvironmentPermission
264                 [ExpectedException (typeof (SecurityException))]
265                 public void GetEnvironmentVariables_Pass ()
266                 {
267                         // this will work as long as PLEASE_NEVER_DEFINE_THIS_VARIABLE
268                         // isn't an environment variable
269                         IDictionary d = Environment.GetEnvironmentVariables ();
270                 }
271
272                 [Test]
273                 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
274                 [ExpectedException (typeof (SecurityException))]
275                 public void GetEnvironmentVariables_Target ()
276                 {
277                         // now that the stack is set, call the method
278                         IDictionary d = Environment.GetEnvironmentVariables (
279                                 EnvironmentVariableTarget.Machine);
280                         // it takes Unrestricted access to read from Machine
281                         // and User environment variables
282                 }
283
284                 [Test]
285                 [EnvironmentPermission (SecurityAction.Deny, Write = "PATH")]
286                 [ExpectedException (typeof (SecurityException))]
287                 public void GetEnvironmentVariables_Target2 ()
288                 {
289                         // note that be removing write access we're no longer
290                         // unrestricted, so this should fail even for a read
291                         IDictionary d = Environment.GetEnvironmentVariables (
292                                 EnvironmentVariableTarget.Machine);
293                         // it takes Unrestricted access to read from Machine
294                         // and User environment variables
295                 }
296
297                 [Test]
298                 [EnvironmentPermission (SecurityAction.Deny, Unrestricted = true)]
299                 public void GetFolderPath_EnvironmentPermission ()
300                 {
301                         // we can get all folders without any EnvironmentPermission
302                         // note: Mono use some environment variable to create the paths
303                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "MyDocuments");
304                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Desktop), "Desktop");
305                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.MyComputer), "MyComputer");
306                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Personal), "Personal");
307                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Favorites), "Favorites");
308                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Startup), "Startup");
309                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Recent), "Recent");
310                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.SendTo), "SendTo");
311                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.StartMenu), "StartMenu");
312                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.MyMusic), "MyMusic");
313                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.DesktopDirectory), "DesktopDirectory");
314                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Templates), "Templates");
315                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "ApplicationData");
316                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), "LocalApplicationData");
317                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.InternetCache), "InternetCache");
318                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Cookies), "Cookies");
319                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.History), "History");
320                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.CommonApplicationData), "CommonApplicationData");
321                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.System), "System");
322                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.ProgramFiles), "ProgramFiles");
323                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.MyPictures), "MyPictures");
324                         Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.CommonProgramFiles), "CommonProgramFiles");
325                 }
326
327                 [Test]
328                 [EnvironmentPermission (SecurityAction.Deny, Write = "PATH")]
329                 [ExpectedException (typeof (SecurityException))]
330                 public void GetLogicalDrives ()
331                 {
332                         // note that be removing write access we're no longer
333                         // unrestricted, so this should fail even if it's not
334                         // related to environment variables at all!
335                         string[] s = Environment.GetLogicalDrives ();
336                 }
337
338                 [Test]
339                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
340                 public void HasShutdownStarted ()
341                 {
342                         // now that the stack is set, call the methods
343                         bool b = Environment.HasShutdownStarted;
344                 }
345
346                 [Test]
347                 [EnvironmentPermission (SecurityAction.Deny, Read = "COMPUTERNAME")]
348                 [ExpectedException (typeof (SecurityException))]
349                 public void MachineName_EnvironmentPermission ()
350                 {
351                         // now that the stack is set, call the method
352                         string s = Environment.MachineName;
353                 }
354
355                 [Test]
356                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode =true)]
357                 [ExpectedException (typeof (SecurityException))]
358                 public void MachineName_SecurityPermission ()
359                 {
360                         // now that the stack is set, call the method
361                         string s = Environment.MachineName;
362                 }
363
364                 [Test]
365                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
366                 public void NewLine ()
367                 {
368                         // now that the stack is set, call the methods
369                         string s = Environment.NewLine;
370                 }
371
372                 [Test]
373                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
374                 public void OSVersion ()
375                 {
376                         // now that the stack is set, call the methods
377                         OperatingSystem os = Environment.OSVersion;
378                 }
379
380                 [Test]
381                 [EnvironmentPermission (SecurityAction.Deny, Read = "NUMBER_OF_PROCESSORS")]
382                 [ExpectedException (typeof (SecurityException))]
383                 public void ProcessorCount ()
384                 {
385                         // now that the stack is set, call the methods
386                         int i = Environment.ProcessorCount;
387                 }
388                 [Test]
389                 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
390                 [ExpectedException (typeof (SecurityException))]
391                 public void SetEnvironmentVariable ()
392                 {
393                         // now that the stack is set, call the method
394                         Environment.SetEnvironmentVariable ("MONO", "GO");
395                 }
396
397                 [Test]
398                 [EnvironmentPermission (SecurityAction.Deny, Write = "PATH")]
399                 [ExpectedException (typeof (SecurityException))]
400                 public void SetEnvironmentVariable2 ()
401                 {
402                         // Note: strangely (but documented as such) it takes
403                         // unrestricted access to call SetEnvironmentVariable
404                         // so denying write to PATH also deny write to any
405                         // other environment variable, like the MONO variable
406                         Environment.SetEnvironmentVariable ("MONO", "GO");
407                 }
408
409                 [Test]
410                 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
411                 [ExpectedException (typeof (SecurityException))]
412                 public void SetEnvironmentVariable_Target ()
413                 {
414                         // now that the stack is set, call the method
415                         Environment.SetEnvironmentVariable ("MONO", "GO",
416                                 EnvironmentVariableTarget.Machine);
417                         // it takes Unrestricted access to read from Machine
418                         // and User environment variables
419                 }
420
421                 [Test]
422                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
423                 [ExpectedException (typeof (SecurityException))]
424                 public void SetEnvironmentVariable_Target2 ()
425                 {
426                         // note that be removing read access we're no longer
427                         // unrestricted, so this should fail even for a write
428                         Environment.SetEnvironmentVariable ("MONO", "GO",
429                                 EnvironmentVariableTarget.Machine);
430                         // it takes Unrestricted access to read from Machine
431                         // and User environment variables
432                 }
433
434                 [Test]
435                 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
436                 [ExpectedException (typeof (SecurityException))]
437                 public void StackTrace ()
438                 {
439                         // note that be removing write access we're no longer
440                         // unrestricted, so this should fail even if the call
441                         // stack isn't related to writing in environment variables
442                         string s = Environment.StackTrace;
443                 }
444
445                 [Test]
446 #if false && RUN_ONDOTNET
447                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "C:\\")]
448                 [ExpectedException (typeof (SecurityException))]
449 #endif
450                 public void SystemDirectory ()
451                 {
452                         // now that the stack is set, call the method
453                         string s = Environment.SystemDirectory;
454                         // note: Under Linux SystemDirectory is empty (so it's not a path)
455                         Assert.AreEqual (String.Empty, s);
456                 }
457
458                 [Test]
459                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
460                 public void TickCount ()
461                 {
462                         // now that the stack is set, call the methods
463                         int i = Environment.TickCount;
464                 }
465
466                 [Test]
467                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERDOMAINNAME")]
468                 [ExpectedException (typeof (SecurityException))]
469                 public void UserDomainName ()
470                 {
471                         // now that the stack is set, call the methods
472                         string s = Environment.UserDomainName;
473                 }
474
475                 [Test]
476                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
477                 public void UserInteractive ()
478                 {
479                         // now that the stack is set, call the methods
480                         bool b = Environment.UserInteractive;
481                 }
482
483                 [Test]
484                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
485                 [ExpectedException (typeof (SecurityException))]
486                 public void UserName ()
487                 {
488                         // now that the stack is set, call the methods
489                         string s = Environment.UserName;
490                         // note: the UserName property doesn't really read the 
491                         // USERNAME variable. You can test this by impersonating
492                         // another user (see unit tests)
493                 }
494
495                 [Test]
496                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
497                 public void Version ()
498                 {
499                         // now that the stack is set, call the methods
500                         Version v = Environment.Version;
501                 }
502
503                 [Test]
504                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
505                 [ExpectedException (typeof (SecurityException))]
506                 public void WorkingSet ()
507                 {
508                         // note that be removing read access to USERNAME we're 
509                         // no longer unrestricted, so this should fail even if
510                         // the working set is unrelated to the username
511                         long l = Environment.WorkingSet;
512                 }
513         }
514 }
515
516 #endif