|
| 1 | +use super::*; |
| 2 | + |
| 3 | +fn lookup_body(paths: &[&str]) -> Body { |
| 4 | + Body::from(json!({"paths": paths}).to_string()) |
| 5 | +} |
| 6 | + |
| 7 | +#[tokio::test] |
| 8 | +async fn mst2_durable_http_lookup_uses_verified_metadata_after_service_rebuild_without_body_reads() |
| 9 | +{ |
| 10 | + let fixture = Fixture::new_with_pg_config(true).await; |
| 11 | + let state = rebuilt(&fixture).await; |
| 12 | + assert!(state.storage.native_snapshot_sessions.get().is_none()); |
| 13 | + assert!(!Arc::ptr_eq( |
| 14 | + &state.storage.native_projection_cache, |
| 15 | + &fixture.state.storage.native_projection_cache |
| 16 | + )); |
| 17 | + let paths = [ |
| 18 | + "/file", |
| 19 | + "/alias", |
| 20 | + "/executable", |
| 21 | + "/empty", |
| 22 | + "/link", |
| 23 | + "/nested/file", |
| 24 | + "/directory", |
| 25 | + "/nested", |
| 26 | + "/missing", |
| 27 | + "/file/child", |
| 28 | + "/link/child", |
| 29 | + ]; |
| 30 | + let value = success_json( |
| 31 | + app(&state) |
| 32 | + .oneshot(fixture.request("POST", "lookup", lookup_body(&paths))) |
| 33 | + .await |
| 34 | + .unwrap(), |
| 35 | + ) |
| 36 | + .await; |
| 37 | + assert_eq!(value["snapshot_id"], fixture.snapshot); |
| 38 | + let results = value["results"].as_array().unwrap(); |
| 39 | + assert_eq!(results.len(), paths.len()); |
| 40 | + for (result, path) in results.iter().zip(paths) { |
| 41 | + assert_eq!(result["path"], path); |
| 42 | + } |
| 43 | + for (index, kind, size, content_digest) in [ |
| 44 | + (0, "regular", fixture.raw.len(), fixture.digest_string()), |
| 45 | + (1, "regular", fixture.raw.len(), fixture.digest_string()), |
| 46 | + (2, "executable", fixture.raw.len(), fixture.digest_string()), |
| 47 | + (3, "regular", 0, format!("sha256:{}", hex_of(&digest(&[])))), |
| 48 | + ( |
| 49 | + 4, |
| 50 | + "symlink", |
| 51 | + 4, |
| 52 | + format!("sha256:{}", hex_of(&digest(b"file"))), |
| 53 | + ), |
| 54 | + (5, "regular", fixture.raw.len(), fixture.digest_string()), |
| 55 | + ] { |
| 56 | + assert_eq!(results[index]["status"], "found"); |
| 57 | + assert_eq!( |
| 58 | + results[index]["node"], |
| 59 | + json!({ |
| 60 | + "fs_kind": kind, |
| 61 | + "name": paths[index].rsplit('/').next().unwrap(), |
| 62 | + "size": size.to_string(), |
| 63 | + "content_digest": content_digest, |
| 64 | + }) |
| 65 | + ); |
| 66 | + } |
| 67 | + let proofs = value["proof_pages"].as_array().unwrap(); |
| 68 | + assert!(!proofs.is_empty()); |
| 69 | + let mut proof_digests = std::collections::HashSet::new(); |
| 70 | + for proof in proofs { |
| 71 | + let bytes = STANDARD |
| 72 | + .decode(proof["data_base64"].as_str().unwrap()) |
| 73 | + .unwrap(); |
| 74 | + mst2_codec::metapage::Page::decode(&bytes).unwrap(); |
| 75 | + let digest = format!("sha256:{}", hex_of(&mst2_codec::metapage::page_id(&bytes))); |
| 76 | + assert_eq!(proof["digest"], digest); |
| 77 | + assert!(proof_digests.insert(digest)); |
| 78 | + } |
| 79 | + for index in [6, 7] { |
| 80 | + let result = &results[index]; |
| 81 | + assert_eq!(result["status"], "found"); |
| 82 | + assert_eq!(result["node"]["fs_kind"], "directory"); |
| 83 | + assert_eq!( |
| 84 | + result["node"]["name"], |
| 85 | + paths[index].rsplit('/').next().unwrap() |
| 86 | + ); |
| 87 | + assert_eq!(result["node"]["node_class"], "native_tree"); |
| 88 | + assert_eq!(result["node"]["lifecycle"], "mutable"); |
| 89 | + assert!(proof_digests.contains(result["node"]["directory_root"].as_str().unwrap())); |
| 90 | + } |
| 91 | + for (index, status) in [ |
| 92 | + (8, "absent"), |
| 93 | + (9, "not_directory"), |
| 94 | + (10, "symlink_traversal"), |
| 95 | + ] { |
| 96 | + assert_eq!( |
| 97 | + results[index], |
| 98 | + json!({"path": paths[index], "status": status}) |
| 99 | + ); |
| 100 | + } |
| 101 | + fixture.counts.assert(0, 0); |
| 102 | +} |
| 103 | + |
| 104 | +#[tokio::test] |
| 105 | +async fn mst2_durable_http_lookup_missing_and_noncurrent_facts_never_fall_back_to_body_or_cache() { |
| 106 | + let fixture = Fixture::new_with_pg_config(true).await; |
| 107 | + fixture.map("/file").await; |
| 108 | + let original = fixture.fact().await; |
| 109 | + fixture.counts.reset(); |
| 110 | + fixture.delete_fact().await; |
| 111 | + error( |
| 112 | + fixture |
| 113 | + .send("POST", "lookup", lookup_body(&["/file"])) |
| 114 | + .await, |
| 115 | + 503, |
| 116 | + "METADATA_NOT_READY", |
| 117 | + true, |
| 118 | + ) |
| 119 | + .await; |
| 120 | + fixture.counts.assert(0, 0); |
| 121 | + for (domain, kind, generation) in [ |
| 122 | + ("git", "blob", 1), |
| 123 | + ("other", "blob", MST2_VERIFICATION_VERSION), |
| 124 | + ("git", "tree", MST2_VERIFICATION_VERSION), |
| 125 | + ] { |
| 126 | + let mut fact = original.clone(); |
| 127 | + fact.storage_domain = domain.to_string(); |
| 128 | + fact.object_kind = kind.to_string(); |
| 129 | + fact.verification_version = generation; |
| 130 | + fixture.replace_fact(fact).await; |
| 131 | + error( |
| 132 | + fixture |
| 133 | + .send("POST", "lookup", lookup_body(&["/alias"])) |
| 134 | + .await, |
| 135 | + 503, |
| 136 | + "METADATA_NOT_READY", |
| 137 | + true, |
| 138 | + ) |
| 139 | + .await; |
| 140 | + fixture.counts.assert(0, 0); |
| 141 | + } |
| 142 | + fixture.replace_fact(original).await; |
| 143 | + success_json( |
| 144 | + fixture |
| 145 | + .send("POST", "lookup", lookup_body(&["/file"])) |
| 146 | + .await, |
| 147 | + ) |
| 148 | + .await; |
| 149 | + fixture.counts.assert(0, 0); |
| 150 | +} |
| 151 | + |
| 152 | +#[tokio::test] |
| 153 | +async fn mst2_durable_http_lookup_invalid_verified_facts_fail_before_body_reads() { |
| 154 | + let fixture = Fixture::new_with_pg_config(true).await; |
| 155 | + let original = fixture.fact().await; |
| 156 | + for case in 0..5 { |
| 157 | + let mut fact = original.clone(); |
| 158 | + match case { |
| 159 | + 0 => fact.state = "PENDING".to_string(), |
| 160 | + 1 => fact.verification_version = MST2_VERIFICATION_VERSION + 1, |
| 161 | + 2 => fact.size = -1, |
| 162 | + 3 => fact.size = 8_796_093_022_209, |
| 163 | + 4 => { |
| 164 | + fact.raw_sha256.pop().unwrap(); |
| 165 | + } |
| 166 | + _ => unreachable!(), |
| 167 | + } |
| 168 | + fixture.replace_fact(fact).await; |
| 169 | + error( |
| 170 | + fixture |
| 171 | + .send("POST", "lookup", lookup_body(&["/file"])) |
| 172 | + .await, |
| 173 | + 502, |
| 174 | + "INTEGRITY_ERROR", |
| 175 | + false, |
| 176 | + ) |
| 177 | + .await; |
| 178 | + fixture.counts.assert(0, 0); |
| 179 | + } |
| 180 | + fixture.replace_fact(original).await; |
| 181 | + success_json( |
| 182 | + fixture |
| 183 | + .send("POST", "lookup", lookup_body(&["/file"])) |
| 184 | + .await, |
| 185 | + ) |
| 186 | + .await; |
| 187 | + fixture.counts.assert(0, 0); |
| 188 | +} |
| 189 | + |
| 190 | +#[tokio::test] |
| 191 | +async fn mst2_durable_http_lookup_symlink_facts_outside_profile_fail_without_body_reads() { |
| 192 | + let fixture = Fixture::new_with_pg_config(true).await; |
| 193 | + let mono = fixture.state.storage.mono_storage(); |
| 194 | + let main = mono.get_main_ref("/").await.unwrap().unwrap(); |
| 195 | + let handler = MonoApiService::from(&fixture.state); |
| 196 | + let root = handler.get_tree_by_hash(&main.ref_tree_hash).await.unwrap(); |
| 197 | + let MetadataWalkOutcome::FoundFile { oid, .. } = |
| 198 | + resolve_abs_metadata(&handler, &root, "/project/link") |
| 199 | + .await |
| 200 | + .unwrap() |
| 201 | + else { |
| 202 | + panic!("fixture link must be a fixed file"); |
| 203 | + }; |
| 204 | + let original = mono |
| 205 | + .get_verified_blobs(vec![oid.clone()]) |
| 206 | + .await |
| 207 | + .unwrap() |
| 208 | + .remove(&oid) |
| 209 | + .unwrap(); |
| 210 | + for size in [0, 4096] { |
| 211 | + let mut fact = original.clone().into_active_model(); |
| 212 | + fact.size = Set(size); |
| 213 | + fact.update(mono.get_connection()).await.unwrap(); |
| 214 | + error( |
| 215 | + fixture |
| 216 | + .send("POST", "lookup", lookup_body(&["/link"])) |
| 217 | + .await, |
| 218 | + 502, |
| 219 | + "INTEGRITY_ERROR", |
| 220 | + false, |
| 221 | + ) |
| 222 | + .await; |
| 223 | + fixture.counts.assert(0, 0); |
| 224 | + } |
| 225 | + let mut restored = original.into_active_model(); |
| 226 | + restored.size = Set(4); |
| 227 | + restored.update(mono.get_connection()).await.unwrap(); |
| 228 | + let result = success_json( |
| 229 | + fixture |
| 230 | + .send("POST", "lookup", lookup_body(&["/link"])) |
| 231 | + .await, |
| 232 | + ) |
| 233 | + .await; |
| 234 | + assert_eq!(result["results"][0]["node"]["size"], "4"); |
| 235 | + fixture.counts.assert(0, 0); |
| 236 | +} |
| 237 | + |
| 238 | +#[tokio::test] |
| 239 | +async fn mst2_durable_http_lookup_verified_fact_database_failure_is_typed_without_body_reads() { |
| 240 | + let fixture = Fixture::new_with_pg_config(true).await; |
| 241 | + let mono = fixture.state.storage.mono_storage(); |
| 242 | + mono.get_connection() |
| 243 | + .execute_unprepared( |
| 244 | + "ALTER TABLE mst2_verified_object RENAME TO mst2_verified_object_unavailable", |
| 245 | + ) |
| 246 | + .await |
| 247 | + .unwrap(); |
| 248 | + error( |
| 249 | + fixture |
| 250 | + .send("POST", "lookup", lookup_body(&["/file"])) |
| 251 | + .await, |
| 252 | + 500, |
| 253 | + "INTERNAL", |
| 254 | + true, |
| 255 | + ) |
| 256 | + .await; |
| 257 | + fixture.counts.assert(0, 0); |
| 258 | + mono.get_connection() |
| 259 | + .execute_unprepared( |
| 260 | + "ALTER TABLE mst2_verified_object_unavailable RENAME TO mst2_verified_object", |
| 261 | + ) |
| 262 | + .await |
| 263 | + .unwrap(); |
| 264 | + success_json( |
| 265 | + fixture |
| 266 | + .send("POST", "lookup", lookup_body(&["/file"])) |
| 267 | + .await, |
| 268 | + ) |
| 269 | + .await; |
| 270 | + fixture.counts.assert(0, 0); |
| 271 | +} |
0 commit comments