Skip to content
Merged
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
34 changes: 33 additions & 1 deletion pkgs/id/src/tag_claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn is_tag_kind(kind: &str) -> bool {
///
/// Returns [`TagError::KindMismatch`] when the operation is not the one `kind` names. Returns
/// [`TagError::Field`] for a name or value that breaks its limit or the NFC rule, and for a clear
/// that names more than [`MAX_CLEAR_CLAIMS`] claims.
/// that names more than [`MAX_CLEAR_CLAIMS`] claims or names the same claim twice.
pub fn check_op(kind: &str, op: &TagOp) -> Result<(), TagError> {
match (kind, op) {
(SET_KIND, TagOp::Set { name, value, .. }) => {
Expand All @@ -136,6 +136,10 @@ pub fn check_op(kind: &str, op: &TagOp) -> Result<(), TagError> {
claims.len()
)));
}
let mut seen = HashSet::with_capacity(claims.len());
if !claims.iter().all(|claim| seen.insert(claim)) {
return Err(TagError::Field("a clear names a claim twice".to_owned()));
}
Ok(())
}
_ => Err(TagError::KindMismatch {
Expand Down Expand Up @@ -382,6 +386,34 @@ mod tests {
assert!(check("a", Some("e\u{301}".to_owned())).is_err());
}

#[test]
fn test_a_clear_naming_a_claim_twice_is_refused() {
let adjacent = TagOp::Clear {
claims: vec![hash(1), hash(1)],
};
assert!(matches!(
check_op(CLEAR_KIND, &adjacent),
Err(TagError::Field(_))
));
let non_adjacent = TagOp::Clear {
claims: vec![hash(1), hash(2), hash(1)],
};
assert!(matches!(
check_op(CLEAR_KIND, &non_adjacent),
Err(TagError::Field(_))
));
}

#[test]
fn test_a_clear_naming_max_distinct_claims_is_accepted() {
let op = TagOp::Clear {
claims: (0..MAX_CLEAR_CLAIMS)
.map(|n| Hash::digest(n.to_be_bytes()))
.collect(),
};
assert!(check_op(CLEAR_KIND, &op).is_ok());
}

#[test]
fn test_a_clear_naming_too_many_claims_is_refused() {
let op = TagOp::Clear {
Expand Down
Loading