public void FromXml_Invalid() { PolicyLevel pl = PolicyLevel.CreateAppDomainLevel(); SecurityElement se = pl.ToXml(); se.Tag = "Mono"; // strangely this works :( pl.FromXml(se); // let's get weirder :) foreach (SecurityElement child in se.Children) { child.Tag = "Mono"; } pl.FromXml(se); // it's enough >:) }
public void FromXml() { PolicyLevel pl = PolicyLevel.CreateAppDomainLevel(); SecurityElement se = pl.ToXml(); pl.FromXml(se); Assert.AreEqual("AppDomain", pl.Label, "Label"); Assert.AreEqual("All_Code", pl.RootCodeGroup.Name, "RootCodeGroup"); Assert.AreEqual("FullTrust", pl.RootCodeGroup.PermissionSetName, "PermissionSetName"); Assert.AreEqual(0, pl.RootCodeGroup.Children.Count, "Children"); }
private static PolicyLevel LoadPolicyLevelFromStringHelper(string str, string path, PolicyLevelType type) { if (str == null) { throw new ArgumentNullException("str"); } Contract.EndContractBlock(); PolicyLevel level = new PolicyLevel(type, path); Parser parser = new Parser(str); SecurityElement elRoot = parser.GetTopElement(); if (elRoot == null) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), "configuration")); } SecurityElement elMscorlib = elRoot.SearchForChildByTag("mscorlib"); if (elMscorlib == null) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), "mscorlib")); } SecurityElement elSecurity = elMscorlib.SearchForChildByTag("security"); if (elSecurity == null) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), "security")); } SecurityElement elPolicy = elSecurity.SearchForChildByTag("policy"); if (elPolicy == null) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), "policy")); } SecurityElement elPolicyLevel = elPolicy.SearchForChildByTag("PolicyLevel"); if (elPolicyLevel != null) { level.FromXml(elPolicyLevel); } else { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), "PolicyLevel")); } return(level); }
public void ToXml() { PolicyLevel pl = Load(minimal, PolicyLevelType.Machine); PolicyLevel pl2 = PolicyLevel.CreateAppDomainLevel(); SecurityElement se = pl.ToXml(); pl2.FromXml(se); Assert.AreEqual(pl.FullTrustAssemblies.Count, pl2.FullTrustAssemblies.Count, "ToXml-FullTrustAssemblies"); Assert.AreEqual(pl.NamedPermissionSets.Count, pl2.NamedPermissionSets.Count, "ToXml-NamedPermissionSets"); Assert.IsTrue(pl.RootCodeGroup.Equals(pl2.RootCodeGroup, true), "ToXml-RootCodeGroup"); Assert.AreEqual(pl.StoreLocation, pl2.StoreLocation, "ToXml-StoreLocation"); }
public PolicyLevel CreateMediumTrustPolicy() { PolicyLevel policyLevel = PolicyLevel.CreateAppDomainLevel(); string contents; using (var file = File.OpenText(MediumTrustConfigFile)) { contents = file.ReadToEnd(); } SecurityElement securityElement = SecurityElement.FromString(Resources.MediumTrustConfig); policyLevel.FromXml(securityElement); return(policyLevel); }
// Compare two PolicyLevels using ToXml and FromXml. private static bool ComparePolicyLevels(PolicyLevel pLevel1, PolicyLevel pLevel2) { bool retVal = false; PolicyLevel firstCopy = PolicyLevel.CreateAppDomainLevel(); PolicyLevel secondCopy = PolicyLevel.CreateAppDomainLevel(); // Create copies of the two PolicyLevels passed in. // Convert the two PolicyLevels to their canonical form using ToXml and FromXml. firstCopy.FromXml(pLevel1.ToXml()); secondCopy.FromXml(pLevel2.ToXml()); if (firstCopy.ToXml().ToString().CompareTo(secondCopy.ToXml().ToString()) == 0) { retVal = true; } return(retVal); }
private static PolicyLevel LoadPolicyLevelFromStringHelper(string str, string path, PolicyLevelType type) { if (str == null) { throw new ArgumentNullException("str"); } PolicyLevel policyLevel = new PolicyLevel(type, path); SecurityElement topElement = new Parser(str).GetTopElement(); if (topElement == null) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), (object)"configuration")); } string tag1 = "mscorlib"; SecurityElement securityElement1 = topElement.SearchForChildByTag(tag1); if (securityElement1 == null) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), (object)"mscorlib")); } string tag2 = "security"; SecurityElement securityElement2 = securityElement1.SearchForChildByTag(tag2); if (securityElement2 == null) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), (object)"security")); } string tag3 = "policy"; SecurityElement securityElement3 = securityElement2.SearchForChildByTag(tag3); if (securityElement3 == null) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), (object)"policy")); } string tag4 = "PolicyLevel"; SecurityElement e = securityElement3.SearchForChildByTag(tag4); if (e == null) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), (object)"PolicyLevel")); } policyLevel.FromXml(e); return(policyLevel); }
// Token: 0x06001E18 RID: 7704 RVA: 0x0006907C File Offset: 0x0006727C private static PolicyLevel LoadPolicyLevelFromStringHelper(string str, string path, PolicyLevelType type) { if (str == null) { throw new ArgumentNullException("str"); } PolicyLevel policyLevel = new PolicyLevel(type, path); Parser parser = new Parser(str); SecurityElement topElement = parser.GetTopElement(); if (topElement == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), "configuration")); } SecurityElement securityElement = topElement.SearchForChildByTag("mscorlib"); if (securityElement == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), "mscorlib")); } SecurityElement securityElement2 = securityElement.SearchForChildByTag("security"); if (securityElement2 == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), "security")); } SecurityElement securityElement3 = securityElement2.SearchForChildByTag("policy"); if (securityElement3 == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), "policy")); } SecurityElement securityElement4 = securityElement3.SearchForChildByTag("PolicyLevel"); if (securityElement4 != null) { policyLevel.FromXml(securityElement4); return(policyLevel); } throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), "PolicyLevel")); }
private static PolicyLevel LoadPolicyLevelFromStringHelper(string str, string path, PolicyLevelType type) { if (str == null) { throw new ArgumentNullException("str"); } PolicyLevel level = new PolicyLevel(type, path); SecurityElement topElement = new Parser(str).GetTopElement(); if (topElement == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), new object[] { "configuration" })); } SecurityElement element2 = topElement.SearchForChildByTag("mscorlib"); if (element2 == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), new object[] { "mscorlib" })); } SecurityElement element3 = element2.SearchForChildByTag("security"); if (element3 == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), new object[] { "security" })); } SecurityElement element4 = element3.SearchForChildByTag("policy"); if (element4 == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), new object[] { "policy" })); } SecurityElement e = element4.SearchForChildByTag("PolicyLevel"); if (e == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), new object[] { "PolicyLevel" })); } level.FromXml(e); return(level); }
public static void PolicyLevelCallMethods() { PolicyLevel pl = (PolicyLevel)Activator.CreateInstance(typeof(PolicyLevel), true); NamedPermissionSet nps = new NamedPermissionSet("test"); pl.AddNamedPermissionSet(nps); nps = pl.ChangeNamedPermissionSet("test", new PermissionSet(new Permissions.PermissionState())); PolicyLevel.CreateAppDomainLevel(); nps = pl.GetNamedPermissionSet("test"); pl.Recover(); NamedPermissionSet nps2 = pl.RemoveNamedPermissionSet(nps); nps2 = pl.RemoveNamedPermissionSet("test"); pl.Reset(); Evidence evidence = new Evidence(); PolicyStatement ps = pl.Resolve(evidence); CodeGroup cg = pl.ResolveMatchingCodeGroups(evidence); SecurityElement se = new SecurityElement(""); pl.FromXml(se); se = pl.ToXml(); }
public static void PolicyLevelCallMethods() { PolicyLevel pl = (PolicyLevel)FormatterServices.GetUninitializedObject(typeof(PolicyLevel)); NamedPermissionSet nps = new NamedPermissionSet("test"); pl.AddNamedPermissionSet(nps); nps = pl.ChangeNamedPermissionSet("test", new PermissionSet(new Permissions.PermissionState())); #pragma warning disable 618 PolicyLevel.CreateAppDomainLevel(); #pragma warning restore 618 nps = pl.GetNamedPermissionSet("test"); pl.Recover(); NamedPermissionSet nps2 = pl.RemoveNamedPermissionSet(nps); nps2 = pl.RemoveNamedPermissionSet("test"); pl.Reset(); Evidence evidence = new Evidence(); PolicyStatement ps = pl.Resolve(evidence); CodeGroup cg = pl.ResolveMatchingCodeGroups(evidence); SecurityElement se = new SecurityElement(""); pl.FromXml(se); se = pl.ToXml(); }
public static void CreateAPolicyLevel() { try { //<Snippet2> // Create an AppDomain policy level. PolicyLevel pLevel = PolicyLevel.CreateAppDomainLevel(); //</Snippet2> // The root code group of the policy level combines all // permissions of its children. UnionCodeGroup rootCodeGroup; PermissionSet ps = new PermissionSet(PermissionState.None); ps.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); rootCodeGroup = new UnionCodeGroup( new AllMembershipCondition(), new PolicyStatement(ps, PolicyStatementAttribute.Nothing)); // This code group grants FullTrust to assemblies with the strong // name key from this assembly. UnionCodeGroup myCodeGroup = new UnionCodeGroup( new StrongNameMembershipCondition( new StrongNamePublicKeyBlob(GetKey()), null, null), new PolicyStatement(new PermissionSet(PermissionState.Unrestricted), PolicyStatementAttribute.Nothing) ); myCodeGroup.Name = "My CodeGroup"; //<Snippet4> // Add the code groups to the policy level. rootCodeGroup.AddChild(myCodeGroup); pLevel.RootCodeGroup = rootCodeGroup; Console.WriteLine("Permissions granted to all code running in this AppDomain level: "); Console.WriteLine(rootCodeGroup.ToXml()); Console.WriteLine("Child code groups in RootCodeGroup:"); IList codeGroups = pLevel.RootCodeGroup.Children; IEnumerator codeGroup = codeGroups.GetEnumerator(); while (codeGroup.MoveNext()) { Console.WriteLine("\t" + ((CodeGroup)codeGroup.Current).Name); } //</Snippet4> //<Snippet5> Console.WriteLine("Demonstrate adding and removing named permission sets."); Console.WriteLine("Original named permission sets:"); ListPermissionSets(pLevel); NamedPermissionSet myInternet = pLevel.GetNamedPermissionSet("Internet"); //</Snippet5> myInternet.Name = "MyInternet"; //<Snippet6> pLevel.AddNamedPermissionSet(myInternet); //</Snippet6> Console.WriteLine("\nNew named permission sets:"); ListPermissionSets(pLevel); myInternet.RemovePermission(typeof(System.Security.Permissions.FileDialogPermission)); //<Snippet7> pLevel.ChangeNamedPermissionSet("MyInternet", myInternet); //</Snippet7> //<Snippet8> pLevel.RemoveNamedPermissionSet("MyInternet"); //</Snippet8> Console.WriteLine("\nCurrent permission sets:"); ListPermissionSets(pLevel); pLevel.AddNamedPermissionSet(myInternet); Console.WriteLine("\nUpdated named permission sets:"); ListPermissionSets(pLevel); //<Snippet9> pLevel.Reset(); //</Snippet9> Console.WriteLine("\nReset named permission sets:"); ListPermissionSets(pLevel); //<Snippet10> Console.WriteLine("\nType property = " + pLevel.Type.ToString()); //</Snippet10> //<Snippet11> Console.WriteLine("The result of GetHashCode is " + pLevel.GetHashCode().ToString()); //</Snippet11> Console.WriteLine("StoreLocation property for the AppDomain level is empty, since AppDomain policy " + "cannot be saved to a file."); Console.WriteLine("StoreLocation property = " + pLevel.StoreLocation); //<Snippet12> PolicyLevel pLevelCopy = PolicyLevel.CreateAppDomainLevel(); // Create a copy of the PolicyLevel using ToXml/FromXml. pLevelCopy.FromXml(pLevel.ToXml()); if (ComparePolicyLevels(pLevel, pLevelCopy)) { Console.WriteLine("The ToXml/FromXml roundtrip was successful."); } else { Console.WriteLine("ToXml/FromXml roundtrip failed."); } //</Snippet12> Console.WriteLine("Show the result of resolving policy for evidence unique to the AppDomain policy level."); Evidence myEvidence = new Evidence(new object[] { myCodeGroup }, null); CheckEvidence(pLevel, myEvidence); return; } catch (Exception e) { Console.WriteLine(e.Message); return; } }
public void FromXml_Null() { PolicyLevel pl = PolicyLevel.CreateAppDomainLevel(); pl.FromXml(null); }
static public PolicyLevel LoadPolicyLevelFromString(String str, PolicyLevelType type) { #if _DEBUG if (debug) { DEBUG_OUT("Input string ="); DEBUG_OUT(str); } #endif if (str == null) { throw new ArgumentNullException("str"); } String name = Enum.GetName(typeof(PolicyLevelType), type); if (name == null) { return(null); } Parser parser = new Parser(str); PolicyLevel level = new PolicyLevel(name, ConfigId.None, type == PolicyLevelType.Machine); SecurityElement elRoot = parser.GetTopElement(); if (elRoot == null) { throw new ArgumentException(String.Format(Environment.GetResourceString("Policy_BadXml"), "configuration")); } SecurityElement elMscorlib = elRoot.SearchForChildByTag("mscorlib"); if (elMscorlib == null) { throw new ArgumentException(String.Format(Environment.GetResourceString("Policy_BadXml"), "mscorlib")); } SecurityElement elSecurity = elMscorlib.SearchForChildByTag("security"); if (elSecurity == null) { throw new ArgumentException(String.Format(Environment.GetResourceString("Policy_BadXml"), "security")); } SecurityElement elPolicy = elSecurity.SearchForChildByTag("policy"); if (elPolicy == null) { throw new ArgumentException(String.Format(Environment.GetResourceString("Policy_BadXml"), "policy")); } SecurityElement elPolicyLevel = elPolicy.SearchForChildByTag("PolicyLevel"); if (elPolicyLevel != null) { level.FromXml(elPolicyLevel); } else { throw new ArgumentException(String.Format(Environment.GetResourceString("Policy_BadXml"), "PolicyLevel")); } level.Loaded = true; return(level); }