示例#1
0
        public void FromXml_NoName()
        {
            NamedPermissionSet nps = new NamedPermissionSet(name, PermissionState.None);
            SecurityElement    se  = nps.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("class", se.Attribute("class"));
            w.AddAttribute("version", "1");
            nps.FromXml(w);

            // having a null name can badly influence the rest of the class code
            Assert.IsNull(nps.Name, "Name");
            NamedPermissionSet copy = (NamedPermissionSet)nps.Copy();

            Assert.IsNull(copy.Name, "Copy.Name");

            copy = nps.Copy("name");
            Assert.AreEqual("name", copy.Name, "Copy(Name).Name");

            se = nps.ToXml();
            Assert.IsNull(se.Attribute("Name"), "Name attribute");
#if NET_2_0
            Assert.AreEqual(0, nps.GetHashCode(), "GetHashCode");
            Assert.IsTrue(nps.Equals(nps), "Equals-self");
#endif
        }
示例#2
0
        public void FromXml()
        {
            NamedPermissionSet nps = new NamedPermissionSet(name, PermissionState.None);
            SecurityElement    se  = nps.ToXml();

            Assert.IsNotNull(se, "ToXml()");

            NamedPermissionSet nps2 = (NamedPermissionSet)nps.Copy();

            nps2.FromXml(se);
            Assert.AreEqual(name, nps2.Name, "FromXml-Copy.Name");
            // strangely it's empty when converted from XML (but null when created)
            Assert.AreEqual("", nps2.Description, "FromXml-Copy.Description");
            Assert.IsTrue(!nps2.IsUnrestricted(), "FromXml-Copy.IsUnrestricted");

            se.AddAttribute("Description", sentinel);
            nps2.FromXml(se);
            Assert.AreEqual(name, nps2.Name, "FromXml-Add1.Name");
            Assert.AreEqual(sentinel, nps2.Description, "FromXml-Add1.Description");
            Assert.IsTrue(!nps2.IsUnrestricted(), "FromXml-Add1.IsUnrestricted");

            se.AddAttribute("Unrestricted", "true");
            nps2.FromXml(se);
            Assert.AreEqual(name, nps2.Name, "FromXml-Add2.Name");
            Assert.AreEqual(sentinel, nps2.Description, "FromXml-Add2.Description");
            Assert.IsTrue(nps2.IsUnrestricted(), "FromXml-Add2.IsUnrestricted");
        }
示例#3
0
        public void FromXml_WrongTagCase()
        {
            NamedPermissionSet nps = new NamedPermissionSet(name, PermissionState.None);
            SecurityElement    se  = nps.ToXml();

            se.Tag = se.Tag.ToUpper(); // instead of PermissionSet
            nps.FromXml(se);
        }
示例#4
0
    public static void Main()
    {
        NamedPermissionSet ps = new NamedPermissionSet("SamplePermissionSet", PermissionState.None);

        ps.AddPermission(new FileIOPermission(FileIOPermissionAccess.AllAccess, @"C:\test\"));
        ps.AddPermission(new ReflectionPermission(PermissionState.Unrestricted));
        ps.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));

        Console.WriteLine(ps.ToXml().ToString());
    }
示例#5
0
        public void FromXml_NoVersion()
        {
            NamedPermissionSet nps = new NamedPermissionSet(name, PermissionState.None);
            SecurityElement    se  = nps.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("class", se.Attribute("class"));
            w.AddAttribute("Name", se.Attribute("Name"));
            nps.FromXml(w);
        }
示例#6
0
        public void FromXml_NoClass()
        {
            NamedPermissionSet nps = new NamedPermissionSet(name, PermissionState.None);
            SecurityElement    se  = nps.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("version", se.Attribute("version"));
            nps.FromXml(w);
            // doesn't even care of the class attribute presence
        }
示例#7
0
 public static void NamedPermissionSetCallMethods()
 {
     NamedPermissionSet nps = new NamedPermissionSet("Test");
     PermissionSet ps = nps.Copy();
     NamedPermissionSet nps2 = nps.Copy("Test");
     nps.Equals(nps2);
     int hash = nps.GetHashCode();
     SecurityElement se = new SecurityElement("");
     nps.FromXml(se);
     se = nps.ToXml();
 }
示例#8
0
        public void ToXml_Unrestricted()
        {
            NamedPermissionSet ps = new NamedPermissionSet(name, PermissionState.Unrestricted);
            SecurityElement    se = ps.ToXml();

            Assert.IsTrue(ps.ToString().StartsWith("<PermissionSet"), "Unrestricted.ToString().StartsWith");
            Assert.AreEqual("System.Security.NamedPermissionSet", (se.Attributes ["class"] as string), "Unrestricted.class");
            Assert.AreEqual("1", (se.Attributes ["version"] as string), "Unrestricted.version");
            Assert.AreEqual(name, (se.Attributes ["Name"] as string), "Unrestricted.Name");
            Assert.IsNull((se.Attributes ["Description"] as string), "Unrestricted.Description");
            Assert.AreEqual("true", (se.Attributes ["Unrestricted"] as string), "Unrestricted.Unrestricted");
        }
示例#9
0
        public void FromXml_InvalidPermission()
        {
            NamedPermissionSet nps = new NamedPermissionSet(name, PermissionState.None);
            SecurityElement    se  = nps.ToXml();
            // can't modify - so we create our own
            SecurityElement se2 = new SecurityElement("InvalidPermissionSet", se.Text);

            se2.AddAttribute("class", se.Attribute("class"));
            se2.AddAttribute("version", se.Attribute("version"));
            se2.AddAttribute("Name", se.Attribute("Name"));
            nps.FromXml(se2);
        }
示例#10
0
        /// <summary>Creates an XML encoding of the security object and its current state.</summary>
        /// <returns>An XML encoding of the security object, including any state information.</returns>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlEvidence" />
        /// </PermissionSet>
        public SecurityElement ToXml()
        {
            Hashtable hashtable = new Hashtable();

            if (this.full_trust_assemblies.Count > 0 && !hashtable.Contains("StrongNameMembershipCondition"))
            {
                hashtable.Add("StrongNameMembershipCondition", typeof(StrongNameMembershipCondition).FullName);
            }
            SecurityElement securityElement = new SecurityElement("NamedPermissionSets");

            foreach (object obj in this.named_permission_sets)
            {
                NamedPermissionSet namedPermissionSet = (NamedPermissionSet)obj;
                SecurityElement    securityElement2   = namedPermissionSet.ToXml();
                object             key = securityElement2.Attributes["class"];
                if (!hashtable.Contains(key))
                {
                    hashtable.Add(key, namedPermissionSet.GetType().FullName);
                }
                securityElement.AddChild(securityElement2);
            }
            SecurityElement securityElement3 = new SecurityElement("FullTrustAssemblies");

            foreach (object obj2 in this.full_trust_assemblies)
            {
                StrongNameMembershipCondition strongNameMembershipCondition = (StrongNameMembershipCondition)obj2;
                securityElement3.AddChild(strongNameMembershipCondition.ToXml(this));
            }
            SecurityElement securityElement4 = new SecurityElement("SecurityClasses");

            if (hashtable.Count > 0)
            {
                foreach (object obj3 in hashtable)
                {
                    DictionaryEntry dictionaryEntry  = (DictionaryEntry)obj3;
                    SecurityElement securityElement5 = new SecurityElement("SecurityClass");
                    securityElement5.AddAttribute("Name", (string)dictionaryEntry.Key);
                    securityElement5.AddAttribute("Description", (string)dictionaryEntry.Value);
                    securityElement4.AddChild(securityElement5);
                }
            }
            SecurityElement securityElement6 = new SecurityElement(typeof(PolicyLevel).Name);

            securityElement6.AddAttribute("version", "1");
            securityElement6.AddChild(securityElement4);
            securityElement6.AddChild(securityElement);
            if (this.root_code_group != null)
            {
                securityElement6.AddChild(this.root_code_group.ToXml(this));
            }
            securityElement6.AddChild(securityElement3);
            return(securityElement6);
        }
        internal SecurityElement ToXml(PolicyLevel level, bool useInternal)
        {
            SecurityElement e = new SecurityElement("PolicyStatement");

            e.AddAttribute("version", "1");
            if (m_attributes != PolicyStatementAttribute.Nothing)
            {
                e.AddAttribute("Attributes", XMLUtil.BitFieldEnumToString(typeof(PolicyStatementAttribute), m_attributes));
            }

            lock (this)
            {
                if (m_permSet != null)
                {
                    if (m_permSet is NamedPermissionSet)
                    {
                        // If the named permission set exists in the parent level of this
                        // policy struct, then just save the name of the permission set.
                        // Otherwise, serialize it like normal.

                        NamedPermissionSet namedPermSet = (NamedPermissionSet)m_permSet;
                        if (level != null && level.GetNamedPermissionSet(namedPermSet.Name) != null)
                        {
                            e.AddAttribute("PermissionSetName", namedPermSet.Name);
                        }
                        else
                        {
                            if (useInternal)
                            {
                                e.AddChild(namedPermSet.InternalToXml());
                            }
                            else
                            {
                                e.AddChild(namedPermSet.ToXml());
                            }
                        }
                    }
                    else
                    {
                        if (useInternal)
                        {
                            e.AddChild(m_permSet.InternalToXml());
                        }
                        else
                        {
                            e.AddChild(m_permSet.ToXml());
                        }
                    }
                }
            }

            return(e);
        }
示例#12
0
        public static void NamedPermissionSetCallMethods()
        {
            NamedPermissionSet nps  = new NamedPermissionSet("Test");
            PermissionSet      ps   = nps.Copy();
            NamedPermissionSet nps2 = nps.Copy("Test");

            nps.Equals(nps2);
            int             hash = nps.GetHashCode();
            SecurityElement se   = new SecurityElement("");

            nps.FromXml(se);
            se = nps.ToXml();
        }
示例#13
0
        // [ExpectedException (typeof (ArgumentException))]
        public void FromXml_WrongVersion()
        {
            NamedPermissionSet nps = new NamedPermissionSet(name, PermissionState.None);
            SecurityElement    se  = nps.ToXml();
            // can't modify - so we create our own
            SecurityElement se2 = new SecurityElement(se.Tag, se.Text);

            se2.AddAttribute("class", se.Attribute("class"));
            se2.AddAttribute("version", "2");
            se2.AddAttribute("Name", se.Attribute("Name"));
            nps.FromXml(se2);
            // wow - here we accept a version 2 !!!
        }
示例#14
0
        public void FromXml_WrongClass()
        {
            NamedPermissionSet nps = new NamedPermissionSet(name, PermissionState.None);
            SecurityElement    se  = nps.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("class", "Wrong" + se.Attribute("class"));
            w.AddAttribute("version", se.Attribute("version"));
            w.AddAttribute("Name", se.Attribute("Name"));
            nps.FromXml(w);
            // doesn't care of the class name at that stage
            // anyway the class has already be created so...
        }
        static void WritePermissionSet(string file)
        {
            WMDDetectorPermission perm =
                new WMDDetectorPermission(WMDDetectorPermissions.Read);
            NamedPermissionSet pset =
                new NamedPermissionSet("ReadWMDDetector");

            pset.Description = "WMD Detector Permission Set";
            pset.SetPermission(perm);
            StreamWriter sw = new StreamWriter(file);

            sw.Write(pset.ToXml());
            sw.Close();
        }
示例#16
0
        // Token: 0x06002A0B RID: 10763 RVA: 0x0009C32C File Offset: 0x0009A52C
        internal SecurityElement ToXml(PolicyLevel level, bool useInternal)
        {
            SecurityElement securityElement = new SecurityElement("PolicyStatement");

            securityElement.AddAttribute("version", "1");
            if (this.m_attributes != PolicyStatementAttribute.Nothing)
            {
                securityElement.AddAttribute("Attributes", XMLUtil.BitFieldEnumToString(typeof(PolicyStatementAttribute), this.m_attributes));
            }
            lock (this)
            {
                if (this.m_permSet != null)
                {
                    if (this.m_permSet is NamedPermissionSet)
                    {
                        NamedPermissionSet namedPermissionSet = (NamedPermissionSet)this.m_permSet;
                        if (level != null && level.GetNamedPermissionSet(namedPermissionSet.Name) != null)
                        {
                            securityElement.AddAttribute("PermissionSetName", namedPermissionSet.Name);
                        }
                        else if (useInternal)
                        {
                            securityElement.AddChild(namedPermissionSet.InternalToXml());
                        }
                        else
                        {
                            securityElement.AddChild(namedPermissionSet.ToXml());
                        }
                    }
                    else if (useInternal)
                    {
                        securityElement.AddChild(this.m_permSet.InternalToXml());
                    }
                    else
                    {
                        securityElement.AddChild(this.m_permSet.ToXml());
                    }
                }
            }
            return(securityElement);
        }