static void AssertScramSha1PlusTlsUnique(SaslMechanismScramSha1Plus sasl, string prefix)
        {
            const string expected   = "c=cD10bHMtdW5pcXVlLCxpbWFwOi8vZWx3b29kLmlubm9zb2Z0LmNvbS8=,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=91nyQ+7jn+YxGsblvCxpfKUnxwk=";
            const string challenge1 = "r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096";
            const string challenge2 = "v=7QQfRcpsgEz8G8pmK+vmYMfLmBU=";
            const string entropy    = "fyko+d2lbbFgONRv9qkxdawL";
            string       token;

            sasl.cnonce = entropy;

            Assert.IsTrue(sasl.SupportsChannelBinding, "{0}: SupportsChannelBinding", prefix);
            Assert.IsTrue(sasl.SupportsInitialResponse, "{0}: SupportsInitialResponse", prefix);

            var challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(null)));

            Assert.AreEqual("p=tls-unique,,n=user,r=" + entropy, challenge, "{0}: initial SCRAM-SHA-1-PLUS 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-PLUS 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-PLUS challenge should be an empty string.", prefix);
            Assert.IsTrue(sasl.IsAuthenticated, "{0}: SCRAM-SHA-1-PLUS should be authenticated now.", prefix);
            Assert.IsTrue(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);
        }
        static void AssertScramSha1PlusTlsServerEndpoint(SaslMechanismScramSha1Plus sasl, string prefix)
        {
            const string expected   = "c=cD10bHMtc2VydmVyLWVuZC1wb2ludCwsaW1hcDovL2Vsd29vZC5pbm5vc29mdC5jb20v,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=TJiKTaOm8umanp3qriQ/tSiJ3iY=";
            const string challenge1 = "r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096";
            const string challenge2 = "v=4FOxt1+Pv761Owg9JJCCJE5ogoU=";
            const string entropy    = "fyko+d2lbbFgONRv9qkxdawL";
            string       token;

            sasl.cnonce = entropy;

            Assert.IsTrue(sasl.SupportsChannelBinding, "{0}: SupportsChannelBinding", prefix);
            Assert.IsTrue(sasl.SupportsInitialResponse, "{0}: SupportsInitialResponse", prefix);

            var challenge = Encoding.UTF8.GetString(Convert.FromBase64String(sasl.Challenge(null)));

            Assert.AreEqual("p=tls-server-end-point,,n=user,r=" + entropy, challenge, "{0}: initial SCRAM-SHA-1-PLUS 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-PLUS 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-PLUS challenge should be an empty string.", prefix);
            Assert.IsTrue(sasl.IsAuthenticated, "{0}: SCRAM-SHA-1-PLUS should be authenticated now.", prefix);
            Assert.IsTrue(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 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));
        }