internal ImapWebClientFetchMessageBodyStream(ImapSession session, bool keepAlive, bool peek, ImapSequenceSet fetchUidSet, int fetchBlockSize, int readWriteTimeout) : this(session, keepAlive, peek, fetchUidSet, null, null, fetchBlockSize, readWriteTimeout) { }
public void TestAuthenticateCredentialNotFound() { server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1 AUTH=DIGEST-MD5] ImapSimulatedServer ready\r\n"); using (var session = new ImapSession(host, port)) { Assert.IsFalse((bool)session.Authenticate(new NullCredential(), ImapAuthenticationMechanism.DigestMD5)); Assert.IsFalse((bool)session.Authenticate(new NullCredential(), "user", ImapAuthenticationMechanism.DigestMD5)); } }
protected void CloseMailbox(ImapSession session, string tag) { // CLOSE transaction server.EnqueueResponse(tag + " OK CLOSE completed\r\n"); session.Close(); Assert.AreEqual(tag + " CLOSE\r\n", server.DequeueRequest()); session.Disconnect(false); }
internal ImapWebClientFetchMessageBodyStream(ImapSession session, bool keepAlive, bool peek, ImapSequenceSet fetchUidSet, string fetchSection, ImapPartialRange? fetchRange, int fetchBlockSize, int readWriteTimeout) : base(session, peek, fetchUidSet, fetchSection, fetchRange, fetchBlockSize) { this.keepAlive = keepAlive; ReadTimeout = readWriteTimeout; WriteTimeout = readWriteTimeout; }
public void TestAuthenticateCancelExchanging() { server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1 AUTH=DIGEST-MD5] ImapSimulatedServer ready\r\n"); using (var session = new ImapSession(host, port)) { server.EnqueueResponse("+ eD0xLHk9Mix6PTM=\r\n"); // x=1,y=2,z=3 server.EnqueueResponse("0000 NO AUTHENTICATE failed.\r\n"); session.Authenticate(new NetworkCredential("test", "test", "test"), ImapAuthenticationMechanism.DigestMD5); Assert.AreEqual("0000 AUTHENTICATE DIGEST-MD5\r\n", server.DequeueRequest()); Assert.AreEqual("*\r\n", server.DequeueRequest()); } }
public void TestConnectSslExceptionWhileUpgrading() { server.EnqueueResponse("* OK ImapSimulatedServer ready\r\n"); try { using (var session = new ImapSession(host, port, delegate(ConnectionBase connection, Stream baseStream) { throw new System.Security.Authentication.AuthenticationException(); })) { Assert.Fail("connected"); } } catch (ImapSecureConnectionException ex) { var upgradeException = ex.InnerException as ImapUpgradeConnectionException; Assert.IsNotNull(upgradeException); Assert.IsNotNull(upgradeException.InnerException); Assert.IsInstanceOfType(typeof(System.Security.Authentication.AuthenticationException), upgradeException.InnerException); } }
public void TestConnectTransactionTimeout() { server.Stop(); try { using (var session = new ImapSession(host, port, 500)) { Assert.Fail("ImapConnectionException not thrown"); } } catch (ImapConnectionException ex) { var timeoutException = ex.InnerException as TimeoutException; Assert.IsNotNull(timeoutException); } }
public void TestConnectSocketError() { try { using (var session = new ImapSession("imap.invalid", 10143)) { Assert.Fail("ImapConnectionException not thrown"); } } catch (ImapConnectionException ex) { var socketException = ex.InnerException as System.Net.Sockets.SocketException; Assert.IsNotNull(socketException); } }
public void TestConnectSsl() { server.EnqueueResponse("* OK ImapSimulatedServer ready\r\n"); var streamUpgraded = false; using (var session = new ImapSession(host, port, delegate(ConnectionBase connection, Stream baseStream) { streamUpgraded = true; return baseStream; // TODO: return SslStream })) { Assert.IsFalse(session.IsIdling); Assert.IsNull(session.SelectedMailbox); Assert.AreEqual(ImapSessionState.NotAuthenticated, session.State); Assert.AreEqual(new Uri(string.Format("imaps://{0}:{1}/", host, port)), session.Authority); Assert.AreEqual(0, session.ServerCapabilities.Count); Assert.IsTrue(streamUpgraded, "stream upgraded"); Assert.IsTrue(session.IsSecureConnection); } }
private void Callback(IAsyncResult asyncResult) { ImapSession ret = null; lock (asyncResultLockObject) { try { ret = ((ConnectProc)innerAsyncResult.AsyncDelegate).EndInvoke(innerAsyncResult); if (!OwnerDisposed) createdSession = ret; } catch (Exception ex) { exception = ex; } } if (asyncCallback != null) asyncCallback(this); if (OwnerDisposed && ret != null) ret.Disconnect(false); // ignore exceptions }
private static ImapCommandResult AuthenticateWithAppropriateMechanism(ImapSession session, bool allowPlainTextMechanism, ICredentialsByHost credentials, string username, IEnumerable<string> usingSaslMechanisms) { ImapCommandResult result = null; foreach (var mechanism in usingSaslMechanisms) { if (!allowPlainTextMechanism && SaslClientMechanism.IsMechanismPlainText(mechanism)) // disallow plain text mechanism continue; if (string.Equals(mechanism, SaslMechanisms.Anonymous, StringComparison.OrdinalIgnoreCase)) // disallow 'ANONYMOUS' mechanism continue; var authMechanism = ImapAuthenticationMechanism.GetKnownOrCreate(mechanism); if (session.ServerCapabilities.IsCapable(authMechanism)) { result = session.Authenticate(credentials, username, authMechanism, true); if (result.Succeeded) break; } } if ((result == null || result.Failed) && allowPlainTextMechanism && !session.ServerCapabilities.Has(ImapCapability.LoginDisabled)) result = session.Login(credentials, username, true); return result; }
public void TestGreetingWithNoCapability() { server.EnqueueResponse("* OK ImapSimulatedServer ready\r\n"); using (var session = new ImapSession(host, port)) { Assert.AreEqual(ImapSessionState.NotAuthenticated, session.State); Assert.AreEqual(0, session.ServerCapabilities.Count); Assert.IsFalse(session.ServerCapabilities.Has(ImapCapability.Imap4Rev1)); try { session.ServerCapabilities.Add(ImapCapability.Imap4Rev1); Assert.Fail("NotSupportedException not thrown"); } catch (NotSupportedException) { } } }
public static void Log(ImapSession session, string format, params object[] args) { Info("CID:{0} {1}", session.Id.HasValue ? session.Id.Value.ToString() : "-", args == null ? format : string.Format(format, args)); }
private static ImapCommandResult AuthenticateWithSuppliedMechanism(ImapSession session, ICredentialsByHost credentials, string username, ImapAuthenticationMechanism authMechanism) { /* * http://tools.ietf.org/html/rfc5092 * 3.2. IMAP User Name and Authentication Mechanism * * An authentication mechanism (as used by the IMAP AUTHENTICATE * command) can be expressed by adding ";AUTH=<enc-auth-type>" to the * end of the user name in an IMAP URL. When such an <enc-auth-type> is * indicated, the client SHOULD request appropriate credentials from * that mechanism and use the "AUTHENTICATE" command instead of the * "LOGIN" command. If no user name is specified, one MUST be obtained * from the mechanism or requested from the user/configuration as * appropriate. */ return session.Authenticate(credentials, username, authMechanism, true); }
public void TestGreetingPreAuthWithCapability() { server.EnqueueResponse("* PREAUTH [CAPABILITY IMAP4rev1 AUTH=PLAIN] ImapSimulatedServer ready\r\n"); using (var session = new ImapSession(host, port)) { Assert.AreEqual(ImapSessionState.Authenticated, session.State); Assert.AreEqual(2, session.ServerCapabilities.Count); Assert.AreEqual(new Uri(string.Format("imap://{0}:{1}/", host, port)), session.Authority); Assert.IsTrue(session.ServerCapabilities.Has(ImapCapability.Imap4Rev1)); Assert.IsTrue(session.ServerCapabilities.IsCapable(ImapAuthenticationMechanism.Plain)); try { session.ServerCapabilities.Add(ImapCapability.Imap4Rev1); Assert.Fail("NotSupportedException not thrown"); } catch (NotSupportedException) { } } }
public static ImapCommandResult CreateSession(IImapSessionProfile profile, SaslClientMechanism authMechanismSpecified, UpgradeConnectionStreamCallback createSslStreamCallback, out ImapSession session) { if (profile == null) throw new ArgumentNullException("profile"); var authority = profile.Authority; var securePort = string.Equals(authority.Scheme, ImapUri.UriSchemeImaps, StringComparison.OrdinalIgnoreCase); if (securePort && createSslStreamCallback == null) throw new ArgumentNullException("createSslStreamCallback"); ImapCommandResult result; session = null; session = new ImapSession(authority.Host, authority.Port, true, profile.Timeout, securePort ? createSslStreamCallback : null); session.HandlesIncapableAsException = false; session.HandlesReferralAsException = false; session.TransactionTimeout = profile.Timeout; session.SendTimeout = profile.SendTimeout; session.ReceiveTimeout = profile.ReceiveTimeout; if (session.ServerCapabilities.Count == 0) // try querying server capability (ignore error) session.Capability(); if (!session.ServerCapabilities.Has(ImapCapability.Imap4Rev1)) throw new ImapIncapableException(ImapCapability.Imap4Rev1); if (profile.UseTlsIfAvailable && session.ServerCapabilities.Has(ImapCapability.StartTls) && !session.IsSecureConnection) { var r = session.StartTls(createSslStreamCallback, true); if (r.Failed) throw new ImapSecureConnectionException(r.ResultText); else if (!session.ServerCapabilities.Has(ImapCapability.Imap4Rev1)) throw new ImapIncapableException(ImapCapability.Imap4Rev1); } if (profile.UseDeflateIfAvailable && session.ServerCapabilities.Has(ImapCapability.CompressDeflate)) { var r = session.Compress(ImapCompressionMechanism.Deflate); if (r.Failed) throw new WebException(r.ResultText, null, WebExceptionStatus.RequestCanceled, null); } if (authMechanismSpecified == null) result = Authenticate(session, profile); else result = session.Authenticate(authMechanismSpecified); if (result == null) { throw new ImapAuthenticationException("appropriate authentication mechanism not found"); } else if (result.Failed) { try { try { session.Disconnect(false); } catch (ImapConnectionException) { // ignore } } finally { session = null; } } return result; }
protected void CloseMailbox(ImapSession session) { CloseMailbox(session, "0005"); }
internal void SetSessionInfo(ImapSession session) { Namespaces = session.Namespaces.Clone(); ServerCapabilities = new ImapCapabilityList(true, session.ServerCapabilities); ServerID = session.ServerID; }
public void TestGreetingHomeServerReferralAsError() { server.EnqueueResponse("* BYE [REFERRAL IMAP://user;AUTH=*@SERVER2/] Server not accepting connections. Try SERVER2\r\n"); using (var session = new ImapSession(host, port)) { } }
private void Disconnect(bool logout, bool checkAsyncConnectRunning) { if (checkAsyncConnectRunning) ThrowIfAsyncConnectRunning(); if (session != null) session.Disconnect(logout); session = null; }
public void TestGreetingOkWithNoImapCapability() { server.EnqueueResponse("* OK [CAPABILITY AUTH=PLAIN] ImapSimulatedServer ready\r\n"); using (var session = new ImapSession(host, port)) { Assert.IsTrue(!session.ServerCapabilities.Has(ImapCapability.Imap4Rev1)); session.HandlesIncapableAsException = true; session.Login(credential); } }
public ImapMailboxManager(ImapSession session) { this.session = session; }
public void TestGreetingWithCapability() { server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1 AUTH=CRAM-MD5 AUTH=PLAIN CHILDREN THREAD=REFERENCES X-EXTENSION1 X-EXTENSION2] ImapSimulatedServer ready\r\n"); using (var session = new ImapSession(host, port)) { Assert.AreEqual(ImapSessionState.NotAuthenticated, session.State); Assert.AreEqual(7, session.ServerCapabilities.Count); Assert.IsTrue(session.ServerCapabilities.Has(ImapCapability.Imap4Rev1)); Assert.IsTrue(session.ServerCapabilities.Has(ImapCapability.Children)); Assert.IsTrue(session.ServerCapabilities.IsCapable(ImapAuthenticationMechanism.CRAMMD5)); Assert.IsTrue(session.ServerCapabilities.IsCapable(ImapAuthenticationMechanism.Plain)); Assert.IsTrue(session.ServerCapabilities.Has("THREAD=REFERENCES")); Assert.IsTrue(session.ServerCapabilities.Has("X-EXTENSION1")); Assert.IsTrue(session.ServerCapabilities.Has("X-EXTENSION2")); try { session.ServerCapabilities.Add(ImapCapability.Imap4Rev1); Assert.Fail("NotSupportedException not thrown"); } catch (NotSupportedException) { } } }
public void TestLogoutInNonAuthenticatedState() { server.EnqueueResponse("* OK ImapSimulatedServer ready\r\n"); server.EnqueueResponse("* BYE Logging out\r\n" + "0000 OK logged out.\r\n"); using (var session = new ImapSession(host, port)) { Assert.IsTrue(session.Logout().Code == ImapCommandResultCode.Bye); Assert.AreEqual(ImapSessionState.NotConnected, session.State); Assert.AreEqual(null, session.Authority); } }
private static ImapSession ConnectCore(ConnectParams @params) { ImapSession session = null; try { session = ImapSessionCreator.CreateSession(@params.Profile, @params.AuthMechanism, @params.CreateSslStreamCallback ?? ImapConnection.CreateSslStream); } finally { @params.Profile.SetCredentials(null); } // update server info if (session.ServerCapabilities.Has(ImapCapability.Namespace)) session.Namespace(); if (session.ServerCapabilities.Has(ImapCapability.ID)) session.ID(null); // TODO: client ID session.UpdateSelectedMailboxSizeAndStatus = false; return session; }
internal void RegisterSession(Uri requestUri, ImapSession session) { var authority = ImapStyleUriParser.GetAuthority(requestUri); ImapSession existSession = null; lock ((sessions as System.Collections.ICollection).SyncRoot) { if (sessions.TryGetValue(authority, out existSession)) sessions.Remove(authority); sessions.Add(authority, session); } if (existSession != null) existSession.Disconnect(true); }
private void SetSession(ImapSession session) { this.session = session; }
public void TestConnectBye() { server.EnqueueResponse("* BYE not available.\r\n"); try { using (var session = new ImapSession(host, port)) { Assert.Fail("ImapConnectionException not thrown"); } } catch (ImapConnectionException ex) { Assert.IsNull(ex.InnerException); StringAssert.Contains("not available.", ex.Message); } }
internal void AttachToSession(ImapSession session) { Session = session; (this as IImapUrl).SetBaseUrl(session.AuthorityBuilder); }
public void TestConnectInvalidResponse() { server.EnqueueResponse("+OK pseudo POP server ready.\r\n"); try { using (var session = new ImapSession(host, port)) { Assert.Fail("ImapConnectionException not thrown"); } } catch (ImapConnectionException ex) { Assert.IsNull(ex.InnerException); StringAssert.Contains("unexpected data response", ex.Message); } }