Skip to content

Test Suite v3 #151

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .config/nextest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Profile to only run `tests/jwk.rs` tests.
[profile.jwk]
default-filter = "binary_id(jose::jwk)"
169 changes: 169 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ eyre = "0.6.12"
clap = { version = "4.5.27", features = ["derive"] }
clio = { version = "0.3.5", features = ["clap-parse"] }

# For integration tests
pretty_assertions = "1.4.1"
rstest = "0.25.0"

# For X.509 certificates of Json Web Keys
# x509-cert = { git = "https://github.com/RustCrypto/formats.git" }

Expand All @@ -140,3 +144,6 @@ required-features = ["std"]
[package.metadata.docs.rs]
features = ["crypto-rustcrypto", "std", "deterministic-ecdsa"]
rustdoc-args = ["--cfg", "docsrs"]

[profile.dev.package.num-bigint-dig]
opt-level = 3
46 changes: 46 additions & 0 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
cargo-udeps
cargo-hack
cargo-nextest
cargo-tarpaulin

jose

openssl
pkg-config
Expand All @@ -30,4 +33,47 @@
"rust-src"
];
};

scripts = {
each-crypto.exec = ''
CRYPTO_FEATURES="$(cat Cargo.toml | grep -o 'crypto-[^ ]* =' | tr -d ' =' | paste -sd ',')"

cargo hack \
--each-feature \
--include-features $CRYPTO_FEATURES \
"$@"
'';

jwk-thumbprints.exec = ''
# write a script, that takes a list of files as arguments
# and calculates sha256, sha384 and sha512 thumbprints
# for each file, and prints the result in a table
# the script should be called jwk-thumbprints

files="$@"

if [ -z "$files" ]; then
echo "No files provided"
exit 1
fi

for file in $files; do
if [ ! -f "$file" ]; then
echo "$file is not a file"
continue
fi

echo "$file"

sha256=$(jose jwk thp -a S256 -i "$file")
sha384=$(jose jwk thp -a S384 -i "$file")
sha512=$(jose jwk thp -a S512 -i "$file")

echo -e "\tSHA256: $sha256"
echo -e "\tSHA384: $sha384"
echo -e "\tSHA512: $sha512"
echo -e "\n"
done
'';
};
}
6 changes: 3 additions & 3 deletions src/jwk/serde_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::vec::Vec;
use alloc::{borrow::Cow, vec::Vec};
use core::ops::Deref;

use base64ct::{Base64, Base64UrlUnpadded, Encoding};
Expand Down Expand Up @@ -53,10 +53,10 @@ pub fn deserialize_ga<'de, D, const N: usize>(deserializer: D) -> Result<Option<
where
D: Deserializer<'de>,
{
Ok(match Option::<&str>::deserialize(deserializer)? {
Ok(match Option::<Cow<'_, str>>::deserialize(deserializer)? {
Some(val) => {
let mut buf = [0u8; N];
Base64UrlUnpadded::decode(val, &mut buf).map_err(<D::Error as Error>::custom)?;
Base64UrlUnpadded::decode(&*val, &mut buf).map_err(<D::Error as Error>::custom)?;
Some(buf)
}
None => None,
Expand Down
16 changes: 16 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Common test helpers.

use serde_json::Value;

pub type TestResult<T = ()> = Result<T, Box<dyn std::error::Error>>;

/// Reads a key file from the `tests/vectors/jwk` directory.
pub fn read_jwk(name: &str) -> TestResult<Value> {
let json = std::fs::read_to_string(format!(
"{}/tests/vectors/jwk/{name}.json",
env!("CARGO_MANIFEST_DIR"),
))?;
let key: Value = serde_json::from_str(&json)?;

Ok(key)
}
Loading