public void Union_Unrestricted() { // Union with unrestricted is unrestricted StorePermission sp1 = new StorePermission(PermissionState.Unrestricted); StorePermission sp2 = new StorePermission(PermissionState.None); // a. source (this) is unrestricted for (int i = 0; i < (int)StorePermissionFlags.AllFlags; i++) { // 8 isn't a valid value (so we exclude it from the rest of the loop) if ((i & 8) == 8) { continue; } sp2.Flags = (StorePermissionFlags)i; StorePermission union = (StorePermission)sp1.Union(sp2); Assert.IsTrue(union.IsUnrestricted(), "target " + sp2.Flags.ToString()); } // b. destination (target) is unrestricted for (int i = 0; i < (int)StorePermissionFlags.AllFlags; i++) { // 8 isn't a valid value (so we exclude it from the rest of the loop) if ((i & 8) == 8) { continue; } sp2.Flags = (StorePermissionFlags)i; StorePermission union = (StorePermission)sp2.Union(sp1); Assert.IsTrue(union.IsUnrestricted(), "source " + sp2.Flags.ToString()); } }
//</Snippet8> // The following method is intended to demonstrate only the behavior of // StorePermission class members,and not their practical usage. Most properties // and methods in this class are used for the resolution and enforcement of // security policy by the security infrastructure code. private static void ShowMembers() { Console.WriteLine("Creating first permission with Flags = OpenStore."); StorePermission sp1 = new StorePermission(StorePermissionFlags.OpenStore); Console.WriteLine("Creating second permission with Flags = AllFlags."); StorePermission sp2 = new StorePermission(StorePermissionFlags.AllFlags); Console.WriteLine("Creating third permission as Unrestricted."); //<Snippet9> StorePermission sp3 = new StorePermission(PermissionState.Unrestricted); //</Snippet9> Console.WriteLine("Creating fourth permission with a permission state of none."); StorePermission sp4 = new StorePermission(PermissionState.None); //<Snippet3> bool rc = sp2.IsSubsetOf(sp3); Console.WriteLine("Is the permission with complete store access (AllFlags) a subset of \n" + "\tthe permission with an Unrestricted permission state? " + (rc ? "Yes" : "No")); rc = sp1.IsSubsetOf(sp2); Console.WriteLine("Is the permission with OpenStore access a subset of the permission with \n" + "\tcomplete store access (AllFlags)? " + (rc ? "Yes" : "No")); //</Snippet3> //<Snippet4> rc = sp3.IsUnrestricted(); Console.WriteLine("Is the third permission unrestricted? " + (rc ? "Yes" : "No")); //</Snippet4> //<Snippet5> Console.WriteLine("Copying the second permission to the fourth permission."); sp4 = (StorePermission)sp2.Copy(); rc = sp4.Equals(sp2); Console.WriteLine("Is the fourth permission equal to the second permission? " + (rc ? "Yes" : "No")); //</Snippet5> //<Snippet10> Console.WriteLine("Creating the intersection of the second and first permissions."); sp4 = (StorePermission)sp2.Intersect(sp1); Console.WriteLine("Value of the Flags property is: " + sp4.Flags.ToString()); //</Snippet10> //<Snippet6> Console.WriteLine("Creating the union of the second and first permissions."); sp4 = (StorePermission)sp2.Union(sp1); Console.WriteLine("Result of the union of the second permission with the first: " + sp4.Flags); //</Snippet6> //<Snippet7> Console.WriteLine("Using an XML roundtrip to reset the fourth permission."); sp4.FromXml(sp2.ToXml()); rc = sp4.Equals(sp2); Console.WriteLine("Does the XML roundtrip result equal the original permission? " + (rc ? "Yes" : "No")); //</Snippet7> }
public void PermissionState_Unrestricted() { PermissionState ps = PermissionState.Unrestricted; StorePermission sp = new StorePermission(ps); Assert.AreEqual(StorePermissionFlags.AllFlags, sp.Flags, "Flags"); Assert.IsTrue(sp.IsUnrestricted(), "IsUnrestricted"); SecurityElement se = sp.ToXml(); Assert.IsNotNull(se.Attribute("Unrestricted"), "Xml-Unrestricted"); Assert.IsNull(se.Attribute("Level"), "Xml-Flags"); Assert.IsNull(se.Children, "Xml-Children"); StorePermission copy = (StorePermission)sp.Copy(); Assert.IsFalse(Object.ReferenceEquals(sp, copy), "ReferenceEquals"); Assert.AreEqual(sp.Flags, copy.Flags, "Copy Flags"); Assert.AreEqual(sp.IsUnrestricted(), copy.IsUnrestricted(), "IsUnrestricted ()"); }
public void PermissionState_None_Copy() { // both will return null under 2.0 final // StorePermission sp1 = new StorePermission (PermissionState.None).Copy(); // StorePermission sp2 = new StorePermission (StorePermissionFlags.NoFlags).Copy (); StorePermission sp = new StorePermission(PermissionState.None); StorePermission copy = (StorePermission)sp.Copy(); Assert.IsFalse(Object.ReferenceEquals(sp, copy), "ReferenceEquals"); Assert.AreEqual(sp.Flags, copy.Flags, "Copy Flags"); Assert.AreEqual(sp.IsUnrestricted(), copy.IsUnrestricted(), "IsUnrestricted ()"); }
public void Union_None() { // Union with none is same StorePermission sp1 = new StorePermission(PermissionState.None); StorePermission sp2 = new StorePermission(PermissionState.None); StorePermission union = (StorePermission)sp1.Union(sp1); Assert.IsNull(union, "NoFlags"); for (int i = 1; i < (int)StorePermissionFlags.AllFlags; i++) { // 8 isn't a valid value (so we exclude it from the rest of the loop) if ((i & 8) == 8) { continue; } sp2.Flags = (StorePermissionFlags)i; union = (StorePermission)sp1.Union(sp2); Assert.IsFalse(union.IsUnrestricted(), "target.Unrestricted " + sp2.Flags.ToString()); Assert.AreEqual(sp2.Flags, union.Flags, "target.Level " + sp2.Flags.ToString()); union = (StorePermission)sp2.Union(sp1); Assert.IsFalse(union.IsUnrestricted(), "source.Unrestricted " + sp2.Flags.ToString()); Assert.AreEqual(sp2.Flags, union.Flags, "source.Level " + sp2.Flags.ToString()); } sp2.Flags = StorePermissionFlags.AllFlags; union = (StorePermission)sp1.Union(sp2); Assert.IsTrue(union.IsUnrestricted(), "target.Unrestricted Unrestricted"); Assert.AreEqual(StorePermissionFlags.AllFlags, union.Flags, "target.Level Unrestricted"); union = (StorePermission)sp2.Union(sp1); Assert.IsTrue(union.IsUnrestricted(), "source.Unrestricted Unrestricted"); Assert.AreEqual(StorePermissionFlags.AllFlags, union.Flags, "source.Level Unrestricted"); }
public void ConstructorLevel_Deny_Unrestricted() { StorePermission p = new StorePermission(StorePermissionFlags.AllFlags); Assert.AreEqual(StorePermissionFlags.AllFlags, p.Flags, "Flags"); Assert.IsTrue(p.IsUnrestricted(), "IsUnrestricted"); Assert.IsNotNull(p.Copy(), "Copy"); SecurityElement se = p.ToXml(); Assert.IsNotNull(se, "ToXml"); p.FromXml(se); Assert.IsNotNull(p.Intersect(p), "Intersect"); Assert.IsTrue(p.IsSubsetOf(p), "IsSubsetOf"); Assert.IsNotNull(p.Union(p), "Union"); }
public void ConstructorState_Deny_Unrestricted() { StorePermission p = new StorePermission(PermissionState.None); Assert.AreEqual(StorePermissionFlags.NoFlags, p.Flags, "Flags"); Assert.IsFalse(p.IsUnrestricted(), "IsUnrestricted"); SecurityElement se = p.ToXml(); Assert.IsNotNull(se, "ToXml"); p.FromXml(se); Assert.IsTrue(p.IsSubsetOf(p), "IsSubsetOf"); // strange behaviour of Copy under MS fx 2.0 (returns null for NoFlags) p.Copy(); p.Intersect(p); p.Union(p); }
public void PermissionState_None() { PermissionState ps = PermissionState.None; StorePermission sp = new StorePermission(ps); Assert.AreEqual(StorePermissionFlags.NoFlags, sp.Flags, "Flags"); Assert.IsFalse(sp.IsUnrestricted(), "IsUnrestricted"); SecurityElement se = sp.ToXml(); // only class and version are present Assert.AreEqual("NoFlags", se.Attribute("Flags"), "Xml-Flags"); Assert.IsNull(se.Children, "Xml-Children"); StorePermission copy = (StorePermission)sp.Copy(); Assert.IsNull(copy, "Copy"); }