Skip to content

Commit

Permalink
prost: clarify encoding test buffer copying
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbz00 committed Apr 2, 2024
1 parent 1ac740e commit ea5ae54
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,10 +1616,14 @@ mod test {

assert_eq!(encoded_len_varint(value), encoded.len());

let roundtrip_value = decode_varint(&mut &*encoded).expect("decoding failed");
// See: https://github.com/tokio-rs/prost/pull/1008 for copying reasoning.
let mut encoded_copy = encoded;
let roundtrip_value = decode_varint(&mut encoded_copy).expect("decoding failed");
assert_eq!(value, roundtrip_value);

let roundtrip_value = decode_varint_slow(&mut &*encoded).expect("slow decoding failed");
let mut encoded_copy = encoded;
let roundtrip_value =
decode_varint_slow(&mut encoded_copy).expect("slow decoding failed");
assert_eq!(value, roundtrip_value);
}

Expand Down Expand Up @@ -1678,14 +1682,18 @@ mod test {
);
}

const U64_MAX_PLUS_ONE: &[u8] = &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02];

#[test]
fn varint_overflow() {
let mut u64_max_plus_one: &[u8] =
&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02];
let mut copy = U64_MAX_PLUS_ONE;
decode_varint(&mut copy).expect_err("decoding u64::MAX + 1 succeeded");
}

decode_varint(&mut u64_max_plus_one).expect_err("decoding u64::MAX + 1 succeeded");
decode_varint_slow(&mut u64_max_plus_one)
.expect_err("slow decoding u64::MAX + 1 succeeded");
#[test]
fn variant_slow_overflow() {
let mut copy = U64_MAX_PLUS_ONE;
decode_varint_slow(&mut copy).expect_err("slow decoding u64::MAX + 1 succeeded");
}

/// This big bowl o' macro soup generates an encoding property test for each combination of map
Expand Down

0 comments on commit ea5ae54

Please sign in to comment.