gent-daemon 0.2.5

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
use gent_crypto::files::{CHUNK_BYTES, FileGrant, MAX_FILE_BYTES, seal};
use gent_daemon::file_objects::{
    self, INCOMPLETE_TTL_SECONDS, MAX_OBJECTS, MAX_RESERVED_BYTES, MAX_RETENTION_SECONDS, Objects,
};
use rusqlite::{Connection, TransactionBehavior, params};
use serde_json::{Value, json};

const NOW: u64 = 1_800_000_000;
const EXPIRY: u64 = NOW + 86_400;

fn open(path: &std::path::Path) -> Connection {
    let mut db = Connection::open(path).unwrap();
    db.execute_batch("PRAGMA foreign_keys=ON; PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL;")
        .unwrap();
    file_objects::initialize(&mut db).unwrap();
    db
}

fn count(db: &Connection, table: &str) -> u64 {
    // Test-owned table names only.
    db.query_row(&format!("SELECT COUNT(*) FROM {table}"), [], |r| r.get(0))
        .unwrap()
}

#[test]
fn sender_restarts_retry_exact_ciphertext_without_duplicate_upload_state() {
    let home = tempfile::tempdir().unwrap();
    let path = home.path().join("state.sqlite3");
    let bytes: Vec<u8> = (0..CHUNK_BYTES * 2 + 9).map(|n| (n % 251) as u8).collect();
    let sealed = seal("private-shot.png", "image/png", &bytes).unwrap();
    let id = &sealed.grant().descriptor().id;
    {
        let mut db = open(&path);
        let tx = db
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .unwrap();
        let store = Objects::new(&tx, NOW).unwrap();
        store.stage(&sealed, EXPIRY).unwrap();
        assert_eq!(store.pending_uploads(id).unwrap(), [0, 1, 2]);
        assert_eq!(store.upload_chunk(id, 0).unwrap(), sealed.chunks()[0]);
        store.acknowledge_upload(id, 0).unwrap();
        store.acknowledge_upload(id, 0).unwrap();
        tx.commit().unwrap();
    }
    let mut db = open(&path);
    let tx = db
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .unwrap();
    let store = Objects::new(&tx, NOW + 1).unwrap();
    store.stage(&sealed, EXPIRY).unwrap();
    assert_eq!(count(&tx, "file_objects"), 1);
    assert_eq!(count(&tx, "file_chunks"), 3);
    assert_eq!(store.pending_uploads(id).unwrap(), [1, 2]);
    assert_eq!(
        store.transport_manifest(id).unwrap(),
        sealed.transport_manifest(EXPIRY).unwrap()
    );
    assert_eq!(
        store.grant(id).unwrap().encode_private().unwrap(),
        sealed.grant().encode_private().unwrap()
    );
    assert_eq!(*store.read(id).unwrap(), bytes);
    for index in [1, 2] {
        assert_eq!(
            store.upload_chunk(id, index).unwrap(),
            sealed.chunks()[index as usize]
        );
        store.acknowledge_upload(id, index).unwrap();
    }
    assert!(store.pending_uploads(id).unwrap().is_empty());
    assert_eq!(store.status(id).unwrap().chunks_uploaded, 3);
    assert!(store.acknowledge_upload(id, 3).is_err());
    store.retry_upload(id).unwrap();
    assert_eq!(store.pending_uploads(id).unwrap(), [0, 1, 2]);
    assert_eq!(store.upload_chunk(id, 0).unwrap(), sealed.chunks()[0]);
    tx.commit().unwrap();
}

#[test]
fn receiver_resumes_out_of_order_chunks_and_verifies_before_reading() {
    let home = tempfile::tempdir().unwrap();
    let path = home.path().join("state.sqlite3");
    let bytes = vec![71; CHUNK_BYTES * 2 + 9];
    let sealed = seal("private.bin", "application/octet-stream", &bytes).unwrap();
    let id = &sealed.grant().descriptor().id;
    {
        let mut db = open(&path);
        let tx = db
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .unwrap();
        let store = Objects::new(&tx, NOW).unwrap();
        store.expect(sealed.grant(), EXPIRY).unwrap();
        store.accept_chunk(id, 2, &sealed.chunks()[2]).unwrap();
        store.accept_chunk(id, 2, &sealed.chunks()[2]).unwrap();
        let mut damaged = sealed.chunks()[0].clone();
        damaged[0] ^= 1;
        assert!(store.accept_chunk(id, 0, &damaged).is_err());
        assert!(store.accept_chunk(id, 0, &sealed.chunks()[1]).is_err());
        assert!(store.accept_chunk(id, 3, &sealed.chunks()[2]).is_err());
        assert_eq!(store.missing_chunks(id).unwrap(), [0, 1]);
        assert!(store.read(id).is_err());
        assert!(store.upload_chunk(id, 2).is_err());
        assert!(store.pending_uploads(id).is_err());
        assert!(store.transport_manifest(id).is_err());
        assert!(store.acknowledge_upload(id, 2).is_err());
        assert!(store.finish(id).is_err());
        tx.commit().unwrap();
    }
    {
        let mut db = open(&path);
        let tx = db
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .unwrap();
        let store = Objects::new(&tx, NOW + 5).unwrap();
        store.expect(sealed.grant(), EXPIRY).unwrap();
        assert_eq!(
            store.status(id).unwrap().incomplete_until,
            NOW + INCOMPLETE_TTL_SECONDS
        );
        for index in [1, 0] {
            store
                .accept_chunk(id, index, &sealed.chunks()[index as usize])
                .unwrap();
        }
        assert!(store.missing_chunks(id).unwrap().is_empty());
        assert!(store.read(id).is_err());
        tx.commit().unwrap();
    }
    let mut db = open(&path);
    let tx = db
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .unwrap();
    let store = Objects::new(&tx, NOW + 9).unwrap();
    store.finish(id).unwrap();
    assert_eq!(*store.read(id).unwrap(), bytes);
    store.finish(id).unwrap();
    assert!(store.status(id).unwrap().complete);
    assert_eq!(count(&tx, "file_chunks"), 3);
    tx.commit().unwrap();
}

#[test]
fn storage_failures_roll_back_files_and_enclosing_message_transaction() {
    let home = tempfile::tempdir().unwrap();
    let mut db = open(&home.path().join("state.sqlite3"));
    let sealed = seal("image.png", "image/png", &vec![1; CHUNK_BYTES + 1]).unwrap();
    db.execute_batch(
        "CREATE TABLE message_markers(id TEXT PRIMARY KEY);
         CREATE TRIGGER fail_chunk BEFORE INSERT ON file_chunks WHEN NEW.position=1
         BEGIN SELECT RAISE(ABORT,'injected chunk failure'); END;",
    )
    .unwrap();
    {
        let tx = db
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .unwrap();
        tx.execute("INSERT INTO message_markers VALUES('first')", [])
            .unwrap();
        let store = Objects::new(&tx, NOW).unwrap();
        assert!(store.stage(&sealed, EXPIRY).is_err());
        // Dropping the failed transaction must discard both message and files.
    }
    for table in ["file_objects", "file_chunks", "message_markers"] {
        assert_eq!(count(&db, table), 0);
    }
    db.execute_batch(
        "DROP TRIGGER fail_chunk;
         CREATE TRIGGER fail_message BEFORE INSERT ON message_markers
         BEGIN SELECT RAISE(ABORT,'injected message failure'); END;",
    )
    .unwrap();
    {
        let tx = db
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .unwrap();
        Objects::new(&tx, NOW)
            .unwrap()
            .stage(&sealed, EXPIRY)
            .unwrap();
        assert!(
            tx.execute("INSERT INTO message_markers VALUES('first')", [])
                .is_err()
        );
    }
    assert_eq!(count(&db, "file_objects"), 0);
    assert_eq!(count(&db, "file_chunks"), 0);
    db.execute_batch("DROP TRIGGER fail_message;").unwrap();
    let tx = db
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .unwrap();
    Objects::new(&tx, NOW)
        .unwrap()
        .stage(&sealed, EXPIRY)
        .unwrap();
    tx.execute("INSERT INTO message_markers VALUES('first')", [])
        .unwrap();
    tx.commit().unwrap();
    assert_eq!(count(&db, "file_objects"), 1);
    assert_eq!(count(&db, "file_chunks"), 2);
    assert_eq!(count(&db, "message_markers"), 1);
}

#[test]
fn retries_cannot_replace_keys_manifests_deadlines_or_committed_chunks() {
    let home = tempfile::tempdir().unwrap();
    let mut db = open(&home.path().join("state.sqlite3"));
    let sealed = seal("image.png", "image/png", b"secret original bytes").unwrap();
    let id = &sealed.grant().descriptor().id;
    let tx = db
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .unwrap();
    let store = Objects::new(&tx, NOW).unwrap();
    store.stage(&sealed, EXPIRY).unwrap();
    assert!(store.expect(sealed.grant(), EXPIRY + 1).is_err());
    for (field, value) in [
        ("name", json!("other.png")),
        ("sha256", json!("f".repeat(64))),
    ] {
        let mut changed: Value =
            serde_json::from_slice(&sealed.grant().encode_private().unwrap()).unwrap();
        changed["manifest"][field] = value;
        let changed = FileGrant::decode_private(&serde_json::to_vec(&changed).unwrap()).unwrap();
        assert!(store.expect(&changed, EXPIRY).is_err());
    }
    let mut changed: Value =
        serde_json::from_slice(&sealed.grant().encode_private().unwrap()).unwrap();
    changed["key"][0] = json!((changed["key"][0].as_u64().unwrap() + 1) % 256);
    let changed = FileGrant::decode_private(&serde_json::to_vec(&changed).unwrap()).unwrap();
    assert!(store.expect(&changed, EXPIRY).is_err());
    let mut damaged = sealed.chunks()[0].clone();
    damaged[0] ^= 1;
    assert!(store.accept_chunk(id, 0, &damaged).is_err());
    assert_eq!(store.upload_chunk(id, 0).unwrap(), sealed.chunks()[0]);
    // Disk corruption must not result in returning unauthenticated plaintext.
    tx.execute(
        "UPDATE file_chunks SET ciphertext=?1 WHERE object_id=?2",
        params![damaged, id],
    )
    .unwrap();
    assert!(store.read(id).is_err());
    assert!(store.upload_chunk(id, 0).is_err());
    assert!(store.stage(&sealed, EXPIRY).is_err());
    tx.commit().unwrap();
}

#[test]
fn quotas_reserve_full_objects_and_cleanup_releases_chunks_and_keys() {
    let home = tempfile::tempdir().unwrap();
    let mut db = open(&home.path().join("state.sqlite3"));
    let tx = db
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .unwrap();
    let store = Objects::new(&tx, NOW).unwrap();
    let mut ids = Vec::new();
    for _ in 0..MAX_OBJECTS {
        let sealed = seal("empty", "application/octet-stream", b"").unwrap();
        store.stage(&sealed, EXPIRY).unwrap();
        ids.push(sealed.grant().descriptor().id.clone());
    }
    let one_more = seal("extra", "application/octet-stream", b"x").unwrap();
    assert!(store.stage(&one_more, EXPIRY).is_err());
    assert!(store.delete(&ids[0]).unwrap());
    assert!(!store.delete(&ids[0]).unwrap());
    store.stage(&one_more, EXPIRY).unwrap();
    assert_eq!(count(&tx, "file_objects"), MAX_OBJECTS);
    assert_eq!(count(&tx, "file_chunks"), MAX_OBJECTS);
    assert_eq!(
        Objects::new(&tx, EXPIRY).unwrap().reap().unwrap() as u64,
        MAX_OBJECTS
    );
    assert_eq!(count(&tx, "file_chunks"), 0);
    let store = Objects::new(&tx, NOW).unwrap();
    // Authenticate and reserve declared size before downloading. No need to
    // allocate 256 MiB of real ciphertext to exercise aggregate reservation.
    let mut total = 0;
    loop {
        let sealed = seal("large", "application/octet-stream", b"").unwrap();
        let mut wire: Value =
            serde_json::from_slice(&sealed.grant().encode_private().unwrap()).unwrap();
        wire["manifest"]["byte_length"] = json!(MAX_FILE_BYTES);
        let grant = FileGrant::decode_private(&serde_json::to_vec(&wire).unwrap()).unwrap();
        if total + grant.ciphertext_bytes() > MAX_RESERVED_BYTES {
            assert!(store.expect(&grant, EXPIRY).is_err());
            break;
        }
        store.expect(&grant, EXPIRY).unwrap();
        total += grant.ciphertext_bytes();
    }
    assert_eq!(count(&tx, "file_objects"), 7); // AEAD tags also consume quota.
    assert_eq!(count(&tx, "file_chunks"), 0);
    let expired = Objects::new(&tx, NOW + INCOMPLETE_TTL_SECONDS).unwrap();
    assert_eq!(expired.reap().unwrap(), 7);
    assert_eq!(count(&tx, "file_objects"), 0);
    expired.stage(&one_more, EXPIRY).unwrap();
    tx.commit().unwrap();
}

#[test]
fn expiry_and_abandonment_are_enforced_before_cleanup_and_cannot_be_extended() {
    let home = tempfile::tempdir().unwrap();
    let mut db = open(&home.path().join("state.sqlite3"));
    let a = seal("complete", "application/octet-stream", b"complete").unwrap();
    let b = seal(
        "partial",
        "application/octet-stream",
        &vec![1; CHUNK_BYTES + 1],
    )
    .unwrap();
    let tx = db
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .unwrap();
    let store = Objects::new(&tx, NOW).unwrap();
    for expiry in [0, NOW, NOW + MAX_RETENTION_SECONDS + 1, u64::MAX] {
        assert!(store.expect(a.grant(), expiry).is_err());
    }
    assert!(Objects::new(&tx, u64::MAX).is_err());
    store.stage(&a, EXPIRY).unwrap();
    store.expect(b.grant(), EXPIRY).unwrap();
    let a_id = &a.grant().descriptor().id;
    let b_id = &b.grant().descriptor().id;
    store.accept_chunk(b_id, 0, &b.chunks()[0]).unwrap();
    let later = Objects::new(&tx, NOW + INCOMPLETE_TTL_SECONDS).unwrap();
    assert!(later.status(b_id).is_err());
    assert!(later.grant(b_id).is_err());
    assert!(later.expect(b.grant(), EXPIRY).is_err());
    assert!(later.accept_chunk(b_id, 1, &b.chunks()[1]).is_err());
    assert!(later.read(a_id).is_ok());
    assert_eq!(later.reap().unwrap(), 1);
    assert_eq!(count(&tx, "file_chunks"), 1);
    let expired = Objects::new(&tx, EXPIRY).unwrap();
    assert!(expired.read(a_id).is_err());
    assert!(expired.upload_chunk(a_id, 0).is_err());
    assert!(expired.pending_uploads(a_id).is_err());
    assert!(expired.acknowledge_upload(a_id, 0).is_err());
    assert!(expired.retry_upload(a_id).is_err());
    assert_eq!(expired.reap().unwrap(), 1);
    assert_eq!(count(&tx, "file_objects"), 0);
    assert_eq!(count(&tx, "file_chunks"), 0);
    tx.commit().unwrap();
}

#[test]
fn schema_version_and_foreign_key_requirements_fail_closed() {
    let mut db = Connection::open_in_memory().unwrap();
    db.pragma_update(None, "foreign_keys", false).unwrap();
    assert!(file_objects::initialize(&mut db).is_err());
    db.pragma_update(None, "foreign_keys", true).unwrap();
    file_objects::initialize(&mut db).unwrap();
    db.execute("UPDATE file_object_schema SET version=2", [])
        .unwrap();
    assert!(file_objects::initialize(&mut db).is_err());
    let tx = db
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .unwrap();
    assert!(Objects::new(&tx, NOW).is_err());
}

#[test]
fn concurrent_writers_cannot_overbook_the_remaining_object_slot() {
    let home = tempfile::tempdir().unwrap();
    let path = home.path().join("state.sqlite3");
    let mut db = open(&path);
    {
        let tx = db
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .unwrap();
        let store = Objects::new(&tx, NOW).unwrap();
        for _ in 0..MAX_OBJECTS - 1 {
            store
                .stage(
                    &seal("empty", "application/octet-stream", b"").unwrap(),
                    EXPIRY,
                )
                .unwrap();
        }
        tx.commit().unwrap();
    }
    let barrier = std::sync::Arc::new(std::sync::Barrier::new(2));
    let writers: Vec<_> = (0..2)
        .map(|_| {
            let mut db = open(&path);
            let barrier = barrier.clone();
            std::thread::spawn(move || {
                let sealed = seal("last", "application/octet-stream", b"last slot").unwrap();
                barrier.wait();
                let tx = db
                    .transaction_with_behavior(TransactionBehavior::Immediate)
                    .unwrap();
                if Objects::new(&tx, NOW)
                    .unwrap()
                    .stage(&sealed, EXPIRY)
                    .is_ok()
                {
                    tx.commit().unwrap();
                    true
                } else {
                    false
                }
            })
        })
        .collect();
    let accepted = writers
        .into_iter()
        .map(|w| usize::from(w.join().unwrap()))
        .sum::<usize>();
    assert_eq!(accepted, 1);
    assert_eq!(count(&db, "file_objects"), MAX_OBJECTS);
    assert_eq!(count(&db, "file_chunks"), MAX_OBJECTS);
}

#[test]
fn missing_saved_chunks_cannot_be_mistaken_for_a_completed_upload() {
    let home = tempfile::tempdir().unwrap();
    let mut db = open(&home.path().join("state.sqlite3"));
    let sealed = seal("empty", "application/octet-stream", b"").unwrap();
    let id = &sealed.grant().descriptor().id;
    let tx = db
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .unwrap();
    let store = Objects::new(&tx, NOW).unwrap();
    store.stage(&sealed, EXPIRY).unwrap();
    tx.execute("DELETE FROM file_chunks WHERE object_id=?1", [id])
        .unwrap();
    assert!(store.read(id).is_err());
    assert!(store.pending_uploads(id).is_err());
    assert!(store.upload_chunk(id, 0).is_err());
    assert!(store.acknowledge_upload(id, 0).is_err());
    assert!(store.retry_upload(id).is_err());
    assert!(store.finish(id).is_err());
    store.stage(&sealed, EXPIRY).unwrap();
    assert_eq!(*store.read(id).unwrap(), b"");
    assert_eq!(store.pending_uploads(id).unwrap(), [0]);
    tx.commit().unwrap();
}