Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Two ways to ask the account whether Notes is actually right

Yashwanth Kamireddic1ba459accd6
diff --git a/probe/examples/edit_loop.rs b/probe/examples/edit_loop.rs
new file mode 100644
index 0000000..3f387c8
--- /dev/null
+++ b/probe/examples/edit_loop.rs
@@ -0,0 +1,120 @@
+//! Throwaway: type into a note the way a person does, and check Apple holds exactly that.
+//!
+//! The failure this is for: a note on the account came back with everything typed into it
+//! still there, over and over, because each save left behind a record of text that was no
+//! longer in it.
+use anyhow::{anyhow, Result};
+use serde_json::Value;
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");
+ let mut client = quince_auth::Client::new(&dir)?;
+ if !client.resume().await? {
+ return Err(anyhow!("no session"));
+ }
+ let root = client
+ .session()
+ .service_url("ckdatabasews")
+ .ok_or_else(|| anyhow!("no ckdatabasews"))?;
+ let http = client.http().clone();
+ let params = client.service_params();
+ let nz =
+ quince_icloud::cloudkit::ZoneClient::new(&http, &root, "com.apple.notes", params.clone());
+
+ let store = std::env::temp_dir().join(format!("quince-{}", uuid::Uuid::new_v4()));
+ std::fs::create_dir_all(&store)?;
+ let mut library = quince_core::Library::open(&store)?;
+ quince_icloud::notes::sync_cloudkit(&http, &root, params.clone(), &mut library, None).await?;
+ let template = library.state(quince_icloud::notes::TEMPLATE_KEY)?;
+
+ let guid = quince_icloud::notes::create_cloudkit(
+ &http,
+ &root,
+ params.clone(),
+ Some("DefaultFolder-CloudKit"),
+ "edit test",
+ template.as_deref(),
+ )
+ .await?;
+ println!("created {guid}");
+
+ // What someone typing actually produces: a growing body, saved after each pause.
+ let steps = [
+ "edit test\n1",
+ "edit test\n1\n2",
+ "edit test\n1\n2\n3",
+ "edit test\n1\n2\n3\n4",
+ "edit test\n1\n2\n3\n4\n56",
+ // And a deletion, which is where an append-only mistake shows up loudest.
+ "edit test\n1\n2",
+ "short",
+ ];
+
+ let mut failures = 0;
+ for (n, want) in steps.iter().enumerate() {
+ let note = library_note(&http, &root, &params, &guid).await?;
+ quince_icloud::notes::edit_cloudkit(&http, &root, params.clone(), &note, want).await?;
+
+ // Read it off Apple, not off the reply.
+ let r = nz
+ .lookup_record("Notes", &guid)
+ .await?
+ .ok_or_else(|| anyhow!("gone"))?;
+ let doc = r
+ .pointer("/fields/TextDataEncrypted/value")
+ .and_then(Value::as_str)
+ .ok_or_else(|| anyhow!("no body"))?;
+ let got = quince_icloud::ckdocument::text_from_base64(doc).unwrap_or_default();
+ let runs = {
+ use base64::{engine::general_purpose::STANDARD as B64, Engine};
+ let raw = B64.decode(doc).unwrap_or_default();
+ quince_icloud::ckdocument::debug_attribute_lengths(&raw)
+ };
+ let units: u64 = want.encode_utf16().count() as u64;
+ let covered: u64 = runs.iter().sum();
+ let ok = got == *want && covered == units;
+ if !ok {
+ failures += 1;
+ }
+ println!(
+ " {} step {n}: {:?} runs cover {covered} of {units}",
+ if ok { "PASS" } else { "FAIL" },
+ got.chars().take(28).collect::<String>()
+ );
+ }
+
+ let note = library_note(&http, &root, &params, &guid).await?;
+ quince_icloud::notes::delete_cloudkit(&http, &root, params.clone(), &note).await?;
+ println!("\ncleaned up");
+ if failures > 0 {
+ return Err(anyhow!("{failures} step(s) did not hold"));
+ }
+ println!("every edit landed as exactly what was typed");
+ Ok(())
+}
+
+/// The note as the app would have it: synced, then read from the library.
+async fn library_note(
+ http: &reqwest::Client,
+ root: &str,
+ params: &[(String, String)],
+ guid: &str,
+) -> Result<quince_core::notes::Note> {
+ let store = std::env::temp_dir().join(format!("quince-{}", uuid::Uuid::new_v4()));
+ std::fs::create_dir_all(&store)?;
+ let mut library = quince_core::Library::open(&store)?;
+ quince_icloud::notes::sync_cloudkit(http, root, params.to_vec(), &mut library, None).await?;
+ library
+ .notes_in(None)?
+ .into_iter()
+ .find(|n| n.guid == guid)
+ .or_else(|| {
+ library
+ .notes_in(Some("TrashFolder-CloudKit"))
+ .ok()?
+ .into_iter()
+ .find(|n| n.guid == guid)
+ })
+ .ok_or_else(|| anyhow!("the note is not in the library"))
+}
diff --git a/probe/examples/notes_audit.rs b/probe/examples/notes_audit.rs
new file mode 100644
index 0000000..3ac6d82
--- /dev/null
+++ b/probe/examples/notes_audit.rs
@@ -0,0 +1,133 @@
+//! Throwaway: every note, as Apple has it and as this app stored it, field by field.
+use anyhow::{anyhow, Result};
+use serde_json::Value;
+
+fn f<'a>(r: &'a Value, k: &str) -> Option<&'a Value> {
+ r.pointer(&format!("/fields/{k}/value"))
+}
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");
+ let mut client = quince_auth::Client::new(&dir)?;
+ if !client.resume().await? {
+ return Err(anyhow!("no session"));
+ }
+ let root = client
+ .session()
+ .service_url("ckdatabasews")
+ .ok_or_else(|| anyhow!("no ckdatabasews"))?;
+ let http = client.http().clone();
+ let params = client.service_params();
+
+ let store = std::env::temp_dir().join(format!("quince-{}", uuid::Uuid::new_v4()));
+ std::fs::create_dir_all(&store)?;
+ let mut library = quince_core::Library::open(&store)?;
+ quince_icloud::notes::sync_cloudkit(&http, &root, params.clone(), &mut library, None).await?;
+
+ let nz = quince_icloud::cloudkit::ZoneClient::new(&http, &root, "com.apple.notes", params);
+ let walk = nz.all_records("Notes", None).await?;
+
+ let mut stored: std::collections::HashMap<String, quince_core::notes::Note> =
+ std::collections::HashMap::new();
+ for folder in [
+ None,
+ Some("TrashFolder-CloudKit"),
+ Some("DefaultFolder-CloudKit"),
+ ] {
+ for n in library.notes_in(folder)? {
+ stored.entry(n.guid.clone()).or_insert(n);
+ }
+ }
+
+ let mut problems = 0;
+ for r in walk
+ .records
+ .iter()
+ .filter(|r| r.get("recordType").and_then(Value::as_str) == Some("Note"))
+ .filter(|r| f(r, "Deleted").and_then(Value::as_i64) != Some(1))
+ {
+ let guid = r
+ .pointer("/recordName")
+ .and_then(Value::as_str)
+ .unwrap_or("?")
+ .to_string();
+ let apple_title = f(r, "TitleEncrypted")
+ .and_then(Value::as_str)
+ .and_then(quince_icloud::ckdocument::text_from_base64)
+ .unwrap_or_default();
+ let apple_body = f(r, "TextDataEncrypted")
+ .and_then(Value::as_str)
+ .and_then(quince_icloud::ckdocument::text_from_base64);
+ let apple_folder = r
+ .pointer("/fields/Folders/value")
+ .and_then(Value::as_array)
+ .and_then(|a| a.first())
+ .and_then(|v| v.get("recordName"))
+ .and_then(Value::as_str)
+ .or_else(|| {
+ r.pointer("/fields/Folder/value/recordName")
+ .and_then(Value::as_str)
+ });
+
+ let Some(mine) = stored.get(&guid) else {
+ println!(
+ "MISSING {guid} {:?} — Apple has it, the library does not",
+ apple_title.chars().take(30).collect::<String>()
+ );
+ problems += 1;
+ continue;
+ };
+
+ let mut says = Vec::new();
+ if mine.folder_guid.as_deref() != apple_folder {
+ says.push(format!(
+ "folder {:?} vs {:?}",
+ mine.folder_guid, apple_folder
+ ));
+ }
+ let body_len = |b: &Option<String>| b.as_deref().map(str::len);
+ let mine_body = library.note_body(&guid)?;
+ if body_len(&mine_body) != body_len(&apple_body) {
+ says.push(format!(
+ "body {:?} chars vs {:?}",
+ body_len(&mine_body),
+ body_len(&apple_body)
+ ));
+ }
+ if mine.title != apple_title && !apple_title.is_empty() {
+ says.push(format!("title {:?} vs {:?}", mine.title, apple_title));
+ }
+ if says.is_empty() {
+ println!(
+ "ok {:<34} {guid}",
+ mine.title.chars().take(32).collect::<String>()
+ );
+ } else {
+ println!(
+ "DIFFERS {:<34} {}",
+ mine.title.chars().take(32).collect::<String>(),
+ says.join("; ")
+ );
+ problems += 1;
+ }
+ }
+ // And the other direction: anything the library holds that the account does not, which
+ // is how a deleted note lives on as a ghost and gets written back on the next save.
+ let live: std::collections::HashSet<&str> = walk
+ .records
+ .iter()
+ .filter(|r| r.get("recordType").and_then(Value::as_str) == Some("Note"))
+ .filter(|r| f(r, "Deleted").and_then(Value::as_i64) != Some(1))
+ .filter_map(|r| r.pointer("/recordName").and_then(Value::as_str))
+ .collect();
+ for guid in stored.keys() {
+ if !live.contains(guid.as_str()) {
+ println!("GHOST {guid} — the library has it, the account does not");
+ problems += 1;
+ }
+ }
+
+ println!("\n{problems} note(s) where the library and the account disagree");
+ Ok(())
+}