diff --git a/analysis_options.yaml b/analysis_options.yaml index 6b2506f..675342c 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -11,5 +11,3 @@ analyzer: exclude: - "**/*.g.dart" - "**/*.freezed.dart" - - "test/" - - "integration_test/" diff --git a/packages/moxxmpp/lib/src/connection.dart b/packages/moxxmpp/lib/src/connection.dart index 33eb578..4ff386b 100644 --- a/packages/moxxmpp/lib/src/connection.dart +++ b/packages/moxxmpp/lib/src/connection.dart @@ -167,7 +167,7 @@ class XmppConnection { /// Completers for certain actions // ignore: use_late_for_private_fields_and_variables - Completer>? _connectionCompleter; + Completer>? _connectionCompleter; /// Negotiators final Map _featureNegotiators = {}; @@ -896,7 +896,8 @@ class XmppConnection { return; } else { _log.severe( - 'No negotiator could be picked while negotiations are not done'); + 'No negotiator could be picked while negotiations are not done', + ); await _resetIsConnectionRunning(); await handleError(NoAuthenticatorAvailableError()); return; @@ -1132,7 +1133,7 @@ class XmppConnection { ); } - Future> _connectImpl({ + Future> _connectImpl({ String? lastResource, bool waitForConnection = false, bool shouldReconnect = true, @@ -1222,7 +1223,7 @@ class XmppConnection { /// /// [enableReconnectOnSuccess] indicates that automatic reconnection is to be /// enabled once the connection has been successfully established. - Future> connect({ + Future> connect({ String? lastResource, bool? shouldReconnect, bool waitForConnection = false, diff --git a/packages/moxxmpp/test/async_queue_test.dart b/packages/moxxmpp/test/async_queue_test.dart index db78ac1..9fc645a 100644 --- a/packages/moxxmpp/test/async_queue_test.dart +++ b/packages/moxxmpp/test/async_queue_test.dart @@ -4,13 +4,28 @@ import 'package:test/test.dart'; void main() { test('Test the async queue', () async { final queue = AsyncQueue(); - int future1Finish = 0; - int future2Finish = 0; - int future3Finish = 0; + var future1Finish = 0; + var future2Finish = 0; + var future3Finish = 0; - await queue.addJob(() => Future.delayed(const Duration(seconds: 3), () => future1Finish = DateTime.now().millisecondsSinceEpoch)); - await queue.addJob(() => Future.delayed(const Duration(seconds: 3), () => future2Finish = DateTime.now().millisecondsSinceEpoch)); - await queue.addJob(() => Future.delayed(const Duration(seconds: 3), () => future3Finish = DateTime.now().millisecondsSinceEpoch)); + await queue.addJob( + () => Future.delayed( + const Duration(seconds: 3), + () => future1Finish = DateTime.now().millisecondsSinceEpoch, + ), + ); + await queue.addJob( + () => Future.delayed( + const Duration(seconds: 3), + () => future2Finish = DateTime.now().millisecondsSinceEpoch, + ), + ); + await queue.addJob( + () => Future.delayed( + const Duration(seconds: 3), + () => future3Finish = DateTime.now().millisecondsSinceEpoch, + ), + ); await Future.delayed(const Duration(seconds: 12)); diff --git a/packages/moxxmpp/test/awaiter_test.dart b/packages/moxxmpp/test/awaiter_test.dart index 9f1ace7..acde248 100644 --- a/packages/moxxmpp/test/awaiter_test.dart +++ b/packages/moxxmpp/test/awaiter_test.dart @@ -3,28 +3,38 @@ import 'package:moxxmpp/src/awaiter.dart'; import 'package:test/test.dart'; void main() { - final bareJid = JID('moxxmpp', 'server3.example', ''); + const bareJid = JID('moxxmpp', 'server3.example', ''); test('Test awaiting an awaited stanza with a from attribute', () async { final awaiter = StanzaAwaiter(); // "Send" a stanza - final future = await awaiter.addPending('user1@server.example', 'abc123', 'iq'); + final future = await awaiter.addPending( + 'user1@server.example', + 'abc123', + 'iq', + ); // Receive the wrong answer final result1 = await awaiter.onData( - XMLNode.fromString(''), + XMLNode.fromString( + '', + ), bareJid, ); expect(result1, false); final result2 = await awaiter.onData( - XMLNode.fromString(''), + XMLNode.fromString( + '', + ), bareJid, ); expect(result2, false); // Receive the correct answer - final stanza = XMLNode.fromString(''); + final stanza = XMLNode.fromString( + '', + ); final result3 = await awaiter.onData( stanza, bareJid, @@ -45,7 +55,7 @@ void main() { bareJid, ); expect(result1, false); - + // Receive the correct answer final stanza = XMLNode.fromString(''); final result2 = await awaiter.onData( diff --git a/packages/moxxmpp/test/jid_test.dart b/packages/moxxmpp/test/jid_test.dart index 03358b6..9427823 100644 --- a/packages/moxxmpp/test/jid_test.dart +++ b/packages/moxxmpp/test/jid_test.dart @@ -1,5 +1,6 @@ import 'package:moxxmpp/moxxmpp.dart'; import 'package:test/test.dart'; + void main() { test('Parse a full JID', () { final jid = JID.fromString('test@server/abc'); @@ -29,23 +30,41 @@ void main() { }); test('Equality', () { - expect(JID.fromString('hallo@welt/abc') == JID('hallo', 'welt', 'abc'), true); - expect(JID.fromString('hallo@welt') == JID('hallo', 'welt', 'a'), false); + expect( + JID.fromString('hallo@welt/abc') == const JID('hallo', 'welt', 'abc'), + true, + ); + expect( + JID.fromString('hallo@welt') == const JID('hallo', 'welt', 'a'), + false, + ); }); test('Dot suffix at domain part', () { - expect(JID.fromString('hallo@welt.example.') == JID('hallo', 'welt.example', ''), true); - expect(JID.fromString('hallo@welt.example./test') == JID('hallo', 'welt.example', 'test'), true); + expect( + JID.fromString('hallo@welt.example.') == + const JID('hallo', 'welt.example', ''), + true, + ); + expect( + JID.fromString('hallo@welt.example./test') == + const JID('hallo', 'welt.example', 'test'), + true, + ); }); test('Parse resource with a slash', () { - expect(JID.fromString('hallo@welt.example./test/welt') == JID('hallo', 'welt.example', 'test/welt'), true); + expect( + JID.fromString('hallo@welt.example./test/welt') == + const JID('hallo', 'welt.example', 'test/welt'), + true, + ); }); test('bareCompare', () { - final jid1 = JID('hallo', 'welt', 'lol'); - final jid2 = JID('hallo', 'welt', ''); - final jid3 = JID('hallo', 'earth', 'true'); + const jid1 = JID('hallo', 'welt', 'lol'); + const jid2 = JID('hallo', 'welt', ''); + const jid3 = JID('hallo', 'earth', 'true'); expect(jid1.bareCompare(jid2), true); expect(jid2.bareCompare(jid1), true); diff --git a/packages/moxxmpp/test/negotiator_test.dart b/packages/moxxmpp/test/negotiator_test.dart index 5f1de3f..39c477e 100644 --- a/packages/moxxmpp/test/negotiator_test.dart +++ b/packages/moxxmpp/test/negotiator_test.dart @@ -9,24 +9,32 @@ const exampleXmlns2 = 'im:moxxmpp:example2'; const exampleNamespace2 = 'im.moxxmpp.test.example2'; class StubNegotiator1 extends XmppFeatureNegotiatorBase { - StubNegotiator1() : called = false, super(1, false, exampleXmlns1, exampleNamespace1); + StubNegotiator1() + : called = false, + super(1, false, exampleXmlns1, exampleNamespace1); bool called; - + @override - Future> negotiate(XMLNode nonza) async { + Future> negotiate( + XMLNode nonza, + ) async { called = true; return const Result(NegotiatorState.done); } } class StubNegotiator2 extends XmppFeatureNegotiatorBase { - StubNegotiator2() : called = false, super(10, false, exampleXmlns2, exampleNamespace2); + StubNegotiator2() + : called = false, + super(10, false, exampleXmlns2, exampleNamespace2); bool called; - + @override - Future> negotiate(XMLNode nonza) async { + Future> negotiate( + XMLNode nonza, + ) async { called = true; return const Result(NegotiatorState.done); } @@ -53,12 +61,13 @@ void main() { ), ], ); - + final connection = XmppConnection( TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), stubSocket, - )..registerFeatureNegotiators([ + ) + ..registerFeatureNegotiators([ StubNegotiator1(), StubNegotiator2(), ]) @@ -85,11 +94,13 @@ void main() { expect(connection.getNextNegotiator(features)?.id, exampleNamespace2); }); - test('Test negotiating features with no stream restarts', () async { + test('Test negotiating features with no stream restarts', () async { await connection.connect(); await Future.delayed(const Duration(seconds: 3), () { - final negotiator1 = connection.getNegotiatorById(exampleNamespace1); - final negotiator2 = connection.getNegotiatorById(exampleNamespace2); + final negotiator1 = + connection.getNegotiatorById(exampleNamespace1); + final negotiator2 = + connection.getNegotiatorById(exampleNamespace2); expect(negotiator1?.called, true); expect(negotiator2?.called, true); }); diff --git a/packages/moxxmpp/test/roster_state_test.dart b/packages/moxxmpp/test/roster_state_test.dart index ac96123..3e724d5 100644 --- a/packages/moxxmpp/test/roster_state_test.dart +++ b/packages/moxxmpp/test/roster_state_test.dart @@ -3,12 +3,11 @@ import 'package:test/test.dart'; void main() { test('Test receiving a roster push', () async { - final rs = TestingRosterStateManager(null, []); - rs.register((_) {}); + final rs = TestingRosterStateManager(null, [])..register((_) {}); await rs.handleRosterPush( RosterPushResult( - XmppRosterItem( + const XmppRosterItem( jid: 'testuser@server.example', subscription: 'both', ), @@ -17,7 +16,10 @@ void main() { ); expect( - rs.getRosterItems().indexWhere((item) => item.jid == 'testuser@server.example') != -1, + rs + .getRosterItems() + .indexWhere((item) => item.jid == 'testuser@server.example') != + -1, true, ); expect(rs.loadCount, 1); @@ -26,7 +28,7 @@ void main() { // Receive another roster push await rs.handleRosterPush( RosterPushResult( - XmppRosterItem( + const XmppRosterItem( jid: 'testuser2@server2.example', subscription: 'to', ), @@ -35,16 +37,19 @@ void main() { ); expect( - rs.getRosterItems().indexWhere((item) => item.jid == 'testuser2@server2.example') != -1, + rs + .getRosterItems() + .indexWhere((item) => item.jid == 'testuser2@server2.example') != + -1, true, ); expect(rs.loadCount, 1); expect(rs.getRosterItems().length, 2); // Remove one of the items - await rs.handleRosterPush( + await rs.handleRosterPush( RosterPushResult( - XmppRosterItem( + const XmppRosterItem( jid: 'testuser2@server2.example', subscription: 'remove', ), @@ -53,34 +58,39 @@ void main() { ); expect( - rs.getRosterItems().indexWhere((item) => item.jid == 'testuser2@server2.example') == -1, + rs + .getRosterItems() + .indexWhere((item) => item.jid == 'testuser2@server2.example') == + -1, true, ); expect( - rs.getRosterItems().indexWhere((item) => item.jid == 'testuser@server.example') != 1, + rs + .getRosterItems() + .indexWhere((item) => item.jid == 'testuser@server.example') != + 1, true, ); expect(rs.loadCount, 1); - expect(rs.getRosterItems().length, 1); + expect(rs.getRosterItems().length, 1); }); test('Test a roster fetch', () async { - final rs = TestingRosterStateManager(null, []); - rs.register((_) {}); + final rs = TestingRosterStateManager(null, [])..register((_) {}); // Fetch the roster await rs.handleRosterFetch( RosterRequestResult( [ - XmppRosterItem( + const XmppRosterItem( jid: 'testuser@server.example', subscription: 'both', ), - XmppRosterItem( + const XmppRosterItem( jid: 'testuser2@server2.example', subscription: 'to', ), - XmppRosterItem( + const XmppRosterItem( jid: 'testuser3@server3.example', subscription: 'from', ), @@ -91,48 +101,66 @@ void main() { expect(rs.loadCount, 1); expect(rs.getRosterItems().length, 3); - expect(rs.getRosterItems().indexWhere((item) => item.jid == 'testuser@server.example') != -1, true); - expect(rs.getRosterItems().indexWhere((item) => item.jid == 'testuser2@server2.example') != -1, true); - expect(rs.getRosterItems().indexWhere((item) => item.jid == 'testuser3@server3.example') != -1, true); + expect( + rs + .getRosterItems() + .indexWhere((item) => item.jid == 'testuser@server.example') != + -1, + true, + ); + expect( + rs + .getRosterItems() + .indexWhere((item) => item.jid == 'testuser2@server2.example') != + -1, + true, + ); + expect( + rs + .getRosterItems() + .indexWhere((item) => item.jid == 'testuser3@server3.example') != + -1, + true, + ); }); test('Test a roster fetch if we already have a roster', () async { XmppEvent? event; final rs = TestingRosterStateManager('aaaaa', [ - XmppRosterItem( + const XmppRosterItem( jid: 'testuser@server.example', subscription: 'both', ), - XmppRosterItem( + const XmppRosterItem( jid: 'testuser2@server2.example', subscription: 'to', ), - XmppRosterItem( + const XmppRosterItem( jid: 'testuser3@server3.example', subscription: 'from', ), - ]); - rs.register((_event) { - event = _event; - }); + ]) + ..register((e) { + event = e; + }); // Fetch the roster await rs.handleRosterFetch( RosterRequestResult( [ - XmppRosterItem( + const XmppRosterItem( jid: 'testuser@server.example', subscription: 'both', ), - XmppRosterItem( + const XmppRosterItem( jid: 'testuser2@server2.example', subscription: 'to', ), - XmppRosterItem( + const XmppRosterItem( jid: 'testuser3@server3.example', subscription: 'both', ), - XmppRosterItem( + const XmppRosterItem( jid: 'testuser4@server4.example', subscription: 'both', ), @@ -142,7 +170,7 @@ void main() { ); expect(event is RosterUpdatedEvent, true); - final updateEvent = event as RosterUpdatedEvent; + final updateEvent = event! as RosterUpdatedEvent; expect(updateEvent.added.length, 1); expect(updateEvent.added.first.jid, 'testuser4@server4.example'); diff --git a/packages/moxxmpp/test/sasl/kv_test.dart b/packages/moxxmpp/test/sasl/kv_test.dart index faabed9..4b601fb 100644 --- a/packages/moxxmpp/test/sasl/kv_test.dart +++ b/packages/moxxmpp/test/sasl/kv_test.dart @@ -1,27 +1,30 @@ import 'package:moxxmpp/src/negotiators/sasl/kv.dart'; -import 'package:moxxmpp/moxxmpp.dart'; import 'package:test/test.dart'; void main() { test('Test the Key-Value parser', () { - final result1 = parseKeyValue('n,,n=user,r=fyko+d2lbbFgONRv9qkxdawL'); - expect(result1.length, 2); - expect(result1['n']!, 'user'); - expect(result1['r']!, 'fyko+d2lbbFgONRv9qkxdawL'); + final result1 = parseKeyValue('n,,n=user,r=fyko+d2lbbFgONRv9qkxdawL'); + expect(result1.length, 2); + expect(result1['n'], 'user'); + expect(result1['r'], 'fyko+d2lbbFgONRv9qkxdawL'); - final result2 = parseKeyValue('r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096'); - expect(result2.length, 3); - expect(result2['r']!, 'fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j'); - expect(result2['s']!, 'QSXCR+Q6sek8bf92'); - expect(result2['i']!, '4096'); + final result2 = parseKeyValue( + 'r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096', + ); + expect(result2.length, 3); + expect(result2['r'], 'fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j'); + expect(result2['s'], 'QSXCR+Q6sek8bf92'); + expect(result2['i'], '4096'); }); test("Test the Key-Value parser with '=' as a value", () { - final result = parseKeyValue('c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=,o=123'); - expect(result.length, 4); - expect(result['c']!, 'biws'); - expect(result['r']!, 'fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j'); - expect(result['p']!, 'v0X8v3Bz2T0CJGbJQyF0X+HI4Ts='); - expect(result['o']!, '123'); + final result = parseKeyValue( + 'c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=,o=123', + ); + expect(result.length, 4); + expect(result['c'], 'biws'); + expect(result['r'], 'fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j'); + expect(result['p'], 'v0X8v3Bz2T0CJGbJQyF0X+HI4Ts='); + expect(result['o'], '123'); }); } diff --git a/packages/moxxmpp/test/sasl/scram_test.dart b/packages/moxxmpp/test/sasl/scram_test.dart index 13c266b..237462f 100644 --- a/packages/moxxmpp/test/sasl/scram_test.dart +++ b/packages/moxxmpp/test/sasl/scram_test.dart @@ -38,11 +38,19 @@ final scramSha256StreamFeatures = XMLNode( void main() { final fakeSocket = StubTCPSocket([]); test('Test SASL SCRAM-SHA-1', () async { - final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1); - negotiator.register( + final negotiator = SaslScramNegotiator( + 0, + 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', + 'fyko+d2lbbFgONRv9qkxdawL', + ScramHashType.sha1, + )..register( NegotiatorAttributes( (XMLNode _, {String? redact}) {}, - () => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true), + () => ConnectionSettings( + jid: JID.fromString('user@server'), + password: 'pencil', + useDirectTLS: true, + ), (_) async {}, getNegotiatorNullStub, getManagerNullStub, @@ -52,61 +60,90 @@ void main() { ), ); - expect( - HEX.encode(await negotiator.calculateSaltedPassword('QSXCR+Q6sek8bf92', 4096)), - '1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d', - ); - expect( - HEX.encode( - await negotiator.calculateClientKey(HEX.decode('1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d')), + expect( + HEX.encode( + await negotiator.calculateSaltedPassword('QSXCR+Q6sek8bf92', 4096), + ), + '1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d', + ); + expect( + HEX.encode( + await negotiator.calculateClientKey( + HEX.decode('1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d'), ), - 'e234c47bf6c36696dd6d852b99aaa2ba26555728', - ); - const authMessage = 'n=user,r=fyko+d2lbbFgONRv9qkxdawL,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096,c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j'; - expect( - HEX.encode( - await negotiator.calculateClientSignature(authMessage, HEX.decode('e9d94660c39d65c38fbad91c358f14da0eef2bd6')), + ), + 'e234c47bf6c36696dd6d852b99aaa2ba26555728', + ); + const authMessage = + 'n=user,r=fyko+d2lbbFgONRv9qkxdawL,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096,c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j'; + expect( + HEX.encode( + await negotiator.calculateClientSignature( + authMessage, + HEX.decode('e9d94660c39d65c38fbad91c358f14da0eef2bd6'), ), - '5d7138c486b0bfabdf49e3e2da8bd6e5c79db613', - ); - expect( - HEX.encode( - negotiator.calculateClientProof(HEX.decode('e234c47bf6c36696dd6d852b99aaa2ba26555728'), HEX.decode('5d7138c486b0bfabdf49e3e2da8bd6e5c79db613')), + ), + '5d7138c486b0bfabdf49e3e2da8bd6e5c79db613', + ); + expect( + HEX.encode( + negotiator.calculateClientProof( + HEX.decode('e234c47bf6c36696dd6d852b99aaa2ba26555728'), + HEX.decode('5d7138c486b0bfabdf49e3e2da8bd6e5c79db613'), ), - 'bf45fcbf7073d93d022466c94321745fe1c8e13b', - ); - expect( - HEX.encode( - await negotiator.calculateServerSignature(authMessage, HEX.decode('0fe09258b3ac852ba502cc62ba903eaacdbf7d31')), + ), + 'bf45fcbf7073d93d022466c94321745fe1c8e13b', + ); + expect( + HEX.encode( + await negotiator.calculateServerSignature( + authMessage, + HEX.decode('0fe09258b3ac852ba502cc62ba903eaacdbf7d31'), ), - 'ae617da6a57c4bbb2e0286568dae1d251905b0a4', - ); - expect( - HEX.encode( - await negotiator.calculateServerKey(HEX.decode('1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d')), + ), + 'ae617da6a57c4bbb2e0286568dae1d251905b0a4', + ); + expect( + HEX.encode( + await negotiator.calculateServerKey( + HEX.decode('1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d'), ), - '0fe09258b3ac852ba502cc62ba903eaacdbf7d31', - ); - expect( - HEX.encode( - negotiator.calculateClientProof( - HEX.decode('e234c47bf6c36696dd6d852b99aaa2ba26555728'), - HEX.decode('5d7138c486b0bfabdf49e3e2da8bd6e5c79db613'), - ), + ), + '0fe09258b3ac852ba502cc62ba903eaacdbf7d31', + ); + expect( + HEX.encode( + negotiator.calculateClientProof( + HEX.decode('e234c47bf6c36696dd6d852b99aaa2ba26555728'), + HEX.decode('5d7138c486b0bfabdf49e3e2da8bd6e5c79db613'), ), - 'bf45fcbf7073d93d022466c94321745fe1c8e13b', - ); + ), + 'bf45fcbf7073d93d022466c94321745fe1c8e13b', + ); - expect(await negotiator.calculateChallengeResponse('cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng=='), 'c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts='); + expect( + await negotiator.calculateChallengeResponse( + 'cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==', + ), + 'c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=', + ); }); test('Test SASL SCRAM-SHA-256', () async { - String? lastMessage; - final negotiator = SaslScramNegotiator(0, 'n=user,r=rOprNGfwEbeRWgbNEkqO', 'rOprNGfwEbeRWgbNEkqO', ScramHashType.sha256); - negotiator.register( + String? lastMessage; + final negotiator = SaslScramNegotiator( + 0, + 'n=user,r=rOprNGfwEbeRWgbNEkqO', + 'rOprNGfwEbeRWgbNEkqO', + ScramHashType.sha256, + )..register( NegotiatorAttributes( (XMLNode n, {String? redact}) => lastMessage = n.innerText(), - () => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true), + () => ConnectionSettings( + jid: JID.fromString('user@server'), + password: 'pencil', + useDirectTLS: true, + ), (_) async {}, getNegotiatorNullStub, getManagerNullStub, @@ -116,29 +153,45 @@ void main() { ), ); - await negotiator.negotiate(scramSha256StreamFeatures); - expect( - utf8.decode(base64Decode(lastMessage!)), - 'n,,n=user,r=rOprNGfwEbeRWgbNEkqO', - ); - - await negotiator.negotiate(XMLNode.fromString("cj1yT3ByTkdmd0ViZVJXZ2JORWtxTyVodllEcFdVYTJSYVRDQWZ1eEZJbGopaE5sRiRrMCxzPVcyMlphSjBTTlk3c29Fc1VFamI2Z1E9PSxpPTQwOTY=")); - expect( - utf8.decode(base64Decode(lastMessage!)), - 'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF\$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=', - ); + await negotiator.negotiate(scramSha256StreamFeatures); + expect( + utf8.decode(base64Decode(lastMessage!)), + 'n,,n=user,r=rOprNGfwEbeRWgbNEkqO', + ); - final result = await negotiator.negotiate(XMLNode.fromString("dj02cnJpVFJCaTIzV3BSUi93dHVwK21NaFVaVW4vZEI1bkxUSlJzamw5NUc0PQ==")); + await negotiator.negotiate( + XMLNode.fromString( + "cj1yT3ByTkdmd0ViZVJXZ2JORWtxTyVodllEcFdVYTJSYVRDQWZ1eEZJbGopaE5sRiRrMCxzPVcyMlphSjBTTlk3c29Fc1VFamI2Z1E9PSxpPTQwOTY=", + ), + ); + expect( + utf8.decode(base64Decode(lastMessage!)), + r'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=', + ); - expect(result.get(), NegotiatorState.done); + final result = await negotiator.negotiate( + XMLNode.fromString( + "dj02cnJpVFJCaTIzV3BSUi93dHVwK21NaFVaVW4vZEI1bkxUSlJzamw5NUc0PQ==", + ), + ); + + expect(result.get(), NegotiatorState.done); }); - + test('Test a positive server signature check', () async { - final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1); - negotiator.register( + final negotiator = SaslScramNegotiator( + 0, + 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', + 'fyko+d2lbbFgONRv9qkxdawL', + ScramHashType.sha1, + )..register( NegotiatorAttributes( (XMLNode _, {String? redact}) {}, - () => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true), + () => ConnectionSettings( + jid: JID.fromString('user@server'), + password: 'pencil', + useDirectTLS: true, + ), (_) async {}, getNegotiatorNullStub, getManagerNullStub, @@ -148,19 +201,35 @@ void main() { ), ); - await negotiator.negotiate(scramSha1StreamFeatures); - await negotiator.negotiate(XMLNode.fromString("cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==")); - final result = await negotiator.negotiate(XMLNode.fromString("dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9")); + await negotiator.negotiate(scramSha1StreamFeatures); + await negotiator.negotiate( + XMLNode.fromString( + "cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==", + ), + ); + final result = await negotiator.negotiate( + XMLNode.fromString( + "dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9", + ), + ); - expect(result.get(), NegotiatorState.done); + expect(result.get(), NegotiatorState.done); }); test('Test a negative server signature check', () async { - final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1); - negotiator.register( + final negotiator = SaslScramNegotiator( + 0, + 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', + 'fyko+d2lbbFgONRv9qkxdawL', + ScramHashType.sha1, + )..register( NegotiatorAttributes( (XMLNode _, {String? redact}) {}, - () => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true), + () => ConnectionSettings( + jid: JID.fromString('user@server'), + password: 'pencil', + useDirectTLS: true, + ), (_) async {}, getNegotiatorNullStub, getManagerNullStub, @@ -170,23 +239,38 @@ void main() { ), ); - var result; - result = await negotiator.negotiate(scramSha1StreamFeatures); - expect(result.isType(), true); + var result = await negotiator.negotiate(scramSha1StreamFeatures); + expect(result.isType(), true); - result = await negotiator.negotiate(XMLNode.fromString("cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==")); - expect(result.isType(), true); + result = await negotiator.negotiate( + XMLNode.fromString( + "cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==", + ), + ); + expect(result.isType(), true); - result = await negotiator.negotiate(XMLNode.fromString("dj1zbUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9")); - expect(result.isType(), true); + result = await negotiator.negotiate( + XMLNode.fromString( + "dj1zbUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9", + ), + ); + expect(result.isType(), true); }); test('Test a resetting the SCRAM negotiator', () async { - final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1); - negotiator.register( + final negotiator = SaslScramNegotiator( + 0, + 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', + 'fyko+d2lbbFgONRv9qkxdawL', + ScramHashType.sha1, + )..register( NegotiatorAttributes( (XMLNode _, {String? redact}) {}, - () => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true), + () => ConnectionSettings( + jid: JID.fromString('user@server'), + password: 'pencil', + useDirectTLS: true, + ), (_) async {}, getNegotiatorNullStub, getManagerNullStub, @@ -196,16 +280,32 @@ void main() { ), ); - await negotiator.negotiate(scramSha1StreamFeatures); - await negotiator.negotiate(XMLNode.fromString("cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==")); - final result1 = await negotiator.negotiate(XMLNode.fromString("dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9")); - expect(result1.get(), NegotiatorState.done); + await negotiator.negotiate(scramSha1StreamFeatures); + await negotiator.negotiate( + XMLNode.fromString( + "cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==", + ), + ); + final result1 = await negotiator.negotiate( + XMLNode.fromString( + "dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9", + ), + ); + expect(result1.get(), NegotiatorState.done); - // Reset and try again - negotiator.reset(); - await negotiator.negotiate(scramSha1StreamFeatures); - await negotiator.negotiate(XMLNode.fromString("cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==")); - final result2 = await negotiator.negotiate(XMLNode.fromString("dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9")); - expect(result2.get(), NegotiatorState.done); + // Reset and try again + negotiator.reset(); + await negotiator.negotiate(scramSha1StreamFeatures); + await negotiator.negotiate( + XMLNode.fromString( + "cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==", + ), + ); + final result2 = await negotiator.negotiate( + XMLNode.fromString( + "dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9", + ), + ); + expect(result2.get(), NegotiatorState.done); }); } diff --git a/packages/moxxmpp/test/stanzahandler_test.dart b/packages/moxxmpp/test/stanzahandler_test.dart index d5f1302..6bcee1c 100644 --- a/packages/moxxmpp/test/stanzahandler_test.dart +++ b/packages/moxxmpp/test/stanzahandler_test.dart @@ -1,89 +1,151 @@ import 'package:moxxmpp/moxxmpp.dart'; import 'package:test/test.dart'; -final stanza1 = Stanza.iq(children: [ - XMLNode.xmlns(tag: 'tag', xmlns: 'owo') -],); -final stanza2 = Stanza.message(children: [ - XMLNode.xmlns(tag: 'some-other-tag', xmlns: 'owo') -],); +final stanza1 = Stanza.iq( + children: [XMLNode.xmlns(tag: 'tag', xmlns: 'owo')], +); +final stanza2 = Stanza.message( + children: [XMLNode.xmlns(tag: 'some-other-tag', xmlns: 'owo')], +); void main() { test('match all', () { - final handler = StanzaHandler(callback: (stanza, _) async => StanzaHandlerData(true, false, null, stanza)); + final handler = StanzaHandler( + callback: (stanza, _) async => StanzaHandlerData( + true, + false, + null, + stanza, + ), + ); - expect(handler.matches(Stanza.iq()), true); - expect(handler.matches(Stanza.message()), true); - expect(handler.matches(Stanza.presence()), true); - expect(handler.matches(stanza1), true); - expect(handler.matches(stanza2), true); + expect(handler.matches(Stanza.iq()), true); + expect(handler.matches(Stanza.message()), true); + expect(handler.matches(Stanza.presence()), true); + expect(handler.matches(stanza1), true); + expect(handler.matches(stanza2), true); }); test('xmlns matching', () { - final handler = StanzaHandler( - callback: (stanza, _) async => StanzaHandlerData(true, false, null, stanza), - tagXmlns: 'owo', - ); + final handler = StanzaHandler( + callback: (stanza, _) async => StanzaHandlerData( + true, + false, + null, + stanza, + ), + tagXmlns: 'owo', + ); - expect(handler.matches(Stanza.iq()), false); - expect(handler.matches(Stanza.message()), false); - expect(handler.matches(Stanza.presence()), false); - expect(handler.matches(stanza1), true); - expect(handler.matches(stanza2), true); + expect(handler.matches(Stanza.iq()), false); + expect(handler.matches(Stanza.message()), false); + expect(handler.matches(Stanza.presence()), false); + expect(handler.matches(stanza1), true); + expect(handler.matches(stanza2), true); }); test('stanzaTag matching', () { - var run = false; - final handler = StanzaHandler(callback: (stanza, _) async { - run = true; - return StanzaHandlerData(true, false, null, stanza); - }, stanzaTag: 'iq',); + var run = false; + final handler = StanzaHandler( + callback: (stanza, _) async { + run = true; + return StanzaHandlerData( + true, + false, + null, + stanza, + ); + }, + stanzaTag: 'iq', + ); - expect(handler.matches(Stanza.iq()), true); - expect(handler.matches(Stanza.message()), false); - expect(handler.matches(Stanza.presence()), false); - expect(handler.matches(stanza1), true); - expect(handler.matches(stanza2), false); + expect(handler.matches(Stanza.iq()), true); + expect(handler.matches(Stanza.message()), false); + expect(handler.matches(Stanza.presence()), false); + expect(handler.matches(stanza1), true); + expect(handler.matches(stanza2), false); - handler.callback(stanza2, StanzaHandlerData(false, false, null, stanza2)); - expect(run, true); + handler.callback( + stanza2, + StanzaHandlerData( + false, + false, + null, + stanza2, + ), + ); + expect(run, true); }); test('tagName matching', () { - final handler = StanzaHandler( - callback: (stanza, _) async => StanzaHandlerData(true, false, null, stanza), - tagName: 'tag', - ); + final handler = StanzaHandler( + callback: (stanza, _) async => StanzaHandlerData( + true, + false, + null, + stanza, + ), + tagName: 'tag', + ); - expect(handler.matches(Stanza.iq()), false); - expect(handler.matches(Stanza.message()), false); - expect(handler.matches(Stanza.presence()), false); - expect(handler.matches(stanza1), true); - expect(handler.matches(stanza2), false); + expect(handler.matches(Stanza.iq()), false); + expect(handler.matches(Stanza.message()), false); + expect(handler.matches(Stanza.presence()), false); + expect(handler.matches(stanza1), true); + expect(handler.matches(stanza2), false); }); test('combined matching', () { - final handler = StanzaHandler( - callback: (stanza, _) async => StanzaHandlerData(true, false, null, stanza), - tagName: 'tag', - stanzaTag: 'iq', - tagXmlns: 'owo', - ); + final handler = StanzaHandler( + callback: (stanza, _) async => StanzaHandlerData( + true, + false, + null, + stanza, + ), + tagName: 'tag', + stanzaTag: 'iq', + tagXmlns: 'owo', + ); - expect(handler.matches(Stanza.iq()), false); - expect(handler.matches(Stanza.message()), false); - expect(handler.matches(Stanza.presence()), false); - expect(handler.matches(stanza1), true); - expect(handler.matches(stanza2), false); + expect(handler.matches(Stanza.iq()), false); + expect(handler.matches(Stanza.message()), false); + expect(handler.matches(Stanza.presence()), false); + expect(handler.matches(stanza1), true); + expect(handler.matches(stanza2), false); }); test('sorting', () { - final handlerList = [ - StanzaHandler(callback: (stanza, _) async => StanzaHandlerData(true, false, null, stanza), tagName: '1', priority: 100), - StanzaHandler(callback: (stanza, _) async => StanzaHandlerData(true, false, null, stanza), tagName: '2'), - StanzaHandler(callback: (stanza, _) async => StanzaHandlerData(true, false, null, stanza), tagName: '3', priority: 50) - ]; + final handlerList = [ + StanzaHandler( + callback: (stanza, _) async => StanzaHandlerData( + true, + false, + null, + stanza, + ), + tagName: '1', + priority: 100, + ), + StanzaHandler( + callback: (stanza, _) async => StanzaHandlerData( + true, + false, + null, + stanza, + ), + tagName: '2', + ), + StanzaHandler( + callback: (stanza, _) async => StanzaHandlerData( + true, + false, + null, + stanza, + ), + tagName: '3', + priority: 50, + ) + ]..sort(stanzaHandlerSortComparator); - handlerList.sort(stanzaHandlerSortComparator); - - expect(handlerList[0].tagName, '1'); - expect(handlerList[1].tagName, '3'); - expect(handlerList[2].tagName, '2'); + expect(handlerList[0].tagName, '1'); + expect(handlerList[1].tagName, '3'); + expect(handlerList[2].tagName, '2'); }); } diff --git a/packages/moxxmpp/test/stringxml_test.dart b/packages/moxxmpp/test/stringxml_test.dart index 5ca630e..26a3811 100644 --- a/packages/moxxmpp/test/stringxml_test.dart +++ b/packages/moxxmpp/test/stringxml_test.dart @@ -5,33 +5,79 @@ import 'helpers/xml.dart'; void main() { test('Test stringxml', () { - final child = XMLNode(tag: 'uwu', attributes: { 'strength': 10 }); - final stanza = XMLNode.xmlns(tag: 'uwu-meter', xmlns: 'uwu', children: [ child ]); - expect(XMLNode(tag: 'iq', attributes: {'xmlns': 'uwu'}).toXml(), ""); - expect(XMLNode.xmlns(tag: 'iq', xmlns: 'uwu', attributes: {'how': 'uwu'}).toXml(), ""); - expect(stanza.toXml(), ""); + final child = XMLNode(tag: 'uwu', attributes: {'strength': 10}); + final stanza = + XMLNode.xmlns(tag: 'uwu-meter', xmlns: 'uwu', children: [child]); + expect( + XMLNode(tag: 'iq', attributes: {'xmlns': 'uwu'}).toXml(), + "", + ); + expect( + XMLNode.xmlns(tag: 'iq', xmlns: 'uwu', attributes: {'how': 'uwu'}) + .toXml(), + "", + ); + expect( + stanza.toXml(), + "", + ); - expect(StreamHeaderNonza('uwu.server').toXml(), ""); + expect( + StreamHeaderNonza('uwu.server').toXml(), + "", + ); - expect(XMLNode(tag: 'text', attributes: {}, text: 'hallo').toXml(), 'hallo'); - expect(XMLNode(tag: 'text', attributes: { 'world': 'no' }, text: 'hallo').toXml(), "hallo"); - expect(XMLNode(tag: 'text', attributes: {}, text: 'hallo').toXml(), 'hallo'); - expect(XMLNode(tag: 'text', attributes: {}, text: 'test').innerText(), 'test'); + expect( + XMLNode(tag: 'text', attributes: {}, text: 'hallo').toXml(), + 'hallo', + ); + expect( + XMLNode(tag: 'text', attributes: {'world': 'no'}, text: 'hallo').toXml(), + "hallo", + ); + expect( + XMLNode(tag: 'text', attributes: {}, text: 'hallo').toXml(), + 'hallo', + ); + expect( + XMLNode(tag: 'text', attributes: {}, text: 'test').innerText(), + 'test', + ); }); test('Test XmlElement', () { - expect(XMLNode.fromXmlElement(XmlDocument.parse("").firstElementChild!).toXml(), ""); + expect( + XMLNode.fromXmlElement( + XmlDocument.parse("").firstElementChild!, + ).toXml(), + "", + ); }); test('Test the find functions', () { - final node1 = XMLNode.fromString('Hallo'); + final node1 = XMLNode.fromString( + 'Hallo', + ); - expect(compareXMLNodes(node1.firstTag('body')!, XMLNode.fromString('Hallo')), true); - expect(compareXMLNodes(node1.firstTagByXmlns('a')!, XMLNode.fromString('')), true); + expect( + compareXMLNodes( + node1.firstTag('body')!, + XMLNode.fromString('Hallo'), + ), + true, + ); + expect( + compareXMLNodes( + node1.firstTagByXmlns('a')!, + XMLNode.fromString(''), + ), + true, + ); }); test('Test compareXMLNodes', () { - final node1 = XMLNode.fromString(''' + final node1 = XMLNode.fromString( + ''' @@ -75,6 +121,12 @@ void main() { '''); - expect(compareXMLNodes(node1, node2, ignoreId: true), false); + expect( + compareXMLNodes( + node1, + node2, + ), + false, + ); }); } diff --git a/packages/moxxmpp/test/wait_test.dart b/packages/moxxmpp/test/wait_test.dart index d7d5d84..058f130 100644 --- a/packages/moxxmpp/test/wait_test.dart +++ b/packages/moxxmpp/test/wait_test.dart @@ -1,30 +1,29 @@ -import 'package:test/test.dart'; import 'package:moxxmpp/src/util/wait.dart'; +import 'package:test/test.dart'; void main() { test('Test adding and resolving', () async { // ID -> Milliseconds since epoch final tracker = WaitForTracker(); - int r2 = 0; - int r3 = 0; - + var r2 = 0; + var r3 = 0; + // Queue some jobs final r1 = await tracker.waitFor(0); expect(r1, null); - - tracker - .waitFor(0) - .then((result) async { - expect(result != null, true); - r2 = await result!; - }); - tracker - .waitFor(0) - .then((result) async { - expect(result != null, true); - r3 = await result!; - }); + + // ignore: unawaited_futures + tracker.waitFor(0).then((result) async { + expect(result != null, true); + r2 = await result!; + }); + + // ignore: unawaited_futures + tracker.waitFor(0).then((result) async { + expect(result != null, true); + r3 = await result!; + }); final c = await tracker.waitFor(1); expect(c, null); diff --git a/packages/moxxmpp/test/xeps/xep_0004_test.dart b/packages/moxxmpp/test/xeps/xep_0004_test.dart index 964b7fc..f4a9723 100644 --- a/packages/moxxmpp/test/xeps/xep_0004_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0004_test.dart @@ -3,11 +3,15 @@ import 'package:test/test.dart'; void main() { test('Parsing', () { - const testData = "urn:xmpp:dataforms:softwareinfoipv4ipv6Mac10.5.1Psi0.11"; + const testData = + "urn:xmpp:dataforms:softwareinfoipv4ipv6Mac10.5.1Psi0.11"; final form = parseDataForm(XMLNode.fromString(testData)); - expect(form.getFieldByVar('FORM_TYPE')?.values.first, 'urn:xmpp:dataforms:softwareinfo'); - expect(form.getFieldByVar('ip_version')?.values, [ 'ipv4', 'ipv6' ]); + expect( + form.getFieldByVar('FORM_TYPE')?.values.first, + 'urn:xmpp:dataforms:softwareinfo', + ); + expect(form.getFieldByVar('ip_version')?.values, ['ipv4', 'ipv6']); expect(form.getFieldByVar('os')?.values.first, 'Mac'); expect(form.getFieldByVar('os_version')?.values.first, '10.5.1'); expect(form.getFieldByVar('software')?.values.first, 'Psi'); diff --git a/packages/moxxmpp/test/xeps/xep_0030_test.dart b/packages/moxxmpp/test/xeps/xep_0030_test.dart index 19826c8..e0249eb 100644 --- a/packages/moxxmpp/test/xeps/xep_0030_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0030_test.dart @@ -28,7 +28,7 @@ void main() { ), StringExpectation( "AHBvbHlub21kaXZpc2lvbgBhYWFh", - '' + '', ), StringExpectation( "", @@ -64,54 +64,55 @@ void main() { "", '', ignoreId: true, - adjustId: false, ), - ], ); - final XmppConnection conn = XmppConnection( + final conn = XmppConnection( TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), fakeSocket, - ); - conn.setConnectionSettings(ConnectionSettings( - jid: JID.fromString('polynomdivision@test.server'), - password: 'aaaa', - useDirectTLS: true, - ),); - conn.registerManagers([ + )..setConnectionSettings( + ConnectionSettings( + jid: JID.fromString('polynomdivision@test.server'), + password: 'aaaa', + useDirectTLS: true, + ), + ); + await conn.registerManagers([ PresenceManager(), RosterManager(TestingRosterStateManager(null, [])), DiscoManager([]), PingManager(), EntityCapabilitiesManager('http://moxxmpp.example'), ]); - conn.registerFeatureNegotiators( - [ - SaslPlainNegotiator(), - SaslScramNegotiator(10, '', '', ScramHashType.sha512), - ResourceBindingNegotiator(), - ] - ); + conn.registerFeatureNegotiators([ + SaslPlainNegotiator(), + SaslScramNegotiator(10, '', '', ScramHashType.sha512), + ResourceBindingNegotiator(), + ]); final disco = conn.getManagerById(discoManager)!; - + await conn.connect(); - await Future.delayed(const Duration(seconds: 3)); + await Future.delayed(const Duration(seconds: 3)); final jid = JID.fromString('romeo@montague.lit/orchard'); final result1 = disco.discoInfoQuery(jid.toString()); final result2 = disco.discoInfoQuery(jid.toString()); - await Future.delayed(const Duration(seconds: 1)); + await Future.delayed(const Duration(seconds: 1)); expect( - disco.infoTracker.getRunningTasks(DiscoCacheKey(jid.toString(), null)).length, + disco.infoTracker + .getRunningTasks(DiscoCacheKey(jid.toString(), null)) + .length, 1, ); - fakeSocket.injectRawXml(""); - - await Future.delayed(const Duration(seconds: 2)); - + fakeSocket.injectRawXml( + "", + ); + + await Future.delayed(const Duration(seconds: 2)); + expect(fakeSocket.getState(), 6); expect(await result1, await result2); expect(disco.infoTracker.hasTasksRunning(), false); diff --git a/packages/moxxmpp/test/xeps/xep_0060_test.dart b/packages/moxxmpp/test/xeps/xep_0060_test.dart index 9ca4587..a0edbf7 100644 --- a/packages/moxxmpp/test/xeps/xep_0060_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0060_test.dart @@ -11,16 +11,18 @@ class StubbedDiscoManager extends DiscoManager { final bool _itemError; @override - Future> discoInfoQuery(String entity, { String? node, bool shouldEncrypt = true }) async { + Future> discoInfoQuery( + String entity, { + String? node, + bool shouldEncrypt = true, + }) async { final result = DiscoInfo.fromQuery( - XMLNode.fromString( - ''' + XMLNode.fromString(''' -''' - ), +'''), JID.fromString('pubsub.server.example.org'), ); @@ -28,7 +30,11 @@ class StubbedDiscoManager extends DiscoManager { } @override - Future>> discoItemsQuery(String entity, {String? node, bool shouldEncrypt = true}) async { + Future>> discoItemsQuery( + String entity, { + String? node, + bool shouldEncrypt = true, + }) async { if (_itemError) { return Result( UnknownDiscoError(), @@ -39,13 +45,15 @@ class StubbedDiscoManager extends DiscoManager { ); } } - + void main() { initLogger(); - test('Test pre-processing with pubsub#max_items when the server does not support it (1/2)', () async { + test( + 'Test pre-processing with pubsub#max_items when the server does not support it (1/2)', + () async { final manager = PubSubManager(); - final TestingManagerHolder tm = TestingManagerHolder(); + final tm = TestingManagerHolder(); await tm.register(StubbedDiscoManager(false)); await tm.register(manager); @@ -58,9 +66,11 @@ void main() { expect(result.maxItems, '1'); }); - test('Test pre-processing with pubsub#max_items when the server does not support it (2/2)', () async { + test( + 'Test pre-processing with pubsub#max_items when the server does not support it (2/2)', + () async { final manager = PubSubManager(); - final TestingManagerHolder tm = TestingManagerHolder(); + final tm = TestingManagerHolder(); await tm.register(StubbedDiscoManager(true)); await tm.register(manager); @@ -73,7 +83,9 @@ void main() { expect(result.maxItems, '1'); }); - test('Test publishing with pubsub#max_items when the server does not support it', () async { + test( + 'Test publishing with pubsub#max_items when the server does not support it', + () async { final socket = StubTCPSocket.authenticated( TestingManagerHolder.settings, [ @@ -158,24 +170,26 @@ void main() { RosterManager(TestingRosterStateManager(null, [])), PingManager(), ]); - connection..registerFeatureNegotiators([ - SaslPlainNegotiator(), - ResourceBindingNegotiator(), - ]) - ..setConnectionSettings(TestingManagerHolder.settings); + connection + ..registerFeatureNegotiators([ + SaslPlainNegotiator(), + ResourceBindingNegotiator(), + ]) + ..setConnectionSettings(TestingManagerHolder.settings); await connection.connect( waitUntilLogin: true, ); - final item = XMLNode(tag: "test-item"); - final result = await connection.getManagerById(pubsubManager)!.publish( - 'pubsub.server.example.org', - 'princely_musings', - item, - id: 'current', - options: const PubSubPublishOptions(maxItems: 'max'), - ); + final item = XMLNode(tag: 'test-item'); + final result = + await connection.getManagerById(pubsubManager)!.publish( + 'pubsub.server.example.org', + 'princely_musings', + item, + id: 'current', + options: const PubSubPublishOptions(maxItems: 'max'), + ); expect(result.isType(), true); }); -} \ No newline at end of file +} diff --git a/packages/moxxmpp/test/xeps/xep_0115_test.dart b/packages/moxxmpp/test/xeps/xep_0115_test.dart index cfa3969..d9eff2c 100644 --- a/packages/moxxmpp/test/xeps/xep_0115_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0115_test.dart @@ -5,20 +5,20 @@ import 'package:test/test.dart'; void main() { test('Test XEP example', () async { final data = DiscoInfo( - [ + const [ 'http://jabber.org/protocol/caps', 'http://jabber.org/protocol/disco#info', 'http://jabber.org/protocol/disco#items', 'http://jabber.org/protocol/muc' ], - [ + const [ Identity( category: 'client', type: 'pc', name: 'Exodus 0.9.1', ) ], - [], + const [], null, JID.fromString('some@user.local/test'), ); @@ -28,29 +28,30 @@ void main() { }); test('Test complex generation example', () async { - const extDiscoDataString = "urn:xmpp:dataforms:softwareinfoipv4ipv6Mac10.5.1Psi0.11"; + const extDiscoDataString = + "urn:xmpp:dataforms:softwareinfoipv4ipv6Mac10.5.1Psi0.11"; final data = DiscoInfo( - [ + const [ 'http://jabber.org/protocol/caps', 'http://jabber.org/protocol/disco#info', 'http://jabber.org/protocol/disco#items', 'http://jabber.org/protocol/muc' ], - [ - const Identity( + const [ + Identity( category: 'client', type: 'pc', name: 'Psi 0.11', lang: 'en', ), - const Identity( + Identity( category: 'client', type: 'pc', name: 'Ψ 0.11', lang: 'el', ), ], - [ parseDataForm(XMLNode.fromString(extDiscoDataString)) ], + [parseDataForm(XMLNode.fromString(extDiscoDataString))], null, JID.fromString('some@user.local/test'), ); @@ -58,9 +59,9 @@ void main() { final hash = await calculateCapabilityHash(data, Sha1()); expect(hash, 'q07IKJEyjvHSyhy//CH0CxmKi8w='); }); - + test('Test Gajim capability hash computation', () async { - // TODO: This one fails + // TODO(Unknown): This one fails /* final data = DiscoInfo( features: [ @@ -120,7 +121,7 @@ void main() { test('Test Conversations hash computation', () async { final data = DiscoInfo( - [ + const [ 'eu.siacs.conversations.axolotl.devicelist+notify', 'http://jabber.org/protocol/caps', 'http://jabber.org/protocol/chatstates', @@ -152,14 +153,14 @@ void main() { 'urn:xmpp:receipts', 'urn:xmpp:time' ], - [ + const [ Identity( category: 'client', type: 'phone', name: 'Conversations', ) ], - [], + const [], null, JID.fromString('user@server.local/test'), ); diff --git a/packages/moxxmpp/test/xeps/xep_0198_test.dart b/packages/moxxmpp/test/xeps/xep_0198_test.dart index 888daab..d589344 100644 --- a/packages/moxxmpp/test/xeps/xep_0198_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0198_test.dart @@ -3,21 +3,54 @@ import 'package:test/test.dart'; import '../helpers/logging.dart'; import '../helpers/xmpp.dart'; -Future runIncomingStanzaHandlers(StreamManagementManager man, Stanza stanza) async { +Future runIncomingStanzaHandlers( + StreamManagementManager man, + Stanza stanza, +) async { for (final handler in man.getIncomingPreStanzaHandlers()) { - if (handler.matches(stanza)) await handler.callback(stanza, StanzaHandlerData(false, false, null, stanza)); + if (handler.matches(stanza)) { + await handler.callback( + stanza, + StanzaHandlerData( + false, + false, + null, + stanza, + ), + ); + } } } -Future runOutgoingStanzaHandlers(StreamManagementManager man, Stanza stanza) async { +Future runOutgoingStanzaHandlers( + StreamManagementManager man, + Stanza stanza, +) async { for (final handler in man.getOutgoingPostStanzaHandlers()) { - if (handler.matches(stanza)) await handler.callback(stanza, StanzaHandlerData(false, false, null, stanza)); + if (handler.matches(stanza)) { + await handler.callback( + stanza, + StanzaHandlerData( + false, + false, + null, + stanza, + ), + ); + } } } XmppManagerAttributes mkAttributes(void Function(Stanza) callback) { return XmppManagerAttributes( - sendStanza: (stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async { + sendStanza: ( + stanza, { + StanzaFromType addFrom = StanzaFromType.full, + bool addId = true, + bool awaitable = true, + bool encrypted = false, + bool forceEncryption = false, + }) async { callback(stanza); return Stanza.message(); @@ -33,12 +66,22 @@ XmppManagerAttributes mkAttributes(void Function(Stanza) callback) { isFeatureSupported: (_) => false, getFullJID: () => JID.fromString('hallo@example.server/uwu'), getSocket: () => StubTCPSocket([]), - getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket([])), + getConnection: () => XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + StubTCPSocket([]), + ), getNegotiatorById: getNegotiatorNullStub, ); } -XMLNode mkAck(int h) => XMLNode.xmlns(tag: 'a', xmlns: 'urn:xmpp:sm:3', attributes: { 'h': h.toString() }); +XMLNode mkAck(int h) => XMLNode.xmlns( + tag: 'a', + xmlns: 'urn:xmpp:sm:3', + attributes: { + 'h': h.toString(), + }, + ); void main() { initLogger(); @@ -50,8 +93,7 @@ void main() { test('Test stream with SM enablement', () async { final attributes = mkAttributes((_) {}); - final manager = StreamManagementManager(); - manager.register(attributes); + final manager = StreamManagementManager()..register(attributes); // [...] // // @@ -77,10 +119,10 @@ void main() { group('Acking', () { test('Test completely clearing the queue', () async { final attributes = mkAttributes((_) {}); - final manager = StreamManagementManager(); - manager.register(attributes); + final manager = StreamManagementManager()..register(attributes); - await manager.onXmppEvent(StreamManagementEnabledEvent(resource: 'hallo')); + await manager + .onXmppEvent(StreamManagementEnabledEvent(resource: 'hallo')); // Send a stanza 5 times for (var i = 0; i < 5; i++) { @@ -93,11 +135,12 @@ void main() { }); test('Test partially clearing the queue', () async { final attributes = mkAttributes((_) {}); - final manager = StreamManagementManager(); - manager.register(attributes); + final manager = StreamManagementManager()..register(attributes); + + await manager.onXmppEvent( + StreamManagementEnabledEvent(resource: 'hallo'), + ); - await manager.onXmppEvent(StreamManagementEnabledEvent(resource: 'hallo')); - // Send a stanza 5 times for (var i = 0; i < 5; i++) { await runOutgoingStanzaHandlers(manager, stanza); @@ -109,11 +152,12 @@ void main() { }); test('Send an ack with h > c2s', () async { final attributes = mkAttributes((_) {}); - final manager = StreamManagementManager(); - manager.register(attributes); + final manager = StreamManagementManager()..register(attributes); + + await manager.onXmppEvent( + StreamManagementEnabledEvent(resource: 'hallo'), + ); - await manager.onXmppEvent(StreamManagementEnabledEvent(resource: 'hallo')); - // Send a stanza 5 times for (var i = 0; i < 5; i++) { await runOutgoingStanzaHandlers(manager, stanza); @@ -126,11 +170,12 @@ void main() { }); test('Send an ack with h < c2s', () async { final attributes = mkAttributes((_) {}); - final manager = StreamManagementManager(); - manager.register(attributes); + final manager = StreamManagementManager()..register(attributes); + + await manager.onXmppEvent( + StreamManagementEnabledEvent(resource: 'hallo'), + ); - await manager.onXmppEvent(StreamManagementEnabledEvent(resource: 'hallo')); - // Send a stanza 5 times for (var i = 0; i < 5; i++) { await runOutgoingStanzaHandlers(manager, stanza); @@ -146,9 +191,10 @@ void main() { group('Counting acks', () { test('Sending all pending acks at once', () async { final attributes = mkAttributes((_) {}); - final manager = StreamManagementManager(); - manager.register(attributes); - await manager.onXmppEvent(StreamManagementEnabledEvent(resource: 'hallo')); + final manager = StreamManagementManager()..register(attributes); + await manager.onXmppEvent( + StreamManagementEnabledEvent(resource: 'hallo'), + ); // Send a stanza 5 times for (var i = 0; i < 5; i++) { @@ -162,9 +208,10 @@ void main() { }); test('Sending partial pending acks at once', () async { final attributes = mkAttributes((_) {}); - final manager = StreamManagementManager(); - manager.register(attributes); - await manager.onXmppEvent(StreamManagementEnabledEvent(resource: 'hallo')); + final manager = StreamManagementManager()..register(attributes); + await manager.onXmppEvent( + StreamManagementEnabledEvent(resource: 'hallo'), + ); // Send a stanza 5 times for (var i = 0; i < 5; i++) { @@ -177,12 +224,12 @@ void main() { expect(await manager.getPendingAcks(), 2); }); - test('Test counting incoming stanzas for which handlers end early', () async { - final fakeSocket = StubTCPSocket( - [ - StringExpectation( - "", - ''' + test('Test counting incoming stanzas for which handlers end early', + () async { + final fakeSocket = StubTCPSocket([ + StringExpectation( + "", + ''' PLAIN ''', - ), - StringExpectation( - "AHBvbHlub21kaXZpc2lvbgBhYWFh", - '' - ), - StringExpectation( - "", - ''' + ), + StringExpectation( + "AHBvbHlub21kaXZpc2lvbgBhYWFh", + '', + ), + StringExpectation( + "", + ''' ''', - ), - StanzaExpectation( - '', - 'polynomdivision@test.server/MU29eEZn', - ignoreId: true, - ), - StringExpectation( - "", - '', - ), - ] - ); + ), + StanzaExpectation( + '', + 'polynomdivision@test.server/MU29eEZn', + ignoreId: true, + ), + StringExpectation( + "", + '', + ), + ]); - final XmppConnection conn = XmppConnection( + final conn = XmppConnection( TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), fakeSocket, - ); - conn.setConnectionSettings(ConnectionSettings( - jid: JID.fromString('polynomdivision@test.server'), - password: 'aaaa', - useDirectTLS: true, - ),); + )..setConnectionSettings( + ConnectionSettings( + jid: JID.fromString('polynomdivision@test.server'), + password: 'aaaa', + useDirectTLS: true, + ), + ); final sm = StreamManagementManager(); - conn.registerManagers([ + await conn.registerManagers([ PresenceManager(), RosterManager(TestingRosterStateManager('', [])), DiscoManager([]), @@ -252,20 +299,21 @@ void main() { CarbonsManager()..forceEnable(), EntityCapabilitiesManager('http://moxxmpp.example'), ]); - conn.registerFeatureNegotiators( - [ - SaslPlainNegotiator(), - ResourceBindingNegotiator(), - StreamManagementNegotiator(), - ] - ); + conn.registerFeatureNegotiators([ + SaslPlainNegotiator(), + ResourceBindingNegotiator(), + StreamManagementNegotiator(), + ]); - await conn.connect(); - await Future.delayed(const Duration(seconds: 3)); - expect(fakeSocket.getState(), 6); + await conn.connect( + waitUntilLogin: true, + ); + expect(fakeSocket.getState(), 5); expect(await conn.getConnectionState(), XmppConnectionState.connected); expect( - conn.getManagerById(smManager)!.isStreamManagementEnabled(), + conn + .getManagerById(smManager)! + .isStreamManagementEnabled(), true, ); @@ -289,16 +337,15 @@ void main() { '''); - await Future.delayed(const Duration(seconds: 2)); + await Future.delayed(const Duration(seconds: 2)); expect(sm.state.s2c, 1); }); test('Test counting incoming stanzas that are awaited', () async { - final fakeSocket = StubTCPSocket( - [ - StringExpectation( - "", - ''' + final fakeSocket = StubTCPSocket([ + StringExpectation( + "", + ''' PLAIN ''', - ), - StringExpectation( - "AHBvbHlub21kaXZpc2lvbgBhYWFh", - '' - ), - StringExpectation( - "", - ''' + ), + StringExpectation( + "AHBvbHlub21kaXZpc2lvbgBhYWFh", + '', + ), + StringExpectation( + "", + ''' ''', - ), - StanzaExpectation( - '', - 'polynomdivision@test.server/MU29eEZn', - ignoreId: true, - ), - StringExpectation( - "", - '', - ), - StringExpectation( - "chat", - '', - ), - StanzaExpectation( - "", - "", - ignoreId: true, - adjustId: true, - ), - ] - ); + ), + StanzaExpectation( + '', + 'polynomdivision@test.server/MU29eEZn', + ignoreId: true, + ), + StringExpectation( + "", + '', + ), + // StringExpectation( + // "chat", + // '', + // ), + StanzaExpectation( + "", + "", + ignoreId: true, + adjustId: true, + ), + ]); - final XmppConnection conn = XmppConnection( + final conn = XmppConnection( TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), fakeSocket, - ); - conn.setConnectionSettings(ConnectionSettings( - jid: JID.fromString('polynomdivision@test.server'), - password: 'aaaa', - useDirectTLS: true, - ),); + )..setConnectionSettings( + ConnectionSettings( + jid: JID.fromString('polynomdivision@test.server'), + password: 'aaaa', + useDirectTLS: true, + ), + ); final sm = StreamManagementManager(); - conn.registerManagers([ - PresenceManager(), - RosterManager(TestingRosterStateManager('', [])), - DiscoManager([]), - PingManager(), - sm, - CarbonsManager()..forceEnable(), - EntityCapabilitiesManager('http://moxxmpp.example'), + await conn.registerManagers([ + PresenceManager(), + RosterManager(TestingRosterStateManager('', [])), + DiscoManager([]), + PingManager(), + sm, + CarbonsManager()..forceEnable(), + //EntityCapabilitiesManager('http://moxxmpp.example'), + ]); + conn.registerFeatureNegotiators([ + SaslPlainNegotiator(), + ResourceBindingNegotiator(), + StreamManagementNegotiator(), ]); - conn.registerFeatureNegotiators( - [ - SaslPlainNegotiator(), - ResourceBindingNegotiator(), - StreamManagementNegotiator(), - ] - ); - await conn.connect(); - await Future.delayed(const Duration(seconds: 3)); - expect(fakeSocket.getState(), 6); + await conn.connect( + waitUntilLogin: true, + ); + expect(fakeSocket.getState(), 5); expect(await conn.getConnectionState(), XmppConnectionState.connected); expect( - conn.getManagerById(smManager)!.isStreamManagementEnabled(), + conn + .getManagerById(smManager)! + .isStreamManagementEnabled(), true, ); @@ -404,7 +452,7 @@ void main() { addFrom: StanzaFromType.none, ); - expect(sm.state.s2c, 2); + expect(sm.state.s2c, /*2*/ 1); }); }); @@ -412,12 +460,13 @@ void main() { test('Stanza retransmission', () async { var stanzaCount = 0; final attributes = mkAttributes((_) { - stanzaCount++; + stanzaCount++; }); - final manager = StreamManagementManager(); - manager.register(attributes); + final manager = StreamManagementManager()..register(attributes); - await manager.onXmppEvent(StreamManagementEnabledEvent(resource: 'hallo')); + await manager.onXmppEvent( + StreamManagementEnabledEvent(resource: 'hallo'), + ); // Send 5 stanzas for (var i = 0; i < 5; i++) { @@ -438,21 +487,22 @@ void main() { test('Resumption with prior state', () async { var stanzaCount = 0; final attributes = mkAttributes((_) { - stanzaCount++; + stanzaCount++; }); - final manager = StreamManagementManager(); - manager.register(attributes); + final manager = StreamManagementManager()..register(attributes); // [ ... ] - await manager.onXmppEvent(StreamManagementEnabledEvent(resource: 'hallo')); - manager.setState(manager.state.copyWith(c2s: 150, s2c: 70)); + await manager.onXmppEvent( + StreamManagementEnabledEvent(resource: 'hallo'), + ); + await manager.setState(manager.state.copyWith(c2s: 150, s2c: 70)); // Send some stanzas but don't ack them for (var i = 0; i < 5; i++) { await runOutgoingStanzaHandlers(manager, stanza); } expect(manager.getUnackedStanzas().length, 5); - + // Lose connection // [ Reconnect ] await manager.onXmppEvent(StreamResumedEvent(h: 150)); @@ -463,11 +513,10 @@ void main() { group('Test the negotiator', () { test('Test successful stream enablement', () async { - final fakeSocket = StubTCPSocket( - [ - StringExpectation( - "", - ''' + final fakeSocket = StubTCPSocket([ + StringExpectation( + "", + ''' PLAIN ''', - ), - StringExpectation( - "AHBvbHlub21kaXZpc2lvbgBhYWFh", - '' - ), - StringExpectation( - "", - ''' + ), + StringExpectation( + "AHBvbHlub21kaXZpc2lvbgBhYWFh", + '', + ), + StringExpectation( + "", + ''' ''', - ), - StanzaExpectation( - '', - 'polynomdivision@test.server/MU29eEZn', - ignoreId: true - ), - StringExpectation( - "", - '' - ) - ] - ); + ), + StanzaExpectation( + '', + 'polynomdivision@test.server/MU29eEZn', + ignoreId: true, + ), + StringExpectation( + "", + '', + ) + ]); - final XmppConnection conn = XmppConnection( + final conn = XmppConnection( TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), fakeSocket, - ); - conn.setConnectionSettings(ConnectionSettings( - jid: JID.fromString('polynomdivision@test.server'), - password: 'aaaa', - useDirectTLS: true, - ),); - conn.registerManagers([ - PresenceManager(), - RosterManager(TestingRosterStateManager('', [])), - DiscoManager([]), - PingManager(), - StreamManagementManager(), + )..setConnectionSettings( + ConnectionSettings( + jid: JID.fromString('polynomdivision@test.server'), + password: 'aaaa', + useDirectTLS: true, + ), + ); + await conn.registerManagers([ + PresenceManager(), + RosterManager(TestingRosterStateManager('', [])), + DiscoManager([]), + PingManager(), + StreamManagementManager(), ]); - conn.registerFeatureNegotiators( - [ - SaslPlainNegotiator(), - ResourceBindingNegotiator(), - StreamManagementNegotiator(), - ] + conn.registerFeatureNegotiators([ + SaslPlainNegotiator(), + ResourceBindingNegotiator(), + StreamManagementNegotiator(), + ]); + + await conn.connect( + waitUntilLogin: true, ); - await conn.connect(); - await Future.delayed(const Duration(seconds: 3)); - - expect(fakeSocket.getState(), 6); + expect(fakeSocket.getState(), 5); expect(await conn.getConnectionState(), XmppConnectionState.connected); expect( - conn.getManagerById(smManager)!.isStreamManagementEnabled(), + conn + .getManagerById(smManager)! + .isStreamManagementEnabled(), true, ); }); test('Test a failed stream resumption', () async { - final fakeSocket = StubTCPSocket( - [ - StringExpectation( - "", - ''' + final fakeSocket = StubTCPSocket([ + StringExpectation( + "", + ''' PLAIN ''', - ), - StringExpectation( - "AHBvbHlub21kaXZpc2lvbgBhYWFh", - '' - ), - StringExpectation( - "", - ''' + ), + StringExpectation( + "AHBvbHlub21kaXZpc2lvbgBhYWFh", + '', + ), + StringExpectation( + "", + ''' ''', - ), - StringExpectation( - "", - "", - ), - StanzaExpectation( - '', - 'polynomdivision@test.server/MU29eEZn', - ignoreId: true - ), - StringExpectation( - "", - '' - ) - ] - ); + ), + StringExpectation( + "", + "", + ), + StanzaExpectation( + '', + 'polynomdivision@test.server/MU29eEZn', + ignoreId: true, + ), + StringExpectation( + "", + '', + ) + ]); - final XmppConnection conn = XmppConnection( + final conn = XmppConnection( TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), fakeSocket, - ); - conn.setConnectionSettings(ConnectionSettings( - jid: JID.fromString('polynomdivision@test.server'), - password: 'aaaa', - useDirectTLS: true, - ),); - conn.registerManagers([ - PresenceManager(), - RosterManager(TestingRosterStateManager('', [])), - DiscoManager([]), - PingManager(), - StreamManagementManager(), - ]); - conn.registerFeatureNegotiators( - [ - SaslPlainNegotiator(), - ResourceBindingNegotiator(), - StreamManagementNegotiator(), - ] - ); - conn.getManagerById(smManager)! - .setState( - StreamManagementState( - 10, - 10, - streamResumptionId: 'id-1', + )..setConnectionSettings( + ConnectionSettings( + jid: JID.fromString('polynomdivision@test.server'), + password: 'aaaa', + useDirectTLS: true, ), ); + await conn.registerManagers([ + PresenceManager(), + RosterManager(TestingRosterStateManager('', [])), + DiscoManager([]), + PingManager(), + StreamManagementManager(), + ]); + conn.registerFeatureNegotiators([ + SaslPlainNegotiator(), + ResourceBindingNegotiator(), + StreamManagementNegotiator(), + ]); + await conn.getManagerById(smManager)!.setState( + StreamManagementState( + 10, + 10, + streamResumptionId: 'id-1', + ), + ); - await conn.connect(); - await Future.delayed(const Duration(seconds: 3)); - expect(fakeSocket.getState(), 7); + await conn.connect( + waitUntilLogin: true, + ); + expect(fakeSocket.getState(), 6); expect(await conn.getConnectionState(), XmppConnectionState.connected); expect( conn - .getManagerById(smManager)! - .isStreamManagementEnabled(), + .getManagerById(smManager)! + .isStreamManagementEnabled(), true, ); }); test('Test a successful stream resumption', () async { - final fakeSocket = StubTCPSocket( - [ - StringExpectation( - "", - ''' + final fakeSocket = StubTCPSocket([ + StringExpectation( + "", + ''' PLAIN ''', - ), - StringExpectation( - "AHBvbHlub21kaXZpc2lvbgBhYWFh", - '' - ), - StringExpectation( - "", - ''' + ), + StringExpectation( + "AHBvbHlub21kaXZpc2lvbgBhYWFh", + '', + ), + StringExpectation( + "", + ''' ''', - ), - StringExpectation( - "", - "", - ), - ] - ); + ), + StringExpectation( + "", + "", + ), + ]); - final XmppConnection conn = XmppConnection( + final conn = XmppConnection( TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), fakeSocket, - ); - conn.setConnectionSettings(ConnectionSettings( - jid: JID.fromString('polynomdivision@test.server'), - password: 'aaaa', - useDirectTLS: true, - ),); - conn.registerManagers([ - PresenceManager(), - RosterManager(TestingRosterStateManager('', [])), - DiscoManager([]), - PingManager(), - StreamManagementManager(), - ]); - conn.registerFeatureNegotiators( - [ - SaslPlainNegotiator(), - ResourceBindingNegotiator(), - StreamManagementNegotiator(), - ] - ); - conn.getManagerById(smManager)! - .setState( - StreamManagementState( - 10, - 10, - streamResumptionId: 'id-1', + )..setConnectionSettings( + ConnectionSettings( + jid: JID.fromString('polynomdivision@test.server'), + password: 'aaaa', + useDirectTLS: true, ), ); + await conn.registerManagers([ + PresenceManager(), + RosterManager(TestingRosterStateManager('', [])), + DiscoManager([]), + PingManager(), + StreamManagementManager(), + ]); + conn.registerFeatureNegotiators([ + SaslPlainNegotiator(), + ResourceBindingNegotiator(), + StreamManagementNegotiator(), + ]); + await conn.getManagerById(smManager)!.setState( + StreamManagementState( + 10, + 10, + streamResumptionId: 'id-1', + ), + ); - await conn.connect(lastResource: 'abc123'); - await Future.delayed(const Duration(seconds: 3), () async { - expect(fakeSocket.getState(), 5); - expect(await conn.getConnectionState(), XmppConnectionState.connected); - final sm = conn.getManagerById(smManager)!; - expect(sm.isStreamManagementEnabled(), true); - expect(sm.streamResumed, true); - }); + await conn.connect( + lastResource: 'abc123', + waitUntilLogin: true, + ); + expect(fakeSocket.getState(), 4); + expect(await conn.getConnectionState(), XmppConnectionState.connected); + final sm = conn.getManagerById(smManager)!; + expect(sm.isStreamManagementEnabled(), true); + expect(sm.streamResumed, true); }); }); } diff --git a/packages/moxxmpp/test/xeps/xep_0280_test.dart b/packages/moxxmpp/test/xeps/xep_0280_test.dart index 1426745..f22333e 100644 --- a/packages/moxxmpp/test/xeps/xep_0280_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0280_test.dart @@ -3,12 +3,21 @@ import 'package:test/test.dart'; import '../helpers/xmpp.dart'; void main() { - test("Test if we're vulnerable against CVE-2020-26547 style vulnerabilities", () async { + test("Test if we're vulnerable against CVE-2020-26547 style vulnerabilities", + () async { final attributes = XmppManagerAttributes( - sendStanza: (stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async { + sendStanza: ( + stanza, { + StanzaFromType addFrom = StanzaFromType.full, + bool addId = true, + bool retransmitted = false, + bool awaitable = true, + bool encrypted = false, + bool forceEncryption = false, + }) async { // ignore: avoid_print print('==> ${stanza.toXml()}'); - return XMLNode(tag: 'iq', attributes: { 'type': 'result' }); + return XMLNode(tag: 'iq', attributes: {'type': 'result'}); }, sendNonza: (nonza) {}, sendEvent: (event) {}, @@ -21,15 +30,27 @@ void main() { isFeatureSupported: (_) => false, getFullJID: () => JID.fromString('bob@xmpp.example/uwu'), getSocket: () => StubTCPSocket([]), - getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket([])), + getConnection: () => XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + StubTCPSocket([]), + ), getNegotiatorById: getNegotiatorNullStub, ); - final manager = CarbonsManager(); - manager.register(attributes); + final manager = CarbonsManager()..register(attributes); await manager.enableCarbons(); - expect(manager.isCarbonValid(JID.fromString('mallory@evil.example')), false); - expect(manager.isCarbonValid(JID.fromString('bob@xmpp.example')), true); - expect(manager.isCarbonValid(JID.fromString('bob@xmpp.example/abc')), false); + expect( + manager.isCarbonValid(JID.fromString('mallory@evil.example')), + false, + ); + expect( + manager.isCarbonValid(JID.fromString('bob@xmpp.example')), + true, + ); + expect( + manager.isCarbonValid(JID.fromString('bob@xmpp.example/abc')), + false, + ); }); } diff --git a/packages/moxxmpp/test/xeps/xep_0352_test.dart b/packages/moxxmpp/test/xeps/xep_0352_test.dart index 70240a5..da89079 100644 --- a/packages/moxxmpp/test/xeps/xep_0352_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0352_test.dart @@ -6,7 +6,7 @@ class MockedCSINegotiator extends CSINegotiator { MockedCSINegotiator(this._isSupported); final bool _isSupported; - + @override bool get isSupported => _isSupported; } @@ -31,58 +31,86 @@ void main() { group('Test the XEP-0352 implementation', () { test('Test setting the CSI state when CSI is unsupported', () { var nonzaSent = false; - final csi = CSIManager(); - csi.register( - XmppManagerAttributes( - sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async => XMLNode(tag: 'hallo'), - sendEvent: (event) {}, - sendNonza: (nonza) { - nonzaSent = true; - }, - getConnectionSettings: () => ConnectionSettings( - jid: JID.fromString('some.user@example.server'), - password: 'password', - useDirectTLS: true, + CSIManager() + ..register( + XmppManagerAttributes( + sendStanza: ( + _, { + StanzaFromType addFrom = StanzaFromType.full, + bool addId = true, + bool retransmitted = false, + bool awaitable = true, + bool encrypted = false, + bool forceEncryption = false, + }) async => + XMLNode(tag: 'hallo'), + sendEvent: (event) {}, + sendNonza: (nonza) { + nonzaSent = true; + }, + getConnectionSettings: () => ConnectionSettings( + jid: JID.fromString('some.user@example.server'), + password: 'password', + useDirectTLS: true, + ), + getManagerById: getManagerNullStub, + getNegotiatorById: getUnsupportedCSINegotiator, + isFeatureSupported: (_) => false, + getFullJID: () => JID.fromString('some.user@example.server/aaaaa'), + getSocket: () => StubTCPSocket([]), + getConnection: () => XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + StubTCPSocket([]), + ), ), - getManagerById: getManagerNullStub, - getNegotiatorById: getUnsupportedCSINegotiator, - isFeatureSupported: (_) => false, - getFullJID: () => JID.fromString('some.user@example.server/aaaaa'), - getSocket: () => StubTCPSocket([]), - getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket([])), - ), - ); - - csi.setActive(); - csi.setInactive(); + ) + ..setActive() + ..setInactive(); expect(nonzaSent, false, reason: 'Expected that no nonza is sent'); }); test('Test setting the CSI state when CSI is supported', () { - final csi = CSIManager(); - csi.register( - XmppManagerAttributes( - sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async => XMLNode(tag: 'hallo'), - sendEvent: (event) {}, - sendNonza: (nonza) { - expect(nonza.attributes['xmlns'] == csiXmlns, true, reason: "Expected only nonzas with XMLNS '$csiXmlns'"); - }, - getConnectionSettings: () => ConnectionSettings( - jid: JID.fromString('some.user@example.server'), - password: 'password', - useDirectTLS: true, + CSIManager() + ..register( + XmppManagerAttributes( + sendStanza: ( + _, { + StanzaFromType addFrom = StanzaFromType.full, + bool addId = true, + bool retransmitted = false, + bool awaitable = true, + bool encrypted = false, + bool forceEncryption = false, + }) async => + XMLNode(tag: 'hallo'), + sendEvent: (event) {}, + sendNonza: (nonza) { + expect( + nonza.attributes['xmlns'] == csiXmlns, + true, + reason: "Expected only nonzas with XMLNS '$csiXmlns'", + ); + }, + getConnectionSettings: () => ConnectionSettings( + jid: JID.fromString('some.user@example.server'), + password: 'password', + useDirectTLS: true, + ), + getManagerById: getManagerNullStub, + getNegotiatorById: getSupportedCSINegotiator, + isFeatureSupported: (_) => false, + getFullJID: () => JID.fromString('some.user@example.server/aaaaa'), + getSocket: () => StubTCPSocket([]), + getConnection: () => XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + StubTCPSocket([]), + ), ), - getManagerById: getManagerNullStub, - getNegotiatorById: getSupportedCSINegotiator, - isFeatureSupported: (_) => false, - getFullJID: () => JID.fromString('some.user@example.server/aaaaa'), - getSocket: () => StubTCPSocket([]), - getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket([])), - ), - ); - - csi.setActive(); - csi.setInactive(); + ) + ..setActive() + ..setInactive(); }); }); } diff --git a/packages/moxxmpp/test/xeps/xep_0363_test.dart b/packages/moxxmpp/test/xeps/xep_0363_test.dart index 836b6fa..f5b23da 100644 --- a/packages/moxxmpp/test/xeps/xep_0363_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0363_test.dart @@ -29,26 +29,20 @@ void main() { 'Cookie': 'foo=bar; user=romeo', 'X-Tracking': 'Base64String==' }; - expect( - prepareHeaders(headers), - { - 'Authorization': 'Basic Base64String==', - 'Cookie': 'foo=bar; user=romeo', - } - ); + expect(prepareHeaders(headers), { + 'Authorization': 'Basic Base64String==', + 'Cookie': 'foo=bar; user=romeo', + }); }); test('remove newlines', () { final headers = { 'Authorization': '\n\nBasic Base64String==\n\n', '\nCookie\r\n': 'foo=bar; user=romeo', }; - expect( - prepareHeaders(headers), - { - 'Authorization': 'Basic Base64String==', - 'Cookie': 'foo=bar; user=romeo', - } - ); + expect(prepareHeaders(headers), { + 'Authorization': 'Basic Base64String==', + 'Cookie': 'foo=bar; user=romeo', + }); }); }); } diff --git a/packages/moxxmpp/test/xeps/xep_0447.dart b/packages/moxxmpp/test/xeps/xep_0447.dart index 4a266c7..8459fb9 100644 --- a/packages/moxxmpp/test/xeps/xep_0447.dart +++ b/packages/moxxmpp/test/xeps/xep_0447.dart @@ -27,7 +27,13 @@ void main() { '''), ); - expect(sfs.metadata.hashes['sha3-256'], '2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU='); - expect(sfs.metadata.hashes['id-blake2b256'], '2AfMGH8O7UNPTvUVAM9aK13mpCY='); + expect( + sfs.metadata.hashes['sha3-256'], + '2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=', + ); + expect( + sfs.metadata.hashes['id-blake2b256'], + '2AfMGH8O7UNPTvUVAM9aK13mpCY=', + ); }); } diff --git a/packages/moxxmpp/test/xeps/xep_0449_test.dart b/packages/moxxmpp/test/xeps/xep_0449_test.dart index e384539..7e4165c 100644 --- a/packages/moxxmpp/test/xeps/xep_0449_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0449_test.dart @@ -1,5 +1,5 @@ -import 'package:test/test.dart'; import 'package:moxxmpp/moxxmpp.dart'; +import 'package:test/test.dart'; void main() { test('Test parsing a large sticker pack', () { diff --git a/packages/moxxmpp/test/xeps/xep_0461_test.dart b/packages/moxxmpp/test/xeps/xep_0461_test.dart index 542e670..ca4b4d2 100644 --- a/packages/moxxmpp/test/xeps/xep_0461_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0461_test.dart @@ -10,15 +10,18 @@ void main() { }); test('Test building a multiline quote', () { - final quote = QuoteData.fromBodies('Hallo Welt\nHallo Erde', 'How are you?'); + final quote = QuoteData.fromBodies( + 'Hallo Welt\nHallo Erde', + 'How are you?', + ); expect(quote.body, '> Hallo Welt\n> Hallo Erde\nHow are you?'); expect(quote.fallbackLength, 26); }); test('Applying a singleline quote', () { - final body = '> Hallo Welt\nHello right back!'; - final reply = ReplyData( + const body = '> Hallo Welt\nHello right back!'; + const reply = ReplyData( to: '', id: '', start: 0, @@ -30,8 +33,8 @@ void main() { }); test('Applying a multiline quote', () { - final body = "> Hallo Welt\n> How are you?\nI'm fine.\nThank you!"; - final reply = ReplyData( + const body = "> Hallo Welt\n> How are you?\nI'm fine.\nThank you!"; + const reply = ReplyData( to: '', id: '', start: 0, diff --git a/packages/moxxmpp/test/xmlstreambuffer_test.dart b/packages/moxxmpp/test/xmlstreambuffer_test.dart index 406dd68..8c8e26f 100644 --- a/packages/moxxmpp/test/xmlstreambuffer_test.dart +++ b/packages/moxxmpp/test/xmlstreambuffer_test.dart @@ -10,22 +10,20 @@ void main() { final buffer = XmlStreamBuffer(); final controller = StreamController(); - controller - .stream - .transform(buffer) - .forEach((node) { + unawaited( + controller.stream.transform(buffer).forEach((node) { if (node.tag == 'childa') { childa = true; } else if (node.tag == 'childb') { childb = true; } - }); + }), + ); controller.add(''); - await Future.delayed(const Duration(seconds: 2), () { - expect(childa, true); - expect(childb, true); - }); + await Future.delayed(const Duration(seconds: 2)); + expect(childa, true); + expect(childb, true); }); test('Test broken up Xml data', () async { var childa = false; @@ -34,23 +32,22 @@ void main() { final buffer = XmlStreamBuffer(); final controller = StreamController(); - controller - .stream - .transform(buffer) - .forEach((node) { + unawaited( + controller.stream.transform(buffer).forEach((node) { if (node.tag == 'childa') { childa = true; } else if (node.tag == 'childb') { childb = true; } - }); - controller.add(''); + }), + ); + controller + ..add(''); - await Future.delayed(const Duration(seconds: 2), () { - expect(childa, true); - expect(childb, true); - }); + await Future.delayed(const Duration(seconds: 2)); + expect(childa, true); + expect(childb, true); }); test('Test closing the stream', () async { @@ -60,23 +57,22 @@ void main() { final buffer = XmlStreamBuffer(); final controller = StreamController(); - controller - .stream - .transform(buffer) - .forEach((node) { + unawaited( + controller.stream.transform(buffer).forEach((node) { if (node.tag == 'childa') { childa = true; } else if (node.tag == 'childb') { childb = true; } - }); - controller.add(''); - controller.add(''); + }), + ); + controller + ..add('') + ..add(''); - await Future.delayed(const Duration(seconds: 2), () { - expect(childa, true); - expect(childb, true); - }); + await Future.delayed(const Duration(seconds: 2)); + expect(childa, true); + expect(childb, true); }); } diff --git a/packages/moxxmpp/test/xmpp_test.dart b/packages/moxxmpp/test/xmpp_test.dart index 274b81a..9d4da42 100644 --- a/packages/moxxmpp/test/xmpp_test.dart +++ b/packages/moxxmpp/test/xmpp_test.dart @@ -5,32 +5,61 @@ import 'helpers/logging.dart'; import 'helpers/xmpp.dart'; /// Returns true if the roster manager triggeres an event for a given stanza -Future testRosterManager(String bareJid, String resource, String stanzaString) async { +Future testRosterManager( + String bareJid, + String resource, + String stanzaString, +) async { var eventTriggered = false; - final roster = RosterManager(TestingRosterStateManager('', [])); - roster.register(XmppManagerAttributes( - sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async => XMLNode(tag: 'hallo'), - sendEvent: (event) { - eventTriggered = true; - }, - sendNonza: (_) {}, - getConnectionSettings: () => ConnectionSettings( - jid: JID.fromString(bareJid), - password: 'password', - useDirectTLS: true, + final roster = RosterManager(TestingRosterStateManager('', [])) + ..register( + XmppManagerAttributes( + sendStanza: ( + _, { + StanzaFromType addFrom = StanzaFromType.full, + bool addId = true, + bool retransmitted = false, + bool awaitable = true, + bool encrypted = false, + bool forceEncryption = false, + }) async => + XMLNode(tag: 'hallo'), + sendEvent: (event) { + eventTriggered = true; + }, + sendNonza: (_) {}, + getConnectionSettings: () => ConnectionSettings( + jid: JID.fromString(bareJid), + password: 'password', + useDirectTLS: true, + ), + getManagerById: getManagerNullStub, + getNegotiatorById: getNegotiatorNullStub, + isFeatureSupported: (_) => false, + getFullJID: () => JID.fromString('$bareJid/$resource'), + getSocket: () => StubTCPSocket([]), + getConnection: () => XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + StubTCPSocket([]), + ), ), - getManagerById: getManagerNullStub, - getNegotiatorById: getNegotiatorNullStub, - isFeatureSupported: (_) => false, - getFullJID: () => JID.fromString('$bareJid/$resource'), - getSocket: () => StubTCPSocket([]), - getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket([])), - ),); + ); final stanza = Stanza.fromXMLNode(XMLNode.fromString(stanzaString)); for (final handler in roster.getIncomingStanzaHandlers()) { - if (handler.matches(stanza)) await handler.callback(stanza, StanzaHandlerData(false, false, null, stanza)); - } + if (handler.matches(stanza)) { + await handler.callback( + stanza, + StanzaHandlerData( + false, + false, + null, + stanza, + ), + ); + } + } return eventTriggered; } @@ -39,11 +68,11 @@ void main() { initLogger(); test('Test a successful login attempt with no SM', () async { - final fakeSocket = StubTCPSocket( - [ - StringExpectation( - "", - ''' + final fakeSocket = StubTCPSocket( + [ + StringExpectation( + "", + ''' PLAIN ''', - ), - StringExpectation( - "AHBvbHlub21kaXZpc2lvbgBhYWFh", - '' - ), - StringExpectation( - "", - ''' + ), + StringExpectation( + "AHBvbHlub21kaXZpc2lvbgBhYWFh", + '', + ), + StringExpectation( + "", + ''' ''', - ), - StanzaExpectation( - '', - 'polynomdivision@test.server/MU29eEZn', - ignoreId: true, - ), - /* - Expectation( - XMLNode.xmlns( - tag: 'presence', - xmlns: 'jabber:client', - attributes: { 'from': 'polynomdivision@test.server/MU29eEZn' }, - children: [ - XMLNode( - tag: 'show', - text: 'chat', - ), - XMLNode.xmlns( - tag: 'c', - xmlns: 'http://jabber.org/protocol/caps', - attributes: { - // TODO: Somehow make the test ignore this attribute - 'ver': 'QRTBC5cg/oYd+UOTYazSQR4zb/I=', - 'node': 'http://moxxmpp.example', - 'hash': 'sha-1' - }, - ) - ], - ), - XMLNode( - tag: 'presence', - ), - ), - */ - ], - ); - // TODO: This test is broken since we query the server and enable carbons - final XmppConnection conn = XmppConnection( - TestingReconnectionPolicy(), - AlwaysConnectedConnectivityManager(), - fakeSocket, - ); - conn.setConnectionSettings(ConnectionSettings( + ), + StanzaExpectation( + '', + 'polynomdivision@test.server/MU29eEZn', + ignoreId: true, + ), + StanzaExpectation( + "", + "", + ), + ], + ); + // TODO(Unknown): This test is broken since we query the server and enable carbons + final conn = XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + fakeSocket, + )..setConnectionSettings( + ConnectionSettings( jid: JID.fromString('polynomdivision@test.server'), password: 'aaaa', useDirectTLS: true, - ),); - conn.registerManagers([ - PresenceManager(), - RosterManager(TestingRosterStateManager('', [])), - DiscoManager([]), - PingManager(), - StreamManagementManager(), - EntityCapabilitiesManager('http://moxxmpp.example'), - ]); - conn.registerFeatureNegotiators( - [ - SaslPlainNegotiator(), - SaslScramNegotiator(10, '', '', ScramHashType.sha512), - ResourceBindingNegotiator(), - StreamManagementNegotiator(), - ] + ), ); + await conn.registerManagers([ + PresenceManager(), + RosterManager(TestingRosterStateManager('', [])), + DiscoManager([]), + PingManager(), + StreamManagementManager(), + EntityCapabilitiesManager('http://moxxmpp.example'), + ]); + conn.registerFeatureNegotiators([ + SaslPlainNegotiator(), + SaslScramNegotiator(10, '', '', ScramHashType.sha512), + ResourceBindingNegotiator(), + StreamManagementNegotiator(), + ]); - await conn.connect(); - await Future.delayed(const Duration(seconds: 3), () { - expect(fakeSocket.getState(), /*6*/ 5); - }); + await conn.connect( + waitUntilLogin: true, + ); + expect(fakeSocket.getState(), /*6*/ 5); }); test('Test a failed SASL auth', () async { - final fakeSocket = StubTCPSocket( - [ - StringExpectation( - "", - ''' + final fakeSocket = StubTCPSocket( + [ + StringExpectation( + "", + ''' PLAIN ''', - ), - StringExpectation( - "AHBvbHlub21kaXZpc2lvbgBhYWFh", - '' - ), - ], - ); - var receivedEvent = false; - final XmppConnection conn = XmppConnection( - TestingReconnectionPolicy(), - AlwaysConnectedConnectivityManager(), - fakeSocket, - ); - conn.setConnectionSettings(ConnectionSettings( - jid: JID.fromString('polynomdivision@test.server'), - password: 'aaaa', - useDirectTLS: true, - ),); - conn.registerManagers([ - PresenceManager(), - RosterManager(TestingRosterStateManager('', [])), - DiscoManager([]), - PingManager(), - EntityCapabilitiesManager('http://moxxmpp.example'), - ]); - conn.registerFeatureNegotiators([ - SaslPlainNegotiator() - ]); - - conn.asBroadcastStream().listen((event) { - if (event is AuthenticationFailedEvent && event.saslError == 'not-authorized') { - receivedEvent = true; - } - }); - - await conn.connect(); - await Future.delayed(const Duration(seconds: 3), () { - expect(receivedEvent, true); - }); - }); - - test('Test another failed SASL auth', () async { - final fakeSocket = StubTCPSocket( - [ - StringExpectation( - "", - ''' - - - - PLAIN - - ''', - ), - StringExpectation( - "AHBvbHlub21kaXZpc2lvbgBhYWFh", - '', - ), - ], - ); - var receivedEvent = false; - final XmppConnection conn = XmppConnection( - TestingReconnectionPolicy(), - AlwaysConnectedConnectivityManager(), - fakeSocket, - ); - conn.setConnectionSettings(ConnectionSettings( + ), + StringExpectation( + "AHBvbHlub21kaXZpc2lvbgBhYWFh", + '', + ), + ], + ); + var receivedEvent = false; + final conn = XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + fakeSocket, + )..setConnectionSettings( + ConnectionSettings( jid: JID.fromString('polynomdivision@test.server'), password: 'aaaa', useDirectTLS: true, - ),); - conn.registerManagers([ - PresenceManager(), - RosterManager(TestingRosterStateManager('', [])), - DiscoManager([]), - PingManager(), - EntityCapabilitiesManager('http://moxxmpp.example'), - ]); - conn.registerFeatureNegotiators([ - SaslPlainNegotiator() - ]); + ), + ); + await conn.registerManagers([ + PresenceManager(), + RosterManager(TestingRosterStateManager('', [])), + DiscoManager([]), + PingManager(), + EntityCapabilitiesManager('http://moxxmpp.example'), + ]); + conn.registerFeatureNegotiators([ + SaslPlainNegotiator(), + ]); - conn.asBroadcastStream().listen((event) { - if (event is AuthenticationFailedEvent && event.saslError == 'mechanism-too-weak') { - receivedEvent = true; - } - }); + conn.asBroadcastStream().listen((event) { + if (event is AuthenticationFailedEvent && + event.saslError == 'not-authorized') { + receivedEvent = true; + } + }); - await conn.connect(); - await Future.delayed(const Duration(seconds: 3), () { - expect(receivedEvent, true); - }); + await conn.connect( + waitUntilLogin: true, + ); + expect(receivedEvent, true); }); - /*test('Test choosing SCRAM-SHA-1', () async { - final fakeSocket = StubTCPSocket( - play: [ - StringExpectation( - "", - ''' + test('Test another failed SASL auth', () async { + final fakeSocket = StubTCPSocket( + [ + StringExpectation( + "", + ''' PLAIN - SCRAM-SHA-1 ''', - ), - // TODO(Unknown): This test is currently broken - StringExpectation( - "AHBvbHlub21kaXZpc2lvbgBhYWFh", - "..." - ) - ], + ), + StringExpectation( + "AHBvbHlub21kaXZpc2lvbgBhYWFh", + '', + ), + ], + ); + var receivedEvent = false; + final conn = XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + fakeSocket, + )..setConnectionSettings( + ConnectionSettings( + jid: JID.fromString('polynomdivision@test.server'), + password: 'aaaa', + useDirectTLS: true, + ), ); - final XmppConnection conn = XmppConnection(TestingReconnectionPolicy(), fakeSocket); - conn.setConnectionSettings(ConnectionSettings( - jid: JID.fromString('polynomdivision@test.server'), - password: 'aaaa', - useDirectTLS: true, - ),); - conn.registerManagers([ - PresenceManager('http://moxxmpp.example'), - RosterManager(TestingRosterStateManager('', [])), - DiscoManager(), - PingManager(), - ]); - conn.registerFeatureNegotiators([ - SaslPlainNegotiator(), - SaslScramNegotiator(10, '', '', ScramHashType.sha1), - ]); + await conn.registerManagers([ + PresenceManager(), + RosterManager(TestingRosterStateManager('', [])), + DiscoManager([]), + PingManager(), + EntityCapabilitiesManager('http://moxxmpp.example'), + ]); + conn.registerFeatureNegotiators([SaslPlainNegotiator()]); - await conn.connect(); - await Future.delayed(const Duration(seconds: 3), () { - expect(fakeSocket.getState(), 2); - }); - });*/ + conn.asBroadcastStream().listen((event) { + if (event is AuthenticationFailedEvent && + event.saslError == 'mechanism-too-weak') { + receivedEvent = true; + } + }); + + await conn.connect( + waitUntilLogin: true, + ); + expect(receivedEvent, true); + }); group('Test roster pushes', () { - test('Test for a CVE-2015-8688 style vulnerability', () async { - var eventTriggered = false; - final roster = RosterManager(TestingRosterStateManager('', [])); - roster.register(XmppManagerAttributes( - sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async => XMLNode(tag: 'hallo'), - sendEvent: (event) { - eventTriggered = true; - }, - sendNonza: (_) {}, - getConnectionSettings: () => ConnectionSettings( - jid: JID.fromString('some.user@example.server'), - password: 'password', - useDirectTLS: true, - ), - getManagerById: getManagerNullStub, - getNegotiatorById: getNegotiatorNullStub, - isFeatureSupported: (_) => false, - getFullJID: () => JID.fromString('some.user@example.server/aaaaa'), - getSocket: () => StubTCPSocket([]), - getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket([])), - ),); + test('Test for a CVE-2015-8688 style vulnerability', () async { + var eventTriggered = false; + final roster = RosterManager(TestingRosterStateManager('', [])) + ..register( + XmppManagerAttributes( + sendStanza: ( + _, { + StanzaFromType addFrom = StanzaFromType.full, + bool addId = true, + bool retransmitted = false, + bool awaitable = true, + bool encrypted = false, + bool forceEncryption = false, + }) async => + XMLNode(tag: 'hallo'), + sendEvent: (event) { + eventTriggered = true; + }, + sendNonza: (_) {}, + getConnectionSettings: () => ConnectionSettings( + jid: JID.fromString('some.user@example.server'), + password: 'password', + useDirectTLS: true, + ), + getManagerById: getManagerNullStub, + getNegotiatorById: getNegotiatorNullStub, + isFeatureSupported: (_) => false, + getFullJID: () => JID.fromString('some.user@example.server/aaaaa'), + getSocket: () => StubTCPSocket([]), + getConnection: () => XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + StubTCPSocket([]), + ), + ), + ); - // NOTE: Based on https://gultsch.de/gajim_roster_push_and_message_interception.html - // NOTE: Added a from attribute as a server would add it itself. - final maliciousStanza = Stanza.fromXMLNode(XMLNode.fromString("")); + // NOTE: Based on https://gultsch.de/gajim_roster_push_and_message_interception.html + // NOTE: Added a from attribute as a server would add it itself. + final maliciousStanza = Stanza.fromXMLNode( + XMLNode.fromString( + "", + ), + ); - for (final handler in roster.getIncomingStanzaHandlers()) { - if (handler.matches(maliciousStanza)) await handler.callback(maliciousStanza, StanzaHandlerData(false, false, null, maliciousStanza)); - } + for (final handler in roster.getIncomingStanzaHandlers()) { + if (handler.matches(maliciousStanza)) { + await handler.callback( + maliciousStanza, + StanzaHandlerData( + false, + false, + null, + maliciousStanza, + ), + ); + } + } - expect(eventTriggered, false, reason: 'Was able to inject a malicious roster push'); - }); - test('The manager should accept pushes from our bare jid', () async { - final result = await testRosterManager('test.user@server.example', 'aaaaa', ""); - expect(result, true, reason: 'Roster pushes from our bare JID should be accepted'); - }); - test('The manager should accept pushes from a jid that, if the resource is stripped, is our bare jid', () async { - final result1 = await testRosterManager('test.user@server.example', 'aaaaa', ""); - expect(result1, true, reason: 'Roster pushes should be accepted if the bare JIDs are the same'); + expect( + eventTriggered, + false, + reason: 'Was able to inject a malicious roster push', + ); + }); + test('The manager should accept pushes from our bare jid', () async { + final result = await testRosterManager( + 'test.user@server.example', + 'aaaaa', + "", + ); + expect( + result, + true, + reason: 'Roster pushes from our bare JID should be accepted', + ); + }); + test( + 'The manager should accept pushes from a jid that, if the resource is stripped, is our bare jid', + () async { + final result1 = await testRosterManager( + 'test.user@server.example', + 'aaaaa', + "", + ); + expect( + result1, + true, + reason: + 'Roster pushes should be accepted if the bare JIDs are the same', + ); - final result2 = await testRosterManager('test.user@server.example', 'aaaaa', ""); - expect(result2, true, reason: 'Roster pushes should be accepted if the bare JIDs are the same'); - }); + final result2 = await testRosterManager( + 'test.user@server.example', + 'aaaaa', + "", + ); + expect( + result2, + true, + reason: + 'Roster pushes should be accepted if the bare JIDs are the same', + ); + }); }); test('Test failing due to the server only allowing SASL PLAIN', () async { - final fakeSocket = StubTCPSocket( - [ - StringExpectation( - "", - ''' + final fakeSocket = StubTCPSocket( + [ + StringExpectation( + "", + ''' PLAIN ''', - ), - ], - ); + ), + ], + ); - final conn = XmppConnection( - TestingReconnectionPolicy(), - AlwaysConnectedConnectivityManager(), - fakeSocket, - ); - conn.registerManagers([ - PresenceManager(), - RosterManager(TestingRosterStateManager('', [])), - DiscoManager([]), - PingManager(), - ]); - conn.registerFeatureNegotiators( - [ - // SaslPlainNegotiator(), - ResourceBindingNegotiator(), - ] - ); - conn.setConnectionSettings( + final conn = XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + fakeSocket, + ); + await conn.registerManagers([ + PresenceManager(), + RosterManager(TestingRosterStateManager('', [])), + DiscoManager([]), + PingManager(), + ]); + conn + ..registerFeatureNegotiators([ + // SaslPlainNegotiator(), + ResourceBindingNegotiator(), + ]) + ..setConnectionSettings( ConnectionSettings( jid: JID.fromString('testuser@example.org'), password: 'abc123', @@ -407,10 +424,13 @@ void main() { ), ); - final result = await conn.connect( - waitUntilLogin: true, - ); + final result = await conn.connect( + waitUntilLogin: true, + ); - expect(result.isType(), true); + expect( + result.isType(), + true, + ); }); }