Example #1
0
        public void TestBeginConnectException()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

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

              Assert.IsFalse(client.IsConnected);

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

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

              Assert.IsFalse(client.IsConnected);
            }
              }
        }
Example #2
0
        public void TestDisposeAsyncConnectRunning()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

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

              Assert.IsFalse(client.IsConnected);

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

              // greeting
              server.EnqueueResponse("+OK\r\n");
              // CAPA
              server.EnqueueResponse("+OK\r\n" +
                                 ".\r\n");
              // USER/PASS
              server.EnqueueResponse("+OK\r\n");
              server.EnqueueResponse("+OK\r\n");

              Assert.IsFalse(asyncResult.EndConnectCalled);

              ((IDisposable)client).Dispose();

              StringAssert.StartsWith("CAPA", server.DequeueRequest());
              StringAssert.StartsWith("USER user", server.DequeueRequest());
              StringAssert.StartsWith("PASS pass", server.DequeueRequest());
              Assert.IsFalse(client.IsConnected);

              client.EndConnect(asyncResult);

              Assert.IsTrue(asyncResult.EndConnectCalled);

              Assert.IsFalse(client.IsConnected);
            }
              }
        }
Example #3
0
        public void TestEndConnectInvalidAsyncResult()
        {
            using (PopPseudoServer
             server1 = new PopPseudoServer(),
             server2 = new PopPseudoServer()) {
            server1.Start();
            server2.Start();

            using (PopClient
               client1 = new PopClient(server1.Host, server1.Port, "user1"),
               client2 = new PopClient(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 (PopConnectionException) {
            // expected exception
              }

              try {
            client2.EndConnect(asyncResult2);
              }
              catch (PopConnectionException) {
            // expected exception
              }
            }
              }
        }
Example #4
0
        private void BeginConnect(bool usePassword)
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

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

              var callbacked = false;

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

              var asyncResult = usePassword
            ? (PopClient.ConnectAsyncResult)client.BeginConnect("pass", asyncCallback, client)
            : (PopClient.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 PopPseudoServer;

            Thread.Sleep(250);

            // greeting
            s.EnqueueResponse("+OK\r\n");
            // CAPA
            s.EnqueueResponse("+OK\r\n" +
                                   ".\r\n");
            // USER/PASS
            s.EnqueueResponse("+OK\r\n");
            s.EnqueueResponse("+OK\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 IPopSessionProfile).Credentials);

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