use super::*; use crate::identity::stub::Plc; use crate::testing::*; use axum::http::{HeaderMap, HeaderValue, Method, header}; use k256::ecdsa::SigningKey; use k256::ecdsa::signature::{Signer, Verifier}; use pds_repo::data::{cid_of, cid_string, encode, record_from_json}; use pds_repo::{car_file, car_with_roots}; use std::path::PathBuf; use std::sync::{Arc, OnceLock}; const CREATE: &str = "com.atproto.server.createAccount"; const IMPORT: &str = "com.atproto.repo.importRepo"; const MISSING: &str = "com.atproto.repo.listMissingBlobs"; const SUBMIT: &str = "com.atproto.identity.submitPlcOperation"; const REQUEST: &str = "com.atproto.identity.requestPlcOperationSignature"; const SIGN: &str = "com.atproto.identity.signPlcOperation"; const CREDENTIALS: &str = "com.atproto.identity.getRecommendedDidCredentials"; const SESSION: &str = "com.atproto.server.getSession"; const ACTIVATE: &str = "com.atproto.server.activateAccount"; const CAR: &str = "application/vnd.ipld.car"; /// A DID the directory does not know. const NOBODY: &str = "did:plc:aaaaaaaaaaaaaaaaaaaaaaaa"; /// A call as it comes over XRPC: its whole `Authorization` header, its /// `Content-Type`, parameters and body. Answers the JSON, `null` for none, /// or the status and error name. async fn send( s: &Shared, nsid: &str, auth: Option<&str>, content_type: Option<&str>, params: &[(String, String)], body: &[u8], ) -> Result { let mut headers = HeaderMap::new(); if let Some(a) = auth { headers.insert(header::AUTHORIZATION, HeaderValue::from_str(a).unwrap()); } if let Some(t) = content_type { headers.insert(header::CONTENT_TYPE, HeaderValue::from_str(t).unwrap()); } let authorization = crate::authorization(&headers); let method = if pds_lexicon::method(nsid).is_some_and(|m| m.kind == pds_lexicon::Kind::Query) { Method::GET } else { Method::POST }; let call = Call { nsid, method: &method, params, body, bearer: Bearer::of(authorization.as_deref()), headers: &headers, client: "127.0.0.1".into(), charged: OnceLock::new(), http: None, }; match crate::answer(s, &call).await { Ok(Resp::Json(j)) => Ok(j), Ok(_) => Ok(Json::Null), Err(e) => Err((e.status, e.name.to_string())), } } /// A JSON call with a bearer token. async fn post( s: &Shared, nsid: &str, token: Option<&str>, body: Json, ) -> Result { let auth = token.map(|t| format!("Bearer {t}")); send( s, nsid, auth.as_deref(), Some("application/json"), &[], body.to_string().as_bytes(), ) .await } /// An import of `car` with the account's token. async fn import(s: &Shared, token: &str, car: &[u8]) -> Result { send( s, IMPORT, Some(&format!("Bearer {token}")), Some(CAR), &[], car, ) .await } /// A compact JWT of this header and payload, signed with `key` (ES256K). fn jwt(key: &SigningKey, header: Json, payload: Json) -> String { let message = format!( "{}.{}", B64.encode(header.to_string()), B64.encode(payload.to_string()) ); let sig: k256::ecdsa::Signature = key.sign(message.as_bytes()); format!("{message}.{}", B64.encode(sig.normalize_s().to_bytes())) } fn now() -> i64 { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_secs() as i64 } /// The servers of a move: `old`, where `alice` was made, and `new`, at /// `did:web:new.test`, with a rotation key of its own, both on one /// directory, each mailing to a sink. struct Move { plc: Arc, dirs: Vec, old: Shared, new: Shared, old_sink: PathBuf, did: String, /// Alice's session on the old server. old_token: String, } impl Move { async fn start(name: &str, change: impl FnOnce(&mut crate::Config)) -> Move { let plc = Plc::start().await; let (od, nd) = ( scratch(&format!("{name}-old")), scratch(&format!("{name}-new")), ); let old_sink = scratch(&format!("{name}-old-mail")); std::fs::create_dir_all(&old_sink).unwrap(); let old = open_with(&od, &plc.url, |c| { c.mailer = crate::email::Mailer::sink(&old_sink) }); let new = open_with(&nd, &plc.url, |c| { c.hostname = "new.test".into(); c.public_url = "http://new.test".into(); c.rotation_key = SigningKey::from_slice(&[7u8; 32]).unwrap(); change(c); }); let (did, old_token) = create(&old, "alice").await; Move { plc, dirs: vec![od, nd, old_sink.clone()], old, new, old_sink, did, old_token, } } /// Alice's signing key on the old server. fn key(&self) -> SigningKey { store_of(&self.old, &self.did, |st| st.repo.signing_key().clone()) } /// A good service token of Alice's for `createAccount` on the new server. fn token(&self) -> String { crate::proxy::service_token(&self.old, &self.did, "did:web:new.test", Some(CREATE), None) .ok() .unwrap() } fn body(&self) -> Json { json!({ "handle": "mover.test", "email": "mover@mail.com", "password": "mover-pw", "did": self.did }) } /// Alice made on the new server: her token there. async fn moved(&self) -> String { let made = post(&self.new, CREATE, Some(&self.token()), self.body()) .await .unwrap(); made["accessJwt"].as_str().unwrap().to_string() } } impl Drop for Move { fn drop(&mut self) { for d in &self.dirs { let _ = std::fs::remove_dir_all(d); } } } /// `createAccount`'s service token is checked before its input, each fault /// with its own name; then `did` against the token's `iss`, `plcOp`, and an /// account made already. #[tokio::test] async fn service_tokens_are_checked_in_order() { let m = Move::start("migration-tokens", |_| {}).await; let key = m.key(); let exp = now() + 60; let claims = |extra: Json| { let mut c = json!({ "iss": m.did, "aud": "did:web:new.test", "exp": exp, "lxm": CREATE }); for (k, v) in extra.as_object().unwrap() { if v.is_null() { c.as_object_mut().unwrap().remove(k); } else { c[k] = v.clone(); } } c }; let es256k = json!({ "alg": "ES256K", "typ": "JWT" }); let signed = |extra: Json| format!("Bearer {}", jwt(&key, es256k.clone(), claims(extra))); let crafted = |header: Json, extra: Json| { format!( "Bearer {}.{}.c2ln", B64.encode(header.to_string()), B64.encode(claims(extra).to_string()) ) }; let other = SigningKey::from_slice(&[3u8; 32]).unwrap(); let body = m.body().to_string(); let cases: Vec<(String, (u16, &str))> = vec![ ("Bearer abc".into(), (401, "BadJwt")), ("Bearer a.b.c.d".into(), (401, "BadJwt")), ("Bearer !!.e30.c2ln".into(), (500, "InternalServerError")), (format!("Bearer {}", m.old_token), (401, "BadJwtType")), (crafted(json!({ "typ": "JWT" }), json!({})), (401, "BadJwt")), ( crafted(json!({ "alg": "ES256K", "typ": "dpop+jwt" }), json!({})), (401, "BadJwtType"), ), (signed(json!({ "aud": null })), (401, "BadJwt")), (signed(json!({ "exp": exp.to_string() })), (401, "BadJwt")), (signed(json!({ "lxm": 5 })), (401, "BadJwt")), (signed(json!({ "exp": now() - 10 })), (401, "JwtExpired")), ( signed(json!({ "lxm": "com.atproto.repo.createRecord" })), (401, "BadJwtLexiconMethod"), ), (signed(json!({ "lxm": null })), (401, "BadJwtLexiconMethod")), (signed(json!({ "iss": "nope" })), (401, "BadJwtIss")), ( signed(json!({ "iss": format!("{}#", m.did) })), (401, "BadJwtIss"), ), ( signed(json!({ "iss": NOBODY })), (401, "AuthenticationRequired"), ), ( format!("Bearer {}", jwt(&other, es256k.clone(), claims(json!({})))), (401, "BadJwtSignature"), ), ( signed(json!({ "aud": "did:web:other.test" })), (401, "BadJwtAudience"), ), ( signed(json!({ "aud": "did:web:new.test#atproto_pds" })), (401, "BadJwtAudience"), ), ( format!("DPoP {}", m.token()), (401, "AuthenticationRequired"), ), ("Basic YTpi".into(), (401, "AuthenticationRequired")), ("Foo bar".into(), (401, "AuthenticationRequired")), ("Bearer".into(), (400, "InvalidToken")), ("Bearer a b".into(), (400, "InvalidToken")), ]; for (auth, (status, name)) in cases { // Before the input: an input the lexicon refuses is not looked at. for body in [body.as_bytes(), b"{".as_slice()] { assert_eq!( send( &m.new, CREATE, Some(&auth), Some("application/json"), &[], body ) .await, Err((status, name.to_string())), "{auth}" ); } } // The token's `did` and the input. let good = m.token(); let refused = |b: Json, t: Option<&str>| { let s = m.new.clone(); let t = t.map(str::to_string); async move { post(&s, CREATE, t.as_deref(), b).await.unwrap_err() } }; let mut b = m.body(); b["plcOp"] = json!({}); assert_eq!( refused(b, Some(&good)).await, (400, "InvalidRequest".into()) ); let mut b = m.body(); b["did"] = json!(NOBODY); assert_eq!( refused(b, Some(&good)).await, (401, "AuthenticationRequired".into()) ); assert_eq!( refused(m.body(), None).await, (401, "AuthenticationRequired".into()) ); // An external handle that resolves to nothing. let mut b = m.body(); b["handle"] = json!("gone.elsewhere.org"); assert_eq!( refused(b, Some(&good)).await, (400, "InvalidRequest".into()) ); // Made, deactivated, with nothing on the firehose; and then no more. let made = post(&m.new, CREATE, Some(&good), m.body()).await.unwrap(); assert_eq!(made["did"], json!(m.did)); assert_eq!(made["didDoc"]["id"], json!(m.did)); assert_eq!( made["didDoc"]["alsoKnownAs"], json!(["at://alice.test"]), "the document names the old server's handle" ); assert!(m.new.events.seqs_of(&m.did).is_empty()); let token = made["accessJwt"].as_str().unwrap(); let session = call(&m.new, SESSION, Some(token), Json::Null) .await .unwrap(); assert_eq!( (session["active"].clone(), session["status"].clone()), (json!(false), json!("deactivated")) ); let mut again = m.body(); again["handle"] = json!("mover2.test"); again["email"] = json!("mover2@mail.com"); assert_eq!( refused(again, Some(&good)).await, (400, "AlreadyExists".into()) ); // A token given without `did` is checked, then not used. let plain = post( &m.new, CREATE, Some(&good), json!({ "handle": "plain.test", "email": "plain@mail.com", "password": "p" }), ) .await .unwrap(); assert_ne!(plain["did"], json!(m.did)); // A restart keeps the account moving in deactivated, with no events. let dir = m.dirs[1].clone(); let plc_url = m.plc.url.clone(); drop(m.new.clone()); let reopened = open_with(&dir, &plc_url, |c| c.hostname = "new.test".into()); let a = reopened.account(&m.did).unwrap(); assert_eq!(crate::lifecycle::status(&a), Some("deactivated")); } /// A repository's blocks, to make CAR files of, good and bad. struct Built { head: (Vec, Vec), root: Vec, nodes: Vec<(Vec, Vec)>, records: Vec<(Vec, Vec)>, } impl Built { /// A repository of `did` holding these records, by path, its commit /// signed with a key of its old server's. fn of(did: &str, records: &[(&str, Json)]) -> Built { let values: Vec<(&str, V)> = records .iter() .map(|(path, value)| (*path, record_from_json(value).unwrap())) .collect(); Built::of_values(did, &values) } /// A repository of `did` holding these records, each the block its /// value encodes to, whatever JSON would make of it. fn of_values(did: &str, records: &[(&str, V)]) -> Built { let mut entries = Vec::new(); let mut blocks = Vec::new(); for (path, value) in records { let r = make_record(value.clone()); entries.push((path.as_bytes().to_vec(), r.cid.clone())); blocks.push((r.cid, r.bytes)); } entries.sort(); let tree = pds_repo::mst::build_tree(&entries); let nodes: Vec<(Vec, Vec)> = tree .nodes() .into_iter() .map(|n| (n.cid.clone(), n.bytes.clone())) .collect(); Built::with_tree(did, tree.cid.clone(), nodes, blocks) } fn with_tree( did: &str, root: Vec, nodes: Vec<(Vec, Vec)>, records: Vec<(Vec, Vec)>, ) -> Built { let key = SigningKey::from_slice(&[5u8; 32]).unwrap(); let mut fields = vec![ ("did".to_string(), V::Str(did.into())), ("version".to_string(), V::Int(3)), ("data".to_string(), V::Link(root.clone())), ("rev".to_string(), V::Str("2222222222222".into())), ("prev".to_string(), V::Null), ]; let sig: k256::ecdsa::Signature = key.sign(&encode(&V::Map(fields.clone()))); fields.push(("sig".into(), V::Bytes(sig.to_bytes().to_vec()))); let bytes = encode(&V::Map(fields)); Built { head: (cid_of(0x71, &bytes), bytes), root, nodes, records, } } /// Its CAR file, rooted at `roots`, of its blocks but those `leave` /// says, then `extra`. fn car( &self, roots: &[&[u8]], leave: impl Fn(&[u8]) -> bool, extra: &[(Vec, Vec)], ) -> Vec { let all: Vec<&(Vec, Vec)> = std::iter::once(&self.head) .chain(&self.nodes) .chain(&self.records) .filter(|(c, _)| !leave(c)) .chain(extra) .collect(); car_with_roots(roots, all.iter().map(|(c, b)| (c.as_slice(), b.as_slice()))) } fn good(&self) -> Vec { car_file( &self.head.0, std::iter::once(&self.head) .chain(&self.nodes) .chain(&self.records) .map(|(c, b)| (c.as_slice(), b.as_slice())), ) } } /// A record that references a blob. fn with_blob(n: i64, cid: &str) -> Json { json!({ "$type": "a.b.c", "n": n, "blob": { "$type": "blob", "ref": { "$link": cid }, "mimeType": "text/plain", "size": 4 } }) } /// The head commit's fields, and whether `key` signed it. fn head_of(s: &Service, did: &str, key: &k256::ecdsa::VerifyingKey) -> (Map, bool) { let bytes = repo_of(s, did, |r| r.head_bytes().to_vec()); let V::Map(mut fields) = decode(&bytes).unwrap() else { panic!("a commit that is no map") }; let i = fields.iter().position(|(k, _)| k == "sig").unwrap(); let V::Bytes(sig) = fields.remove(i).1 else { panic!("a sig that is no bytes") }; let sig = k256::ecdsa::Signature::from_slice(&sig).unwrap(); let signed = key.verify(&encode(&V::Map(fields.clone())), &sig).is_ok(); let Json::Object(m) = pds_repo::data::to_json(&V::Map(fields)) else { unreachable!() }; (m, signed) } /// `importRepo` refuses, in order, a token not of the account's password /// session, another `Content-Type`, a body too long, and each fault of the /// CAR file, changing nothing; a good one becomes the repository whole, in /// a new commit signed with the account's key, its blobs missing until /// they are uploaded, through a restart. #[tokio::test] async fn imports_are_checked_and_whole() { let m = Move::start("migration-import", |c| c.max_import = 4096).await; let token = m.moved().await; let blob = b"blob".to_vec(); let blob_cid = cid_string(&cid_of(0x55, &blob)); let built = Built::of( &m.did, &[ ("a.b.c/one", record(1)), ("a.b.c/two", with_blob(2, &blob_cid)), ("a.b.c/three", with_blob(3, &blob_cid)), ], ); let good = built.good(); let before = repo_of(&m.new, &m.did, |r| (r.head_string(), r.rev.clone())); let unchanged = |what: &str| { assert_eq!( repo_of(&m.new, &m.did, |r| (r.head_string(), r.rev.clone())), before, "{what} changed the repository" ); }; // The token, the type, the size. assert_eq!( send(&m.new, IMPORT, None, Some(CAR), &[], &good).await, Err((401, "AuthMissing".into())) ); let auth = format!("Bearer {token}"); for t in [None, Some("application/json")] { assert_eq!( send(&m.new, IMPORT, Some(&auth), t, &[], &good).await, Err((400, "InvalidRequest".into())) ); } assert_eq!( import(&m.new, &token, &[0u8; 4097]).await, Err((413, "PayloadTooLarge".into())) ); // The CAR file. let unreached = (cid_of(0x71, b"\xa0"), b"\xa0".to_vec()); let other = Built::of(NOBODY, &[("a.b.c/one", record(1))]); let record_cid = built.records[0].0.clone(); let faults: Vec<(&str, Vec, (u16, &str))> = vec![ ("no CAR", vec![1, 2, 3], (500, "InternalServerError")), ("an empty body", vec![], (500, "InternalServerError")), ( "two roots", built.car(&[&built.head.0, &built.root], |_| false, &[]), (400, "InvalidRequest"), ), ( "no root", built.car(&[], |_| false, &[]), (500, "InternalServerError"), ), ( "a root not in it", built.car(&[&built.head.0], |c| c == built.head.0, &[]), (500, "InternalServerError"), ), ( "a root that is no commit", built.car(&[&built.root], |_| false, &[]), (500, "InternalServerError"), ), ( "a block the commit does not reach", built.car( &[&built.head.0], |_| false, std::slice::from_ref(&unreached), ), (500, "InternalServerError"), ), ( "another DID's commit", other.good(), (400, "InvalidRequest"), ), ( "a record missing", built.car(&[&built.head.0], |c| c == record_cid, &[]), (400, "InvalidRequest"), ), ( "the tree's root missing", built.car(&[&built.head.0], |c| c == built.root, &[]), (400, "InvalidRequest"), ), ]; for (what, car, (status, name)) in faults { assert_eq!( import(&m.new, &token, &car).await, Err((status, name.to_string())), "{what}" ); unchanged(what); } // A record that does not decode, and a tree that is not the one its records make. let bad_record = (cid_of(0x71, b"\xff"), b"\xff".to_vec()); let entries = vec![(b"a.b.c/x".to_vec(), bad_record.0.clone())]; let tree = pds_repo::mst::build_tree(&entries); let undecodable = Built::with_tree( &m.did, tree.cid.clone(), vec![(tree.cid.clone(), tree.bytes.clone())], vec![bad_record], ); assert_eq!( import(&m.new, &token, &undecodable.good()).await, Err((400, "InvalidRequest".into())) ); // Keys of more than one level, all in one node. let deep: Vec = (0..200) .map(|i| format!("a.b.c/k{i}")) .filter(|k| pds_repo::mst::depth(k.as_bytes()) > 0) .take(1) .collect(); let mut flat_records: Vec<(String, Json)> = vec![("a.b.c/a".into(), record(1))]; flat_records.push((deep[0].clone(), record(2))); let mut entries = Vec::new(); let mut blocks = Vec::new(); for (p, v) in &flat_records { let r = make_record(record_from_json(v).unwrap()); entries.push((p.as_bytes().to_vec(), r.cid.clone())); blocks.push((r.cid, r.bytes)); } entries.sort(); let mut list = Vec::new(); let mut prev: &[u8] = &[]; for (k, v) in &entries { let p = prev.iter().zip(k).take_while(|(a, b)| a == b).count(); list.push(V::Map(vec![ ("k".into(), V::Bytes(k[p..].to_vec())), ("p".into(), V::Int(p as i64)), ("t".into(), V::Null), ("v".into(), V::Link(v.clone())), ])); prev = k; } let node = encode(&V::Map(vec![ ("e".into(), V::Arr(list)), ("l".into(), V::Null), ])); let node_cid = cid_of(0x71, &node); let flat = Built::with_tree(&m.did, node_cid.clone(), vec![(node_cid, node)], blocks); assert_eq!( import(&m.new, &token, &flat.good()).await, Err((400, "InvalidRequest".into())) ); unchanged("a refused import"); // Good: the tree is the repository's, in a commit of the account's. let new_key = repo_of(&m.new, &m.did, |r| *r.verifying_key()); assert_eq!(import(&m.new, &token, &good).await, Ok(Json::Null)); let (head, signed) = head_of(&m.new, &m.did, &new_key); assert!(signed, "the head is signed with the account's key"); assert_eq!(head["data"], json!({ "$link": cid_string(&built.root) })); assert_eq!(head["prev"], Json::Null); assert!(head["rev"].as_str().unwrap() > before.1.as_str()); let paths: Vec = records_of(&m.new, &m.did) .into_iter() .map(|(p, _)| p) .collect(); assert_eq!(paths, ["a.b.c/one", "a.b.c/three", "a.b.c/two"]); assert!( m.new.events.seqs_of(&m.did).is_empty(), "deactivated: no events" ); // The blob is missing, listed with a record that references it. let missing = |params: Vec<(String, String)>| { let s = m.new.clone(); let auth = auth.clone(); async move { send(&s, MISSING, Some(&auth), None, ¶ms, b"").await } }; let listed = missing(vec![]).await.unwrap(); assert_eq!( listed, json!({ "blobs": [{ "cid": blob_cid, "recordUri": format!("at://{}/a.b.c/three", m.did) }], "cursor": blob_cid }) ); assert_eq!( missing(vec![("cursor".into(), "zzz".into())]) .await .unwrap(), json!({ "blobs": [] }) ); assert_eq!( missing(vec![("limit".into(), "0".into())]).await, Err((400, "InvalidRequest".into())) ); assert_eq!( send(&m.new, MISSING, None, None, &[], b"").await, Err((401, "AuthMissing".into())) ); // Again: the same tree, a new commit. assert_eq!(import(&m.new, &token, &good).await, Ok(Json::Null)); let (again, _) = head_of(&m.new, &m.did, &new_key); assert!(again["rev"].as_str() > head["rev"].as_str()); assert_eq!(again["data"], head["data"]); // Uploaded, it is missing no more. crate::blobs::test_upload( &m.new, Some(&token), Some("text/plain"), blob.clone(), false, ) .await .unwrap(); assert_eq!(missing(vec![]).await.unwrap(), json!({ "blobs": [] })); // Another tree: what it has not is deleted, what differs replaced. let next = Built::of( &m.did, &[("a.b.c/two", record(22)), ("a.b.c/four", record(4))], ); assert_eq!(import(&m.new, &token, &next.good()).await, Ok(Json::Null)); let now_records: Vec<(String, Json)> = records_of(&m.new, &m.did) .into_iter() .map(|(p, r)| (p, pds_repo::data::to_json(&r.value))) .collect(); assert_eq!( now_records, [ ("a.b.c/four".to_string(), record(4)), ("a.b.c/two".to_string(), record(22)) ] ); let checked = repo_of(&m.new, &m.did, |r| r.root.clone()); assert_eq!(checked, next.root); // Through a restart. let head_now = repo_of(&m.new, &m.did, |r| r.head_string()); let dir = m.dirs[1].clone(); let reopened = open_with(&dir, &m.plc.url, |c| c.hostname = "new.test".into()); assert_eq!(repo_of(&reopened, &m.did, |r| r.head_string()), head_now); assert_eq!(records_of(&reopened, &m.did).len(), 2); } /// A CAR file's records are kept as its blocks, byte for byte, where JSON /// would write one as another value: a map of the one key `$link`, a CID /// string, as some servers wrote a blob ref's `ref`, which JSON writes as a /// link; a map of `$bytes`; and a `$link` that is no CID. Each is held at /// the CAR's CID, as its bytes, once the log is compacted into a table and /// read from it, in `getRepo`, and through a restart; and the blob such a /// `ref` names is missing until it is uploaded, as a link's is. #[tokio::test] async fn imports_keep_each_record_as_its_block() { let m = Move::start("migration-blocks", |c| c.log_floor = 2048).await; let token = m.moved().await; let blob_cid = cid_string(&cid_of(0x55, b"blob")); let text = |t: &str| V::Str(t.into()); let map = |kv: &[(&str, V)]| V::Map(kv.iter().map(|(k, v)| (k.to_string(), v.clone())).collect()); let rec = |field: &str, v: V| { map(&[ ("$type", text("a.b.c")), ("pad", text(&"x".repeat(800))), (field, v), ]) }; let icon = map(&[ ("$type", text("blob")), ("ref", map(&[("$link", text(&blob_cid))])), ("mimeType", text("text/plain")), ("size", V::Int(4)), ]); let values = [ ("a.b.c/bytes", rec("b", map(&[("$bytes", text("AQID"))]))), ("a.b.c/icon", rec("icon", icon)), ("a.b.c/odd", rec("l", map(&[("$link", text("not a cid"))]))), ("a.b.c/plain", rec("n", V::Int(1))), ]; let built = Built::of_values(&m.did, &values); let want: Vec<(String, Vec, Vec)> = values .iter() .map(|(p, v)| { let r = make_record(v.clone()); (p.to_string(), r.cid, r.bytes) }) .collect(); let held = |s: &Service| -> Vec<(String, Vec, Vec)> { records_of(s, &m.did) .into_iter() .map(|(p, r)| (p, r.cid, r.bytes)) .collect() }; let auth = format!("Bearer {token}"); let missing = |s: &Shared| { let (s, auth) = (s.clone(), auth.clone()); async move { send(&s, MISSING, Some(&auth), None, &[], b"") .await .unwrap() } }; let listed = json!({ "blobs": [{ "cid": blob_cid, "recordUri": format!("at://{}/a.b.c/icon", m.did) }], "cursor": blob_cid }); assert_eq!(import(&m.new, &token, &built.good()).await, Ok(Json::Null)); assert_eq!(held(&m.new), want); assert_eq!(missing(&m.new).await, listed); // Compacted, its records read from the table. let wal = store_of(&m.new, &m.did, |st| st.wal.clone()); wal.wait_compacted(); assert!(wal.checkpoint_generation() > 0, "the log was not compacted"); store_of(&m.new, &m.did, |st| { st.ctx.blocks.forget(st.disk.table.as_ref().unwrap().id()) }); assert_eq!(held(&m.new), want); let car = store_of(&m.new, &m.did, |st| { crate::sync::CarStream::of(st, None).unwrap() }); let (_, blocks) = pds_repo::read_car(&car.bytes().unwrap()).unwrap(); assert!( blocks.iter().all(|(c, b)| cid_of(c[1], b) == *c), "a block of getRepo is not its CID's" ); for (_, cid, bytes) in &want { assert!( blocks.contains(&(cid.clone(), bytes.clone())), "getRepo leaves a record out" ); } // Through a restart. let reopened = open_with(&m.dirs[1], &m.plc.url, |c| c.hostname = "new.test".into()); assert_eq!(repo_of(&reopened, &m.did, |r| r.root.clone()), built.root); assert_eq!(held(&reopened), want); assert_eq!(missing(&reopened).await, listed); } /// The old server signs the operation that moves the DID, with its mailed /// token, once; the new one submits it, refusing one that would not leave /// the DID here, and puts `#identity` with no handle on its firehose; the /// account is then activated there. #[tokio::test] async fn moving_signs_and_submits() { let m = Move::start("migration-plc", |_| {}).await; let token = m.moved().await; let creds = call(&m.new, CREDENTIALS, Some(&token), Json::Null) .await .unwrap(); // Signing needs the mailed token. let mut input = creds.clone(); assert_eq!( post(&m.old, SIGN, Some(&m.old_token), input.clone()).await, Err((400, "InvalidRequest".into())) ); input["token"] = json!("AAAAA-AAAAA"); assert_eq!( post(&m.old, SIGN, Some(&m.old_token), input.clone()).await, Err((400, "InvalidToken".into())) ); assert_eq!( send( &m.old, REQUEST, Some(&format!("Bearer {}", m.old_token)), None, &[], b"" ) .await, Ok(Json::Null) ); let mailed = crate::email::testing::sent(&m.old_sink).pop().unwrap(); assert_eq!(mailed["kind"], "plcOperation"); input["token"] = json!(mailed["token"].as_str().unwrap().to_lowercase()); let before = m.plc.log(&m.did); let op = post(&m.old, SIGN, Some(&m.old_token), input.clone()) .await .unwrap()["operation"] .clone(); assert_eq!(m.plc.log(&m.did), before, "signing sends nothing"); let signer = crate::plc::verify_update(before.last().unwrap(), &op).unwrap(); assert_eq!( signer, crate::plc::did_key(m.old.config.rotation_key.verifying_key()) ); for k in crate::plc::FIELDS { assert_eq!(op[k], creds[k], "{k}"); } assert_eq!( post(&m.old, SIGN, Some(&m.old_token), input).await, Err((400, "InvalidToken".into())), "the token is used up" ); // Submitted to the new server: refused where it would not leave the DID here. let refusals = [ json!({ "type": "plc_operation" }), json!({ "type": "plc_tombstone", "prev": op["prev"], "sig": op["sig"] }), { let mut o = op.clone(); o["services"]["atproto_pds"]["endpoint"] = json!("https://elsewhere.test"); o }, { let mut o = op.clone(); o["alsoKnownAs"] = json!(["at://nobody.test"]); o }, { let mut o = op.clone(); let sig = o["sig"].as_str().unwrap(); o["sig"] = json!(format!("{}AAAA", &sig[..sig.len() - 4])); o }, ]; for bad in refusals { assert_eq!( post(&m.new, SUBMIT, Some(&token), json!({ "operation": bad })).await, Err((400, "InvalidRequest".into())), "{bad}" ); assert_eq!(m.plc.log(&m.did), before); } assert_eq!( post(&m.new, SUBMIT, Some(&token), json!({ "operation": null })).await, Err((400, "InvalidRequest".into())) ); assert_eq!( post(&m.new, SUBMIT, None, json!({ "operation": op })).await, Err((401, "AuthMissing".into())) ); assert_eq!( post(&m.new, SUBMIT, Some(&token), json!({ "operation": op })).await, Ok(Json::Null) ); assert_eq!(m.plc.log(&m.did).last(), Some(&op)); let seqs = m.new.events.seqs_of(&m.did); assert_eq!(seqs.len(), 1); let frame = m .new .events .get(&m.new, seqs[0] - 1, seqs[0], 0) .pop() .unwrap(); let (header, n) = pds_repo::data::decode_prefix(&frame).unwrap(); assert_eq!(pds_repo::data::to_json(&header)["t"], json!("#identity")); let body = pds_repo::data::to_json(&decode(&frame[n..]).unwrap()); assert!(body.get("handle").is_none(), "{body}"); // The document it made is served at once; the account may be activated. let doc = crate::identity::did_doc(&m.new, &m.did).await.ok().unwrap(); assert_eq!( doc["service"][0]["serviceEndpoint"], json!("http://new.test") ); assert_eq!( send( &m.new, ACTIVATE, Some(&format!("Bearer {token}")), None, &[], b"" ) .await, Ok(Json::Null) ); // An active account's import is `#sync` of its new head. let built = Built::of(&m.did, &[("a.b.c/one", record(1))]); assert_eq!(import(&m.new, &token, &built.good()).await, Ok(Json::Null)); let last = *m.new.events.seqs_of(&m.did).last().unwrap(); let frame = m.new.events.get(&m.new, last - 1, last, 0).pop().unwrap(); let (header, _) = pds_repo::data::decode_prefix(&frame).unwrap(); assert_eq!(pds_repo::data::to_json(&header)["t"], json!("#sync")); } /// An app password's session may list missing blobs and submit an /// operation, but not import, nor ask for or sign one; a taken-down /// account's `com.atproto.takendown` session may ask for and sign one, and /// nothing else of a move (`spec/LIMITS.md`, `spec/LIFECYCLE.md`). #[tokio::test] async fn tokens_of_a_move() { let m = Move::start("migration-app", |_| {}).await; let app = call( &m.old, "com.atproto.server.createAppPassword", Some(&m.old_token), json!({ "name": "app" }), ) .await .unwrap(); let app = call( &m.old, "com.atproto.server.createSession", None, json!({ "identifier": "alice.test", "password": app["password"] }), ) .await .unwrap()["accessJwt"] .as_str() .unwrap() .to_string(); let invalid_token = Err((400, "InvalidToken".to_string())); let car = Built::of(&m.did, &[("a.b.c/one", record(1))]).good(); assert_eq!(import(&m.old, &app, &car).await, invalid_token); assert_eq!( send( &m.old, REQUEST, Some(&format!("Bearer {app}")), None, &[], b"" ) .await, invalid_token ); assert_eq!( post(&m.old, SIGN, Some(&app), json!({ "token": "AAAAA-AAAAA" })).await, invalid_token ); assert_eq!( send( &m.old, MISSING, Some(&format!("Bearer {app}")), None, &[], b"" ) .await, Ok(json!({ "blobs": [] })) ); assert_eq!( post(&m.old, SUBMIT, Some(&app), json!({ "operation": {} })).await, Err((400, "InvalidRequest".into())) ); // Taken down. let basic = format!( "Basic {}", base64::engine::general_purpose::STANDARD.encode(format!("admin:{ADMIN_PASSWORD}")) ); let takedown = json!({ "subject": { "$type": "com.atproto.admin.defs#repoRef", "did": m.did }, "takedown": { "applied": true, "ref": "t" } }); send( &m.old, "com.atproto.admin.updateSubjectStatus", Some(&basic), Some("application/json"), &[], takedown.to_string().as_bytes(), ) .await .unwrap(); let down = call( &m.old, "com.atproto.server.createSession", None, json!({ "identifier": "alice.test", "password": "alice-pw", "allowTakendown": true }), ) .await .unwrap()["accessJwt"] .as_str() .unwrap() .to_string(); assert_eq!(import(&m.old, &down, &car).await, invalid_token); assert_eq!( send( &m.old, MISSING, Some(&format!("Bearer {down}")), None, &[], b"" ) .await, invalid_token ); assert_eq!( post(&m.old, SUBMIT, Some(&down), json!({ "operation": {} })).await, invalid_token ); assert_eq!( import(&m.old, &m.old_token, &car).await, Err((401, "AccountTakedown".into())) ); assert_eq!( send( &m.old, REQUEST, Some(&format!("Bearer {down}")), None, &[], b"" ) .await, Ok(Json::Null) ); let token = crate::email::testing::last_token(&m.old_sink); let creds = json!({ "token": token, "alsoKnownAs": ["at://elsewhere.test"] }); let op = post(&m.old, SIGN, Some(&down), creds).await.unwrap()["operation"].clone(); assert_eq!(op["alsoKnownAs"], json!(["at://elsewhere.test"])); } /// Below `PDS_MIN_FREE_BYTES`, an import is `507 InsufficientStorage` once /// its token, `Content-Type` and size are checked, and changes nothing. #[tokio::test] async fn imports_need_free_space() { let m = Move::start("migration-disk", |c| c.min_free = 1 << 20).await; let token = m.moved().await; let car = Built::of(&m.did, &[("a.b.c/one", record(1))]).good(); let before = repo_of(&m.new, &m.did, |r| r.head_string()); m.new.limits.disk.set_free(Some(1000)); assert_eq!( send(&m.new, IMPORT, None, Some(CAR), &[], &car).await, Err((401, "AuthMissing".into())) ); assert_eq!( send( &m.new, IMPORT, Some(&format!("Bearer {token}")), None, &[], &car ) .await, Err((400, "InvalidRequest".into())) ); assert_eq!( import(&m.new, &token, &car).await, Err((507, "InsufficientStorage".into())) ); assert_eq!(repo_of(&m.new, &m.did, |r| r.head_string()), before); m.new.limits.disk.set_free(None); assert_eq!(import(&m.new, &token, &car).await, Ok(Json::Null)); } // ---- A did:web account -------------------------------------------------------- const CHECK: &str = "com.atproto.server.checkAccountStatus"; const DESCRIBE: &str = "com.atproto.repo.describeRepo"; const UPDATE_HANDLE: &str = "com.atproto.identity.updateHandle"; const RESOLVE: &str = "com.atproto.identity.resolveHandle"; /// A `did:web` of the test's own, `did:web:localhost%3A`: its host /// serves `doc` at `/.well-known/did.json`, `404` while it is `None`. struct WebDid { did: String, key: SigningKey, doc: Arc>>, } impl WebDid { async fn start() -> WebDid { use axum::response::IntoResponse; let doc: Arc>> = Arc::default(); let served = doc.clone(); let app = axum::Router::new().route( "/.well-known/did.json", axum::routing::get(move || { let d = served.lock().unwrap().clone(); async move { match d { Some(d) => axum::Json(d).into_response(), None => axum::http::StatusCode::NOT_FOUND.into_response(), } } }), ); let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0)) .await .unwrap(); let port = listener.local_addr().unwrap().port(); tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); WebDid { did: format!("did:web:localhost%3A{port}"), key: SigningKey::from_slice(&[11u8; 32]).unwrap(), doc, } } /// Serve a document naming `handle`, the signing key `signing` (a /// `did:key`) and the endpoint `pds`, as `atproto/did.md` lays one out. fn serve(&self, handle: &str, signing: &str, pds: &str) { let did = &self.did; *self.doc.lock().unwrap() = Some(json!({ "@context": ["https://www.w3.org/ns/did/v1", "https://w3id.org/security/multikey/v1"], "id": did, "alsoKnownAs": [format!("at://{handle}")], "verificationMethod": [{ "id": format!("{did}#atproto"), "type": "Multikey", "controller": did, "publicKeyMultibase": signing.trim_start_matches("did:key:") }], "service": [{ "id": "#atproto_pds", "type": "AtprotoPersonalDataServer", "serviceEndpoint": pds }], })); } fn served(&self) -> Option { self.doc.lock().unwrap().clone() } fn set(&self, doc: Option) { *self.doc.lock().unwrap() = doc; } } /// A service token of `iss` for `createAccount` on `did:web:new.test`, signed with `key`. fn web_token(iss: &str, key: &SigningKey) -> String { let claims = json!({ "iss": iss, "aud": "did:web:new.test", "exp": now() + 60, "lxm": CREATE }); format!( "Bearer {}", jwt(key, json!({ "alg": "ES256K", "typ": "JWT" }), claims) ) } /// The server a `did:web` moves to: `did:web:new.test` at `http://new.test`, /// mailing to `sink`. fn web_server(dir: &std::path::Path, plc: &str, sink: &std::path::Path) -> Shared { let sink = sink.to_path_buf(); open_with(dir, plc, move |c| { c.hostname = "new.test".into(); c.public_url = "http://new.test".into(); c.mailer = crate::email::Mailer::sink(&sink); }) } /// A DID neither a `did:plc` nor a `did:web` as `atproto/did.md` allows one. #[test] fn atproto_did_webs() { for ok in [ "did:web:example.com", "did:web:localhost%3A2583", "did:web:Localhost%3a2583", ] { assert!(crate::outbound::atproto_did_web(ok), "{ok}"); } for bad in [ "did:web:example.com%3A8443", "did:web:example.com:path", "did:web:", "did:web:a..b", "did:plc:aaaaaaaaaaaaaaaaaaaaaaaa", ] { assert!(!crate::outbound::atproto_did_web(bad), "{bad}"); } } /// What a `did:web` document names, as `atproto/did.md` takes it: the first /// `#atproto` method's key, and the first `#atproto_pds` service of its type. #[test] fn did_web_documents_name() { let key = SigningKey::from_slice(&[11u8; 32]).unwrap(); let other = SigningKey::from_slice(&[12u8; 32]).unwrap(); let mk = |k: &SigningKey| { crate::plc::did_key(k.verifying_key()) .trim_start_matches("did:key:") .to_string() }; let doc = json!({ "id": "did:web:x.test", "verificationMethod": [ { "id": "did:web:x.test#other", "type": "Multikey", "publicKeyMultibase": mk(&other) }, { "id": "did:web:x.test#atproto", "type": "Multikey", "publicKeyMultibase": mk(&key) }, { "id": "#atproto", "type": "Multikey", "publicKeyMultibase": mk(&other) }, ], "service": [ { "id": "#atproto_pds", "type": "Other", "serviceEndpoint": "https://a.test" }, { "id": "did:web:x.test#atproto_pds", "type": "AtprotoPersonalDataServer", "serviceEndpoint": "https://b.test" }, { "id": "#atproto_pds", "type": "AtprotoPersonalDataServer", "serviceEndpoint": "https://c.test" }, ], }); assert_eq!( crate::identity::web_signing_key(&doc), Some(Key::K256(*key.verifying_key())) ); assert_eq!(crate::identity::web_endpoint(&doc), Some("https://b.test")); assert_eq!(crate::identity::web_signing_key(&json!({})), None); assert_eq!( crate::identity::web_endpoint(&json!({ "service": [] })), None ); } /// A `did:web` account moves in: made by its owner's own service token, /// its document read from its host, with the errors `spec/MIGRATION.md` /// gives; activated once its document names this server, by endpoint and /// key alone; no PLC method applies; and its handle changes only to the one /// its document names, read again each time. #[tokio::test] async fn a_did_web_moves_in() { let plc = Plc::start().await; let (dir, sink) = (scratch("migration-web"), scratch("migration-web-mail")); std::fs::create_dir_all(&sink).unwrap(); let s = web_server(&dir, &plc.url, &sink); let w = WebDid::start().await; let own = crate::plc::did_key(w.key.verifying_key()); w.serve("carlos.test", &own, "https://old.example.com"); let body = |did: &str| json!({ "did": did, "handle": "carlos.test", "email": "carlos@mail.com", "password": "carlos-pw" }); let create = |did: &str, auth: Option| { let (s, b) = (s.clone(), body(did)); async move { send( &s, CREATE, auth.as_deref(), Some("application/json"), &[], b.to_string().as_bytes(), ) .await } }; let auth_required = Err((401, "AuthenticationRequired".to_string())); let internal = Err((500, "InternalServerError".to_string())); let refused = Err((400, "InvalidRequest".to_string())); // Refused: no token; a key its document does not name; its document not // found; one of another DID; a host that does not answer. assert_eq!(create(&w.did, None).await, auth_required); let other = SigningKey::from_slice(&[3u8; 32]).unwrap(); assert_eq!( create(&w.did, Some(web_token(&w.did, &other))).await, Err((401, "BadJwtSignature".into())) ); let served = w.served().unwrap(); w.set(None); assert_eq!( create(&w.did, Some(web_token(&w.did, &w.key))).await, auth_required ); let mut not_its = served.clone(); not_its["id"] = json!("did:web:other.test"); w.set(Some(not_its)); assert_eq!( create(&w.did, Some(web_token(&w.did, &w.key))).await, internal ); let closed = std::net::TcpListener::bind("127.0.0.1:0") .unwrap() .local_addr() .unwrap() .port(); let silent = format!("did:web:localhost%3A{closed}"); assert_eq!( create(&silent, Some(web_token(&silent, &w.key))).await, internal ); w.set(Some(served.clone())); // Made, deactivated, its didDoc the document served; nothing on the firehose. let made = create(&w.did, Some(web_token(&w.did, &w.key))) .await .unwrap(); assert_eq!(made["did"], json!(w.did)); assert_eq!(made["didDoc"], served); assert!(s.events.seqs_of(&w.did).is_empty()); let token = made["accessJwt"].as_str().unwrap().to_string(); let bearer = format!("Bearer {token}"); let status = || async { post(&s, CHECK, Some(&token), Json::Null).await.unwrap()["validDid"].clone() }; let activate = || send(&s, ACTIVATE, Some(&bearer), None, &[], b""); let describe = || { let p = [("repo".to_string(), w.did.clone())]; let s = s.clone(); async move { send(&s, DESCRIBE, None, None, &p, b"").await } }; assert_eq!(status().await, json!(false)); assert_eq!(activate().await, refused); // No PLC method applies, and nothing goes to the directory. let creds = post(&s, CREDENTIALS, Some(&token), Json::Null) .await .unwrap(); assert_eq!( send(&s, REQUEST, Some(&bearer), None, &[], b"").await, Ok(Json::Null) ); let mailed = crate::email::testing::last_token(&sink); assert_eq!( post(&s, SIGN, Some(&token), json!({ "token": mailed })).await, refused ); let mut op = creds.clone(); op["type"] = json!("plc_operation"); op["prev"] = Json::Null; op["sig"] = json!("c2ln"); assert_eq!( post(&s, SUBMIT, Some(&token), json!({ "operation": op })).await, refused ); assert!(plc.log(&w.did).is_empty()); // Its document pointed here, with the recommended key: activated. let signing = creds["verificationMethods"]["atproto"].as_str().unwrap(); w.serve("carlos.test", signing, "http://new.test"); assert_eq!(status().await, json!(true)); // Not with a key other than the account's. w.serve("carlos.test", &own, "http://new.test"); assert_eq!(status().await, json!(false)); w.serve("carlos.test", signing, "http://new.test"); assert_eq!(activate().await, Ok(Json::Null)); let got = describe().await.unwrap(); assert_eq!( (&got["didDoc"], &got["handleIsCorrect"]), (&w.served().unwrap(), &json!(true)) ); // Its handle: refused while its document names another, with no event; // then, once it does, given, with its `#identity`. let events = s.events.seqs_of(&w.did).len(); let update = |h: &str| post(&s, UPDATE_HANDLE, Some(&token), json!({ "handle": h })); assert_eq!(update("carlosx.test").await, refused); assert_eq!(s.account(&w.did).unwrap().handle(), "carlos.test"); assert_eq!(s.events.seqs_of(&w.did).len(), events); w.serve("carlosx.test", signing, "http://new.test"); assert_eq!(update("CarlosX.test").await, Ok(Json::Null)); assert_eq!(s.account(&w.did).unwrap().handle(), "carlosx.test"); assert_eq!(s.events.seqs_of(&w.did).len(), events + 1); assert_eq!( send( &s, RESOLVE, None, None, &[("handle".to_string(), "carlosx.test".to_string())], b"" ) .await, Ok(json!({ "did": w.did })) ); assert!(plc.log(&w.did).is_empty()); // The handle it had is free again. assert!(s.account("carlos.test").is_none()); // Its document gone: describeRepo `400`, validDid `false`, an activation // `400`, and a handle change `500`, which changes nothing. let named = w.served(); w.set(None); assert_eq!(describe().await, refused); assert_eq!(status().await, json!(false)); assert_eq!(activate().await, refused); assert_eq!(update("carlosx.test").await, internal); assert_eq!(s.events.seqs_of(&w.did).len(), events + 1); // No document of it: an activation is `500`. let mut not_its = named.clone().unwrap(); not_its["id"] = json!("did:web:other.test"); w.set(Some(not_its)); assert_eq!(activate().await, internal); w.set(named); // A restart keeps the handle, and settles nothing with the directory. drop(s); let s = web_server(&dir, &plc.url, &sink); assert_eq!(s.account(&w.did).unwrap().handle(), "carlosx.test"); assert_eq!( post( &s, UPDATE_HANDLE, Some(&token), json!({ "handle": "carlosx.test" }) ) .await, Ok(Json::Null) ); for d in [dir, sink] { let _ = std::fs::remove_dir_all(d); } } /// The DIDs `listRepos` lists. async fn repos(s: &Shared) -> Vec { let listed = post(s, "com.atproto.sync.listRepos", None, Json::Null) .await .unwrap(); listed["repos"] .as_array() .unwrap() .iter() .map(|r| r["did"].as_str().unwrap().to_string()) .collect() } /// An account deleted that moves here again with its DID is one account /// again, listed once: whether its row before was in the table or its /// deletion is, across a compaction and a start that loads none. #[tokio::test] async fn an_account_deleted_moves_here_again() { let sink = scratch("migration-again-mail"); std::fs::create_dir_all(&sink).unwrap(); let configure = { let sink = sink.clone(); move |c: &mut crate::Config| { c.hostname = "new.test".into(); c.public_url = "http://new.test".into(); c.rotation_key = SigningKey::from_slice(&[7u8; 32]).unwrap(); c.mailer = crate::email::Mailer::sink(&sink); } }; let mut m = Move::start("migration-again", configure.clone()).await; m.dirs.push(sink.clone()); let delete = |token: String| { let (new, did, sink) = (m.new.clone(), m.did.clone(), sink.clone()); async move { call( &new, "com.atproto.server.requestAccountDelete", Some(&token), Json::Null, ) .await .unwrap(); let body = json!({ "did": did, "password": "mover-pw", "token": crate::email::testing::last_token(&sink) }); call(&new, "com.atproto.server.deleteAccount", None, body) .await .unwrap(); } }; // Made, its row in the table; deleted and made again, both in the log. let token = m.moved().await; assert_eq!(repos(&m.new).await, [m.did.clone()]); m.new.log.compact().unwrap(); delete(token).await; assert_eq!(repos(&m.new).await, Vec::::new()); let token = m.moved().await; assert_eq!(repos(&m.new).await, [m.did.clone()]); let session = post(&m.new, SESSION, Some(&token), Json::Null) .await .unwrap(); assert_eq!(session["handle"], json!("mover.test")); // The table covers the deletion and the making again. m.new.log.compact().unwrap(); assert_eq!(repos(&m.new).await, [m.did.clone()]); // Deleted and made again once more, and a start that loads none. delete(token).await; m.moved().await; assert_eq!(repos(&m.new).await, [m.did.clone()]); let (dir, plc) = (m.dirs[1].clone(), m.plc.url.clone()); drop(std::mem::replace(&mut m.new, m.old.clone())); for compacted in [false, true] { let new = open_with(&dir, &plc, |c| { configure(c); c.load_all = false; }); if compacted { new.log.compact().unwrap(); } assert_eq!(repos(&new).await, [m.did.clone()], "compacted: {compacted}"); let found = new.account("mover.test").map(|a| a.did.clone()); assert_eq!(found, Some(m.did.clone()), "compacted: {compacted}"); } } /// A creation undone once its first commit is durable, the journal holding /// its frames: the journal forgets the account's directory before it goes. /// The same DID made again, and the server killed, a start writes nothing /// of the first making into the second's files, and serves the second. #[tokio::test] async fn an_undone_creation_leaves_nothing_in_the_next() { let m = Move::start("migration-undone", |_| {}).await; crate::server::UNDO_NEXT.with(|u| u.set(true)); assert!( post(&m.new, CREATE, Some(&m.token()), m.body()) .await .is_err() ); m.moved().await; let key = store_of(&m.new, &m.did, |st| st.repo.signing_key().to_bytes()); let copy = crate::log::copied(&m.dirs[1]); let redone = pds_store::journal::redo(©, pds_store::journal::Redo::Eager).unwrap(); assert_eq!( redone.written, 0, "the first making written into the second's files" ); let s = open_with(©, &m.plc.url, |c| { c.hostname = "new.test".into(); c.public_url = "http://new.test".into(); c.rotation_key = SigningKey::from_slice(&[7u8; 32]).unwrap(); }); assert_eq!( store_of(&s, &m.did, |st| st.repo.signing_key().to_bytes()), key ); let _ = std::fs::remove_dir_all(©); }