コード例 #1
0
ファイル: Pop3ClientTests.cs プロジェクト: rajeshwarn/MailKit
        public void TestAuthenticationExceptions()
        {
            var commands = new List<Pop3ReplayCommand> ();
            commands.Add (new Pop3ReplayCommand ("", "comcast.greeting.txt"));
            commands.Add (new Pop3ReplayCommand ("CAPA\r\n", "comcast.capa1.txt"));
            commands.Add (new Pop3ReplayCommand ("USER username\r\n", "comcast.ok.txt"));
            commands.Add (new Pop3ReplayCommand ("PASS password\r\n", "comcast.err.txt"));
            commands.Add (new Pop3ReplayCommand ("QUIT\r\n", "comcast.quit.txt"));

            using (var client = new Pop3Client ()) {
                try {
                    client.ReplayConnect ("localhost", new Pop3ReplayStream (commands, false), CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Connect: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "Client failed to connect.");

                Assert.AreEqual (ComcastCapa1, client.Capabilities);
                Assert.AreEqual (0, client.AuthenticationMechanisms.Count);
                Assert.AreEqual (31, client.ExpirePolicy);

                try {
                    var credentials = new NetworkCredential ("username", "password");
                    client.Authenticate (credentials, CancellationToken.None);
                    Assert.Fail ("Expected AuthenticationException");
                } catch (AuthenticationException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "AuthenticationException should not cause a disconnect.");

                try {
                    var count = client.GetMessageCount (CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Count: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var sizes = client.GetMessageSizes (CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageSizes: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var size = client.GetMessageSize ("uid", CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageSize(uid): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var size = client.GetMessageSize (0, CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageSize(int): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var uids = client.GetMessageUids (CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageUids: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var uid = client.GetMessageUid (0, CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageUid: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var message = client.GetMessage ("uid", CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessage(uid): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var message = client.GetMessage (0, CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessage(int): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    client.DeleteMessage ("uid", CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in DeleteMessage(uid): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    client.DeleteMessage (0, CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in DeleteMessage(int): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    client.Disconnect (true, CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Disconnect: {0}", ex);
                }

                Assert.IsFalse (client.IsConnected, "Failed to disconnect");
            }
        }