Beispiel #1
0
 public static void XamlAccessLevelTestsCallMethods()
 {
     Assembly        execAssembly = Assembly.GetExecutingAssembly();
     Type            type         = typeof(int);
     XamlAccessLevel accessLevel  = XamlAccessLevel.AssemblyAccessTo(execAssembly);
     XamlAccessLevel accessLevel2 = XamlAccessLevel.AssemblyAccessTo(execAssembly.GetName());
     XamlAccessLevel accessLevel3 = XamlAccessLevel.PrivateAccessTo(type);
     XamlAccessLevel accessLevel4 = XamlAccessLevel.PrivateAccessTo(type.AssemblyQualifiedName);
     AssemblyName    an           = accessLevel.AssemblyAccessToAssemblyName;
 }
Beispiel #2
0
        public void SingleAssembly()
        {
            XamlAccessLevel    assemblyAccess = XamlAccessLevel.AssemblyAccessTo(typeof(LoadPermissionTests).Assembly);
            XamlLoadPermission assemblyPerm   = new XamlLoadPermission(assemblyAccess);

            VerifyInvariants(assemblyPerm);

            XamlLoadPermission testPerm, intersect, union;

            // Identical permission
            XamlAccessLevel sameAssemblyAccess = XamlAccessLevel.AssemblyAccessTo(typeof(LoadPermissionTests).Assembly);

            testPerm = new XamlLoadPermission(sameAssemblyAccess);
            VerifyInvariants(testPerm);
            Assert.AreEqual(assemblyPerm, testPerm);
            Assert.IsTrue(testPerm.IsSubsetOf(assemblyPerm));
            Assert.IsTrue(assemblyPerm.IsSubsetOf(testPerm));
            intersect = (XamlLoadPermission)testPerm.Intersect(assemblyPerm);
            Assert.AreEqual(assemblyPerm, intersect);
            union = (XamlLoadPermission)testPerm.Union(assemblyPerm);
            Assert.AreEqual(assemblyPerm, union);
            Assert.IsTrue(testPerm.Includes(sameAssemblyAccess));

            // Type permission to same assembly
            XamlAccessLevel typeAccess = XamlAccessLevel.PrivateAccessTo(typeof(LoadPermissionTests));

            testPerm = new XamlLoadPermission(typeAccess);
            VerifyInvariants(testPerm);
            Assert.AreNotEqual(assemblyPerm, testPerm);
            Assert.IsFalse(testPerm.IsSubsetOf(assemblyPerm));
            Assert.IsTrue(assemblyPerm.IsSubsetOf(testPerm));
            intersect = (XamlLoadPermission)testPerm.Intersect(assemblyPerm);
            Assert.AreEqual(assemblyPerm, intersect);
            union = (XamlLoadPermission)testPerm.Union(assemblyPerm);
            Assert.AreEqual(testPerm, union);
            Assert.IsTrue(testPerm.Includes(sameAssemblyAccess));
            Assert.IsTrue(testPerm.Includes(typeAccess));

            // Assembly permission on different assembly
            XamlAccessLevel diffAssemblyAccess = XamlAccessLevel.AssemblyAccessTo(typeof(string).Assembly);

            testPerm = new XamlLoadPermission(diffAssemblyAccess);
            VerifyInvariants(testPerm);
            Assert.AreNotEqual(assemblyPerm, testPerm);
            Assert.IsFalse(testPerm.IsSubsetOf(assemblyPerm));
            Assert.IsFalse(assemblyPerm.IsSubsetOf(testPerm));
            intersect = (XamlLoadPermission)testPerm.Intersect(assemblyPerm);
            Assert.AreEqual(s_EmptyPerm, intersect);
            union = (XamlLoadPermission)testPerm.Union(assemblyPerm);
            Assert.IsTrue(testPerm.IsSubsetOf(union));
            Assert.IsTrue(assemblyPerm.IsSubsetOf(union));
            Assert.IsFalse(testPerm.Includes(sameAssemblyAccess));
            Assert.IsFalse(testPerm.Includes(typeAccess));
        }
Beispiel #3
0
        public void AccessLevelCtorsPositive()
        {
            XamlAccessLevel byRef  = XamlAccessLevel.AssemblyAccessTo(typeof(LoadPermissionTests).Assembly);
            XamlAccessLevel byName = XamlAccessLevel.AssemblyAccessTo(typeof(LoadPermissionTests).Assembly.GetName());

            Assert.AreEqual(new XamlLoadPermission(byRef), new XamlLoadPermission(byName));

            byRef  = XamlAccessLevel.PrivateAccessTo(typeof(LoadPermissionTests));
            byName = XamlAccessLevel.PrivateAccessTo(typeof(LoadPermissionTests).AssemblyQualifiedName);
            Assert.AreEqual(new XamlLoadPermission(byRef), new XamlLoadPermission(byName));
        }
        static void InitializeComponentFromXamlResource(Type componentType, string resource, object componentInstance, XamlSchemaContext schemaContext)
        {
            Stream           initializeXaml = componentType.Assembly.GetManifestResourceStream(resource);
            XmlReader        xmlReader      = null;
            XamlReader       reader         = null;
            XamlObjectWriter objectWriter   = null;

            try
            {
                xmlReader = XmlReader.Create(initializeXaml, new XmlReaderSettings {
                    XmlResolver = null
                });
                XamlXmlReaderSettings readerSettings = new XamlXmlReaderSettings();
                readerSettings.LocalAssembly = componentType.Assembly;
                readerSettings.AllowProtectedMembersOnRoot = true;
                reader = new XamlXmlReader(xmlReader, schemaContext, readerSettings);
                XamlObjectWriterSettings writerSettings = new XamlObjectWriterSettings();
                writerSettings.RootObjectInstance = componentInstance;
                writerSettings.AccessLevel        = XamlAccessLevel.PrivateAccessTo(componentType);
                objectWriter = new XamlObjectWriter(schemaContext, writerSettings);

                // We need the XamlLoadPermission for the assembly we are dealing with.
                XamlLoadPermission perm = new XamlLoadPermission(XamlAccessLevel.PrivateAccessTo(componentType));
                perm.Assert();
                try
                {
                    XamlServices.Transform(reader, objectWriter);
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }
            }
            finally
            {
                if ((xmlReader != null))
                {
                    ((IDisposable)(xmlReader)).Dispose();
                }
                if ((reader != null))
                {
                    ((IDisposable)(reader)).Dispose();
                }
                if ((objectWriter != null))
                {
                    ((IDisposable)(objectWriter)).Dispose();
                }
            }
        }
Beispiel #5
0
        public static void InitializeFromStream(Activity a, Assembly localAssembly, Stream xamlStream,
                                                XamlSchemaContext schemaContext)
        {
            XmlReader        xmlReader    = null;
            XamlReader       reader       = null;
            XamlObjectWriter objectWriter = null;

            try
            {
                xmlReader = XmlReader.Create(xamlStream);

                XamlXmlReaderSettings readerSettings = new XamlXmlReaderSettings();
                readerSettings.LocalAssembly = localAssembly;
                readerSettings.AllowProtectedMembersOnRoot = true;
                reader = new XamlXmlReader(xmlReader, schemaContext, readerSettings);

                XamlObjectWriterSettings writerSettings = new XamlObjectWriterSettings();
                writerSettings.RootObjectInstance = a;
                writerSettings.AccessLevel        = XamlAccessLevel.PrivateAccessTo(a.GetType());
                objectWriter = new XamlObjectWriter(schemaContext, writerSettings);

                XamlServices.Transform(reader, objectWriter);
            }
            finally
            {
                if (xmlReader != null)
                {
                    ((IDisposable)xmlReader).Dispose();
                }
                if (reader != null)
                {
                    ((IDisposable)reader).Dispose();
                }
                if (objectWriter != null)
                {
                    ((IDisposable)objectWriter).Dispose();
                }
            }
        }
Beispiel #6
0
 public void AccessLevelCtorUnqualifiedTypeName()
 {
     XamlAccessLevel.PrivateAccessTo(typeof(LoadPermissionTests).FullName);
 }