MCPcopy Create free account
hub / github.com/bootc-dev/bootc / test_non_utf8

Function test_non_utf8

crates/lib/src/lints.rs:1216–1296  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1214
1215 #[test]
1216 fn test_non_utf8() {
1217 use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
1218
1219 let root = &fixture().unwrap();
1220 let config = &LintExecutionConfig::default();
1221
1222 // Try to create some adversarial symlink situations to ensure the walk doesn't crash
1223 root.create_dir("subdir").unwrap();
1224 // Self-referential symlinks
1225 root.symlink("self", "self").unwrap();
1226 // Infinitely looping dir symlinks
1227 root.symlink("..", "subdir/parent").unwrap();
1228 // Broken symlinks
1229 root.symlink("does-not-exist", "broken").unwrap();
1230 // Out-of-scope symlinks
1231 root.symlink("../../x", "escape").unwrap();
1232 // Should be fine
1233 run_recursive_lint(root, check_utf8, config)
1234 .unwrap()
1235 .unwrap();
1236
1237 // But this will cause an issue
1238 let baddir = OsStr::from_bytes(b"subdir/2/bad\xffdir");
1239 root.create_dir("subdir/2").unwrap();
1240 root.create_dir(baddir).unwrap();
1241 let Err(err) = run_recursive_lint(root, check_utf8, config).unwrap() else {
1242 unreachable!("Didn't fail");
1243 };
1244 assert_eq!(
1245 err.to_string(),
1246 r#"/subdir/2: Found non-utf8 filename "bad\xFFdir""#
1247 );
1248 root.remove_dir(baddir).unwrap(); // Get rid of the problem
1249 run_recursive_lint(root, check_utf8, config)
1250 .unwrap()
1251 .unwrap(); // Check it
1252
1253 // Create a new problem in the form of a regular file
1254 let badfile = OsStr::from_bytes(b"regular\xff");
1255 root.write(badfile, b"Hello, world!\n").unwrap();
1256 let Err(err) = run_recursive_lint(root, check_utf8, config).unwrap() else {
1257 unreachable!("Didn't fail");
1258 };
1259 assert_eq!(
1260 err.to_string(),
1261 r#"/: Found non-utf8 filename "regular\xFF""#
1262 );
1263 root.remove_file(badfile).unwrap(); // Get rid of the problem
1264 run_recursive_lint(root, check_utf8, config)
1265 .unwrap()
1266 .unwrap(); // Check it
1267
1268 // And now test invalid symlink targets
1269 root.symlink(badfile, "subdir/good-name").unwrap();
1270 let Err(err) = run_recursive_lint(root, check_utf8, config).unwrap() else {
1271 unreachable!("Didn't fail");
1272 };
1273 assert_eq!(

Callers

nothing calls this directly

Calls 2

run_recursive_lintFunction · 0.85
fixtureFunction · 0.70

Tested by

no test coverage detected