Skip to content

Commit

Permalink
Consolidate test vectors, add nextest filter
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe committed Aug 16, 2024
1 parent 298f2e6 commit 891405d
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 149 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ jobs:
- name: Run Rust Tests
run: |
mkdir -p test-results
cargo nextest run --profile ci --config-file ./nextest.toml
cargo nextest run --profile ci --config-file ./nextest.toml -E 'test(test_vectors::)'
- name: Modify testsuite name in XML for test runner consumption
run: |
sed -i '' 's/<testsuite name="web5"/<testsuite name="Web5TestVectorsProtocolTest"/' target/nextest/ci/junit.xml
Expand All @@ -252,4 +252,4 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: rust-test-results
path: test-results/rust-test-results.xml
path: test-results/rust-test-results.xml
102 changes: 51 additions & 51 deletions crates/web5/src/credentials/presentation_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,54 +239,54 @@ impl JsonSchemaBuilder {
}
}

#[cfg(test)]
mod tests {
use std::collections::HashSet;

use crate::test_helpers::TestVectorFile;

use super::PresentationDefinition;

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

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

#[test]
#[ignore] // TODO temporarily ignoring, because web5-spec test vectors use did:key which isn't supported
fn test_web5_spec_test_vectors() {
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;
let vc_jwts = vector.input.credential_jwts;
let error_msg = format!(
"Selected Credential test vector ({}) should not have thrown error",
vector.description
);

let selected_credentials = presentation_definition
.select_credentials(&vc_jwts)
.expect(&error_msg);

let set1: HashSet<_> = selected_credentials.iter().collect();
let set2: HashSet<_> = vector.output.selected_credentials.iter().collect();
assert_eq!(
set1, set2,
"Vectors do not contain the same elements: {}",
error_msg
);
}
}
}
// #[cfg(test)]
// mod test_vectors {
// use std::collections::HashSet;

// use crate::test_helpers::TestVectorFile;

// use super::PresentationDefinition;

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

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

// #[test]
// #[ignore] // TODO temporarily ignoring, because web5-spec test vectors use did:key which isn't supported
// fn test_web5_spec_test_vectors() {
// 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;
// let vc_jwts = vector.input.credential_jwts;
// let error_msg = format!(
// "Selected Credential test vector ({}) should not have thrown error",
// vector.description
// );

// let selected_credentials = presentation_definition
// .select_credentials(&vc_jwts)
// .expect(&error_msg);

// let set1: HashSet<_> = selected_credentials.iter().collect();
// let set2: HashSet<_> = vector.output.selected_credentials.iter().collect();
// assert_eq!(
// set1, set2,
// "Vectors do not contain the same elements: {}",
// error_msg
// );
// }
// }
// }
134 changes: 67 additions & 67 deletions crates/web5/src/dids/methods/did_jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,70 +116,70 @@ impl DidJwk {
}
}

#[cfg(test)]
mod tests {
use crate::{
dids::{
data_model::document::Document,
resolution::{
document_metadata::DocumentMetadata, resolution_metadata::ResolutionMetadata,
},
},
test_helpers::TestVectorFile,
};

#[derive(Debug, PartialEq, serde::Deserialize)]
struct VectorOutput {
#[serde(rename = "@context")]
context: String,
#[serde(rename = "didDocument")]
did_document: Option<Document>,
#[serde(rename = "didDocumentMetadata")]
did_document_metadata: DocumentMetadata,
#[serde(rename = "didResolutionMetadata")]
did_resolution_metadata: ResolutionMetadata,
}

#[test]
fn test_web5_spec_did_jwk_test_vectors() {
let path = "did_jwk/resolve.json";
let vectors: TestVectorFile<String, VectorOutput> = TestVectorFile::load_from_path(path);

for vector in vectors.vectors {
let did_uri = vector.input;
let resolution_result = super::DidJwk::resolve(&did_uri);

let all_none = vector.output.did_document_metadata.created.is_none()
&& vector.output.did_document_metadata.updated.is_none()
&& vector.output.did_document_metadata.deactivated.is_none()
&& vector.output.did_document_metadata.next_update.is_none()
&& vector.output.did_document_metadata.version_id.is_none()
&& vector
.output
.did_document_metadata
.next_version_id
.is_none()
&& vector.output.did_document_metadata.equivalent_id.is_none()
&& vector.output.did_document_metadata.canonical_id.is_none();

let vector_document_metadata = if all_none {
None
} else {
Some(vector.output.did_document_metadata.clone())
};

assert_eq!(
resolution_result.resolution_metadata, vector.output.did_resolution_metadata,
"Resolution metadata does not match."
);
assert_eq!(
resolution_result.document, vector.output.did_document,
"DID Document does not match."
);
assert_eq!(
resolution_result.document_metadata, vector_document_metadata,
"Document metadata does not match."
);
}
}
}
// #[cfg(test)]
// mod test_vectors {
// use crate::{
// dids::{
// data_model::document::Document,
// resolution::{
// document_metadata::DocumentMetadata, resolution_metadata::ResolutionMetadata,
// },
// },
// test_helpers::TestVectorFile,
// };

// #[derive(Debug, PartialEq, serde::Deserialize)]
// struct VectorOutput {
// #[serde(rename = "@context")]
// context: String,
// #[serde(rename = "didDocument")]
// did_document: Option<Document>,
// #[serde(rename = "didDocumentMetadata")]
// did_document_metadata: DocumentMetadata,
// #[serde(rename = "didResolutionMetadata")]
// did_resolution_metadata: ResolutionMetadata,
// }

// #[test]
// fn test_web5_spec_did_jwk_test_vectors() {
// let path = "did_jwk/resolve.json";
// let vectors: TestVectorFile<String, VectorOutput> = TestVectorFile::load_from_path(path);

// for vector in vectors.vectors {
// let did_uri = vector.input;
// let resolution_result = super::DidJwk::resolve(&did_uri);

// let all_none = vector.output.did_document_metadata.created.is_none()
// && vector.output.did_document_metadata.updated.is_none()
// && vector.output.did_document_metadata.deactivated.is_none()
// && vector.output.did_document_metadata.next_update.is_none()
// && vector.output.did_document_metadata.version_id.is_none()
// && vector
// .output
// .did_document_metadata
// .next_version_id
// .is_none()
// && vector.output.did_document_metadata.equivalent_id.is_none()
// && vector.output.did_document_metadata.canonical_id.is_none();

// let vector_document_metadata = if all_none {
// None
// } else {
// Some(vector.output.did_document_metadata.clone())
// };

// assert_eq!(
// resolution_result.resolution_metadata, vector.output.did_resolution_metadata,
// "Resolution metadata does not match."
// );
// assert_eq!(
// resolution_result.document, vector.output.did_document,
// "DID Document does not match."
// );
// assert_eq!(
// resolution_result.document_metadata, vector_document_metadata,
// "Document metadata does not match."
// );
// }
// }
// }
2 changes: 2 additions & 0 deletions crates/web5/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pub mod rfc3339;

#[cfg(test)]
mod test_helpers;
#[cfg(test)]
mod test_vectors;
30 changes: 1 addition & 29 deletions crates/web5/src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,4 @@
use serde::de::DeserializeOwned;
use std::{fs, path::PathBuf, sync::Mutex};

#[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> {
#[allow(dead_code)]
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()
}
}
use std::{fs, sync::Mutex};

#[macro_export]
macro_rules! test_name {
Expand Down
Loading

0 comments on commit 891405d

Please sign in to comment.