Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 1.8 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! Name rules never panic, and agree with each other: a valid name is a valid
5//! one-component path, and an alias of a metadata directory is never valid.
6//!
7//! The alias checks take a fast path for names that are all ASCII; it must
8//! give the same fold and the same answers as the full Unicode fold. Most
9//! inputs are not ASCII, so each one is also checked as ASCII, and as text
10//! made of the characters of the special names.
11
12#![no_main]
13
14use braid_format::name::{
15 fold_for_aliases, forbidden_as_symlink, is_reserved, reference, validate_name, validate_path,
16};
17use libfuzzer_sys::fuzz_target;
18
19/// The characters of `.git`, `.braid`, their short names and the files that
20/// must not be symlinks, in both cases, and what the fold cuts or trims.
21const SPECIAL: &[u8] = b".gitbraGITBRAD~19mdulensoMUL: ";
22
23fn agrees(name: &[u8]) {
24 assert_eq!(fold_for_aliases(name), reference::fold_for_aliases(name));
25 assert_eq!(is_reserved(name), reference::is_reserved(name));
26 assert_eq!(
27 forbidden_as_symlink(name),
28 reference::forbidden_as_symlink(name)
29 );
30}
31
32fuzz_target!(|data: &[u8]| {
33 agrees(data);
34 let ascii: Vec<u8> = data.iter().map(|byte| byte & 0x7f).collect();
35 agrees(&ascii);
36 let special: Vec<u8> = data
37 .iter()
38 .map(|byte| SPECIAL[usize::from(*byte) % SPECIAL.len()])
39 .collect();
40 agrees(&special);
41
42 let folded = fold_for_aliases(data);
43 let valid = validate_name(data).is_ok();
44 if valid {
45 assert!(validate_path(data).is_ok());
46 assert!(!is_reserved(data));
47 let mut nested = b"dir/".to_vec();
48 nested.extend_from_slice(data);
49 assert!(validate_path(&nested).is_ok());
50 }
51 if folded == b".git" || folded == b".braid" {
52 assert!(!valid);
53 }
54});