gent-daemon 0.2.2

Local daemon, storage and synchronization for Gent chat clusters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
use anyhow::{Result, bail};
use base64::{Engine as _, engine::general_purpose::STANDARD};
use gent_daemon::{
    config,
    pairing::{self, Options, Peer, Snapshot},
    store::Store,
};
use gent_protocol::{OwnerOp, PairingOp, Policy, Request, Response};
use serde_json::Value;
use std::{
    fs,
    os::unix::fs::{PermissionsExt, symlink},
    path::{Path, PathBuf},
};
#[path = "pairing/replacement.rs"]
mod replacement;

struct Owner {
    home: tempfile::TempDir,
    store: Store,
    credential: String,
    cluster: String,
    fail_after: Option<usize>,
    change_before: Option<usize>,
    calls: usize,
    corrupt_grant: bool,
    publication_conflict: Option<PathBuf>,
}
impl Owner {
    fn new() -> Self {
        Self::with_cluster(None)
    }
    fn with_cluster(cluster: Option<&str>) -> Self {
        let home = tempfile::tempdir().unwrap();
        fs::set_permissions(home.path(), fs::Permissions::from_mode(0o700)).unwrap();
        let mut cfg = config::initialize(home.path(), "pairing").unwrap();
        if let Some(cluster) = cluster {
            cfg.cluster.id = cluster.into();
        }
        cfg.relay = Some(config::RelayConfig {
            endpoint: "https://relay.example".into(),
            credential_file: "relay.key".into(),
        });
        config::write_private(&home.path().join("relay.key"), config::secret().as_bytes()).unwrap();
        config::save(home.path(), &cfg).unwrap();
        gent_daemon::entitlements::grant_test(home.path(), &cfg);
        let store = Store::open(home.path()).unwrap();
        let credential = config::read_private(&home.path().join("owner.key")).unwrap();
        Self {
            home,
            store,
            credential,
            cluster: cfg.cluster.id,
            fail_after: None,
            change_before: None,
            calls: 0,
            corrupt_grant: false,
            publication_conflict: None,
        }
    }
    fn owner(&mut self, action: OwnerOp) -> Result<Value> {
        match self.store.handle(Request::Owner {
            credential: self.credential.clone(),
            action,
        }) {
            Response::Ok(value) => Ok(value),
            Response::Error(error) => bail!(error),
        }
    }
    fn reopen(&mut self) {
        self.store = Store::open(self.home.path()).unwrap();
    }
}
impl Peer for Owner {
    async fn call(&mut self, action: PairingOp) -> Result<Value> {
        self.calls += 1;
        if self.change_before == Some(self.calls) {
            self.owner(OwnerOp::SetPolicy {
                policy: Policy::default(),
            })?;
        }
        let is_configuration = matches!(action, PairingOp::Configuration { .. });
        let is_accept = matches!(action, PairingOp::AcceptData { .. });
        let mut value = self.owner(OwnerOp::Pair {
            cluster_id: self.cluster.clone(),
            action,
        })?;
        if self.corrupt_grant && is_configuration {
            let mut bytes = STANDARD.decode(value["grant"].as_str().unwrap())?;
            let index = bytes.len() / 2;
            bytes[index] ^= 1;
            value["grant"] = STANDARD.encode(bytes).into();
        }
        if is_accept && let Some(path) = self.publication_conflict.take() {
            config::write_private(&path, b"preserve conflicting file")?;
        }
        if self.fail_after == Some(self.calls) {
            bail!("lost SSH reply after commit");
        }
        Ok(value)
    }
}

#[tokio::test]
async fn publication_recovers_partial_copies_and_preserves_login_and_lock_inodes() {
    use std::os::unix::fs::MetadataExt;
    let mut owner = Owner::new();
    let temp = tempfile::tempdir().unwrap();
    let home = temp.path().join("joined");
    config::private_dir(&home).unwrap();
    config::write_private(&home.join("auth/session.json"), b"unchanged fixture login").unwrap();
    drop(config::state_lock(&home, "service.lock").unwrap());
    let locked = config::state_lock(&home, "daemon.lock").unwrap();
    assert!(join(&home, &mut owner).await.is_err());
    assert_eq!(owner.calls, 0);
    drop(locked);
    let inodes = ["auth/session.json", "service.lock", "daemon.lock"]
        .map(|p| fs::metadata(home.join(p)).unwrap().ino());
    owner.publication_conflict = Some(home.join("owner.key"));
    assert!(join(&home, &mut owner).await.is_err());
    assert!(!config::path(&home).exists());
    assert!(home.join("identity.key").exists());
    assert_eq!(
        fs::read(home.join("owner.key")).unwrap(),
        b"preserve conflicting file"
    );
    assert!(config::initialize(&home, "replacement").is_err());
    let dir = config::pending_join(&home).unwrap();
    assert!(dir.join("publication.json").exists());
    let identity = fs::read(home.join("identity.key")).unwrap();
    let calls = owner.calls;
    fs::remove_file(home.join("owner.key")).unwrap(); // Remove only the injected fixture conflict.
    join(&home, &mut owner).await.unwrap();
    assert_eq!(
        owner.calls, calls,
        "committed publication should need no new owner requests"
    );
    assert_eq!(fs::read(home.join("identity.key")).unwrap(), identity);
    assert_eq!(
        fs::read(home.join("auth/session.json")).unwrap(),
        b"unchanged fixture login"
    );
    assert_eq!(
        inodes,
        ["auth/session.json", "service.lock", "daemon.lock"]
            .map(|p| fs::metadata(home.join(p)).unwrap().ino())
    );
    assert!(!dir.exists());
}

#[tokio::test]
async fn service_snapshot_never_exports_owner_credential_and_rejects_unprepared_clients() {
    let mut owner = Owner::new();
    let mut cfg = config::load(owner.home.path()).unwrap();
    cfg.cluster.local_only = false;
    cfg.service = Some(config::ServiceConfig {
        origin: "https://accounts.example/".into(),
        account_id: uuid::Uuid::new_v4().to_string(),
    });
    let token = format!("gent_t1.{}.{}", gent_daemon::store::now(), "ab".repeat(32));
    config::write_private(&owner.home.path().join("relay.key"), token.as_bytes()).unwrap();
    config::save(owner.home.path(), &cfg).unwrap();
    owner.reopen();
    let snapshot = owner
        .owner(OwnerOp::Pair {
            cluster_id: owner.cluster.clone(),
            action: PairingOp::Snapshot,
        })
        .unwrap();
    assert!(snapshot["relay"].is_null());
    assert_eq!(
        snapshot["service"]["account"]["account_id"],
        cfg.service.unwrap().account_id
    );
    assert!(!snapshot.to_string().contains(&token));
    let temp = tempfile::tempdir().unwrap();
    assert!(join(&temp.path().join("joined"), &mut owner).await.is_err());
    assert_eq!(
        owner
            .owner(OwnerOp::Nodes)
            .unwrap()
            .as_array()
            .unwrap()
            .len(),
        1
    );
}
async fn join(home: &Path, owner: &mut Owner) -> Result<Value> {
    let cluster = owner.cluster.clone();
    pairing::join(
        home,
        Options {
            replace_node: None,
            cluster: &cluster,
            name: "new-node",
            relay_endpoint: None,
        },
        owner,
    )
    .await
}

#[tokio::test]
async fn joined_node_keeps_its_keys_and_imports_only_development_relay_access() {
    let mut owner = Owner::new();
    let temp = tempfile::tempdir().unwrap();
    let home = temp.path().join("joined");
    let result = join(&home, &mut owner).await.unwrap();
    assert_eq!(result["joined"], true);
    assert_eq!(result["configuration_relay"], true);
    let cfg = config::load(&home).unwrap();
    assert_eq!(cfg.node.name, "new-node");
    assert_eq!(cfg.cluster.id, owner.cluster);
    assert_eq!(
        config::read_private(&home.join("relay.key")).unwrap(),
        config::read_private(&owner.home.path().join("relay.key")).unwrap()
    );
    assert_ne!(
        config::read_private(&home.join("owner.key")).unwrap(),
        owner.credential
    );
    let identity = fs::read(home.join("identity.key")).unwrap();
    assert_eq!(&identity[..8], b"GENTKEY2");
    let owner_key = fs::read(owner.home.path().join("identity.key")).unwrap();
    assert!(
        !identity
            .windows(32)
            .any(|bytes| bytes == &owner_key[40..72])
    );
    let mut joined = Store::open(&home).unwrap();
    assert!(matches!(
        joined.handle(Request::Owner {
            credential: config::read_private(&home.join("owner.key")).unwrap(),
            action: OwnerOp::Pair {
                cluster_id: owner.cluster.clone(),
                action: PairingOp::Snapshot
            }
        }),
        Response::Error(_)
    ));
    assert!(!home.with_file_name("joined.joining").exists());
    assert_eq!(
        owner
            .owner(OwnerOp::Nodes)
            .unwrap()
            .as_array()
            .unwrap()
            .len(),
        2
    );
    let calls = owner.calls;
    assert_eq!(
        join(&home, &mut owner).await.unwrap()["already_joined"],
        true
    );
    assert_eq!(owner.calls, calls);
    let node = config::load(&home).unwrap().node.id;
    owner
        .owner(OwnerOp::DelegateConfiguration {
            node_id: node.clone(),
            allow: false,
        })
        .unwrap();
    join(&home, &mut owner).await.unwrap();
    assert!(
        owner
            .owner(OwnerOp::Nodes)
            .unwrap()
            .as_array()
            .unwrap()
            .iter()
            .any(|n| n["id"] == node && n["can_configure"] == false)
    );
}

#[tokio::test]
async fn interrupted_pairing_pins_history_until_verified_configuration_catches_up() {
    use gent_crypto::NodeIdentity;
    use gent_daemon::{encrypted::ordered::OrderedEndpoint, store::now};
    let mut owner = Owner::new();
    let temp = tempfile::tempdir().unwrap();
    let home = temp.path().join("joined");
    owner.fail_after = Some(4);
    assert!(join(&home, &mut owner).await.is_err());
    for _ in 0..80 {
        owner.owner(OwnerOp::Renew).unwrap();
    }
    owner
        .owner(OwnerOp::TransportClean { dry_run: false })
        .unwrap();
    let status = owner.owner(OwnerOp::TransportStatus).unwrap();
    assert_eq!(status["authority_history"]["pending_join_pins"], 1);
    assert!(
        status["authority_history"]["full_records"]
            .as_u64()
            .unwrap()
            > 64
    );
    owner.fail_after = None;
    owner.reopen();
    join(&home, &mut owner).await.unwrap();
    let load = |home: &Path| {
        let config = config::load(home).unwrap();
        let key = fs::read(home.join("identity.key")).unwrap();
        NodeIdentity::from_secret_bytes(&config.node.id, key[8..40].try_into().unwrap()).unwrap()
    };
    let writer = load(owner.home.path());
    let reader = load(&home);
    let mut source = OrderedEndpoint::open_runtime(owner.home.path(), &writer).unwrap();
    let mut target = OrderedEndpoint::open_runtime(&home, &reader).unwrap();
    let db = rusqlite::Connection::open(owner.home.path().join("state.sqlite3")).unwrap();
    let group: Vec<u8> = db
        .query_row("SELECT group_id FROM configuration_streams", [], |r| {
            r.get(0)
        })
        .unwrap();
    while let Some(packet) = source
        .next_configuration(&group, uuid::Uuid::new_v4(), now() as u64)
        .unwrap()
    {
        target.stage_configuration(&packet, now() as u64).unwrap();
        target.drain_configuration(&group, 1, now() as u64).unwrap();
    }
    let receipt = target.receipt(&group, now() as u64).unwrap();
    source.confirm(&receipt, reader.id(), now() as u64).unwrap();
    while owner
        .owner(OwnerOp::TransportClean { dry_run: false })
        .unwrap()["authority_records_compacted"]
        .as_u64()
        .unwrap()
        > 0
    {}
    let status = owner.owner(OwnerOp::TransportStatus).unwrap();
    assert_eq!(status["authority_history"]["pending_join_pins"], 0);
    assert_eq!(status["authority_history"]["full_records"], 64);
}

#[tokio::test]
async fn lost_pairing_replies_resume_without_duplicate_members_or_streams() {
    for interrupted in 1..=8 {
        let mut owner = Owner::new();
        let temp = tempfile::tempdir().unwrap();
        let home = temp.path().join("joined");
        owner.fail_after = Some(interrupted);
        assert!(join(&home, &mut owner).await.is_err());
        assert!(!config::path(&home).exists());
        let before = fs::read(home.with_file_name("joined.joining").join("join.key")).unwrap();
        owner.fail_after = None;
        owner.reopen();
        join(&home, &mut owner).await.unwrap();
        let after = fs::read(home.join("identity.key")).unwrap();
        assert_eq!(&before[8..40], &after[8..40]);
        assert_eq!(
            owner
                .owner(OwnerOp::Nodes)
                .unwrap()
                .as_array()
                .unwrap()
                .len(),
            2
        );
        let db = rusqlite::Connection::open(owner.home.path().join("state.sqlite3")).unwrap();
        assert_eq!(
            db.query_row("SELECT COUNT(*) FROM configuration_streams", [], |r| r
                .get::<_, usize>(0))
                .unwrap(),
            1
        );
    }
}

#[tokio::test]
async fn concurrent_owner_edits_are_verified_before_joining_the_stream() {
    let mut owner = Owner::new();
    owner.change_before = Some(4); // Between key-package creation and the stream grant.
    let temp = tempfile::tempdir().unwrap();
    let home = temp.path().join("joined");
    join(&home, &mut owner).await.unwrap();
    assert_eq!(config::load(&home).unwrap().applied_revision, Some(2));
}

#[tokio::test]
async fn stale_enrollment_retries_preserve_identity_and_existing_directories() {
    let mut owner = Owner::new();
    owner.change_before = Some(2); // Enrollment request no longer has the owner's head.
    let temp = tempfile::tempdir().unwrap();
    let home = temp.path().join("joined");
    assert!(join(&home, &mut owner).await.is_err());
    let seed = fs::read(home.with_file_name("joined.joining").join("join.key")).unwrap();
    owner.change_before = None;
    join(&home, &mut owner).await.unwrap();
    assert_eq!(
        &seed[8..40],
        &fs::read(home.join("identity.key")).unwrap()[8..40]
    );
    let existing = temp.path().join("unrelated");
    config::private_dir(&existing).unwrap();
    fs::write(existing.join("keep"), "untouched").unwrap();
    assert!(join(&existing, &mut owner).await.is_err());
    assert_eq!(
        fs::read_to_string(existing.join("keep")).unwrap(),
        "untouched"
    );
}

#[tokio::test]
async fn tampered_grants_and_missing_or_shared_pending_keys_fail_closed() {
    let mut owner = Owner::new();
    owner.corrupt_grant = true;
    let temp = tempfile::tempdir().unwrap();
    let home = temp.path().join("joined");
    assert!(join(&home, &mut owner).await.is_err());
    assert!(!config::path(&home).exists());
    let pending = home.with_file_name("joined.joining");
    let key = pending.join("join.key");
    fs::set_permissions(&key, fs::Permissions::from_mode(0o644)).unwrap();
    assert!(join(&home, &mut owner).await.is_err());
    fs::remove_file(&key).unwrap();
    assert!(join(&home, &mut owner).await.is_err());
    symlink(temp.path().join("nonexistent"), &key).unwrap();
    assert!(join(&home, &mut owner).await.is_err());
    assert!(!temp.path().join("nonexistent").exists());
}

#[tokio::test]
async fn resumed_pairing_rejects_a_valid_snapshot_under_a_replaced_owner_key() {
    let mut owner = Owner::new();
    owner.fail_after = Some(2);
    let temp = tempfile::tempdir().unwrap();
    let home = temp.path().join("joined");
    assert!(join(&home, &mut owner).await.is_err());
    let mut replacement = Owner::with_cluster(Some(&owner.cluster));
    let error = join(&home, &mut replacement).await.unwrap_err();
    assert!(error.to_string().contains("authority changed"));
    assert_eq!(replacement.calls, 1);
    assert_eq!(
        replacement
            .owner(OwnerOp::Nodes)
            .unwrap()
            .as_array()
            .unwrap()
            .len(),
        1
    );
    assert!(!config::path(&home).exists());
}

#[tokio::test]
async fn a_stale_unconsumed_stream_grant_gets_a_new_package_without_replacing_node_keys() {
    let mut owner = Owner::new();
    owner.fail_after = Some(4);
    let temp = tempfile::tempdir().unwrap();
    let home = temp.path().join("joined");
    assert!(join(&home, &mut owner).await.is_err());
    let dir = home.with_file_name("joined.joining");
    let bytes = fs::read(dir.join("join.key")).unwrap();
    let seed: Value = serde_json::from_slice(&bytes[40..]).unwrap();
    let node = gent_crypto::NodeIdentity::from_secret_bytes(
        seed["node"].as_str().unwrap(),
        bytes[8..40].try_into().unwrap(),
    )
    .unwrap();
    owner
        .owner(OwnerOp::SetPolicy {
            policy: Policy::default(),
        })
        .unwrap();
    owner.fail_after = None;
    let value = owner
        .owner(OwnerOp::Pair {
            cluster_id: owner.cluster.clone(),
            action: PairingOp::Snapshot,
        })
        .unwrap();
    let snapshot: Snapshot = serde_json::from_value(value).unwrap();
    gent_daemon::encrypted::ordered::OrderedEndpoint::open_runtime(&dir.join("node"), &node)
        .unwrap()
        .apply_authority(&[gent_crypto::SignedState::decode(
            &STANDARD.decode(snapshot.state).unwrap(),
        )
        .unwrap()])
        .unwrap();
    join(&home, &mut owner).await.unwrap();
    assert_eq!(
        &bytes[8..40],
        &fs::read(home.join("identity.key")).unwrap()[8..40]
    );
    let db = rusqlite::Connection::open(owner.home.path().join("state.sqlite3")).unwrap();
    assert_eq!(
        db.query_row("SELECT COUNT(*) FROM configuration_streams", [], |r| r
            .get::<_, usize>(0))
            .unwrap(),
        2
    );
    assert_eq!(
        owner
            .owner(OwnerOp::Nodes)
            .unwrap()
            .as_array()
            .unwrap()
            .len(),
        2
    );
}

#[test]
fn pairing_requires_owner_authority_exact_cluster_and_checkpoint() {
    let mut owner = Owner::new();
    let response = owner.store.handle(Request::Owner {
        credential: config::secret(),
        action: OwnerOp::Pair {
            cluster_id: owner.cluster.clone(),
            action: PairingOp::Snapshot,
        },
    });
    assert!(matches!(response, Response::Error(_)));
    assert!(
        owner
            .owner(OwnerOp::Pair {
                cluster_id: uuid::Uuid::new_v4().to_string(),
                action: PairingOp::Snapshot
            })
            .is_err()
    );
    let value = owner
        .owner(OwnerOp::Pair {
            cluster_id: owner.cluster.clone(),
            action: PairingOp::Snapshot,
        })
        .unwrap();
    let snapshot: Snapshot = serde_json::from_value(value).unwrap();
    assert!(
        owner
            .owner(OwnerOp::Pair {
                cluster_id: owner.cluster.clone(),
                action: PairingOp::History {
                    revision: snapshot.checkpoint.revision,
                    digest: [0; 32]
                }
            })
            .is_err()
    );
    let db = rusqlite::Connection::open(owner.home.path().join("state.sqlite3")).unwrap();
    assert_eq!(
        db.query_row("SELECT COUNT(*) FROM authority_history", [], |r| r
            .get::<_, usize>(0))
            .unwrap(),
        1
    );
}

#[tokio::test]
async fn joining_without_a_configuration_transport_does_not_enroll_a_disconnected_node() {
    let mut owner = Owner::new();
    let mut cfg = config::load(owner.home.path()).unwrap();
    cfg.relay = None;
    config::save(owner.home.path(), &cfg).unwrap();
    owner.reopen();
    let temp = tempfile::tempdir().unwrap();
    let error = join(&temp.path().join("node"), &mut owner)
        .await
        .unwrap_err();
    assert!(error.to_string().contains("no configuration relay"));
    assert_eq!(owner.calls, 1);
    assert_eq!(
        owner
            .owner(OwnerOp::Nodes)
            .unwrap()
            .as_array()
            .unwrap()
            .len(),
        1
    );
}

#[tokio::test]
async fn upgrading_joined_runtime_certifies_legacy_agents_without_changing_credentials() {
    let mut owner = Owner::new();
    let temp = tempfile::tempdir().unwrap();
    let home = temp.path().join("joined");
    join(&home, &mut owner).await.unwrap();
    let identity = fs::read(home.join("identity.key")).unwrap();
    let credential = config::secret();
    let agent = uuid::Uuid::new_v4().to_string();
    // An old runtime has the original registration table but no data link or
    // agent-origin records. Preserve its random ID and hashed MCP credential.
    let db = rusqlite::Connection::open(home.join("state.sqlite3")).unwrap();
    db.execute("DELETE FROM network_links", []).unwrap();
    use sha2::{Digest, Sha256};
    let hash = format!("{:x}", Sha256::digest(credential.as_bytes()));
    db.execute(
        "INSERT INTO agents VALUES(?1,?2,?3,'legacy','existing-host',0,0)",
        rusqlite::params![agent, hash, config::load(&home).unwrap().node.id],
    )
    .unwrap();
    drop(db);
    join(&home, &mut owner).await.unwrap();
    assert_eq!(identity, fs::read(home.join("identity.key")).unwrap());
    let mut joined = Store::open(&home).unwrap();
    let response = joined.handle(Request::Agent {
        credential,
        action: gent_protocol::AgentOp::Register {
            name: "legacy".into(),
            provider: "existing-host".into(),
        },
    });
    match response {
        Response::Ok(value) => assert_eq!(value["agent"]["id"], agent),
        Response::Error(error) => panic!("{error}"),
    }
    let db = rusqlite::Connection::open(home.join("state.sqlite3")).unwrap();
    let certificate: Vec<u8> = db
        .query_row(
            "SELECT legacy FROM network_origins WHERE agent=?1",
            [&agent],
            |r| r.get(0),
        )
        .unwrap();
    let certificate: gent_crypto::SignedLegacyAgentBinding =
        serde_json::from_slice(&certificate).unwrap();
    assert_eq!(certificate.binding.agent, agent);
    assert_eq!(
        certificate.binding.node,
        config::load(&home).unwrap().node.id
    );
}