Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wasm tests using was-bindgen-test and wasm-pack #346

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 43 additions & 28 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ on:
- master
pull_request:


jobs:
style:
name: Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Check format
run: cargo fmt --check
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Check format
run: cargo fmt --check

clippy:
name: Clippy
Expand All @@ -36,27 +35,43 @@ jobs:
matrix:
build: [pinned, stable, nightly]
include:
- build: pinned
os: ubuntu-20.04
rust: 1.67.0
- build: stable
os: ubuntu-20.04
rust: stable
- build: nightly
os: ubuntu-20.04
rust: nightly
- build: pinned
os: ubuntu-20.04
rust: 1.67.0
- build: stable
os: ubuntu-20.04
rust: stable
- build: nightly
os: ubuntu-20.04
rust: nightly
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}

- name: Build System Info
run: rustc --version

- name: Build System Info
run: rustc --version
- name: Run tests default features
run: cargo test

- name: Run tests no features
run: cargo test --no-default-features

wasm:
name: Run tests in wasm
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-unknown-unknown

- name: Run tests default features
run: cargo test
- name: Run tests default features
run: wasm-pack test --node

- name: Run tests no features
run: cargo test --no-default-features
- name: Run tests no features
run: wasm-pack test --node --no-default-features
24 changes: 19 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,44 @@ homepage = "https://github.com/Keats/jsonwebtoken"
repository = "https://github.com/Keats/jsonwebtoken"
keywords = ["jwt", "api", "token", "jwk"]
edition = "2021"
include = ["src/**/*", "benches/**/*", "tests/**/*", "LICENSE", "README.md", "CHANGELOG.md"]
include = [
"src/**/*",
"benches/**/*",
"tests/**/*",
"LICENSE",
"README.md",
"CHANGELOG.md",
]
rust-version = "1.67.0"

[dependencies]
serde_json = "1.0"
serde = {version = "1.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
base64 = "0.21.0"
# For PEM decoding
pem = {version = "3", optional = true}
simple_asn1 = {version = "0.6", optional = true}
pem = { version = "3", optional = true }
simple_asn1 = { version = "0.6", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.17.4", features = ["std"] }


[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3"
ring = { version = "0.17.4", features = ["std", "wasm32_unknown_unknown_js"] }

[dev-dependencies]
wasm-bindgen-test = "0.3.1"

[target.'cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))'.dev-dependencies]
# For the custom time example
time = "0.3"
criterion = "0.4"

[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dev-dependencies]
# For the custom time example
time = { version = "0.3", features = ["wasm-bindgen"] }
criterion = { version = "0.4", default-features = false }

[features]
default = ["use_pem"]
use_pem = ["pem", "simple_asn1"]
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header,
struct Claims {
sub: String,
company: String,
exp: usize,
exp: u64,
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Claims {
aud: String,
sub: String,
company: String,
exp: usize,
exp: u64,
}

fn main() {
Expand Down
7 changes: 7 additions & 0 deletions tests/ecdsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
use jsonwebtoken::{decode, encode, Header, Validation};
#[cfg(feature = "use_pem")]
use time::OffsetDateTime;
use wasm_bindgen_test::wasm_bindgen_test;

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Claims {
Expand All @@ -17,6 +18,7 @@ pub struct Claims {
}

#[test]
#[wasm_bindgen_test]
fn round_trip_sign_verification_pk8() {
let privkey = include_bytes!("private_ecdsa_key.pk8");
let pubkey = include_bytes!("public_ecdsa_key.pk8");
Expand All @@ -31,6 +33,7 @@ fn round_trip_sign_verification_pk8() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn round_trip_sign_verification_pem() {
let privkey_pem = include_bytes!("private_ecdsa_key.pem");
let pubkey_pem = include_bytes!("public_ecdsa_key.pem");
Expand All @@ -49,6 +52,7 @@ fn round_trip_sign_verification_pem() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn round_trip_claim() {
let privkey_pem = include_bytes!("private_ecdsa_key.pem");
let pubkey_pem = include_bytes!("public_ecdsa_key.pem");
Expand All @@ -74,6 +78,7 @@ fn round_trip_claim() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn ec_x_y() {
let privkey = include_str!("private_ecdsa_key.pem");
let my_claims = Claims {
Expand All @@ -100,6 +105,7 @@ fn ec_x_y() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn ed_jwk() {
use jsonwebtoken::jwk::Jwk;
use serde_json::json;
Expand Down Expand Up @@ -138,6 +144,7 @@ fn ed_jwk() {
// https://jwt.io/ is often used for examples so ensure their example works with jsonwebtoken
#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn roundtrip_with_jwtio_example() {
// We currently do not support SEC1 so we use the converted PKCS8 formatted
let privkey_pem = include_bytes!("private_jwtio_pkcs8.pem");
Expand Down
6 changes: 6 additions & 0 deletions tests/eddsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use jsonwebtoken::{
Algorithm, DecodingKey, EncodingKey,
};
use serde::{Deserialize, Serialize};
use wasm_bindgen_test::wasm_bindgen_test;

#[cfg(feature = "use_pem")]
use jsonwebtoken::{decode, encode, Header, Validation};
Expand All @@ -17,6 +18,7 @@ pub struct Claims {
}

#[test]
#[wasm_bindgen_test]
fn round_trip_sign_verification_pk8() {
let privkey = include_bytes!("private_ed25519_key.pk8");
let pubkey = include_bytes!("public_ed25519_key.pk8");
Expand All @@ -31,6 +33,7 @@ fn round_trip_sign_verification_pk8() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn round_trip_sign_verification_pem() {
let privkey_pem = include_bytes!("private_ed25519_key.pem");
let pubkey_pem = include_bytes!("public_ed25519_key.pem");
Expand All @@ -49,6 +52,7 @@ fn round_trip_sign_verification_pem() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn round_trip_claim() {
let privkey_pem = include_bytes!("private_ed25519_key.pem");
let pubkey_pem = include_bytes!("public_ed25519_key.pem");
Expand All @@ -74,6 +78,7 @@ fn round_trip_claim() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn ed_x() {
let privkey = include_str!("private_ed25519_key.pem");
let my_claims = Claims {
Expand All @@ -99,6 +104,7 @@ fn ed_x() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn ed_jwk() {
use jsonwebtoken::jwk::Jwk;
use serde_json::json;
Expand Down
4 changes: 4 additions & 0 deletions tests/header/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use base64::{engine::general_purpose::STANDARD, Engine};
use jsonwebtoken::Header;
use wasm_bindgen_test::wasm_bindgen_test;

static CERT_CHAIN: [&str; 3] = include!("cert_chain.json");

#[test]
#[wasm_bindgen_test]
fn x5c_der_empty_chain() {
let header = Header { x5c: None, ..Default::default() };
assert_eq!(header.x5c_der().unwrap(), None);
Expand All @@ -13,6 +15,7 @@ fn x5c_der_empty_chain() {
}

#[test]
#[wasm_bindgen_test]
fn x5c_der_valid_chain() {
let der_chain: Vec<Vec<u8>> =
CERT_CHAIN.iter().map(|x| STANDARD.decode(x)).collect::<Result<_, _>>().unwrap();
Expand All @@ -24,6 +27,7 @@ fn x5c_der_valid_chain() {
}

#[test]
#[wasm_bindgen_test]
fn x5c_der_invalid_chain() {
let mut x5c: Vec<_> = CERT_CHAIN.iter().map(ToString::to_string).collect();
x5c.push("invalid base64 data".to_string());
Expand Down
9 changes: 9 additions & 0 deletions tests/rsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use jsonwebtoken::{
Algorithm, DecodingKey, EncodingKey,
};
use serde::{Deserialize, Serialize};
use wasm_bindgen_test::wasm_bindgen_test;

#[cfg(feature = "use_pem")]
use jsonwebtoken::{decode, encode, Header, Validation};
Expand All @@ -27,7 +28,9 @@ pub struct Claims {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn round_trip_sign_verification_pem_pkcs1() {

let privkey_pem = include_bytes!("private_rsa_key_pkcs1.pem");
let pubkey_pem = include_bytes!("public_rsa_key_pkcs1.pem");
let certificate_pem = include_bytes!("certificate_rsa_key_pkcs1.crt");
Expand Down Expand Up @@ -58,6 +61,7 @@ fn round_trip_sign_verification_pem_pkcs1() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn round_trip_sign_verification_pem_pkcs8() {
let privkey_pem = include_bytes!("private_rsa_key_pkcs8.pem");
let pubkey_pem = include_bytes!("public_rsa_key_pkcs8.pem");
Expand Down Expand Up @@ -88,6 +92,7 @@ fn round_trip_sign_verification_pem_pkcs8() {
}

#[test]
#[wasm_bindgen_test]
fn round_trip_sign_verification_der() {
let privkey_der = include_bytes!("private_rsa_key.der");
let pubkey_der = include_bytes!("public_rsa_key.der");
Expand All @@ -103,6 +108,7 @@ fn round_trip_sign_verification_der() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn round_trip_claim() {
let my_claims = Claims {
sub: "b@b.com".to_string(),
Expand Down Expand Up @@ -139,6 +145,7 @@ fn round_trip_claim() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn rsa_modulus_exponent() {
let privkey = include_str!("private_rsa_key_pkcs1.pem");
let my_claims = Claims {
Expand All @@ -165,6 +172,7 @@ fn rsa_modulus_exponent() {

#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
fn rsa_jwk() {
use jsonwebtoken::jwk::Jwk;
use serde_json::json;
Expand Down Expand Up @@ -201,6 +209,7 @@ fn rsa_jwk() {
// https://jwt.io/ is often used for examples so ensure their example works with jsonwebtoken
#[cfg(feature = "use_pem")]
#[test]
#[wasm_bindgen_test]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to put that on every test? :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is unavoidable. Couldn't find aything about the reusability of the standard #[test] macro.

fn roundtrip_with_jwtio_example_jey() {
let privkey_pem = include_bytes!("private_jwtio.pem");
let pubkey_pem = include_bytes!("public_jwtio.pem");
Expand Down