public void PermissionStateUnrestricted () { PermissionSet ps = new PermissionSet (PermissionState.Unrestricted); Assert.IsTrue (ps.IsUnrestricted (), "PermissionStateUnrestricted.IsUnrestricted"); Assert.IsTrue (!ps.IsEmpty (), "PermissionStateUnrestricted.IsEmpty"); Assert.IsTrue (!ps.IsReadOnly, "PermissionStateUnrestricted.IsReadOnly"); Assert.AreEqual (ps.ToXml ().ToString (), ps.ToString (), "PermissionStateUnrestricted.ToXml().ToString()==ToString()"); Assert.IsTrue (!ps.ContainsNonCodeAccessPermissions (), "ContainsNonCodeAccessPermissions"); }
public void PermissionSetNull () { // no exception is thrown PermissionSet ps = new PermissionSet (null); Assert.IsTrue (!ps.IsUnrestricted (), "PermissionStateNull.IsUnrestricted"); Assert.IsTrue (ps.IsEmpty (), "PermissionStateNull.IsEmpty"); Assert.IsTrue (!ps.IsReadOnly, "PermissionStateNull.IsReadOnly"); Assert.AreEqual (ps.ToXml ().ToString (), ps.ToString (), "PermissionStateNull.ToXml().ToString()==ToString()"); Assert.IsTrue (!ps.ContainsNonCodeAccessPermissions (), "ContainsNonCodeAccessPermissions"); }
public void PermissionSetPermissionSet () { FileDialogPermission fdp = new FileDialogPermission (FileDialogPermissionAccess.Open); PermissionSet ps1 = new PermissionSet (PermissionState.None); ps1.AddPermission (fdp); Assert.IsTrue (!ps1.IsEmpty (), "ps1.IsEmpty"); PermissionSet ps = new PermissionSet (ps1); Assert.IsTrue (!ps.IsUnrestricted (), "PermissionSetPermissionSet.IsUnrestricted"); Assert.IsTrue (!ps.IsEmpty (), "PermissionSetPermissionSet.IsEmpty"); Assert.IsTrue (!ps.IsReadOnly, "PermissionSetPermissionSet.IsReadOnly"); Assert.AreEqual (ps.ToXml ().ToString (), ps.ToString (), "PermissionSetPermissionSet.ToXml().ToString()==ToString()"); Assert.IsTrue (!ps.ContainsNonCodeAccessPermissions (), "ContainsNonCodeAccessPermissions"); }
public void ContainsNonCodeAccessPermissions () { PermissionSet ps = new PermissionSet (PermissionState.None); Assert.IsTrue (!ps.ContainsNonCodeAccessPermissions (), "Empty"); SecurityPermission sp = new SecurityPermission (PermissionState.Unrestricted); ps.AddPermission (sp); Assert.IsTrue (!ps.ContainsNonCodeAccessPermissions (), "SecurityPermission"); PrincipalPermission pp = new PrincipalPermission ("mono", "hacker"); ps.AddPermission (pp); Assert.IsTrue (ps.ContainsNonCodeAccessPermissions (), "PrincipalPermission"); }
public void AddPermission_NonCasPermissionUnrestricted_Unrestricted () { PermissionSet ps = new PermissionSet (PermissionState.Unrestricted); ps.AddPermission (new PrincipalPermission (PermissionState.Unrestricted)); Assert.AreEqual (0, ps.Count, "Count"); Assert.IsTrue (!ps.ContainsNonCodeAccessPermissions (), "ContainsNonCodeAccessPermissions"); }
public void PermitOnly_NonCasPermission () { PermissionSet ps = new PermissionSet (PermissionState.None); ps.AddPermission (new PrincipalPermission (PermissionState.None)); Assert.IsTrue (ps.ContainsNonCodeAccessPermissions (), "ContainsNonCodeAccessPermissions"); Assert.AreEqual (1, ps.Count, "Count"); ps.PermitOnly (); // it's simply ignored }
public void PermissionSetNamedPermissionSet () { NamedPermissionSet nps = new NamedPermissionSet ("Test", PermissionState.Unrestricted); PermissionSet ps = new PermissionSet (nps); Assert.IsTrue (ps.IsUnrestricted (), "IsUnrestricted"); Assert.IsTrue (!ps.ContainsNonCodeAccessPermissions (), "ContainsNonCodeAccessPermissions"); }
/// <include file='doc\PermissionSet.uex' path='docs/doc[@for="PermissionSet.Union"]/*' /> public virtual PermissionSet Union(PermissionSet other) { PermissionSet pset = null; // if other is null or empty, return a clone of myself if (other == null || other.IsEmpty()) { pset = this.Copy(); } else if ((this.IsUnrestricted() && !this.ContainsNonCodeAccessPermissions()) || (other.IsUnrestricted() && !other.ContainsNonCodeAccessPermissions())) { // if either this or other are unrestricted, return an unrestricted set pset = new PermissionSet(null, null); pset.m_Unrestricted = true; pset.m_unrestrictedPermSet = null; if (this.m_normalPermSet == null) pset.m_normalPermSet = other.m_normalPermSet == null ? null : new TokenBasedSet( other.m_normalPermSet ); else pset.m_normalPermSet = this.m_normalPermSet.Union( other.m_normalPermSet ); } else if (this.IsEmpty()) { pset = other.Copy(); } else { pset = new PermissionSet(null, null); if (this.m_unrestrictedPermSet == null) pset.m_unrestrictedPermSet = other.m_unrestrictedPermSet == null ? null : new TokenBasedSet( other.m_unrestrictedPermSet ); else pset.m_unrestrictedPermSet = this.m_unrestrictedPermSet.Union( other.m_unrestrictedPermSet ); if (this.m_normalPermSet == null) pset.m_normalPermSet = other.m_normalPermSet == null ? null : new TokenBasedSet( other.m_normalPermSet ); else pset.m_normalPermSet = this.m_normalPermSet.Union( other.m_normalPermSet ); } return pset; }
internal virtual void InplaceUnion( PermissionSet other ) { // Unions the "other" PermissionSet into this one. It can be optimized to do less copies than // need be done by the traditional union (and we don't have to create a new PermissionSet). // Quick out conditions, union doesn't change this PermissionSet if (other == null || other.IsEmpty()) return; // Save exceptions until the end Exception savedException = null; m_CheckedForNonCas = false; m_toBeLoaded = null; // We have to union "normal" permission no matter what now. try { if (this.m_normalPermSet == null) this.m_normalPermSet = other.m_normalPermSet == null ? null : new TokenBasedSet( other.m_normalPermSet ); else this.m_normalPermSet.InplaceUnion( other.m_normalPermSet ); } catch (Exception e) { savedException = e; } // If this is unrestricted, we're done. if (this.IsUnrestricted() && !this.ContainsNonCodeAccessPermissions()) goto END; // Union makes this an unrestricted. if (other.IsUnrestricted() && !other.ContainsNonCodeAccessPermissions()) { this.SetUnrestricted( true ); goto END; } if (this.m_unrestrictedPermSet == null) this.m_unrestrictedPermSet = other.m_unrestrictedPermSet == null ? null : new TokenBasedSet( other.m_unrestrictedPermSet ); else this.m_unrestrictedPermSet.InplaceUnion( other.m_unrestrictedPermSet ); END: if (savedException != null) throw savedException; }
public static void PermissionSetDemo() { Console.WriteLine("Executing Permission Set Demo"); try { // Open a permission set. PermissionSet ps1 = new PermissionSet(PermissionState.None); Console.WriteLine("Adding permission to open a file from a file dialog box."); // Add a permission to the permission set. ps1.AddPermission(new FileDialogPermission(FileDialogPermissionAccess.Open)); Console.WriteLine("Demanding Permission to open a file."); ps1.Demand(); Console.WriteLine("Demand succeeded."); Console.WriteLine("Adding permission to save a file from a file dialog box."); ps1.AddPermission(new FileDialogPermission(FileDialogPermissionAccess.Save)); Console.WriteLine("Demanding permission to open and save a file."); ps1.Demand(); Console.WriteLine("Demand succeeded."); Console.WriteLine("Adding a permission to read environment variable USERNAME."); ps1.AddPermission(new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME")); ps1.Demand(); Console.WriteLine("Demand succeeded."); Console.WriteLine("Adding permission to read environment variable COMPUTERNAME."); ps1.AddPermission(new EnvironmentPermission(EnvironmentPermissionAccess.Read, "COMPUTERNAME")); // Demand all the permissions in the set. Console.WriteLine("Demand all permissions."); ps1.Demand(); Console.WriteLine("Demand succeeded."); // Display the number of permissions in the set. Console.WriteLine("Number of permissions = " + ps1.Count); // Display the value of the IsSynchronized property. Console.WriteLine("IsSynchronized property = " + ps1.IsSynchronized); // Display the value of the IsReadOnly property. Console.WriteLine("IsReadOnly property = " + ps1.IsReadOnly); // Display the value of the SyncRoot property. Console.WriteLine("SyncRoot property = " + ps1.SyncRoot); // Display the result of a call to the ContainsNonCodeAccessPermissions method. // Gets a value indicating whether the PermissionSet contains permissions // that are not derived from CodeAccessPermission. // Returns true if the PermissionSet contains permissions that are not // derived from CodeAccessPermission; otherwise, false. Console.WriteLine("ContainsNonCodeAccessPermissions method returned " + ps1.ContainsNonCodeAccessPermissions()); Console.WriteLine("Value of the permission set ToString = \n" + ps1.ToString()); PermissionSet ps2 = new PermissionSet(PermissionState.None); // Create a second permission set and compare it to the first permission set. ps2.AddPermission(new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME")); ps2.AddPermission(new EnvironmentPermission(EnvironmentPermissionAccess.Write, "COMPUTERNAME")); Console.WriteLine("Permission set 2 = " + ps2); IEnumerator list = ps1.GetEnumerator(); Console.WriteLine("Permissions in first permission set:"); foreach (var permission in ps1) Console.WriteLine(permission.ToString()); Console.WriteLine("Second permission IsSubSetOf first permission = " + ps2.IsSubsetOf(ps1)); // Display the intersection of two permission sets. PermissionSet ps3 = ps2.Intersect(ps1); Console.WriteLine("The intersection of the first permission set and the second permission set = " + ps3.ToString()); // Create a new permission set. PermissionSet ps4 = new PermissionSet(PermissionState.None); ps4.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read, "C:\\Temp\\Testfile.txt")); ps4.AddPermission(new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Append, "C:\\Temp\\Testfile.txt")); // Display the union of two permission sets. PermissionSet ps5 = ps3.Union(ps4); Console.WriteLine("The union of permission set 3 and permission set 4 = " + ps5.ToString()); // Remove FileIOPermission from the permission set. ps5.RemovePermission(typeof(FileIOPermission)); Console.WriteLine("The last permission set after removing FileIOPermission = " + ps5.ToString()); // Change the permission set using SetPermission ps5.SetPermission(new EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "USERNAME")); Console.WriteLine("Permission set after SetPermission = " + ps5.ToString()); // Display result of ToXml and FromXml operations. PermissionSet ps6 = new PermissionSet(PermissionState.None); ps6.FromXml(ps5.ToXml()); Console.WriteLine("Result of ToFromXml = " + ps6.ToString() + "\n"); // Display result of PermissionSet.GetEnumerator. IEnumerator psEnumerator = ps1.GetEnumerator(); while (psEnumerator.MoveNext()) { Console.WriteLine(psEnumerator.Current.ToString()); } // Check for an unrestricted permission set. PermissionSet ps7 = new PermissionSet(PermissionState.Unrestricted); Console.WriteLine("Permission set is unrestricted = " + ps7.IsUnrestricted()); // Create and display a copy of a permission set. ps7 = ps5.Copy(); Console.WriteLine("Result of copy = " + ps7.ToString()); } catch (Exception e) { Console.WriteLine(e.Message.ToString()); } }
public void PermitOnly_NonCasPermission () { PermissionSet ps = new PermissionSet (PermissionState.None); ps.AddPermission (new PrincipalPermission (PermissionState.None)); Assert ("ContainsNonCodeAccessPermissions", ps.ContainsNonCodeAccessPermissions ()); AssertEquals ("Count", 1, ps.Count); ps.PermitOnly (); // it's simply ignored }