Пример #1
0
    public static void Main(string[] args)
    {
        PopConnection.ServerCertificateValidationCallback += delegate {
          // 適切な証明書の検証を行うように書き換えてください
          return true;
        };

        using (var client = new PopClient(new Uri("pop://[email protected]/"))) {
          client.Connect("pass");

          Console.WriteLine("connected!!");
        }
    }
Пример #2
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);
            }
              }
        }
Пример #3
0
 public void TestPropertyGetServerCapabilitiesNotConnected()
 {
     using (var client = new PopClient()) {
     Assert.IsNotNull(client.ServerCapabilities);
       }
 }
Пример #4
0
 public void TestPropertyGetMessageCountNotConnected()
 {
     using (var client = new PopClient()) {
     Assert.AreNotEqual(0L, client.MessageCount);
       }
 }
Пример #5
0
 public void TestPropertyGetIsSecureSessionNotConnected()
 {
     using (var client = new PopClient()) {
     Assert.IsTrue(client.IsSecureSession);
       }
 }
Пример #6
0
 public void TestGetMessageNotConnected()
 {
     using (var client = new PopClient()) {
     client.GetMessage(1L);
       }
 }
Пример #7
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
              }
            }
              }
        }
Пример #8
0
        private void Connect(Action<PopPseudoServer, PopClient> action)
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            using (var client = new PopClient(server.Host, server.Port, "user")) {
              Assert.IsFalse(client.IsConnected);
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsTrue(client.IsSecureSession));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.IsNotNull(client.ServerCapabilities));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.AreNotEqual(0L, client.TotalSize));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.AreNotEqual(0L, client.MessageCount));

              // 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");

              action(server, client);
            }
              }
        }
Пример #9
0
        public void TestConnectWithSpecifiedSaslMechanism()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            using (var client = new PopClient(server.Host, server.Port)) {
              // greeting
              server.EnqueueResponse("+OK\r\n");
              // CAPA
              server.EnqueueResponse("+OK\r\n" +
                                 ".\r\n");
              // AUTH X-PSEUDO-MECHANISM
              server.EnqueueResponse("+ \r\n");
              server.EnqueueResponse("+OK\r\n");

              using (var authMechanism = new SaslPseudoMechanism(false, 1)) {
            client.Connect(authMechanism);

            Assert.AreEqual(Smdn.Security.Authentication.Sasl.SaslExchangeStatus.Succeeded,
                            authMechanism.ExchangeStatus);
              }

              Assert.IsTrue(client.IsConnected);
              Assert.IsFalse(client.IsSecureSession);
              Assert.IsNotNull(client.ServerCapabilities);
              Assert.IsNull((client.Profile as IPopSessionProfile).Credentials);
            }
              }
        }
Пример #10
0
        public void TestConnectBeginConnectRunning()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

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

              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) {
              }
            }
              }
        }
Пример #11
0
 private void OperationAfterException(PopClient client)
 {
     try {
     client.CancelDelete();
     Assert.Fail("InvalidOperationException not thrown");
       }
       catch (InvalidOperationException) {
       }
 }
Пример #12
0
        private void DisconnectAsyncConnectRunning(bool logout)
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

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

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

            Assert.Fail("InvalidOperationException not thrown");
              }
              catch (InvalidOperationException) {
              }
            }
              }
        }
Пример #13
0
        private PopClient DefaultPropertyAssertion(PopClient client, int timeoutMilliseconds)
        {
            Assert.IsNotNull(client.Profile);
              Assert.IsTrue(client.Profile.UseTlsIfAvailable);
              Assert.IsFalse(client.Profile.AllowInsecureLogin);
              Assert.IsFalse(client.DeleteAfterRetrieve);
              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.ServerCapabilities));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.AreNotEqual(0L, client.TotalSize));
              TestUtils.ExpectExceptionThrown<InvalidOperationException>(() => Assert.AreNotEqual(0L, client.MessageCount));

              return client;
        }
Пример #14
0
 private PopClient DefaultPropertyAssertion(PopClient client)
 {
     return DefaultPropertyAssertion(client, System.Threading.Timeout.Infinite);
 }
Пример #15
0
 public void TestPropertyGetTotalSizeNotConnected()
 {
     using (var client = new PopClient()) {
     Assert.AreNotEqual(0L, client.TotalSize);
       }
 }
Пример #16
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);
            }
              }
        }
Пример #17
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) {
              }
            }
              }
        }