Skip to content

Commit

Permalink
RUST-647 Sync spec tests for invalid relaxed UUIDs (crossterm-rs#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelatkinson authored Jan 29, 2021
1 parent 503eaa4 commit d8d8ebe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
29 changes: 4 additions & 25 deletions src/extjson/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,42 +173,21 @@ pub(crate) struct Uuid {
}

impl Uuid {
pub(crate) fn parse(mut self) -> extjson::de::Result<crate::Binary> {
if !valid_uuid_format(&self.value) {
return Err(extjson::de::Error::invalid_value(
Unexpected::Str(&self.value),
&"$uuid values does not follow RFC 4122 format regarding length and hyphens",
));
}

self.value.retain(|c| c != '-');

let bytes = hex::decode(&self.value).map_err(|_| {
pub(crate) fn parse(self) -> extjson::de::Result<crate::Binary> {
let uuid = uuid::Uuid::parse_str(&self.value).map_err(|_| {
extjson::de::Error::invalid_value(
Unexpected::Str(&self.value),
&"$uuid does not follow RFC 4122 format regarding hex bytes",
&"$uuid value does not follow RFC 4122 format regarding length and hyphens",
)
})?;

Ok(crate::Binary {
subtype: BinarySubtype::Uuid,
bytes,
bytes: uuid.as_bytes().to_vec(),
})
}
}

fn valid_uuid_format(s: &str) -> bool {
// RFC 4122 defines the hyphens in a UUID as appearing in the 8th, 13th, 18th, and 23rd
// characters.
//
// See https://tools.ietf.org/html/rfc4122#section-3
s.chars().count() == 36
&& s.chars().nth(8) == Some('-')
&& s.chars().nth(13) == Some('-')
&& s.chars().nth(18) == Some('-')
&& s.chars().nth(23) == Some('-')
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct JavaScriptCodeWithScope {
Expand Down
14 changes: 13 additions & 1 deletion src/tests/spec/json/bson-corpus/binary.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,20 @@
"string": "{\"x\" : { \"$uuid\" : { \"data\" : \"73ffd264-44b3-4c69-90e8-e7d1dfc035d4\"}}}"
},
{
"description": "$uuid invalid value",
"description": "$uuid invalid value--too short",
"string": "{\"x\" : { \"$uuid\" : \"73ffd264-44b3-90e8-e7d1dfc035d4\"}}"
},
{
"description": "$uuid invalid value--too long",
"string": "{\"x\" : { \"$uuid\" : \"73ffd264-44b3-4c69-90e8-e7d1dfc035d4-789e4\"}}"
},
{
"description": "$uuid invalid value--misplaced hyphens",
"string": "{\"x\" : { \"$uuid\" : \"73ff-d26444b-34c6-990e8e-7d1dfc035d4\"}}"
},
{
"description": "$uuid invalid value--too many hyphens",
"string": "{\"x\" : { \"$uuid\" : \"----d264-44b3-4--9-90e8-e7d1dfc0----\"}}"
}
]
}

0 comments on commit d8d8ebe

Please sign in to comment.