public void TestScramSha1() { const string cnonce = "fyko+d2lbbFgONRv9qkxdawL"; var uri = new Uri("imap://elwood.innosoft.com"); var credentials = new NetworkCredential("user", "pencil"); var sasl = new SaslMechanismScramSha1(uri, 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-1 challenge response does not match the expected string."); Assert.IsFalse(sasl.IsAuthenticated, "SCRAM-SHA-1 should not be authenticated yet."); token = Convert.ToBase64String(Encoding.UTF8.GetBytes("r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096")); challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(token))); const string expected = "c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts="; Assert.AreEqual(expected, challenge, "Second SCRAM-SHA-1 challenge response does not match the expected string."); Assert.IsFalse(sasl.IsAuthenticated, "SCRAM-SHA-1 should not be authenticated yet."); token = Convert.ToBase64String(Encoding.UTF8.GetBytes("v=rmF9pqV8S7suAoZWja4dJRkFsKQ=")); challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(token))); Assert.AreEqual(string.Empty, challenge, "Third SCRAM-SHA-1 challenge should be an empty string."); Assert.IsTrue(sasl.IsAuthenticated, "SCRAM-SHA-1 should be authenticated now."); }
static void AssertScramSha1 (SaslMechanismScramSha1 sasl, string prefix) { const string expected = "c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts="; const string challenge1 = "r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096"; const string challenge2 = "v=rmF9pqV8S7suAoZWja4dJRkFsKQ="; const string entropy = "fyko+d2lbbFgONRv9qkxdawL"; 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-1 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-1 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-1 challenge should be an empty string.", prefix); Assert.IsTrue (sasl.IsAuthenticated, "{0}: SCRAM-SHA-1 should be authenticated now.", prefix); Assert.AreEqual (string.Empty, sasl.Challenge (string.Empty)); }
public void TestScramSha1() { var credentials = new NetworkCredential("user", "pencil"); var sasl = new SaslMechanismScramSha1(credentials); var uri = new Uri("imap://elwood.innosoft.com"); AssertScramSha1(sasl, "NetworkCredential"); sasl = new SaslMechanismScramSha1("user", "pencil"); AssertScramSha1(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 (SaslMechanismScramSha1 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 SaslMechanismScramSha1 (credentials); Assert.DoesNotThrow (() => sasl.Challenge (null)); 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)); }
public void TestArgumentExceptions() { var credentials = new NetworkCredential("username", "password"); var sasl = new SaslMechanismScramSha1(credentials); Assert.DoesNotThrow(() => sasl.Challenge(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1("username", null)); sasl = new SaslMechanismScramSha1Plus(credentials); Assert.DoesNotThrow(() => sasl.Challenge(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1Plus(null)); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1Plus(null, "password")); Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1Plus("username", null)); }
public void TestSaslExceptions() { const string nonce = "r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j"; const string salt = "s=QSXCR+Q6sek8bf92"; const string iterations = "i=4096"; const string expected = "c=biws," + nonce + ",p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts="; const string challenge1 = nonce + "," + salt + "," + iterations; const string challenge2 = "v=rmF9pqV8S7suAoZWja4dJRkFsKQ="; const string entropy = "fyko+d2lbbFgONRv9qkxdawL"; var sasl = new SaslMechanismScramSha1("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-1 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-1 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=rmF9pqV8S7suAoZWja4dJRkF", SaslErrorCode.IncorrectHash); // incorrect hash length AssertSaslException(sasl, "v=AAAAAAAAAAAAAAAAAAAAAAAAAAA=", 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-1 challenge should be an empty string."); Assert.IsTrue(sasl.IsAuthenticated, "SCRAM-SHA-1 should be authenticated now."); Assert.AreEqual(string.Empty, 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)); }