-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd63528
commit 595ac21
Showing
6 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ libs/ | |
.project | ||
.settings | ||
.DS_Store | ||
xenon-unit-test-*/ | ||
mls/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.wire.xenon; | ||
|
||
import com.wire.crypto.CoreCryptoException; | ||
import com.wire.xenon.crypto.mls.CryptoMlsClient; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Base64; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class MlsClientTest { | ||
@Test | ||
public void testMlsClientInitialization() { | ||
String client1 = "alice1_" + UUID.randomUUID(); | ||
CryptoMlsClient mlsClient = new CryptoMlsClient(client1, "pwd"); | ||
assert mlsClient != null; | ||
mlsClient.close(); | ||
|
||
CryptoMlsClient mlsSameClient = new CryptoMlsClient(client1, "pwd"); | ||
assert mlsSameClient != null; | ||
assert mlsSameClient.getId().equals(client1); | ||
|
||
final byte[] publicKey = mlsSameClient.getPublicKey(); | ||
assert publicKey.length > 10; | ||
|
||
final List<byte[]> keyPackages = mlsSameClient.generateKeyPackages(10); | ||
assert keyPackages.size() == 10; | ||
mlsSameClient.close(); | ||
} | ||
|
||
@Test | ||
public void testMlsClientFailOnDifferentPassword(){ | ||
String client1 = "alice1_" + UUID.randomUUID(); | ||
CryptoMlsClient mlsClient = new CryptoMlsClient(client1, "pwd"); | ||
assert mlsClient != null; | ||
mlsClient.close(); | ||
|
||
assertThrows(CoreCryptoException.class, () -> { | ||
new CryptoMlsClient(client1, "WRONG_PASSWORD"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testMlsClientCreateConversationAndEncrypt() throws IOException { | ||
String client1 = "alice1_" + UUID.randomUUID(); | ||
// Group ID in base64 format, copied from a real one | ||
String groupIdBase64 = "AAEAAliWyGZ3/FqGpDPZdcuLQ0UAYW50YS53aXJlLmxpbms="; | ||
|
||
// GroupInfo of a real conversation, stored in a binary test file | ||
InputStream inputStream = new FileInputStream("src/test/resources/tmp.bin"); | ||
byte[] groupInfo = inputStream.readAllBytes(); | ||
|
||
// Create a new client and join the conversation | ||
CryptoMlsClient mlsClient = new CryptoMlsClient(client1, "pwd"); | ||
final byte[] commitBundle = mlsClient.createJoinConversationRequest(groupInfo); | ||
assert commitBundle.length > groupInfo.length; | ||
mlsClient.markConversationAsJoined(groupIdBase64); | ||
|
||
// Encrypt a message for the joined conversation | ||
String plainMessage = UUID.randomUUID().toString(); | ||
final byte[] encryptedMessage = mlsClient.encrypt(groupIdBase64, plainMessage.getBytes()); | ||
assert encryptedMessage.length > 10; | ||
final String encryptedBase64Message = Base64.getEncoder().encodeToString(encryptedMessage); | ||
|
||
// assertThrows cannot check for exception messages, so had to use try-catch and assert we have the correct error | ||
try { | ||
mlsClient.decrypt(groupIdBase64, encryptedBase64Message); | ||
throw new IllegalArgumentException("Decryption should fail"); | ||
} catch (Exception e) { | ||
// Unfortunately it is not possible for a single client to decrypt a message it encrypted itself | ||
// By getting the duplicated message exception we know that the encryption and decryption works | ||
// but we cannot attest that the decrypted message is the same as the original | ||
assert e.getMessage().contains("DuplicateMessage: We already decrypted this message once"); | ||
} | ||
} | ||
} |
Binary file not shown.