public void TestArgumentExceptions()
        {
            var enumeratedRights = new [] { AccessRight.OpenFolder, AccessRight.CreateFolder };
            var array            = new AccessRight[10];

            var rights = new AccessRights(enumeratedRights);

            Assert.Throws <ArgumentNullException> (() => rights.AddRange((string)null));
            Assert.Throws <ArgumentNullException> (() => rights.AddRange((IEnumerable <AccessRight>)null));
            Assert.Throws <ArgumentNullException> (() => new AccessRights((string)null));
            Assert.Throws <ArgumentNullException> (() => new AccessRights((IEnumerable <AccessRight>)null));
            Assert.Throws <ArgumentOutOfRangeException> (() => { var x = rights [-1]; });
            Assert.Throws <ArgumentNullException> (() => rights.CopyTo(null, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => rights.CopyTo(array, -1));

            var control = new AccessControl("control");

            Assert.Throws <ArgumentNullException> (() => new AccessControl(null));
            Assert.Throws <ArgumentNullException> (() => new AccessControl(null, "rk"));
            Assert.Throws <ArgumentNullException> (() => new AccessControl(null, enumeratedRights));
            Assert.Throws <ArgumentNullException> (() => new AccessControl("name", (string)null));
            Assert.Throws <ArgumentNullException> (() => new AccessControl("name", (IEnumerable <AccessRight>)null));

            var list = new AccessControlList();

            Assert.Throws <ArgumentNullException> (() => new AccessControlList(null));
            //Assert.Throws<ArgumentNullException> (() => list.Add (null));
            Assert.Throws <ArgumentNullException> (() => list.AddRange(null));
        }
Esempio n. 2
0
        public SecurityDbFaker()
        {
            Features = FeatureDbFaker.Generate(50)
                       .GroupBy(l => l.Name).Select(l => l.First()).ToList();
            Functions = new List <AccessFunctionDb>();
            foreach (var feature in Features)
            {
                var featureFunctions = AccessFunctionDbFaker.Generate(10)
                                       .GroupBy(l => l.Name).Select(l => l.First()).ToList();
                foreach (var function in featureFunctions)
                {
                    function.AccessRights = AccessRightDbFaker
                                            .Generate(feature.Name, function.Name, 5)
                                            .GroupBy(l => l.Name).Select(l => l.First()).ToList();
                }
                Functions.AddRange(featureFunctions);

                feature.AvailableAccessRights = featureFunctions.SelectMany(l => l.AccessRights).ToList();
            }

            AccessRights = Features.SelectMany(l => l.AvailableAccessRights).ToList();

            Roles = RoleDbFaker.Generate(Functions, 15);
            foreach (var role in Roles)
            {
                var rights = AccessRightDbFaker
                             .Generate(count: 5).GroupBy(l => l.Name).Select(l => l.First()).ToList();
                role.AccessRights = rights;
                var rightsDen = AccessRightDbFaker
                                .Generate(count: 5).GroupBy(l => l.Name).Select(l => l.First()).ToList();
                role.DeniedRights = rightsDen;
                AccessRights.AddRange(rights);
                AccessRights.AddRange(rightsDen);
            }

            UserRights = UserRightsDbFaker.Generate(Roles, Functions, 10);
            foreach (var userRight in UserRights)
            {
                var rights = AccessRightDbFaker
                             .Generate(count: 5).GroupBy(l => l.Name).Select(l => l.First()).ToList();
                userRight.AccessRights = rights;
                var rightsDen = AccessRightDbFaker
                                .Generate(count: 5).GroupBy(l => l.Name).Select(l => l.First()).ToList();
                userRight.DeniedRights = rightsDen;
                AccessRights.AddRange(rights);
                AccessRights.AddRange(rightsDen);
            }
        }
Esempio n. 3
0
        public void TestArgumentExceptions()
        {
            var enumeratedRights = new [] { AccessRight.OpenFolder, AccessRight.CreateFolder };

            Assert.Throws <ArgumentNullException> (() => new AccessControl(null));
            Assert.Throws <ArgumentNullException> (() => new AccessControl(null, "rk"));
            Assert.Throws <ArgumentNullException> (() => new AccessControl(null, enumeratedRights));
            Assert.Throws <ArgumentNullException> (() => new AccessControl("name", (string)null));
            Assert.Throws <ArgumentNullException> (() => new AccessControl("name", (IEnumerable <AccessRight>)null));

            Assert.Throws <ArgumentNullException> (() => new AccessControlList(null));

            Assert.Throws <ArgumentNullException> (() => new AccessRights((IEnumerable <AccessRight>)null));
            Assert.Throws <ArgumentNullException> (() => new AccessRights((string)null));

            var rights = new AccessRights();

            Assert.Throws <ArgumentNullException> (() => rights.AddRange((string)null));
            Assert.Throws <ArgumentNullException> (() => rights.AddRange((IEnumerable <AccessRight>)null));

            Assert.Throws <ArgumentNullException> (() => new AlertEventArgs(null));

            Assert.Throws <ArgumentNullException> (() => new FolderNamespace('.', null));

            var             namespaces = new FolderNamespaceCollection();
            FolderNamespace ns;

            Assert.Throws <ArgumentNullException> (() => namespaces.Add(null));
            Assert.Throws <ArgumentNullException> (() => namespaces.Contains(null));
            Assert.Throws <ArgumentNullException> (() => namespaces.Remove(null));
            Assert.Throws <ArgumentOutOfRangeException> (() => ns             = namespaces[-1]);
            Assert.Throws <ArgumentOutOfRangeException> (() => namespaces[-1] = new FolderNamespace('.', ""));

            namespaces.Add(new FolderNamespace('.', ""));
            Assert.Throws <ArgumentNullException> (() => namespaces[0] = null);

            Assert.Throws <ArgumentNullException> (() => new FolderNotFoundException(null));
            Assert.Throws <ArgumentNullException> (() => new FolderNotFoundException("message", null));
            Assert.Throws <ArgumentNullException> (() => new FolderNotFoundException("message", null, new Exception("message")));

            Assert.Throws <ArgumentNullException> (() => new FolderNotOpenException(null, FolderAccess.ReadOnly));
            Assert.Throws <ArgumentNullException> (() => new FolderNotOpenException(null, FolderAccess.ReadOnly, "message"));
            Assert.Throws <ArgumentNullException> (() => new FolderNotOpenException(null, FolderAccess.ReadOnly, "message", new Exception("message")));

            Assert.Throws <ArgumentNullException> (() => new FolderRenamedEventArgs(null, "name"));
            Assert.Throws <ArgumentNullException> (() => new FolderRenamedEventArgs("name", null));

            Assert.Throws <ArgumentOutOfRangeException> (() => new MessageEventArgs(-1));

            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, MessageFlags.Answered, null));
            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, MessageFlags.Answered, null, 1));
            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, UniqueId.MinValue, MessageFlags.Answered, null));
            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, UniqueId.MinValue, MessageFlags.Answered, null, 1));

            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, null));
            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, null, 1));
            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, UniqueId.MinValue, null));
            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, UniqueId.MinValue, null, 1));

            Assert.Throws <ArgumentNullException> (() => new MessageSentEventArgs(null, "response"));
            Assert.Throws <ArgumentNullException> (() => new MessageSentEventArgs(new MimeMessage(), null));

            Assert.Throws <ArgumentNullException> (() => new MessageSummaryFetchedEventArgs(null));

            Assert.Throws <ArgumentNullException> (() => new MessagesVanishedEventArgs(null, false));

            Assert.Throws <ArgumentNullException> (() => new MetadataCollection(null));

            var metadataOptions = new MetadataOptions();

            Assert.Throws <ArgumentOutOfRangeException> (() => metadataOptions.Depth = 500);

            Assert.Throws <ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs(-1));
            Assert.Throws <ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs(-1, 1));
            Assert.Throws <ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs(-1, UniqueId.MinValue, 1));

            Assert.Throws <ArgumentOutOfRangeException> (() => new OrderBy(OrderByType.To, SortOrder.None));

            Assert.Throws <ArgumentNullException> (() => new ProtocolLogger((string)null));
            Assert.Throws <ArgumentNullException> (() => new ProtocolLogger((Stream)null));
            using (var logger = new ProtocolLogger(new MemoryStream())) {
                var buffer = new byte[1024];

                Assert.Throws <ArgumentNullException> (() => logger.LogConnect(null));
                Assert.Throws <ArgumentNullException> (() => logger.LogClient(null, 0, 0));
                Assert.Throws <ArgumentNullException> (() => logger.LogServer(null, 0, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogClient(buffer, -1, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogServer(buffer, -1, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogClient(buffer, 0, -1));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogServer(buffer, 0, -1));
            }

            Assert.Throws <ArgumentNullException> (() => new UniqueIdMap(null, new [] { UniqueId.MinValue }));
            Assert.Throws <ArgumentNullException> (() => new UniqueIdMap(new [] { UniqueId.MinValue }, null));
        }
		public void TestArgumentExceptions ()
		{
			var enumeratedRights = new [] { AccessRight.OpenFolder, AccessRight.CreateFolder };

			Assert.Throws<ArgumentNullException> (() => new AccessControl (null));
			Assert.Throws<ArgumentNullException> (() => new AccessControl (null, "rk"));
			Assert.Throws<ArgumentNullException> (() => new AccessControl (null, enumeratedRights));
			Assert.Throws<ArgumentNullException> (() => new AccessControl ("name", (string) null));
			Assert.Throws<ArgumentNullException> (() => new AccessControl ("name", (IEnumerable<AccessRight>) null));

			Assert.Throws<ArgumentNullException> (() => new AccessControlList (null));

			Assert.Throws<ArgumentNullException> (() => new AccessRights ((IEnumerable<AccessRight>) null));
			Assert.Throws<ArgumentNullException> (() => new AccessRights ((string) null));

			var rights = new AccessRights ();
			Assert.Throws<ArgumentNullException> (() => rights.AddRange ((string) null));
			Assert.Throws<ArgumentNullException> (() => rights.AddRange ((IEnumerable<AccessRight>) null));

			Assert.Throws<ArgumentNullException> (() => new AlertEventArgs (null));

			Assert.Throws<ArgumentNullException> (() => new FolderNamespace ('.', null));

			var namespaces = new FolderNamespaceCollection ();
			FolderNamespace ns;

			Assert.Throws<ArgumentNullException> (() => namespaces.Add (null));
			Assert.Throws<ArgumentNullException> (() => namespaces.Contains (null));
			Assert.Throws<ArgumentNullException> (() => namespaces.Remove (null));
			Assert.Throws<ArgumentOutOfRangeException> (() => ns = namespaces[-1]);
			Assert.Throws<ArgumentOutOfRangeException> (() => namespaces[-1] = new FolderNamespace ('.', ""));

			namespaces.Add (new FolderNamespace ('.', ""));
			Assert.Throws<ArgumentNullException> (() => namespaces[0] = null);

			Assert.Throws<ArgumentNullException> (() => new FolderNotFoundException (null));
			Assert.Throws<ArgumentNullException> (() => new FolderNotFoundException ("message", null));
			Assert.Throws<ArgumentNullException> (() => new FolderNotFoundException ("message", null, new Exception ("message")));

			Assert.Throws<ArgumentNullException> (() => new FolderNotOpenException (null, FolderAccess.ReadOnly));
			Assert.Throws<ArgumentNullException> (() => new FolderNotOpenException (null, FolderAccess.ReadOnly, "message"));
			Assert.Throws<ArgumentNullException> (() => new FolderNotOpenException (null, FolderAccess.ReadOnly, "message", new Exception ("message")));

			Assert.Throws<ArgumentNullException> (() => new FolderRenamedEventArgs (null, "name"));
			Assert.Throws<ArgumentNullException> (() => new FolderRenamedEventArgs ("name", null));

			Assert.Throws<ArgumentOutOfRangeException> (() => new MessageEventArgs (-1));

			Assert.Throws<ArgumentNullException> (() => new MessageFlagsChangedEventArgs (0, MessageFlags.Answered, null));
			Assert.Throws<ArgumentNullException> (() => new MessageFlagsChangedEventArgs (0, MessageFlags.Answered, null, 1));
			Assert.Throws<ArgumentNullException> (() => new MessageFlagsChangedEventArgs (0, UniqueId.MinValue, MessageFlags.Answered, null));
			Assert.Throws<ArgumentNullException> (() => new MessageFlagsChangedEventArgs (0, UniqueId.MinValue, MessageFlags.Answered, null, 1));

			Assert.Throws<ArgumentNullException> (() => new MessageLabelsChangedEventArgs (0, null));
			Assert.Throws<ArgumentNullException> (() => new MessageLabelsChangedEventArgs (0, null, 1));
			Assert.Throws<ArgumentNullException> (() => new MessageLabelsChangedEventArgs (0, UniqueId.MinValue, null));
			Assert.Throws<ArgumentNullException> (() => new MessageLabelsChangedEventArgs (0, UniqueId.MinValue, null, 1));

			Assert.Throws<ArgumentNullException> (() => new MessageSentEventArgs (null, "response"));
			Assert.Throws<ArgumentNullException> (() => new MessageSentEventArgs (new MimeMessage (), null));

			Assert.Throws<ArgumentNullException> (() => new MessageSummaryFetchedEventArgs (null));

			Assert.Throws<ArgumentNullException> (() => new MessagesVanishedEventArgs (null, false));

			Assert.Throws<ArgumentNullException> (() => new MetadataCollection (null));

			var metadataOptions = new MetadataOptions ();
			Assert.Throws<ArgumentOutOfRangeException> (() => metadataOptions.Depth = 500);

			Assert.Throws<ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs (-1));
			Assert.Throws<ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs (-1, 1));
			Assert.Throws<ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs (-1, UniqueId.MinValue, 1));

			Assert.Throws<ArgumentOutOfRangeException> (() => new OrderBy (OrderByType.To, SortOrder.None));

			Assert.Throws<ArgumentNullException> (() => new ProtocolLogger ((string) null));
			Assert.Throws<ArgumentNullException> (() => new ProtocolLogger ((Stream) null));
			using (var logger = new ProtocolLogger (new MemoryStream ())) {
				var buffer = new byte[1024];

				Assert.Throws<ArgumentNullException> (() => logger.LogConnect (null));
				Assert.Throws<ArgumentNullException> (() => logger.LogClient (null, 0, 0));
				Assert.Throws<ArgumentNullException> (() => logger.LogServer (null, 0, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => logger.LogClient (buffer, -1, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => logger.LogServer (buffer, -1, 0));
				Assert.Throws<ArgumentOutOfRangeException> (() => logger.LogClient (buffer, 0, -1));
				Assert.Throws<ArgumentOutOfRangeException> (() => logger.LogServer (buffer, 0, -1));
			}

			Assert.Throws<ArgumentNullException> (() => new UniqueIdMap (null, new [] { UniqueId.MinValue }));
			Assert.Throws<ArgumentNullException> (() => new UniqueIdMap (new [] { UniqueId.MinValue }, null));
		}
        public void TestAccessRights()
        {
            var expected = new [] { AccessRight.OpenFolder, AccessRight.CreateFolder, AccessRight.DeleteFolder, AccessRight.ExpungeFolder, AccessRight.AppendMessages, AccessRight.SetMessageDeleted };
            var rights   = new AccessRights();
            int i;

            Assert.IsFalse(rights.IsReadOnly, "IsReadOnly");

            Assert.IsTrue(rights.Add(AccessRight.OpenFolder), "Add OpenFolder");
            Assert.AreEqual(1, rights.Count, "Count after adding OpenFolder");
            Assert.IsFalse(rights.Add(AccessRight.OpenFolder), "Add OpenFolder again");
            Assert.AreEqual(1, rights.Count, "Count after adding OpenFolder again");

            Assert.IsTrue(rights.Add(AccessRight.CreateFolder.Right), "Add CreateFolder");
            Assert.AreEqual(2, rights.Count, "Count after adding CreateFolder");
            Assert.IsFalse(rights.Add(AccessRight.CreateFolder), "Add CreateFolder again");
            Assert.AreEqual(2, rights.Count, "Count after adding OpenFolder again");

            rights.AddRange(new [] { AccessRight.DeleteFolder, AccessRight.ExpungeFolder });
            Assert.AreEqual(4, rights.Count, "Count after adding DeleteFolder and ExpungeFolder");

            Assert.IsTrue(rights.Contains(AccessRight.DeleteFolder), "Contains DeleteFolder");
            Assert.IsTrue(rights.Contains(AccessRight.ExpungeFolder), "Contains ExpungeFolder");
            Assert.IsFalse(rights.Contains(AccessRight.Administer), "Contains Administer");

            rights.AddRange("it");
            Assert.AreEqual(6, rights.Count, "Count after adding AppendMessages and SetMessageDeleted");

            Assert.IsTrue(rights.Contains(AccessRight.AppendMessages), "Contains AppendMessages");
            Assert.IsTrue(rights.Contains(AccessRight.SetMessageDeleted), "Contains SetMessageDeleted");
            Assert.IsFalse(rights.Contains(AccessRight.Administer), "Contains Administer");

            for (i = 0; i < 6; i++)
            {
                Assert.AreEqual(expected[i], rights[i], "rights[{0}]", i);
            }

            ((ICollection <AccessRight>)rights).Add(AccessRight.Administer);
            Assert.IsTrue(rights.Remove(AccessRight.Administer), "Remove Administer");
            Assert.IsFalse(rights.Remove(AccessRight.Administer), "Remove Administer again");

            i = 0;
            foreach (var right in rights)
            {
                Assert.AreEqual(expected[i], right, "foreach rights[{0}]", i++);
            }

            i = 0;
            foreach (AccessRight right in ((IEnumerable)rights))
            {
                Assert.AreEqual(expected[i], right, "generic foreach rights[{0}]", i++);
            }

            var array = new AccessRight[rights.Count];

            rights.CopyTo(array, 0);

            for (i = 0; i < 6; i++)
            {
                Assert.AreEqual(expected[i], array[i], "CopyTo[{0}]", i);
            }

            Assert.AreEqual("rkxeit", rights.ToString(), "ToString");
        }