Skip to content

Commit

Permalink
tests: Run on wasm with wasm-bindgen-test
Browse files Browse the repository at this point in the history
  • Loading branch information
felinira committed Nov 23, 2024
1 parent ef890f9 commit 434018f
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.wasm32-unknown-unknown]
runner = 'wasm-bindgen-test-runner'
47 changes: 47 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ getrandom = { version = "0.2.5", features = ["js"] }
test-log = { workspace = true }
eyre = { workspace = true }

[target.'cfg(target_family = "wasm")'.dev-dependencies]
wasm-bindgen-test = "0.3"

[features]

# Check the entropy of custom codes. Will fail for any weak passwords.
Expand Down
6 changes: 6 additions & 0 deletions src/core/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ mod test {
use crate::core::EitherSide;

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_extract_pake_msg() {
// let _key = super::KeyMachine::new(
// &AppID::new("appid"),
Expand All @@ -241,6 +242,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_derive_key() {
let main = secretbox::Key::from_exact_iter(
hex::decode("588ba9eef353778b074413a0140205d90d7479e36e0dd4ee35bb729d26131ef1")
Expand All @@ -261,6 +263,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_derive_phase_key() {
let main = secretbox::Key::from_exact_iter(
hex::decode("588ba9eef353778b074413a0140205d90d7479e36e0dd4ee35bb729d26131ef1")
Expand Down Expand Up @@ -290,6 +293,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_derive_phase_key2() {
// feed python's derive_phase_key with these inputs:
// key = b"key"
Expand Down Expand Up @@ -317,6 +321,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_encrypt_data() {
let k = secretbox::Key::from_exact_iter(
hex::decode("ddc543ef8e4629a603d39dd0307a51bb1e7adb9cb259f6b085c91d0842a18679")
Expand All @@ -335,6 +340,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_decrypt_data() {
let k = secretbox::Key::from_exact_iter(
hex::decode("ddc543ef8e4629a603d39dd0307a51bb1e7adb9cb259f6b085c91d0842a18679")
Expand Down
17 changes: 17 additions & 0 deletions src/core/server_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ mod test {
use serde_json::{from_str, json, Value};

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_bind() {
let m1 = OutboundMessage::bind(
AppID::new("appid"),
Expand All @@ -270,6 +271,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_list() {
let m1 = OutboundMessage::List;
let s = serde_json::to_string(&m1).unwrap();
Expand All @@ -278,6 +280,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_allocate() {
let m1 = OutboundMessage::Allocate;
let s = serde_json::to_string(&m1).unwrap();
Expand All @@ -286,6 +289,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_claim() {
let m1 = OutboundMessage::claim("nameplate1");
let s = serde_json::to_string(&m1).unwrap();
Expand All @@ -294,6 +298,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_release() {
let m1 = OutboundMessage::release("nameplate1");
let s = serde_json::to_string(&m1).unwrap();
Expand All @@ -302,6 +307,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_open() {
let m1 = OutboundMessage::open(Mailbox(String::from("mailbox1")));
let s = serde_json::to_string(&m1).unwrap();
Expand All @@ -310,6 +316,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_add() {
let m1 = OutboundMessage::add(Phase("phase1".into()), b"body".to_vec());
let s = serde_json::to_string(&m1).unwrap();
Expand All @@ -322,6 +329,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_close() {
let m1 = OutboundMessage::close(Mailbox(String::from("mailbox1")), Mood::Happy);
let s = serde_json::to_string(&m1).unwrap();
Expand All @@ -334,6 +342,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_close_errory() {
let m1 = OutboundMessage::close(Mailbox(String::from("mailbox1")), Mood::Errory);
let s = serde_json::to_string(&m1).unwrap();
Expand All @@ -346,6 +355,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_close_scared() {
let m1 = OutboundMessage::close(Mailbox(String::from("mailbox1")), Mood::Scared);
let s = serde_json::to_string(&m1).unwrap();
Expand All @@ -358,6 +368,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[allow(deprecated)]
fn test_welcome3() {
let s = r#"{"type": "welcome", "welcome": {}, "server_tx": 1234.56}"#;
Expand All @@ -376,6 +387,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[allow(deprecated)]
fn test_welcome4() {
let s = r#"{"type": "welcome", "welcome": {} }"#;
Expand All @@ -396,6 +408,7 @@ mod test {
// TODO: when "error_on_line_overflow=false" lands on rustfmt(stable),
// let's replace this cfg_attr with a change to our .rustfmt.toml
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[rustfmt::skip]
#[allow(deprecated)]
fn test_welcome5() {
Expand All @@ -406,6 +419,7 @@ mod test {

/// Test permission_required field deserialization
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[allow(deprecated)]
fn test_welcome6() {
let s = r#"{"type": "welcome", "welcome": { "motd": "hello world", "permission-required": { "none": {}, "hashcash": { "bits": 6, "resource": "resource-string" }, "dark-ritual": { "hocrux": true } } } }"#;
Expand Down Expand Up @@ -433,6 +447,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_submit_permissions() {
let m = OutboundMessage::SubmitPermission(SubmitPermission::Hashcash {
stamp: "stamp".into(),
Expand All @@ -445,6 +460,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_ack() {
let s = r#"{"type": "ack", "id": null, "server_tx": 1234.56}"#;
let m = serde_json::from_str(s).unwrap();
Expand All @@ -455,6 +471,7 @@ mod test {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_message() {
let s = r#"{"body": "7b2270616b655f7631223a22353361346566366234363434303364376534633439343832663964373236646538396462366631336632613832313537613335646562393562366237633536353533227d", "server_rx": 1523468188.293486, "id": null, "phase": "pake", "server_tx": 1523498654.753594, "type": "message", "side": "side1"}"#;
let m = serde_json::from_str(s).unwrap();
Expand Down
6 changes: 6 additions & 0 deletions src/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn default_relay_hints() -> Vec<transit::RelayHint> {
}

#[test(async_std::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
pub async fn test_connect_with_unknown_code_and_allocate_passes() {
let code = generate_random_code();

Expand All @@ -70,6 +71,7 @@ pub async fn test_connect_with_unknown_code_and_allocate_passes() {
}

#[test(async_std::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
pub async fn test_connect_with_unknown_code_and_no_allocate_fails() {
tracing::info!("hola!");
let code = generate_random_code();
Expand Down Expand Up @@ -542,6 +544,7 @@ pub async fn test_wrong_code() -> eyre::Result<()> {

/** Connect three people to the party and watch it explode … gracefully */
#[test(async_std::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
pub async fn test_crowded() {
let initial_mailbox_connection = MailboxConnection::create(APP_CONFIG, 2).await.unwrap();
tracing::info!("This test's code is: {}", &initial_mailbox_connection.code);
Expand All @@ -564,6 +567,7 @@ pub async fn test_crowded() {
}

#[async_std::test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
pub async fn test_connect_with_code_expecting_nameplate() {
let code = generate_random_code();
let result = MailboxConnection::connect(APP_CONFIG, code.clone(), false).await;
Expand All @@ -586,13 +590,15 @@ fn generate_random_code() -> Code {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_phase() {
let p = Phase::PAKE;
assert!(p.is_pake());
assert!(!p.is_version());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_mood() {
// The serialized forms of these variants are part of the wire protocol,
// so they must be spelled exactly as shown (they must match the strings
Expand Down
Loading

0 comments on commit 434018f

Please sign in to comment.