opys-bundle 0.6.0

The opys bundle: a manifest and the blobs it names, as one zip.
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
//! The bundle: one file that is a manifest and the blobs it names.

use std::io::{Cursor, Read, Write};

use opys_bundle::{
    blob_id, blob_id_of, head_from_front, open_bundle, read_bundle_head, write_bundle, BlobSource,
    Blobs, BundleError, Front, Head, BUNDLE_FORMAT,
};
use opys_core::Manifest;
use serde_json::json;

const HELLO: &str = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";

fn manifest(artifacts: serde_json::Value) -> Manifest {
    serde_json::from_value(json!({
        "vars": { "root": "/srv/game" },
        "launch": { "command": "java", "workdir": "${root}", "args": ["-jar", "server.jar"] },
        "cleanup": [{ "includes": ["${root}/mods/**"] }],
        "artifacts": artifacts,
    }))
    .unwrap()
}

fn sample() -> (Manifest, Blobs) {
    let other = blob_id(b"world");
    let manifest = manifest(json!([
        { "path": "${root}/a.jar", "source": { "url": "https://example.test/a.jar" }, "integrity": { "sha1": "0".repeat(40) } },
        { "path": "${root}/hello.txt", "source": { "blob": HELLO }, "size": 5 },
        { "path": "${root}/world.txt", "source": { "blob": other }, "size": 5, "rules": "allow.os.linux" },
    ]));
    let blobs = Blobs::from([
        (HELLO.to_owned(), BlobSource::Bytes(b"hello".to_vec())),
        (other, BlobSource::Bytes(b"world".to_vec())),
    ]);
    (manifest, blobs)
}

fn written(manifest: &Manifest, blobs: &Blobs) -> Vec<u8> {
    let mut out = Cursor::new(Vec::new());
    write_bundle(&mut out, &Head::default(), manifest, blobs).unwrap();
    out.into_inner()
}

/// The raw entries of a zip, in the order they were written.
fn entries(bytes: &[u8]) -> Vec<(String, Vec<u8>, zip::CompressionMethod)> {
    let mut archive = zip::ZipArchive::new(Cursor::new(bytes)).unwrap();
    (0..archive.len())
        .map(|i| {
            let mut entry = archive.by_index(i).unwrap();
            let mut body = Vec::new();
            entry.read_to_end(&mut body).unwrap();
            (entry.name().to_owned(), body, entry.compression())
        })
        .collect()
}

/// A zip with exactly these entries — a bundle some other writer made.
fn zip_of(files: &[(&str, &[u8])]) -> Vec<u8> {
    let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
    for (name, body) in files {
        zip.start_file(*name, zip::write::SimpleFileOptions::default())
            .unwrap();
        zip.write_all(body).unwrap();
    }
    zip.finish().unwrap().into_inner()
}

fn open_error(bytes: Vec<u8>) -> BundleError {
    match open_bundle(Cursor::new(bytes)) {
        Ok(_) => panic!("the bundle opened"),
        Err(error) => error,
    }
}

// ── round trip ────────────────────────────────────────────────────────────

#[test]
fn a_bundle_reads_back_as_the_manifest_it_was_written_from() {
    let (manifest, blobs) = sample();
    let bundle = open_bundle(Cursor::new(written(&manifest, &blobs))).unwrap();
    assert_eq!(bundle.manifest(), &manifest);
    assert_eq!(bundle.into_manifest(), manifest);
}

#[test]
fn a_blob_comes_back_out_byte_for_byte() {
    let (manifest, blobs) = sample();
    let mut bundle = open_bundle(Cursor::new(written(&manifest, &blobs))).unwrap();
    let mut out = Vec::new();
    assert_eq!(bundle.copy_blob(HELLO, &mut out).unwrap(), 5);
    assert_eq!(out, b"hello");
    assert!(matches!(
        bundle.copy_blob(&"0".repeat(64), &mut Vec::new()),
        Err(BundleError::MissingBlob(_))
    ));
}

#[test]
fn a_blob_is_read_from_a_file_as_well_as_from_memory() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("hello.txt");
    std::fs::write(&path, b"hello").unwrap();
    let manifest = manifest(json!([{ "path": "a", "source": { "blob": HELLO } }]));
    let from_file = written(
        &manifest,
        &Blobs::from([(HELLO.to_owned(), BlobSource::File(path))]),
    );
    let from_memory = written(
        &manifest,
        &Blobs::from([(HELLO.to_owned(), BlobSource::Bytes(b"hello".to_vec()))]),
    );
    // Where a blob was kept leaves no trace in the bundle.
    assert_eq!(from_file, from_memory);
}

// ── layout ────────────────────────────────────────────────────────────────

#[test]
fn both_json_entries_are_written_to_be_read() {
    let (manifest, blobs) = sample();
    let all = entries(&written(&manifest, &blobs));
    for (name, body, _) in all.iter().filter(|(name, ..)| name.ends_with(".json")) {
        // One field to a line, indented: what `unzip -p` shows is readable.
        let text = std::str::from_utf8(body).unwrap();
        assert!(text.starts_with("{\n  \""), "{name}: {text}");
    }
}

#[test]
fn the_head_is_the_first_entry_and_is_stored_so_it_can_be_read_off_the_front() {
    let (manifest, blobs) = sample();
    let bytes = written(&manifest, &blobs);
    let all = entries(&bytes);
    let names: Vec<&str> = all.iter().map(|(name, ..)| name.as_str()).collect();
    let world = blob_id(b"world");
    let mut blob_names = [format!("blobs/{HELLO}"), format!("blobs/{world}")];
    blob_names.sort();
    assert_eq!(
        names,
        ["opys.json", "manifest.json", &blob_names[0], &blob_names[1]]
    );

    let (_, head, method) = &all[0];
    assert_eq!(*method, zip::CompressionMethod::Stored);
    // Stored means the head's own bytes are in the file as they are.
    let at = bytes
        .windows(head.len())
        .position(|w| w == &head[..])
        .unwrap();
    assert!(at < 100, "the head starts {at} bytes in");
}

#[test]
fn the_head_carries_the_format_and_the_manifest_is_whole_beside_it() {
    let (manifest, blobs) = sample();
    let all = entries(&written(&manifest, &blobs));
    let head: serde_json::Value = serde_json::from_slice(&all[0].1).unwrap();
    assert_eq!(head, json!({ "format": BUNDLE_FORMAT }));
    // Exactly what `opys-core` writes for a manifest: the container adds no
    // spelling of its own, and nothing of the manifest is in the head.
    let body: serde_json::Value = serde_json::from_slice(&all[1].1).unwrap();
    assert_eq!(body, serde_json::to_value(&manifest).unwrap());
    assert_eq!(
        opys_core::parse_manifest(std::str::from_utf8(&all[1].1).unwrap()).unwrap(),
        manifest
    );
}

#[test]
fn reading_the_head_does_not_need_the_manifest_to_be_readable() {
    let (manifest, blobs) = sample();
    let head = read_bundle_head(Cursor::new(written(&manifest, &blobs))).unwrap();
    assert_eq!(head, Head::default());
    assert_eq!(head.format, BUNDLE_FORMAT);

    // A bundle whose manifest is garbage still tells you what it is.
    let head_json = serde_json::to_vec(&head).unwrap();
    let broken = zip_of(&[("opys.json", &head_json), ("manifest.json", b"not json")]);
    assert_eq!(
        read_bundle_head(Cursor::new(broken.clone())).unwrap(),
        Head::default()
    );
    assert!(matches!(
        open_error(broken),
        BundleError::Json {
            entry: "manifest.json",
            ..
        }
    ));
}

#[test]
fn a_head_may_say_more_than_this_reader_asks_of_it() {
    // Metadata about the bundle is the head's to grow; the format is what
    // decides whether the manifest beside it can be read.
    let head = serde_json::to_vec(&json!({ "format": BUNDLE_FORMAT, "name": "my-pack" })).unwrap();
    let bundle = zip_of(&[("opys.json", &head), ("manifest.json", b"{}")]);
    assert_eq!(
        read_bundle_head(Cursor::new(bundle.clone())).unwrap(),
        Head::default()
    );
    assert_eq!(
        open_bundle(Cursor::new(bundle)).unwrap().into_manifest(),
        Manifest::default()
    );
}

#[test]
fn two_builds_of_the_same_manifest_are_the_same_bytes() {
    let (manifest, blobs) = sample();
    assert_eq!(written(&manifest, &blobs), written(&manifest, &blobs));
}

#[test]
fn a_blob_two_artifacts_share_is_written_once() {
    let manifest = manifest(json!([
        { "path": "a", "source": { "blob": HELLO } },
        { "path": "b", "source": { "blob": HELLO } },
    ]));
    let blobs = Blobs::from([(HELLO.to_owned(), BlobSource::Bytes(b"hello".to_vec()))]);
    assert_eq!(entries(&written(&manifest, &blobs)).len(), 3);
}

#[test]
fn a_blob_no_artifact_names_is_left_out() {
    // A later plugin replaced the artifact; its blob is still in the table.
    let (manifest, mut blobs) = sample();
    blobs.insert(blob_id(b"orphan"), BlobSource::Bytes(b"orphan".to_vec()));
    assert_eq!(entries(&written(&manifest, &blobs)).len(), 4);
}

#[test]
fn a_manifest_with_no_blobs_is_a_bundle_with_no_blobs() {
    let manifest =
        manifest(json!([{ "path": "a", "source": { "url": "https://example.test/a" } }]));
    let bytes = written(&manifest, &Blobs::new());
    assert_eq!(entries(&bytes).len(), 2);
    assert_eq!(
        open_bundle(Cursor::new(bytes)).unwrap().into_manifest(),
        manifest
    );
}

// ── what a writer refuses ─────────────────────────────────────────────────

#[test]
fn a_blob_the_table_does_not_hold_is_named() {
    let (manifest, _) = sample();
    let error = write_bundle(
        Cursor::new(Vec::new()),
        &Head::default(),
        &manifest,
        &Blobs::new(),
    )
    .unwrap_err();
    assert!(
        matches!(&error, BundleError::MissingBlob(id) if id == HELLO),
        "{error}"
    );
}

#[test]
fn a_blob_that_is_not_what_its_name_says_is_refused() {
    let manifest = manifest(json!([{ "path": "a", "source": { "blob": HELLO } }]));
    let blobs = Blobs::from([(HELLO.to_owned(), BlobSource::Bytes(b"goodbye".to_vec()))]);
    let error =
        write_bundle(Cursor::new(Vec::new()), &Head::default(), &manifest, &blobs).unwrap_err();
    let BundleError::BlobMismatch { id, found } = &error else {
        panic!("{error}");
    };
    assert_eq!(
        (id.as_str(), found.as_str()),
        (HELLO, blob_id(b"goodbye").as_str())
    );
}

#[test]
fn a_blob_file_that_is_gone_is_named_with_its_path() {
    let manifest = manifest(json!([{ "path": "a", "source": { "blob": HELLO } }]));
    let blobs = Blobs::from([(
        HELLO.to_owned(),
        BlobSource::File("/nonexistent/hello.txt".into()),
    )]);
    let message = write_bundle(Cursor::new(Vec::new()), &Head::default(), &manifest, &blobs)
        .unwrap_err()
        .to_string();
    assert!(message.contains("/nonexistent/hello.txt"), "{message}");
}

// ── what a reader refuses ─────────────────────────────────────────────────

#[test]
fn something_that_is_not_a_zip_is_not_a_bundle() {
    assert!(matches!(
        open_error(b"{ \"vars\": {} }".to_vec()),
        BundleError::Zip(_)
    ));
    assert!(read_bundle_head(Cursor::new(b"nope".to_vec())).is_err());
}

#[test]
fn a_zip_without_a_head_or_without_a_manifest_is_not_a_bundle() {
    let error = open_error(zip_of(&[("readme.txt", b"hi")]));
    assert!(
        matches!(error, BundleError::MissingEntry("opys.json")),
        "{error}"
    );
    let head = serde_json::to_vec(&json!({ "format": BUNDLE_FORMAT })).unwrap();
    let error = open_error(zip_of(&[("opys.json", &head)]));
    assert!(
        matches!(error, BundleError::MissingEntry("manifest.json")),
        "{error}"
    );
}

#[test]
fn a_format_this_reader_does_not_know_is_refused_before_anything_else_is_read() {
    // The rest of this head would not parse — and that is not what is reported.
    let newer = u64::from(BUNDLE_FORMAT) + 1;
    let head = serde_json::to_vec(&json!({ "format": newer })).unwrap();
    let bundle = zip_of(&[
        ("opys.json", &head),
        ("manifest.json", b"spelled some new way"),
    ]);
    assert!(matches!(
        open_error(bundle.clone()),
        BundleError::Format { found } if found == newer
    ));
    assert!(matches!(
        read_bundle_head(Cursor::new(bundle)),
        Err(BundleError::Format { found }) if found == newer
    ));
}

#[test]
fn a_lower_format_is_refused_like_a_higher_one() {
    // Nothing is migrated or read leniently, whichever way the number is off.
    let head = serde_json::to_vec(&json!({ "format": 0 })).unwrap();
    let bundle = zip_of(&[("opys.json", &head), ("manifest.json", b"{}")]);
    assert!(matches!(
        open_error(bundle.clone()),
        BundleError::Format { found: 0 }
    ));
    assert!(matches!(
        read_bundle_head(Cursor::new(bundle)),
        Err(BundleError::Format { found: 0 })
    ));
}

#[test]
fn a_head_without_a_format_is_refused() {
    let head = serde_json::to_vec(&json!({ "name": "my-pack" })).unwrap();
    let error = open_error(zip_of(&[("opys.json", &head), ("manifest.json", b"{}")]));
    assert!(
        matches!(
            error,
            BundleError::Json {
                entry: "opys.json",
                ..
            }
        ),
        "{error}"
    );
}

#[test]
fn a_bundle_missing_a_blob_its_manifest_names_does_not_open() {
    let head = serde_json::to_vec(&json!({ "format": BUNDLE_FORMAT })).unwrap();
    let body = serde_json::to_vec(&json!({
        "artifacts": [{ "path": "a", "source": { "blob": HELLO } }],
    }))
    .unwrap();
    let error = open_error(zip_of(&[("opys.json", &head), ("manifest.json", &body)]));
    assert!(
        matches!(&error, BundleError::MissingBlob(id) if id == HELLO),
        "{error}"
    );
}

// ── the blob table ────────────────────────────────────────────────────────

#[test]
fn a_blob_id_is_the_sha256_of_the_bytes_however_they_are_read() {
    assert_eq!(blob_id(b"hello"), HELLO);
    assert_eq!(blob_id_of(&b"hello"[..]).unwrap(), (HELLO.to_owned(), 5));
}

#[test]
fn a_blob_source_crosses_as_a_path_or_as_base64() {
    let file: BlobSource = serde_json::from_value(json!({ "file": "/srv/a.jar" })).unwrap();
    assert_eq!(file, BlobSource::File("/srv/a.jar".into()));
    let bytes: BlobSource = serde_json::from_value(json!({ "bytes": "aGVsbG8=" })).unwrap();
    assert_eq!(bytes, BlobSource::Bytes(b"hello".to_vec()));
    assert_eq!(
        serde_json::to_value(&bytes).unwrap(),
        json!({ "bytes": "aGVsbG8=" })
    );
    assert_eq!(
        serde_json::to_value(&file).unwrap(),
        json!({ "file": "/srv/a.jar" })
    );
    assert!(serde_json::from_value::<BlobSource>(json!({ "bytes": "not base64!" })).is_err());
    let wrong = serde_json::from_value::<BlobSource>(json!({ "url": "https://x" })).unwrap_err();
    assert!(wrong.to_string().contains("unknown field `url`"), "{wrong}");
    for neither_or_both in [json!({}), json!({ "file": "/a", "bytes": "aGVsbG8=" })] {
        let wrong = serde_json::from_value::<BlobSource>(neither_or_both).unwrap_err();
        assert!(
            wrong.to_string().contains("`{ file }` or as `{ bytes }`"),
            "{wrong}"
        );
    }
}

// ── the head, off the front ───────────────────────────────────────────────

fn with_options() -> Head {
    serde_json::from_value(json!({
        "format": BUNDLE_FORMAT,
        "options": [{ "feature": "fullscreen", "title": "Fullscreen" }],
    }))
    .unwrap()
}

fn written_under(head: &Head) -> Vec<u8> {
    let (manifest, blobs) = sample();
    let mut out = Cursor::new(Vec::new());
    write_bundle(&mut out, head, &manifest, &blobs).unwrap();
    out.into_inner()
}

#[test]
fn the_head_is_read_off_the_front_as_it_is_from_the_directory() {
    let head = with_options();
    let bytes = written_under(&head);
    assert_eq!(head_from_front(&bytes).unwrap(), Front::Head(head.clone()));
    assert_eq!(read_bundle_head(Cursor::new(&bytes)).unwrap(), head);
}

#[test]
fn a_front_that_stops_short_says_how_much_the_head_takes() {
    let head = with_options();
    let bytes = written_under(&head);
    // Whatever was given, the answer leads to the head in at most two more
    // askings: the header, the name, then the head itself.
    for given in [0, 10, 30, 35, 60] {
        let mut front = &bytes[..given];
        let mut asked = 0;
        let read = loop {
            match head_from_front(front).unwrap() {
                Front::Head(read) => break read,
                Front::Needs(needs) => {
                    assert!(needs as usize > front.len(), "given {given}");
                    front = &bytes[..needs as usize];
                    asked += 1;
                }
            }
        };
        assert_eq!(read, head);
        assert!(asked <= 3, "given {given}: asked {asked} times");
    }
}

#[test]
fn a_front_that_is_not_a_bundles_is_refused() {
    let refusal = |bytes: &[u8]| head_from_front(bytes).unwrap_err().to_string();

    assert!(refusal(&[b'<'; 64]).contains("not a bundle"));
    // A zip, and its first entry is something else.
    let other = zip_of(&[("readme.txt", b"hi"), ("opys.json", b"{}")]);
    assert!(refusal(&other).contains("it has no `opys.json`"));
    // A head some other writer compressed is not where this looks for it.
    let deflated = zip_of(&[("opys.json", br#"{"format":1}"#)]);
    assert!(refusal(&deflated).contains("its head is not stored"));
}

#[test]
fn a_head_in_another_format_is_refused_off_the_front_too() {
    let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
    let stored =
        zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
    zip.start_file("opys.json", stored).unwrap();
    zip.write_all(br#"{"format":99}"#).unwrap();
    let future = zip.finish().unwrap().into_inner();
    assert!(matches!(
        head_from_front(&future),
        Err(BundleError::Format { found: 99 })
    ));
}