From 8358dec3a5f2740999c1da62863a24601a7352a1 Mon Sep 17 00:00:00 2001 From: Sorunome Date: Fri, 5 Jun 2020 10:56:51 +0200 Subject: [PATCH] analyze and format --- lib/encryption/key_manager.dart | 3 +- test/encryption/key_manager_test.dart | 159 ++++++++++++++------- test/encryption/key_verification_test.dart | 1 - test/encryption/olm_manager_test.dart | 8 +- test/fake_matrix_api.dart | 6 +- 5 files changed, 120 insertions(+), 57 deletions(-) diff --git a/lib/encryption/key_manager.dart b/lib/encryption/key_manager.dart index 8a8ff42b..4121c11f 100644 --- a/lib/encryption/key_manager.dart +++ b/lib/encryption/key_manager.dart @@ -437,7 +437,8 @@ class KeyManager { .deviceKeys[event.content['requesting_device_id']] .ed25519Key; } - setInboundGroupSession(roomId, sessionId, event.encryptedContent['sender_key'], event.content, + setInboundGroupSession(roomId, sessionId, + event.encryptedContent['sender_key'], event.content, forwarded: false); } } diff --git a/test/encryption/key_manager_test.dart b/test/encryption/key_manager_test.dart index 3094332b..7f29edd3 100644 --- a/test/encryption/key_manager_test.dart +++ b/test/encryption/key_manager_test.dart @@ -16,13 +16,11 @@ * along with this program. If not, see . */ -import 'dart:convert'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:test/test.dart'; import 'package:olm/olm.dart' as olm; import '../fake_client.dart'; -import '../fake_matrix_api.dart'; void main() { group('Key Manager', () { @@ -47,22 +45,22 @@ void main() { test('handle new m.room_key', () async { final validSessionId = 'ciM/JWTPrmiWPPZNkRLDPQYf9AW/I46bxyLSr+Bx5oU'; final validSenderKey = 'JBG7ZaPn54OBC7TuIEiylW3BZ+7WcGQhFBPB9pogbAg'; - final sessionKey = 'AgAAAAAQcQ6XrFJk6Prm8FikZDqfry/NbDz8Xw7T6e+/9Yf/q3YHIPEQlzv7IZMNcYb51ifkRzFejVvtphS7wwG2FaXIp4XS2obla14iKISR0X74ugB2vyb1AydIHE/zbBQ1ic5s3kgjMFlWpu/S3FQCnCrv+DPFGEt3ERGWxIl3Bl5X53IjPyVkz65oljz2TZESwz0GH/QFvyOOm8ci0q/gceaF3S7Dmafg3dwTKYwcA5xkcc+BLyrLRzB6Hn+oMAqSNSscnm4mTeT5zYibIhrzqyUTMWr32spFtI9dNR/RFSzfCw'; - + final sessionKey = + 'AgAAAAAQcQ6XrFJk6Prm8FikZDqfry/NbDz8Xw7T6e+/9Yf/q3YHIPEQlzv7IZMNcYb51ifkRzFejVvtphS7wwG2FaXIp4XS2obla14iKISR0X74ugB2vyb1AydIHE/zbBQ1ic5s3kgjMFlWpu/S3FQCnCrv+DPFGEt3ERGWxIl3Bl5X53IjPyVkz65oljz2TZESwz0GH/QFvyOOm8ci0q/gceaF3S7Dmafg3dwTKYwcA5xkcc+BLyrLRzB6Hn+oMAqSNSscnm4mTeT5zYibIhrzqyUTMWr32spFtI9dNR/RFSzfCw'; client.encryption.keyManager.clearInboundGroupSessions(); var event = ToDeviceEvent( - sender: '@alice:example.com', - type: 'm.room_key', - content: { - 'algorithm': 'm.megolm.v1.aes-sha2', - 'room_id': '!726s6s6q:example.com', - 'session_id': validSessionId, - 'session_key': sessionKey, - }, - encryptedContent: { - 'sender_key': validSessionId, - }); + sender: '@alice:example.com', + type: 'm.room_key', + content: { + 'algorithm': 'm.megolm.v1.aes-sha2', + 'room_id': '!726s6s6q:example.com', + 'session_id': validSessionId, + 'session_key': sessionKey, + }, + encryptedContent: { + 'sender_key': validSessionId, + }); await client.encryption.keyManager.handleToDeviceEvent(event); expect( client.encryption.keyManager.getInboundGroupSession( @@ -75,14 +73,14 @@ void main() { // not encrypted client.encryption.keyManager.clearInboundGroupSessions(); event = ToDeviceEvent( - sender: '@alice:example.com', - type: 'm.room_key', - content: { - 'algorithm': 'm.megolm.v1.aes-sha2', - 'room_id': '!726s6s6q:example.com', - 'session_id': validSessionId, - 'session_key': sessionKey, - }); + sender: '@alice:example.com', + type: 'm.room_key', + content: { + 'algorithm': 'm.megolm.v1.aes-sha2', + 'room_id': '!726s6s6q:example.com', + 'session_id': validSessionId, + 'session_key': sessionKey, + }); await client.encryption.keyManager.handleToDeviceEvent(event); expect( client.encryption.keyManager.getInboundGroupSession( @@ -93,42 +91,72 @@ void main() { test('outbound group session', () async { final roomId = '!726s6s6q:example.com'; - expect(client.encryption.keyManager.getOutboundGroupSession(roomId) != null, false); - var sess = await client.encryption.keyManager.createOutboundGroupSession(roomId); - expect(client.encryption.keyManager.getOutboundGroupSession(roomId) != null, true); + expect( + client.encryption.keyManager.getOutboundGroupSession(roomId) != null, + false); + var sess = + await client.encryption.keyManager.createOutboundGroupSession(roomId); + expect( + client.encryption.keyManager.getOutboundGroupSession(roomId) != null, + true); await client.encryption.keyManager.clearOutboundGroupSession(roomId); - expect(client.encryption.keyManager.getOutboundGroupSession(roomId) != null, true); - expect(client.encryption.keyManager.getInboundGroupSession(roomId, sess.outboundGroupSession.session_id(), client.identityKey) != null, true); + expect( + client.encryption.keyManager.getOutboundGroupSession(roomId) != null, + true); + expect( + client.encryption.keyManager.getInboundGroupSession(roomId, + sess.outboundGroupSession.session_id(), client.identityKey) != + null, + true); // rotate after too many messages sess.sentMessages = 300; await client.encryption.keyManager.clearOutboundGroupSession(roomId); - expect(client.encryption.keyManager.getOutboundGroupSession(roomId) != null, false); + expect( + client.encryption.keyManager.getOutboundGroupSession(roomId) != null, + false); // rotate if devices in room change - sess = await client.encryption.keyManager.createOutboundGroupSession(roomId); - client.userDeviceKeys['@alice:example.com'].deviceKeys['JLAFKJWSCS'].blocked = true; + sess = + await client.encryption.keyManager.createOutboundGroupSession(roomId); + client.userDeviceKeys['@alice:example.com'].deviceKeys['JLAFKJWSCS'] + .blocked = true; await client.encryption.keyManager.clearOutboundGroupSession(roomId); - expect(client.encryption.keyManager.getOutboundGroupSession(roomId) != null, false); - client.userDeviceKeys['@alice:example.com'].deviceKeys['JLAFKJWSCS'].blocked = false; + expect( + client.encryption.keyManager.getOutboundGroupSession(roomId) != null, + false); + client.userDeviceKeys['@alice:example.com'].deviceKeys['JLAFKJWSCS'] + .blocked = false; // rotate if too far in the past - sess = await client.encryption.keyManager.createOutboundGroupSession(roomId); + sess = + await client.encryption.keyManager.createOutboundGroupSession(roomId); sess.creationTime = DateTime.now().subtract(Duration(days: 30)); await client.encryption.keyManager.clearOutboundGroupSession(roomId); - expect(client.encryption.keyManager.getOutboundGroupSession(roomId) != null, false); + expect( + client.encryption.keyManager.getOutboundGroupSession(roomId) != null, + false); // force wipe - sess = await client.encryption.keyManager.createOutboundGroupSession(roomId); - await client.encryption.keyManager.clearOutboundGroupSession(roomId, wipe: true); - expect(client.encryption.keyManager.getOutboundGroupSession(roomId) != null, false); + sess = + await client.encryption.keyManager.createOutboundGroupSession(roomId); + await client.encryption.keyManager + .clearOutboundGroupSession(roomId, wipe: true); + expect( + client.encryption.keyManager.getOutboundGroupSession(roomId) != null, + false); // load from database - sess = await client.encryption.keyManager.createOutboundGroupSession(roomId); + sess = + await client.encryption.keyManager.createOutboundGroupSession(roomId); client.encryption.keyManager.clearOutboundGroupSessions(); - expect(client.encryption.keyManager.getOutboundGroupSession(roomId) != null, false); + expect( + client.encryption.keyManager.getOutboundGroupSession(roomId) != null, + false); await client.encryption.keyManager.loadOutboundGroupSession(roomId); - expect(client.encryption.keyManager.getOutboundGroupSession(roomId) != null, true); + expect( + client.encryption.keyManager.getOutboundGroupSession(roomId) != null, + true); }); test('inbound group session', () async { @@ -143,20 +171,49 @@ void main() { 'AgAAAAAQcQ6XrFJk6Prm8FikZDqfry/NbDz8Xw7T6e+/9Yf/q3YHIPEQlzv7IZMNcYb51ifkRzFejVvtphS7wwG2FaXIp4XS2obla14iKISR0X74ugB2vyb1AydIHE/zbBQ1ic5s3kgjMFlWpu/S3FQCnCrv+DPFGEt3ERGWxIl3Bl5X53IjPyVkz65oljz2TZESwz0GH/QFvyOOm8ci0q/gceaF3S7Dmafg3dwTKYwcA5xkcc+BLyrLRzB6Hn+oMAqSNSscnm4mTeT5zYibIhrzqyUTMWr32spFtI9dNR/RFSzfCw' }; client.encryption.keyManager.clearInboundGroupSessions(); - expect(client.encryption.keyManager.getInboundGroupSession(roomId, sessionId, senderKey) != null, false); - client.encryption.keyManager.setInboundGroupSession(roomId, sessionId, senderKey, sessionContent); + expect( + client.encryption.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + false); + client.encryption.keyManager + .setInboundGroupSession(roomId, sessionId, senderKey, sessionContent); await Future.delayed(Duration(milliseconds: 10)); - expect(client.encryption.keyManager.getInboundGroupSession(roomId, sessionId, senderKey) != null, true); + expect( + client.encryption.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + true); - expect(client.encryption.keyManager.getInboundGroupSession(roomId, sessionId, senderKey) != null, true); - expect(client.encryption.keyManager.getInboundGroupSession('otherroom', sessionId, senderKey) != null, true); - expect(client.encryption.keyManager.getInboundGroupSession('otherroom', 'invalid', senderKey) != null, false); + expect( + client.encryption.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + true); + expect( + client.encryption.keyManager + .getInboundGroupSession('otherroom', sessionId, senderKey) != + null, + true); + expect( + client.encryption.keyManager + .getInboundGroupSession('otherroom', 'invalid', senderKey) != + null, + false); client.encryption.keyManager.clearInboundGroupSessions(); - expect(client.encryption.keyManager.getInboundGroupSession(roomId, sessionId, senderKey) != null, false); - await client.encryption.keyManager.loadInboundGroupSession(roomId, sessionId, senderKey); - expect(client.encryption.keyManager.getInboundGroupSession(roomId, sessionId, senderKey) != null, true); - + expect( + client.encryption.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + false); + await client.encryption.keyManager + .loadInboundGroupSession(roomId, sessionId, senderKey); + expect( + client.encryption.keyManager + .getInboundGroupSession(roomId, sessionId, senderKey) != + null, + true); }); }); } diff --git a/test/encryption/key_verification_test.dart b/test/encryption/key_verification_test.dart index 78a24d26..48825f03 100644 --- a/test/encryption/key_verification_test.dart +++ b/test/encryption/key_verification_test.dart @@ -43,7 +43,6 @@ void main() { var updateCounter = 0; KeyVerification keyVerification; - test('setupClient', () async { client = await getClient(); room = Room(id: '!localpart:server.abc', client: client); diff --git a/test/encryption/olm_manager_test.dart b/test/encryption/olm_manager_test.dart index 1274432a..65c10d1b 100644 --- a/test/encryption/olm_manager_test.dart +++ b/test/encryption/olm_manager_test.dart @@ -101,8 +101,12 @@ void main() { test('startOutgoingOlmSessions', () async { // start an olm session.....with ourself! - await client.encryption.olmManager.startOutgoingOlmSessions([client.userDeviceKeys[client.userID].deviceKeys[client.deviceID]]); - expect(client.encryption.olmManager.olmSessions.containsKey(client.identityKey), true); + await client.encryption.olmManager.startOutgoingOlmSessions( + [client.userDeviceKeys[client.userID].deviceKeys[client.deviceID]]); + expect( + client.encryption.olmManager.olmSessions + .containsKey(client.identityKey), + true); }); }); } diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart index 0c5d2565..f21b667c 100644 --- a/test/fake_matrix_api.dart +++ b/test/fake_matrix_api.dart @@ -539,7 +539,8 @@ class FakeMatrixApi extends MockClient { // }, // 'type': 'm.room_key' // }, - { // this is the commented out m.room_key event - only encrypted + { + // this is the commented out m.room_key event - only encrypted 'sender': '@othertest:fakeServer.notExisting', 'content': { 'algorithm': 'm.olm.v1.curve25519-aes-sha2', @@ -547,7 +548,8 @@ class FakeMatrixApi extends MockClient { 'ciphertext': { '7rvl3jORJkBiK4XX1e5TnGnqz068XfYJ0W++Ml63rgk': { 'type': 0, - 'body': 'Awogyh7K4iLUQjcOxIfi7q7LhBBqv9w0mQ6JI9+U9tv7iF4SIHC6xb5YFWf9voRnmDBbd+0vxD/xDlVNRDlPIKliLGkYGiAkEbtlo+fng4ELtO4gSLKVbcFn7tZwZCEUE8H2miBsCCKABgMKIFrKDJwB7gM3lXPt9yVoh6gQksafKt7VFCNRN5KLKqsDEAAi0AX5EfTV7jJ1ZWAbxftjoSN6kCVIxzGclbyg1HjchmNCX7nxNCHWl+q5ZgqHYZVu2n2mCVmIaKD0kvoEZeY3tV1Itb6zf67BLaU0qgW/QzHCHg5a44tNLjucvL2mumHjIG8k0BY2uh+52H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