Search in sources :

Example 16 with ServiceException

use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.

the class ContactTest method createInvalidImageAttachment.

/**
     * Tests Invalid image attachment (bug 71868).
     */
@Test
public void createInvalidImageAttachment() throws Exception {
    // Create a contact with an attachment.
    Map<String, String> attrs = new HashMap<String, String>();
    attrs.put("fullName", "Get Attachment Content");
    byte[] attachData = "attachment 1".getBytes();
    Attachment attachment = new Attachment(attachData, "image/png", "image", "file1.png");
    try {
        ParsedContact pc = new ParsedContact(attrs, Lists.newArrayList(attachment));
        Assert.fail("Expected INVALID_IMAGE exception");
    } catch (ServiceException se) {
        Assert.assertEquals("check the INVALID_IMAGE exception", "mail.INVALID_IMAGE", se.getCode());
    }
}
Also used : ParsedContact(com.zimbra.cs.mime.ParsedContact) ServiceException(com.zimbra.common.service.ServiceException) HashMap(java.util.HashMap) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) Test(org.junit.Test)

Example 17 with ServiceException

use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.

the class ACLTest method testRegrantDifferentPermission.

@Test
public void testRegrantDifferentPermission() throws Exception {
    Account owner = Provisioning.getInstance().get(Key.AccountBy.name, "owner@zimbra.com");
    Account grantee = Provisioning.getInstance().get(Key.AccountBy.name, "principal@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(owner);
    Folder folder = mbox.createFolder(null, "shared", new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT));
    OperationContext octxt = new OperationContext(owner);
    mbox.grantAccess(octxt, folder.getId(), grantee.getId(), ACL.GRANTEE_USER, ACL.stringToRights("r"), null);
    try {
        mbox.grantAccess(octxt, folder.getId(), grantee.getId(), ACL.GRANTEE_USER, ACL.stringToRights("rw"), null);
    } catch (ServiceException se) {
        if (!se.getCode().equals(MailServiceException.GRANT_EXISTS)) {
            Assert.fail("regrant throws ServiceException with code " + se.getCode());
        }
        Assert.fail("regrant has failed");
    }
}
Also used : GuestAccount(com.zimbra.cs.account.GuestAccount) Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) Test(org.junit.Test)

Example 18 with ServiceException

use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.

the class ACLTest method testRegrant.

@Test
public void testRegrant() throws Exception {
    Account owner = Provisioning.getInstance().get(Key.AccountBy.name, "owner@zimbra.com");
    Account grantee = Provisioning.getInstance().get(Key.AccountBy.name, "principal@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(owner);
    Folder folder = mbox.createFolder(null, "shared", new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT));
    OperationContext octxt = new OperationContext(owner);
    mbox.grantAccess(octxt, folder.getId(), grantee.getId(), ACL.GRANTEE_USER, ACL.stringToRights("r"), null);
    try {
        mbox.grantAccess(octxt, folder.getId(), grantee.getId(), ACL.GRANTEE_USER, ACL.stringToRights("r"), null);
        Assert.fail("regrant has succeeded");
    } catch (ServiceException se) {
        if (!se.getCode().equals(MailServiceException.GRANT_EXISTS)) {
            Assert.fail("regrant throws ServiceException with code " + se.getCode());
        }
    }
}
Also used : GuestAccount(com.zimbra.cs.account.GuestAccount) Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) Test(org.junit.Test)

Example 19 with ServiceException

use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.

the class MailboxLockTest method tooManyWaiters.

@Test
public void tooManyWaiters() {
    Mailbox mbox = null;
    try {
        mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    } catch (ServiceException e) {
        Assert.fail();
    }
    int threads = LC.zimbra_mailbox_lock_max_waiting_threads.intValue();
    final AtomicBoolean done = new AtomicBoolean(false);
    final Set<Thread> waitThreads = new HashSet<Thread>();
    for (int i = 0; i < threads; i++) {
        Thread waitThread = new Thread("MailboxLockTest-Waiter") {

            @Override
            public void run() {
                Mailbox mbox;
                try {
                    mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                    mbox.lock.lock(false);
                    while (!done.get()) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                        }
                    }
                    mbox.lock.release();
                } catch (ServiceException e) {
                }
            }
        };
        waitThread.setDaemon(true);
        waitThreads.add(waitThread);
    }
    Thread writeThread = new Thread("MailboxLockTest-Writer") {

        @Override
        public void run() {
            Mailbox mbox;
            try {
                mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                mbox.lock.lock(true);
                for (Thread waiter : waitThreads) {
                    waiter.start();
                }
                while (!done.get()) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                }
                mbox.lock.release();
            } catch (ServiceException e) {
            }
        }
    };
    writeThread.start();
    while (mbox.lock.getQueueLength() < LC.zimbra_mailbox_lock_max_waiting_threads.intValue()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }
    try {
        //one more reader...this should give too many waiters
        mbox.lock.lock(false);
        Assert.fail("expected too many waiters");
    } catch (LockFailedException e) {
        //expected
        Assert.assertTrue(e.getMessage().startsWith("too many waiters"));
        //cause writer to finish
        done.set(true);
    }
    long joinTimeout = 50000;
    joinWithTimeout(writeThread, joinTimeout);
    for (Thread t : waitThreads) {
        joinWithTimeout(t, joinTimeout);
    }
    //now do a write lock in same thread. previously this would break due to read assert not clearing
    mbox.lock.lock(true);
    mbox.lock.release();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServiceException(com.zimbra.common.service.ServiceException) LockFailedException(com.zimbra.cs.mailbox.MailboxLock.LockFailedException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with ServiceException

use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.

the class MailboxLockTest method tooManyWaitersWithMultipleReadOwners.

@Test
public void tooManyWaitersWithMultipleReadOwners() {
    Mailbox mbox = null;
    try {
        mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    } catch (ServiceException e) {
        Assert.fail();
    }
    int threads = LC.zimbra_mailbox_lock_max_waiting_threads.intValue();
    final AtomicBoolean done = new AtomicBoolean(false);
    final Set<Thread> waitThreads = new HashSet<Thread>();
    for (int i = 0; i < threads; i++) {
        Thread waitThread = new Thread("MailboxLockTest-Waiter-" + i) {

            @Override
            public void run() {
                Mailbox mbox;
                try {
                    mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                    mbox.lock.lock(true);
                    while (!done.get()) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                        }
                    }
                    mbox.lock.release();
                } catch (ServiceException e) {
                }
            }
        };
        waitThread.setDaemon(true);
        waitThreads.add(waitThread);
    }
    int readThreadCount = 20;
    final Set<Thread> readThreads = new HashSet<Thread>();
    for (int i = 0; i < readThreadCount; i++) {
        Thread readThread = new Thread("MailboxLockTest-Reader-" + i) {

            @Override
            public void run() {
                Mailbox mbox;
                try {
                    mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                    int holdCount = 20;
                    for (int i = 0; i < holdCount; i++) {
                        mbox.lock.lock(false);
                    }
                    while (!done.get()) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                        }
                    }
                    for (int i = 0; i < holdCount; i++) {
                        mbox.lock.release();
                    }
                } catch (ServiceException e) {
                }
            }
        };
        readThreads.add(readThread);
    }
    Thread lastReadThread = new Thread("MailboxLockTest-LastReader") {

        @Override
        public void run() {
            Mailbox mbox;
            try {
                mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                int holdCount = 20;
                for (int i = 0; i < holdCount; i++) {
                    mbox.lock.lock(false);
                }
                //and the other readers
                for (Thread reader : readThreads) {
                    reader.start();
                }
                for (Thread waiter : waitThreads) {
                    waiter.start();
                }
                while (!done.get()) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                }
                for (int i = 0; i < holdCount; i++) {
                    mbox.lock.release();
                }
            } catch (ServiceException e) {
            }
        }
    };
    lastReadThread.start();
    while (mbox.lock.getQueueLength() < LC.zimbra_mailbox_lock_max_waiting_threads.intValue()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }
    try {
        //one more reader...this should give too many waiters
        mbox.lock.lock(false);
        Assert.fail("expected too many waiters");
    } catch (LockFailedException e) {
        //expected
        Assert.assertTrue(e.getMessage().startsWith("too many waiters"));
        //cause writer to finish
        done.set(true);
    }
    long joinTimeout = 50000;
    joinWithTimeout(lastReadThread, joinTimeout);
    for (Thread t : readThreads) {
        joinWithTimeout(t, joinTimeout);
    }
    for (Thread t : waitThreads) {
        joinWithTimeout(t, joinTimeout);
    }
    //now do a write lock in same thread. previously this would break due to read assert not clearing
    mbox.lock.lock(true);
    mbox.lock.release();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServiceException(com.zimbra.common.service.ServiceException) LockFailedException(com.zimbra.cs.mailbox.MailboxLock.LockFailedException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ServiceException (com.zimbra.common.service.ServiceException)772 AccountServiceException (com.zimbra.cs.account.AccountServiceException)220 Account (com.zimbra.cs.account.Account)193 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)149 IOException (java.io.IOException)127 Mailbox (com.zimbra.cs.mailbox.Mailbox)122 ArrayList (java.util.ArrayList)107 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)100 Element (com.zimbra.common.soap.Element)97 HashMap (java.util.HashMap)93 Test (org.junit.Test)89 Provisioning (com.zimbra.cs.account.Provisioning)86 Domain (com.zimbra.cs.account.Domain)60 Folder (com.zimbra.cs.mailbox.Folder)54 Server (com.zimbra.cs.account.Server)53 ItemId (com.zimbra.cs.service.util.ItemId)52 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)51 ZMailbox (com.zimbra.client.ZMailbox)50 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)46 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)44