| 1 | use super::*; |
| 2 | use crate::install::place::{finalize_install, restore_backup}; |
| 3 | use crate::install::stage::stage_local_copy; |
| 4 | |
| 5 | fn incumbent(plugins: &Path) -> PathBuf { |
| 6 | let target = write_bundle(plugins, "demo", "demo"); |
| 7 | fs::write(target.join("data.txt"), b"old payload").unwrap(); |
| 8 | fs::write(target.join(INSTALLED_FROM_MARKER), b"old provenance").unwrap(); |
| 9 | target |
| 10 | } |
| 11 | |
| 12 | fn prepared_update(root: &Path, plugins: &Path) -> super::super::stage::StagedPlugin { |
| 13 | let source = write_bundle(root, "source", "demo"); |
| 14 | fs::write(source.join("data.txt"), b"new payload").unwrap(); |
| 15 | stage_local_copy(&source, plugins, DEFAULT_MAX_SIZE_BYTES).unwrap() |
| 16 | } |
| 17 | |
| 18 | #[test] |
| 19 | fn update_preserves_a_legitimate_dot_bak_plugin() { |
| 20 | let tmp = tempfile::tempdir().unwrap(); |
| 21 | let plugins = tmp.path().join("plugins"); |
| 22 | let target = incumbent(&plugins); |
| 23 | let other = plugins.join("demo.bak"); |
| 24 | fs::create_dir(&other).unwrap(); |
| 25 | let manifest = serde_json::json!({ |
| 26 | "$schema": crate::agent_plugin::PLUGIN_SCHEMA_URL, |
| 27 | "name": "demo.bak", "version": "1.0.0" |
| 28 | }); |
| 29 | fs::write(other.join("plugin.json"), manifest.to_string()).unwrap(); |
| 30 | fs::write(other.join("canary.txt"), b"unrelated installed plugin").unwrap(); |
| 31 | let before = crate::manifest::PluginManifest::validate_from_path(&other.join("plugin.json")) |
| 32 | .unwrap() |
| 33 | .content_hash; |
| 34 | let staged = prepared_update(tmp.path(), &plugins); |
| 35 | let outcome = |
| 36 | finalize_install(staged, "path:/controlled/source", None, "", &plugins, true).unwrap(); |
| 37 | assert!(matches!(outcome, PluginInstallOutcome::Installed(_))); |
| 38 | assert_eq!(fs::read(target.join("data.txt")).unwrap(), b"new payload"); |
| 39 | assert_eq!( |
| 40 | fs::read(other.join("canary.txt")).unwrap(), |
| 41 | b"unrelated installed plugin" |
| 42 | ); |
| 43 | let after = crate::manifest::PluginManifest::validate_from_path(&other.join("plugin.json")) |
| 44 | .unwrap() |
| 45 | .content_hash; |
| 46 | assert_eq!( |
| 47 | before, after, |
| 48 | "unrelated valid bundle must be byte-identical" |
| 49 | ); |
| 50 | assert_eq!( |
| 51 | fs::read_dir(&plugins).unwrap().count(), |
| 52 | 2, |
| 53 | "successful publication leaves no backup container" |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn failed_publication_restores_the_original_bytes_and_marker() { |
| 59 | let tmp = tempfile::tempdir().unwrap(); |
| 60 | let plugins = tmp.path().join("plugins"); |
| 61 | let target = incumbent(&plugins); |
| 62 | let staged = prepared_update(tmp.path(), &plugins); |
| 63 | fs::remove_dir_all(&staged.staged_path).unwrap(); |
| 64 | let error = |
| 65 | finalize_install(staged, "path:/controlled/source", None, "", &plugins, true).unwrap_err(); |
| 66 | assert!(format!("{error:#}").contains("previous plugin restored")); |
| 67 | assert_eq!(fs::read(target.join("data.txt")).unwrap(), b"old payload"); |
| 68 | assert_eq!( |
| 69 | fs::read(target.join(INSTALLED_FROM_MARKER)).unwrap(), |
| 70 | b"old provenance" |
| 71 | ); |
| 72 | assert_eq!(fs::read_dir(&plugins).unwrap().count(), 1); |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn failed_restore_retains_the_old_copy_and_never_replaces_an_empty_contender() { |
| 77 | let tmp = tempfile::tempdir().unwrap(); |
| 78 | let container = tempfile::Builder::new() |
| 79 | .prefix(".plugin-backup-") |
| 80 | .rand_bytes(16) |
| 81 | .tempdir_in(tmp.path()) |
| 82 | .unwrap() |
| 83 | .keep(); |
| 84 | let old = write_bundle(&container, "bundle", "demo"); |
| 85 | fs::write(old.join("data.txt"), b"recoverable old payload").unwrap(); |
| 86 | fs::write(old.join(INSTALLED_FROM_MARKER), b"recoverable old marker").unwrap(); |
| 87 | let contender = tmp.path().join("demo"); |
| 88 | fs::create_dir(&contender).unwrap(); |
| 89 | let error = restore_backup( |
| 90 | &container, |
| 91 | &contender, |
| 92 | anyhow::anyhow!("publication refused"), |
| 93 | ); |
| 94 | let message = format!("{error:#}"); |
| 95 | assert!(message.contains("automatic restoration failed")); |
| 96 | assert!(message.contains(&old.display().to_string())); |
| 97 | assert!(message.contains("current destination") && message.contains("was not replaced")); |
| 98 | assert_eq!( |
| 99 | fs::read(old.join("data.txt")).unwrap(), |
| 100 | b"recoverable old payload" |
| 101 | ); |
| 102 | assert_eq!( |
| 103 | fs::read(old.join(INSTALLED_FROM_MARKER)).unwrap(), |
| 104 | b"recoverable old marker" |
| 105 | ); |
| 106 | assert!(contender.is_dir()); |
| 107 | assert_eq!( |
| 108 | fs::read_dir(&contender).unwrap().count(), |
| 109 | 0, |
| 110 | "atomic no-replace must preserve even an empty contender directory" |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn marker_failure_retains_prior_and_current_publications_for_recovery() { |
| 116 | let tmp = tempfile::tempdir().unwrap(); |
| 117 | let plugins = tmp.path().join("plugins"); |
| 118 | let target = incumbent(&plugins); |
| 119 | let staged = prepared_update(tmp.path(), &plugins); |
| 120 | // A deterministic real filesystem refusal after rename: a directory |
| 121 | // cannot be overwritten by the provenance marker's fs::write. |
| 122 | fs::create_dir(staged.staged_path.join(INSTALLED_FROM_MARKER)).unwrap(); |
| 123 | let error = |
| 124 | finalize_install(staged, "path:/controlled/source", None, "", &plugins, true).unwrap_err(); |
| 125 | let message = format!("{error:#}"); |
| 126 | assert!(message.contains("automatic restoration refused")); |
| 127 | assert!(message.contains("publication identity is not pinned")); |
| 128 | assert!(!message.contains("previous plugin restored")); |
| 129 | assert_eq!(fs::read(target.join("data.txt")).unwrap(), b"new payload"); |
| 130 | assert!(target.join(INSTALLED_FROM_MARKER).is_dir()); |
| 131 | let containers = fs::read_dir(&plugins) |
| 132 | .unwrap() |
| 133 | .map(|entry| entry.unwrap().path()) |
| 134 | .filter(|path| { |
| 135 | path.file_name() |
| 136 | .unwrap() |
| 137 | .to_string_lossy() |
| 138 | .starts_with(".plugin-backup-") |
| 139 | }) |
| 140 | .collect::<Vec<_>>(); |
| 141 | assert_eq!(containers.len(), 1); |
| 142 | #[cfg(unix)] |
| 143 | { |
| 144 | use std::os::unix::fs::PermissionsExt as _; |
| 145 | assert_eq!( |
| 146 | fs::metadata(&containers[0]).unwrap().permissions().mode() & 0o077, |
| 147 | 0, |
| 148 | "recovery container must be owner-only at creation" |
| 149 | ); |
| 150 | } |
| 151 | let retained = containers[0].join("bundle"); |
| 152 | assert!(message.contains(&retained.display().to_string())); |
| 153 | assert_eq!(fs::read(retained.join("data.txt")).unwrap(), b"old payload"); |
| 154 | assert_eq!( |
| 155 | fs::read(retained.join(INSTALLED_FROM_MARKER)).unwrap(), |
| 156 | b"old provenance" |
| 157 | ); |
| 158 | } |
| 159 |