public SecurityException DoTest()
            {
                SecurityException exception = null;

                try
                {
                    var _ = Thread.CurrentPrincipal.Identity.Name;
                }
                catch (SecurityException e)
                {
                    exception = e;
                }

                StringBuilder sb     = new StringBuilder();
                StringWriter  writer = new StringWriter(sb);

                MockTextExceptionFormatter formatter = new MockTextExceptionFormatter(writer, exception, Guid.Empty);

                formatter.Format();

                formatter = new MockTextExceptionFormatter(writer, exception, Guid.Empty);
                formatter.Format();

                return(exception);
            }
예제 #2
0
        public void ReflectionFormatterReadSecurityExceptionPropertiesWithoutPermissionTest()
        {
            SecurityPermission denyPermission
                = new SecurityPermission(SecurityPermissionFlag.ControlPolicy | SecurityPermissionFlag.ControlEvidence);
            PermissionSet permissions = new PermissionSet(PermissionState.None);

            permissions.AddPermission(denyPermission);
            permissions.Deny();
            StringBuilder     sb        = new StringBuilder();
            StringWriter      writer    = new StringWriter(sb);
            SecurityException exception = null;

            try
            {
                DemandException(denyPermission);
            }
            catch (SecurityException e)
            {
                exception = e;
            }
            MockTextExceptionFormatter formatter = new MockTextExceptionFormatter(writer, exception, Guid.Empty);

            formatter.Format();
            CodeAccessPermission.RevertDeny();
            formatter = new MockTextExceptionFormatter(writer, exception, Guid.Empty);
            formatter.Format();
            Assert.AreEqual(exception.Demanded.ToString(), formatter.properties["Demanded"]);
        }
        public void ReflectionFormatterReadSecurityExceptionPropertiesWithoutPermissionTest()
        {
            var evidence = new Evidence();

            evidence.AddHostEvidence(new Zone(SecurityZone.Internet));
            var set = SecurityManager.GetStandardSandbox(evidence);

            set.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess));

            var domain = AppDomain.CreateDomain("partial trust", null, AppDomain.CurrentDomain.SetupInformation, set);

            try
            {
                var instance  = ((Tester)domain.CreateInstanceAndUnwrap(typeof(Tester).Assembly.FullName, typeof(Tester).FullName));
                var exception = instance.DoTest();

                StringBuilder sb     = new StringBuilder();
                StringWriter  writer = new StringWriter(sb);

                var formatter = new MockTextExceptionFormatter(writer, exception, Guid.Empty);
                formatter.Format();
                Assert.AreEqual(exception.Demanded.ToString(), formatter.properties["Demanded"]);
            }
            catch
            {
                throw;
            }
            finally
            {
                AppDomain.Unload(domain);
            }
        }
        public void ReflectionFormatterReadSecurityExceptionPropertiesWithoutPermissionTest()
        {
            SecurityPermission denyPermission
                = new SecurityPermission(SecurityPermissionFlag.ControlPolicy | SecurityPermissionFlag.ControlEvidence);
            PermissionSet permissions = new PermissionSet(PermissionState.None);
            permissions.AddPermission(denyPermission);
            permissions.Deny();

            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);

            SecurityException exception = null;
            try
            {
                DemandException(denyPermission);
            }
            catch (SecurityException e)
            {
                exception = e;
            }

            MockTextExceptionFormatter formatter = new MockTextExceptionFormatter(writer, exception, Guid.Empty);
            formatter.Format();

            CodeAccessPermission.RevertDeny();

            formatter = new MockTextExceptionFormatter(writer, exception, Guid.Empty);
            formatter.Format();
            Assert.AreEqual(exception.Demanded.ToString(), formatter.properties["Demanded"]);
        }
예제 #5
0
        public void ReflectionFormatterReadTest()
        {
            StringBuilder sb     = new StringBuilder();
            StringWriter  writer = new StringWriter(sb);
            MockTextExceptionFormatter formatter
                = new MockTextExceptionFormatter(writer, new MockException(), Guid.Empty);

            formatter.Format();
            Assert.AreEqual(formatter.fields[fieldString], mockFieldString);
            Assert.AreEqual(formatter.properties[propertyString], mockPropertyString);
            Assert.AreEqual(null, formatter.properties[message]);
        }
        public void ReflectionFormatterReadTest()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);

            MockTextExceptionFormatter formatter = new MockTextExceptionFormatter(writer, new MockException());

            formatter.Format();

            Assert.AreEqual(formatter.fields[fieldString], mockFieldString);
            Assert.AreEqual(formatter.properties[propertyString], mockPropertyString);
            // The message should be null because the reflection formatter should ignore this property
            Assert.AreEqual(null, formatter.properties[message]);
        }
        public void ReflectionFormatterReadTest()
        {
            StringBuilder sb     = new StringBuilder();
            StringWriter  writer = new StringWriter(sb);

            MockTextExceptionFormatter formatter = new MockTextExceptionFormatter(writer, new MockException());

            formatter.Format();

            Assert.AreEqual(formatter.fields[fieldString], mockFieldString);
            Assert.AreEqual(formatter.properties[propertyString], mockPropertyString);
            // The message should be null because the reflection formatter should ignore this property
            Assert.AreEqual(null, formatter.properties[message]);
        }
예제 #8
0
        public void ReflectionFormatterReadTest()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb, TestUtility.DefaultCulture);

            MockTextExceptionFormatter formatter = new MockTextExceptionFormatter(writer, new MockException());

            formatter.Format();

            Assert.AreEqual(formatter.fields["FieldString"], "MockFieldString");
            Assert.AreEqual(formatter.properties["PropertyString"], "MockPropertyString");
            // The message should be null because the reflection formatter should ignore this property
            Assert.AreEqual(null, formatter.properties["Message"]);
        }
예제 #9
0
        public void ReflectionFormatterReadTest()
        {
            StringBuilder sb     = new StringBuilder();
            StringWriter  writer = new StringWriter(sb, TestUtility.DefaultCulture);

            MockTextExceptionFormatter formatter = new MockTextExceptionFormatter(writer, new MockException());

            formatter.Format();

            Assert.AreEqual(formatter.fields["FieldString"], "MockFieldString");
            Assert.AreEqual(formatter.properties["PropertyString"], "MockPropertyString");
            // The message should be null because the reflection formatter should ignore this property
            Assert.AreEqual(null, formatter.properties["Message"]);
        }