static void AssertExampleFromRfc2831(SaslMechanismDigestMd5 sasl, string prefix) { const string serverToken1 = "realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",qop=\"auth\",algorithm=md5-sess,charset=utf-8"; const string expected1 = "username=\"chris\",realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",cnonce=\"OA6MHXh6VqTrRk\",nc=00000001,qop=\"auth\",digest-uri=\"imap/elwood.innosoft.com\",response=d388dad90d4bbd760a152321f2143af7,charset=utf-8,algorithm=md5-sess"; const string serverToken2 = "rspauth=ea40f60335c427b5527b84dbabcdfffd"; const string entropy = "OA6MHXh6VqTrRk"; string challenge, result; byte[] token, decoded; sasl.cnonce = entropy; Assert.IsFalse(sasl.SupportsChannelBinding, "{0}: SupportsChannelBinding", prefix); Assert.IsFalse(sasl.SupportsInitialResponse, "{0}: SupportsInitialResponse", prefix); token = Encoding.ASCII.GetBytes(serverToken1); challenge = sasl.Challenge(Convert.ToBase64String(token)); decoded = Convert.FromBase64String(challenge); result = Encoding.ASCII.GetString(decoded); Assert.AreEqual(expected1, result, "{0}: challenge response does not match the expected string.", prefix); Assert.IsFalse(sasl.IsAuthenticated, "{0}: should not be authenticated yet.", prefix); token = Encoding.ASCII.GetBytes(serverToken2); challenge = sasl.Challenge(Convert.ToBase64String(token)); decoded = Convert.FromBase64String(challenge); result = Encoding.ASCII.GetString(decoded); Assert.AreEqual(string.Empty, result, "{0}: second DIGEST-MD5 challenge should be an empty string.", prefix); Assert.IsTrue(sasl.IsAuthenticated, "{0}: should be authenticated now.", prefix); Assert.IsFalse(sasl.NegotiatedChannelBinding, "{0}: NegotiatedChannelBinding", prefix); Assert.IsFalse(sasl.NegotiatedSecurityLayer, "{0}: NegotiatedSecurityLayer", prefix); Assert.AreEqual(string.Empty, sasl.Challenge(string.Empty), "{0}: challenge while authenticated.", prefix); }
public void TestDigestMd5ExampleFromRfc2831() { const string serverToken1 = "realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",qop=\"auth\",algorithm=md5-sess,charset=utf-8"; const string expected1 = "username=\"chris\",realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",cnonce=\"OA6MHXh6VqTrRk\",nc=00000001,qop=\"auth\",digest-uri=\"imap/elwood.innosoft.com\",response=d388dad90d4bbd760a152321f2143af7,charset=utf-8,algorithm=md5-sess"; const string serverToken2 = "rspauth=ea40f60335c427b5527b84dbabcdfffd"; var credentials = new NetworkCredential("chris", "secret"); var uri = new Uri("imap://elwood.innosoft.com"); var sasl = new SaslMechanismDigestMd5(uri, credentials, "OA6MHXh6VqTrRk"); var token = Encoding.ASCII.GetBytes(serverToken1); var challenge = sasl.Challenge(Convert.ToBase64String(token)); var decoded = Convert.FromBase64String(challenge); var result = Encoding.ASCII.GetString(decoded); Assert.AreEqual(expected1, result, "DIGEST-MD5 challenge response does not match the expected string."); Assert.IsFalse(sasl.IsAuthenticated, "DIGEST-MD5 should not be authenticated yet."); token = Encoding.ASCII.GetBytes(serverToken2); challenge = sasl.Challenge(Convert.ToBase64String(token)); decoded = Convert.FromBase64String(challenge); result = Encoding.ASCII.GetString(decoded); Assert.AreEqual(string.Empty, result, "Second DIGEST-MD5 challenge should be an empty string."); Assert.IsTrue(sasl.IsAuthenticated, "DIGEST-MD5 should be authenticated now."); }
public void TestExampleFromRfc2831() { var credentials = new NetworkCredential("chris", "secret"); var uri = new Uri("imap://elwood.innosoft.com"); var sasl = new SaslMechanismDigestMd5(credentials) { Uri = uri }; AssertExampleFromRfc2831(sasl, "NetworkCredential"); sasl = new SaslMechanismDigestMd5("chris", "secret") { Uri = uri }; AssertExampleFromRfc2831(sasl, "user/pass"); sasl = new SaslMechanismDigestMd5(uri, credentials); AssertExampleFromRfc2831(sasl, "uri/credential"); sasl = new SaslMechanismDigestMd5(uri, "chris", "secret"); AssertExampleFromRfc2831(sasl, "uri/user/pass"); }
public void TestArgumentExceptions() { var credentials = new NetworkCredential("username", "password"); var uri = new Uri("smtp://localhost"); SaslMechanism sasl; Assert.Throws <ArgumentNullException> (() => new SaslException(null, SaslErrorCode.MissingChallenge, "message")); sasl = new SaslMechanismCramMd5(uri, credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(uri, null)); Assert.Throws <NotSupportedException> (() => sasl.Challenge(null)); sasl = new SaslMechanismDigestMd5(uri, credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(uri, null)); Assert.Throws <NotSupportedException> (() => sasl.Challenge(null)); sasl = new SaslMechanismLogin(uri, credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null)); Assert.Throws <NotSupportedException> (() => sasl.Challenge(null)); sasl = new SaslMechanismNtlm(uri, credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(uri, null)); Assert.DoesNotThrow(() => sasl.Challenge(null)); sasl = new SaslMechanismOAuth2(uri, credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(uri, null)); Assert.DoesNotThrow(() => sasl.Challenge(null)); sasl = new SaslMechanismPlain(uri, credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null)); Assert.DoesNotThrow(() => sasl.Challenge(null)); sasl = new SaslMechanismScramSha1(uri, credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(uri, null)); Assert.DoesNotThrow(() => sasl.Challenge(null)); sasl = new SaslMechanismScramSha256(uri, credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(uri, null)); Assert.DoesNotThrow(() => sasl.Challenge(null)); }
public void TestSaslExceptions() { const string serverToken1 = "realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",qop=\"auth\",algorithm=md5-sess,charset=utf-8,cipher=\"des,3des,rc4\""; const string expected1 = "username=\"chris\",realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",cnonce=\"OA6MHXh6VqTrRk\",nc=00000001,qop=\"auth\",digest-uri=\"imap/elwood.innosoft.com\",response=d388dad90d4bbd760a152321f2143af7,charset=utf-8,algorithm=md5-sess"; const string serverToken2 = "rspauth=ea40f60335c427b5527b84dbabcdfffd"; const string entropy = "OA6MHXh6VqTrRk"; string challenge, result; byte[] token, decoded; var uri = new Uri("imap://elwood.innosoft.com"); var sasl = new SaslMechanismDigestMd5("chris", "secret") { Uri = uri, cnonce = entropy }; AssertSaslException(sasl, serverToken1 + ",xyz=" + new string ('x', 2048), SaslErrorCode.ChallengeTooLong); AssertSaslException(sasl, "username = \"domain\\chris", SaslErrorCode.InvalidChallenge); // incomplete quoted value AssertSaslException(sasl, serverToken1 + ",xyz", SaslErrorCode.InvalidChallenge); // missing '=' AssertSaslException(sasl, serverToken1 + ",xyz=", SaslErrorCode.InvalidChallenge); // incomplete value AssertSaslException(sasl, serverToken1 + ",maxbuf=xyz", SaslErrorCode.InvalidChallenge); // invalid maxbuf value AssertSaslException(sasl, serverToken1 + ",nonce=\"OA6MG9tEQGm2hh\"", SaslErrorCode.InvalidChallenge); // multiple nonce AssertSaslException(sasl, serverToken1 + ",stale=false,stale=true", SaslErrorCode.InvalidChallenge); // multiple stale AssertSaslException(sasl, serverToken1 + ",maxbuf=1024,maxbuf=512", SaslErrorCode.InvalidChallenge); // multiple maxbuf AssertSaslException(sasl, serverToken1 + ",charset=iso-8859-1", SaslErrorCode.InvalidChallenge); // multiple charset AssertSaslException(sasl, serverToken1 + ",algorithm=md5-sess", SaslErrorCode.InvalidChallenge); // multiple algorithm AssertSaslException(sasl, serverToken1 + ",cipher=\"des,3des\"", SaslErrorCode.InvalidChallenge); // multiple ciphers token = Encoding.ASCII.GetBytes(serverToken1); challenge = sasl.Challenge(Convert.ToBase64String(token)); decoded = Convert.FromBase64String(challenge); result = Encoding.ASCII.GetString(decoded); Assert.AreEqual(expected1, result, "challenge response does not match the expected string."); Assert.IsFalse(sasl.IsAuthenticated, "should not be authenticated yet."); AssertSaslException(sasl, string.Empty, SaslErrorCode.MissingChallenge); AssertSaslException(sasl, "rspauth", SaslErrorCode.IncompleteChallenge); AssertSaslException(sasl, "maxbuf=123", SaslErrorCode.InvalidChallenge); AssertSaslException(sasl, "rspauth=fffffffffffffffffffffffffffffff", SaslErrorCode.IncorrectHash); token = Encoding.ASCII.GetBytes(serverToken2); challenge = sasl.Challenge(Convert.ToBase64String(token)); decoded = Convert.FromBase64String(challenge); result = Encoding.ASCII.GetString(decoded); Assert.AreEqual(string.Empty, result, "second DIGEST-MD5 challenge should be an empty string."); Assert.IsTrue(sasl.IsAuthenticated, "should be authenticated now."); Assert.AreEqual(string.Empty, sasl.Challenge(string.Empty), "challenge while authenticated."); }
public void TestArgumentExceptions() { var credentials = new NetworkCredential("username", "password"); var uri = new Uri("smtp://localhost"); var sasl = new SaslMechanismDigestMd5(credentials) { Uri = uri }; Assert.Throws <NotSupportedException> (() => sasl.Challenge(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5("username", null)); }
static void AssertSaslException(SaslMechanismDigestMd5 sasl, string challenge, SaslErrorCode code) { var token = Encoding.ASCII.GetBytes(challenge); try { sasl.Challenge(Convert.ToBase64String(token)); } catch (SaslException sex) { Assert.AreEqual(code, sex.ErrorCode, "ErrorCode"); return; } catch (Exception ex) { Assert.Fail("SaslException expected, but got: {0}", ex.GetType().Name); return; } Assert.Fail("SaslException expected."); }
public void TestArgumentExceptions() { var credentials = new NetworkCredential("username", "password"); var uri = new Uri("smtp://localhost"); SaslMechanism sasl; Assert.Throws <ArgumentNullException> (() => new SaslException(null, SaslErrorCode.MissingChallenge, "message")); sasl = new SaslMechanismCramMd5(credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(uri, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(uri, null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(uri, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5("username", null)); Assert.Throws <NotSupportedException> (() => sasl.Challenge(null)); sasl = new SaslMechanismDigestMd5(credentials) { Uri = uri }; Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(uri, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(uri, (string)null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(uri, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5("username", null)); Assert.Throws <NotSupportedException> (() => sasl.Challenge(null)); sasl = new SaslMechanismLogin(credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, Encoding.UTF8, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, Encoding.UTF8, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, Encoding.UTF8, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, Encoding.UTF8, null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, Encoding.UTF8, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(Encoding.UTF8, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Encoding)null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(Encoding.UTF8, null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(Encoding.UTF8, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin("username", null)); Assert.Throws <NotSupportedException> (() => sasl.Challenge(null)); sasl = new SaslMechanismNtlm(credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm((Uri)null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(uri, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm((Uri)null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(uri, (string)null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(uri, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm("username", null)); Assert.DoesNotThrow(() => sasl.Challenge(null)); sasl = new SaslMechanismOAuth2(credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2((Uri)null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(uri, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2((Uri)null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(uri, (string)null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(uri, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2("username", null)); Assert.DoesNotThrow(() => sasl.Challenge(null)); sasl = new SaslMechanismPlain(credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain((Uri)null, Encoding.UTF8, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, Encoding.UTF8, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain((Uri)null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain((Uri)null, Encoding.UTF8, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, Encoding.UTF8, null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, Encoding.UTF8, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain((Uri)null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(Encoding.UTF8, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain((Encoding)null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(Encoding.UTF8, null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(Encoding.UTF8, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain("username", null)); Assert.DoesNotThrow(() => sasl.Challenge(null)); sasl = new SaslMechanismScramSha1(credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(uri, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(uri, (string)null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(uri, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1((string)null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1("username", null)); Assert.DoesNotThrow(() => sasl.Challenge(null)); sasl = new SaslMechanismScramSha256(credentials); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(null, credentials)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(uri, null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(null, "username", "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(uri, (string)null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(uri, "username", null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256((string)null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256("username", null)); Assert.DoesNotThrow(() => sasl.Challenge(null)); Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create(null, uri, Encoding.UTF8, credentials)); Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create("PLAIN", null, Encoding.UTF8, credentials)); Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create("PLAIN", uri, null, credentials)); Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create("PLAIN", uri, Encoding.UTF8, null)); Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create(null, uri, credentials)); Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create("PLAIN", null, credentials)); Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create("PLAIN", uri, null)); Assert.Throws <ArgumentNullException> (() => SaslMechanism.SaslPrep(null)); }