| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Rules for the names that make up paths. |
| 5 | //! |
| 6 | //! The store accepts nearly any name, about what `git fsck` accepts. It refuses |
| 7 | //! names that cannot be one path component, and names that some file system |
| 8 | //! would treat as `.braid` or `.git`: a crafted repository could otherwise |
| 9 | //! write into the metadata of the checkout that receives it. |
| 10 | //! |
| 11 | //! Every name of a tree is checked each time the tree is built or read, so |
| 12 | //! the common case is cheap: a name that is all ASCII is folded with ASCII |
| 13 | //! lowercasing alone, without allocating. [`fold_for_aliases`] says why that |
| 14 | //! gives the same answer as the full Unicode fold, which every other name |
| 15 | //! goes through. |
| 16 | |
| 17 | use core::fmt; |
| 18 | |
| 19 | use unicode_normalization::UnicodeNormalization; |
| 20 | |
| 21 | /// Longest name accepted, in bytes. |
| 22 | pub const MAX_NAME_BYTES: usize = 4096; |
| 23 | |
| 24 | /// Directory names that hold version control metadata. |
| 25 | const RESERVED: [&[u8]; 2] = [b".braid", b".git"]; |
| 26 | |
| 27 | /// Their 8.3 short-name stems (without the `~N` suffix). |
| 28 | const RESERVED_SHORT: [&[u8]; 2] = [b"braid", b"git"]; |
| 29 | |
| 30 | /// Files that git or braid read, and that must therefore never be symlinks. |
| 31 | const NO_SYMLINK: [&[u8]; 4] = [ |
| 32 | b".gitmodules", |
| 33 | b".gitignore", |
| 34 | b".gitattributes", |
| 35 | b".braidignore", |
| 36 | ]; |
| 37 | |
| 38 | /// Why a name is not accepted. |
| 39 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 40 | pub enum NameError { |
| 41 | /// The name is empty. |
| 42 | Empty, |
| 43 | /// The name is longer than [`MAX_NAME_BYTES`]. |
| 44 | TooLong(usize), |
| 45 | /// The name contains `/`. |
| 46 | Slash, |
| 47 | /// The name contains a NUL byte. |
| 48 | Nul, |
| 49 | /// The name is `.` or `..`. |
| 50 | DotOrDotDot, |
| 51 | /// Some file system would treat the name as `.braid` or `.git`. |
| 52 | Reserved, |
| 53 | } |
| 54 | |
| 55 | impl fmt::Display for NameError { |
| 56 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 57 | match self { |
| 58 | NameError::Empty => f.write_str("empty name"), |
| 59 | NameError::TooLong(len) => { |
| 60 | write!(f, "name of {len} bytes is longer than {MAX_NAME_BYTES}") |
| 61 | } |
| 62 | NameError::Slash => f.write_str("name contains '/'"), |
| 63 | NameError::Nul => f.write_str("name contains a NUL byte"), |
| 64 | NameError::DotOrDotDot => f.write_str("name is '.' or '..'"), |
| 65 | NameError::Reserved => { |
| 66 | f.write_str("name would be read as .braid or .git on some file system") |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl std::error::Error for NameError {} |
| 73 | |
| 74 | /// Why a path is not accepted. |
| 75 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 76 | pub enum PathError { |
| 77 | /// The component at this position (0-based) is not a valid name. An empty |
| 78 | /// component means a leading, trailing or doubled `/`. |
| 79 | Component { index: usize, error: NameError }, |
| 80 | } |
| 81 | |
| 82 | impl fmt::Display for PathError { |
| 83 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 84 | match self { |
| 85 | PathError::Component { index, error } => write!(f, "component {index}: {error}"), |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | impl std::error::Error for PathError {} |
| 91 | |
| 92 | /// Checks one name, as stored in a tree. |
| 93 | pub fn validate_name(name: &[u8]) -> Result<(), NameError> { |
| 94 | if name.is_empty() { |
| 95 | return Err(NameError::Empty); |
| 96 | } |
| 97 | if name.len() > MAX_NAME_BYTES { |
| 98 | return Err(NameError::TooLong(name.len())); |
| 99 | } |
| 100 | if name.contains(&b'/') { |
| 101 | return Err(NameError::Slash); |
| 102 | } |
| 103 | if name.contains(&0) { |
| 104 | return Err(NameError::Nul); |
| 105 | } |
| 106 | if name == b"." || name == b".." { |
| 107 | return Err(NameError::DotOrDotDot); |
| 108 | } |
| 109 | if is_reserved(name) { |
| 110 | return Err(NameError::Reserved); |
| 111 | } |
| 112 | Ok(()) |
| 113 | } |
| 114 | |
| 115 | /// Checks a path: names joined by single `/`, no leading or trailing `/`. |
| 116 | pub fn validate_path(path: &[u8]) -> Result<(), PathError> { |
| 117 | for (index, component) in path.split(|byte| *byte == b'/').enumerate() { |
| 118 | validate_name(component).map_err(|error| PathError::Component { index, error })?; |
| 119 | } |
| 120 | Ok(()) |
| 121 | } |
| 122 | |
| 123 | /// Whether some file system would treat `name` as `.braid` or `.git`. |
| 124 | pub fn is_reserved(name: &[u8]) -> bool { |
| 125 | match ascii_stem(name) { |
| 126 | Some(stem) => { |
| 127 | RESERVED |
| 128 | .iter() |
| 129 | .any(|reserved| stem.eq_ignore_ascii_case(reserved)) |
| 130 | || is_short_stem(stem) |
| 131 | } |
| 132 | None => { |
| 133 | let folded = fold_unicode(name); |
| 134 | RESERVED.contains(&folded.as_slice()) || is_short_name(&folded) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /// Whether `name` must not be a symlink. |
| 140 | pub fn forbidden_as_symlink(name: &[u8]) -> bool { |
| 141 | match ascii_stem(name) { |
| 142 | Some(stem) => NO_SYMLINK |
| 143 | .iter() |
| 144 | .any(|special| stem.eq_ignore_ascii_case(special)), |
| 145 | None => NO_SYMLINK.contains(&fold_unicode(name).as_slice()), |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /// The name as the most forgiving file systems would compare it: without the |
| 150 | /// code points HFS+ ignores, in Unicode NFC, lowercased, and without anything |
| 151 | /// from the first `:` on (NTFS streams) or trailing dots and spaces (NTFS). |
| 152 | /// |
| 153 | /// Only meant to detect aliases of special names; checkouts use a fold that |
| 154 | /// matches the file system they write to. |
| 155 | /// |
| 156 | /// A name that is all ASCII is folded by ASCII lowercasing alone, which gives |
| 157 | /// the same bytes as the full fold: such a name is valid UTF-8; every code |
| 158 | /// point HFS+ ignores is outside ASCII; NFC leaves ASCII text as it is, since |
| 159 | /// no ASCII character has a decomposition or composes with another; and the |
| 160 | /// Unicode lowercase of an ASCII character is its ASCII lowercase. ASCII |
| 161 | /// lowercasing keeps every byte in place, so cutting at the first `:` and |
| 162 | /// trimming dots and spaces can come first, and the checks above can compare |
| 163 | /// that stem without letter case instead of lowercasing a copy. The tests |
| 164 | /// and the `names` fuzz target compare both paths. |
| 165 | pub fn fold_for_aliases(name: &[u8]) -> Vec<u8> { |
| 166 | match ascii_stem(name) { |
| 167 | Some(stem) => stem.to_ascii_lowercase(), |
| 168 | None => fold_unicode(name), |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// For a name that is all ASCII, what [`fold_for_aliases`] keeps of it, before |
| 173 | /// lowercasing: everything before the first `:`, without trailing dots and |
| 174 | /// spaces. `None` for any other name. |
| 175 | fn ascii_stem(name: &[u8]) -> Option<&[u8]> { |
| 176 | if !name.is_ascii() { |
| 177 | return None; |
| 178 | } |
| 179 | let end = name |
| 180 | .iter() |
| 181 | .position(|byte| *byte == b':') |
| 182 | .unwrap_or(name.len()); |
| 183 | let mut stem = &name[..end]; |
| 184 | while let [rest @ .., b'.' | b' '] = stem { |
| 185 | stem = rest; |
| 186 | } |
| 187 | Some(stem) |
| 188 | } |
| 189 | |
| 190 | /// [`fold_for_aliases`] for any name, through Unicode: what names that are |
| 191 | /// not all ASCII go through. |
| 192 | fn fold_unicode(name: &[u8]) -> Vec<u8> { |
| 193 | let mut folded = match core::str::from_utf8(name) { |
| 194 | Ok(text) => text |
| 195 | .chars() |
| 196 | .filter(|c| !is_hfs_ignorable(*c)) |
| 197 | .nfc() |
| 198 | .flat_map(char::to_lowercase) |
| 199 | .collect::<String>() |
| 200 | .into_bytes(), |
| 201 | // A name that is not UTF-8 can only alias an ASCII name byte for byte. |
| 202 | Err(_) => name.to_ascii_lowercase(), |
| 203 | }; |
| 204 | if let Some(colon) = folded.iter().position(|byte| *byte == b':') { |
| 205 | folded.truncate(colon); |
| 206 | } |
| 207 | while matches!(folded.last(), Some(b'.' | b' ')) { |
| 208 | folded.pop(); |
| 209 | } |
| 210 | folded |
| 211 | } |
| 212 | |
| 213 | /// Code points that HFS+ drops when it compares names. |
| 214 | fn is_hfs_ignorable(c: char) -> bool { |
| 215 | matches!( |
| 216 | c, |
| 217 | '\u{200c}'..='\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{206a}'..='\u{206f}' | '\u{feff}' |
| 218 | ) |
| 219 | } |
| 220 | |
| 221 | /// `braid~N` or `git~N`: the 8.3 short names NTFS can give `.braid` and `.git`. |
| 222 | fn is_short_name(folded: &[u8]) -> bool { |
| 223 | RESERVED_SHORT.iter().any(|stem| { |
| 224 | folded.len() == stem.len() + 2 |
| 225 | && folded.starts_with(stem) |
| 226 | && folded[stem.len()] == b'~' |
| 227 | && matches!(folded[stem.len() + 1], b'1'..=b'9') |
| 228 | }) |
| 229 | } |
| 230 | |
| 231 | /// [`is_short_name`] on the stem of an ASCII name, in any letter case. |
| 232 | fn is_short_stem(stem: &[u8]) -> bool { |
| 233 | RESERVED_SHORT.iter().any(|short| { |
| 234 | stem.len() == short.len() + 2 |
| 235 | && stem[..short.len()].eq_ignore_ascii_case(short) |
| 236 | && stem[short.len()] == b'~' |
| 237 | && matches!(stem[short.len() + 1], b'1'..=b'9') |
| 238 | }) |
| 239 | } |
| 240 | |
| 241 | /// The alias checks without the ASCII fast path, every name through the full |
| 242 | /// Unicode fold, as they were written before it: what the tests and the |
| 243 | /// `names` fuzz target compare the fast path with. Not part of the stable |
| 244 | /// API. |
| 245 | #[cfg(any(test, feature = "fuzzing"))] |
| 246 | #[doc(hidden)] |
| 247 | pub mod reference { |
| 248 | use super::{NO_SYMLINK, RESERVED, fold_unicode, is_short_name}; |
| 249 | |
| 250 | /// [`super::fold_for_aliases`] through Unicode, whatever the name. |
| 251 | pub fn fold_for_aliases(name: &[u8]) -> Vec<u8> { |
| 252 | fold_unicode(name) |
| 253 | } |
| 254 | |
| 255 | /// [`super::is_reserved`] through Unicode, whatever the name. |
| 256 | pub fn is_reserved(name: &[u8]) -> bool { |
| 257 | let folded = fold_unicode(name); |
| 258 | RESERVED.contains(&folded.as_slice()) || is_short_name(&folded) |
| 259 | } |
| 260 | |
| 261 | /// [`super::forbidden_as_symlink`] through Unicode, whatever the name. |
| 262 | pub fn forbidden_as_symlink(name: &[u8]) -> bool { |
| 263 | NO_SYMLINK.contains(&fold_unicode(name).as_slice()) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | #[cfg(test)] |
| 268 | mod tests { |
| 269 | use super::*; |
| 270 | use proptest::prelude::*; |
| 271 | |
| 272 | #[test] |
| 273 | fn accepts_ordinary_names() { |
| 274 | for name in [ |
| 275 | &b"README.md"[..], |
| 276 | b"src", |
| 277 | b".github", |
| 278 | b".gitkeep", |
| 279 | b".git-blame-ignore-revs", |
| 280 | b"git", |
| 281 | b"braid", |
| 282 | b"...", |
| 283 | b"a b", |
| 284 | b"caf\xc3\xa9", |
| 285 | b"\xff\xfe", |
| 286 | b"git~10", |
| 287 | ] { |
| 288 | assert_eq!( |
| 289 | validate_name(name), |
| 290 | Ok(()), |
| 291 | "{:?}", |
| 292 | String::from_utf8_lossy(name) |
| 293 | ); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | #[test] |
| 298 | fn rejects_structural_problems() { |
| 299 | assert_eq!(validate_name(b""), Err(NameError::Empty)); |
| 300 | assert_eq!(validate_name(b"a/b"), Err(NameError::Slash)); |
| 301 | assert_eq!(validate_name(b"a\0b"), Err(NameError::Nul)); |
| 302 | assert_eq!(validate_name(b"."), Err(NameError::DotOrDotDot)); |
| 303 | assert_eq!(validate_name(b".."), Err(NameError::DotOrDotDot)); |
| 304 | let long = vec![b'a'; MAX_NAME_BYTES + 1]; |
| 305 | assert_eq!( |
| 306 | validate_name(&long), |
| 307 | Err(NameError::TooLong(MAX_NAME_BYTES + 1)) |
| 308 | ); |
| 309 | assert_eq!(validate_name(&long[..MAX_NAME_BYTES]), Ok(())); |
| 310 | } |
| 311 | |
| 312 | #[test] |
| 313 | fn rejects_aliases_of_metadata_directories() { |
| 314 | let aliases: &[&str] = &[ |
| 315 | ".git", |
| 316 | ".braid", |
| 317 | ".GIT", |
| 318 | ".Braid", |
| 319 | ".git.", |
| 320 | ".git ", |
| 321 | ".git . .", |
| 322 | ".git::$INDEX_ALLOCATION", |
| 323 | ".braid:stream", |
| 324 | "GIT~1", |
| 325 | "git~2", |
| 326 | "BRAID~1", |
| 327 | // HFS+ ignores these code points. |
| 328 | ".g\u{200c}it", |
| 329 | "\u{feff}.git", |
| 330 | ".bra\u{206f}id", |
| 331 | ]; |
| 332 | for alias in aliases { |
| 333 | assert_eq!( |
| 334 | validate_name(alias.as_bytes()), |
| 335 | Err(NameError::Reserved), |
| 336 | "{alias:?}" |
| 337 | ); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | #[test] |
| 342 | fn unicode_normalisation_is_applied() { |
| 343 | // "é" precomposed and decomposed fold to the same bytes. |
| 344 | assert_eq!( |
| 345 | fold_for_aliases("caf\u{e9}".as_bytes()), |
| 346 | fold_for_aliases("cafe\u{301}".as_bytes()) |
| 347 | ); |
| 348 | } |
| 349 | |
| 350 | #[test] |
| 351 | fn symlink_restrictions() { |
| 352 | assert!(forbidden_as_symlink(b".gitmodules")); |
| 353 | assert!(forbidden_as_symlink(b".GITIGNORE")); |
| 354 | assert!(forbidden_as_symlink(b".braidignore.")); |
| 355 | assert!(forbidden_as_symlink(".gitattr\u{200d}ibutes".as_bytes())); |
| 356 | assert!(!forbidden_as_symlink(b"gitmodules")); |
| 357 | assert!(!forbidden_as_symlink(b"README")); |
| 358 | } |
| 359 | |
| 360 | #[test] |
| 361 | fn paths() { |
| 362 | assert_eq!(validate_path(b"a"), Ok(())); |
| 363 | assert_eq!(validate_path(b"a/b/c.txt"), Ok(())); |
| 364 | let empty = |index| PathError::Component { |
| 365 | index, |
| 366 | error: NameError::Empty, |
| 367 | }; |
| 368 | assert_eq!(validate_path(b""), Err(empty(0))); |
| 369 | assert_eq!(validate_path(b"/a"), Err(empty(0))); |
| 370 | assert_eq!(validate_path(b"a/"), Err(empty(1))); |
| 371 | assert_eq!(validate_path(b"a//b"), Err(empty(1))); |
| 372 | assert_eq!( |
| 373 | validate_path(b"a/.git/config"), |
| 374 | Err(PathError::Component { |
| 375 | index: 1, |
| 376 | error: NameError::Reserved |
| 377 | }) |
| 378 | ); |
| 379 | assert_eq!( |
| 380 | validate_path(b"a/../b"), |
| 381 | Err(PathError::Component { |
| 382 | index: 1, |
| 383 | error: NameError::DotOrDotDot |
| 384 | }) |
| 385 | ); |
| 386 | } |
| 387 | |
| 388 | /// The fast path gives the same fold and the same answers as the full |
| 389 | /// Unicode fold. |
| 390 | fn assert_agrees(name: &[u8]) { |
| 391 | let shown = String::from_utf8_lossy(name); |
| 392 | assert_eq!( |
| 393 | fold_for_aliases(name), |
| 394 | reference::fold_for_aliases(name), |
| 395 | "{shown:?}" |
| 396 | ); |
| 397 | assert_eq!(is_reserved(name), reference::is_reserved(name), "{shown:?}"); |
| 398 | assert_eq!( |
| 399 | forbidden_as_symlink(name), |
| 400 | reference::forbidden_as_symlink(name), |
| 401 | "{shown:?}" |
| 402 | ); |
| 403 | } |
| 404 | |
| 405 | /// The special names, as the full fold gives them. |
| 406 | const SPECIAL: [&str; 10] = [ |
| 407 | ".git", |
| 408 | ".braid", |
| 409 | "git~1", |
| 410 | "braid~9", |
| 411 | ".gitmodules", |
| 412 | ".gitignore", |
| 413 | ".gitattributes", |
| 414 | ".braidignore", |
| 415 | "git~0", |
| 416 | "braid~10", |
| 417 | ]; |
| 418 | |
| 419 | /// Characters that make folding interesting: those of the special names |
| 420 | /// in both cases, what the fold cuts or trims, bytes no name may hold, |
| 421 | /// code points HFS+ ignores, combining marks, and characters that NFC or |
| 422 | /// lowercasing turn into others, some of them into ASCII. |
| 423 | const PALETTE: [char; 59] = [ |
| 424 | '.', 'g', 'G', 'i', 'I', 't', 'T', 'b', 'B', 'r', 'R', 'a', 'A', 'd', 'D', 'm', 'M', 'o', |
| 425 | 'u', 'l', 'e', 's', 'n', 'k', 'K', '~', '0', '1', '9', ':', ' ', '$', '/', '\0', '\u{7f}', |
| 426 | '\u{200c}', '\u{200f}', '\u{202a}', '\u{202e}', '\u{206a}', '\u{206f}', '\u{feff}', |
| 427 | '\u{300}', '\u{301}', '\u{307}', '\u{338}', |
| 428 | // KELVIN SIGN, whose NFC is K; ANGSTROM SIGN, whose NFC is Å. |
| 429 | '\u{212a}', '\u{212b}', |
| 430 | // İ lowercases to i and a combining dot; dotless ı; long s. |
| 431 | '\u{130}', '\u{131}', '\u{17f}', |
| 432 | // GREEK QUESTION MARK and GREEK VARIA, whose NFC are ; and `. |
| 433 | '\u{37e}', '\u{1fef}', // Lookalikes that stay what they are. |
| 434 | '\u{ff27}', '\u{ff0e}', '\u{2024}', '\u{e9}', '\u{1e9e}', '\u{fb01}', |
| 435 | ]; |
| 436 | |
| 437 | #[test] |
| 438 | fn fast_path_agrees_on_known_aliases_and_near_misses() { |
| 439 | let names: &[&[u8]] = &[ |
| 440 | b"", |
| 441 | b".", |
| 442 | b"..", |
| 443 | b":", |
| 444 | b". :", |
| 445 | b".git", |
| 446 | b".GiT", |
| 447 | b".git.", |
| 448 | b".git . .", |
| 449 | b".git::$INDEX_ALLOCATION", |
| 450 | b".git:", |
| 451 | b":.git", |
| 452 | b"x.git", |
| 453 | b".gitx", |
| 454 | b"GIT~1", |
| 455 | b"Git~9", |
| 456 | b"git~0", |
| 457 | b"git~10", |
| 458 | b"git~1.", |
| 459 | b"git~1:x", |
| 460 | b"BRAID~1", |
| 461 | b".Braid", |
| 462 | b".GITMODULES", |
| 463 | b".gitattributes. ", |
| 464 | b".braidignore:stream", |
| 465 | b"\xff.git", |
| 466 | b".git\xff", |
| 467 | b"\x80", |
| 468 | ".g\u{200c}it".as_bytes(), |
| 469 | "\u{feff}.git".as_bytes(), |
| 470 | ".bra\u{206f}id".as_bytes(), |
| 471 | ".gi\u{307}t".as_bytes(), |
| 472 | ".g\u{130}t".as_bytes(), |
| 473 | ".g\u{131}t".as_bytes(), |
| 474 | "git\u{ff5e}1".as_bytes(), |
| 475 | "\u{212a}".as_bytes(), |
| 476 | ".git\u{2024}".as_bytes(), |
| 477 | ".gitmodule\u{17f}".as_bytes(), |
| 478 | ".git\u{37e}".as_bytes(), |
| 479 | ]; |
| 480 | for name in names { |
| 481 | assert_agrees(name); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | #[test] |
| 486 | fn fast_path_agrees_on_every_short_ascii_name() { |
| 487 | assert_agrees(b""); |
| 488 | for a in 0..0x80u8 { |
| 489 | assert_agrees(&[a]); |
| 490 | for b in 0..0x80u8 { |
| 491 | assert_agrees(&[a, b]); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | #[test] |
| 497 | fn fast_path_agrees_on_every_case_and_ending_of_the_special_names() { |
| 498 | let endings: [&[u8]; 8] = [b"", b".", b" ", b". .", b":", b":x", b"::$DATA", b"x"]; |
| 499 | for special in SPECIAL { |
| 500 | let special = special.as_bytes(); |
| 501 | let letters = special.iter().filter(|b| b.is_ascii_alphabetic()).count(); |
| 502 | for case in 0..1u32 << letters { |
| 503 | let mut name = Vec::new(); |
| 504 | let mut bit = 0; |
| 505 | for byte in special { |
| 506 | if byte.is_ascii_alphabetic() { |
| 507 | name.push(if (case >> bit) & 1 == 1 { |
| 508 | byte.to_ascii_uppercase() |
| 509 | } else { |
| 510 | *byte |
| 511 | }); |
| 512 | bit += 1; |
| 513 | } else { |
| 514 | name.push(*byte); |
| 515 | } |
| 516 | } |
| 517 | for ending in endings { |
| 518 | let mut with_ending = name.clone(); |
| 519 | with_ending.extend_from_slice(ending); |
| 520 | assert_agrees(&with_ending); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /// A special name, with letters in either case, a few characters of the |
| 527 | /// palette inserted, and maybe dots, spaces or a stream name after it. |
| 528 | fn near_special() -> impl Strategy<Value = Vec<u8>> { |
| 529 | ( |
| 530 | prop::sample::select(SPECIAL.to_vec()), |
| 531 | any::<u64>(), |
| 532 | prop::collection::vec( |
| 533 | ( |
| 534 | any::<prop::sample::Index>(), |
| 535 | prop::sample::select(PALETTE.to_vec()), |
| 536 | ), |
| 537 | 0..3, |
| 538 | ), |
| 539 | "[. ]{0,3}(:[a-z$]{0,4})?", |
| 540 | ) |
| 541 | .prop_map(|(special, case, inserts, ending)| { |
| 542 | let mut chars: Vec<char> = special |
| 543 | .chars() |
| 544 | .enumerate() |
| 545 | .map(|(i, c)| { |
| 546 | if (case >> (i % 64)) & 1 == 1 { |
| 547 | c.to_ascii_uppercase() |
| 548 | } else { |
| 549 | c |
| 550 | } |
| 551 | }) |
| 552 | .collect(); |
| 553 | for (at, c) in inserts { |
| 554 | let at = at.index(chars.len() + 1); |
| 555 | chars.insert(at, c); |
| 556 | } |
| 557 | let mut name: String = chars.into_iter().collect(); |
| 558 | name.push_str(&ending); |
| 559 | name.into_bytes() |
| 560 | }) |
| 561 | } |
| 562 | |
| 563 | proptest! { |
| 564 | #![proptest_config(ProptestConfig::with_cases(2000))] |
| 565 | |
| 566 | #[test] |
| 567 | fn fast_path_agrees_on_any_bytes(name in prop::collection::vec(any::<u8>(), 0..24)) { |
| 568 | assert_agrees(&name); |
| 569 | } |
| 570 | |
| 571 | #[test] |
| 572 | fn fast_path_agrees_on_ascii(name in prop::collection::vec(0u8..0x80, 0..24)) { |
| 573 | assert_agrees(&name); |
| 574 | } |
| 575 | |
| 576 | #[test] |
| 577 | fn fast_path_agrees_on_mixed_text( |
| 578 | chars in prop::collection::vec(prop::sample::select(PALETTE.to_vec()), 0..16), |
| 579 | ) { |
| 580 | let name: String = chars.into_iter().collect(); |
| 581 | assert_agrees(name.as_bytes()); |
| 582 | } |
| 583 | |
| 584 | #[test] |
| 585 | fn fast_path_agrees_near_the_special_names(name in near_special()) { |
| 586 | assert_agrees(&name); |
| 587 | } |
| 588 | } |
| 589 | } |