zond-engine 0.16.0

The core networking library powering Zond. Raw packet injection, protocol parsing, service and OS fingerprinting, and asynchronous network scanning.
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! # What a journal is a journal of
//!
//! A cursor is a number. It means something only against the plan it was counted
//! in, so the plan travels with it and a resumed scan can prove the plan has not
//! moved.
//!
//! ## Why a position is not self-describing
//!
//! [`Cursor`](super::cursor) records that position 4,001,927 is settled.
//! [`TargetMap::iter`](crate::model::target::TargetMap::iter) says which target
//! that is, and it answers differently if anything about the plan changed: a port
//! added to the list, a range widened, an exclusion policy edited, a unit added.
//! None of those are exotic; they are what happens when somebody edits a settings
//! file between two sittings.
//!
//! Resuming across such a change does not fail. It scans the wrong targets and
//! reports success, which is the invisible wrongness
//! [`settle`](super::settle) exists to prevent arriving by another route.
//!
//! So the plan is fingerprinted when the journal is created and checked when it
//! is resumed, and a mismatch is a refusal rather than a warning. A caller who
//! wants the new plan is asking for a new scan, which is a different journal.
//!
//! ## Two shapes of plan
//!
//! The engine has two entry points and they count in different units.
//! [`discover`](crate::scanner::discover) walks addresses; [`scan`] walks
//! addresses paired with ports. Position 400 is the four-hundredth address of
//! one and the four-hundredth address-and-port pair of the other, so a journal
//! records which phase it holds and [`Plan`] is how a caller says.
//!
//! The phase goes into the fingerprint before anything else, which means a
//! sweep and a port scan over the same addresses can never be mistaken for each
//! other however alike the rest of them looks.
//!
//! [`scan`]: crate::scanner::scan
//!
//! ## What the fingerprint covers, and what it costs
//!
//! Not the enumeration; hashing sixteen billion targets to check a `/8` would
//! cost more than the scan. It covers the structure that decides the enumeration:
//! the canonical address ranges, each unit's port list in order, the technique or
//! the sweep flag, and the privilege level, plus the total as a cheap
//! cross-check.
//!
//! That is a few hundred bytes of hashing for a plan of any size, and it moves
//! whenever a position's meaning moves, which is the only property required of
//! it.
//!
//! ## Privilege is part of the plan, for the plans that probe
//!
//! A scan begun privileged and resumed unprivileged is not the same scan
//! continued. The connect fallback can only complete handshakes, so it answers a
//! different question than a raw technique does, which is the argument
//! [`TcpScanTechnique`] makes about not substituting one for the other. Folding
//! it into the fingerprint puts the refusal up front, rather than letting the
//! second sitting fill the first one's gaps with weaker evidence.
//!
//! A watch has no such pair and is not covered. A listener sends nothing and has
//! no fallback to be substituted: it either opened a capture or did nothing, and
//! it enumerated nothing either way, so there is no position a privilege change
//! could give a second meaning to. Covering it refused a resume across `sudo`,
//! which is ordinary on a machine that captures through `access_bpf` or
//! `cap_net_raw`, and refused it by reporting that recorded positions would name
//! different targets, of which such a journal has none.

use std::time::SystemTime;

use serde::{Deserialize, Serialize};

use crate::model::exclusion::Exclusions;
use crate::model::ip::scoped::Zone;
use crate::model::ip::set::IpSet;
use crate::model::target::TargetMap;
use crate::model::technique::TcpScanTechnique;
use crate::record::{PlanRecord, wire};
use crate::report::ScanKind;
use crate::system::privilege::Privilege;

/// What a scan will actually walk, in the shape the phase it belongs to counts.
///
/// The engine's two entry points enumerate different things:
/// [`discover`](crate::scanner::discover) walks addresses and
/// [`scan`](crate::scanner::scan) walks addresses paired with ports. A position
/// means one or the other, never both, so a journal says which it holds.
///
/// # The exclusion policy is part of the plan
///
/// An excluded address is never probed, so it never settles. Numbering a plan
/// that still holds one stalls a resumed scan's watermark at the first
/// exclusion for the rest of the job, and counts a total the scan can never
/// reach.
///
/// The policy also decides the enumeration: withhold the first half of a range
/// and every position after it names a different target. Two sittings under
/// different policies would agree on a fingerprint and disagree on what position
/// 400 means, which is the silent wrong coverage [`settle`](super::settle) exists
/// to prevent arriving by another route.
///
/// So constructing a plan applies the policy, and a caller cannot hold one that
/// has not had it applied. Applying it again inside the scan costs nothing:
/// withholding what is already withheld removes nothing.
#[derive(Debug, Clone)]
pub struct Plan(Resolved);

/// A plan's two shapes. Private, which makes the constructors the only way in: a
/// variant a caller could fill in themselves would be a plan with no exclusion
/// policy applied, which is what this type exists to prevent.
#[derive(Debug, Clone)]
enum Resolved {
    /// Which hosts among these addresses are alive.
    Discovery { addresses: IpSet, sweep: bool },
    /// Which of these addresses' ports are open.
    PortScan {
        targets: TargetMap,
        technique: TcpScanTechnique,
    },
    /// What these links carry.
    ///
    /// The one plan that enumerates nothing. A listener is pointed at a link
    /// rather than at targets, so there is no set to walk, no position to settle
    /// and no total to reach. See [`Plan::listen`].
    Listen { links: Vec<Zone> },
}

impl Plan {
    /// A sweep of `addresses`, less whatever `exclusions` withholds.
    pub fn discovery(addresses: &IpSet, exclusions: &Exclusions, sweep: bool) -> Self {
        let mut addresses = addresses.clone();
        exclusions.withhold(&mut addresses);
        addresses.canonicalize();

        Self(Resolved::Discovery { addresses, sweep })
    }

    /// A port scan of `targets`, less whatever `exclusions` withholds.
    pub fn port_scan(
        targets: &TargetMap,
        exclusions: &Exclusions,
        technique: TcpScanTechnique,
    ) -> Self {
        let mut targets = targets.clone();
        exclusions.withhold_targets(&mut targets);

        Self(Resolved::PortScan { targets, technique })
    }

    /// A watch of `links`.
    ///
    /// # The plan that counts nothing
    ///
    /// The other two enumerate: a sweep walks addresses and a port scan walks
    /// addresses paired with ports, and the cursor and the watermark and the
    /// total are all arithmetic over that enumeration. A listener has none. It
    /// was pointed at a link, the link carries what it carries, and there is no
    /// set of things that could be finished.
    ///
    /// So a listen journal has no cursor, and resuming one appends a sitting
    /// rather than skipping settled work, since there is nothing settled to skip.
    /// What it buys is the other half of what the journal buys the other two: the
    /// findings survive a listener that stopped, and the report describes the
    /// whole watch rather than its last sitting.
    ///
    /// # Why the exclusion policy is not applied here
    ///
    /// It has nothing to apply to. The other constructors narrow a set before it
    /// is numbered, since the policy decides the enumeration. A listener cannot
    /// narrow what a link carries and enforces its scope where findings are
    /// recorded instead. The policy is still in force, applied at the store as it
    /// is for every phase, and it is not part of this plan's identity.
    ///
    /// # What identifies the job
    ///
    /// The links, by name. Not the recording scope: with nothing enumerated
    /// there is nothing a changed scope could renumber, and a sitting that
    /// recorded more or less than the last is still a sitting of the same watch
    /// from the same place. What each sitting covered is on its own phase.
    pub fn listen(links: Vec<Zone>) -> Self {
        Self(Resolved::Listen { links })
    }

    /// Which phase this plan belongs to.
    pub fn kind(&self) -> ScanKind {
        match self.0 {
            Resolved::Discovery { .. } => ScanKind::Discovery,
            Resolved::PortScan { .. } => ScanKind::PortScan,
            Resolved::Listen { .. } => ScanKind::Listen,
        }
    }

    /// The links a watch reads, or `None` for a phase that walks targets.
    pub fn links(&self) -> Option<&[Zone]> {
        match &self.0 {
            Resolved::Listen { links } => Some(links),
            _ => None,
        }
    }

    /// How many targets the plan holds, counted in the units its phase probes:
    /// addresses for a sweep, address-and-port pairs for a port scan.
    pub fn total_targets(&self) -> u128 {
        match &self.0 {
            Resolved::Discovery { addresses, .. } => addresses.len(),
            Resolved::PortScan { targets, .. } => targets.gross_targets().unwrap_or_default(),
            // Not "none were found": there is no unit a watch could be counted
            // in. See `Plan::listen`.
            Resolved::Listen { .. } => 0,
        }
    }

    /// The addresses a sweep will walk, or `None` for a port scan, which is
    /// counted in address-and-port pairs rather than addresses.
    pub fn addresses(&self) -> Option<&IpSet> {
        match &self.0 {
            Resolved::Discovery { addresses, .. } => Some(addresses),
            Resolved::PortScan { .. } | Resolved::Listen { .. } => None,
        }
    }

    /// The targets a port scan will walk, or `None` for a sweep, which has no
    /// ports.
    pub fn targets(&self) -> Option<&TargetMap> {
        match &self.0 {
            Resolved::PortScan { targets, .. } => Some(targets),
            Resolved::Discovery { .. } | Resolved::Listen { .. } => None,
        }
    }

    /// Which TCP segment a port scan's probes carry, or `None` for a sweep,
    /// which sends no segment of its choosing.
    pub fn technique(&self) -> Option<TcpScanTechnique> {
        match &self.0 {
            Resolved::PortScan { technique, .. } => Some(*technique),
            Resolved::Discovery { .. } | Resolved::Listen { .. } => None,
        }
    }

    /// Whether a sweep may go beyond the addresses it was given. False for a
    /// port scan, whose liveness pass is targeted by construction.
    pub fn sweeps_the_segment(&self) -> bool {
        matches!(self.0, Resolved::Discovery { sweep: true, .. })
    }

    /// The plan as a file holds it.
    pub fn record(&self) -> PlanRecord {
        match &self.0 {
            Resolved::Discovery { addresses, .. } => PlanRecord::from(addresses),
            Resolved::PortScan { targets, .. } => PlanRecord::from(targets),
            // A watch names links rather than targets, and those are recorded
            // on the manifest beside the technique and the sweep flag, the other
            // two fields belonging to one phase and not the others.
            Resolved::Listen { .. } => PlanRecord::default(),
        }
    }
}

/// A fingerprint of the plan a cursor's positions are counted in.
///
/// Compared, never interpreted. The value has no meaning beyond equality with
/// another one, and its derivation is free to change when
/// [`JOURNAL_VERSION`](super::format::JOURNAL_VERSION) does, which is what
/// refuses a journal written under an older derivation rather than reporting it
/// as a plan that moved.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PlanFingerprint(u64);

impl PlanFingerprint {
    /// Fingerprints a resolved plan.
    ///
    /// `privilege` is what the scan could actually send, not what it asked for:
    /// what matters is which question the probes answered. **It is read for the
    /// two enumerating phases and ignored for a watch**, which sends no probe
    /// and so has no second question a privilege change could switch it to; see
    /// the `Listen` arm.
    ///
    /// It goes in as the boolean the manifest writes, because that is the shape
    /// the field has on disk and there is no reason for the two to differ.
    ///
    /// The digest walks each unit's canonical ranges and ports rather than its
    /// targets, so this is cheap on a plan of any size. Feeding each field's
    /// count before the fields themselves is what keeps two differently-shaped
    /// plans from colliding: without it, one unit of two ranges and two units of
    /// one would digest the same.
    ///
    /// Every enum reaches the digest as its wire name rather than as a derived
    /// hash, for the reason [`record::wire`](crate::record::wire) gives about
    /// names generally. A derived hash is a variant's position in a declaration,
    /// so inserting a technique anywhere but the end would silently invalidate
    /// every journal on disk, which is precisely the change
    /// [`JOURNAL_VERSION`](super::JOURNAL_VERSION) exists to announce.
    pub fn of(plan: &Plan, privilege: Privilege) -> Self {
        let mut digest = Digest::new();

        // The phase first, so a sweep and a port scan over the same addresses
        // can never agree. They count different things, and a position from one
        // read against the other names a target nobody probed.
        digest.text(wire::scan_kind_name(plan.kind()));

        match &plan.0 {
            Resolved::Discovery { addresses, sweep } => {
                // Privilege belongs to the enumerating phases and to them only.
                // A raw SYN and a connect attempt ask different questions of
                // the same port, so a journal half of each would count two
                // things, which is what this bit refuses.
                digest.flag(privilege.is_raw());
                digest.flag(*sweep);
                digest.addresses(addresses);
            }
            Resolved::Listen { links } => {
                // A watch has no such pair to tell apart, so privilege is not
                // digested here. A listener has one way of working and no
                // fallback: it either opened a capture or it did nothing, and
                // either way it enumerated nothing and left no position for a
                // privilege change to invalidate.
                //
                // Hashing it refused a resume across `sudo`, which is ordinary
                // on a machine that captures through `access_bpf` or
                // `cap_net_raw`, and refused it with a message about recorded
                // positions this journal does not have.
                //
                // By name, and not by index. An interface's number is a fact
                // about a running kernel and changes across a reboot; the name
                // is what a person meant by the link and what two sittings of
                // one watch agree on.
                digest.count(links.len());
                for link in links {
                    digest.text(link.name());
                }
            }
            Resolved::PortScan { targets, technique } => {
                digest.flag(privilege.is_raw());
                digest.text(technique.name());
                digest.count(targets.units.len());

                for unit in &targets.units {
                    digest.addresses(unit.ips());

                    let ports = unit.ports().to_vec();
                    digest.count(ports.len());
                    for (port, protocol) in ports {
                        digest.number(u64::from(port));
                        digest.text(wire::protocol_name(protocol));
                    }
                }
            }
        }

        // A cheap cross-check on everything above. Cannot catch a change the
        // structure digest missed on its own, but it costs one call and it turns
        // a collision into a mismatch rather than a silent agreement.
        digest.wide(plan.total_targets());

        Self(digest.finish())
    }
}

/// The plan digest: FNV-1a over bytes this file chooses, and nothing borrowed
/// from a `Hash` implementation.
///
/// A fingerprint that is written down cannot be built out of `Hash`.
/// `DefaultHasher` is not kept stable across releases of the standard library,
/// so upgrading the compiler would move the value, every journal on disk would
/// stop matching, and the refusal would say the plan had changed. The same
/// caveat covers the `Hash` implementations of the types fed to it, so the
/// bytes are chosen here instead.
///
/// Non-cryptographic on purpose. Nothing here is defending against a chosen
/// collision: anyone who can edit a manifest can edit the fingerprint beside it.
/// What is required is that the value be a function of the plan and of nothing
/// else, which this is.
struct Digest(u64);

impl Digest {
    /// The FNV-1a 64-bit offset basis and prime, as the algorithm defines them.
    const BASIS: u64 = 0xcbf2_9ce4_8422_2325;
    const PRIME: u64 = 0x0000_0100_0000_01b3;

    fn new() -> Self {
        Self(Self::BASIS)
    }

    fn bytes(&mut self, bytes: &[u8]) {
        for byte in bytes {
            self.0 ^= u64::from(*byte);
            self.0 = self.0.wrapping_mul(Self::PRIME);
        }
    }

    /// A string, length first, so that two adjacent fields cannot be run
    /// together into a third that digests the same.
    fn text(&mut self, text: &str) {
        self.number(text.len() as u64);
        self.bytes(text.as_bytes());
    }

    fn number(&mut self, value: u64) {
        self.bytes(&value.to_le_bytes());
    }

    fn wide(&mut self, value: u128) {
        self.bytes(&value.to_le_bytes());
    }

    fn count(&mut self, value: usize) {
        self.number(value as u64);
    }

    fn flag(&mut self, value: bool) {
        self.bytes(&[u8::from(value)]);
    }

    /// One address set's canonical ranges.
    ///
    /// Each family's count goes in before its ranges. Without it, one set of two
    /// ranges and two sets of one would digest the same. The family tag goes in
    /// too, so a v4 range and a v6 range whose octets happen to coincide cannot.
    fn addresses(&mut self, ips: &IpSet) {
        self.count(ips.v4().len());
        for range in ips.v4() {
            self.bytes(&[4]);
            self.bytes(&range.start_addr().octets());
            self.bytes(&range.end_addr().octets());
        }

        self.count(ips.v6().len());
        for range in ips.v6() {
            self.bytes(&[6]);
            self.bytes(&range.start_addr().octets());
            self.bytes(&range.end_addr().octets());
            // The zone is part of the address for a link-local range: `fe80::1`
            // names a different machine on every segment. Absent and zero are
            // told apart, since zero is a scope id a kernel can report.
            match range.zone() {
                Some(zone) => {
                    self.bytes(&[1]);
                    self.number(u64::from(zone));
                }
                None => self.bytes(&[0]),
            }
        }
    }

    fn finish(self) -> u64 {
        self.0
    }
}

/// What a journal is a journal of.
///
/// Written once when the journal is created and never rewritten, which is what
/// makes it safe to read without a lock: nothing that reads a manifest can race
/// a writer changing it.
///
/// `#[non_exhaustive]` because this type has grown six times and will grow
/// again: `kind`, `targets`, `technique`, `sweep`, `links` and `privilege` each
/// carry a `#[serde(default)]` recording a journal written before they existed,
/// and each addition would have broken a caller who built one by literal.
/// [`new`](Self::new) is how one is made, and reading is what everything else
/// does.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JournalManifest {
    /// The journal format this was written under, so a reader that predates it
    /// refuses rather than guessing. Mirrors the header on every journal file;
    /// carried here too because a manifest is the first thing read and should
    /// not depend on another file to be interpretable.
    pub journal_version: u32,
    /// The scan this journal belongs to.
    pub id: String,
    /// The engine build that created it, for diagnostics.
    pub engine_version: String,
    /// When the first sitting began.
    pub created_at: SystemTime,
    /// Which phase this is a journal of, by wire name.
    ///
    /// A sweep counts addresses and a port scan counts address-and-port pairs,
    /// so this decides what everything below is measured in. Absent in a
    /// journal written before sweeps were recorded, which read as port scans
    /// because that is all there was.
    #[serde(default)]
    pub kind: String,
    /// The plan every position in this journal is counted in.
    pub plan: PlanFingerprint,
    /// That plan itself, so a scan can be continued without being described
    /// again.
    ///
    /// A fingerprint can only check a plan somebody supplies; this is what gives
    /// one back. Ranges and port lists, so it stays small for a plan of any
    /// size.
    #[serde(default)]
    pub targets: PlanRecord,
    /// Which segment each TCP probe carried, by wire name. Part of the plan: a
    /// port's verdict means different things under different techniques. Empty
    /// for a sweep, which sends no TCP segment of its choosing.
    #[serde(default)]
    pub technique: String,
    /// Whether a sweep was allowed onto the segment beyond the addresses it was
    /// given. Part of the plan for the same reason the technique is: it decides
    /// what the scan covered. Always false for a port scan, whose liveness pass
    /// is targeted by construction.
    #[serde(default)]
    pub sweep: bool,
    /// The links a watch reads, by name. Part of the plan for the reason the
    /// technique and the sweep flag are: it is what the job *is*.
    ///
    /// By name and not by index, because an index is a fact about a running
    /// kernel and does not survive a reboot, where the name is what a person
    /// meant by the link. Empty for the two phases that walk targets.
    #[serde(default)]
    pub links: Vec<String>,
    /// What the scan was able to send.
    ///
    /// Recorded because a resume must run under the same answer. The connect
    /// fallback asks a different question than a raw technique does, and a
    /// journal half of each would be counting two things.
    #[serde(
        rename = "privileged",
        default = "wire_privilege::unrecorded",
        with = "wire_privilege"
    )]
    pub privilege: Privilege,
    /// How many targets that plan holds, so a caller can report progress without
    /// walking it.
    pub total_targets: u128,
    /// The key the order this journal's targets are asked in is a function of.
    ///
    /// Not part of the plan, and not part of [`PlanFingerprint`]: it decides the
    /// order the same targets are asked in and nothing about which targets those
    /// are, so a sitting resumed under a different one still covers the job. What it buys is that a resumed
    /// sitting does not have to. A scan that switched to a fresh order halfway
    /// through would emit a change of shape mid-run, which is a signature of its
    /// own; see [`Permutation`](crate::scanner::order::Permutation).
    ///
    /// [`None`] in a journal written before the order was keyed, which is
    /// resumed the way it was started: in plan order, shuffled within a batch.
    #[serde(default)]
    pub order_seed: Option<u64>,
    /// A human-readable summary of what was scanned, for a caller listing
    /// journals. **Not** load-bearing: nothing is decided from this text, which
    /// is why it is free to change shape between versions.
    pub summary: String,
}

/// The manifest's `privileged` field as it is written: a boolean, which is the
/// only shape it has ever had on disk.
///
/// The distinction is worth making in the type and not worth making twice.
/// Spelling it out in the file as well would change what every journal already
/// written says, and that is a
/// [`JOURNAL_VERSION`](crate::journal::JOURNAL_VERSION) bump for no reader's
/// benefit.
mod wire_privilege {
    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    use crate::system::privilege::Privilege;

    /// What a journal written before the field existed was scanning under.
    ///
    /// Connect, which is what the boolean's absence has always meant here.
    pub(super) fn unrecorded() -> Privilege {
        Privilege::Connect
    }

    /// Writes a [`Privilege`] as the one bit a journal needs: whether the scan
    /// held raw sockets. The enum's other distinctions are about *why* it did
    /// not, which a resume cannot act on.
    pub(super) fn serialize<S: Serializer>(
        privilege: &Privilege,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        privilege.is_raw().serialize(serializer)
    }

    /// Reads the bit [`serialize`] wrote back into a [`Privilege`].
    pub(super) fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Privilege, D::Error> {
        Ok(if bool::deserialize(deserializer)? {
            Privilege::Raw
        } else {
            Privilege::Connect
        })
    }
}

impl JournalManifest {
    /// Describes a scan about to start.
    pub fn new(
        id: impl Into<String>,
        plan: &Plan,
        privilege: Privilege,
        summary: impl Into<String>,
    ) -> Self {
        Self {
            journal_version: super::JOURNAL_VERSION,
            id: id.into(),
            engine_version: env!("CARGO_PKG_VERSION").to_string(),
            created_at: SystemTime::now(),
            kind: wire::scan_kind_name(plan.kind()).to_owned(),
            plan: PlanFingerprint::of(plan, privilege),
            targets: plan.record(),
            technique: plan
                .technique()
                .map(|technique| technique.name().to_owned())
                .unwrap_or_default(),
            sweep: plan.sweeps_the_segment(),
            links: plan
                .links()
                .unwrap_or_default()
                .iter()
                .map(|link| link.name().to_owned())
                .collect(),
            privilege,
            total_targets: plan.total_targets(),
            // Drawn here because a journal is created once and the order is a
            // property of the job rather than of a sitting. Every sitting after
            // the first reads it back, which is what it is written down for.
            order_seed: Some(rand::random()),
            summary: summary.into(),
        }
    }

    /// Which phase this journal records.
    ///
    /// A journal written before sweeps were recorded names no kind, and is a
    /// port scan, because that is all there was to record.
    pub fn kind(&self) -> ScanKind {
        wire::scan_kind(&self.kind).unwrap_or(ScanKind::PortScan)
    }

    /// The plan this journal was counted in, as it was recorded.
    ///
    /// What a resume scans, in the shape its phase counts in. Rebuilt from the
    /// ranges and ports written down rather than from anything a caller typed, so
    /// a hostname that has since moved does not change what is being continued.
    /// The exclusion policy is already in it, having been applied before the plan
    /// was recorded.
    pub fn recorded(&self) -> Plan {
        // Built here rather than through the constructors. The policy was
        // applied before this was written down, and applying it again would
        // subtract a second time against whatever policy is in force now.
        Plan(match self.kind() {
            ScanKind::Discovery => Resolved::Discovery {
                addresses: self.targets.addresses(),
                sweep: self.sweep,
            },
            // Unresolved zones, deliberately. The recorded plan is what *names*
            // the job and what a fingerprint is taken over, and a name is the
            // whole of that. A caller running the watch supplies links it looked
            // up against this machine, since an index read from a file was true
            // of some other boot.
            ScanKind::Listen => Resolved::Listen {
                links: self
                    .links
                    .iter()
                    .map(|name| Zone::unresolved(name.as_str()))
                    .collect(),
            },
            ScanKind::PortScan => Resolved::PortScan {
                targets: TargetMap::from(&self.targets),
                technique: self.technique(),
            },
        })
    }

    /// The technique the recorded plan ran under.
    ///
    /// Falls back to the default for a journal written before this was recorded,
    /// which the fingerprint then refuses if it was anything else.
    pub fn technique(&self) -> TcpScanTechnique {
        self.technique.parse().unwrap_or_default()
    }

    /// Whether `plan` under these conditions is the plan this journal was
    /// counted in.
    pub fn covers(&self, plan: &Plan, privilege: Privilege) -> Result<(), PlanChanged> {
        let found = PlanFingerprint::of(plan, privilege);
        if found == self.plan {
            return Ok(());
        }

        Err(PlanChanged {
            expected: self.plan,
            found,
            expected_targets: self.total_targets,
            found_targets: plan.total_targets(),
        })
    }
}

/// The plan a journal was counted in is not the plan now being resumed.
///
/// Carries both target counts because they are the half of the difference a
/// person can act on: "40,960 then, 81,920 now" points at the edit, where two
/// hashes do not.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PlanChanged {
    /// What the journal was written against.
    pub expected: PlanFingerprint,
    /// What was offered.
    pub found: PlanFingerprint,
    /// How many targets the original plan held.
    pub expected_targets: u128,
    /// How many the offered plan holds.
    pub found_targets: u128,
}

impl std::fmt::Display for PlanChanged {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "this journal was written against a different plan, so its recorded \
             positions would name different targets"
        )?;

        if self.expected_targets != self.found_targets {
            write!(
                f,
                " ({} targets then, {} now)",
                self.expected_targets, self.found_targets
            )?;
        } else {
            write!(
                f,
                " (the same {} targets, differently arranged, or a different \
                 technique or privilege level)",
                self.expected_targets
            )?;
        }

        Ok(())
    }
}

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

// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║    ██║   █████╗  ███████╗   ██║   ███████╗ ║
// ║    ██║   ██╔══╝  ╚════██║   ██║   ╚════██║ ║
// ║    ██║   ███████╗███████║   ██║   ███████║ ║
// ║    ╚═╝   ╚══════╝╚══════╝   ╚═╝   ╚══════╝ ║
// ╚════════════════════════════════════════════╝

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::ip::set::IpSet;
    use crate::model::port::PortSet;
    use crate::model::target::TargetSet;

    fn plan(pairs: &[(&str, &str)]) -> TargetMap {
        let mut map = TargetMap::new();
        for (range, ports) in pairs {
            map.add_unit(TargetSet::new(
                range.parse::<IpSet>().expect("a range"),
                ports.parse::<PortSet>().expect("ports"),
            ));
        }
        map
    }

    fn ports(map: &TargetMap) -> Plan {
        Plan::port_scan(map, &Exclusions::none(), TcpScanTechnique::Syn)
    }

    fn print(map: &TargetMap) -> PlanFingerprint {
        PlanFingerprint::of(&ports(map), Privilege::Raw)
    }

    fn addresses(written: &str) -> IpSet {
        written.parse().expect("a range")
    }

    fn sweeping(ips: &IpSet, sweep: bool) -> Plan {
        Plan::discovery(ips, &Exclusions::none(), sweep)
    }

    /// The derivation is pinned to a value, not merely to itself.
    ///
    /// Every other test here asks whether two fingerprints agree, and every one
    /// of them would pass with a derivation built on `DefaultHasher`, whose
    /// output the standard library declines to keep stable across compiler
    /// releases. Its value moves when the toolchain does, and every journal on
    /// disk would be refused as a plan that had changed. A test comparing two
    /// fingerprints taken in one process cannot see that. This one can.
    ///
    /// A failure here means the derivation moved. That is allowed, and it is a
    /// [`JOURNAL_VERSION`](crate::journal::JOURNAL_VERSION) bump: every journal
    /// already written carries the old value and cannot be continued under the
    /// new one. Bump the version, update the number here, and check that
    /// `Journal::resume` still refuses the older format by name.
    #[test]
    fn the_derivation_is_pinned_to_a_value() {
        assert_eq!(
            print(&plan(&[("192.0.2.1-192.0.2.10", "80,443")])).0,
            0xa4d1_e087_ea5b_98c2,
            "the plan fingerprint derivation has moved"
        );
    }

    /// The same plan fingerprints the same, however many times it is asked. A
    /// hash that moved between two runs of one build would refuse every resume.
    #[test]
    fn the_same_plan_fingerprints_the_same() {
        let a = plan(&[("192.0.2.1-192.0.2.10", "80,443")]);
        let b = plan(&[("192.0.2.1-192.0.2.10", "80,443")]);

        assert_eq!(print(&a), print(&a), "not stable within one value");
        assert_eq!(print(&a), print(&b), "not stable across equal values");
    }

    /// Every edit that moves what a position means has to move the fingerprint.
    /// Each case here is a plausible thing to do between two sittings.
    #[test]
    fn any_change_that_renumbers_targets_changes_the_fingerprint() {
        let base = plan(&[("192.0.2.1-192.0.2.10", "80,443")]);
        let original = print(&base);

        for (what, changed) in [
            (
                "a port added",
                plan(&[("192.0.2.1-192.0.2.10", "80,443,8080")]),
            ),
            ("a port removed", plan(&[("192.0.2.1-192.0.2.10", "80")])),
            (
                "the range widened",
                plan(&[("192.0.2.1-192.0.2.20", "80,443")]),
            ),
            (
                "the range narrowed",
                plan(&[("192.0.2.1-192.0.2.5", "80,443")]),
            ),
            (
                "a unit added",
                plan(&[("192.0.2.1-192.0.2.10", "80,443"), ("198.51.100.1", "22")]),
            ),
        ] {
            assert_ne!(
                original,
                print(&changed),
                "{what} left the fingerprint alone"
            );
        }
    }

    /// Port *order* decides the enumeration, so two plans holding the same ports
    /// in a different order are different plans.
    #[test]
    fn the_order_of_the_ports_is_part_of_the_plan() {
        let ascending = plan(&[("192.0.2.1", "80,443")]);
        let descending = plan(&[("192.0.2.1", "443,80")]);

        // Only meaningful if the set actually preserves the written order; if it
        // canonicalises, the two are genuinely the same plan and must agree.
        let same_order =
            ascending.units[0].ports().to_vec() == descending.units[0].ports().to_vec();
        assert_eq!(
            print(&ascending) == print(&descending),
            same_order,
            "the fingerprint must follow the enumeration, whichever way the set orders it"
        );
    }

    /// Two units of one range must not hash as one unit of two: the shapes
    /// enumerate differently, and a length-free hash would collide them.
    #[test]
    fn the_shape_of_the_units_is_not_flattened_away() {
        let split = plan(&[
            ("192.0.2.1-192.0.2.5", "80"),
            ("192.0.2.6-192.0.2.10", "80"),
        ]);
        let joined = plan(&[("192.0.2.1-192.0.2.10", "80")]);

        assert_eq!(
            split.gross_targets().unwrap(),
            joined.gross_targets().unwrap(),
            "the same ten targets either way, which is what makes this the trap"
        );
        assert_ne!(
            print(&split),
            print(&joined),
            "two units enumerate differently from one"
        );
    }

    /// A scan begun privileged and resumed unprivileged is a different scan:
    /// the connect fallback can only complete handshakes, so it answers a
    /// different question. The refusal belongs up front.
    #[test]
    fn privilege_and_technique_are_part_of_the_plan() {
        let map = plan(&[("192.0.2.1-192.0.2.10", "80,443")]);

        assert_ne!(
            PlanFingerprint::of(&ports(&map), Privilege::Raw),
            PlanFingerprint::of(&ports(&map), Privilege::Connect),
            "privilege decides which question the probes answered"
        );
        assert_ne!(
            PlanFingerprint::of(&ports(&map), Privilege::Raw),
            PlanFingerprint::of(
                &Plan::port_scan(&map, &Exclusions::none(), TcpScanTechnique::Fin),
                Privilege::Raw
            ),
            "a technique decides what silence means"
        );
    }

    /// Privilege is a type in this build and a boolean in the file, and the
    /// boolean is the half that may not move.
    ///
    /// Every journal on disk was written with `"privileged": true` and
    /// fingerprinted from that byte. Spelling the variant out instead would
    /// refuse all of them without
    /// [`JOURNAL_VERSION`](super::super::JOURNAL_VERSION) having moved to say
    /// so, and the refusal would arrive as a plan that changed.
    #[cfg(feature = "journal-format")]
    #[test]
    fn privilege_is_written_as_the_boolean_the_format_promised() {
        let map = plan(&[("192.0.2.1", "80")]);
        let manifest = JournalManifest::new("01J8Z5Q7VN", &ports(&map), Privilege::Raw, "");

        let mut written = serde_json::to_value(&manifest).expect("a manifest serializes");
        assert_eq!(written["privileged"], serde_json::Value::Bool(true));

        let read: JournalManifest =
            serde_json::from_value(written.clone()).expect("and reads back");
        assert_eq!(read.privilege, Privilege::Raw);
        assert_eq!(read.plan, manifest.plan, "the same plan, still");

        written["privileged"] = serde_json::Value::Bool(false);
        let read: JournalManifest = serde_json::from_value(written).expect("a connect scan reads");
        assert_eq!(read.privilege, Privilege::Connect, "the polarity is intact");
    }

    /// The order a journal's targets are asked in survives the round trip, and a
    /// journal written before the field existed reads as no order rather than as
    /// the one seed 0 names.
    ///
    /// The difference is what a sitting resuming an old journal gets: the walk
    /// that journal was written under, rather than a rearrangement it never used.
    #[cfg(feature = "journal-format")]
    #[test]
    fn a_manifest_that_records_no_order_asks_for_none() {
        let map = plan(&[("192.0.2.1", "80")]);
        let manifest = JournalManifest::new("01J8Z5Q7VN", &ports(&map), Privilege::Raw, "");
        assert!(manifest.order_seed.is_some(), "a fresh journal draws one");

        let mut written = serde_json::to_value(&manifest).expect("a manifest serializes");
        let read: JournalManifest =
            serde_json::from_value(written.clone()).expect("and reads back");
        assert_eq!(read.order_seed, manifest.order_seed);

        written
            .as_object_mut()
            .expect("an object")
            .remove("order_seed");
        let read: JournalManifest =
            serde_json::from_value(written).expect("an older journal reads");
        assert_eq!(read.order_seed, None);
    }

    /// Two journals of the same plan ask about it in different orders, so the
    /// seed is drawn per journal rather than derived from anything the plan says.
    #[cfg(feature = "journal-format")]
    #[test]
    fn two_journals_of_one_plan_ask_about_it_differently() {
        let map = plan(&[("192.0.2.0/24", "1-1024")]);
        let one = JournalManifest::new("01J8Z5Q7VN", &ports(&map), Privilege::Raw, "");
        let other = JournalManifest::new("01J8Z5Q7VP", &ports(&map), Privilege::Raw, "");

        assert_eq!(one.plan, other.plan, "the same plan");
        assert_ne!(one.order_seed, other.order_seed, "asked in its own order");
    }

    /// A journal written before the field existed reads as a connect scan,
    /// which is what its absence has always meant here.
    #[cfg(feature = "journal-format")]
    #[test]
    fn a_manifest_that_records_no_privilege_reads_as_a_connect_scan() {
        let map = plan(&[("192.0.2.1", "80")]);
        let manifest = JournalManifest::new("01J8Z5Q7VN", &ports(&map), Privilege::Raw, "");

        let mut written = serde_json::to_value(&manifest).expect("a manifest serializes");
        written
            .as_object_mut()
            .expect("an object")
            .remove("privileged");

        let read: JournalManifest = serde_json::from_value(written).expect("an older manifest");
        assert_eq!(read.privilege, Privilege::Connect);
    }

    /// The manifest accepts the plan it was made from and refuses anything else,
    /// naming the counts so a person can see what moved.
    #[test]
    fn a_manifest_covers_its_own_plan_and_refuses_another() {
        let original = plan(&[("192.0.2.1-192.0.2.10", "80,443")]);
        let manifest = JournalManifest::new(
            "01J8Z5Q7VN",
            &ports(&original),
            Privilege::Raw,
            "192.0.2.1-192.0.2.10 on 2 ports",
        );

        assert_eq!(manifest.total_targets, 20);
        assert_eq!(manifest.kind(), ScanKind::PortScan);
        assert!(manifest.covers(&ports(&original), Privilege::Raw).is_ok());

        let widened = plan(&[("192.0.2.1-192.0.2.20", "80,443")]);
        let refused = manifest
            .covers(&ports(&widened), Privilege::Raw)
            .expect_err("a widened plan renumbers every position past the first host");

        assert_eq!(refused.expected_targets, 20);
        assert_eq!(refused.found_targets, 40);
        assert!(
            refused.to_string().contains("20 targets then, 40 now"),
            "{refused}"
        );
    }

    /// A sweep and a port scan count in different units, so a position from one
    /// names a different target under the other. The two must never fingerprint
    /// alike, however much the addresses they cover overlap.
    #[test]
    fn a_sweep_is_never_the_same_plan_as_a_port_scan() {
        let ips = addresses("192.0.2.1-192.0.2.10");
        let map = plan(&[("192.0.2.1-192.0.2.10", "80")]);

        assert_ne!(
            PlanFingerprint::of(&sweeping(&ips, false), Privilege::Raw),
            print(&map),
            "the same ten addresses, asked two different questions"
        );

        let manifest =
            JournalManifest::new("01J8Z5Q7VN", &sweeping(&ips, false), Privilege::Raw, "");
        assert_eq!(manifest.kind(), ScanKind::Discovery);
        assert_eq!(manifest.total_targets, 10, "a sweep counts addresses");
        assert!(
            manifest.covers(&ports(&map), Privilege::Raw).is_err(),
            "a sweep's journal must not accept a port scan's plan"
        );
    }

    /// Whether a sweep may go beyond its addresses decides what it covered, so
    /// the two are different plans.
    #[test]
    fn a_segment_sweep_is_not_a_targeted_pass() {
        let ips = addresses("192.0.2.1-192.0.2.10");

        assert_ne!(
            PlanFingerprint::of(&sweeping(&ips, true), Privilege::Raw),
            PlanFingerprint::of(&sweeping(&ips, false), Privilege::Raw)
        );
    }

    /// A sweep's addresses have to come back as they went in, since that is the
    /// whole of its plan.
    #[test]
    fn a_sweeps_addresses_survive_the_round_trip() {
        let ips = addresses("192.0.2.1-192.0.2.10,2001:db8::1");
        let manifest =
            JournalManifest::new("01J8Z5Q7VN", &sweeping(&ips, false), Privilege::Raw, "");

        assert_eq!(
            manifest.recorded().addresses().map(IpSet::len),
            Some(ips.len())
        );
        assert!(
            manifest
                .covers(
                    &sweeping(
                        &manifest.recorded().addresses().cloned().unwrap_or_default(),
                        false
                    ),
                    Privilege::Raw
                )
                .is_ok(),
            "a plan rebuilt from the record must fingerprint as the original"
        );
    }

    /// A link-local plan has to survive the round trip in order. The set sorts
    /// IPv6 by zone before address, so a record coming back with the interfaces
    /// in another order would enumerate differently and every position an earlier
    /// sitting settled would name a different machine.
    #[test]
    fn a_link_local_plan_comes_back_in_the_order_it_was_counted() {
        let mut ips = IpSet::new();
        for (zone, last) in [(9u32, 4u16), (3, 6), (7, 2)] {
            ips.insert_range(crate::model::ip::range::IpRange::V6(
                crate::model::ip::range::Ipv6Range::scoped(
                    "fe80::1".parse().expect("an address"),
                    format!("fe80::{last}").parse().expect("an address"),
                    Some(zone),
                )
                .expect("a range"),
            ));
        }
        ips.canonicalize();

        let manifest =
            JournalManifest::new("01J8Z5Q7VN", &sweeping(&ips, false), Privilege::Raw, "");
        let recovered = manifest
            .recorded()
            .addresses()
            .cloned()
            .expect("a sweep's plan");

        assert_eq!(
            recovered.iter().collect::<Vec<_>>(),
            ips.iter().collect::<Vec<_>>(),
            "the same addresses in the same order, so positions still mean what they did"
        );
        assert!(
            manifest
                .covers(&sweeping(&recovered, false), Privilege::Raw)
                .is_ok(),
            "and the plan rebuilt from the record fingerprints as the original"
        );
    }

    /// A rearrangement holding the same number of targets still refuses, and the
    /// message must not claim a count changed when it did not.
    #[test]
    fn a_refusal_over_an_equal_count_says_so_rather_than_reporting_a_change() {
        let map = plan(&[("192.0.2.1-192.0.2.10", "80,443")]);
        let manifest = JournalManifest::new("01J8Z5Q7VN", &ports(&map), Privilege::Raw, "");

        let refused = manifest
            .covers(
                &Plan::port_scan(&map, &Exclusions::none(), TcpScanTechnique::Fin),
                Privilege::Raw,
            )
            .expect_err("a different technique is a different plan");

        assert_eq!(refused.expected_targets, refused.found_targets);
        let message = refused.to_string();
        assert!(message.contains("differently arranged"), "{message}");
        assert!(!message.contains("then,"), "{message}");
    }

    /// A watch names links, where a sweep and a port scan name targets. The phase
    /// is hashed first, so no two of the three can agree and a journal of one
    /// cannot be continued as another.
    #[test]
    fn a_watch_never_shares_a_fingerprint_with_a_phase_that_walks_targets() {
        let listen = Plan::listen(vec![Zone::unresolved("en0")]);
        let mut ips = IpSet::new();
        ips.insert_range("192.0.2.0/24".parse().expect("a valid range"));

        assert_ne!(
            PlanFingerprint::of(&listen, Privilege::Raw),
            PlanFingerprint::of(&sweeping(&ips, true), Privilege::Raw),
        );
        assert_eq!(listen.kind(), ScanKind::Listen);
        assert_eq!(
            listen.total_targets(),
            0,
            "not `none were found`: there is no unit a watch is counted in"
        );
    }

    /// The links are what the job is, so a watch of a different link is a
    /// different job and may not be appended to this one's record.
    #[test]
    fn a_watch_of_another_link_is_another_job() {
        let one = Plan::listen(vec![Zone::unresolved("en0")]);
        let other = Plan::listen(vec![Zone::unresolved("en1")]);
        let both = Plan::listen(vec![Zone::unresolved("en0"), Zone::unresolved("en1")]);

        assert_ne!(
            PlanFingerprint::of(&one, Privilege::Raw),
            PlanFingerprint::of(&other, Privilege::Raw),
        );
        assert_ne!(
            PlanFingerprint::of(&one, Privilege::Raw),
            PlanFingerprint::of(&both, Privilege::Raw),
        );
    }

    /// A watch is the same watch whether or not this sitting is root, and the
    /// phases that probe still are not.
    ///
    /// A port scan begun with raw sockets and resumed without them falls back to
    /// completing handshakes, which answers a different question, so the second
    /// sitting would fill the first's gaps with weaker evidence and report
    /// success. A listener has no second way of working: it opened a capture or
    /// it did nothing, and it enumerated nothing either way.
    ///
    /// Both halves are asserted together because the risk runs both ways.
    /// Covering a watch refused a resume across `sudo`, which is ordinary on a
    /// machine that captures through `access_bpf` or `cap_net_raw`, and
    /// uncovering a port scan is the silent wrong coverage this module exists to
    /// prevent.
    #[test]
    fn privilege_decides_a_probing_plan_and_says_nothing_about_a_watch() {
        let watch = Plan::listen(vec![Zone::unresolved("en0")]);
        assert_eq!(
            PlanFingerprint::of(&watch, Privilege::Connect),
            PlanFingerprint::of(&watch, Privilege::Raw),
            "a watch under sudo is the same watch"
        );

        let ips = addresses("192.0.2.0/30");
        assert_ne!(
            PlanFingerprint::of(&sweeping(&ips, false), Privilege::Connect),
            PlanFingerprint::of(&sweeping(&ips, false), Privilege::Raw),
            "a sweep's probes are not the same probes"
        );

        let map = plan(&[("192.0.2.1-192.0.2.10", "80,443")]);
        assert_ne!(
            PlanFingerprint::of(&ports(&map), Privilege::Connect),
            PlanFingerprint::of(&ports(&map), Privilege::Raw),
            "and neither are a port scan's"
        );
    }

    /// By name and not by index. An interface's number is a fact about a running
    /// kernel; a watch resumed after a reboot is the same watch.
    #[test]
    fn a_link_is_the_same_link_whatever_number_the_kernel_gave_it_today() {
        let before = Plan::listen(vec![Zone::new(3, "en0")]);
        let after = Plan::listen(vec![Zone::new(11, "en0")]);

        assert_eq!(
            PlanFingerprint::of(&before, Privilege::Raw),
            PlanFingerprint::of(&after, Privilege::Raw),
        );
    }

    /// The recorded plan has to survive the round trip through a manifest, or a
    /// caller with nothing but a journal id cannot say what it was watching.
    #[test]
    fn a_watch_reads_back_as_the_links_it_was_written_with() {
        let plan = Plan::listen(vec![Zone::new(3, "en0"), Zone::new(4, "en1")]);
        let manifest = JournalManifest::new("id", &plan, Privilege::Raw, "listening");

        let recorded = manifest.recorded();
        assert_eq!(recorded.kind(), ScanKind::Listen);
        assert_eq!(
            recorded
                .links()
                .expect("a watch names links")
                .iter()
                .map(|link| link.name().to_owned())
                .collect::<Vec<_>>(),
            vec!["en0".to_owned(), "en1".to_owned()],
        );
        manifest
            .covers(&plan, Privilege::Raw)
            .expect("the plan it was written against still covers it");
    }
}