Files
craft-libs/crates/sourcemap/tests/entry_budgets.rs
T
dcda149a8a Add bounded Source Map v3 parser and lookup core (#2037)
* sourcemap: Craft Anything

Port parser/model/lookup core from Craft Anything f64ae3bf3c8863a982af1304e06de20653803968. Annotation and data URL helpers stay app-owned.

* sourcemap: Format parsers refuse or flag input they used to half-read (#652)

Port parser/model/lookup core from Craft Anything 15de36614b2d1039ed68bffcc36e0e92053b3f12. Annotation and data URL helpers stay app-owned.

* sourcemap: Keep source lookups inside their declared index map section (R3.12) (#1130)

Port parser/model/lookup core from Craft Anything a205f1d1571cbce001c546f4753943a569d7a7fe. Annotation and data URL helpers stay app-owned.

* sourcemap: Decode source map position deltas within the format's 32-bit range (R3.12) (#1133)

Port parser/model/lookup core from Craft Anything f5aad102b048cb6f01a8a7b90dcf2d7a7a395567. Annotation and data URL helpers stay app-owned.

* sourcemap: Look up source mappings without scanning every segment on a generated line (R3.12) (#1139)

Port parser/model/lookup core from Craft Anything 1fab01712915e744ea042154ff81013a19c9bfc5. Annotation and data URL helpers stay app-owned.

* sourcemap: Preserve source map URL roots when joining relative sources (R3.12) (#1332)

Port parser/model/lookup core from Craft Anything 7fd244005da7a37a6389ac09643fb759420f72a2. Annotation and data URL helpers stay app-owned.

* sourcemap: Bound generated source-map lines across inline sections (R3.12) (#1533)

Port parser/model/lookup core from Craft Anything 68ba5239a85c7f6e842c2a7a9c0254f125eaa917. Annotation and data URL helpers stay app-owned.

* sourcemap: Reject source map mappings with missing source or name entries (R3.12) (#1526)

Port parser/model/lookup core from Craft Anything 0ba60eb4e24d1c280db93304ee53a79393f41610. Annotation and data URL helpers stay app-owned.

* sourcemap: Apply source map root prefixes before interpreting source paths (R3.12) (#1680)

Port parser/model/lookup core from Craft Anything 500a09a217b9a6ce03b2c93035167ce983a83897. Annotation and data URL helpers stay app-owned.

* sourcemap: Bound expanded source-map source lists before building them (R3.12) (#1689)

Port parser/model/lookup core from Craft Anything 22c44d01d271c49187104802459085f273f855f0. Annotation and data URL helpers stay app-owned.

* sourcemap: Refuse source maps with more entries than the parser keeps (R3.12) (#1703)

Port parser/model/lookup core from Craft Anything ba83da2bc567611d0c8aef124c2c3d1ba03ade58. Annotation and data URL helpers stay app-owned.

* sourcemap: Count source-map sections, check skipped values and keep refused maps' sources uncertain (R3.12) (#1705)

Port parser/model/lookup core from Craft Anything ab79ec87a4da58d8965b6d4f95b751464ba6e999. Annotation and data URL helpers stay app-owned.

* sourcemap: Keep the sources of a source map refused for bad mappings or section offsets (R3.12) (#1717)

Port parser/model/lookup core from Craft Anything 12aad97757d692e8dbc8af03ceb60c6b44f53339. Annotation and data URL helpers stay app-owned.

* sourcemap: Mark sources of unread external source-map sections uncertain (R3.12) (#1767) (#1767)

Port parser/model/lookup core from Craft Anything d56fa23860f1fd20249fb945e94fdf3fcfc83a2d. Annotation and data URL helpers stay app-owned.

* sourcemap: List sources from the section maps of an indexed source map (R3.12)

Port parser/model/lookup core from Craft Anything 0b1a643944e119bc4d580de94b287ab257ab91c5. Annotation and data URL helpers stay app-owned.

* sourcemap: retain entry_budgets regression coverage

Port crates/formats/tests/sourcemap_entry_budgets.rs from Craft Anything, latest source change 12aad97757d692e8dbc8af03ceb60c6b44f53339. Only crate imports adapted.

* sourcemap: retain external_sections regression coverage

Port crates/formats/tests/sourcemap_external_sections.rs from Craft Anything, latest source change d56fa23860f1fd20249fb945e94fdf3fcfc83a2d. Only crate imports adapted.

* sourcemap: retain inventory_budgets regression coverage

Port crates/formats/tests/sourcemap_inventory_budgets.rs from Craft Anything, latest source change 22c44d01d271c49187104802459085f273f855f0. Only crate imports adapted.

* sourcemap: retain line_budgets regression coverage

Port crates/formats/tests/sourcemap_line_budgets.rs from Craft Anything, latest source change 68ba5239a85c7f6e842c2a7a9c0254f125eaa917. Only crate imports adapted.

* sourcemap: retain root_prefix regression coverage

Port crates/formats/tests/sourcemap_root_prefix.rs from Craft Anything, latest source change 500a09a217b9a6ce03b2c93035167ce983a83897. Only crate imports adapted.

* sourcemap: retain section_columns regression coverage

Port crates/formats/tests/sourcemap_section_columns.rs from Craft Anything, latest source change 15de36614b2d1039ed68bffcc36e0e92053b3f12. Only crate imports adapted.

* Expose bounded Source Map v3 core with retained regression tests

---------

Co-authored-by: Brandon Thomas <echelon@gmail.com>
Co-authored-by: BFlat <bombayapirat@gmail.com>
Co-authored-by: Afloat <ShuffleMonkey@163.com>
Co-authored-by: RRFZ <247457381+Plastic-time@users.noreply.github.com>
Co-authored-by: Nicolas Bohn <dev@nicolasbohn.de>
Co-authored-by: Alex Joaquim Pereira <alexpereira1781@gmail.com>
2026-11-17 14:13:02 -05:00

146 lines
8.5 KiB
Rust

use craft_sourcemap::{Limits, SourceMap, SourceMapError, parse};
use serde_json::json;
fn with(max_entries: usize) -> Limits {
Limits { max_entries, ..Limits::default() }
}
fn decode(value: serde_json::Value, max_entries: usize) -> Result<SourceMap, SourceMapError> {
parse(&serde_json::to_vec(&value).unwrap(), with(max_entries))
}
#[test]
fn entries_up_to_the_limit_are_kept() {
let value = json!({"version":3,"sources":["a.js","b.js"],"sourcesContent":["a();",null],"names":["x"],"mappings":"AAAAA,CCAA"});
let map = decode(value.clone(), 5).unwrap();
assert_eq!(map.sources, vec![Some("a.js".to_string()), Some("b.js".to_string())]);
assert_eq!(map.sources_content, vec![Some("a();".to_string()), None]);
assert_eq!(map.names, vec!["x".to_string()]);
assert_eq!(decode(value, 4), Err(SourceMapError::TooManyEntries(4)));
}
#[test]
fn every_kind_of_entry_counts_once() {
for value in [
json!({"version":3,"sources":["a","b","c"],"mappings":""}),
json!({"version":3,"sourcesContent":["a","b","c"],"mappings":""}),
json!({"version":3,"names":["a","b","c"],"mappings":""}),
json!({"version":3,"ignoreList":[0,1,2],"mappings":""}),
json!({"version":3,"x_google_ignoreList":[0,1,2],"mappings":""}),
// Entries that are not strings still take a slot each.
json!({"version":3,"sources":[null, 1, {"a":[1,2,3]}],"mappings":""}),
] {
assert_eq!(decode(value.clone(), 2), Err(SourceMapError::TooManyEntries(2)), "{value}");
assert!(decode(value.clone(), 3).is_ok(), "{value}");
}
}
#[test]
fn entries_a_repeated_key_replaces_give_their_slots_back() {
let json = br#"{"version":3,"mappings":"","sources":["a","b"],"sources":["c"],"names":["x"],"names":[]}"#;
let map = parse(json, with(2)).unwrap();
assert_eq!(map.sources, vec![Some("c".to_string())]);
assert!(map.names.is_empty());
// `ignoreList` is kept over `x_google_ignoreList`, before or after it.
let first = br#"{"version":3,"mappings":"","x_google_ignoreList":[0,0],"ignoreList":[0]}"#;
assert_eq!(parse(first, with(2)).unwrap().ignore_list, vec![0]);
let after = br#"{"version":3,"mappings":"","ignoreList":[0],"x_google_ignoreList":[0,0,0]}"#;
assert_eq!(parse(after, with(1)).unwrap().ignore_list, vec![0]);
// A replaced sections array gives back its sections and what their maps held: without that,
// the second array would need slots four and five.
let sections = br#"{"version":3,"sections":[{"offset":{"line":0,"column":0},"map":{"version":3,"sources":["a"],"mappings":""}},{"offset":{"line":1,"column":0},"url":"b.map"}],"sections":[{"offset":{"line":0,"column":0},"url":"c.map"}]}"#;
assert_eq!(parse(sections, with(3)).unwrap().sections.len(), 1);
// So does a section's replaced map: one section, then one source instead of two.
let map = br#"{"version":3,"sections":[{"offset":{"line":0,"column":0},"map":{"version":3,"sources":["a","b"],"mappings":""},"map":{"version":3,"sources":["c"],"mappings":""}}]}"#;
assert_eq!(parse(map, with(3)).unwrap().sections[0].2.sources, vec![Some("c".to_string())]);
}
#[test]
fn sections_and_their_maps_share_the_budget() {
let section = |line: u32, name: &str| json!({"offset":{"line":line,"column":0},"map":{"version":3,"sources":[name],"mappings":"AAAA"}});
let value = json!({"version":3,"sections":[section(0, "a.js"), section(1, "b.js")]});
// Two sections and one source in each.
assert_eq!(decode(value.clone(), 4).unwrap().sections.len(), 2);
assert_eq!(decode(value, 3), Err(SourceMapError::TooManyEntries(3)));
// Sections without a map count too, so a long list of them is refused early.
let external = json!({"offset":{"line":0,"column":0},"url":"a.map"});
let value = json!({"version":3,"sections":[external.clone(), external.clone(), external]});
assert_eq!(decode(value.clone(), 3).unwrap().sections.len(), 3);
assert_eq!(decode(value, 2), Err(SourceMapError::TooManyEntries(2)));
let not_sections = br#"{"version":3,"sections":[0,0,0]}"#;
assert_eq!(parse(not_sections, with(2)), Err(SourceMapError::TooManyEntries(2)));
assert_eq!(parse(not_sections, with(3)), Err(SourceMapError::BadSections));
}
#[test]
fn reading_stops_at_the_limit() {
// Everything after the entry that goes over the limit is never read: the broken tail would
// otherwise be reported as bad JSON.
let json = br#"{"version":3,"sources":["a","b","c","d" this is not JSON"#;
assert_eq!(parse(json, with(2)), Err(SourceMapError::TooManyEntries(2)));
assert!(matches!(parse(json, Limits::default()), Err(SourceMapError::BadJson(_))));
}
#[test]
fn unknown_fields_are_skipped_without_counting() {
let value = json!({"version":3,"x_vendor":[1,2,3,4,5,6,7,8],"sources":["a.js"],"mappings":"AAAA"});
assert_eq!(decode(value, 1).unwrap().sources.len(), 1);
}
#[test]
fn values_the_map_does_not_keep_are_still_checked() {
let deep = format!("{{\"version\":3,\"mappings\":\"\",\"x\":{}{}}}", "[".repeat(200), "]".repeat(200));
for json in [
br#"{"version":3,"mappings":"","x":"\ud800"}"#.as_slice(),
br#"{"version":3,"mappings":"","sources":{"a":"\udc00"}}"#,
br#"{"version":3,"mappings":"","x":1e400}"#,
br#"{"version":3,"mappings":"","sources":[{"a":1e400}]}"#,
b"{\"version\":3,\"mappings\":\"\",\"x\":\"\xff\"}",
br#"{"version":3,"mappings":"","x":[1,]}"#,
deep.as_bytes(),
] {
let got = parse(json, Limits::default());
let full = serde_json::from_slice::<serde_json::Value>(json).unwrap_err().to_string();
assert_eq!(got, Err(SourceMapError::BadJson(full)), "{}", String::from_utf8_lossy(json));
}
}
#[test]
fn a_hostile_map_of_empty_names_is_refused_by_default() {
// About 21 million empty strings fit in the 64 MiB a map may have.
let limit = Limits::default().max_entries;
let names = vec!["\"\""; limit + 1].join(",");
let json = format!("{{\"version\":3,\"mappings\":\"\",\"names\":[{names}]}}").into_bytes();
assert_eq!(parse(&json, Limits::default()), Err(SourceMapError::TooManyEntries(limit)));
// The same map is well formed: with room for every name it reads.
assert_eq!(parse(&json, with(limit + 1)).unwrap().names.len(), limit + 1);
}
#[test]
fn sources_are_read_from_a_map_with_broken_mappings_or_offsets() {
use craft_sourcemap::parse_sources;
let bad = br#"{"version":3,"sourceRoot":"src","sources":["a.js"],"sourcesContent":["a();"],"names":["x"],"mappings":"!!"}"#;
assert_eq!(parse(bad, Limits::default()), Err(SourceMapError::BadMappings(1)));
let map = parse_sources(bad, Limits::default()).unwrap();
assert_eq!((map.source_path(0).as_deref(), map.sources_content.len(), map.names.len(), map.lines.len()), (Some("src/a.js"), 1, 1, 0));
let unordered = br#"{"version":3,"sections":[{"offset":{"line":2,"column":0},"map":{"version":3,"sources":["a.js"],"mappings":"AAAA"}},{"offset":{"line":-1,"column":0},"map":{"version":3,"sources":["b.js"],"mappings":""}},{"offset":{"line":0,"column":0},"map":{"version":3,"sources":["c.js"],"mappings":"!"}}]}"#;
assert_eq!(parse(unordered, Limits::default()), Err(SourceMapError::BadSections));
let map = parse_sources(unordered, Limits::default()).unwrap();
assert_eq!(map.source_inventory(Default::default()).sources, vec!["a.js".to_string(), "b.js".to_string(), "c.js".to_string()]);
// Past mappings that can't be decoded, the indices aren't known.
assert_eq!(parse_sources(br#"{"version":3,"sources":["a"],"mappings":"AAAA,!,ACAA"}"#, Limits::default()).unwrap().sources.len(), 1);
// What a section holds must still be known, and the version and limits still apply.
for (json, limit, err) in [
(br#"{"version":3,"sections":[{"offset":{"line":0,"column":0}}]}"#.as_slice(), 9, SourceMapError::BadSections),
(br#"{"version":3,"sections":{}}"#, 9, SourceMapError::BadSections),
(br#"{"version":3,"sections":[{"offset":{"line":0,"column":0},"map":{"version":2,"sources":["a"]}}]}"#, 9, SourceMapError::BadVersion),
(br#"{"version":3,"sources":["a","b"],"mappings":"!"}"#, 1, SourceMapError::TooManyEntries(1)),
// Mappings that name a source past the end of the list show it is short.
(br#"{"version":3,"mappings":"AAAA"}"#, 9, SourceMapError::BadMappings(1)),
(br#"{"version":3,"sources":["a"],"mappings":"AAAA;ACAA,!"}"#, 9, SourceMapError::BadMappings(2)),
(br#"{"version":3,"sources":["a"],"mappings":"AAAA,ADAA,AEAA"}"#, 9, SourceMapError::BadMappings(3)),
] {
assert_eq!(parse_sources(json, with(limit)), Err(err), "{}", String::from_utf8_lossy(json));
}
}