onelf-format 0.6.1

Binary format definitions for onelf
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! The plugin directory record, `.onelf/plugin-dirs`.
//!
//! A package carries plugin directories, whether a descriptor's group
//! carries them or inference does. A bundled library usually compiles in
//! the absolute path of the directory it was built beside, and the host
//! keeps its own copy of the same directory, possibly under a different
//! library directory on a multiarch distribution. The runtime reads this
//! record to know which directories those are, so it can consider the
//! host's modules in their counterparts.
//!
//! Each line names one carried directory, as
//! `RELATIVE<tab>LIB_RELATIVE[<tab>ABSOLUTE]*`. The relative side is the
//! directory's package path. The library side is the directory's path
//! under a host library directory, such as `alsa-lib` for
//! `/usr/lib/alsa-lib`, and is empty when the package's layout names no
//! such counterpart. Each absolute side is a path an object was built to
//! open the directory by; a described group's is usually unknown and the
//! field is then absent.
//!
//! Lines are separated by a single `\n` and the record carries no
//! trailing newline, one directory per line. The packer writes them
//! sorted by the relative side, each once.
//!
//! The record is parsed strictly and within named bounds, like the
//! anchors and paths records. Every named path is normalized: no `.` or
//! `..` component, no empty component and no control character (C0, DEL
//! or C1), NUL included. A relative side stays inside the package, a
//! library side stays relative, and an absolute side names a directory
//! below the root. Anything else is refused as a whole, because a record
//! the packer would not have written is damage rather than an
//! instruction.
//!
//! [`encode`] applies the same rules as [`decode`], so the packer
//! refuses at build time every record a runtime would refuse at launch,
//! and every accepted record re-encodes to the same bytes.

use std::collections::BTreeSet;
use std::fmt;

use crate::paths::{MAX_RELATIVE_BYTES, PathsErrorKind, absolute_error, excerpt, relative_error};

/// Where a package carries the record.
pub const PLUGIN_DIRS_FILE: &str = ".onelf/plugin-dirs";
/// The most directories one record holds.
pub const MAX_DIRS: usize = 256;
/// The largest complete record, in bytes, checked before anything is
/// parsed or allocated.
pub const MAX_RECORD_BYTES: usize = 64 * 1024;

/// One carried plugin directory: its package path, its path under a
/// host library directory when one is known, and every compiled-in
/// path an object names it by.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PluginDir {
    /// The directory's package path, relative to the package root.
    pub relative: String,
    /// The directory's path under a host library directory, such as
    /// `alsa-lib` for `/usr/lib/alsa-lib`, or empty when the package's
    /// layout names no such counterpart.
    pub lib_relative: String,
    /// Every compiled-in path an object opens the directory by.
    pub absolutes: Vec<String>,
}

/// Why a record or a directory was refused.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PluginDirsErrorKind {
    /// The record is over [`MAX_RECORD_BYTES`].
    RecordTooLarge,
    /// The record holds more than [`MAX_DIRS`] directories.
    TooManyDirs,
    /// The record is not UTF-8.
    NotUtf8,
    /// The line holds no directory: a blank line, or a trailing newline.
    EmptyLine,
    /// The line has no tab after its package path.
    MissingSeparator,
    /// The package path was already named earlier in the record.
    DuplicateDir,
    /// An absolute path was already named earlier in the record.
    DuplicateTarget,
    /// The package path is not one a record may name.
    Relative(PathsErrorKind),
    /// The library side is not one a record may name.
    LibRelative(PathsErrorKind),
    /// An absolute path is not one a record may name.
    Absolute(PathsErrorKind),
}

/// A refused record or directory, naming its line, numbered from 1, and
/// the path that caused it when there is one.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PluginDirsError {
    kind: PluginDirsErrorKind,
    line: Option<usize>,
    path: Option<String>,
}

impl PluginDirsError {
    fn record(kind: PluginDirsErrorKind) -> Self {
        Self {
            kind,
            line: None,
            path: None,
        }
    }

    fn line(kind: PluginDirsErrorKind, index: usize) -> Self {
        Self {
            kind,
            line: Some(index + 1),
            path: None,
        }
    }

    fn dir(kind: PluginDirsErrorKind, index: usize, path: &str) -> Self {
        Self {
            kind,
            line: Some(index + 1),
            path: Some(excerpt(path)),
        }
    }

    /// Why the record was refused.
    pub fn kind(&self) -> PluginDirsErrorKind {
        self.kind
    }
}

impl PluginDirsErrorKind {
    /// The refusal in words, without where it lies.
    fn reason(self) -> String {
        match self {
            Self::RecordTooLarge => {
                format!("the record is over {MAX_RECORD_BYTES} bytes")
            }
            Self::TooManyDirs => format!("the record holds over {MAX_DIRS} directories"),
            Self::NotUtf8 => "the record is not UTF-8".into(),
            Self::EmptyLine => "the line holds no directory".into(),
            Self::MissingSeparator => "the line has no tab after its package path".into(),
            Self::DuplicateDir => "the directory is named more than once".into(),
            Self::DuplicateTarget => "the absolute path is named more than once".into(),
            Self::Relative(kind) => kind.reason(),
            Self::Absolute(kind) => kind.reason(),
            Self::LibRelative(kind) => match kind {
                PathsErrorKind::RelativeTooLong => {
                    format!("the library path is over {MAX_RELATIVE_BYTES} bytes")
                }
                PathsErrorKind::RelativeIsAbsolute => {
                    "the library path must be relative to a library directory".into()
                }
                PathsErrorKind::Escapes => "the library path leaves its library directory".into(),
                other => other.reason(),
            },
        }
    }
}

impl fmt::Display for PluginDirsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let reason = self.kind.reason();
        match (&self.line, &self.path) {
            (Some(number), Some(path)) => write!(f, "line {number}, \"{path}\": {reason}"),
            (Some(number), None) => write!(f, "line {number}: {reason}"),
            _ => f.write_str(&reason),
        }
    }
}

impl std::error::Error for PluginDirsError {}

/// Encode directories as a record, refusing any record [`decode`]
/// would refuse.
///
/// Each directory's position in `dirs` is the line it lands on, so a
/// refusal names it. The record's size is counted before any line is
/// validated or written, so nothing large is built on the way to a
/// refusal.
pub fn encode(dirs: &[PluginDir]) -> Result<Vec<u8>, PluginDirsError> {
    if dirs.len() > MAX_DIRS {
        return Err(PluginDirsError::record(PluginDirsErrorKind::TooManyDirs));
    }
    let mut relatives = BTreeSet::new();
    let mut targets = BTreeSet::new();
    let mut out = Vec::new();
    for (index, dir) in dirs.iter().enumerate() {
        let mut needed = usize::from(index > 0) + dir.relative.len() + 1 + dir.lib_relative.len();
        for absolute in &dir.absolutes {
            needed = needed.saturating_add(1 + absolute.len());
        }
        if out.len().saturating_add(needed) > MAX_RECORD_BYTES {
            return Err(PluginDirsError::record(PluginDirsErrorKind::RecordTooLarge));
        }
        check_dir(
            index,
            &dir.relative,
            &dir.lib_relative,
            dir.absolutes.iter().map(String::as_str),
            &mut relatives,
            &mut targets,
        )?;
        if index > 0 {
            out.push(b'\n');
        }
        out.extend_from_slice(dir.relative.as_bytes());
        out.push(b'\t');
        out.extend_from_slice(dir.lib_relative.as_bytes());
        for absolute in &dir.absolutes {
            out.push(b'\t');
            out.extend_from_slice(absolute.as_bytes());
        }
    }
    Ok(out)
}

/// Decode a record into its directories, in record order.
///
/// The size is checked before the bytes are looked at, so nothing
/// allocated here is larger than [`MAX_RECORD_BYTES`] allows.
pub fn decode(bytes: &[u8]) -> Result<Vec<PluginDir>, PluginDirsError> {
    if bytes.len() > MAX_RECORD_BYTES {
        return Err(PluginDirsError::record(PluginDirsErrorKind::RecordTooLarge));
    }
    if bytes.is_empty() {
        return Ok(Vec::new());
    }
    let text = std::str::from_utf8(bytes)
        .map_err(|_| PluginDirsError::record(PluginDirsErrorKind::NotUtf8))?;
    let mut dirs = Vec::new();
    let mut relatives = BTreeSet::new();
    let mut targets = BTreeSet::new();
    for (index, line) in text.split('\n').enumerate() {
        if index >= MAX_DIRS {
            return Err(PluginDirsError::record(PluginDirsErrorKind::TooManyDirs));
        }
        if line.is_empty() {
            return Err(PluginDirsError::line(PluginDirsErrorKind::EmptyLine, index));
        }
        let Some((relative, rest)) = line.split_once('\t') else {
            return Err(PluginDirsError::line(
                PluginDirsErrorKind::MissingSeparator,
                index,
            ));
        };
        let mut fields = rest.split('\t');
        let lib_relative = fields.next().unwrap_or_default();
        let absolutes: Vec<&str> = fields.collect();
        check_dir(
            index,
            relative,
            lib_relative,
            absolutes.iter().copied(),
            &mut relatives,
            &mut targets,
        )?;
        dirs.push(PluginDir {
            relative: relative.to_owned(),
            lib_relative: lib_relative.to_owned(),
            absolutes: absolutes.iter().map(|a| (*a).to_owned()).collect(),
        });
    }
    Ok(dirs)
}

/// Check the directory at `index`: each side is one a record may name,
/// its package path is new to `relatives` and its absolute paths are new
/// to `targets`. An empty library side names no counterpart and is
/// allowed.
fn check_dir<'a>(
    index: usize,
    relative: &'a str,
    lib_relative: &str,
    absolutes: impl IntoIterator<Item = &'a str>,
    relatives: &mut BTreeSet<&'a str>,
    targets: &mut BTreeSet<&'a str>,
) -> Result<(), PluginDirsError> {
    let refuse = |kind, path: &str| Err(PluginDirsError::dir(kind, index, path));
    if let Some(kind) = relative_error(relative) {
        return refuse(PluginDirsErrorKind::Relative(kind), relative);
    }
    if !relatives.insert(relative) {
        return refuse(PluginDirsErrorKind::DuplicateDir, relative);
    }
    if !lib_relative.is_empty()
        && let Some(kind) = relative_error(lib_relative)
    {
        return refuse(PluginDirsErrorKind::LibRelative(kind), lib_relative);
    }
    for absolute in absolutes {
        if let Some(kind) = absolute_error(absolute) {
            return refuse(PluginDirsErrorKind::Absolute(kind), absolute);
        }
        if !targets.insert(absolute) {
            return refuse(PluginDirsErrorKind::DuplicateTarget, absolute);
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::paths::{EXCERPT_CHARS, MAX_ABSOLUTE_BYTES};

    fn refused(bytes: &[u8]) -> PluginDirsErrorKind {
        decode(bytes).unwrap_err().kind()
    }

    fn refused_dir(dir: PluginDir) -> PluginDirsErrorKind {
        encode(&[dir]).unwrap_err().kind()
    }

    /// Why one directory is refused, if it is not: the same kind through
    /// both APIs, so the packer can never write a line decode refuses.
    fn refused_sides(
        relative: &str,
        lib_relative: &str,
        absolutes: &[&str],
    ) -> PluginDirsErrorKind {
        let mut line = format!("{relative}\t{lib_relative}");
        for absolute in absolutes {
            line.push('\t');
            line.push_str(absolute);
        }
        let from_decode = refused(line.as_bytes());
        let from_encode = refused_dir(dir(relative, lib_relative, absolutes));
        assert_eq!(from_decode, from_encode);
        from_decode
    }

    fn dir(relative: &str, lib_relative: &str, absolutes: &[&str]) -> PluginDir {
        PluginDir {
            relative: relative.to_owned(),
            lib_relative: lib_relative.to_owned(),
            absolutes: absolutes.iter().map(|a| (*a).to_owned()).collect(),
        }
    }

    #[test]
    fn an_empty_record_holds_no_directories() {
        assert_eq!(decode(b"").unwrap(), Vec::new());
        assert_eq!(encode(&[]).unwrap(), Vec::new());
    }

    #[test]
    fn a_line_carries_its_sides() {
        let dirs = decode(b"lib/alsa-lib\talsa-lib\t/usr/lib/alsa-lib\t/lib/alsa-lib").unwrap();
        assert_eq!(
            dirs,
            vec![dir(
                "lib/alsa-lib",
                "alsa-lib",
                &["/usr/lib/alsa-lib", "/lib/alsa-lib"]
            )]
        );
        let record = encode(&dirs).unwrap();
        assert_eq!(
            record,
            b"lib/alsa-lib\talsa-lib\t/usr/lib/alsa-lib\t/lib/alsa-lib"
        );
        assert_eq!(decode(&record).unwrap(), dirs);
    }

    #[test]
    fn an_unknown_library_side_is_allowed() {
        let dirs = decode(b"lib/xorg/modules\t").unwrap();
        assert_eq!(dirs, vec![dir("lib/xorg/modules", "", &[])]);
        assert_eq!(encode(&dirs).unwrap(), b"lib/xorg/modules\t");
    }

    #[test]
    fn a_record_without_a_library_side_is_still_strict() {
        assert_eq!(
            refused(b"lib/xorg/modules"),
            PluginDirsErrorKind::MissingSeparator
        );
    }

    #[test]
    fn directories_stay_in_record_order() {
        let dirs = decode(b"b\tb\na\ta\t/a").unwrap();
        assert_eq!(dirs, vec![dir("b", "b", &[]), dir("a", "a", &["/a"])]);
    }

    #[test]
    fn the_same_bytes_encode_always() {
        let dirs = vec![
            dir("a", "a", &["/a"]),
            dir("b", "", &[]),
            dir("c/d", "d", &["/c/d"]),
        ];
        let first = encode(&dirs).unwrap();
        let second = encode(&dirs).unwrap();
        assert_eq!(first, second);
        assert_eq!(decode(&first).unwrap(), dirs);
    }

    #[test]
    fn lines_hold_no_blanks() {
        assert_eq!(refused(b"a\tb\n"), PluginDirsErrorKind::EmptyLine);
        assert_eq!(refused(b"a\tb\n\nc\td"), PluginDirsErrorKind::EmptyLine);
        assert_eq!(refused(b"\na\tb"), PluginDirsErrorKind::EmptyLine);
    }

    #[test]
    fn a_line_without_a_tab_is_refused() {
        assert_eq!(refused(b"alsa-lib"), PluginDirsErrorKind::MissingSeparator);
    }

    #[test]
    fn a_record_must_be_utf8() {
        assert_eq!(refused(b"a\t\xffb"), PluginDirsErrorKind::NotUtf8);
    }

    #[test]
    fn a_directory_is_named_once() {
        assert_eq!(
            refused(b"a\tb\nc\td\na\tb"),
            PluginDirsErrorKind::DuplicateDir
        );
        assert_eq!(
            encode(&[dir("a", "b", &[]), dir("a", "c", &[])])
                .unwrap_err()
                .kind(),
            PluginDirsErrorKind::DuplicateDir
        );
    }

    #[test]
    fn an_absolute_path_is_named_once() {
        assert_eq!(
            refused(b"a\tb\t/x\nc\td\t/x"),
            PluginDirsErrorKind::DuplicateTarget
        );
        assert_eq!(
            refused_dir(dir("a", "b", &["/x", "/x"])),
            PluginDirsErrorKind::DuplicateTarget
        );
    }

    #[test]
    fn the_package_path_follows_the_bundle_rules() {
        assert_eq!(
            refused_sides("/usr/lib/alsa-lib", "alsa-lib", &[]),
            PluginDirsErrorKind::Relative(PathsErrorKind::RelativeIsAbsolute)
        );
        assert_eq!(
            refused_sides("../alsa-lib", "alsa-lib", &[]),
            PluginDirsErrorKind::Relative(PathsErrorKind::Escapes)
        );
        assert_eq!(
            refused_sides("alsa-lib/", "alsa-lib", &[]),
            PluginDirsErrorKind::Relative(PathsErrorKind::Unnormalized)
        );
        assert_eq!(
            refused_sides("alsa//lib", "alsa-lib", &[]),
            PluginDirsErrorKind::Relative(PathsErrorKind::Unnormalized)
        );
        assert_eq!(
            refused_sides("alsa/\x01lib", "alsa-lib", &[]),
            PluginDirsErrorKind::Relative(PathsErrorKind::ControlCharacter)
        );
        let long = "a".repeat(MAX_RELATIVE_BYTES + 1);
        assert_eq!(
            refused_sides(&long, "alsa-lib", &[]),
            PluginDirsErrorKind::Relative(PathsErrorKind::RelativeTooLong)
        );
    }

    #[test]
    fn the_library_side_stays_under_a_library_directory() {
        assert_eq!(
            refused_sides("a", "/alsa-lib", &[]),
            PluginDirsErrorKind::LibRelative(PathsErrorKind::RelativeIsAbsolute)
        );
        assert_eq!(
            refused_sides("a", "alsa\x01lib", &[]),
            PluginDirsErrorKind::LibRelative(PathsErrorKind::ControlCharacter)
        );
        assert_eq!(
            refused_sides("a", "../alsa-lib", &[]),
            PluginDirsErrorKind::LibRelative(PathsErrorKind::Escapes)
        );
        assert_eq!(
            refused_sides("a", ".", &[]),
            PluginDirsErrorKind::LibRelative(PathsErrorKind::Unnormalized)
        );
        let long = "a".repeat(MAX_RELATIVE_BYTES + 1);
        assert_eq!(
            refused_sides("a", &long, &[]),
            PluginDirsErrorKind::LibRelative(PathsErrorKind::RelativeTooLong)
        );
    }

    #[test]
    fn an_absolute_side_names_a_normalized_directory() {
        assert_eq!(
            refused_sides("a", "b", &["c"]),
            PluginDirsErrorKind::Absolute(PathsErrorKind::NotAbsolute)
        );
        assert_eq!(
            refused_sides("a", "b", &["/usr/lib//alsa-lib"]),
            PluginDirsErrorKind::Absolute(PathsErrorKind::Unnormalized)
        );
        assert_eq!(
            refused_sides("a", "b", &["/usr/lib/./alsa-lib"]),
            PluginDirsErrorKind::Absolute(PathsErrorKind::Unnormalized)
        );
        assert_eq!(
            refused_sides("a", "b", &["/usr/lib/alsa-lib/../x"]),
            PluginDirsErrorKind::Absolute(PathsErrorKind::Unnormalized)
        );
        assert_eq!(
            refused_sides("a", "b", &["/usr/lib/alsa\x01lib"]),
            PluginDirsErrorKind::Absolute(PathsErrorKind::ControlCharacter)
        );
        let long = format!("/{}", "a".repeat(MAX_ABSOLUTE_BYTES));
        assert_eq!(
            refused_sides("a", "b", &[&long]),
            PluginDirsErrorKind::Absolute(PathsErrorKind::AbsoluteTooLong)
        );
        let exact = format!("/{}", "a".repeat(MAX_ABSOLUTE_BYTES - 1));
        assert_eq!(
            encode(&[dir("a", "b", &[&exact])]).unwrap(),
            format!("a\tb\t{exact}").into_bytes()
        );
    }

    #[test]
    fn a_record_holds_at_most_max_dirs_directories() {
        let dirs: Vec<PluginDir> = (0..MAX_DIRS)
            .map(|i| dir(&format!("d{i}"), "x", &[]))
            .collect();
        let record = encode(&dirs).unwrap();
        assert_eq!(decode(&record).unwrap().len(), MAX_DIRS);
        let dirs: Vec<PluginDir> = (0..MAX_DIRS + 1)
            .map(|i| dir(&format!("d{i}"), "x", &[]))
            .collect();
        assert_eq!(
            encode(&dirs).unwrap_err().kind(),
            PluginDirsErrorKind::TooManyDirs
        );
        let lines: Vec<String> = (0..MAX_DIRS + 1).map(|i| format!("d{i}\tx")).collect();
        let record = lines.join("\n");
        assert_eq!(refused(record.as_bytes()), PluginDirsErrorKind::TooManyDirs);
    }

    #[test]
    fn a_record_holds_at_most_max_record_bytes() {
        let dirs: Vec<PluginDir> = (0..64)
            .map(|i| dir(&format!("{}/{}", "a".repeat(1024), i), "x", &[]))
            .collect();
        assert_eq!(
            encode(&dirs).unwrap_err().kind(),
            PluginDirsErrorKind::RecordTooLarge
        );
        let dirs: Vec<PluginDir> = (0..63)
            .map(|i| dir(&format!("{}/{}", "a".repeat(1024), i), "x", &[]))
            .collect();
        let record = encode(&dirs).unwrap();
        assert!(record.len() <= MAX_RECORD_BYTES);
        assert_eq!(decode(&record).unwrap().len(), 63);
    }

    #[test]
    fn a_record_of_exactly_the_size_bound_round_trips() {
        let chars: Vec<char> = "abcdefghijklmnop".chars().collect();
        let dirs: Vec<PluginDir> = (0..16)
            .map(|i| {
                let lib = "a".repeat(2046 + usize::from(i == 15));
                dir(&format!("{}{}", "a".repeat(2047), chars[i]), &lib, &[])
            })
            .collect();
        let record = encode(&dirs).unwrap();
        assert_eq!(record.len(), MAX_RECORD_BYTES);
        assert_eq!(decode(&record).unwrap(), dirs);
        assert_eq!(encode(&decode(&record).unwrap()).unwrap(), record);
    }

    #[test]
    fn a_record_one_byte_over_the_size_bound_is_refused() {
        let chars: Vec<char> = "abcdefghijklmnop".chars().collect();
        let mut dirs: Vec<PluginDir> = (0..16)
            .map(|i| {
                let lib = "a".repeat(2046 + usize::from(i == 15));
                dir(&format!("{}{}", "a".repeat(2047), chars[i]), &lib, &[])
            })
            .collect();
        dirs[15].lib_relative = "a".repeat(2048);
        assert_eq!(
            encode(&dirs).unwrap_err().kind(),
            PluginDirsErrorKind::RecordTooLarge
        );
    }

    #[test]
    fn an_oversized_record_is_refused_before_it_is_read() {
        assert_eq!(
            refused(&vec![b'a'; MAX_RECORD_BYTES + 1]),
            PluginDirsErrorKind::RecordTooLarge
        );
        assert_eq!(
            refused(&vec![0xff; MAX_RECORD_BYTES + 1]),
            PluginDirsErrorKind::RecordTooLarge
        );
    }

    #[test]
    fn one_directory_cannot_exceed_the_record_bound() {
        let absolutes: Vec<String> = (0..80)
            .map(|i| format!("/lib/{i}/{}", "a".repeat(1024)))
            .collect();
        let refs: Vec<&str> = absolutes.iter().map(String::as_str).collect();
        assert_eq!(
            encode(&[dir("a", "b", &refs)]).unwrap_err().kind(),
            PluginDirsErrorKind::RecordTooLarge
        );
    }

    #[test]
    fn a_refusal_names_its_line_and_path() {
        let error = encode(&[dir("a", "b", &[]), dir("c", "", &["bad"])]).unwrap_err();
        assert_eq!(
            error.kind(),
            PluginDirsErrorKind::Absolute(PathsErrorKind::NotAbsolute)
        );
        let text = error.to_string();
        assert!(text.starts_with("line 2, \"bad\": "), "{text}");
        let error = decode(b"a\tb\nc\td\na\tb").unwrap_err();
        assert!(
            error.to_string().starts_with("line 3, \"a\": "),
            "{}",
            error
        );
    }

    #[test]
    fn a_refusal_excerpts_a_long_path() {
        let long = format!("/{}", "a".repeat(MAX_ABSOLUTE_BYTES));
        let error = encode(&[dir("a", "b", &[&long])]).unwrap_err();
        assert_eq!(
            error.kind(),
            PluginDirsErrorKind::Absolute(PathsErrorKind::AbsoluteTooLong)
        );
        let text = error.to_string();
        assert!(text.contains(&long[..EXCERPT_CHARS - 8]), "{text}");
        assert!(!text.contains(&long[..EXCERPT_CHARS + 8]), "{text}");
    }
}