Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //<Snippet2>
            GacInstalled myGacInstalled = new GacInstalled();

            //</Snippet2>

            //<Snippet3>
            Object []             hostEvidence     = { myGacInstalled };
            Object []             assemblyEvidence = {};
            Evidence              myEvidence       = new Evidence(hostEvidence, assemblyEvidence);
            GacIdentityPermission myPerm           =
                (GacIdentityPermission)myGacInstalled.CreateIdentityPermission(
                    myEvidence);

            Console.WriteLine(myPerm.ToXml().ToString());
            //</Snippet3>

            //<Snippet4>
            GacInstalled myGacInstalledCopy =
                (GacInstalled)myGacInstalled.Copy();
            bool result = myGacInstalled.Equals(myGacInstalledCopy);

            //</Snippet4>

            //<Snippet5>
            Console.WriteLine(
                "Hashcode = " + myGacInstalled.GetHashCode().ToString());
            //</Snippet5>

            //<Snippet6>
            Console.WriteLine(myGacInstalled.ToString());
            //</Snippet6>
        }
    //</Snippet5>
    //<Snippet6>
    // ToXml creates an XML encoding of the permission and its current state; FromXml reconstructs a
    // permission with the specified state from the XML encoding.
    private bool ToFromXmlDemo()
    {
        GacIdentityPermission Gac1 = new GacIdentityPermission();
        GacIdentityPermission Gac2 = new GacIdentityPermission();

        Console.WriteLine("**************************************************************************");
        try
        {
            Gac2 = new GacIdentityPermission(PermissionState.None);
            Gac2.FromXml(Gac1.ToXml());
            bool result = Gac2.Equals(Gac1);
            if (Gac2.IsSubsetOf(Gac1) && Gac1.IsSubsetOf(Gac2))
            {
                Console.WriteLine("Result of ToFromXml = " + Gac2.ToString());
            }
            else
            {
                Console.WriteLine(Gac2.ToString());
                Console.WriteLine(Gac1.ToString());
                return(false);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("ToFromXml failed. " + e);
            return(false);
        }

        return(true);
    }
Exemplo n.º 3
0
        public void FromXml_WrongTagCase()
        {
            GacIdentityPermission gip = new GacIdentityPermission();
            SecurityElement       se  = gip.ToXml();

            se.Tag = "IPERMISSION";             // instead of IPermission
            gip.FromXml(se);
        }
Exemplo n.º 4
0
        public void FromXml_WrongTag()
        {
            GacIdentityPermission gip = new GacIdentityPermission();
            SecurityElement       se  = gip.ToXml();

            se.Tag = "IMono";
            gip.FromXml(se);
        }
Exemplo n.º 5
0
        public void FromXml_WrongVersion()
        {
            GacIdentityPermission gip = new GacIdentityPermission();
            SecurityElement       se  = gip.ToXml();

            se.Attributes.Remove("version");
            se.Attributes.Add("version", "2");
            gip.FromXml(se);
        }
Exemplo n.º 6
0
        public void FromXml_NoVersion()
        {
            GacIdentityPermission gip = new GacIdentityPermission();
            SecurityElement       se  = gip.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("class", se.Attribute("class"));
            gip.FromXml(w);
        }
Exemplo n.º 7
0
 public static void GacIdentityPermissionCallMethods()
 {
     GacIdentityPermission gip = new GacIdentityPermission();
     IPermission ip = gip.Copy();
     IPermission ip2 = gip.Intersect(ip);
     bool issubset = gip.IsSubsetOf(ip);
     IPermission ip3 = gip.Union(ip2);
     SecurityElement se = new SecurityElement("");
     gip.FromXml(se);
     se = gip.ToXml();
 }
Exemplo n.º 8
0
        public void FromXml_NoClass()
        {
            GacIdentityPermission gip = new GacIdentityPermission();
            SecurityElement       se  = gip.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("version", se.Attribute("version"));
            gip.FromXml(w);
            // doesn't even care of the class attribute presence
        }
Exemplo n.º 9
0
        public static void GacIdentityPermissionCallMethods()
        {
            GacIdentityPermission gip = new GacIdentityPermission();
            IPermission           ip  = gip.Copy();
            IPermission           ip2 = gip.Intersect(ip);
            bool            issubset  = gip.IsSubsetOf(ip);
            IPermission     ip3       = gip.Union(ip2);
            SecurityElement se        = new SecurityElement("");

            gip.FromXml(se);
            se = gip.ToXml();
        }
Exemplo n.º 10
0
        public void FromXml_WrongClass()
        {
            GacIdentityPermission gip = new GacIdentityPermission();
            SecurityElement       se  = gip.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("class", "Wrong" + se.Attribute("class"));
            w.AddAttribute("version", se.Attribute("version"));
            gip.FromXml(w);
            // doesn't care of the class name at that stage
            // anyway the class has already be created so...
        }
Exemplo n.º 11
0
        public void PermissionStateNone()
        {
            GacIdentityPermission gip = new GacIdentityPermission(PermissionState.None);

            SecurityElement se = gip.ToXml();

            // only class and version are present
            Assert.AreEqual(2, se.Attributes.Count, "Xml-Attributes");
            Assert.IsNull(se.Children, "Xml-Children");

            GacIdentityPermission copy = (GacIdentityPermission)gip.Copy();

            Assert.IsFalse(Object.ReferenceEquals(gip, copy), "ReferenceEquals");
        }
Exemplo n.º 12
0
        public void PermissionStateUnrestricted()
        {
            GacIdentityPermission gip = new GacIdentityPermission(PermissionState.Unrestricted);

            // FX 2.0 now supports Unrestricted for Identity Permissions
            // However the XML doesn't show the Unrestricted status...

            SecurityElement se = gip.ToXml();

            // only class and version are present
            Assert.AreEqual(2, se.Attributes.Count, "Xml-Attributes");
            Assert.IsNull(se.Children, "Xml-Children");

            GacIdentityPermission copy = (GacIdentityPermission)gip.Copy();

            Assert.IsFalse(Object.ReferenceEquals(gip, copy), "ReferenceEquals");

            // ... and because it doesn't implement IUnrestrictedPermission
            // there is not way to know if it's unrestricted so...
            Assert.IsTrue(gip.Equals(new GacIdentityPermission(PermissionState.None)), "Unrestricted==None");
            // there is not much difference after all ;-)
        }