test_pending.html (3741B)
1 <!DOCTYPE HTML> 2 <html> 3 4 <head> 5 <title>Test for Messaging Layer Security</title> 6 <!-- SimpleTest Helpers --> 7 <script src="/tests/SimpleTest/SimpleTest.js"></script> 8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 9 <!-- Local Helpers --> 10 <script src="head_mls.js"></script> 11 </head> 12 13 <body> 14 <pre id="test"> 15 <script class="testbody" type="text/javascript"> 16 17 async function test_pending() { 18 19 const mls = new MLS(); 20 21 // Alice: Create signature keypair and credential 22 const alice = await mls.generateIdentity(); 23 const alice_credential = await mls.generateCredential("alice"); 24 25 // Bob: Create signature keypair and credential 26 const bob = await mls.generateIdentity(); 27 const bob_credential = await mls.generateCredential("bob"); 28 29 // Bob: Generate a key package 30 const bob_key_package = await mls.generateKeyPackage(bob, bob_credential); 31 32 // Alice: Create a group 33 let group_alice = await mls.groupCreate(alice, alice_credential); 34 35 // Alice: Add Bob to the group 36 await group_alice.add(bob_key_package); 37 38 // Check that Alice has a pending commit 39 let has_pending_commit = await group_alice.hasPendingCommit(); 40 info("Does Alice have pending commit? ", has_pending_commit); 41 is(has_pending_commit, true); 42 43 // Discard Alice's pending commit 44 await group_alice.clearPendingCommit(); 45 46 // Check that Alice has a pending commit 47 let has_pending_commit2 = await group_alice.hasPendingCommit(); 48 info("Does Alice have pending commit? ", has_pending_commit2); 49 is(has_pending_commit2, false); 50 isnot(has_pending_commit2, null); 51 52 // Alice: Add Bob to the group 53 let commit_output = await group_alice.add(bob_key_package); 54 55 // Check that Alice has a pending commit 56 let has_pending_commit3 = await group_alice.hasPendingCommit(); 57 info("Does Alice have pending commit? ", has_pending_commit3); 58 is(has_pending_commit3, true); 59 60 // Alice: process her Add commit instead of receiving the commit 61 await group_alice.applyPendingCommit(); 62 63 // Check that Alice has a pending commit 64 let has_pending_commit4 = await group_alice.hasPendingCommit(); 65 info("Does Alice have pending commit? ", has_pending_commit4); 66 is(has_pending_commit4, false); 67 isnot(has_pending_commit4, null); 68 69 // Bob: Join the group 70 let group_bob = await mls.groupJoin(bob, commit_output.welcome); 71 72 // Test: compare group identifier from Alice and Bob 73 is(byteArrayToHexString(group_alice.groupId), byteArrayToHexString(group_bob.groupId), "Alice GID == Bob GID"); 74 75 // Alice & Bob: Export a secret 76 const context_bytes = new Uint8Array([99, 111, 110, 116, 101, 120, 116]); // "context" in ASCII 77 78 const exportAlice = await group_alice.exportSecret("label", context_bytes, 15); 79 const exportBob = await group_bob.exportSecret("label", context_bytes, 15); 80 81 // Test: compare exporter from Alice and Bob 82 is(byteArrayToHexString(exportAlice.exporter), byteArrayToHexString(exportBob.exporter), "Exporter Alice == Exporter Bob"); 83 84 // Bob: send a message to the group 85 const message = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 32, 33]); // "Hello World !" in ASCII 86 const ctx = await group_bob.send(message); 87 88 // Alice: receive a message from the group 89 const pt = await group_alice.receive(ctx); 90 info("Alice received a message from Bob: " + JSON.stringify(pt)); 91 92 // Test: compare the message and the decrypted message 93 is(byteArrayToHexString(message), byteArrayToHexString(pt.content), "Plaintext == Decrypted Message"); 94 95 SimpleTest.finish(); 96 } 97 98 SimpleTest.waitForExplicitFinish(); 99 test_pending(); 100 101 </script> 102 </pre> 103 </body> 104 105 </html>