mirror of
https://github.com/sudosylabs/vnidrop.git
synced 2026-08-05 02:29:55 +02:00
Improve ticket copy and parsing
This commit is contained in:
@@ -52,6 +52,26 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_ticket_round_trip_tolerates_wrapped_whitespace() {
|
||||
let secret = SecretKey::generate();
|
||||
let addr = iroh::EndpointAddr::new(secret.public());
|
||||
let blob_ticket = BlobTicket::new(addr, Hash::new([9; 32]), BlobFormat::HashSeq);
|
||||
let metadata = TransferMetadata::new(7, "Wrapped", None, blob_ticket.hash(), 1, 10);
|
||||
let encoded = VnidropTicket::new(blob_ticket.clone(), metadata)
|
||||
.encode()
|
||||
.unwrap();
|
||||
let wrapped = encoded
|
||||
.as_bytes()
|
||||
.chunks(8)
|
||||
.map(|chunk| std::str::from_utf8(chunk).unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n ");
|
||||
|
||||
let parsed = parse_transfer_ticket(&wrapped).unwrap();
|
||||
assert_eq!(parsed.blob_ticket.hash(), blob_ticket.hash());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_ticket_is_rejected() {
|
||||
assert!(parse_transfer_ticket("not-a-ticket").is_err());
|
||||
|
||||
@@ -34,7 +34,8 @@ impl VnidropTicket {
|
||||
}
|
||||
|
||||
fn decode(value: &str) -> Result<Self> {
|
||||
let encoded = value
|
||||
let normalized = normalize_ticket_input(value);
|
||||
let encoded = normalized
|
||||
.strip_prefix(VNIDROP_TICKET_PREFIX)
|
||||
.context("not a VniDrop ticket")?;
|
||||
let bytes = BASE64URL_NOPAD
|
||||
@@ -51,8 +52,9 @@ pub(crate) struct ParsedTransferTicket {
|
||||
}
|
||||
|
||||
pub(crate) fn parse_transfer_ticket(value: &str) -> Result<ParsedTransferTicket> {
|
||||
if value.starts_with(VNIDROP_TICKET_PREFIX) {
|
||||
let ticket = VnidropTicket::decode(value)?;
|
||||
let normalized = normalize_ticket_input(value);
|
||||
if normalized.starts_with(VNIDROP_TICKET_PREFIX) {
|
||||
let ticket = VnidropTicket::decode(&normalized)?;
|
||||
let blob_ticket = BlobTicket::from_str(&ticket.blob_ticket)
|
||||
.context("invalid BlobTicket inside VniDrop ticket")?;
|
||||
return Ok(ParsedTransferTicket {
|
||||
@@ -61,9 +63,13 @@ pub(crate) fn parse_transfer_ticket(value: &str) -> Result<ParsedTransferTicket>
|
||||
});
|
||||
}
|
||||
|
||||
let blob_ticket = BlobTicket::from_str(value).context("invalid BlobTicket")?;
|
||||
let blob_ticket = BlobTicket::from_str(&normalized).context("invalid BlobTicket")?;
|
||||
Ok(ParsedTransferTicket {
|
||||
blob_ticket,
|
||||
metadata: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn normalize_ticket_input(value: &str) -> String {
|
||||
value.chars().filter(|char| !char.is_whitespace()).collect()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user