migration_preflight_tests.rs (11805B)
1 //! Public-open regressions use only synthetic owned databases. 2 3 use super::{tests, *}; 4 use crate::{OpenOptions, Paths, SqliteStorage}; 5 use std::path::Path; 6 7 async fn file_connection(path: &Path, create: bool) -> SqliteConnection { 8 SqliteConnection::connect_with( 9 &SqliteConnectOptions::new() 10 .filename(path) 11 .create_if_missing(create), 12 ) 13 .await 14 .expect("fixture connection") 15 } 16 17 async fn fixture(runtime_version: u32, private_version: u32) -> (tempfile::TempDir, Paths) { 18 let directory = tempfile::tempdir().expect("temporary directory"); 19 let paths = Paths::from_directory(directory.path()).expect("owned paths"); 20 let mut runtime = file_connection(paths.runtime(), true).await; 21 tests::establish_runtime_version(&mut runtime, runtime_version).await; 22 sqlx::query("INSERT INTO radroots_runtime_source_generations (generation, state, created_at_unix_ms) VALUES (?, 'active', 10)") 23 .bind([7_u8; 32].as_slice()).execute(&mut runtime).await.expect("retained generation"); 24 runtime.close().await.expect("close runtime fixture"); 25 let mut private = file_connection(paths.private(), true).await; 26 tests::establish_private_version(&mut private, private_version).await; 27 private.close().await.expect("close private fixture"); 28 (directory, paths) 29 } 30 31 async fn alter(path: &Path, sql: &'static str) { 32 let mut connection = file_connection(path, false).await; 33 sqlx::raw_sql(sql) 34 .execute(&mut connection) 35 .await 36 .expect("fixture alteration"); 37 connection.close().await.expect("close fixture alteration"); 38 } 39 40 async fn assert_refused_unchanged(paths: Paths, mode: OpenMode, expected: fn(&Error) -> bool) { 41 let runtime = std::fs::read(paths.runtime()).expect("runtime bytes"); 42 let private = std::fs::read(paths.private()).expect("private bytes"); 43 for _ in 0..2 { 44 let error = match SqliteStorage::open(OpenOptions::new(paths.clone(), mode)).await { 45 Ok(store) => { 46 store.close().await.expect("close unexpected store"); 47 panic!("incompatible pair opened") 48 } 49 Err(error) => error, 50 }; 51 assert!(expected(&error), "unexpected error: {error:?}"); 52 assert!( 53 std::fs::read(paths.runtime()).expect("retained runtime") == runtime, 54 "runtime bytes changed" 55 ); 56 assert!( 57 std::fs::read(paths.private()).expect("retained private") == private, 58 "private bytes changed" 59 ); 60 } 61 } 62 63 #[tokio::test] 64 async fn future_private_schema_preserves_pending_runtime_and_both_original_files() { 65 let (_directory, paths) = fixture(16, private::CURRENT_VERSION).await; 66 alter(paths.private(), "PRAGMA user_version = 999").await; 67 assert_refused_unchanged(paths, OpenMode::ReadWriteExisting, |error| { 68 matches!( 69 error, 70 Error::SchemaTooNew { 71 database: PRIVATE_DATABASE, 72 actual: 999, 73 .. 74 } 75 ) 76 }) 77 .await; 78 } 79 80 #[tokio::test] 81 async fn foreign_private_namespace_preserves_pending_runtime_and_both_original_files() { 82 let (_directory, paths) = fixture(16, private::CURRENT_VERSION).await; 83 alter(paths.private(), "PRAGMA application_id = 42").await; 84 assert_refused_unchanged(paths, OpenMode::ReadWriteExisting, |error| { 85 matches!( 86 error, 87 Error::SchemaIdentityMismatch { 88 database: PRIVATE_DATABASE, 89 actual: 42, 90 .. 91 } 92 ) 93 }) 94 .await; 95 } 96 97 #[tokio::test] 98 async fn unexpected_private_catalog_preserves_pending_runtime_and_both_original_files() { 99 let (_directory, paths) = fixture(16, private::CURRENT_VERSION).await; 100 alter(paths.private(), "CREATE TABLE foreign_state (value TEXT)").await; 101 assert_refused_unchanged(paths, OpenMode::ReadWriteExisting, |error| { 102 matches!( 103 error, 104 Error::SchemaCatalogMismatch { 105 database: PRIVATE_DATABASE, 106 .. 107 } 108 ) 109 }) 110 .await; 111 } 112 113 #[tokio::test] 114 async fn future_runtime_schema_preserves_pending_private_and_both_original_files() { 115 let (_directory, paths) = fixture(runtime::CURRENT_VERSION, 3).await; 116 alter(paths.runtime(), "PRAGMA user_version = 999").await; 117 assert_refused_unchanged(paths, OpenMode::ReadWriteExisting, |error| { 118 matches!( 119 error, 120 Error::SchemaTooNew { 121 database: RUNTIME_DATABASE, 122 actual: 999, 123 .. 124 } 125 ) 126 }) 127 .await; 128 } 129 130 #[tokio::test] 131 async fn read_only_pending_pair_preserves_both_original_files() { 132 let (_directory, paths) = fixture(16, 3).await; 133 assert_refused_unchanged(paths, OpenMode::ReadOnly, |error| { 134 matches!( 135 error, 136 Error::SchemaMigrationRequired { 137 database: RUNTIME_DATABASE, 138 actual: 16, 139 .. 140 } 141 ) 142 }) 143 .await; 144 } 145 146 #[tokio::test] 147 async fn compatible_pending_pair_migrates_and_reopens_with_same_generation() { 148 use radroots_storage::EventStore; 149 let (_directory, paths) = fixture(16, 3).await; 150 for _ in 0..2 { 151 let store = 152 SqliteStorage::open(OpenOptions::new(paths.clone(), OpenMode::ReadWriteExisting)) 153 .await 154 .expect("supported pair"); 155 if matches!( 156 store.status().await, 157 Err(radroots_storage::Error::VisibilityRebuilding) 158 ) { 159 assert!( 160 store 161 .maintain_visibility( 162 radroots_storage::event::VisibilityIndexBounds::new(64, 16 * 1024 * 1024) 163 .unwrap() 164 ) 165 .await 166 .expect("explicit empty-index initialization") 167 .is_complete() 168 ); 169 } 170 assert_eq!( 171 store 172 .status() 173 .await 174 .expect("status") 175 .generation() 176 .as_bytes(), 177 &[7; 32] 178 ); 179 store.close().await.expect("close supported pair"); 180 } 181 for (path, version) in [ 182 (paths.runtime(), runtime::CURRENT_VERSION), 183 (paths.private(), private::CURRENT_VERSION), 184 ] { 185 let mut connection = file_connection(path, false).await; 186 assert_eq!( 187 tests::pragma(&mut connection, "user_version").await, 188 i64::from(version) 189 ); 190 connection.close().await.expect("close inspected fixture"); 191 } 192 } 193 194 #[tokio::test] 195 async fn unversioned_namespace_collision_preserves_both_original_files() { 196 let (_directory, paths) = fixture(16, private::CURRENT_VERSION).await; 197 alter( 198 paths.private(), 199 "PRAGMA user_version = 0; PRAGMA application_id = 0", 200 ) 201 .await; 202 assert_refused_unchanged(paths, OpenMode::ReadWriteExisting, |error| { 203 matches!( 204 error, 205 Error::UnrecognizedSchema { 206 database: PRIVATE_DATABASE 207 } 208 ) 209 }) 210 .await; 211 } 212 213 #[tokio::test] 214 async fn corrupt_private_member_preserves_pending_runtime_and_corrupt_evidence() { 215 let (_directory, paths) = fixture(16, private::CURRENT_VERSION).await; 216 std::fs::write(paths.private(), b"synthetic invalid SQLite evidence").expect("corrupt fixture"); 217 assert_refused_unchanged(paths, OpenMode::ReadWriteExisting, |error| { 218 matches!( 219 error, 220 Error::DatabaseCorrupt { 221 database: PRIVATE_DATABASE 222 } 223 ) 224 }) 225 .await; 226 } 227 228 #[tokio::test] 229 async fn incompatible_private_member_prevents_creation_of_missing_runtime() { 230 use radroots_storage::event::SourceGeneration; 231 let (_directory, paths) = fixture(16, private::CURRENT_VERSION).await; 232 std::fs::remove_file(paths.runtime()).expect("remove synthetic runtime"); 233 alter(paths.private(), "PRAGMA user_version = 999").await; 234 let before = std::fs::read(paths.private()).expect("private evidence"); 235 for _ in 0..2 { 236 let options = OpenOptions::new(paths.clone(), OpenMode::Create) 237 .with_source_generation(SourceGeneration::new([9; 32]).expect("generation"), 10) 238 .expect("options"); 239 assert!(matches!( 240 SqliteStorage::open(options).await, 241 Err(Error::SchemaTooNew { 242 database: PRIVATE_DATABASE, 243 actual: 999, 244 .. 245 }) 246 )); 247 assert!(!paths.runtime().exists()); 248 assert!(std::fs::read(paths.private()).expect("private retained") == before); 249 } 250 } 251 252 #[tokio::test] 253 async fn ineligible_v10_authored_evidence_preserves_both_original_files() { 254 let (_directory, paths) = fixture(10, 3).await; 255 alter( 256 paths.runtime(), 257 "INSERT INTO radroots_runtime_journal_operations ( 258 instance_id, operation_id, idempotency_key, input_digest, prepared_at_unix_ms, 259 revision, stage, cancellation_state, updated_at_unix_ms 260 ) VALUES (zeroblob(16), x'ff', 'retained-invalid-journal', zeroblob(32), 10, 261 1, 'prepared', 'not_requested', 10)", 262 ) 263 .await; 264 assert_refused_unchanged(paths, OpenMode::ReadWriteExisting, |error| { 265 matches!( 266 error, 267 Error::AuthoredMigrationBlocked { 268 invalid_or_unsupported: 1, 269 .. 270 } 271 ) 272 }) 273 .await; 274 } 275 276 #[tokio::test] 277 async fn incompatible_generation_preserves_pending_schemas_and_original_files() { 278 use radroots_storage::event::SourceGeneration; 279 let (_directory, paths) = fixture(16, 3).await; 280 let runtime = std::fs::read(paths.runtime()).expect("runtime evidence"); 281 let private = std::fs::read(paths.private()).expect("private evidence"); 282 for _ in 0..2 { 283 let options = OpenOptions::new(paths.clone(), OpenMode::ReadWriteExisting) 284 .with_source_generation(SourceGeneration::new([9; 32]).expect("generation"), 10) 285 .expect("options"); 286 assert!(matches!( 287 SqliteStorage::open(options).await, 288 Err(Error::SourceGenerationMismatch) 289 )); 290 assert!( 291 std::fs::read(paths.runtime()).expect("retained runtime") == runtime, 292 "runtime bytes changed before rejecting generation" 293 ); 294 assert!( 295 std::fs::read(paths.private()).expect("retained private") == private, 296 "private bytes changed before rejecting generation" 297 ); 298 } 299 } 300 301 #[test] 302 fn unrelated_metadata_failure_retains_typed_metadata_diagnostic() { 303 assert!(matches!( 304 schema_metadata_error(&sqlx::Error::RowNotFound, PRIVATE_DATABASE), 305 Error::SchemaMetadataUnavailable { 306 database: PRIVATE_DATABASE 307 } 308 )); 309 } 310 311 #[tokio::test] 312 async fn unfingerprinted_private_v2_envelope_preserves_both_original_files() { 313 let (_directory, paths) = fixture(16, 3).await; 314 alter( 315 paths.private(), 316 "INSERT INTO radroots_private_artifacts ( 317 artifact_id, artifact_kind, schema_id, commitment, protected_size_bytes, 318 secret_provider, secret_reference, key_version, envelope_version, 319 encrypted_envelope, revision, stage, created_at_unix_ms, updated_at_unix_ms 320 ) VALUES (zeroblob(16), 'trade.private_terms', 'trade.private_terms.v1', zeroblob(32), 1, 321 'memory', 'synthetic-key', 1, 2, x'03', 1, 'active', 1, 1)", 322 ) 323 .await; 324 assert_refused_unchanged(paths, OpenMode::ReadWriteExisting, |error| { 325 matches!( 326 error, 327 Error::SchemaMigrationFailed { 328 database: PRIVATE_DATABASE, 329 target_version: 4 330 } 331 ) 332 }) 333 .await; 334 }