Ejemplo n.º 1
0
        public void ExtraInfo_NotSerializable()
        {
            ApplicationTrust at = new ApplicationTrust();

            at.ExtraInfo = this;
            SecurityElement se = at.ToXml();
        }
Ejemplo n.º 2
0
        public void FromXml_InvalidTag()
        {
            ApplicationTrust at = new ApplicationTrust();
            SecurityElement  se = at.ToXml();

            se.Tag = "MonoTrust";
            at.FromXml(se);
        }
Ejemplo n.º 3
0
        public static void ApplicationTrustCallMethods()
        {
            ApplicationTrust at = new ApplicationTrust();
            SecurityElement  se = new SecurityElement("");

            at.FromXml(se);
            se = at.ToXml();
        }
Ejemplo n.º 4
0
        public void ApplicationIdentity()
        {
            ApplicationTrust at = new ApplicationTrust();

            at.ApplicationIdentity = new ApplicationIdentity("Mono Unit Test");
            Assert.IsNotNull(at.ApplicationIdentity, "not null");
            string expected = AdjustLineEnds("<ApplicationTrust version=\"1\"\r\nFullName=\"Mono Unit Test, Culture=neutral\">\r\n<DefaultGrant>\r\n<PolicyStatement version=\"1\">\r\n<PermissionSet class=\"System.Security.PermissionSet\"\r\nversion=\"1\"/>\r\n</PolicyStatement>\r\n</DefaultGrant>\r\n</ApplicationTrust>\r\n");

            Assert.AreEqual(expected, AdjustLineEnds(at.ToXml().ToString()), "XML");
        }
        /// <summary>
        ///     An ApplicationTrust object contains a default grant set as well as a list of assemblies which
        ///     are fully trusted. The GetFullTrustAssemblies method retrieves the strong names of assemblies
        ///     which the ApplicationTrust object considers to be fully trusted.
        /// </summary>
        public static IList <StrongName> GetFullTrustAssemblies(this ApplicationTrust applicationTrust)
        {
            List <StrongName> fullTrustAssemblies = new List <StrongName>();

            // ApplicationTrust does not expose the full trust list programatically.  To access this
            // information, we need to write out the ApplicationTrust XML and then pull out the strong names
            // from the serialized XML.
            SecurityElement applicationTrustXml = applicationTrust.ToXml();

            // First look for the FullTrustAssemblies node
            SecurityElement fullTrustAssembliesXml = null;

            if (applicationTrustXml.Children != null)
            {
                for (int i = 0; i < applicationTrustXml.Children.Count && fullTrustAssembliesXml == null; ++i)
                {
                    SecurityElement currentChild = applicationTrustXml.Children[i] as SecurityElement;
                    if (String.Equals(currentChild.Tag, "FullTrustAssemblies", StringComparison.Ordinal))
                    {
                        fullTrustAssembliesXml = currentChild;
                    }
                }
            }

            // If we found a FullTrustAssemblies node, each child will represent the strong name of one
            // fully trusted assembly
            if (fullTrustAssembliesXml != null &&
                fullTrustAssembliesXml.Children != null)
            {
                foreach (SecurityElement fullTrustAssemblyXml in fullTrustAssembliesXml.Children)
                {
                    // We only know how to parse v1 StrongName XML
                    if (String.Equals(fullTrustAssemblyXml.Tag, "StrongName", StringComparison.Ordinal) &&
                        String.Equals(fullTrustAssemblyXml.Attribute("version"), "1", StringComparison.Ordinal))
                    {
                        string assemblyName = fullTrustAssemblyXml.Attribute("Name");
                        assemblyName = assemblyName != null ? assemblyName : String.Empty;

                        string  assemblyVersionString = fullTrustAssemblyXml.Attribute("Version");
                        Version assemblyVersion       = assemblyVersionString != null ? new Version(assemblyVersionString) : new Version();

                        string assemblyKeyString = fullTrustAssemblyXml.Attribute("Key");
                        byte[] assemblyKey       = assemblyKeyString != null?HexToBytes(assemblyKeyString) : new byte[0];

                        StrongName fullTrustAssembly = new StrongName(new StrongNamePublicKeyBlob(assemblyKey),
                                                                      assemblyName,
                                                                      assemblyVersion);

                        fullTrustAssemblies.Add(fullTrustAssembly);
                    }
                }
            }

            return(fullTrustAssemblies.AsReadOnly());
        }
Ejemplo n.º 6
0
        public void ToFromXmlRoundtrip()
        {
            ApplicationTrust at = new ApplicationTrust();

            at.ApplicationIdentity       = new ApplicationIdentity("Mono Unit Test");
            at.DefaultGrantSet           = new PolicyStatement(new PermissionSet(PermissionState.Unrestricted));
            at.ExtraInfo                 = "Mono";
            at.IsApplicationTrustedToRun = true;
            at.Persist = true;

            SecurityElement se       = at.ToXml();
            string          expected = AdjustLineEnds("<ApplicationTrust version=\"1\"\r\nFullName=\"Mono Unit Test, Culture=neutral\"\r\nTrustedToRun=\"true\"\r\nPersist=\"true\">\r\n<DefaultGrant>\r\n<PolicyStatement version=\"1\">\r\n<PermissionSet class=\"System.Security.PermissionSet\"\r\nversion=\"1\"\r\nUnrestricted=\"true\"/>\r\n</PolicyStatement>\r\n</DefaultGrant>\r\n<ExtraInfo Data=\"0001000000FFFFFFFF01000000000000000601000000044D6F6E6F0B\"/>\r\n</ApplicationTrust>\r\n");

            Assert.AreEqual(expected, AdjustLineEnds(at.ToXml().ToString()), "XML");

            ApplicationTrust copy = new ApplicationTrust();

            copy.FromXml(se);
            se = copy.ToXml();
            Assert.AreEqual(expected, AdjustLineEnds(at.ToXml().ToString()), "Copy");
        }
Ejemplo n.º 7
0
        public void FromXml_NoVersion()
        {
            ApplicationTrust at = new ApplicationTrust();
            SecurityElement  se = at.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            foreach (SecurityElement child in se.Children)
            {
                w.AddChild(child);
            }

            at.FromXml(w);
        }
Ejemplo n.º 8
0
        public void Persist()
        {
            ApplicationTrust at = new ApplicationTrust();

            at.Persist = true;
            Assert.IsTrue(at.Persist, "true");

            string expected = AdjustLineEnds("<ApplicationTrust version=\"1\"\r\nPersist=\"true\">\r\n<DefaultGrant>\r\n<PolicyStatement version=\"1\">\r\n<PermissionSet class=\"System.Security.PermissionSet\"\r\nversion=\"1\"/>\r\n</PolicyStatement>\r\n</DefaultGrant>\r\n</ApplicationTrust>\r\n");

            Assert.AreEqual(expected, AdjustLineEnds(at.ToXml().ToString()), "XML");

            at.Persist = false;
            Assert.IsFalse(at.Persist, "false");
        }
Ejemplo n.º 9
0
        public void ExtraInfo()
        {
            ApplicationTrust at = new ApplicationTrust();

            at.ExtraInfo = "Mono";
            Assert.IsNotNull(at.ExtraInfo, "not null");

            string expected = AdjustLineEnds("<ApplicationTrust version=\"1\">\r\n<DefaultGrant>\r\n<PolicyStatement version=\"1\">\r\n<PermissionSet class=\"System.Security.PermissionSet\"\r\nversion=\"1\"/>\r\n</PolicyStatement>\r\n</DefaultGrant>\r\n<ExtraInfo Data=\"0001000000FFFFFFFF01000000000000000601000000044D6F6E6F0B\"/>\r\n</ApplicationTrust>\r\n");

            Assert.AreEqual(expected, AdjustLineEnds(at.ToXml().ToString()), "XML");

            at.ExtraInfo = null;
            Assert.IsNull(at.ExtraInfo, "null");
        }
Ejemplo n.º 10
0
        public void FromXml_InvalidVersion()
        {
            ApplicationTrust at = new ApplicationTrust();
            SecurityElement  se = at.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("version", "2");
            foreach (SecurityElement child in se.Children)
            {
                w.AddChild(child);
            }

            at.FromXml(w);
        }
Ejemplo n.º 11
0
        public void Constructor_Empty()
        {
            ApplicationTrust at = new ApplicationTrust();

            Assert.IsNull(at.ApplicationIdentity, "ApplicationIdentity");
            Assert.AreEqual(PolicyStatementAttribute.Nothing, at.DefaultGrantSet.Attributes, "DefaultGrantSet.Attributes");
            Assert.AreEqual(String.Empty, at.DefaultGrantSet.AttributeString, "DefaultGrantSet.AttributeString");
            Assert.IsTrue(at.DefaultGrantSet.PermissionSet.IsEmpty(), "DefaultGrantSet.PermissionSet.IsEmpty");
            Assert.IsFalse(at.DefaultGrantSet.PermissionSet.IsUnrestricted(), "DefaultGrantSet.PermissionSet.IsUnrestricted");
            Assert.IsNull(at.ExtraInfo, "ExtraInfo");
            Assert.IsFalse(at.IsApplicationTrustedToRun, "IsApplicationTrustedToRun");
            Assert.IsFalse(at.Persist, "Persist");
            string expected = AdjustLineEnds("<ApplicationTrust version=\"1\">\r\n<DefaultGrant>\r\n<PolicyStatement version=\"1\">\r\n<PermissionSet class=\"System.Security.PermissionSet\"\r\nversion=\"1\"/>\r\n</PolicyStatement>\r\n</DefaultGrant>\r\n</ApplicationTrust>\r\n");

            Assert.AreEqual(expected, AdjustLineEnds(at.ToXml().ToString()), "XML");
        }
Ejemplo n.º 12
0
        public void DefaultGrantSet()
        {
            ApplicationTrust at = new ApplicationTrust();

            at.DefaultGrantSet = new PolicyStatement(new PermissionSet(PermissionState.Unrestricted));
            Assert.IsNotNull(at.DefaultGrantSet, "not null");
            string expected = AdjustLineEnds("<ApplicationTrust version=\"1\">\r\n<DefaultGrant>\r\n<PolicyStatement version=\"1\">\r\n<PermissionSet class=\"System.Security.PermissionSet\"\r\nversion=\"1\"\r\nUnrestricted=\"true\"/>\r\n</PolicyStatement>\r\n</DefaultGrant>\r\n</ApplicationTrust>\r\n");

            Assert.AreEqual(expected, AdjustLineEnds(at.ToXml().ToString()), "XML");

            at.DefaultGrantSet = null;
            // returns to defaults
            Assert.IsNotNull(at.DefaultGrantSet, "null");
            Assert.AreEqual(PolicyStatementAttribute.Nothing, at.DefaultGrantSet.Attributes, "DefaultGrantSet.Attributes");
            Assert.AreEqual(String.Empty, at.DefaultGrantSet.AttributeString, "DefaultGrantSet.AttributeString");
            Assert.IsTrue(at.DefaultGrantSet.PermissionSet.IsEmpty(), "DefaultGrantSet.PermissionSet.IsEmpty");
            Assert.IsFalse(at.DefaultGrantSet.PermissionSet.IsUnrestricted(), "DefaultGrantSet.PermissionSet.IsUnrestricted");
        }
Ejemplo n.º 13
0
        public void FromXml_NoChild()
        {
            ApplicationTrust at = new ApplicationTrust();
            SecurityElement  se = at.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("version", "1");

            at.FromXml(w);

            Assert.IsNull(at.ApplicationIdentity, "ApplicationIdentity");
            Assert.AreEqual(PolicyStatementAttribute.Nothing, at.DefaultGrantSet.Attributes, "DefaultGrantSet.Attributes");
            Assert.AreEqual(String.Empty, at.DefaultGrantSet.AttributeString, "DefaultGrantSet.AttributeString");
            Assert.IsTrue(at.DefaultGrantSet.PermissionSet.IsEmpty(), "DefaultGrantSet.PermissionSet.IsEmpty");
            Assert.IsFalse(at.DefaultGrantSet.PermissionSet.IsUnrestricted(), "DefaultGrantSet.PermissionSet.IsUnrestricted");
            Assert.IsNull(at.ExtraInfo, "ExtraInfo");
            Assert.IsFalse(at.IsApplicationTrustedToRun, "IsApplicationTrustedToRun");
            Assert.IsFalse(at.Persist, "Persist");
        }