Skip to content

Commit c8cc5d4

Browse files
committed
Consolidate constant pool UTF-8 parser tests
1 parent c07f1ca commit c8cc5d4

1 file changed

Lines changed: 6 additions & 41 deletions

File tree

‎classfile/src/constant_pool.rs‎

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl FieldMethodref {
290290

291291
#[cfg(test)]
292292
mod tests {
293-
use super::ConstantPoolItem;
293+
use super::{ConstantPoolItem, parse_utf8};
294294

295295
#[test]
296296
fn empty_constant_pool_is_valid() {
@@ -304,48 +304,13 @@ mod tests {
304304
fn long_must_fit_in_two_constant_pool_slots() {
305305
assert!(ConstantPoolItem::parse_all(&[0x00, 0x02, 0x05, 0, 0, 0, 0, 0, 0, 0, 0]).is_err());
306306
}
307-
}
308-
309-
#[cfg(test)]
310-
mod modified_utf8_tests {
311-
use super::parse_utf8;
312-
313-
fn constant(bytes: &[u8]) -> Option<alloc::string::String> {
314-
let mut data = (bytes.len() as u16).to_be_bytes().to_vec();
315-
data.extend_from_slice(bytes);
316-
parse_utf8(&data).ok().map(|(rest, x)| {
317-
assert!(rest.is_empty());
318-
(*x).clone()
319-
})
320-
}
321-
322-
#[test]
323-
fn nul_is_c0_80() {
324-
// javac's encoding of "MTR\0"
325-
assert_eq!(constant(b"MTR\xC0\x80").as_deref(), Some("MTR\0"));
326-
}
327307

328308
#[test]
329-
fn supplementary_is_a_surrogate_pair() {
330-
// javac's encoding of "\uD83D\uDC0D" (U+1F40D) — six bytes, two 3-byte halves
331-
assert_eq!(constant(b"a\xED\xA0\xBD\xED\xB0\x8Db").as_deref(), Some("a\u{1F40D}b"));
332-
}
309+
fn utf8_constant_decodes_length_delimited_modified_utf8() {
310+
let (remaining, value) = parse_utf8(b"\x00\x0b\xc0\x80\xed\xa0\xbd\xed\xb0\x8d\xed\xa0\xbd\xff").unwrap();
333311

334-
#[test]
335-
fn ascii_and_hangul_are_unchanged() {
336-
assert_eq!(constant(b"java/lang/Object").as_deref(), Some("java/lang/Object"));
337-
assert_eq!(constant("한글 5개".as_bytes()).as_deref(), Some("한글 5개"));
338-
assert_eq!(constant(b"").as_deref(), Some(""));
339-
}
340-
341-
#[test]
342-
fn malformed_input_and_unpaired_surrogates() {
343-
assert_eq!(constant(b"\xF0\x9F\x90\x8D"), None);
344-
// a lone surrogate half cannot live in a Rust String
345-
assert_eq!(constant(b"x\xED\xA0\xBD").as_deref(), Some("x\u{FFFD}"));
346-
// still malformed: truncated sequence, stray continuation byte, invalid lead byte
347-
assert_eq!(constant(b"\xC0"), None);
348-
assert_eq!(constant(b"\x80"), None);
349-
assert_eq!(constant(b"\xFF"), None);
312+
assert_eq!(value.as_str(), "\0\u{1f40d}\u{fffd}");
313+
assert_eq!(remaining, b"\xff");
314+
assert!(parse_utf8(b"\x00\x04\xf0\x9f\x90\x8d").is_err());
350315
}
351316
}

0 commit comments

Comments
 (0)