示例#1
0
        public void TestBeginConnectException()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            using (var client = new ImapClient(server.Host, server.Port, "user")) {
              client.Profile.Timeout = 250;

              Assert.IsFalse(client.IsConnected);

              var asyncResult = client.BeginConnect("pass");

              try {
            client.EndConnect(asyncResult);
            Assert.Fail("ImapConnectionException not thrown");
              }
              catch (ImapConnectionException) {
            Assert.IsNull((client.Profile as IImapSessionProfile).Credentials);
              }

              Assert.IsFalse(client.IsConnected);
            }
              }
        }
示例#2
0
 internal ImapMailboxInfo(ImapClient client, ImapMailbox mailbox)
 {
     this.client = client;
       this.mailbox = mailbox;
 }
示例#3
0
        public static ImapMailboxInfo Create(ImapClient client, string mailboxName, bool subscribe)
        {
            if (client == null)
            throw new ArgumentNullException("client");

              return client.CreateMailbox(mailboxName, subscribe);
        }
示例#4
0
 public static ImapMailboxInfo Create(ImapClient client, string mailboxName)
 {
     return Create(client, mailboxName, ImapClient.DefaultSubscription);
 }
 internal ImapOpenedMailboxInfo(ImapClient client, ImapMailbox mailbox)
     : base(client, mailbox)
 {
 }
示例#6
0
        public void TestDisposeAsyncConnectRunning()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            using (var client = new ImapClient(server.Host, server.Port, "user")) {
              client.Profile.AllowInsecureLogin = true;
              client.Profile.Timeout = 5000;

              Assert.IsFalse(client.IsConnected);

              var asyncResult = (ImapClient.ConnectAsyncResult)client.BeginConnect("pass");

              // greeting
              server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1] ImapPseudoServer ready\r\n");
              // LOGIN
              server.EnqueueTaggedResponse("* CAPABILITY IMAP4rev1\r\n" +
                                       "$tag OK done\r\n");

              Assert.IsFalse(asyncResult.EndConnectCalled);

              ((IDisposable)client).Dispose();

              StringAssert.Contains("LOGIN user pass", server.DequeueRequest());
              Assert.IsFalse(client.IsConnected);

              client.EndConnect(asyncResult);

              Assert.IsTrue(asyncResult.EndConnectCalled);

              Assert.IsFalse(client.IsConnected);
            }
              }
        }
示例#7
0
        public void TestEndConnectInvalidAsyncResult()
        {
            using (ImapPseudoServer
             server1 = new ImapPseudoServer(),
             server2 = new ImapPseudoServer()) {
            server1.Start();
            server2.Start();

            using (ImapClient
               client1 = new ImapClient(server1.Host, server1.Port, "user1"),
               client2 = new ImapClient(server2.Host, server2.Port, "user2")) {
              client1.Profile.Timeout = 10;
              client2.Profile.Timeout = 10;

              var asyncResult1 = client1.BeginConnect("pass1");
              var asyncResult2 = client2.BeginConnect(new NetworkCredential("user2", "pass2"));

              try {
            client1.EndConnect(asyncResult2);
            Assert.Fail("ArgumentException not thrown");
              }
              catch (ArgumentException) {
              }

              try {
            client2.EndConnect(asyncResult1);
            Assert.Fail("ArgumentException not thrown");
              }
              catch (ArgumentException) {
              }

              try {
            client1.EndConnect(asyncResult1);
              }
              catch (ImapConnectionException) {
            // expected exception
              }

              try {
            client2.EndConnect(asyncResult2);
              }
              catch (ImapConnectionException) {
            // expected exception
              }
            }
              }
        }
示例#8
0
        private void DisconnectAsyncConnectRunning(bool logout)
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            using (var client = new ImapClient(server.Host, server.Port, "user")) {
              client.BeginConnect("pass");

              try {
            if (logout)
              client.Logout();
            else
              client.Disconnect();

            Assert.Fail("InvalidOperationException not thrown");
              }
              catch (InvalidOperationException) {
              }
            }
              }
        }
示例#9
0
 private void OperationAfterException(ImapClient client)
 {
     try {
     client.Refresh();
     Assert.Fail("InvalidOperationException not thrown");
       }
       catch (InvalidOperationException) {
       }
 }
示例#10
0
        private ImapClient DefaultPropertyAssertion(ImapClient client, int timeoutMilliseconds)
        {
            Assert.IsNotNull(client.Profile);
              Assert.IsTrue(client.Profile.UseTlsIfAvailable);
              Assert.IsFalse(client.Profile.AllowInsecureLogin);
              Assert.IsFalse(client.IsConnected);
              Assert.AreEqual(timeoutMilliseconds, client.Timeout);
              Assert.AreEqual(System.Threading.Timeout.Infinite, client.ReceiveTimeout);
              Assert.AreEqual(System.Threading.Timeout.Infinite, client.SendTimeout);

              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsTrue(client.IsSecureSession));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsNotNull(client.OpenedMailbox));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsNotNull(client.ServerCapabilities));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsNotNull(client.ServerID));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsNotNull(client.ServerNamespace));

              return client;
        }
示例#11
0
 private ImapClient DefaultPropertyAssertion(ImapClient client)
 {
     return DefaultPropertyAssertion(client, System.Threading.Timeout.Infinite);
 }
示例#12
0
        private void Connect(Action<ImapPseudoServer, ImapClient> action)
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            using (var client = new ImapClient(server.Host, server.Port, "user")) {
              Assert.IsFalse(client.IsConnected);
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsTrue(client.IsSecureSession));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsNotNull(client.OpenedMailbox));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsNotNull(client.ServerCapabilities));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsNotNull(client.ServerNamespace));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsNotNull(client.ServerID));

              action(server, client);
            }
              }
        }
示例#13
0
        public void TestConnectBeginConnectRunning()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            using (var client = new ImapClient(server.Host, server.Port, "user")) {
              client.BeginConnect("pass");

              try {
            client.Connect("pass");
            Assert.Fail("InvalidOperationException not thrown");
              }
              catch (InvalidOperationException) {
              }

              try {
            client.Connect(new NetworkCredential("user", "pass"));
            Assert.Fail("InvalidOperationException not thrown");
              }
              catch (InvalidOperationException) {
              }
            }
              }
        }
示例#14
0
        private void BeginConnect(bool usePassword)
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            using (var client = new ImapClient(server.Host, server.Port, "user")) {
              client.Profile.Timeout = 5000;

              var callbacked = false;

              var asyncCallback = (AsyncCallback)delegate(IAsyncResult ar) {
            callbacked = true;
            Assert.IsNotNull(ar);
            Assert.AreSame(ar.AsyncState, client);
              };

              client.Profile.AllowInsecureLogin = true;

              var asyncResult = usePassword
            ? (ImapClient.ConnectAsyncResult)client.BeginConnect("pass", asyncCallback, client)
            : (ImapClient.ConnectAsyncResult)client.BeginConnect(new NetworkCredential("user", "pass"), asyncCallback, client);

              Assert.IsFalse(client.IsConnected);
              Assert.IsNotNull(asyncResult);
              Assert.IsFalse(asyncResult.IsCompleted);
              Assert.IsFalse(asyncResult.EndConnectCalled);
              Assert.IsFalse(callbacked);

              try {
            client.BeginConnect("pass");
            Assert.Fail("InvalidOperationException not thrown");
              }
              catch (InvalidOperationException) {
              }

              var sw = Stopwatch.StartNew();

              ThreadPool.QueueUserWorkItem(delegate(object state) {
            var s = state as ImapPseudoServer;

            Thread.Sleep(250);

            // greeting
            s.EnqueueResponse("* OK [CAPABILITY IMAP4rev1] ImapPseudoServer ready\r\n");
            // LOGIN
            s.EnqueueTaggedResponse("* CAPABILITY IMAP4rev1\r\n" +
                                    "$tag OK done\r\n");
              }, server);

              Assert.IsTrue(asyncResult.AsyncWaitHandle.WaitOne(3000, false));

              sw.Stop();

              Assert.IsFalse(client.IsConnected);
              Assert.GreaterOrEqual(sw.ElapsedMilliseconds, 250);

              Thread.Sleep(50); // wait for callback
              Assert.IsTrue(callbacked);
              Assert.IsFalse(asyncResult.EndConnectCalled);

              client.EndConnect(asyncResult);

              Assert.IsTrue(asyncResult.EndConnectCalled);
              Assert.IsTrue(client.IsConnected);
              Assert.IsFalse(client.IsSecureSession);

              Assert.IsNull((client.Profile as IImapSessionProfile).Credentials);

              try {
            client.EndConnect(asyncResult);
            Assert.Fail("InvalidOperationException not thrown");
              }
              catch (ArgumentException) {
              }
            }
              }
        }