| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- //
- // TempFileCollectionCas.cs
- // - CAS unit tests for System.CodeDom.Compiler.TempFileCollection
- //
- // Author:
- // Sebastien Pouliot <[email protected]>
- //
- // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using NUnit.Framework;
- using System;
- using System.CodeDom.Compiler;
- using System.Collections;
- using System.IO;
- using System.Reflection;
- using System.Security;
- using System.Security.Permissions;
- namespace MonoCasTests.System.CodeDom.Compiler {
- [TestFixture]
- [Category ("CAS")]
- public class TempFileCollectionCas {
- private string temp;
- private string[] array;
- [TestFixtureSetUp]
- public void FixtureSetUp ()
- {
- // at full trust
- temp = Path.GetTempPath ();
- array = new string[1];
- }
- [SetUp]
- public void SetUp ()
- {
- if (!SecurityManager.SecurityEnabled)
- Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- public void Constructor0_Deny_Unrestricted ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- Assert.AreEqual (0, tfc.Count, "Count");
- Assert.IsFalse (tfc.KeepFiles, "KeepFiles");
- Assert.AreEqual (String.Empty, tfc.TempDir, "TempDir");
- tfc.AddFile ("main.cs", false);
- tfc.CopyTo (array, 0);
- tfc.Delete ();
- (tfc as IDisposable).Dispose ();
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- public void Constructor1_Deny_Unrestricted ()
- {
- TempFileCollection tfc = new TempFileCollection (temp);
- Assert.AreEqual (0, tfc.Count, "Count");
- Assert.IsFalse (tfc.KeepFiles, "KeepFiles");
- Assert.AreEqual (temp, tfc.TempDir, "TempDir");
- tfc.AddFile ("main.cs", false);
- tfc.CopyTo (array, 0);
- tfc.Delete ();
- (tfc as IDisposable).Dispose ();
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- public void Constructor2_Deny_Unrestricted ()
- {
- TempFileCollection tfc = new TempFileCollection (temp, true);
- Assert.AreEqual (0, tfc.Count, "Count");
- Assert.IsTrue (tfc.KeepFiles, "KeepFiles");
- Assert.AreEqual (temp, tfc.TempDir, "TempDir");
- tfc.AddFile ("main.cs", false);
- tfc.CopyTo (array, 0);
- tfc.Delete ();
- (tfc as IDisposable).Dispose ();
- }
- [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- private void Cache (TempFileCollection tfc)
- {
- Assert.IsNotNull (tfc.BasePath, "BasePath-Restricted");
- // no failure because the BasePath was calculated when
- // unrestricted and cached for further calls. This is
- // design tradeoff between security and performance but
- // shouldn't occurs much in real life (but it's worth
- // keeping in mind ;-).
- }
- [Test]
- public void BasePath_Caching ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- Assert.IsNotNull (tfc.BasePath, "BasePath-Unrestricted");
- Cache (tfc);
- }
- [Test]
- [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- public void BasePath_PermitOnly_EnvironmentPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // good but not enough
- Assert.IsNotNull (tfc.BasePath, "BasePath");
- }
- [Test]
- [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
- [ExpectedException (typeof (SecurityException))]
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
- #endif
- public void BasePath_Deny_EnvironmentPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // requires Unrestricted Environment access
- Assert.IsNotNull (tfc.BasePath, "BasePath");
- }
- [Test]
- [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
- #endif
- public void BasePath_PermitOnly_FileIOPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // good but not enough
- Assert.IsNotNull (tfc.BasePath, "BasePath");
- }
- [Test]
- [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- public void BasePath_Deny_FileIOPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // requires path access
- Assert.IsNotNull (tfc.BasePath, "BasePath");
- }
- [Test]
- [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- public void AddExtension_PermitOnly_EnvironmentPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // good but not enough
- tfc.AddExtension (".cs");
- }
- [Test]
- [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
- [ExpectedException (typeof (SecurityException))]
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
- #endif
- public void AddExtension_Deny_EnvironmentPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // requires Unrestricted Environment access
- tfc.AddExtension (".cs");
- }
- [Test]
- [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
- #endif
- public void AddExtension_PermitOnly_FileIOPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // good but not enough
- tfc.AddExtension (".cs");
- }
- [Test]
- [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- public void AddExtension_Deny_FileIOPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // requires path access
- tfc.AddExtension (".cs");
- }
- [Test]
- [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- public void AddExtension2_PermitOnly_EnvironmentPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // good but not enough
- tfc.AddExtension (".cx", true);
- }
- [Test]
- [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
- [ExpectedException (typeof (SecurityException))]
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
- #endif
- public void AddExtension2_Deny_EnvironmentPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // requires Unrestricted Environment access
- tfc.AddExtension (".cx", true);
- }
- [Test]
- [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
- #endif
- public void AddExtension2_PermitOnly_FileIOPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // good but not enough
- tfc.AddExtension (".cx", true);
- }
- [Test]
- [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
- [ExpectedException (typeof (SecurityException))]
- public void AddExtension2_Deny_FileIOPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // requires path access
- tfc.AddExtension (".cx", true);
- }
- [Test]
- [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
- public void PermitOnly_FileIOPermission_EnvironmentPermission ()
- {
- TempFileCollection tfc = new TempFileCollection ();
- // ok
- Assert.IsNotNull (tfc.BasePath, "BasePath");
- tfc.AddExtension (".cs");
- tfc.AddExtension (".cx", true);
- // both AddExtension methods depends on BasePath
- Assert.AreEqual (String.Empty, tfc.TempDir, "TempDir");
- // and that last one is *important*
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- public void ICollection_Deny_Unrestricted ()
- {
- ICollection coll = (ICollection) new TempFileCollection ();
- Assert.AreEqual (0, coll.Count, "Count");
- Assert.IsNull (coll.SyncRoot, "SyncRoot");
- Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
- coll.CopyTo (array, 0);
- }
- [Test]
- [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
- public void IEnumerable_Deny_Unrestricted ()
- {
- IEnumerable e = (IEnumerable) new TempFileCollection ();
- Assert.IsNotNull (e.GetEnumerator (), "GetEnumerator");
- }
- [Test]
- public void LinkDemand_No_Restriction ()
- {
- ConstructorInfo ci = typeof (TempFileCollection).GetConstructor (new Type[0]);
- Assert.IsNotNull (ci, "default .ctor");
- Assert.IsNotNull (ci.Invoke (null), "invoke");
- }
- [Test]
- [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
- [ExpectedException (typeof (SecurityException))]
- public void LinkDemand_Deny_Unrestricted ()
- {
- // denying anything results in a non unrestricted permission set
- ConstructorInfo ci = typeof (TempFileCollection).GetConstructor (new Type[0]);
- Assert.IsNotNull (ci, "default .ctor");
- Assert.IsNotNull (ci.Invoke (null), "invoke");
- }
- }
- }
|