interop/
main.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Wire
// Copyright (C) 2022 Wire Swiss GmbH

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.

#![cfg_attr(target_family = "wasm", allow(dead_code, unused_imports))]

#[cfg(not(target_family = "wasm"))]
use crate::clients::{EmulatedClient, EmulatedProteusClient};
#[cfg(not(target_family = "wasm"))]
use crate::util::{MlsTransportSuccessProvider, MlsTransportTestExt};
use color_eyre::eyre::{Result, eyre};
use core_crypto::prelude::CiphersuiteName;
use std::rc::Rc;
use std::sync::Arc;
use tls_codec::Serialize;

#[cfg(not(target_family = "wasm"))]
mod build;
#[cfg(not(target_family = "wasm"))]
mod clients;
#[cfg(not(target_family = "wasm"))]
mod util;

#[cfg(not(target_family = "wasm"))]
const TEST_SERVER_PORT: &str = "8000";
#[cfg(not(target_family = "wasm"))]
const TEST_SERVER_URI: &str = const_format::concatcp!("http://localhost:", TEST_SERVER_PORT);

const MLS_MAIN_CLIENTID: &[u8] = b"test_main";
const MLS_CONVERSATION_ID: &[u8] = b"test_conversation";
const ROUNDTRIP_MSG_AMOUNT: usize = 100;

const CIPHERSUITE_IN_USE: CiphersuiteName = CiphersuiteName::MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519;

// TODO: Add support for Android emulator. Tracking issue: WPB-9646
// TODO: Add support for iOS emulator when on macOS. Tracking issue: WPB-9646
fn main() -> Result<()> {
    run_test()
}

// need to be handled like this because https://github.com/rust-lang/cargo/issues/5220, otherwise
// it complains over a lacking main function
#[cfg(not(target_family = "wasm"))]
fn run_test() -> Result<()> {
    use std::time::{Duration, Instant};

    use tokio::net::{TcpListener, TcpStream};

    color_eyre::install()?;
    if std::env::var("RUST_LOG").is_ok() || std::env::var("CI").is_ok() {
        femme::start();
    }

    let force_webdriver_install = std::env::var("FORCE_WEBDRIVER_INSTALL").is_ok();

    // Check if we have a correct pwd
    let current_dir = std::env::current_dir()?;
    if current_dir.ends_with("interop") {
        let new_cwd = current_dir.parent().unwrap();
        log::info!("cwd was {current_dir:?}; setting it to {new_cwd:?}");
        std::env::set_current_dir(new_cwd)?;
    }
    let tempdir = tempfile::tempdir()?;

    // because cannot use `#[tokio::main]` on wasm target because tokio does not compile on this target
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap();

    runtime.block_on(async {
        build::web::webdriver::setup_webdriver(force_webdriver_install).await?;

        build::web::wasm::build_wasm(tempdir.path().to_path_buf()).await?;

        let spinner = util::RunningProcess::new("Starting HTTP server...", false);
        let http_server_hwnd = tokio::task::spawn(build::web::wasm::spawn_http_server(tempdir.path().to_path_buf()));
        spinner.success(format!("HTTP server started at 0.0.0.0:{TEST_SERVER_PORT} [OK]"));

        let mut spinner = util::RunningProcess::new("Starting WebDriver [ChromeDriver & GeckoDriver]...", false);
        let chrome_driver_addr = TcpListener::bind("127.0.0.1:0").await?.local_addr()?;
        let mut chrome_webdriver = build::web::webdriver::start_webdriver_chrome(&chrome_driver_addr).await?;
        spinner.update("Sleeping to wait for Webdrivers to get ready...");
        let timeout = Duration::from_secs(5);
        let start = Instant::now();
        while start.elapsed() < timeout {
            let chrome_ready = TcpStream::connect(&chrome_driver_addr).await.is_ok();
            if chrome_ready {
                break;
            }
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
        spinner.success("WebDriver [OK]");

        run_mls_test(&chrome_driver_addr).await?;

        #[cfg(feature = "proteus")]
        run_proteus_test(&chrome_driver_addr).await?;

        chrome_webdriver.kill().await?;
        http_server_hwnd.abort();
        Ok(())
    })
}

#[cfg(target_family = "wasm")]
fn run_test() -> Result<()> {
    panic!("E2E tests cannot be run on WASM")
}

#[cfg(not(target_family = "wasm"))]
async fn run_mls_test(chrome_driver_addr: &std::net::SocketAddr) -> Result<()> {
    use core_crypto::prelude::*;
    use rand::distributions::DistString;

    log::info!("Using ciphersuite {}", CIPHERSUITE_IN_USE);

    let spinner = util::RunningProcess::new("[MLS] Step 0: Initializing clients & env...", true);

    let ios_client = Rc::new(clients::corecrypto::ios::CoreCryptoIosClient::new().await?);
    let native_client = Rc::new(clients::corecrypto::native::CoreCryptoNativeClient::new().await?);
    let ffi_client = Rc::new(clients::corecrypto::ffi::CoreCryptoFfiClient::new().await?);
    let web_client = Rc::new(clients::corecrypto::web::CoreCryptoWebClient::new(chrome_driver_addr).await?);

    let mut clients: Vec<Rc<dyn clients::EmulatedMlsClient>> = vec![
        native_client.clone(),
        ffi_client.clone(),
        web_client.clone(),
        ios_client.clone(),
    ];

    let ciphersuites = vec![CIPHERSUITE_IN_USE.into()];
    let configuration = MlsCentralConfiguration::try_new(
        "whatever".into(),
        "test".into(),
        Some(MLS_MAIN_CLIENTID.into()),
        ciphersuites,
        None,
        Some(100),
    )?;
    let master_client = MlsCentral::try_new_in_memory(configuration).await?;

    let conversation_id = MLS_CONVERSATION_ID.to_vec();
    let config = MlsConversationConfiguration {
        ciphersuite: CIPHERSUITE_IN_USE.into(),
        ..Default::default()
    };
    let cc = CoreCrypto::from(master_client.clone());

    let success_provider = Arc::new(MlsTransportSuccessProvider::default());

    cc.provide_transport(success_provider.clone()).await;
    let transaction = cc.new_transaction().await?;
    transaction
        .new_conversation(&conversation_id, MlsCredentialType::Basic, config)
        .await?;

    spinner.success("[MLS] Step 0: Initializing clients [OK]");

    let spinner = util::RunningProcess::new("[MLS] Step 1: Fetching KeyPackages from clients...", true);

    use tls_codec::Deserialize as _;
    let mut key_packages = vec![];
    for c in clients.iter_mut() {
        let kp = c.get_keypackage().await?;
        let kp = KeyPackageIn::tls_deserialize(&mut kp.as_slice())?;
        key_packages.push(kp);
    }

    spinner.success("[MLS] Step 1: KeyPackages [OK]");

    let spinner = util::RunningProcess::new("[MLS] Step 2: Adding clients to conversation...", true);

    transaction
        .conversation(&conversation_id)
        .await?
        .add_members(key_packages)
        .await?;

    let conversation_add_msg = success_provider.latest_welcome_message().await;
    let welcome_raw = conversation_add_msg.tls_serialize_detached()?;

    for c in clients.iter_mut() {
        let conversation_id_from_welcome = c.process_welcome(&welcome_raw).await?;
        assert_eq!(conversation_id_from_welcome, conversation_id);
    }

    spinner.success("[MLS] Step 2: Added clients [OK]");

    let mut spinner = util::RunningProcess::new(
        format!("[MLS] Step 3: Roundtripping messages [0/{ROUNDTRIP_MSG_AMOUNT}]"),
        true,
    );

    let mut prng = rand::thread_rng();
    let mut message;
    for i in 1..=ROUNDTRIP_MSG_AMOUNT {
        message = rand::distributions::Alphanumeric.sample_string(&mut prng, 16);

        log::info!(
            "Master client [{}] >>> {}",
            hex::encode(master_client.client_id().await?.as_slice()),
            message
        );

        let mut message_to_decrypt = transaction
            .conversation(&conversation_id)
            .await?
            .encrypt_message(&message)
            .await?;

        for c in clients.iter_mut() {
            let decrypted_message_raw = c
                .decrypt_message(&conversation_id, &message_to_decrypt)
                .await?
                .ok_or_else(|| {
                    eyre!(
                        "[MLS] No message, something went very wrong [Client = {}]",
                        c.client_type()
                    )
                })?;

            let decrypted_message = String::from_utf8(decrypted_message_raw)?;

            log::info!(
                "{} [{}] <<< {}",
                c.client_name(),
                hex::encode(c.client_id()),
                decrypted_message
            );

            assert_eq!(
                decrypted_message,
                message,
                "[MLS] Messages differ [Client = {}]",
                c.client_type()
            );

            message_to_decrypt = c
                .encrypt_message(&conversation_id, decrypted_message.as_bytes())
                .await?;
        }

        let decrypted_master_raw = transaction
            .conversation(&conversation_id)
            .await
            .unwrap()
            .decrypt_message(message_to_decrypt)
            .await?
            .app_msg
            .ok_or_else(|| eyre!("[MLS] No message received on master client"))?;

        let decrypted_master = String::from_utf8(decrypted_master_raw)?;

        assert_eq!(decrypted_master, message);

        spinner.update(format!(
            "[MLS] Step 3: Roundtripping messages... [{i}/{ROUNDTRIP_MSG_AMOUNT}]"
        ));
    }

    clients.clear();

    spinner.success(format!(
        "[MLS] Step 3: Roundtripping {ROUNDTRIP_MSG_AMOUNT} messages... [OK]"
    ));

    let spinner = util::RunningProcess::new("[MLS] Step 4: Deleting clients...", true);

    Rc::into_inner(ios_client)
        .expect("Only one strong reference to the interop client")
        .wipe()
        .await?;
    Rc::into_inner(native_client)
        .expect("Only one strong reference to the interop client")
        .wipe()
        .await?;
    Rc::into_inner(ffi_client)
        .expect("Only one strong reference to the interop client")
        .wipe()
        .await?;
    Rc::into_inner(web_client)
        .expect("Only one strong reference to the interop client")
        .wipe()
        .await?;

    spinner.success("[MLS] Step 4: Deleting clients [OK]");

    Ok(())
}

#[cfg(all(not(target_family = "wasm"), feature = "proteus"))]
async fn run_proteus_test(chrome_driver_addr: &std::net::SocketAddr) -> Result<()> {
    use core_crypto::prelude::*;

    let spinner = util::RunningProcess::new("[Proteus] Step 0: Initializing clients & env...", true);

    let mut ios_client = clients::corecrypto::ios::CoreCryptoIosClient::new().await?;
    let mut native_client = clients::corecrypto::native::CoreCryptoNativeClient::new().await?;
    let mut ffi_client = clients::corecrypto::ffi::CoreCryptoFfiClient::new().await?;
    let mut web_client = clients::corecrypto::web::CoreCryptoWebClient::new(chrome_driver_addr).await?;
    let mut cryptobox_native_client = clients::cryptobox::native::CryptoboxNativeClient::new();
    let mut cryptobox_web_client = clients::cryptobox::web::CryptoboxWebClient::new(chrome_driver_addr).await?;

    ios_client.init().await?;
    native_client.init().await?;
    ffi_client.init().await?;
    web_client.init().await?;
    cryptobox_native_client.init().await?;
    cryptobox_web_client.init().await?;

    let ios_client = Rc::new(ios_client);
    let native_client = Rc::new(native_client);
    let ffi_client = Rc::new(ffi_client);
    let web_client = Rc::new(web_client);
    let cryptobox_native_client = Rc::new(cryptobox_native_client);
    let cryptobox_web_client = Rc::new(cryptobox_web_client);

    let mut clients: Vec<Rc<dyn clients::EmulatedProteusClient>> = vec![
        ios_client.clone(),
        native_client.clone(),
        ffi_client.clone(),
        web_client.clone(),
        cryptobox_native_client.clone(),
        cryptobox_web_client.clone(),
    ];

    let configuration = MlsCentralConfiguration::try_new(
        "whatever".into(),
        "test".into(),
        Some(MLS_MAIN_CLIENTID.into()),
        vec![MlsCiphersuite::default()],
        None,
        Some(100),
    )?;
    let master_client = CoreCrypto::from(MlsCentral::try_new_in_memory(configuration).await?);
    let transaction = master_client.new_transaction().await?;
    transaction.proteus_init().await?;

    let master_fingerprint = master_client.proteus_fingerprint().await?;

    spinner.success("[Proteus] Step 0: Initializing clients [OK]");

    let mut spinner = util::RunningProcess::new("[Proteus] Step 1: Fetching PreKeys from clients...", true);

    let mut client_type_mapping = std::collections::HashMap::new();
    let mut prekeys = std::collections::HashMap::new();
    for c in clients.iter_mut() {
        let client_name = c.client_name();
        spinner.update(format!("[Proteus] Step 1: PreKeys - {client_name}"));
        let fingerprint = c.fingerprint().await?;
        spinner.update(format!("[Proteus] Step 1: PreKeys - {fingerprint}@{client_name}"));
        client_type_mapping.insert(fingerprint.clone(), client_name.to_string());
        let prekey = c.get_prekey().await?;
        prekeys.insert(fingerprint, prekey);
    }

    spinner.success("[Proteus] Step 1: PreKeys [OK]");

    let mut spinner = util::RunningProcess::new("[Proteus] Step 2: Creating sessions...", true);

    let mut master_sessions = vec![];
    let mut messages = std::collections::HashMap::new();
    const PROTEUS_INITIAL_MESSAGE: &[u8] = b"Hello world!";
    for (fingerprint, prekey) in prekeys {
        spinner.update(format!(
            "[Proteus] Step 2: Session master -> {fingerprint}@{}",
            client_type_mapping[&fingerprint]
        ));
        let session_arc = transaction.proteus_session_from_prekey(&fingerprint, &prekey).await?;
        let mut session = session_arc.write().await;
        messages.insert(fingerprint, session.encrypt(PROTEUS_INITIAL_MESSAGE)?);
        master_sessions.push(session.identifier().to_string());
    }

    let session_id_with_master = format!("session-{master_fingerprint}");
    for c in clients.iter_mut() {
        let fingerprint = c.fingerprint().await?;
        spinner.update(format!(
            "[Proteus] Step 2: Session {fingerprint}@{} -> master",
            c.client_name()
        ));
        let message = messages.remove(&fingerprint).unwrap();
        let message_recv = c.session_from_message(&session_id_with_master, &message).await?;
        assert_eq!(PROTEUS_INITIAL_MESSAGE, message_recv);
    }

    assert!(messages.is_empty());

    spinner.success("[Proteus] Step 2: Creating sessions [OK]");

    let mut spinner = util::RunningProcess::new(
        format!("[Proteus] Step 3: Roundtripping messages [0/{ROUNDTRIP_MSG_AMOUNT}]"),
        true,
    );

    let mut prng = rand::thread_rng();
    let mut message = [0u8; 128];
    let mut master_messages_to_decrypt = std::collections::HashMap::new();
    for i in 0..ROUNDTRIP_MSG_AMOUNT {
        use rand::RngCore as _;

        prng.fill_bytes(&mut message);

        let mut messages_to_decrypt = transaction.proteus_encrypt_batched(&master_sessions, &message).await?;

        for c in clients.iter_mut() {
            let fingerprint = c.fingerprint().await?;
            let decrypted_message = c
                .decrypt(
                    &session_id_with_master,
                    &messages_to_decrypt.remove(&fingerprint).unwrap(),
                )
                .await?;

            assert_eq!(
                decrypted_message,
                message,
                "[Proteus] Messages differ [Client = {}::{}]",
                c.client_name(),
                c.client_type(),
            );

            let ciphertext = c.encrypt(&session_id_with_master, &decrypted_message).await?;
            master_messages_to_decrypt.insert(fingerprint, ciphertext);
        }

        for (fingerprint, encrypted) in master_messages_to_decrypt.drain() {
            let decrypted = transaction.proteus_decrypt(&fingerprint, &encrypted).await?;
            assert_eq!(decrypted, message);
        }

        spinner.update(format!(
            "[Proteus] Step 3: Roundtripping messages... [{i}/{ROUNDTRIP_MSG_AMOUNT}]"
        ));
    }

    clients.clear();

    spinner.success(format!(
        "[Proteus] Step 3: Roundtripping {ROUNDTRIP_MSG_AMOUNT} messages... [OK]"
    ));

    let spinner = util::RunningProcess::new("[Proteus] Step 4: Deleting clients...", true);

    Rc::into_inner(ios_client)
        .expect("Only one strong reference to the interop client")
        .wipe()
        .await?;
    Rc::into_inner(native_client)
        .expect("Only one strong reference to the interop client")
        .wipe()
        .await?;
    Rc::into_inner(ffi_client)
        .expect("Only one strong reference to the interop client")
        .wipe()
        .await?;
    Rc::into_inner(web_client)
        .expect("Only one strong reference to the interop client")
        .wipe()
        .await?;
    Rc::into_inner(cryptobox_native_client)
        .expect("Only one strong reference to the interop client")
        .wipe()
        .await?;
    Rc::into_inner(cryptobox_web_client)
        .expect("Only one strong reference to the interop client")
        .wipe()
        .await?;

    spinner.success("[Proteus] Step 4: Deleting clients [OK]");

    Ok(())
}