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); } } }
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 } } } }
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); } } }
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) { } } } }
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) { } } } }
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) { } } } }