2 // EnvironmentCas.cs - CAS unit tests for System.Environment
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.
30 using NUnit.Framework;
33 using System.Collections;
35 using System.Security;
36 using System.Security.Permissions;
38 namespace MonoCasTests.System {
42 public class EnvironmentCas {
47 if (!SecurityManager.SecurityEnabled)
48 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
51 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
54 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
55 public void PartialTrust_PermitOnly_Environment ()
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 ();
75 // test Demand by denying it's caller from the required privileges
78 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
79 [ExpectedException (typeof (SecurityException))]
80 public void CommandLine ()
82 // now that the stack is set, call the method
83 string s = Environment.CommandLine;
87 [EnvironmentPermission (SecurityAction.Deny, Unrestricted = true)]
88 public void CurrentDirectory_EnvironmentPermission ()
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
97 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
98 [ExpectedException (typeof (SecurityException))]
99 public void CurrentDirectory_SecurityPermission ()
103 cd = Environment.CurrentDirectory;
105 catch (SecurityException) {
106 // this isn't the part that should fail
107 Assert.Fail ("shouldn't fail when getting current dir");
109 // now that the stack is set, call the method
110 Environment.CurrentDirectory = cd;
115 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
116 [ExpectedException (typeof (SecurityException))]
117 public void CurrentDirectory_Get_FileIOPermission ()
119 // now that the stack is set, call the method
120 string cd = Environment.CurrentDirectory;
124 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
125 [ExpectedException (typeof (SecurityException))]
126 public void GetFolderPath ()
128 // now that the stack is set, call the method
129 string s = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
133 public void CurrentDirectory_Set_FileIOPermission ()
135 // this test will change the current directory
136 // and cause tests failures elsewhere...
137 string cd = Environment.CurrentDirectory;
139 CurrentDirectory_Set_FileIOPermission_Restricted ();
142 // ... unless we return to the original directory
143 Environment.CurrentDirectory = cd;
147 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
148 private void CurrentDirectory_Set_FileIOPermission_Restricted ()
150 // now that the stack is set, call the method
152 Environment.CurrentDirectory = "C:\\";
154 Environment.CurrentDirectory = "/";
156 // no rights are required (as we already know the path)
160 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
161 [ExpectedException (typeof (SecurityException))]
164 // now that the stack is set, call the method
165 Environment.Exit (1);
169 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
170 public void ExitCode ()
172 // now that the stack is set, call the method
173 int ec = Environment.ExitCode;
174 Environment.ExitCode = ec;
178 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
179 [ExpectedException (typeof (SecurityException))]
180 public void ExpandEnvironmentVariables ()
182 // now that the stack is set, call the method
183 string s = Environment.ExpandEnvironmentVariables ("%PATH%");
187 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
188 [Ignore ("Protected by a LinkDemand, will throw an ExecutionEngineException to stop process")]
189 public void FailFast ()
191 // now that the stack is set, call the method
192 Environment.FailFast ("bye bye");
196 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
197 [ExpectedException (typeof (SecurityException))]
198 public void GetCommandLineArgs ()
200 // now that the stack is set, call the method
201 string[] s = Environment.GetCommandLineArgs ();
205 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
206 [ExpectedException (typeof (SecurityException))]
207 public void GetEnvironmentVariable ()
209 // now that the stack is set, call the method
210 string s = Environment.GetEnvironmentVariable ("PATH");
214 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
215 public void GetEnvironmentVariable_Target_Process ()
217 // now that the stack is set, call the method
218 string s = Environment.GetEnvironmentVariable ("PATH",
219 EnvironmentVariableTarget.Process);
220 // it doesn't takes Unrestricted access to read from
221 // Process environment variables (like older API)
225 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
226 [ExpectedException (typeof (SecurityException))]
227 public void GetEnvironmentVariable_Target ()
229 // now that the stack is set, call the method
230 string s = Environment.GetEnvironmentVariable ("PATH",
231 EnvironmentVariableTarget.Machine);
232 // it takes Unrestricted access to read from Machine
233 // and User environment variables
237 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
238 [ExpectedException (typeof (SecurityException))]
239 public void GetEnvironmentVariable_Target2 ()
241 // note that be removing write access we're no longer
242 // unrestricted, so this should fail even for a read
243 string s = Environment.GetEnvironmentVariable ("PATH",
244 EnvironmentVariableTarget.Machine);
245 // it takes Unrestricted access to read from Machine
246 // and User environment variables
250 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
251 [ExpectedException (typeof (SecurityException))]
252 public void GetEnvironmentVariables ()
254 // note that whis wouldn't fail if no PATH variable
255 // was part of the environment variables
256 IDictionary d = Environment.GetEnvironmentVariables ();
260 [EnvironmentPermission (SecurityAction.Deny, Read = "PLEASE_NEVER_DEFINE_THIS_VARIABLE")]
261 // Before 2.0 this required Unrestricted EnvironmentPermission
262 [ExpectedException (typeof (SecurityException))]
263 public void GetEnvironmentVariables_Pass ()
265 // this will work as long as PLEASE_NEVER_DEFINE_THIS_VARIABLE
266 // isn't an environment variable
267 IDictionary d = Environment.GetEnvironmentVariables ();
271 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
272 [ExpectedException (typeof (SecurityException))]
273 public void GetEnvironmentVariables_Target ()
275 // now that the stack is set, call the method
276 IDictionary d = Environment.GetEnvironmentVariables (
277 EnvironmentVariableTarget.Machine);
278 // it takes Unrestricted access to read from Machine
279 // and User environment variables
283 [EnvironmentPermission (SecurityAction.Deny, Write = "PATH")]
284 [ExpectedException (typeof (SecurityException))]
285 public void GetEnvironmentVariables_Target2 ()
287 // note that be removing write access we're no longer
288 // unrestricted, so this should fail even for a read
289 IDictionary d = Environment.GetEnvironmentVariables (
290 EnvironmentVariableTarget.Machine);
291 // it takes Unrestricted access to read from Machine
292 // and User environment variables
296 [EnvironmentPermission (SecurityAction.Deny, Unrestricted = true)]
297 public void GetFolderPath_EnvironmentPermission ()
299 // we can get all folders without any EnvironmentPermission
300 // note: Mono use some environment variable to create the paths
301 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "MyDocuments");
302 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Desktop), "Desktop");
303 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.MyComputer), "MyComputer");
304 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Personal), "Personal");
305 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Favorites), "Favorites");
306 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Startup), "Startup");
307 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Recent), "Recent");
308 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.SendTo), "SendTo");
309 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.StartMenu), "StartMenu");
310 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.MyMusic), "MyMusic");
311 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.DesktopDirectory), "DesktopDirectory");
312 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Templates), "Templates");
313 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "ApplicationData");
314 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), "LocalApplicationData");
315 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.InternetCache), "InternetCache");
316 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.Cookies), "Cookies");
317 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.History), "History");
318 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.CommonApplicationData), "CommonApplicationData");
319 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.System), "System");
320 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.ProgramFiles), "ProgramFiles");
321 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.MyPictures), "MyPictures");
322 Assert.IsNotNull (Environment.GetFolderPath (Environment.SpecialFolder.CommonProgramFiles), "CommonProgramFiles");
326 [EnvironmentPermission (SecurityAction.Deny, Write = "PATH")]
327 [ExpectedException (typeof (SecurityException))]
328 public void GetLogicalDrives ()
330 // note that be removing write access we're no longer
331 // unrestricted, so this should fail even if it's not
332 // related to environment variables at all!
333 string[] s = Environment.GetLogicalDrives ();
337 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
338 public void HasShutdownStarted ()
340 // now that the stack is set, call the methods
341 bool b = Environment.HasShutdownStarted;
345 [EnvironmentPermission (SecurityAction.Deny, Read = "COMPUTERNAME")]
346 [ExpectedException (typeof (SecurityException))]
347 public void MachineName_EnvironmentPermission ()
349 // now that the stack is set, call the method
350 string s = Environment.MachineName;
354 [SecurityPermission (SecurityAction.Deny, UnmanagedCode =true)]
355 [ExpectedException (typeof (SecurityException))]
356 public void MachineName_SecurityPermission ()
358 // now that the stack is set, call the method
359 string s = Environment.MachineName;
363 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
364 public void NewLine ()
366 // now that the stack is set, call the methods
367 string s = Environment.NewLine;
371 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
372 public void OSVersion ()
374 // now that the stack is set, call the methods
375 OperatingSystem os = Environment.OSVersion;
379 [EnvironmentPermission (SecurityAction.Deny, Read = "NUMBER_OF_PROCESSORS")]
380 [ExpectedException (typeof (SecurityException))]
381 public void ProcessorCount ()
383 // now that the stack is set, call the methods
384 int i = Environment.ProcessorCount;
387 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
388 [ExpectedException (typeof (SecurityException))]
389 public void SetEnvironmentVariable ()
391 // now that the stack is set, call the method
392 Environment.SetEnvironmentVariable ("MONO", "GO");
396 [EnvironmentPermission (SecurityAction.Deny, Write = "PATH")]
397 [ExpectedException (typeof (SecurityException))]
398 public void SetEnvironmentVariable2 ()
400 // Note: strangely (but documented as such) it takes
401 // unrestricted access to call SetEnvironmentVariable
402 // so denying write to PATH also deny write to any
403 // other environment variable, like the MONO variable
404 Environment.SetEnvironmentVariable ("MONO", "GO");
408 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
409 [ExpectedException (typeof (SecurityException))]
410 public void SetEnvironmentVariable_Target ()
412 // now that the stack is set, call the method
413 Environment.SetEnvironmentVariable ("MONO", "GO",
414 EnvironmentVariableTarget.Machine);
415 // it takes Unrestricted access to read from Machine
416 // and User environment variables
420 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
421 [ExpectedException (typeof (SecurityException))]
422 public void SetEnvironmentVariable_Target2 ()
424 // note that be removing read access we're no longer
425 // unrestricted, so this should fail even for a write
426 Environment.SetEnvironmentVariable ("MONO", "GO",
427 EnvironmentVariableTarget.Machine);
428 // it takes Unrestricted access to read from Machine
429 // and User environment variables
433 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
434 [ExpectedException (typeof (SecurityException))]
435 public void StackTrace ()
437 // note that be removing write access we're no longer
438 // unrestricted, so this should fail even if the call
439 // stack isn't related to writing in environment variables
440 string s = Environment.StackTrace;
444 #if false && RUN_ONDOTNET
445 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "C:\\")]
446 [ExpectedException (typeof (SecurityException))]
448 public void SystemDirectory ()
450 // now that the stack is set, call the method
451 string s = Environment.SystemDirectory;
452 // note: Under Linux SystemDirectory is empty (so it's not a path)
453 Assert.AreEqual (String.Empty, s);
457 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
458 public void TickCount ()
460 // now that the stack is set, call the methods
461 int i = Environment.TickCount;
465 [EnvironmentPermission (SecurityAction.Deny, Read = "USERDOMAINNAME")]
466 [ExpectedException (typeof (SecurityException))]
467 public void UserDomainName ()
469 // now that the stack is set, call the methods
470 string s = Environment.UserDomainName;
474 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
475 public void UserInteractive ()
477 // now that the stack is set, call the methods
478 bool b = Environment.UserInteractive;
482 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
483 [ExpectedException (typeof (SecurityException))]
484 public void UserName ()
486 // now that the stack is set, call the methods
487 string s = Environment.UserName;
488 // note: the UserName property doesn't really read the
489 // USERNAME variable. You can test this by impersonating
490 // another user (see unit tests)
494 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
495 public void Version ()
497 // now that the stack is set, call the methods
498 Version v = Environment.Version;
502 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
503 [ExpectedException (typeof (SecurityException))]
504 public void WorkingSet ()
506 // note that be removing read access to USERNAME we're
507 // no longer unrestricted, so this should fail even if
508 // the working set is unrelated to the username
509 long l = Environment.WorkingSet;