Deny() private méthode

private Deny ( ) : void
Résultat void
        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 CanReflectOnSecurityExceptionWithoutPermission()
		{
			SecurityPermission denyPermission
				= new SecurityPermission(SecurityPermissionFlag.ControlPolicy | SecurityPermissionFlag.ControlEvidence);
			PermissionSet permissions = new PermissionSet(PermissionState.None);
			permissions.AddPermission(denyPermission);
			permissions.Deny();

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

			String message = formatter.GetMessage(exception);
			Match demandedMatch = DemandedEntryRegex.Match(message);
			Assert.IsNotNull(demandedMatch);
			Assert.AreEqual(Resources.PropertyAccessFailed, demandedMatch.Groups[1].Value);

			CodeAccessPermission.RevertDeny();

			message = formatter.GetMessage(exception);
			demandedMatch = DemandedEntryRegex.Match(message);
			Assert.IsNotNull(demandedMatch);
			Assert.AreNotEqual(Resources.PropertyAccessFailed, demandedMatch.Groups[1].Value);
		}
Exemple #3
0
        /// <summary>
        /// Try/finally is completely unnecessary. Permissions are associated with stack frames
        /// and go out of scope once stack frame returns.
        /// </summary>
        private string GetMachineNameWithoutPermissions()
        {
            EnvironmentPermission denyPermission = new EnvironmentPermission(PermissionState.None);

            PermissionSet permissions = new PermissionSet(PermissionState.None);
            permissions.AddPermission(denyPermission);
            permissions.Deny();

            try
            {
                return Environment.MachineName;
            }
            finally
            {
                CodeAccessPermission.RevertDeny();
            }
        }
        public void NoPermissionToReadFileWhenCreated()
        {
            FileIOPermission denyPermission =
                new FileIOPermission(FileIOPermissionAccess.Read, Path.GetFullPath("TestFile"));

            PermissionSet permissions = new PermissionSet(PermissionState.None);
            permissions.AddPermission(denyPermission);
            permissions.Deny();

            try
            {
                new FileDependency("TestFile");
            }
            finally
            {
                CodeAccessPermission.RevertDeny();
            }
        }
        public void NoPermissionToReadWhenCheckingExpiration()
        {
            FileDependency dependency = new FileDependency("TestFile");

            FileIOPermission denyPermission =
                new FileIOPermission(FileIOPermissionAccess.Read, Path.GetFullPath("TestFile"));

            PermissionSet permissions = new PermissionSet(PermissionState.None);
            permissions.AddPermission(denyPermission);
            permissions.Deny();

            try
            {
                dependency.HasExpired();
            }
            finally
            {
                CodeAccessPermission.RevertDeny();
            }
        }
		public void Deny_NonCasPermission ()
		{
			PermissionSet ps = new PermissionSet (PermissionState.None);
			ps.AddPermission (new PrincipalPermission (PermissionState.None));
			Assert.IsTrue (ps.ContainsNonCodeAccessPermissions (), "ContainsNonCodeAccessPermissions");
			Assert.AreEqual (1, ps.Count, "Count");
			ps.Deny ();
			// it's simply ignored
		}
 public void OpenMachineConfigWithoutPermissions()
 {
     FileIOPermission denyPermission = new FileIOPermission(FileIOPermissionAccess.Read, ConfigurationBuilder.MachineConfigurationFile);
     PermissionSet permissions = new PermissionSet(PermissionState.None);
     permissions.AddPermission(denyPermission);
     permissions.Deny();
     using (new ConfigurationBuilder(ConfigurationBuilder.MachineConfigurationFile))
     {
     }
 }
		public void CanGetMachineNameWithoutSecurity()
		{
			EnvironmentPermission denyPermission = new EnvironmentPermission(EnvironmentPermissionAccess.Read, computerName);
			PermissionSet permissions = new PermissionSet(PermissionState.None);
			permissions.AddPermission(denyPermission);
			permissions.Deny();

			StringBuilder sb = new StringBuilder();
			StringWriter writer = new StringWriter(sb);
			Exception exception = new MockException();
			TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);
			Assert.IsTrue(sb.Length == 0);
			formatter.Format();

			Assert.IsTrue(sb.ToString().Contains(machineName + " : " + permissionDenied));
		}
		public void CanGetWindowsIdentityWithoutSecurity()
		{
			SecurityPermission denyPermission = new SecurityPermission(SecurityPermissionFlag.ControlPrincipal);
			PermissionSet permissions = new PermissionSet(PermissionState.None);
			permissions.AddPermission(denyPermission);
			permissions.Deny();

			StringBuilder sb = new StringBuilder();
			StringWriter writer = new StringWriter(sb);
			Exception exception = new MockException();
			TextExceptionFormatter formatter = new TextExceptionFormatter(writer, exception);
			Assert.IsTrue(sb.Length == 0);
			formatter.Format();
			Console.WriteLine(sb.ToString());
			Assert.IsTrue(sb.ToString().Contains(windowsIdentity + " : " + permissionDenied));
		}				
		public void Deny_NonCasPermission ()
		{
			PermissionSet ps = new PermissionSet (PermissionState.None);
			ps.AddPermission (new PrincipalPermission (PermissionState.None));
			Assert ("ContainsNonCodeAccessPermissions", ps.ContainsNonCodeAccessPermissions ());
			AssertEquals ("Count", 1, ps.Count);
			ps.Deny ();
			// it's simply ignored
		}
        public void OpenConfigFileWithoutPermissions()
        {
            FileIOPermission denyPermission =
                new FileIOPermission(FileIOPermissionAccess.Read, Path.GetFullPath(myConfigTestFile));

            PermissionSet permissions = new PermissionSet(PermissionState.None);
            permissions.AddPermission(denyPermission);
            permissions.Deny();
            ConfigurationFile configFile = new ConfigurationFile();
            configFile.Load(Path.GetFullPath(myConfigTestFile));
        }