Skip to content

Commit

Permalink
Add standard means to load test vector (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
Diane Huxley authored May 13, 2024
1 parent b0791f1 commit 268cc8c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
3 changes: 2 additions & 1 deletion crates/credentials/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ uuid = { version = "1.8.0", features = ["v4"] }
[dev-dependencies]
crypto = { path = "../crypto" }
serde_canonical_json = "1.0.0"
tokio = { version = "1.34.0", features = ["macros", "test-util"] }
tokio = { version = "1.34.0", features = ["macros", "test-util"] }
test-helpers = { path = "../test-helpers" }
31 changes: 7 additions & 24 deletions crates/credentials/src/presentation_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,47 +236,30 @@ impl JsonSchemaBuilder {
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use std::fs;

use test_helpers::TestVectorFile;

use super::PresentationDefinition;

#[derive(Debug, serde::Deserialize)]
struct SelectCredentialsVectorInput {
struct VectorInput {
#[serde(rename = "presentationDefinition")]
pub presentation_definition: PresentationDefinition,
#[serde(rename = "credentialJwts")]
pub credential_jwts: Vec<String>,
}

#[derive(Debug, serde::Deserialize)]
struct SelectCredentialsVectorOutput {
struct VectorOutput {
#[serde(rename = "selectedCredentials")]
pub selected_credentials: Vec<String>,
}

#[derive(Debug, serde::Deserialize)]
struct SelectCredentialsVector {
pub description: String,
pub input: SelectCredentialsVectorInput,
pub output: SelectCredentialsVectorOutput,
}

#[derive(Debug, serde::Deserialize)]
struct Vectors {
pub vectors: Vec<SelectCredentialsVector>,
}

fn load_json_fixture(file_path: &str) -> Vectors {
let data = fs::read_to_string(file_path).unwrap();
let json = serde_json::from_str(&data).unwrap();
json
}

#[test]
fn test_web5_spec_test_vectors() {
let json_path =
"../../web5-spec/test-vectors/presentation_exchange/select_credentials.json";
let vectors = load_json_fixture(json_path);
let path = "presentation_exchange/select_credentials.json";
let vectors: TestVectorFile<VectorInput, VectorOutput> =
TestVectorFile::load_from_path(path);

for vector in vectors.vectors {
let presentation_definition = vector.input.presentation_definition;
Expand Down
7 changes: 5 additions & 2 deletions crates/jws/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ mod tests {
&header,
&payload,
)
.unwrap() + "123";
.unwrap()
+ "123";

let result = CompactJws::verify(&compact_jws).await;

Expand Down Expand Up @@ -520,7 +521,9 @@ mod tests {
},
)
.expect("failed to create bearer did");
let invalid_key_id = invalid_bearer_did.document.verification_method[0].id.clone();
let invalid_key_id = invalid_bearer_did.document.verification_method[0]
.id
.clone();

let compact_jws = CompactJws::sign(
&invalid_bearer_did,
Expand Down
12 changes: 12 additions & 0 deletions crates/test-helpers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "test-helpers"
version = "0.1.0"
edition = "2021"
homepage.workspace = true
repository.workspace = true
license-file.workspace = true

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true }
29 changes: 29 additions & 0 deletions crates/test-helpers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::{fs, path::PathBuf};

use serde::de::DeserializeOwned;

#[derive(Debug, serde::Deserialize)]
pub struct TestVector<I, O> {
pub description: String,
pub input: I,
pub output: O,
}

#[derive(Debug, serde::Deserialize)]
pub struct TestVectorFile<I, O> {
pub description: String,
pub vectors: Vec<TestVector<I, O>>,
}

impl<I, O> TestVectorFile<I, O> {
pub fn load_from_path(file_path: &str) -> TestVectorFile<I, O>
where
I: DeserializeOwned,
O: DeserializeOwned,
{
let mut vector_path = PathBuf::from("../../web5-spec/test-vectors/");
vector_path.push(file_path);
let data = fs::read_to_string(vector_path).unwrap();
serde_json::from_str(&data).unwrap()
}
}

0 comments on commit 268cc8c

Please sign in to comment.