From c5843933f3d1aecd2da86055200b0a07a0a24807 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 17:40:36 +0100 Subject: [PATCH 01/11] Add build_and_test.yml workflow --- .github/workflows/build_and_test.yml | 88 ++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 .github/workflows/build_and_test.yml diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml new file mode 100644 index 0000000..665f0e2 --- /dev/null +++ b/.github/workflows/build_and_test.yml @@ -0,0 +1,88 @@ +name: Build, Test, Publish Coverage + +on: + push: + branches: ["main"] + pull_request: + branches: ["main", "dev"] + +env: + CARGO_TERM_COLOR: always + +jobs: + linux: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: Swatinem/rust-cache@v2 + with: + cache-all-crates: "true" + prefix-key: "linux" + - name: Build, Test and Publish Coverage + run: | + if [ -n "${{ secrets.COVERALLS_REPO_TOKEN }}" ]; then + curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash + cargo binstall --no-confirm cargo-tarpaulin --force + cargo tarpaulin --all-features --avoid-cfg-tarpaulin --tests --examples --verbose --skip-clean --coveralls ${{ secrets.COVERALLS_REPO_TOKEN }} --timeout 120 + else + echo "Code Coverage step is skipped on forks!" + cargo build --verbose --all-features + cargo test --verbose --all-features + fi + # wasm-safari: + # runs-on: macos-latest + # steps: + # - uses: actions/checkout@v4 + # - name: Clone spacebar server + # run: | + # git clone https://github.com/bitfl0wer/server.git + # - uses: actions/setup-node@v3 + # with: + # node-version: 18 + # cache: 'npm' + # cache-dependency-path: server/package-lock.json + # - name: Prepare and start Spacebar server + # run: | + # npm install + # npm run setup + # npm run start & + # working-directory: ./server + # - uses: Swatinem/rust-cache@v2 + # with: + # cache-all-crates: "true" + # prefix-key: "macos-safari" + # - name: Run WASM tests with Safari, Firefox, Chrome + # run: | + # rustup target add wasm32-unknown-unknown + # curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash + # cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force + # SAFARIDRIVER=$(which safaridriver) cargo test --target wasm32-unknown-unknown --no-default-features --features="client, rt" --no-fail-fast + wasm-gecko: + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - uses: Swatinem/rust-cache@v2 + with: + cache-all-crates: "true" + prefix-key: "macos" + - name: Run WASM tests with Safari, Firefox, Chrome + run: | + rustup target add wasm32-unknown-unknown + curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash + cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force + GECKODRIVER=$(which geckodriver) cargo test --target wasm32-unknown-unknown --no-default-features --features="client, rt" + wasm-chrome: + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - uses: Swatinem/rust-cache@v2 + with: + cache-all-crates: "true" + prefix-key: "macos" + - name: Run WASM tests with Safari, Firefox, Chrome + run: | + rustup target add wasm32-unknown-unknown + curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash + cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force + CHROMEDRIVER=$(which chromedriver) cargo test --target wasm32-unknown-unknown --no-default-features --features="client, rt" From b75870143691fda5cd09cd90cdadd85e71893ca4 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 17:40:44 +0100 Subject: [PATCH 02/11] Update README.md with badges --- README.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 628eab0..0178ee7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,16 @@ +
+ +[![Discord]][Discord-invite] +[![Matrix]][Matrix-invite] +[![Build][build-shield]][build-url] +[![Coverage][coverage-shield]][coverage-url] +Blue status badge, reading 'Early Development' + +
+ # polyproto -(Generic) Rust types and traits to quickly get a +Crate supplying (generic) Rust types and traits to quickly get a [polyproto](https://docs.polyphony.chat/Protocol%20Specifications/core/) implementation up and running. @@ -21,9 +31,21 @@ choice. Popular crates for cryptography and signature algorithms supply their ow You can then use the [crate::certs] types to build certificates using your implementations of the aforementioned traits. +View the [examples](./examples/) directory for a simple example on how to implement and use this +crate. + ## Cryptography This crate provides no cryptographic functionality whatsoever; its sole purpose is to aid in implementing polyproto by transforming the [polyproto specification](https://docs.polyphony.chat/Protocol%20Specifications/core/) into well-defined yet adaptable Rust types. + +[build-shield]: https://img.shields.io/github/actions/workflow/status/polyphony-chat/polyproto/build_and_test.yml?style=flat +[build-url]: https://github.com/polyphony-chat/polyproto/blob/main/.github/workflows/build_and_test.yml +[coverage-shield]: https://coveralls.io/repos/github/polyphony-chat/polyproto/badge.svg?branch=main +[coverage-url]: https://coveralls.io/github/polyphony-chat/polyproto?branch=main +[Discord]: https://dcbadge.vercel.app/api/server/m3FpcapGDD?style=flat +[Discord-invite]: https://discord.com/invite/m3FpcapGDD +[Matrix]: https://img.shields.io/matrix/polyproto%3Atu-dresden.de?server_fqdn=matrix.tu-dresden.de&style=flat&label=Matrix%20Room +[Matrix-invite]: https://matrix.to/#/#polyproto:tu-dresden.de \ No newline at end of file From ebd8b036956136e3649ae4c2c53fcec80013d433 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 17:43:32 +0100 Subject: [PATCH 03/11] Change flags passed to cargo test on wasm builds --- .github/workflows/build_and_test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 665f0e2..83548f9 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -71,7 +71,7 @@ jobs: rustup target add wasm32-unknown-unknown curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force - GECKODRIVER=$(which geckodriver) cargo test --target wasm32-unknown-unknown --no-default-features --features="client, rt" + GECKODRIVER=$(which geckodriver) cargo test --tests --examples --target wasm32-unknown-unknown" wasm-chrome: runs-on: macos-latest steps: @@ -85,4 +85,4 @@ jobs: rustup target add wasm32-unknown-unknown curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force - CHROMEDRIVER=$(which chromedriver) cargo test --target wasm32-unknown-unknown --no-default-features --features="client, rt" + CHROMEDRIVER=$(which chromedriver) cargo test --tests --examples --target wasm32-unknown-unknown" From dc8e7ddb88a41065ad5974795950d41757a69163 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 17:47:52 +0100 Subject: [PATCH 04/11] add features-flag --- Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 10d0ac9..0a8c781 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,12 @@ repository = "https://github.com/polyphony-chat/polyproto" [lib] crate-type = ["rlib", "cdylib", "staticlib"] +[features] +wasm = ["dep:getrandom", "getrandom/js"] + [dependencies] der = { version = "0.7.8", features = ["pem"] } +getrandom = { version = "0.2.12", optional = true } spki = "0.7.3" thiserror = "1.0.57" x509-cert = { version = "0.2.5", default-features = false } From f869ca4cb882ce97aad30ce94999bcd0ae40bff8 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 17:48:19 +0100 Subject: [PATCH 05/11] Execute with feature flag wasm --- .github/workflows/build_and_test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 83548f9..38fa3cc 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -71,7 +71,7 @@ jobs: rustup target add wasm32-unknown-unknown curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force - GECKODRIVER=$(which geckodriver) cargo test --tests --examples --target wasm32-unknown-unknown" + GECKODRIVER=$(which geckodriver) cargo test --tests --examples --target wasm32-unknown-unknown" --features wasm wasm-chrome: runs-on: macos-latest steps: @@ -85,4 +85,4 @@ jobs: rustup target add wasm32-unknown-unknown curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force - CHROMEDRIVER=$(which chromedriver) cargo test --tests --examples --target wasm32-unknown-unknown" + CHROMEDRIVER=$(which chromedriver) cargo test --tests --examples --target wasm32-unknown-unknown" --features wasm From 8a6c1f8c7e4a566df864857e326de9012bdbf108 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 17:49:31 +0100 Subject: [PATCH 06/11] remove accidental " --- .github/workflows/build_and_test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 38fa3cc..8478a8b 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -71,7 +71,7 @@ jobs: rustup target add wasm32-unknown-unknown curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force - GECKODRIVER=$(which geckodriver) cargo test --tests --examples --target wasm32-unknown-unknown" --features wasm + GECKODRIVER=$(which geckodriver) cargo test --tests --examples --target wasm32-unknown-unknown --features wasm wasm-chrome: runs-on: macos-latest steps: @@ -85,4 +85,4 @@ jobs: rustup target add wasm32-unknown-unknown curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force - CHROMEDRIVER=$(which chromedriver) cargo test --tests --examples --target wasm32-unknown-unknown" --features wasm + CHROMEDRIVER=$(which chromedriver) cargo test --tests --examples --target wasm32-unknown-unknown --features wasm From efd28b795c6ca70dc325632b4aaf7271ac6506eb Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 17:58:07 +0100 Subject: [PATCH 07/11] add wasm specific dev-dependencies --- Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 0a8c781..c800c5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,7 @@ x509-cert = { version = "0.2.5", default-features = false } ed25519-dalek = { version = "2.1.1", features = ["rand_core", "signature"] } rand = "0.8.5" polyproto = { path = "./" } + +[target.'cfg(target_arch = "wasm32")'.dev-dependencies] +wasm-bindgen-test = "0.3.39" +wasm-bindgen = "0.2.89" From d2826e2235d0519ae705c5733443583bb0085d81 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 17:58:26 +0100 Subject: [PATCH 08/11] prefix main method with wasm_bindgen_test macro --- examples/ed25519_basic.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/ed25519_basic.rs b/examples/ed25519_basic.rs index 7d5a60c..ede19c9 100644 --- a/examples/ed25519_basic.rs +++ b/examples/ed25519_basic.rs @@ -17,6 +17,7 @@ use rand::rngs::OsRng; use spki::{AlgorithmIdentifierOwned, ObjectIdentifier, SignatureBitStringEncoding}; use thiserror::Error; +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] fn main() { let mut csprng = rand::rngs::OsRng; // Generate a key pair From 8a09e9f2da9de2946eaf14addb96bd599bad0320 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 17:58:39 +0100 Subject: [PATCH 09/11] prefix main method with wasm_bindgen_test macro, disable writing of file to disk when run with wasm --- examples/ed25519_csr.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/ed25519_csr.rs b/examples/ed25519_csr.rs index 0d709d7..2b666dd 100644 --- a/examples/ed25519_csr.rs +++ b/examples/ed25519_csr.rs @@ -27,6 +27,7 @@ use x509_cert::request::CertReq; /// openssl req -in cert.csr -verify /// ``` +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] fn main() { let mut csprng = rand::rngs::OsRng; let priv_key = Ed25519PrivateKey::gen_keypair(&mut csprng); @@ -45,6 +46,7 @@ fn main() { println!("Certrequest der bytes: {:?}", certrequest.to_der().unwrap()); let data = certrequest.to_der().unwrap(); let file_name_with_extension = "cert.csr"; + #[cfg(not(target_arch = "wasm32"))] std::fs::write(file_name_with_extension, &data).unwrap(); // TODO: The attributes are still missing. CA Certificates and Actor Certificates should have From 5e2cd35983373664a448be360d21228cc3d75e64 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 18:09:20 +0100 Subject: [PATCH 10/11] add conditionally compiled wasm bindgen test macros --- src/lib.rs | 3 ++- src/value_constraints.rs | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index af7ada7..54d2b96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -155,7 +155,8 @@ mod test { } } - #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), test)] fn test_convert_serial_number() { let biguint = Uint::new(&[10u8, 240u8]).unwrap(); assert_eq!(biguint.as_bytes(), &[10u8, 240u8]); diff --git a/src/value_constraints.rs b/src/value_constraints.rs index 5204a47..50e36a5 100644 --- a/src/value_constraints.rs +++ b/src/value_constraints.rs @@ -109,7 +109,8 @@ mod name_constraints { use crate::Constrained; - #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), test)] fn correct() { let name = Name::from_str( "cn=flori,dc=localhost,uid=h3g2jt4dhfgj8hjs,uniqueIdentifier=flori@localhost", @@ -120,19 +121,22 @@ mod name_constraints { name.validate().unwrap(); } - #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), test)] fn no_domain_component() { let name = Name::from_str("CN=flori").unwrap(); assert!(name.validate().is_err()); } - #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), test)] fn two_cns() { let name = Name::from_str("CN=flori,CN=xenia,DC=localhost").unwrap(); assert!(name.validate().is_err()) } - #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), test)] fn two_uid_or_uniqueid() { let name = Name::from_str("CN=flori,CN=xenia,uid=numbaone,uid=numbatwo").unwrap(); assert!(name.validate().is_err()); @@ -142,7 +146,8 @@ mod name_constraints { assert!(name.validate().is_err()) } - #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), test)] fn uid_and_no_uniqueid_or_uniqueid_and_no_uid() { let name = Name::from_str("CN=flori,CN=xenia,uid=numbaone").unwrap(); assert!(name.validate().is_err()); @@ -157,17 +162,20 @@ mod session_id_constraints { use crate::certs::SessionId; - #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), test)] fn zero_long_session_id_fails() { assert!(SessionId::new(Ia5String::new("").unwrap()).is_err()) } - #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), test)] fn thirtytwo_length_session_id_is_ok() { assert!(SessionId::new(Ia5String::new("11111111111111111111111111222222").unwrap()).is_ok()) } - #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), test)] fn thirtythree_length_session_id_fails() { assert!( SessionId::new(Ia5String::new("111111111111111111111111112222223").unwrap()).is_err() From 8d3e32082aeddb948d0f472202271a6b9d6adddc Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 4 Mar 2024 18:09:37 +0100 Subject: [PATCH 11/11] remove one runner, change test execution strategy on wasm --- .github/workflows/build_and_test.yml | 56 ++++++---------------------- 1 file changed, 11 insertions(+), 45 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 8478a8b..22fdd5c 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -28,37 +28,9 @@ jobs: else echo "Code Coverage step is skipped on forks!" cargo build --verbose --all-features - cargo test --verbose --all-features + cargo test --verbose --all-features --tests --examples fi - # wasm-safari: - # runs-on: macos-latest - # steps: - # - uses: actions/checkout@v4 - # - name: Clone spacebar server - # run: | - # git clone https://github.com/bitfl0wer/server.git - # - uses: actions/setup-node@v3 - # with: - # node-version: 18 - # cache: 'npm' - # cache-dependency-path: server/package-lock.json - # - name: Prepare and start Spacebar server - # run: | - # npm install - # npm run setup - # npm run start & - # working-directory: ./server - # - uses: Swatinem/rust-cache@v2 - # with: - # cache-all-crates: "true" - # prefix-key: "macos-safari" - # - name: Run WASM tests with Safari, Firefox, Chrome - # run: | - # rustup target add wasm32-unknown-unknown - # curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash - # cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force - # SAFARIDRIVER=$(which safaridriver) cargo test --target wasm32-unknown-unknown --no-default-features --features="client, rt" --no-fail-fast - wasm-gecko: + wasm: runs-on: macos-latest steps: - uses: actions/checkout@v4 @@ -71,18 +43,12 @@ jobs: rustup target add wasm32-unknown-unknown curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force - GECKODRIVER=$(which geckodriver) cargo test --tests --examples --target wasm32-unknown-unknown --features wasm - wasm-chrome: - runs-on: macos-latest - steps: - - uses: actions/checkout@v4 - - uses: Swatinem/rust-cache@v2 - with: - cache-all-crates: "true" - prefix-key: "macos" - - name: Run WASM tests with Safari, Firefox, Chrome - run: | - rustup target add wasm32-unknown-unknown - curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash - cargo binstall --no-confirm wasm-bindgen-cli --version "0.2.88" --force - CHROMEDRIVER=$(which chromedriver) cargo test --tests --examples --target wasm32-unknown-unknown --features wasm + cargo binstall --no-confirm wasm-pack --force + wasm-pack test --node -- --features wasm --examples + wasm-pack test --firefox -- --features wasm --examples + wasm-pack test --chrome -- --features wasm --examples + wasm-pack test --safari -- --features wasm --examples + wasm-pack test --node -- --features wasm + wasm-pack test --firefox -- --features wasm + wasm-pack test --chrome -- --features wasm + wasm-pack test --safari -- --features wasm