static void AssertScramSha256(SaslMechanismScramSha256 sasl, string prefix) { const string expected = "c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ="; const string challenge1 = "r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096"; const string challenge2 = "v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4="; const string entropy = "rOprNGfwEbeRWgbNEkqO"; string token; sasl.cnonce = entropy; Assert.IsTrue(sasl.SupportsInitialResponse, "{0}: SupportsInitialResponse", prefix); var challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(null))); Assert.AreEqual("n,,n=user,r=" + entropy, challenge, "{0}: initial SCRAM-SHA-256 challenge response does not match the expected string.", prefix); Assert.IsFalse(sasl.IsAuthenticated, "{0}: should not be authenticated yet.", prefix); token = Convert.ToBase64String(Encoding.UTF8.GetBytes(challenge1)); challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(token))); Assert.AreEqual(expected, challenge, "{0}: second SCRAM-SHA-256 challenge response does not match the expected string.", prefix); Assert.IsFalse(sasl.IsAuthenticated, "{0}: should not be authenticated yet.", prefix); token = Convert.ToBase64String(Encoding.UTF8.GetBytes(challenge2)); challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(token))); Assert.AreEqual(string.Empty, challenge, "{0}: third SCRAM-SHA-256 challenge should be an empty string.", prefix); Assert.IsTrue(sasl.IsAuthenticated, "{0}: SCRAM-SHA-256 should be authenticated now.", prefix); Assert.Throws <InvalidOperationException> (() => sasl.Challenge(string.Empty)); }
public void TestScramSha256() { const string cnonce = "rOprNGfwEbeRWgbNEkqO"; var uri = new Uri("imap://elwood.innosoft.com"); var credentials = new NetworkCredential("user", "pencil"); var sasl = new SaslMechanismScramSha256(credentials, cnonce); string token; var challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(null))); Assert.AreEqual("n,,n=user,r=" + cnonce, challenge, "Initial SCRAM-SHA-256 challenge response does not match the expected string."); Assert.IsFalse(sasl.IsAuthenticated, "SCRAM-SHA-256 should not be authenticated yet."); token = Convert.ToBase64String(Encoding.UTF8.GetBytes("r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096")); challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(token))); const string expected = "c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ="; Assert.AreEqual(expected, challenge, "Second SCRAM-SHA-256 challenge response does not match the expected string."); Assert.IsFalse(sasl.IsAuthenticated, "SCRAM-SHA-256 should not be authenticated yet."); token = Convert.ToBase64String(Encoding.UTF8.GetBytes("v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=")); challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(token))); Assert.AreEqual(string.Empty, challenge, "Third SCRAM-SHA-256 challenge should be an empty string."); Assert.IsTrue(sasl.IsAuthenticated, "SCRAM-SHA-256 should be authenticated now."); }
public void TestScramSha256() { var credentials = new NetworkCredential("user", "pencil"); var sasl = new SaslMechanismScramSha256(credentials); var uri = new Uri("imap://elwood.innosoft.com"); AssertScramSha256(sasl, "NetworkCredential"); sasl = new SaslMechanismScramSha256("user", "pencil"); AssertScramSha256(sasl, "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)); }
static void AssertSaslException(SaslMechanismScramSha256 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"); var sasl = new SaslMechanismScramSha256(credentials); Assert.DoesNotThrow(() => sasl.Challenge(null)); 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)); }
public void TestArgumentExceptions() { var credentials = new NetworkCredential("username", "password"); var sasl = new SaslMechanismScramSha256(credentials); Assert.DoesNotThrow(() => sasl.Challenge(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256("username", null)); sasl = new SaslMechanismScramSha256Plus(credentials); Assert.DoesNotThrow(() => sasl.Challenge(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256Plus(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256Plus(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256Plus("username", null)); }
public void TestSaslExceptions() { const string nonce = "r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0"; const string salt = "s=W22ZaJ0SNY7soEsUEjb6gQ=="; const string iterations = "i=4096"; const string expected = "c=biws," + nonce + ",p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ="; const string challenge1 = nonce + "," + salt + "," + iterations; const string challenge2 = "v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4="; const string entropy = "rOprNGfwEbeRWgbNEkqO"; var sasl = new SaslMechanismScramSha256("user", "pencil") { cnonce = entropy }; string challenge, token; challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(null))); Assert.AreEqual("n,,n=user,r=" + entropy, challenge, "initial SCRAM-SHA-256 challenge response does not match the expected string."); Assert.IsFalse(sasl.IsAuthenticated, "should not be authenticated yet."); AssertSaslException(sasl, challenge1.Replace(salt + ",", string.Empty), SaslErrorCode.IncompleteChallenge); // missing salt AssertSaslException(sasl, challenge1.Replace(nonce + ",", string.Empty), SaslErrorCode.IncompleteChallenge); // missing nonce AssertSaslException(sasl, challenge1.Replace("," + iterations, string.Empty), SaslErrorCode.IncompleteChallenge); // missing iterations AssertSaslException(sasl, challenge1.Replace(nonce, "r=asfhajksfhkafhakhafk"), SaslErrorCode.InvalidChallenge); // invalid nonce AssertSaslException(sasl, challenge1.Replace(iterations, "i=abcd"), SaslErrorCode.InvalidChallenge); // invalid iterations token = Convert.ToBase64String(Encoding.UTF8.GetBytes(challenge1)); challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(token))); Assert.AreEqual(expected, challenge, "second SCRAM-SHA-256 challenge response does not match the expected string."); Assert.IsFalse(sasl.IsAuthenticated, "should not be authenticated yet."); AssertSaslException(sasl, "x=abcdefg", SaslErrorCode.InvalidChallenge); AssertSaslException(sasl, "v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl9", SaslErrorCode.IncorrectHash); // incorrect hash length AssertSaslException(sasl, "v=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", SaslErrorCode.IncorrectHash); // incorrect hash token = Convert.ToBase64String(Encoding.UTF8.GetBytes(challenge2)); challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(token))); Assert.AreEqual(string.Empty, challenge, "third SCRAM-SHA-256 challenge should be an empty string."); Assert.IsTrue(sasl.IsAuthenticated, "SCRAM-SHA-256 should be authenticated now."); Assert.Throws <InvalidOperationException> (() => sasl.Challenge(string.Empty)); }
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)); }