Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/synapse/blob/main/crates/synapse-git/tests/h1_read_limits.rs # synapse A component of the AienOS ecosystem. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `synapse / crates / synapse-git / tests / h1_read_limits.rs` 162 lines ยท 5031 bytes Raw Blame History 1 //! ADR 0003 step H1 (issue #48): the configurable read limits of the native Git reader 2 //! (`ReadLimits`, review of PR #90 finding B2). The system `git` only builds fixture history. 3 4 use std::fs; 5 use std::path::{Path, PathBuf}; 6 use std::process::Command; 7 use synapse_core::RefusalCode; 8 use synapse_git::{GitRepository, ReadLimits}; 9 10 fn temp_dir(label: &str) -> PathBuf { 11 let dir = std::env::temp_dir().join(format!( 12 "synapse-h1-limits-{label}-{}-{}", 13 std::process::id(), 14 std::time::SystemTime::now() 15 .duration_since(std::time::UNIX_EPOCH) 16 .unwrap() 17 .as_nanos() 18 )); 19 fs::create_dir_all(&dir).unwrap(); 20 dir 21 } 22 23 fn git(dir: &Path, args: &[&str]) -> String { 24 let out = Command::new("git") 25 .arg("-C") 26 .arg(dir) 27 .args(args) 28 .env("GIT_CONFIG_NOSYSTEM", "1") 29 .env("GIT_CONFIG_GLOBAL", "/dev/null") 30 .env("GIT_AUTHOR_NAME", "Fixture Author") 31 .env("GIT_AUTHOR_EMAIL", "author@fixture.invalid") 32 .env("GIT_AUTHOR_DATE", "`1700000000` +0000") 33 .env("GIT_COMMITTER_NAME", "Fixture Committer") 34 .env("GIT_COMMITTER_EMAIL", "committer@fixture.invalid") 35 .env("GIT_COMMITTER_DATE", "`1700000000` +0000") 36 .output() 37 .unwrap(); 38 assert!( 39 out.status.success(), 40 "git {args:?} failed: {}", 41 String::from_utf8_lossy(&out.stderr) 42 ); 43 String::from_utf8(out.stdout).unwrap().trim().to_string() 44 } 45 46 /// A repository with a few commits; packed (`git gc`) or loose. Returns its `.git` directory 47 /// and the total size of its reachable objects as Git reports it. 48 fn repo(label: &str, packed: bool) -> (PathBuf, u64) { 49 let work = temp_dir(label); 50 git(&work, &["init", "-q", "-b", "main"]); 51 for i in 0..4 { 52 fs::write(work.join("f.txt"), format!("version {i}\n").repeat(200)).unwrap(); 53 git(&work, &["add", "-A"]); 54 git(&work, &["commit", "-q", "-m", &format!("c{i}")]); 55 } 56 if packed { 57 git(&work, &["gc", "-q", "--aggressive", "--prune=now"]); 58 } 59 let sizes = git( 60 &work, 61 &[ 62 "cat-file", 63 "--batch-all-objects", 64 "--batch-check=%(objectsize)", 65 ], 66 ); 67 let total = sizes.lines().map(|l| l.parse::().unwrap()).sum(); 68 (work.join(".git"), total) 69 } 70 71 fn code_of(git_dir: &Path, limits: ReadLimits) -> Option { 72 GitRepository::open_with_limits(git_dir, limits) 73 .and_then(|mut r| r.full_history()) 74 .err() 75 .map(|e| e.code) 76 } 77 78 #[test] 79 fn h1_total_object_budget_is_enforced_for_packed_and_loose_objects() { 80 for packed in [true, false] { 81 let (dir, total) = repo( 82 if packed { 83 "budget-packed" 84 } else { 85 "budget-loose" 86 }, 87 packed, 88 ); 89 let fits = ReadLimits { 90 max_total_object_bytes: total, 91 ..ReadLimits::default() 92 }; 93 assert_eq!(code_of(&dir, fits), None, "packed={packed}: exact budget"); 94 let short = ReadLimits { 95 max_total_object_bytes: total - 1, 96 ..ReadLimits::default() 97 }; 98 assert_eq!( 99 code_of(&dir, short), 100 Some(RefusalCode::QuotaPayloadTooLarge), 101 "packed={packed}: one byte short" 102 ); 103 } 104 } 105 106 #[test] 107 fn h1_per_object_cap_is_enforced_for_packed_and_loose_objects() { 108 for packed in [true, false] { 109 let (dir, _) = repo(if packed { "cap-packed" } else { "cap-loose" }, packed); 110 let limits = ReadLimits { 111 max_object_bytes: 100, 112 ..ReadLimits::default() 113 }; 114 assert_eq!( 115 code_of(&dir, limits), 116 Some(RefusalCode::QuotaPayloadTooLarge), 117 "packed={packed}" 118 ); 119 } 120 } 121 122 #[test] 123 fn h1_pack_file_and_ref_file_caps_are_enforced() { 124 let (dir, _) = repo("file-caps", true); 125 let limits = ReadLimits { 126 max_pack_files_bytes: 64, 127 ..ReadLimits::default() 128 }; 129 assert_eq!( 130 code_of(&dir, limits), 131 Some(RefusalCode::QuotaPayloadTooLarge) 132 ); 133 let limits = ReadLimits { 134 max_packed_refs_bytes: 8, 135 ..ReadLimits::default() 136 }; 137 assert_eq!( 138 code_of(&dir, limits), 139 Some(RefusalCode::QuotaPayloadTooLarge), 140 "packed-refs over its cap" 141 ); 142 let limits = ReadLimits { 143 max_ref_file_bytes: 8, 144 ..ReadLimits::default() 145 }; 146 assert_eq!( 147 code_of(&dir, limits), 148 Some(RefusalCode::QuotaPayloadTooLarge), 149 "HEAD over its cap" 150 ); 151 } 152 153 #[test] 154 fn h1_lock_files_under_refs_are_skipped() { 155 let (dir, _) = repo("lock", false); 156 let tip = fs::read_to_string(dir.join("refs/heads/main")).unwrap(); 157 fs::write(dir.join("refs/heads/main.lock"), tip).unwrap(); 158 let mut r = GitRepository::open(&dir).unwrap(); 159 let (refs, _) = r.refs().unwrap(); 160 assert_eq!(refs.keys().collect::>(), ["refs/heads/main"]); 161 r.full_history().unwrap(); 162 }