From c66bf9bd38a752aa7c3245e2363d42469d6eef4b Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Fri, 15 Jul 2022 21:43:00 +0200 Subject: [PATCH] test: Fix SCRAM test --- test/sasl/scram.dart | 73 +++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/test/sasl/scram.dart b/test/sasl/scram.dart index 21b835b4..428fb83f 100644 --- a/test/sasl/scram.dart +++ b/test/sasl/scram.dart @@ -1,20 +1,37 @@ import "package:moxxyv2/xmpp/settings.dart"; import "package:moxxyv2/xmpp/jid.dart"; import "package:moxxyv2/xmpp/stringxml.dart"; -import "package:moxxyv2/xmpp/sasl/scram.dart"; -import "package:moxxyv2/xmpp/sasl/authenticator.dart"; +import "package:moxxyv2/xmpp/namespaces.dart"; +import "package:moxxyv2/xmpp/negotiators/negotiator.dart"; +import "package:moxxyv2/xmpp/negotiators/sasl/scram.dart"; import "package:test/test.dart"; import "package:hex/hex.dart"; +final scramSha1StreamFeatures = XMLNode( + tag: "stream:features", + children: [ + XMLNode.xmlns( + tag: "mechanisms", + xmlns: saslXmlns, + children: [ + XMLNode( + tag: "mechanism", + text: "SCRAM-SHA1" + ) + ] + ) + ] +); + void main() { test("Test SASL SCRAM-SHA-1", () async { - final negotiator = SaslScramNegotiator( - settings: ConnectionSettings(jid: JID.fromString("user@server"), password: "pencil", useDirectTLS: true, allowPlainAuth: true), - clientNonce: "fyko+d2lbbFgONRv9qkxdawL", - initialMessageNoGS2: "n=user,r=fyko+d2lbbFgONRv9qkxdawL", - sendRawXML: (data, { String? redact }) {}, - hashType: ScramHashType.sha1 + final negotiator = SaslScramNegotiator(0, "n=user,r=fyko+d2lbbFgONRv9qkxdawL", "fyko+d2lbbFgONRv9qkxdawL", ScramHashType.sha1); + negotiator.register( + NegotiatorAttributes( + (XMLNode _) {}, + () => ConnectionSettings(jid: JID.fromString("user@server"), password: "pencil", useDirectTLS: true, allowPlainAuth: true) + ) ); expect( @@ -66,33 +83,33 @@ void main() { }); test("Test a positive server signature check", () async { - final negotiator = SaslScramNegotiator( - settings: ConnectionSettings(jid: JID.fromString("user@server"), password: "pencil", useDirectTLS: true, allowPlainAuth: true), - clientNonce: "fyko+d2lbbFgONRv9qkxdawL", - initialMessageNoGS2: "n,,n=user,r=fyko+d2lbbFgONRv9qkxdawL", - sendRawXML: (data, { String? redact }) {}, - hashType: ScramHashType.sha1 + final negotiator = SaslScramNegotiator(0, "n=user,r=fyko+d2lbbFgONRv9qkxdawL", "fyko+d2lbbFgONRv9qkxdawL", ScramHashType.sha1); + negotiator.register( + NegotiatorAttributes( + (XMLNode _) {}, + () => ConnectionSettings(jid: JID.fromString("user@server"), password: "pencil", useDirectTLS: true, allowPlainAuth: true) + ) ); - await negotiator.next(null); - await negotiator.next(XMLNode.fromString("cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==")); - final result = await negotiator.next(XMLNode.fromString("dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9")); + await negotiator.negotiate(scramSha1StreamFeatures); + await negotiator.negotiate(XMLNode.fromString("cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==")); + await negotiator.negotiate(XMLNode.fromString("dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9")); - expect(result.getState(), AuthenticationResult.success, reason: result.getValue()); + expect(negotiator.state, NegotiatorState.done); }); test("Test a negative server signature check", () async { - final negotiator = SaslScramNegotiator( - settings: ConnectionSettings(jid: JID.fromString("user@server"), password: "pencil", useDirectTLS: true, allowPlainAuth: true), - clientNonce: "fyko+d2lbbFgONRv9qkxdawL", - initialMessageNoGS2: "n,,n=user,r=fyko+d2lbbFgONRv9qkxdawL", - sendRawXML: (data, { String? redact }) {}, - hashType: ScramHashType.sha1 + final negotiator = SaslScramNegotiator(0, "n=user,r=fyko+d2lbbFgONRv9qkxdawL", "fyko+d2lbbFgONRv9qkxdawL", ScramHashType.sha1); + negotiator.register( + NegotiatorAttributes( + (XMLNode _) {}, + () => ConnectionSettings(jid: JID.fromString("user@server"), password: "pencil", useDirectTLS: true, allowPlainAuth: true) + ) ); - await negotiator.next(null); - await negotiator.next(XMLNode.fromString("cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==")); - final result = await negotiator.next(XMLNode.fromString("dj1zbUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9")); + await negotiator.negotiate(scramSha1StreamFeatures); + await negotiator.negotiate(XMLNode.fromString("cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==")); + await negotiator.negotiate(XMLNode.fromString("dj1zbUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9")); - expect(result.getState(), AuthenticationResult.failure, reason: result.getValue()); + expect(negotiator.state, NegotiatorState.error); }); }