Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 9 additions & 5 deletions src/uu/uuidgen/src/uuidgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn uu_app() -> Command {
.long(options::NAMESPACE)
.action(ArgAction::Set)
.value_parser(namespace_from_str)
.help("namespace for md5/sha1 - one of: @dns @url @oid @x500"),
.help("namespace for md5/sha1 - a UUID or one of: @dns @url @oid @x500"),
)
.arg(
Arg::new(options::NAME)
Expand Down Expand Up @@ -132,10 +132,14 @@ fn namespace_from_str(s: &str) -> Result<Uuid, USimpleError> {
"@url" => Ok(Uuid::NAMESPACE_URL),
"@oid" => Ok(Uuid::NAMESPACE_OID),
"@x500" => Ok(Uuid::NAMESPACE_X500),
_ => Err(USimpleError {
code: 1,
message: format!("Invalid namespace {s}."),
}),
// only the 36-character hyphenated form, like libuuid's uuid_parse()
_ => match Uuid::try_parse(s) {
Ok(uuid) if s.len() == 36 => Ok(uuid),
_ => Err(USimpleError {
code: 1,
message: format!("Invalid namespace {s}."),
}),
},
}
}

Expand Down
37 changes: 37 additions & 0 deletions tests/by-util/test_uuidgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,43 @@ fn test_md5_sha1() {
.stdout_only("34da942e-f4a3-5169-9c65-267d2b22cf11\n");
}

#[test]
fn test_uuid_namespace() {
// the @dns namespace written out, in both cases
new_ucmd!()
.args(&[
"-m",
"-n",
"6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"-N",
"a",
])
.succeeds()
.stdout_only("4c104dd0-4821-30d5-9ce3-0e7a1f8b7c0d\n");
new_ucmd!()
.args(&[
"-s",
"-n",
"6BA7B810-9DAD-11D1-80B4-00C04FD430C8",
"-N",
"a",
])
.succeeds()
.stdout_only("4f3f2898-69e3-5a0d-820a-c4e87987dbce\n");
for namespace in [
"{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
"6ba7b8109dad11d180b400c04fd430c8",
"urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"6ba7b810-9dad-11d1-80b4-00c04fd430c",
] {
new_ucmd!()
.args(&["-s", "-n", namespace, "-N", "a"])
.fails()
.code_is(1)
.stderr_contains(format!("Invalid namespace {namespace}."));
}
}

#[test]
fn test_invalid_arg() {
new_ucmd!().arg("-Z").fails().code_is(1);
Expand Down
Loading