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;
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
116 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
117 [ExpectedException (typeof (SecurityException))]
118 public void CurrentDirectory_Get_FileIOPermission ()
120 // now that the stack is set, call the method
121 string cd = Environment.CurrentDirectory;
125 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
126 [ExpectedException (typeof (SecurityException))]
127 public void GetFolderPath ()
129 // now that the stack is set, call the method
130 string s = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
135 public void CurrentDirectory_Set_FileIOPermission ()
137 // this test will change the current directory
138 // and cause tests failures elsewhere...
139 string cd = Environment.CurrentDirectory;
141 CurrentDirectory_Set_FileIOPermission_Restricted ();
144 // ... unless we return to the original directory
145 Environment.CurrentDirectory = cd;
149 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
150 private void CurrentDirectory_Set_FileIOPermission_Restricted ()
152 // now that the stack is set, call the method
154 Environment.CurrentDirectory = "C:\\";
156 Environment.CurrentDirectory = "/";
158 // no rights are required (as we already know the path)
162 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
163 [ExpectedException (typeof (SecurityException))]
166 // now that the stack is set, call the method
167 Environment.Exit (1);
171 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
172 public void ExitCode ()
174 // now that the stack is set, call the method
175 int ec = Environment.ExitCode;
176 Environment.ExitCode = ec;
180 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
181 [ExpectedException (typeof (SecurityException))]
182 public void ExpandEnvironmentVariables ()
184 // now that the stack is set, call the method
185 string s = Environment.ExpandEnvironmentVariables ("%PATH%");
189 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
190 [Ignore ("Protected by a LinkDemand, will throw an ExecutionEngineException to stop process")]
191 public void FailFast ()
193 // now that the stack is set, call the method
194 Environment.FailFast ("bye bye");
198 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
199 [ExpectedException (typeof (SecurityException))]
200 public void GetCommandLineArgs ()
202 // now that the stack is set, call the method
203 string[] s = Environment.GetCommandLineArgs ();
207 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
208 [ExpectedException (typeof (SecurityException))]
209 public void GetEnvironmentVariable ()
211 // now that the stack is set, call the method
212 string s = Environment.GetEnvironmentVariable ("PATH");
216 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
217 public void GetEnvironmentVariable_Target_Process ()
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)
227 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
228 [ExpectedException (typeof (SecurityException))]
229 public void GetEnvironmentVariable_Target ()
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
239 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
240 [ExpectedException (typeof (SecurityException))]
241 public void GetEnvironmentVariable_Target2 ()
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
252 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
253 [ExpectedException (typeof (SecurityException))]
254 public void GetEnvironmentVariables ()
256 // note that whis wouldn't fail if no PATH variable
257 // was part of the environment variables
258 IDictionary d = Environment.GetEnvironmentVariables ();
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 ()
267 // this will work as long as PLEASE_NEVER_DEFINE_THIS_VARIABLE
268 // isn't an environment variable
269 IDictionary d = Environment.GetEnvironmentVariables ();
273 [EnvironmentPermission (SecurityAction.Deny, Read = "PATH")]
274 [ExpectedException (typeof (SecurityException))]
275 public void GetEnvironmentVariables_Target ()
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
285 [EnvironmentPermission (SecurityAction.Deny, Write = "PATH")]
286 [ExpectedException (typeof (SecurityException))]
287 public void GetEnvironmentVariables_Target2 ()
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
298 [EnvironmentPermission (SecurityAction.Deny, Unrestricted = true)]
299 public void GetFolderPath_EnvironmentPermission ()
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");
328 [EnvironmentPermission (SecurityAction.Deny, Write = "PATH")]
329 [ExpectedException (typeof (SecurityException))]
330 public void GetLogicalDrives ()
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 ();
339 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
340 public void HasShutdownStarted ()
342 // now that the stack is set, call the methods
343 bool b = Environment.HasShutdownStarted;
347 [EnvironmentPermission (SecurityAction.Deny, Read = "COMPUTERNAME")]
348 [ExpectedException (typeof (SecurityException))]
349 public void MachineName_EnvironmentPermission ()
351 // now that the stack is set, call the method
352 string s = Environment.MachineName;
356 [SecurityPermission (SecurityAction.Deny, UnmanagedCode =true)]
357 [ExpectedException (typeof (SecurityException))]
358 public void MachineName_SecurityPermission ()
360 // now that the stack is set, call the method
361 string s = Environment.MachineName;
365 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
366 public void NewLine ()
368 // now that the stack is set, call the methods
369 string s = Environment.NewLine;
373 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
374 public void OSVersion ()
376 // now that the stack is set, call the methods
377 OperatingSystem os = Environment.OSVersion;
381 [EnvironmentPermission (SecurityAction.Deny, Read = "NUMBER_OF_PROCESSORS")]
382 [ExpectedException (typeof (SecurityException))]
383 public void ProcessorCount ()
385 // now that the stack is set, call the methods
386 int i = Environment.ProcessorCount;
389 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
390 [ExpectedException (typeof (SecurityException))]
391 public void SetEnvironmentVariable ()
393 // now that the stack is set, call the method
394 Environment.SetEnvironmentVariable ("MONO", "GO");
398 [EnvironmentPermission (SecurityAction.Deny, Write = "PATH")]
399 [ExpectedException (typeof (SecurityException))]
400 public void SetEnvironmentVariable2 ()
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");
410 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
411 [ExpectedException (typeof (SecurityException))]
412 public void SetEnvironmentVariable_Target ()
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
422 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
423 [ExpectedException (typeof (SecurityException))]
424 public void SetEnvironmentVariable_Target2 ()
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
435 [EnvironmentPermission (SecurityAction.Deny, Write = "MONO")]
436 [ExpectedException (typeof (SecurityException))]
437 public void StackTrace ()
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;
446 #if false && RUN_ONDOTNET
447 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "C:\\")]
448 [ExpectedException (typeof (SecurityException))]
450 public void SystemDirectory ()
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);
459 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
460 public void TickCount ()
462 // now that the stack is set, call the methods
463 int i = Environment.TickCount;
467 [EnvironmentPermission (SecurityAction.Deny, Read = "USERDOMAINNAME")]
468 [ExpectedException (typeof (SecurityException))]
469 public void UserDomainName ()
471 // now that the stack is set, call the methods
472 string s = Environment.UserDomainName;
476 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
477 public void UserInteractive ()
479 // now that the stack is set, call the methods
480 bool b = Environment.UserInteractive;
484 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
485 [ExpectedException (typeof (SecurityException))]
486 public void UserName ()
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)
496 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
497 public void Version ()
499 // now that the stack is set, call the methods
500 Version v = Environment.Version;
504 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
505 [ExpectedException (typeof (SecurityException))]
506 public void WorkingSet ()
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;