Skip to content

Commit

Permalink
fix: Hopefully fix all tests being flaky
Browse files Browse the repository at this point in the history
It seems that the varint encoding function would not work for
some integers as input. This should in theory fix this issue. Since
the SPK IDs are randomly between 0 and 2**32 - 1, it makes sense that
the tests fail only sometimes.
  • Loading branch information
PapaTutuWawa committed Sep 14, 2022
1 parent 79704da commit 438012d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
18 changes: 10 additions & 8 deletions lib/src/omemo/sessionmanager.dart
Expand Up @@ -177,15 +177,17 @@ class OmemoSessionManager {
// Pick the correct SPK
final device = await getDevice();
OmemoKeyPair? spk;
if (kex.spkId == device.spkId) {
spk = device.spk;
} else if (kex.spkId == device.oldSpkId) {
spk = device.oldSpk;
} else {
await _lock.synchronized(() async {
if (kex.spkId == _device.spkId) {
spk = _device.spk;
} else if (kex.spkId == _device.oldSpkId) {
spk = _device.oldSpk;
}
});
if (spk == null) {
throw UnknownSignedPrekeyException();
}
assert(spk != null, 'The used SPK must be found');
final kexResult = await x3dhFromInitialMessage(
X3DHMessage(
Expand All @@ -198,7 +200,7 @@ class OmemoSessionManager {
device.ik,
);
final ratchet = await OmemoDoubleRatchet.acceptNewSession(
spk,
spk!,
OmemoPublicKey.fromBytes(kex.ik!, KeyPairType.ed25519),
kexResult.sk,
kexResult.ad,
Expand Down
11 changes: 4 additions & 7 deletions lib/src/protobuf/protobuf.dart
Expand Up @@ -46,21 +46,18 @@ List<int> encodeVarint(int i) {
assert(i >= 0, "Two's complement is not implemented");
final ret = List<int>.empty(growable: true);

var j = 0;
while (true) {
// Thanks to https://github.com/hathibelagal-dev/LEB128 for the trick with toRadixString!
final numSevenBlocks = (i.toRadixString(2).length / 7).ceil();
for (var j = 0; j < numSevenBlocks; j++) {
// The 7 LSB of the byte we're creating
final x = (i & (lsb7Mask << j * 7)) >> j * 7;
// The next bits
final next = i & (lsb7Mask << (j + 1) * 7);

if (next == 0) {
if (j == numSevenBlocks - 1) {
// If we were to shift further, we only get zero, so we're at the end
ret.add(x);
break;
} else {
// We still have at least one bit more to go, so set the MSB to 1
ret.add(x + msb);
j++;
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/omemo_test.dart
Expand Up @@ -220,7 +220,7 @@ void main() {
expect(messagePlaintext, aliceMessage2);
});

test('Test using sending empty OMEMO messages', () async {
test('Test sending empty OMEMO messages', () async {
const aliceJid = 'alice@server.example';
const bobJid = 'bob@other.server.example';

Expand Down Expand Up @@ -638,6 +638,7 @@ void main() {
await bobSession.getDeviceBundle(),
],
);

await bobSession.decryptMessage(
msg1.ciphertext,
aliceJid,
Expand Down
12 changes: 12 additions & 0 deletions test/protobuf_test.dart
Expand Up @@ -48,6 +48,10 @@ void main() {
<int>[172, 2],
);
});

test('Test some special cases', () {
expect(decodeVarint(encodeVarint(1042464893), 0).n, 1042464893);
});
});

group('OMEMOMessage', () {
Expand Down Expand Up @@ -170,5 +174,13 @@ void main() {
expect(decoded.message!.mac, <int>[5, 6, 8, 0]);
expect(decoded.message!.message, <int>[4, 5, 7, 3, 2]);
});
test('Test decoding an issue', () {
/*
final data = 'CAAQfRogc2GwslU219dUkrMHNM4KdZRmuFnBTae+bQaJ+55IsAMiII7aZKj2sUpb6xR/3Ari7WZUmKFV0G6czUc4NMvjKDBaKnwKEM2ZpI8X3TgcxhxwENANnlsSaAgAEAAaICy8T9WPgLb7RdYd8/4JkrLF0RahEkC3ZaEfk5jw3dsLIkBMILzLyByweLgF4lCn0oNea+kbdrFr6rY7r/7WyI8hXEQz38QpnN+jyGGwC7Ga0dq70WuyqE7VpiFArQwqZh2G';
final kex = OmemoKeyExchange.fromBuffer(base64Decode(data));
expect(kex.spkId!, 1042464893);
*/
});
});
}

0 comments on commit 438012d

Please sign in to comment.