cratefield-testing 0.5.1

Conformance kit for Cratefield modules: fake ports, in-memory Database, request helpers
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
//! The `Database` blob round-trip contract (issue #39), asserted against
//! a live adapter rather than trusted from its docs. A database that
//! mangles bound bytes — trimming a NUL, re-encoding through text,
//! padding a base64 hop — cannot hold a ciphertext, a wrapped key or a
//! nonce.
//!
//! The `Blob` large-object contract (issue #586) lives beside it: the
//! streamed reads/writes and multipart uploads an adapter needs to carry
//! an object past [`MAX_BLOB_BYTES`](cratefield_core::MAX_BLOB_BYTES).

use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use bytes::Bytes;
use futures_core::Stream;

use cratefield_core::{
    Blob, BlobError, BoxStream, Database, PartReceipt, ScopedBlob, Statement, StreamError, UploadId,
};

/// Proves `db` round-trips bound bytes exactly: a `Vec<u8>` bound as
/// `Value::Bytes` reads back byte-for-byte identical, and a NULL blob
/// reads back as no bytes. Runs its own probe table, so it can be called
/// against any adapter with a writable connection.
///
/// An adapter that stores blobs as text fails loudly here — the port's
/// answer to "bytes where supported" (issue #39).
///
/// # Panics
///
/// Panics when the blob contract is violated: the bound payload does not
/// read back exactly, a NULL blob reads back as bytes, or the probe
/// table cannot be created on the given connection.
pub async fn assert_blob_round_trips(db: &dyn Database) {
    // One DDL for both engines: Postgres spells the byte column BYTEA and
    // has no BLOB (the portability lint bans the token outright), while
    // SQLite accepts any declared type and stores a bound blob faithfully
    // under it — the same reason auth-core's Postgres migration
    // overrides only ever rename the byte columns.
    db.execute(&Statement::new(
        "CREATE TABLE IF NOT EXISTS blob_round_trip_probe \
         (id INTEGER PRIMARY KEY, payload BYTEA)",
    ))
    .await
    .expect("probe table");

    db.execute(&Statement::new("DELETE FROM blob_round_trip_probe"))
        .await
        .expect("clear probe");

    // Bytes that die in any text-encoding hop: a NUL (truncation bait),
    // 0xFF and 0xFE (invalid UTF-8 in any position), a stray continuation
    // byte 0x80, the broken pair 0xC3 0x28, and a newline (trim bait).
    // Eleven bytes — not a multiple of three, so a base64 round trip
    // would leave padding artefacts behind.
    let payload: Vec<u8> = vec![
        0x00, 0xDE, 0xAD, 0xBE, 0xEF, 0xFF, 0x80, 0xFE, 0xC3, 0x28, 0x0A,
    ];
    db.execute(&Statement::with_values(
        "INSERT INTO blob_round_trip_probe (id, payload) VALUES (?, ?)",
        vec![1_i32.into(), payload.clone().into()],
    ))
    .await
    .expect("bind the byte payload");

    // A NULL blob: `Value::Bytes(None)` binds as SQL NULL. The adapters
    // do not agree on how NULL is reported back — adapter-sqlite
    // flattens every SQL NULL to `Value::String(None)` (its
    // `sqlite_to_sea`), adapter-postgres uses the type-appropriate
    // `Value::Bytes(None)` — so the raw variant is not portable. What
    // every adapter must agree on is the typed read: no bytes came back.
    db.execute(&Statement::with_values(
        "INSERT INTO blob_round_trip_probe (id, payload) VALUES (?, ?)",
        vec![2_i32.into(), None::<Vec<u8>>.into()],
    ))
    .await
    .expect("bind a NULL blob");

    let stored = db
        .query(&Statement::with_values(
            "SELECT payload FROM blob_round_trip_probe WHERE id = ?",
            vec![1_i32.into()],
        ))
        .await
        .expect("read the payload back");
    let row = stored.first().expect("the payload row is visible");
    assert_eq!(
        row.get::<Vec<u8>>("payload"),
        Some(payload),
        "the bytes read back are exactly the bytes bound"
    );

    let nulled = db
        .query(&Statement::with_values(
            "SELECT payload FROM blob_round_trip_probe WHERE id = ?",
            vec![2_i32.into()],
        ))
        .await
        .expect("read the NULL blob back");
    let row = nulled.first().expect("the NULL row is visible");
    assert!(
        row.get::<Vec<u8>>("payload").is_none(),
        "a NULL blob reads back as no bytes, not as empty or garbage"
    );

    db.execute(&Statement::new("DELETE FROM blob_round_trip_probe"))
        .await
        .expect("clean probe");
}

/// One MiB, the unit the large-object contract is written in.
const MIB: usize = 1024 * 1024;

/// The chunk size the contract streams a body in: small enough that the
/// 12 MiB probe arrives in many chunks, not one.
const CHUNK: usize = 64 * 1024;

/// The declared ceiling the contract gives its [`ScopedBlob`]: 16 MiB,
/// above the 12 MiB probe and below the 20 MiB refusal probe.
const CEILING: u64 = 16 * 1024 * 1024;

/// The content type the probe objects are stored with.
const CONTENT_TYPE: &str = "application/octet-stream";

/// Proves `blob` satisfies the large-object half of the [`Blob`] port
/// (issue #586): streamed puts and gets for an object past
/// [`MAX_BLOB_BYTES`](cratefield_core::MAX_BLOB_BYTES), the declared
/// ceiling enforced mid-stream, multipart uploads that assemble in part
/// order, abort leaving nothing behind, the bad-key rules, and a listing
/// that never surfaces another module's keys.
///
/// Wraps `blob` in a [`ScopedBlob`] for module `large_probe` with a 16 MiB
/// declared ceiling, so the probe also exercises the scoping and the
/// ceiling the harness applies. Objects it creates are removed before it
/// returns; run it against a store a test can leave empty, or accept that
/// it writes under `large_probe/` and `other_probe/`.
///
/// # Panics
///
/// Panics with a message naming the violated expectation when the store
/// does not honour the contract.
pub async fn assert_blob_large_round_trips(blob: Arc<dyn Blob>) {
    let scoped = ScopedBlob::new(Arc::clone(&blob), "large_probe").with_max_object_bytes(CEILING);

    check_streamed_round_trip(&scoped).await;
    check_ceiling_refusal(&scoped).await;
    check_multipart(&scoped).await;
    check_complete_ceiling(&scoped).await;
    check_abort(&scoped).await;
    check_bad_keys(&scoped).await;
    check_listing(&blob).await;

    for key in ["clip.bin", "movie.bin", "huge.bin"] {
        scoped
            .delete(key)
            .await
            .expect("the contract removes the objects it created");
    }
}

/// A streamed object larger than `MAX_BLOB_BYTES` round-trips exactly, and
/// `head` reports its size and content type without reading the body.
async fn check_streamed_round_trip(scoped: &ScopedBlob) {
    let payload = pattern(12 * MIB, 7);
    let written = scoped
        .put_stream(
            "clip.bin",
            body_stream(&payload, CHUNK),
            CONTENT_TYPE,
            (12 * MIB) as u64,
        )
        .await
        .expect("a 12 MiB streamed put is under the 16 MiB declared ceiling");
    assert_eq!(
        written,
        payload.len() as u64,
        "put_stream reports the bytes it wrote"
    );

    let stream = scoped
        .get_stream("clip.bin")
        .await
        .expect("get_stream succeeds")
        .expect("the object is present");
    assert_eq!(
        stream.size,
        payload.len() as u64,
        "the streamed size matches"
    );
    assert_eq!(
        stream.content_type, CONTENT_TYPE,
        "the streamed content type matches"
    );
    assert_eq!(
        read_all(stream.body).await,
        payload,
        "the streamed bytes round trip exactly"
    );

    let meta = scoped
        .head("clip.bin")
        .await
        .expect("head succeeds")
        .expect("the object is present");
    assert_eq!(meta.key, "clip.bin", "head reports the module-relative key");
    assert_eq!(meta.size, payload.len() as u64, "head reports the size");
    assert_eq!(
        meta.content_type, CONTENT_TYPE,
        "head reports the content type"
    );
}

/// A put past the declared ceiling is refused mid-stream and leaves no
/// object; so is one past the caller's own `max_bytes`.
async fn check_ceiling_refusal(scoped: &ScopedBlob) {
    let oversized = pattern(20 * MIB, 1);
    let err = scoped
        .put_stream(
            "too-big.bin",
            body_stream(&oversized, CHUNK),
            CONTENT_TYPE,
            (20 * MIB) as u64,
        )
        .await
        .expect_err("20 MiB is over the 16 MiB declared ceiling");
    assert!(
        matches!(err, BlobError::TooLarge(_)),
        "the ceiling breach is TooLarge, got {err:?}"
    );
    assert!(
        scoped.head("too-big.bin").await.expect("head").is_none(),
        "a refused streamed put leaves no object behind"
    );

    let payload = pattern(12 * MIB, 2);
    let err = scoped
        .put_stream(
            "capped.bin",
            body_stream(&payload, CHUNK),
            CONTENT_TYPE,
            1024,
        )
        .await
        .expect_err("a raw max_bytes below the stream is refused");
    assert!(
        matches!(err, BlobError::TooLarge(_)),
        "the raw cap is TooLarge, got {err:?}"
    );
    assert!(
        scoped.head("capped.bin").await.expect("head").is_none(),
        "the raw cap leaves no object behind"
    );
}

/// Parts uploaded out of order assemble in part-number order, min part
/// size included.
async fn check_multipart(scoped: &ScopedBlob) {
    let part1 = pattern(5 * MIB, 11);
    let part2 = pattern(5 * MIB, 22);
    let part3 = pattern(MIB, 33);

    let upload = scoped
        .create_multipart("movie.bin", "video/mp4")
        .await
        .expect("create_multipart");
    let receipt2 = scoped
        .upload_part(
            "movie.bin",
            &upload,
            2,
            body_stream(&part2, CHUNK),
            (5 * MIB) as u64,
        )
        .await
        .expect("part 2");
    let receipt1 = scoped
        .upload_part(
            "movie.bin",
            &upload,
            1,
            body_stream(&part1, CHUNK),
            (5 * MIB) as u64,
        )
        .await
        .expect("part 1");
    let receipt3 = scoped
        .upload_part(
            "movie.bin",
            &upload,
            3,
            body_stream(&part3, CHUNK),
            (5 * MIB) as u64,
        )
        .await
        .expect("part 3");
    assert_eq!(
        (
            receipt1.part_number,
            receipt2.part_number,
            receipt3.part_number
        ),
        (1, 2, 3),
        "each receipt carries the part number it was uploaded as"
    );

    scoped
        .complete_multipart("movie.bin", &upload, &[receipt1, receipt2, receipt3])
        .await
        .expect("complete_multipart");

    let mut expected = part1.clone();
    expected.extend_from_slice(&part2);
    expected.extend_from_slice(&part3);
    let stream = scoped
        .get_stream("movie.bin")
        .await
        .expect("get_stream")
        .expect("the assembled object is present");
    assert_eq!(
        read_all(stream.body).await,
        expected,
        "parts assemble in part-number order"
    );
}

/// A completed object larger than the declared ceiling is refused and
/// removed, even though every part was itself under the ceiling — the
/// assembled size is only known at completion.
async fn check_complete_ceiling(scoped: &ScopedBlob) {
    let upload = scoped
        .create_multipart("huge.bin", CONTENT_TYPE)
        .await
        .expect("create_multipart");
    let mut receipts = Vec::new();
    for number in 1..=4u16 {
        let part = pattern(
            5 * MIB,
            u8::try_from(number).expect("a part number below 5 fits a byte"),
        );
        let receipt = scoped
            .upload_part(
                "huge.bin",
                &upload,
                number,
                body_stream(&part, CHUNK),
                (5 * MIB) as u64,
            )
            .await
            .expect("a 5 MiB part is under the 16 MiB ceiling");
        receipts.push(receipt);
    }
    let err = scoped
        .complete_multipart("huge.bin", &upload, &receipts)
        .await
        .expect_err("four 5 MiB parts assemble to 20 MiB, over the 16 MiB ceiling");
    assert!(
        matches!(err, BlobError::TooLarge(_)),
        "the assembled-object breach is TooLarge, got {err:?}"
    );
    assert!(
        scoped.head("huge.bin").await.expect("head").is_none(),
        "a refused completion leaves no object behind"
    );
}

/// Aborting an upload leaves no object and no pending upload.
async fn check_abort(scoped: &ScopedBlob) {
    let upload = scoped
        .create_multipart("draft.bin", CONTENT_TYPE)
        .await
        .expect("create_multipart");
    scoped
        .upload_part("draft.bin", &upload, 1, body_stream(b"partial", 4), CEILING)
        .await
        .expect("upload a part");
    scoped
        .abort_multipart("draft.bin", &upload)
        .await
        .expect("abort_multipart");

    assert!(
        scoped.head("draft.bin").await.expect("head").is_none(),
        "abort leaves no object"
    );
    match scoped.list_multipart_uploads("").await {
        Ok(uploads) => assert!(
            !uploads.iter().any(|pending| pending.upload_id == upload),
            "the aborted upload is gone"
        ),
        // A store without multipart listing is acceptable for this one call.
        Err(BlobError::Unsupported(_)) => {}
        Err(err) => panic!("list_multipart_uploads failed: {err:?}"),
    }
    let page = scoped.list("", None, 100).await.expect("list");
    assert!(
        !page.objects.iter().any(|meta| meta.key == "draft.bin"),
        "abort leaves no listed object"
    );
}

/// Every new method refuses an escaping key; a listing refuses an escaping
/// prefix but allows the empty one.
async fn check_bad_keys(scoped: &ScopedBlob) {
    let receipt = PartReceipt {
        part_number: 1,
        etag: "irrelevant".to_owned(),
    };
    let upload = UploadId::new("missing");
    for bad in ["", "/abs", "../escape"] {
        assert!(
            matches!(
                scoped
                    .put_stream(bad, body_stream(b"x", 1), "text/plain", 16)
                    .await
                    .unwrap_err(),
                BlobError::BadKey(_)
            ),
            "put_stream must refuse `{bad}`"
        );
        assert!(
            matches!(
                scoped.get_stream(bad).await.unwrap_err(),
                BlobError::BadKey(_)
            ),
            "get_stream must refuse `{bad}`"
        );
        assert!(
            matches!(scoped.head(bad).await.unwrap_err(), BlobError::BadKey(_)),
            "head must refuse `{bad}`"
        );
        assert!(
            matches!(
                scoped
                    .create_multipart(bad, "text/plain")
                    .await
                    .unwrap_err(),
                BlobError::BadKey(_)
            ),
            "create_multipart must refuse `{bad}`"
        );
        assert!(
            matches!(
                scoped
                    .upload_part(bad, &upload, 1, body_stream(b"x", 1), 16)
                    .await
                    .unwrap_err(),
                BlobError::BadKey(_)
            ),
            "upload_part must refuse `{bad}`"
        );
        assert!(
            matches!(
                scoped
                    .complete_multipart(bad, &upload, std::slice::from_ref(&receipt))
                    .await
                    .unwrap_err(),
                BlobError::BadKey(_)
            ),
            "complete_multipart must refuse `{bad}`"
        );
        assert!(
            matches!(
                scoped.abort_multipart(bad, &upload).await.unwrap_err(),
                BlobError::BadKey(_)
            ),
            "abort_multipart must refuse `{bad}`"
        );
    }
    for bad in ["/abs", "../escape"] {
        assert!(
            matches!(
                scoped.list(bad, None, 10).await.unwrap_err(),
                BlobError::BadKey(_)
            ),
            "list must refuse prefix `{bad}`"
        );
        assert!(
            matches!(
                scoped.list_multipart_uploads(bad).await.unwrap_err(),
                BlobError::BadKey(_)
            ),
            "list_multipart_uploads must refuse prefix `{bad}`"
        );
    }
}

/// A listing returns only the module's keys, stripped and in order,
/// paginates, and never surfaces another module's object.
async fn check_listing(blob: &Arc<dyn Blob>) {
    let scoped = ScopedBlob::new(Arc::clone(blob), "large_probe");
    let other = ScopedBlob::new(Arc::clone(blob), "other_probe");
    for name in ["list/a.txt", "list/b.txt", "list/c.txt"] {
        scoped
            .put(name, b"x", "text/plain")
            .await
            .expect("seed a listed object");
    }
    other
        .put("list/other.txt", b"x", "text/plain")
        .await
        .expect("seed another module's object");

    let other_page = other.list("list/", None, 100).await.expect("other list");
    assert!(
        other_page
            .objects
            .iter()
            .any(|meta| meta.key == "list/other.txt"),
        "the other module sees its own object"
    );

    let first = scoped.list("list/", None, 2).await.expect("first page");
    let keys: Vec<&str> = first.objects.iter().map(|meta| meta.key.as_str()).collect();
    assert_eq!(
        keys,
        ["list/a.txt", "list/b.txt"],
        "list strips the module prefix and orders keys"
    );
    assert!(first.cursor.is_some(), "more pages remain");

    let second = scoped
        .list("list/", first.cursor.as_deref(), 2)
        .await
        .expect("second page");
    let keys: Vec<&str> = second
        .objects
        .iter()
        .map(|meta| meta.key.as_str())
        .collect();
    assert_eq!(
        keys,
        ["list/c.txt"],
        "the second page continues past the cursor"
    );
    assert!(second.cursor.is_none(), "the last page has no cursor");

    let all = scoped.list("", None, 1000).await.expect("all keys");
    assert!(
        all.objects
            .iter()
            .all(|meta| !meta.key.starts_with("other_probe")),
        "another module's keys never surface"
    );
    assert!(
        all.objects
            .iter()
            .all(|meta| !meta.key.starts_with("large_probe")),
        "returned keys are module-relative"
    );

    scoped.delete("list/a.txt").await.expect("cleanup a");
    scoped.delete("list/b.txt").await.expect("cleanup b");
    scoped.delete("list/c.txt").await.expect("cleanup c");
    other.delete("list/other.txt").await.expect("cleanup other");
}

/// A deterministic probe body: byte `i` is `(i + seed) % 251`, so two
/// different seeds never produce the same bytes and a wrong reassembly is
/// visible.
fn pattern(len: usize, seed: u8) -> Vec<u8> {
    (0..len)
        .map(|i| {
            u8::try_from((i + usize::from(seed)) % 251).expect("a value below 251 fits a byte")
        })
        .collect()
}

/// Type-erases `bytes` into a body stream of `chunk`-sized chunks.
fn body_stream(bytes: &[u8], chunk: usize) -> BoxStream<'static, Result<Bytes, StreamError>> {
    let chunks: Vec<Result<Bytes, StreamError>> = bytes
        .chunks(chunk)
        .map(|piece| Ok(Bytes::copy_from_slice(piece)))
        .collect();
    Box::pin(Chunks {
        chunks: chunks.into_iter(),
    })
}

/// Reads a body stream to the end, panicking on a mid-stream error.
async fn read_all(mut body: BoxStream<'static, Result<Bytes, StreamError>>) -> Vec<u8> {
    let mut out = Vec::new();
    while let Some(chunk) = std::future::poll_fn(|cx| body.as_mut().poll_next(cx)).await {
        out.extend_from_slice(&chunk.expect("the body stream did not fail"));
    }
    out
}

/// A `Stream` over pre-built chunks. Written out because the kit has no
/// `futures-util`.
struct Chunks {
    chunks: std::vec::IntoIter<Result<Bytes, StreamError>>,
}

impl Stream for Chunks {
    type Item = Result<Bytes, StreamError>;

    fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Poll::Ready(self.get_mut().chunks.next())
    }
}