Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 committed Sep 23, 2024
1 parent 0c1364e commit 8ba37a1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
24 changes: 6 additions & 18 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,7 @@ mod test {
fn returns_an_error_for_nonexistant_object_paths() {
let result = SdJwtBuilder::new(json!({})).unwrap().make_concealable("/email");

assert_eq!(
result.unwrap_err(),
Error::InvalidPath("email does not exist".to_string()),
);
assert_eq!(result.unwrap_err(), Error::InvalidPath("/email".to_string()),);
}

#[test]
Expand All @@ -252,10 +249,7 @@ mod test {
.unwrap()
.make_concealable("/nationalities/0");

assert_eq!(
result.unwrap_err(),
Error::InvalidPath("nationalities/0 does not exist".to_string()),
);
assert_eq!(result.unwrap_err(), Error::InvalidPath("/nationalities/0".to_string()),);
}

#[test]
Expand All @@ -266,10 +260,7 @@ mod test {
.unwrap()
.make_concealable("/nationalities/2");

assert_eq!(
result.unwrap_err(),
Error::InvalidPath("nationalities/2 does not exist".to_string()),
);
assert_eq!(result.unwrap_err(), Error::InvalidPath("/nationalities/2".to_string()),);
}
}

Expand All @@ -284,10 +275,7 @@ mod test {
.unwrap()
.make_concealable("/address/region");

assert_eq!(
result.unwrap_err(),
Error::InvalidPath("address/region does not exist".to_string()),
);
assert_eq!(result.unwrap_err(), Error::InvalidPath("/address/region".to_string()),);
}

#[test]
Expand All @@ -300,7 +288,7 @@ mod test {

assert_eq!(
result.unwrap_err(),
Error::InvalidPath("address/contact_person/2 does not exist".to_string()),
Error::InvalidPath("/address/contact_person/2".to_string()),
);
}

Expand All @@ -314,7 +302,7 @@ mod test {

assert_eq!(
result.unwrap_err(),
Error::InvalidPath("address/contact_person/2 does not exist".to_string()),
Error::InvalidPath("/address/contact_person/2".to_string()),
);
}
}
Expand Down
29 changes: 13 additions & 16 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,30 @@ impl<H: Hasher> SdObjectEncoder<H> {

let element_pointer = path
.parse::<JsonPointer<_, _>>()
.map_err(|err| Error::InvalidPath(format!("{:?}", err)))?;
.map_err(|_| Error::InvalidPath(path.to_string()))?;

let mut parent_pointer = element_pointer.clone();
let element_key = parent_pointer
.pop()
.ok_or(Error::InvalidPath("path does not contain any values".to_string()))?;
let element_key = parent_pointer.pop().ok_or(Error::InvalidPath(path.to_string()))?;

let parent = parent_pointer
.get(&self.object)
.map_err(|err| Error::InvalidPath(format!("{:?}", err)))?;
.map_err(|_| Error::InvalidPath(path.to_string()))?;

match parent {
Value::Object(_) => {
let parent = parent_pointer
.get_mut(&mut self.object)
.map_err(|err| Error::InvalidPath(format!("{:?}", err)))?
.map_err(|_| Error::InvalidPath(path.to_string()))?
.as_object_mut()
.ok_or(Error::InvalidPath("path does not contain any values".to_string()))?;
.ok_or(Error::InvalidPath(path.to_string()))?;

// Remove the value from the parent and create a disclosure for it.
let disclosure = Disclosure::new(
salt,
Some(element_key.to_owned()),
parent
.remove(&element_key)
.ok_or(Error::InvalidPath(format!("{} does not exist", element_key)))?,
.ok_or(Error::InvalidPath(path.to_string()))?,
);

// Hash the disclosure.
Expand All @@ -106,7 +104,9 @@ impl<H: Hasher> SdObjectEncoder<H> {
Ok(disclosure)
}
Value::Array(_) => {
let element = element_pointer.get_mut(&mut self.object).unwrap();
let element = element_pointer
.get_mut(&mut self.object)
.map_err(|_| Error::InvalidPath(path.to_string()))?;
let disclosure = Disclosure::new(salt, None, element.clone());
let hash = self.hasher.encoded_digest(disclosure.as_str());
let tripledot = json!({ARRAY_DIGEST_KEY: hash});
Expand Down Expand Up @@ -144,13 +144,13 @@ impl<H: Hasher> SdObjectEncoder<H> {
}

fn add_decoy(&mut self, path: &str) -> Result<()> {
let mut element_pointer = path
let element_pointer = path
.parse::<JsonPointer<_, _>>()
.map_err(|err| Error::InvalidPath(format!("{:?}", err)))?;
.map_err(|_| Error::InvalidPath(path.to_string()))?;

let value = element_pointer
.get_mut(&mut self.object)
.map_err(|err| Error::InvalidPath(format!("{:?}", err)))?;
.map_err(|_| Error::InvalidPath(path.to_string()))?;
if let Some(object) = value.as_object_mut() {
let (_, hash) = Self::random_digest(&self.hasher, self.salt_size, false);
Self::add_digest_to_object(object, hash)?;
Expand All @@ -161,10 +161,7 @@ impl<H: Hasher> SdObjectEncoder<H> {
array.push(tripledot);
Ok(())
} else {
Err(Error::InvalidPath(format!(
"{:?} is neither an object nor an array",
element_pointer.pop()
)))
Err(Error::InvalidPath(path.to_string()))
}
}

Expand Down

0 comments on commit 8ba37a1

Please sign in to comment.