restore_capability_tests.rs (10166B)
1 use super::*; 2 use crate::OpenOptions; 3 use radroots_storage::{ 4 EventStore, 5 authored_draft::{AuthoredDraft, AuthoredDraftId, AuthoredDraftStage, AuthoredDraftStore}, 6 event::SourceGeneration, 7 status::ShutdownState, 8 }; 9 10 fn draft(id: u8) -> AuthoredDraft { 11 AuthoredDraft::initial( 12 AuthoredDraftId::new([id; 16]).unwrap(), 13 [3; 32], 14 "fixture.restore.v1", 15 vec![id], 16 AuthoredDraftStage::Draft, 17 None, 18 100, 19 ) 20 .unwrap() 21 } 22 23 async fn fixture(root: &Path, backup: &Path) -> (SqliteStorage, RestorePlan) { 24 let store = SqliteStorage::open( 25 OpenOptions::new( 26 crate::Paths::from_directory(root).unwrap(), 27 OpenMode::Create, 28 ) 29 .with_source_generation(SourceGeneration::new([7; 32]).unwrap(), 100) 30 .unwrap() 31 .with_backup_root(backup) 32 .unwrap(), 33 ) 34 .await 35 .unwrap(); 36 store.append_authored_draft(draft(1), None).await.unwrap(); 37 let plan = BackupPlan::new( 38 BackupId::new([6; 16]).unwrap(), 39 BackupFormatVersion::V1, 40 BackupSecretPolicy::IncludeProtectedStorage, 41 100, 42 ) 43 .unwrap(); 44 let owner: &dyn StorageReliability = &store; 45 let manifest = owner.capture_backup(plan.clone()).await.unwrap(); 46 owner.finalize_backup(plan, manifest.clone()).await.unwrap(); 47 store.append_authored_draft(draft(2), None).await.unwrap(); 48 let restore = 49 RestorePlan::new(manifest, BackupSecretPolicy::IncludeProtectedStorage, 200).unwrap(); 50 (store, restore) 51 } 52 53 #[tokio::test] 54 async fn restore_spi_stages_without_live_changes_then_closes_and_reopens_exact_history() { 55 let root = tempfile::tempdir().unwrap(); 56 let backup = tempfile::tempdir().unwrap(); 57 let (store, restore) = fixture(root.path(), backup.path()).await; 58 let source_token = store.source_token().await.unwrap(); 59 let owner: &dyn StorageReliability = &store; 60 let paths = crate::Paths::from_directory(root.path()).unwrap(); 61 let staging = RestoreStaging::new(&paths, restore.manifest()).unwrap(); 62 drop(owner.stage_restore(restore.clone())); 63 drop(owner.finalize_restore(restore.clone())); 64 assert!(!staging.runtime.exists() && !staging.private.exists()); 65 assert_eq!( 66 store.storage_status().await.unwrap().shutdown(), 67 ShutdownState::Open 68 ); 69 let statuses = owner.stage_restore(restore.clone()).await.unwrap(); 70 assert_eq!(statuses.len(), 2); 71 assert!( 72 statuses 73 .iter() 74 .all(|v| v.verification() == MemberVerification::Verified) 75 ); 76 assert_eq!( 77 store 78 .authored_draft_head(draft(2).draft_id()) 79 .await 80 .unwrap(), 81 Some(draft(2)) 82 ); 83 assert_eq!( 84 owner.stage_restore(restore.clone()).await, 85 Err(RestoreCapabilityError::Conflict) 86 ); 87 owner.finalize_restore(restore.clone()).await.unwrap(); 88 assert_eq!( 89 store.source_token().await, 90 Err(radroots_storage::Error::BackendUnavailable) 91 ); 92 assert_eq!( 93 store.storage_status().await.unwrap().shutdown(), 94 ShutdownState::Closed 95 ); 96 assert_eq!( 97 owner.stage_restore(restore.clone()).await, 98 Err(RestoreCapabilityError::Unavailable) 99 ); 100 assert_eq!( 101 owner.finalize_restore(restore).await, 102 Err(RestoreCapabilityError::Unavailable) 103 ); 104 let reopened = SqliteStorage::open(OpenOptions::new(paths, OpenMode::ReadWriteExisting)) 105 .await 106 .unwrap(); 107 assert_eq!(reopened.source_token().await.unwrap(), source_token); 108 assert_eq!( 109 reopened 110 .authored_draft_head(draft(1).draft_id()) 111 .await 112 .unwrap(), 113 Some(draft(1)) 114 ); 115 assert_eq!( 116 reopened 117 .authored_draft_head(draft(2).draft_id()) 118 .await 119 .unwrap(), 120 None 121 ); 122 reopened.close().await.unwrap(); 123 } 124 125 #[tokio::test] 126 async fn restore_spi_rejects_configuration_version_and_tampering_without_live_changes() { 127 let root = tempfile::tempdir().unwrap(); 128 let backup = tempfile::tempdir().unwrap(); 129 let (mut store, restore) = fixture(root.path(), backup.path()).await; 130 let backup_root = store.backup_root.take(); 131 assert_eq!( 132 StorageReliability::stage_restore(&store, restore.clone()).await, 133 Err(RestoreCapabilityError::InvalidConfiguration) 134 ); 135 store.backup_root = Some(std::sync::Arc::new(backup.path().join("absent"))); 136 assert_eq!( 137 StorageReliability::stage_restore(&store, restore.clone()).await, 138 Err(RestoreCapabilityError::InvalidConfiguration) 139 ); 140 store.backup_root = backup_root; 141 let original = restore.manifest(); 142 for future in [false, true] { 143 let mut members = original.members().to_vec(); 144 if !future { 145 let member = &members[0]; 146 members[0] = BackupMember::new( 147 member.relative_path(), 148 member.kind(), 149 member.byte_length() + 1, 150 member.sha256(), 151 ) 152 .unwrap(); 153 } 154 let manifest = BackupManifest::new( 155 if future { 156 BackupFormatVersion::new(2).unwrap() 157 } else { 158 BackupFormatVersion::V1 159 }, 160 original.backup_id(), 161 original.created_at_unix_ms(), 162 original.secret_policy(), 163 members, 164 ) 165 .unwrap(); 166 let bad = RestorePlan::new(manifest, original.secret_policy(), 200).unwrap(); 167 assert_eq!( 168 StorageReliability::stage_restore(&store, bad.clone()).await, 169 Err(RestoreCapabilityError::VerificationFailed) 170 ); 171 let finalization = StorageReliability::finalize_restore(&store, bad).await; 172 if future { 173 assert_eq!( 174 finalization, 175 Err(RestoreCapabilityError::UnsupportedVersion) 176 ); 177 } else { 178 assert_eq!(finalization, Err(RestoreCapabilityError::Failed)); 179 } 180 assert_eq!( 181 store.storage_status().await.unwrap().shutdown(), 182 ShutdownState::Open 183 ); 184 } 185 assert_eq!( 186 store 187 .authored_draft_head(draft(2).draft_id()) 188 .await 189 .unwrap(), 190 Some(draft(2)) 191 ); 192 store.close().await.unwrap(); 193 let reader = SqliteStorage::open( 194 OpenOptions::new( 195 crate::Paths::from_directory(root.path()).unwrap(), 196 OpenMode::ReadOnly, 197 ) 198 .with_backup_root(backup.path()) 199 .unwrap(), 200 ) 201 .await 202 .unwrap(); 203 assert_eq!( 204 StorageReliability::stage_restore(&reader, restore.clone()).await, 205 Err(RestoreCapabilityError::Unavailable) 206 ); 207 assert_eq!( 208 StorageReliability::finalize_restore(&reader, restore).await, 209 Err(RestoreCapabilityError::Unavailable) 210 ); 211 reader.close().await.unwrap(); 212 } 213 214 #[tokio::test] 215 async fn cancelled_restore_close_can_be_drained_without_releasing_a_live_writer() { 216 let root = tempfile::tempdir().unwrap(); 217 let backup = tempfile::tempdir().unwrap(); 218 let (store, restore) = fixture(root.path(), backup.path()).await; 219 StorageReliability::stage_restore(&store, restore.clone()) 220 .await 221 .unwrap(); 222 let held = store.pool.acquire().await.unwrap(); 223 let held_private = store.private_pool.acquire().await.unwrap(); 224 let mut finalization = Box::pin(StorageReliability::finalize_restore(&store, restore)); 225 tokio::time::timeout(std::time::Duration::from_secs(10), async { 226 loop { 227 std::future::poll_fn(|context| { 228 assert!(finalization.as_mut().poll(context).is_pending()); 229 std::task::Poll::Ready(()) 230 }) 231 .await; 232 if store.storage_status().await.unwrap().shutdown() == ShutdownState::Closing { 233 break; 234 } 235 tokio::task::yield_now().await; 236 } 237 }) 238 .await 239 .unwrap(); 240 drop(finalization); 241 let paths = crate::Paths::from_directory(root.path()).unwrap(); 242 assert!(matches!( 243 SqliteStorage::open(OpenOptions::new(paths.clone(), OpenMode::ReadWriteExisting)).await, 244 Err(Error::WriterAlreadyActive { .. }) 245 )); 246 drop(held); 247 let mut close = Box::pin(store.close()); 248 tokio::time::timeout(std::time::Duration::from_secs(10), async { 249 loop { 250 std::future::poll_fn(|context| { 251 assert!(close.as_mut().poll(context).is_pending()); 252 std::task::Poll::Ready(()) 253 }) 254 .await; 255 if store.private_pool.is_closed() { 256 break; 257 } 258 tokio::task::yield_now().await; 259 } 260 }) 261 .await 262 .unwrap(); 263 assert!(matches!( 264 SqliteStorage::open(OpenOptions::new(paths.clone(), OpenMode::ReadWriteExisting)).await, 265 Err(Error::WriterAlreadyActive { .. }) 266 )); 267 drop(held_private); 268 assert_eq!(close.await.unwrap().shutdown(), ShutdownState::Closed); 269 let reopened = SqliteStorage::open(OpenOptions::new(paths, OpenMode::ReadWriteExisting)) 270 .await 271 .unwrap(); 272 assert_eq!( 273 reopened 274 .authored_draft_head(draft(2).draft_id()) 275 .await 276 .unwrap(), 277 Some(draft(2)) 278 ); 279 reopened.close().await.unwrap(); 280 } 281 282 #[test] 283 fn restore_spi_errors_do_not_expose_filesystem_evidence_or_nested_causes() { 284 for raw in [ 285 Error::RestoreFilesystem { 286 operation: "/private/canary", 287 source: std::io::Error::other("credential-canary"), 288 }, 289 Error::RestoreMarkerCorrupt(PathBuf::from("/private/canary")), 290 Error::RestoreRecoveryConflict(PathBuf::from("/private/canary")), 291 Error::RestoreStagingFailed { 292 member: "credential-canary", 293 }, 294 ] { 295 let bounded = capability::map_restore_error(raw); 296 let report = format!("{bounded:?} {bounded}"); 297 assert!(!report.contains("canary") && !report.contains('/')); 298 assert!(std::error::Error::source(&bounded).is_none()); 299 } 300 }