示例#1
0
		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.");
		}
示例#2
0
		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));
		}