| 84b9e9ac Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use super::*; |
| 7 |
use crate::identity::SecretKey; |
| 8 |
|
| 9 |
|
| 10 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 11 |
pub struct FileEdit { |
| 12 |
pub path: String, |
| 13 |
pub content: Option<Vec<u8>>, |
| 14 |
} |
| 15 |
|
| 16 |
impl Repo { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| eae59556 Andy McCall ↶ |
22 |
pub fn build_change( |
| 23 |
&self, |
| 24 |
heads: &[ChangeHash], |
| 25 |
edits: &[FileEdit], |
| 26 |
message: &str, |
| 27 |
author: KeyId, |
| 28 |
) -> Result<Change> { |
| f2f91ceb Andy McCall ↶ |
29 |
self.build_change_with(heads, edits, &[], message, author) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
pub fn build_pin_change( |
| 37 |
&self, |
| 38 |
heads: &[ChangeHash], |
| 39 |
path: &str, |
| 40 |
pin: &crate::subrepo::Pin, |
| 41 |
message: &str, |
| 42 |
author: KeyId, |
| 43 |
) -> Result<Change> { |
| 44 |
self.build_change_with( |
| 45 |
heads, |
| 46 |
&[], |
| 47 |
&[(path.to_string(), pin.clone())], |
| 48 |
message, |
| 49 |
author, |
| 50 |
) |
| 51 |
} |
| 52 |
|
| 53 |
fn build_change_with( |
| 54 |
&self, |
| 55 |
heads: &[ChangeHash], |
| 56 |
edits: &[FileEdit], |
| 57 |
pins: &[(String, crate::subrepo::Pin)], |
| 58 |
message: &str, |
| 59 |
author: KeyId, |
| 60 |
) -> Result<Change> { |
| 84b9e9ac Andy McCall ↶ |
61 |
if message.trim().is_empty() { |
| 62 |
return fail("a change message is required"); |
| 63 |
} |
| f2f91ceb Andy McCall ↶ |
64 |
if edits.is_empty() && pins.is_empty() { |
| 84b9e9ac Andy McCall ↶ |
65 |
return fail("nothing to change"); |
| 66 |
} |
| 67 |
let base = self.load_state(heads)?; |
| 68 |
let base_snapshot = base.materialize(); |
| 69 |
let mut entries: BTreeMap<String, FsEntry> = BTreeMap::new(); |
| 70 |
for (path, f) in &base_snapshot.files { |
| 71 |
let entry = match (&f.kind, &f.content) { |
| 72 |
(NodeKind::Dir, _) | (_, FileContent::Dir) => continue, |
| eae59556 Andy McCall ↶ |
73 |
(kind, FileContent::Text { bytes, .. }) => { |
| 74 |
FsEntry::new(*kind, f.mode.exec, bytes.clone()) |
| 75 |
} |
| 76 |
(kind, FileContent::Opaque { blobs }) => FsEntry { |
| 77 |
blob: blobs.first().copied(), |
| cc7a0056 Andy McCall ↶ |
78 |
..FsEntry::new(*kind, f.mode.exec, vec![]) |
| eae59556 Andy McCall ↶ |
79 |
}, |
| 84b9e9ac Andy McCall ↶ |
80 |
}; |
| 81 |
entries.insert(path.clone(), entry); |
| 82 |
} |
| 83 |
for e in edits { |
| 84 |
let path = e.path.trim_matches('/'); |
| eae59556 Andy McCall ↶ |
85 |
if path.is_empty() |
| 86 |
|| path |
| 87 |
.split('/') |
| 88 |
.any(|c| c.is_empty() || c == "." || c == ".." || c == OXY_DIR) |
| 89 |
{ |
| 84b9e9ac Andy McCall ↶ |
90 |
return fail(format!("{:?} is not a valid path", e.path)); |
| 91 |
} |
| 92 |
match &e.content { |
| 93 |
Some(bytes) => { |
| 94 |
let exec = entries.get(path).map(|x| x.exec).unwrap_or(false); |
| 95 |
|
| eae59556 Andy McCall ↶ |
96 |
if base_snapshot |
| 97 |
.files |
| 98 |
.get(path) |
| 99 |
.is_some_and(|f| f.kind == NodeKind::Dir) |
| 100 |
{ |
| 84b9e9ac Andy McCall ↶ |
101 |
return fail(format!("{path} is a directory")); |
| 102 |
} |
| 103 |
let mut parent = path; |
| 104 |
while let Some((p, _)) = parent.rsplit_once('/') { |
| eae59556 Andy McCall ↶ |
105 |
if base_snapshot |
| 106 |
.files |
| 107 |
.get(p) |
| 108 |
.is_some_and(|f| f.kind != NodeKind::Dir) |
| 109 |
{ |
| 84b9e9ac Andy McCall ↶ |
110 |
return fail(format!("{p} is a file, not a directory")); |
| 111 |
} |
| 112 |
parent = p; |
| 113 |
} |
| eae59556 Andy McCall ↶ |
114 |
entries.insert( |
| 115 |
path.to_string(), |
| 116 |
FsEntry::new(NodeKind::File, exec, bytes.clone()), |
| 117 |
); |
| 84b9e9ac Andy McCall ↶ |
118 |
} |
| 119 |
None => { |
| 120 |
if entries.remove(path).is_none() { |
| 121 |
|
| 122 |
let prefix = format!("{path}/"); |
| 123 |
let before = entries.len(); |
| 124 |
entries.retain(|p, _| !p.starts_with(&prefix)); |
| 125 |
if entries.len() == before { |
| 126 |
return fail(format!("{path} does not exist")); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| f2f91ceb Andy McCall ↶ |
130 |
} |
| 131 |
} |
| 132 |
for (path, pin) in pins { |
| 133 |
let path = path.trim_matches('/'); |
| 134 |
match base_snapshot.files.get(path) { |
| 135 |
Some(f) if f.kind == NodeKind::Subrepo => {} |
| 136 |
Some(_) => return fail(format!("{path} is not a subrepo")), |
| 137 |
None => return fail(format!("{path} does not exist")), |
| 84b9e9ac Andy McCall ↶ |
138 |
} |
| f2f91ceb Andy McCall ↶ |
139 |
entries.insert( |
| 140 |
path.to_string(), |
| 141 |
FsEntry::new(NodeKind::Subrepo, false, pin.encode()), |
| 142 |
); |
| 84b9e9ac Andy McCall ↶ |
143 |
} |
| f7510b63 Andy McCall ↶ |
144 |
|
| 145 |
|
| 146 |
let written: std::collections::HashSet<String> = edits |
| 147 |
.iter() |
| 148 |
.filter(|e| e.content.is_some()) |
| 149 |
.map(|e| e.path.trim_matches('/').to_string()) |
| f2f91ceb Andy McCall ↶ |
150 |
.chain(pins.iter().map(|(p, _)| p.trim_matches('/').to_string())) |
| f7510b63 Andy McCall ↶ |
151 |
.collect(); |
| 152 |
let built = self.build_from_entries_resolving( |
| 153 |
&base, |
| 154 |
&base_snapshot, |
| 155 |
HashMap::new(), |
| 156 |
&entries, |
| 157 |
&written, |
| 158 |
)?; |
| eae59556 Andy McCall ↶ |
159 |
if let Some(s) = built |
| 160 |
.statuses |
| 161 |
.iter() |
| 162 |
.find(|s| s.status == ChangeStatus::ConflictEdited) |
| 163 |
{ |
| 84b9e9ac Andy McCall ↶ |
164 |
return fail(format!("{} still contains conflict markers", s.path)); |
| 165 |
} |
| 166 |
if built.tree_ops.is_empty() && built.edits.is_empty() { |
| 167 |
return fail("the edits change nothing"); |
| 168 |
} |
| 169 |
let mut change = Change { |
| 6f8aeb99 Andy McCall ↶ |
170 |
version: crate::objects::CHANGE_VERSION, |
| 84b9e9ac Andy McCall ↶ |
171 |
change_id: ChangeId::random(), |
| 172 |
deps: vec![], |
| 173 |
author, |
| 174 |
hlc: Hlc::now(), |
| 175 |
message: message.trim_end().to_string(), |
| 176 |
tree_ops: built.tree_ops, |
| 177 |
edits: built.edits, |
| eae59556 Andy McCall ↶ |
178 |
kind: if built.resolves { |
| 179 |
ChangeKind::Resolution |
| 180 |
} else { |
| 181 |
ChangeKind::Normal |
| 182 |
}, |
| 84b9e9ac Andy McCall ↶ |
183 |
}; |
| 184 |
let mut deps: BTreeSet<ChangeHash> = referenced_changes(&change, &base); |
| 185 |
deps.extend(built.contributors.iter().copied()); |
| 186 |
change.deps = deps.into_iter().collect(); |
| 187 |
change.normalize(); |
| 188 |
Ok(change) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| eae59556 Andy McCall ↶ |
193 |
pub fn host_ref_update( |
| 194 |
&self, |
| 195 |
bookmark: &str, |
| 196 |
heads: &[ChangeHash], |
| 197 |
signer: Option<(&KeyId, &SecretKey)>, |
| 198 |
) -> Result<RefUpdate> { |
| 84b9e9ac Andy McCall ↶ |
199 |
let view = self.view()?; |
| 200 |
let empty = self.store.put_state(&StateObject::new(vec![]))?; |
| 201 |
let old_state = view.bookmarks.get(bookmark).copied().unwrap_or(empty); |
| 202 |
let new_state = self.store.put_state(&StateObject::new(heads.to_vec()))?; |
| 203 |
let attestation = match signer { |
| 204 |
Some((key, sk)) => { |
| 205 |
let serial = self |
| 206 |
.latest_ref_attest(bookmark)? |
| 207 |
.and_then(|(_, a)| match a.attest.body { |
| 208 |
AttestBody::Ref { serial, .. } => Some(serial), |
| 209 |
_ => None, |
| 210 |
}) |
| 211 |
.map(|s| s + 1) |
| 212 |
.unwrap_or(1); |
| 213 |
let attest = Attest { |
| 214 |
repo: self.id()?, |
| 215 |
hlc: Hlc::now(), |
| eae59556 Andy McCall ↶ |
216 |
body: AttestBody::Ref { |
| 217 |
name: bookmark.to_string(), |
| 218 |
old_state, |
| 219 |
new_state, |
| 220 |
serial, |
| 221 |
}, |
| 84b9e9ac Andy McCall ↶ |
222 |
}; |
| 223 |
let r = attest.hash(); |
| 224 |
let sig = sk.sign(Domain::Attest, key, &r); |
| eae59556 Andy McCall ↶ |
225 |
Some(SignedAttest { |
| 226 |
attest, |
| 227 |
sigs: vec![sig], |
| 228 |
}) |
| 84b9e9ac Andy McCall ↶ |
229 |
} |
| 230 |
None => None, |
| 231 |
}; |
| eae59556 Andy McCall ↶ |
232 |
Ok(RefUpdate { |
| 233 |
name: bookmark.to_string(), |
| 234 |
old_state, |
| 235 |
new_state, |
| 236 |
force: false, |
| 237 |
attestation, |
| 238 |
}) |
| 84b9e9ac Andy McCall ↶ |
239 |
} |
| 240 |
|
| b63fc7a7 Andy McCall ↶ |
241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
pub fn host_tag_create( |
| 246 |
&self, |
| 247 |
name: &str, |
| 248 |
heads: &[ChangeHash], |
| 249 |
message: &str, |
| 250 |
signer: (&KeyId, &SecretKey), |
| 251 |
identities: &[Identity], |
| 252 |
) -> Result<PushReport> { |
| 253 |
crate::objects::validate_name(name) |
| 254 |
.map_err(|e| RepoError::Msg(format!("tag name: {e}")))?; |
| 255 |
let st = self.load_state_history(heads)?; |
| 256 |
let heads = st.maximal(heads); |
| 257 |
if heads.is_empty() { |
| 258 |
return fail("a tag needs at least one change"); |
| 259 |
} |
| 260 |
let state_object = StateObject::new(heads.clone()); |
| 261 |
let state = state_object.hash(); |
| 262 |
let body = AttestBody::Tag { |
| 263 |
name: name.into(), |
| 264 |
state, |
| 265 |
message: message.into(), |
| 266 |
}; |
| 267 |
let mut bundle = self.hosted_bundle(identities); |
| 268 |
bundle |
| 269 |
.objects |
| 270 |
.push((ObjectType::State, state_object.encode())); |
| 271 |
let t = crate::protocol::TagRef::new( |
| 272 |
name.into(), |
| 273 |
state, |
| 274 |
heads, |
| 275 |
Some(self.host_attest(body, signer)?), |
| 276 |
); |
| 277 |
self.receive_push_report(bundle, vec![], None, vec![t]) |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
pub fn host_tag_withdrawal( |
| 284 |
&self, |
| 285 |
name: &str, |
| 286 |
reason: &str, |
| 287 |
signer: (&KeyId, &SecretKey), |
| 288 |
identities: &[Identity], |
| 289 |
) -> Result<PushReport> { |
| 290 |
let Some(state) = self.view()?.tags.get(name).copied() else { |
| 291 |
return fail(format!("no tag {name:?}")); |
| 292 |
}; |
| 293 |
let body = AttestBody::Untag { |
| 294 |
name: name.into(), |
| 295 |
state, |
| 296 |
reason: reason.into(), |
| 297 |
}; |
| 298 |
let t = crate::protocol::TagRef::new( |
| 299 |
name.into(), |
| 300 |
state, |
| 301 |
vec![], |
| 302 |
Some(self.host_attest(body, signer)?), |
| 303 |
); |
| 304 |
self.receive_push_report(self.hosted_bundle(identities), vec![], None, vec![t]) |
| 305 |
} |
| 306 |
|
| 307 |
fn host_attest( |
| 308 |
&self, |
| 309 |
body: AttestBody, |
| 310 |
(key, sk): (&KeyId, &SecretKey), |
| 311 |
) -> Result<SignedAttest> { |
| 312 |
let attest = Attest { |
| 313 |
repo: self.id()?, |
| 314 |
hlc: Hlc::now(), |
| 315 |
body, |
| 316 |
}; |
| 317 |
let sig = sk.sign(Domain::Attest, key, &attest.hash()); |
| 318 |
Ok(SignedAttest { |
| 319 |
attest, |
| 320 |
sigs: vec![sig], |
| 321 |
}) |
| 322 |
} |
| 323 |
|
| 324 |
fn hosted_bundle(&self, identities: &[Identity]) -> Bundle { |
| 325 |
let mut bundle = Bundle::default(); |
| 326 |
for id in identities { |
| 327 |
bundle.objects.push((ObjectType::Identity, id.encode())); |
| 328 |
} |
| 329 |
bundle |
| 330 |
} |
| 331 |
|
| 84b9e9ac Andy McCall ↶ |
332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| eae59556 Andy McCall ↶ |
336 |
pub fn apply_hosted( |
| 337 |
&self, |
| 338 |
change: Option<(Change, Vec<Signature>)>, |
| 339 |
identities: &[Identity], |
| 340 |
update: RefUpdate, |
| 341 |
) -> Result<PushReport> { |
| 84b9e9ac Andy McCall ↶ |
342 |
let mut bundle = Bundle::default(); |
| 343 |
for id in identities { |
| 344 |
bundle.objects.push((ObjectType::Identity, id.encode())); |
| 345 |
} |
| 346 |
if let Ok(st) = self.store.get_state(&update.new_state) { |
| 347 |
bundle.objects.push((ObjectType::State, st.encode())); |
| 348 |
} |
| 349 |
if let Some((c, sigs)) = change { |
| 350 |
bundle.changes.push((c, sigs)); |
| 351 |
} |
| 352 |
self.receive_push_report(bundle, vec![update], None, vec![]) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
#[cfg(test)] |
| 357 |
mod tests { |
| 358 |
use super::*; |
| 359 |
use crate::identity::SecretKey; |
| 360 |
|
| 361 |
fn hermetic() { |
| 362 |
static ONCE: std::sync::Once = std::sync::Once::new(); |
| 363 |
static DIR: std::sync::OnceLock<tempfile::TempDir> = std::sync::OnceLock::new(); |
| 364 |
ONCE.call_once(|| { |
| 365 |
let d = tempfile::tempdir().unwrap(); |
| 366 |
std::env::set_var("OXY_HOME", d.path()); |
| 367 |
DIR.set(d).ok(); |
| 368 |
}); |
| 369 |
} |
| 370 |
|
| 371 |
fn write(root: &Path, rel: &str, content: &[u8]) { |
| 372 |
let p = root.join(rel); |
| 373 |
fs::create_dir_all(p.parent().unwrap()).unwrap(); |
| 374 |
fs::write(p, content).unwrap(); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| eae59556 Andy McCall ↶ |
379 |
fn setup( |
| 380 |
root: &Path, |
| 381 |
with_policy: bool, |
| 382 |
delegate: bool, |
| 383 |
) -> (Repo, Repo, KeyId, SecretKey, Identity) { |
| 84b9e9ac Andy McCall ↶ |
384 |
hermetic(); |
| 385 |
let host_dir = root.join("host"); |
| 386 |
Repo::init_bare_unbound(&host_dir).unwrap(); |
| 387 |
let work = root.join("work"); |
| 388 |
let mut user = Repo::init(&work).unwrap(); |
| eae59556 Andy McCall ↶ |
389 |
user.create_identity("Andy", "[email protected]", IdentityScope::Repo) |
| 390 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
391 |
user.config.set("core.sign", "change").unwrap(); |
| eae59556 Andy McCall ↶ |
392 |
user.config |
| 393 |
.set("remote.origin.url", host_dir.to_str().unwrap()) |
| 394 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
395 |
user.save_config().unwrap(); |
| 396 |
let hub_sk = SecretKey::generate(); |
| 397 |
let hub_id = hub_sk.identity("Hub", "hub@example", 1); |
| 398 |
let hub_key = hub_id.hash(); |
| 399 |
if with_policy { |
| 400 |
user.policy_init().unwrap(); |
| 401 |
} |
| 402 |
if delegate { |
| 7ba155f2 Andy McCall ↶ |
403 |
user.delegate_add(hub_id.clone(), Scope::CHANGES | Scope::REFS, None) |
| 404 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
405 |
} |
| 406 |
write(&work, "a.txt", b"one\ntwo\n"); |
| 407 |
user.snapshot("t").unwrap(); |
| 408 |
user.commit("first", &[]).unwrap(); |
| 409 |
let r = user.push("origin", false).unwrap(); |
| 410 |
assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| eae59556 Andy McCall ↶ |
411 |
( |
| 412 |
Repo::open(&host_dir).unwrap(), |
| 413 |
user, |
| 414 |
hub_key, |
| 415 |
hub_sk, |
| 416 |
hub_id, |
| 417 |
) |
| 84b9e9ac Andy McCall ↶ |
418 |
} |
| 419 |
|
| 420 |
#[test] |
| 421 |
fn a_host_edit_signed_by_a_delegate_lands() { |
| 422 |
let dir = tempfile::tempdir().unwrap(); |
| 423 |
let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true); |
| 424 |
let author = user.config.author_key.unwrap(); |
| 425 |
let heads = host.bookmark_heads().unwrap(); |
| eae59556 Andy McCall ↶ |
426 |
let change = host |
| 427 |
.build_change( |
| 428 |
&heads, |
| 429 |
&[ |
| 430 |
FileEdit { |
| 431 |
path: "a.txt".into(), |
| 432 |
content: Some(b"one\ntwo\nthree\n".to_vec()), |
| 433 |
}, |
| 434 |
FileEdit { |
| 435 |
path: "docs/new.md".into(), |
| 436 |
content: Some(b"# New\n".to_vec()), |
| 437 |
}, |
| 438 |
], |
| 439 |
"Edit on the web", |
| 440 |
author, |
| 441 |
) |
| 442 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
443 |
assert_eq!(change.author, author); |
| 444 |
assert_eq!(change.kind, ChangeKind::Normal); |
| 445 |
let hash = change.hash(); |
| 446 |
let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| 447 |
let mut new_heads = heads.clone(); |
| 448 |
new_heads.push(hash); |
| eae59556 Andy McCall ↶ |
449 |
let update = host |
| 450 |
.host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 451 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
452 |
let ids = vec![hub_id, user.identity(&author).unwrap()]; |
| eae59556 Andy McCall ↶ |
453 |
let report = host |
| 454 |
.apply_hosted(Some((change, vec![sig])), &ids, update) |
| 455 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
456 |
assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 457 |
assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| eae59556 Andy McCall ↶ |
458 |
assert_eq!( |
| 459 |
host.file_at("a.txt", Some(&[hash])).unwrap(), |
| 460 |
b"one\ntwo\nthree\n" |
| 461 |
); |
| 462 |
assert_eq!( |
| 463 |
host.file_at("docs/new.md", Some(&[hash])).unwrap(), |
| 464 |
b"# New\n" |
| 465 |
); |
| 84b9e9ac Andy McCall ↶ |
466 |
|
| eae59556 Andy McCall ↶ |
467 |
assert!( |
| 468 |
matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Valid { key, name, .. } if key == hub_key && name == "Hub") |
| 469 |
); |
| 84b9e9ac Andy McCall ↶ |
470 |
let _ = new_heads; |
| 471 |
|
| eae59556 Andy McCall ↶ |
472 |
assert!(host |
| 473 |
.build_change( |
| 474 |
&[hash], |
| 475 |
&[FileEdit { |
| 476 |
path: "../x".into(), |
| 477 |
content: Some(vec![]) |
| 478 |
}], |
| 479 |
"m", |
| 480 |
author |
| 481 |
) |
| 482 |
.is_err()); |
| 483 |
assert!( |
| 484 |
host.build_change( |
| 485 |
&[hash], |
| 486 |
&[FileEdit { |
| 487 |
path: "a.txt".into(), |
| 488 |
content: Some(b"one\ntwo\nthree\n".to_vec()) |
| 489 |
}], |
| 490 |
"m", |
| 491 |
author |
| 492 |
) |
| 493 |
.is_err(), |
| 494 |
"no-op edits are refused" |
| 495 |
); |
| 496 |
assert!( |
| 497 |
host.build_change( |
| 498 |
&[hash], |
| 499 |
&[FileEdit { |
| 500 |
path: "a.txt/b".into(), |
| 501 |
content: Some(vec![]) |
| 502 |
}], |
| 503 |
"m", |
| 504 |
author |
| 505 |
) |
| 506 |
.is_err(), |
| 507 |
"a file inside a file" |
| 508 |
); |
| 509 |
let rm = host |
| 510 |
.build_change( |
| 511 |
&[hash], |
| 512 |
&[FileEdit { |
| 513 |
path: "docs".into(), |
| 514 |
content: None, |
| 515 |
}], |
| 516 |
"Remove docs", |
| 517 |
author, |
| 518 |
) |
| 519 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
520 |
assert!(!rm.tree_ops.is_empty()); |
| 521 |
} |
| 522 |
|
| 523 |
#[test] |
| 524 |
fn a_host_edit_by_a_key_that_is_not_a_delegate_is_refused() { |
| 525 |
let dir = tempfile::tempdir().unwrap(); |
| 526 |
let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, false); |
| 527 |
let author = user.config.author_key.unwrap(); |
| 528 |
let heads = host.bookmark_heads().unwrap(); |
| eae59556 Andy McCall ↶ |
529 |
let change = host |
| 530 |
.build_change( |
| 531 |
&heads, |
| 532 |
&[FileEdit { |
| 533 |
path: "a.txt".into(), |
| 534 |
content: Some(b"x\n".to_vec()), |
| 535 |
}], |
| 536 |
"Edit", |
| 537 |
author, |
| 538 |
) |
| 539 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
540 |
let hash = change.hash(); |
| 541 |
let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| eae59556 Andy McCall ↶ |
542 |
let update = host |
| 543 |
.host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 544 |
.unwrap(); |
| 545 |
let report = host |
| 546 |
.apply_hosted( |
| 547 |
Some((change, vec![sig])), |
| 548 |
&[hub_id, user.identity(&author).unwrap()], |
| 549 |
update, |
| 550 |
) |
| 551 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
552 |
|
| 553 |
|
| 554 |
assert!(report.results.iter().all(|r| r.ok)); |
| eae59556 Andy McCall ↶ |
555 |
assert!( |
| 556 |
matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Invalid(why) if why.contains("neither the author")) |
| 557 |
); |
| 84b9e9ac Andy McCall ↶ |
558 |
} |
| 559 |
|
| 560 |
#[test] |
| 561 |
fn under_a_policy_a_delegate_may_update_the_reference_and_a_stranger_may_not() { |
| 562 |
let dir = tempfile::tempdir().unwrap(); |
| 563 |
let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), true, true); |
| 564 |
assert!(host.current_policy().unwrap().is_some()); |
| 565 |
let author = user.config.author_key.unwrap(); |
| 566 |
let heads = host.bookmark_heads().unwrap(); |
| eae59556 Andy McCall ↶ |
567 |
let change = host |
| 568 |
.build_change( |
| 569 |
&heads, |
| 570 |
&[FileEdit { |
| 571 |
path: "a.txt".into(), |
| 572 |
content: Some(b"one\n".to_vec()), |
| 573 |
}], |
| 574 |
"Trim", |
| 575 |
author, |
| 576 |
) |
| 577 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
578 |
let hash = change.hash(); |
| 579 |
let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| 580 |
let ids = vec![hub_id.clone(), user.identity(&author).unwrap()]; |
| eae59556 Andy McCall ↶ |
581 |
let update = host |
| 582 |
.host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 583 |
.unwrap(); |
| 584 |
let report = host |
| 585 |
.apply_hosted(Some((change, vec![sig])), &ids, update) |
| 586 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
587 |
assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 588 |
assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| 589 |
|
| 590 |
|
| 591 |
let stranger = SecretKey::generate(); |
| 592 |
let sid = stranger.identity("Mallory", "m@example", 1); |
| 593 |
let skey = sid.hash(); |
| eae59556 Andy McCall ↶ |
594 |
let change2 = host |
| 595 |
.build_change( |
| 596 |
&[hash], |
| 597 |
&[FileEdit { |
| 598 |
path: "a.txt".into(), |
| 599 |
content: Some(b"evil\n".to_vec()), |
| 600 |
}], |
| 601 |
"Evil", |
| 602 |
author, |
| 603 |
) |
| 604 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
605 |
let h2 = change2.hash(); |
| 606 |
let sig2 = stranger.sign(Domain::Change, &skey, &h2); |
| eae59556 Andy McCall ↶ |
607 |
let update = host |
| 608 |
.host_ref_update("main", &[h2], Some((&skey, &stranger))) |
| 609 |
.unwrap(); |
| 610 |
let report = host |
| 611 |
.apply_hosted(Some((change2, vec![sig2])), &[sid], update) |
| 612 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
613 |
assert!(report.results.iter().all(|r| !r.ok), "{:?}", report.results); |
| eae59556 Andy McCall ↶ |
614 |
assert_eq!( |
| 615 |
host.bookmark_heads().unwrap(), |
| 616 |
vec![hash], |
| 617 |
"the reference did not move" |
| 618 |
); |
| 84b9e9ac Andy McCall ↶ |
619 |
|
| 620 |
let update = host.host_ref_update("main", &[hash], None).unwrap(); |
| 621 |
let report = host.apply_hosted(None, &[], update).unwrap(); |
| 622 |
assert!(report.results.iter().all(|r| !r.ok)); |
| 7ba155f2 Andy McCall ↶ |
623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
#[test] |
| 629 |
fn a_changes_only_delegate_signs_changes_but_cannot_seal_or_move_a_reference() { |
| 630 |
let dir = tempfile::tempdir().unwrap(); |
| 631 |
let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 632 |
let agent_sk = SecretKey::generate(); |
| 633 |
let agent_id = agent_sk.identity("aider on a laptop", "agent@example", 1); |
| 634 |
let agent = agent_id.hash(); |
| 635 |
user.delegate_add(agent_id.clone(), Scope::CHANGES, None) |
| 636 |
.unwrap(); |
| 637 |
|
| 638 |
assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 639 |
let author = user.config.author_key.unwrap(); |
| 640 |
let ids = vec![agent_id.clone(), user.identity(&author).unwrap()]; |
| 641 |
let heads = host.bookmark_heads().unwrap(); |
| 642 |
let change = host |
| 643 |
.build_change( |
| 644 |
&heads, |
| 645 |
&[FileEdit { |
| 646 |
path: "a.txt".into(), |
| 647 |
content: Some(b"by the agent\n".to_vec()), |
| 648 |
}], |
| 649 |
"Agent edit", |
| 650 |
author, |
| 651 |
) |
| 652 |
.unwrap(); |
| 653 |
let hash = change.hash(); |
| 654 |
let sig = agent_sk.sign(Domain::Change, &agent, &hash); |
| 655 |
|
| 656 |
let update = host |
| 657 |
.host_ref_update("main", &[hash], Some((&agent, &agent_sk))) |
| 658 |
.unwrap(); |
| 659 |
let report = host |
| 660 |
.apply_hosted(Some((change.clone(), vec![sig.clone()])), &ids, update) |
| 661 |
.unwrap(); |
| 662 |
assert!(report.results.iter().all(|r| !r.ok), "{:?}", report.results); |
| 663 |
assert_eq!(host.bookmark_heads().unwrap(), heads); |
| 664 |
|
| 665 |
let (owner, owner_sk) = user.secret_key().unwrap().unwrap(); |
| 666 |
let update = host |
| 667 |
.host_ref_update("main", &[hash], Some((&owner, &owner_sk))) |
| 668 |
.unwrap(); |
| 669 |
let report = host |
| 670 |
.apply_hosted(Some((change, vec![sig])), &ids, update) |
| 671 |
.unwrap(); |
| 672 |
assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 673 |
assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| 674 |
assert!(matches!( |
| 675 |
host.authenticate_change(&hash).unwrap(), |
| 676 |
SignatureStatus::Valid { key, sealed: false, .. } if key == agent |
| 677 |
)); |
| 678 |
|
| 679 |
let change2 = host |
| 680 |
.build_change( |
| 681 |
&[hash], |
| 682 |
&[FileEdit { |
| 683 |
path: "a.txt".into(), |
| 684 |
content: Some(b"sealed by the agent\n".to_vec()), |
| 685 |
}], |
| 686 |
"Sealed", |
| 687 |
author, |
| 688 |
) |
| 689 |
.unwrap(); |
| 690 |
let h2 = host.store().put_change(&change2).unwrap(); |
| 691 |
let seal = host |
| 692 |
.store() |
| 693 |
.put_attest(&Attest::seal(host.id().unwrap(), vec![h2])) |
| 694 |
.unwrap(); |
| 695 |
host.store() |
| 696 |
.put_signature(&seal, &agent_sk.sign(Domain::Attest, &agent, &seal)) |
| 697 |
.unwrap(); |
| 698 |
host.store().index_seal(&h2, &seal).unwrap(); |
| 699 |
assert!(matches!( |
| 700 |
host.authenticate_change(&h2).unwrap(), |
| 701 |
SignatureStatus::Invalid(e) if e.contains("may not seal") |
| 702 |
)); |
| 703 |
} |
| 704 |
|
| ff1a64f8 Andy McCall ↶ |
705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
#[test] |
| 711 |
fn an_agent_signs_its_owner_s_changes_through_author_signer() { |
| 712 |
let dir = tempfile::tempdir().unwrap(); |
| 713 |
let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 714 |
let owner = user.config.author_key.unwrap(); |
| 715 |
|
| 716 |
let agent_home = dir.path().join("agent-home"); |
| 717 |
let mut agent_repo = Repo::init(&dir.path().join("agent-work")) |
| 718 |
.unwrap() |
| 719 |
.with_home(agent_home.clone()); |
| 720 |
let (agent, agent_id) = agent_repo |
| 721 |
.create_identity("aider on a laptop", "agent@example", IdentityScope::User) |
| 722 |
.unwrap(); |
| 723 |
user.delegate_add(agent_id.clone(), Scope::CHANGES, None) |
| 724 |
.unwrap(); |
| 725 |
assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 767ac6cd Andy McCall |
726 |
host.refresh().unwrap(); |
| ff1a64f8 Andy McCall ↶ |
727 |
let owner = { |
| 728 |
let _ = owner; |
| 729 |
user.config.author_key.unwrap() |
| 730 |
}; |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
let work = dir.path().join("work"); |
| 736 |
let mut as_agent = Repo::open(&work).unwrap().with_home(agent_home); |
| 737 |
as_agent |
| 738 |
.set_user_config("author.signer", &agent.to_hex()) |
| 739 |
.unwrap(); |
| 740 |
as_agent.set_user_config("core.sign", "change").unwrap(); |
| 741 |
assert_eq!(as_agent.config.author_key, Some(owner)); |
| 742 |
assert_eq!(as_agent.signing_key(), Some(agent)); |
| 743 |
write(&work, "a.txt", b"by the agent\n"); |
| 744 |
as_agent.snapshot("t").unwrap(); |
| 745 |
let h = as_agent.commit("Agent: edit a", &[]).unwrap(); |
| 746 |
let meta = as_agent.store().get_change_meta(&h).unwrap(); |
| 747 |
assert_eq!(meta.author, owner, "the owner stays the author"); |
| 748 |
assert!(matches!( |
| 749 |
as_agent.verify_change(&h).unwrap(), |
| 750 |
SignatureStatus::Valid { key, .. } if key == agent |
| 751 |
)); |
| 752 |
assert_eq!(as_agent.revset("delegated()").unwrap().hashes, vec![h]); |
| 753 |
|
| 754 |
|
| 755 |
let r = as_agent.push("origin", false).unwrap(); |
| 767ac6cd Andy McCall |
756 |
host.refresh().unwrap(); |
| ff1a64f8 Andy McCall ↶ |
757 |
assert!(r.iter().all(|x| !x.ok), "{r:?}"); |
| 758 |
assert!(!host.bookmark_heads().unwrap().contains(&h)); |
| 5cde7882 Andy McCall ↶ |
759 |
|
| 767ac6cd Andy McCall |
760 |
|
| 761 |
|
| 762 |
user.refresh().unwrap(); |
| 5cde7882 Andy McCall ↶ |
763 |
let r = user.push("origin", false).unwrap(); |
| 767ac6cd Andy McCall |
764 |
host.refresh().unwrap(); |
| ff1a64f8 Andy McCall ↶ |
765 |
assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 766 |
assert!(host.bookmark_heads().unwrap().contains(&h)); |
| 767 |
assert!(matches!( |
| 768 |
host.authenticate_change(&h).unwrap(), |
| 769 |
SignatureStatus::Valid { key, .. } if key == agent |
| 770 |
)); |
| 771 |
} |
| 772 |
|
| 68a92484 Andy McCall ↶ |
773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
#[test] |
| 778 |
fn a_delegate_s_first_push_of_a_bookmark_carries_its_identity() { |
| 779 |
let dir = tempfile::tempdir().unwrap(); |
| 780 |
let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 781 |
let agent_home = dir.path().join("agent-home"); |
| 782 |
let mut agent_repo = Repo::init(&dir.path().join("agent-work")) |
| 783 |
.unwrap() |
| 784 |
.with_home(agent_home.clone()); |
| 785 |
let (agent, agent_id) = agent_repo |
| 786 |
.create_identity("agent", "agent@example", IdentityScope::User) |
| 787 |
.unwrap(); |
| 788 |
user.delegate_add(agent_id, Scope::CHANGES | Scope::REFS, None) |
| 789 |
.unwrap(); |
| 790 |
assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 791 |
let work = dir.path().join("work"); |
| 792 |
let mut as_agent = Repo::open(&work).unwrap().with_home(agent_home); |
| 793 |
as_agent |
| 794 |
.set_user_config("author.signer", &agent.to_hex()) |
| 795 |
.unwrap(); |
| 796 |
assert!( |
| 797 |
host.store().get_identity(&agent).is_err(), |
| 798 |
"the host has never seen the agent" |
| 799 |
); |
| 800 |
let heads = as_agent.bookmark_heads().unwrap(); |
| 801 |
as_agent.bookmark_create("feature", Some(&heads)).unwrap(); |
| 802 |
let r = as_agent.push_bookmark("origin", "feature", false).unwrap(); |
| 803 |
assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 804 |
assert!( |
| 805 |
host.store().get_identity(&agent).is_ok(), |
| 806 |
"its identity came with the move" |
| 807 |
); |
| 808 |
} |
| 809 |
|
| d36ba2f7 Andy McCall ↶ |
810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
#[test] |
| 817 |
fn a_bookmark_attested_by_a_key_that_signed_no_change_is_fetched() { |
| 818 |
let dir = tempfile::tempdir().unwrap(); |
| 819 |
let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 820 |
let agent_home = dir.path().join("agent-home"); |
| 821 |
let mut agent_repo = Repo::init(&dir.path().join("agent-work")) |
| 822 |
.unwrap() |
| 823 |
.with_home(agent_home.clone()); |
| 824 |
let (agent, agent_id) = agent_repo |
| 825 |
.create_identity("agent", "agent@example", IdentityScope::User) |
| 826 |
.unwrap(); |
| 827 |
user.delegate_add(agent_id, Scope::CHANGES | Scope::REFS, None) |
| 828 |
.unwrap(); |
| 829 |
assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 830 |
let mut as_agent = Repo::open(&dir.path().join("work")) |
| 831 |
.unwrap() |
| 832 |
.with_home(agent_home); |
| 833 |
as_agent |
| 834 |
.set_user_config("author.signer", &agent.to_hex()) |
| 835 |
.unwrap(); |
| 836 |
let heads = as_agent.bookmark_heads().unwrap(); |
| 837 |
as_agent.bookmark_create("feature", Some(&heads)).unwrap(); |
| 838 |
let r = as_agent.push_bookmark("origin", "feature", false).unwrap(); |
| 839 |
assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 840 |
host.refresh().unwrap(); |
| 841 |
let (_, sa) = host.latest_ref_attest("feature").unwrap().unwrap(); |
| 842 |
assert_eq!(sa.sigs[0].key, agent, "the agent attested the move"); |
| 843 |
|
| 844 |
|
| 845 |
let remote = crate::protocol::LocalRemote::open(&dir.path().join("host")).unwrap(); |
| 846 |
let carries = |b: &Bundle| { |
| 847 |
b.objects |
| 848 |
.iter() |
| 849 |
.any(|(t, body)| *t == ObjectType::Identity && hash_object(*t, body) == agent) |
| 850 |
}; |
| 851 |
assert!(carries( |
| 852 |
&crate::protocol::Remote::fetch(&remote, &[], None).unwrap() |
| 853 |
)); |
| 854 |
assert!(carries(&host.bundle_for_filtered(&[], None).unwrap())); |
| 855 |
|
| 856 |
|
| 857 |
let theirs = Repo::clone( |
| 858 |
dir.path().join("host").to_str().unwrap(), |
| 859 |
&dir.path().join("theirs"), |
| 860 |
) |
| 861 |
.unwrap(); |
| 862 |
let tracked = theirs.view().unwrap().remotes["origin"].clone(); |
| 863 |
assert_eq!( |
| 864 |
tracked.get("feature"), |
| 865 |
host.view().unwrap().bookmarks.get("feature") |
| 866 |
); |
| 867 |
assert!(theirs.store().get_identity(&agent).is_ok()); |
| 868 |
let report = theirs.fetch("origin").unwrap(); |
| 869 |
assert!(report.skipped.is_empty(), "{:?}", report.skipped); |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
fn reattest(host: &Repo, bookmark: &str, key: KeyId, sk: &SecretKey) { |
| 876 |
let state = host.view().unwrap().bookmarks[bookmark]; |
| 877 |
let (_, last) = host.latest_ref_attest(bookmark).unwrap().unwrap(); |
| 878 |
let AttestBody::Ref { |
| 879 |
old_state, serial, .. |
| 880 |
} = last.attest.body |
| 881 |
else { |
| 882 |
panic!("not a ref attestation") |
| 883 |
}; |
| 884 |
let attest = Attest { |
| 885 |
repo: host.id().unwrap(), |
| 886 |
hlc: Hlc::now(), |
| 887 |
body: AttestBody::Ref { |
| 888 |
name: bookmark.into(), |
| 889 |
old_state, |
| 890 |
new_state: state, |
| 891 |
serial: serial + 1, |
| 892 |
}, |
| 893 |
}; |
| 894 |
let r = host.store().put_attest(&attest).unwrap(); |
| 895 |
host.store() |
| 896 |
.put_signature(&r, &sk.sign(Domain::Attest, &key, &attest.hash())) |
| 897 |
.unwrap(); |
| 898 |
host.set_ref_attest(bookmark, &r).unwrap(); |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
#[test] |
| 906 |
fn an_unverifiable_bookmark_is_skipped_but_never_the_current_one() { |
| 907 |
let dir = tempfile::tempdir().unwrap(); |
| 908 |
let (host, user, _, _, _) = setup(dir.path(), false, false); |
| 909 |
let author = user.config.author_key.unwrap(); |
| 910 |
let heads = user.bookmark_heads().unwrap(); |
| 911 |
for b in ["unknown", "forged", "fine"] { |
| 912 |
user.bookmark_create(b, Some(&heads)).unwrap(); |
| 913 |
let r = user.push_bookmark("origin", b, false).unwrap(); |
| 914 |
assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 915 |
} |
| 916 |
host.refresh().unwrap(); |
| 917 |
let stranger_sk = SecretKey::generate(); |
| 918 |
let stranger = stranger_sk.identity("s", "s@example", 1).hash(); |
| 919 |
reattest(&host, "unknown", stranger, &stranger_sk); |
| 920 |
reattest(&host, "forged", author, &stranger_sk); |
| 921 |
|
| 922 |
let url = dir.path().join("host").to_string_lossy().to_string(); |
| 923 |
let (theirs, skipped) = |
| 924 |
Repo::clone_reporting(&url, &dir.path().join("theirs"), None, &[], None).unwrap(); |
| 925 |
let names: Vec<&str> = skipped.iter().map(|(n, _)| n.as_str()).collect(); |
| 926 |
assert_eq!(names, ["forged", "unknown"]); |
| 927 |
assert!(skipped[1].1.contains("unknown key"), "{skipped:?}"); |
| 928 |
assert!(skipped[1].1.contains("origin/unknown"), "{skipped:?}"); |
| 929 |
let tracked = theirs.view().unwrap().remotes["origin"].clone(); |
| 930 |
assert!(tracked.contains_key("main") && tracked.contains_key("fine")); |
| 931 |
assert!(!tracked.contains_key("unknown") && !tracked.contains_key("forged")); |
| 932 |
assert_eq!(theirs.config.bookmark, "main"); |
| 933 |
assert_eq!(theirs.fetch("origin").unwrap().skipped.len(), 2); |
| 934 |
assert_eq!(theirs.pull("origin").unwrap().skipped.len(), 2); |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
reattest(&host, "main", stranger, &stranger_sk); |
| 939 |
for err in [ |
| 940 |
theirs.fetch("origin").unwrap_err(), |
| 941 |
theirs.pull("origin").unwrap_err(), |
| 942 |
Repo::clone(&url, &dir.path().join("again")) |
| 943 |
.err() |
| 944 |
.expect("the clone fails"), |
| 945 |
] { |
| 946 |
let err = err.to_string(); |
| 947 |
assert!( |
| 948 |
err.contains("attestation for origin/main: unknown key"), |
| 949 |
"{err}" |
| 950 |
); |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 7ba155f2 Andy McCall ↶ |
954 |
|
| 955 |
|
| 956 |
#[test] |
| 957 |
fn an_expired_delegation_is_refused_and_the_delegator_may_revoke() { |
| 958 |
let dir = tempfile::tempdir().unwrap(); |
| 959 |
let (_, mut user, _, _, _) = setup(dir.path(), false, false); |
| 960 |
let agent_sk = SecretKey::generate(); |
| 961 |
let agent_id = agent_sk.identity("agent", "agent@example", 1); |
| 962 |
let agent = agent_id.hash(); |
| 963 |
let yesterday = Hlc::now().wall_ms - 24 * 3600 * 1000; |
| 964 |
user.delegate_add(agent_id, Scope::CHANGES, Some(yesterday)) |
| 965 |
.unwrap(); |
| 966 |
user.config.set("core.sign", "seal").unwrap(); |
| 967 |
user.save_config().unwrap(); |
| 968 |
write(&dir.path().join("work"), "a.txt", b"later\n"); |
| 969 |
user.snapshot("t").unwrap(); |
| 970 |
let h = user.commit("after the delegation ended", &[]).unwrap(); |
| 971 |
user.store() |
| 972 |
.put_signature(&h, &agent_sk.sign(Domain::Change, &agent, &h)) |
| 973 |
.unwrap(); |
| 974 |
let status = user.verify_change(&h).unwrap(); |
| 975 |
assert!( |
| 976 |
matches!(&status, SignatureStatus::Invalid(e) if e.contains("expired")), |
| 977 |
"{status:?}" |
| 978 |
); |
| 979 |
|
| 980 |
let stranger = SecretKey::generate().identity("x", "x@example", 1).hash(); |
| 981 |
assert!(user.revoke_identity(&stranger, "").is_err()); |
| 982 |
assert!(user.revoke_identity(&agent, "session over").is_ok()); |
| 983 |
assert!(user.is_revoked(&agent).unwrap()); |
| 984 |
let r = user.delegate_remove(&agent).unwrap(); |
| 985 |
assert!(user.identity(&r.new).unwrap().delegates.is_empty()); |
| 84b9e9ac Andy McCall ↶ |
986 |
} |
| 987 |
|
| 988 |
#[test] |
| 989 |
fn a_host_edit_that_removes_markers_resolves_the_conflict() { |
| 990 |
let dir = tempfile::tempdir().unwrap(); |
| 991 |
let (host, mut user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true); |
| 992 |
let author = user.config.author_key.unwrap(); |
| 993 |
let base = host.bookmark_heads().unwrap(); |
| 994 |
|
| eae59556 Andy McCall ↶ |
995 |
let a = host |
| 996 |
.build_change( |
| 997 |
&base, |
| 998 |
&[FileEdit { |
| 999 |
path: "a.txt".into(), |
| 1000 |
content: Some(b"ONE\ntwo\n".to_vec()), |
| 1001 |
}], |
| 1002 |
"A", |
| 1003 |
author, |
| 1004 |
) |
| 1005 |
.unwrap(); |
| 1006 |
let b = host |
| 1007 |
.build_change( |
| 1008 |
&base, |
| 1009 |
&[FileEdit { |
| 1010 |
path: "a.txt".into(), |
| 1011 |
content: Some(b"uno\ntwo\n".to_vec()), |
| 1012 |
}], |
| 1013 |
"B", |
| 1014 |
author, |
| 1015 |
) |
| 1016 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
1017 |
let (ha, hb) = (a.hash(), b.hash()); |
| 1018 |
let ids = vec![hub_id.clone(), user.identity(&author).unwrap()]; |
| 1019 |
for (c, h) in [(a, ha), (b, hb)] { |
| 1020 |
let sig = hub_sk.sign(Domain::Change, &hub_key, &h); |
| 1021 |
let mut heads = host.bookmark_heads().unwrap(); |
| 1022 |
heads.retain(|x| !base.contains(x)); |
| 1023 |
heads.push(h); |
| eae59556 Andy McCall ↶ |
1024 |
let update = host |
| 1025 |
.host_ref_update("main", &heads, Some((&hub_key, &hub_sk))) |
| 1026 |
.unwrap(); |
| 1027 |
let r = host |
| 1028 |
.apply_hosted(Some((c, vec![sig])), &ids, update) |
| 1029 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
1030 |
assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results); |
| 1031 |
} |
| 1032 |
let heads = host.bookmark_heads().unwrap(); |
| 1033 |
assert_eq!(heads.len(), 2); |
| 1034 |
let marked = String::from_utf8(host.file_at("a.txt", Some(&heads)).unwrap()).unwrap(); |
| 1035 |
assert!(marked.contains("<<<<<<< oxy conflict"), "{marked}"); |
| 1036 |
|
| eae59556 Andy McCall ↶ |
1037 |
assert!(host |
| 1038 |
.build_change( |
| 1039 |
&heads, |
| 1040 |
&[FileEdit { |
| 1041 |
path: "a.txt".into(), |
| 1042 |
content: Some(format!("{marked}x\n").into_bytes()) |
| 1043 |
}], |
| 1044 |
"half", |
| 1045 |
author |
| 1046 |
) |
| 1047 |
.is_err()); |
| 1048 |
let res = host |
| 1049 |
.build_change( |
| 1050 |
&heads, |
| 1051 |
&[FileEdit { |
| 1052 |
path: "a.txt".into(), |
| 1053 |
content: Some(b"One\ntwo\n".to_vec()), |
| 1054 |
}], |
| 1055 |
"Resolve", |
| 1056 |
author, |
| 1057 |
) |
| 1058 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
1059 |
assert_eq!(res.kind, ChangeKind::Resolution); |
| eae59556 Andy McCall ↶ |
1060 |
assert!( |
| 1061 |
res.deps.contains(&ha) && res.deps.contains(&hb), |
| 1062 |
"a resolution depends on both sides" |
| 1063 |
); |
| 84b9e9ac Andy McCall ↶ |
1064 |
let hr = res.hash(); |
| 1065 |
let sig = hub_sk.sign(Domain::Change, &hub_key, &hr); |
| eae59556 Andy McCall ↶ |
1066 |
let update = host |
| 1067 |
.host_ref_update("main", &[hr], Some((&hub_key, &hub_sk))) |
| 1068 |
.unwrap(); |
| 1069 |
let r = host |
| 1070 |
.apply_hosted(Some((res, vec![sig])), &ids, update) |
| 1071 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
1072 |
assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results); |
| 1073 |
assert_eq!(host.file_at("a.txt", Some(&[hr])).unwrap(), b"One\ntwo\n"); |
| eae59556 Andy McCall ↶ |
1074 |
assert!(!host |
| 1075 |
.load_state(&[hr]) |
| 1076 |
.unwrap() |
| 1077 |
.materialize() |
| 1078 |
.is_conflicted()); |
| 84b9e9ac Andy McCall ↶ |
1079 |
let _ = &mut user; |
| 1080 |
} |
| 1081 |
} |