iec61850-kit 0.8.1

IEC 61850 in Rust: GOOSE and Sampled Values on capture files and live interfaces, an MMS client and server over the full OSI stack, and SCL loading and editing. Panic-free, sans-IO, no_std-capable.
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
//! The `ied` binary, end to end.
//!
//! Every subcommand works on capture files, so all of this runs in CI without a network
//! interface. The merging unit generates a stream, and the monitor reads it back: if the
//! encoder and the decoder ever disagree, this fails.

#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing)]

mod common;

use std::path::PathBuf;
use std::process::Command;

/// The `ied` binary built alongside this test.
fn ied() -> PathBuf {
    // `CARGO_BIN_EXE_<name>` is set by cargo for every binary target in the package.
    PathBuf::from(env!("CARGO_BIN_EXE_ied"))
}

struct Output {
    stdout: String,
    stderr: String,
    ok: bool,
}

fn run(args: &[&str]) -> Output {
    let out = Command::new(ied()).args(args).output().expect("run ied");
    Output { stdout: String::from_utf8_lossy(&out.stdout).into_owned(), stderr: String::from_utf8_lossy(&out.stderr).into_owned(), ok: out.status.success() }
}

fn tempdir(name: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("iec61850-cli-{}-{name}", std::process::id()));
    std::fs::create_dir_all(&dir).expect("create temp dir");
    dir
}

/// Every subcommand answers `--help` with its own usage, and an option is never taken for a
/// file name: `ied mu --help` once wrote a capture called `--help`.
#[test]
fn every_subcommand_answers_help_and_no_option_becomes_a_file() {
    let dir = tempdir("help");
    let commands: [&[&str]; 12] = [
        &["mu"],
        &["sim"],
        &["goose", "sniff"],
        &["goose", "publish"],
        &["sv", "monitor"],
        &["pcap", "info"],
        &["scl", "validate"],
        &["scl", "show"],
        &["scl", "subs"],
        &["mms", "sniff"],
        &["mms", "read"],
        &["mms", "report"],
    ];
    for command in commands {
        for flag in ["--help", "-h"] {
            let args: Vec<&str> = command.iter().copied().chain([flag]).collect();
            let out = Command::new(ied()).args(&args).current_dir(&dir).output().expect("run ied");
            let text = String::from_utf8_lossy(&out.stdout);
            assert!(out.status.success(), "{args:?} failed");
            assert!(text.contains(&format!("ied {}", command.join(" "))), "{args:?} printed no usage line for itself:\n{text}");
        }
    }
    let mu = Command::new(ied()).args(["mu", "--frames", "--help"]).current_dir(&dir).output().expect("run ied");
    assert!(mu.status.success());
    assert!(std::fs::read_dir(&dir).unwrap().next().is_none(), "--help wrote a file");
    let refused = run(&["pcap", "info", "--nope"]);
    assert!(!refused.ok && refused.stderr.contains("expected a file name"), "{}", refused.stderr);
    let refused = run(&["mu", "--frames", "3"]);
    assert!(!refused.ok && refused.stderr.contains("usage: ied mu"), "{}", refused.stderr);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn help_and_version() {
    let h = run(&["--help"]);
    assert!(h.ok && h.stdout.contains("ied — IEC 61850 command line"), "{h:?}", h = h.stdout);
    assert!(run(&["--version"]).stdout.contains(env!("CARGO_PKG_VERSION")));
    let bad = run(&["nonsense"]);
    assert!(!bad.ok, "an unknown command must fail");
    assert!(bad.stderr.contains("unknown command"), "{}", bad.stderr);
}

#[test]
fn errors_are_reported_not_panicked() {
    for args in [
        vec!["pcap", "info", "/nonexistent.pcap"],
        vec!["goose", "sniff", "/nonexistent.pcap"],
        vec!["scl", "show", "/nonexistent.scl"],
        vec!["mu", "/nonexistent/dir/out.pcap"],
    ] {
        let o = run(&args);
        assert!(!o.ok, "{args:?} should fail");
        assert!(o.stderr.starts_with("ied: "), "{args:?}: {}", o.stderr);
        assert!(!o.stderr.contains("panicked"), "{args:?}: {}", o.stderr);
    }
    // A file that exists but is not a capture.
    let dir = tempdir("notapcap");
    let junk = dir.join("junk.pcap");
    std::fs::write(&junk, b"this is not a pcap").unwrap();
    let o = run(&["pcap", "info", junk.to_str().unwrap()]);
    assert!(!o.ok && o.stderr.contains("not a classic pcap"), "{}", o.stderr);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn merging_unit_output_is_read_back_by_the_monitor() {
    let dir = tempdir("mu");
    for (profile, frames, asdus_per_frame, rate) in [("le80-50", 100u32, 1u32, 4000u32), ("f4800s2", 100, 2, 4800), ("f14400s6", 60, 6, 14_400)] {
        let out = dir.join(format!("{profile}.pcap"));
        let path = out.to_str().unwrap();
        let made = run(&["mu", path, "--profile", profile, "--frames", &frames.to_string(), "--sv-id", "TESTMU"]);
        assert!(made.ok, "{}", made.stderr);
        assert!(made.stdout.contains(&format!("{rate} samples/s")), "{}", made.stdout);

        let mon = run(&["sv", "monitor", path]);
        assert!(mon.ok, "{}", mon.stderr);
        assert!(mon.stdout.contains("svID=TESTMU"), "{}", mon.stdout);
        assert!(mon.stdout.contains(&format!("frames={frames}")), "{}", mon.stdout);
        assert!(mon.stdout.contains(&format!("asdus={}", frames * asdus_per_frame)), "{}", mon.stdout);
        assert!(mon.stdout.contains("gaps=0 samples lost=0"), "the generated stream must be continuous: {}", mon.stdout);

        let info = run(&["pcap", "info", path]);
        assert!(info.stdout.contains(&format!("sampled values {frames}")), "{}", info.stdout);
        assert!(info.stdout.contains(&format!("VLAN-tagged {frames}")), "{}", info.stdout);
    }
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn generated_frames_dissect_cleanly_in_wireshark() {
    let Some(tshark) = common::tshark() else { return };
    let dir = tempdir("oracle");
    let out = dir.join("mu.pcap");
    let path = out.to_str().unwrap();
    assert!(run(&["mu", path, "--profile", "f4800s2", "--frames", "50"]).ok);

    let json = Command::new(tshark).arg("-r").arg(path).args(["-T", "json"]).output().expect("run tshark");
    let text = String::from_utf8_lossy(&json.stdout);
    assert!(json.status.success(), "{}", String::from_utf8_lossy(&json.stderr));
    assert!(!text.contains("_ws.malformed"), "the merging unit produced a malformed frame");
    assert!(text.contains("\"sv.svID\": \"MU01\""), "{text}");
    assert!(text.contains("\"sv.noASDU\": \"2\""), "{text}");
    assert!(text.contains("\"sv.smpRate\": \"4800\""), "{text}");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn goose_sniff_reads_a_real_capture() {
    let Some(capture) = common::reference("pcap/goose-mdehus.pcap") else { return };
    let o = run(&["goose", "sniff", capture.to_str().unwrap()]);
    assert!(o.ok, "{}", o.stderr);
    assert!(o.stdout.contains("16 GOOSE frames"), "{}", o.stdout);
    assert!(o.stdout.contains("SEL_351_1CFG/LLN0$GO$NewGOOSEMessage"), "{}", o.stdout);
    assert!(!o.stdout.contains("COUNT-MISMATCH"), "a real capture must not look malformed: {}", o.stdout);
}

#[test]
fn goose_sniff_calls_out_a_replay() {
    // A capture with a frame repeated after the stream has moved on. Decoding it says
    // nothing is wrong — every field is well formed. Running the subscriber state machine
    // over it says what is: the same frame twice is a replay while the state is live.
    let dir = tempdir("replay");
    let path = dir.join("replay.pcap");
    let frames = goose_frames();
    let replayed = vec![frames[0].clone(), frames[1].clone(), frames[0].clone()];
    common::write_pcap(&path, &replayed);

    let o = run(&["goose", "sniff", path.to_str().unwrap()]);
    assert!(o.ok, "{}", o.stderr);
    assert!(o.stdout.contains("Replay"), "the sniffer must name a replay: {}", o.stdout);
    assert!(o.stdout.contains("replays=1"), "{}", o.stdout);
    assert!(o.stdout.contains("3 GOOSE frames in 1 stream(s)"), "{}", o.stdout);
    assert!(o.stdout.contains("stDiff="), "the IDS delta features belong in the summary: {}", o.stdout);
    std::fs::remove_dir_all(&dir).ok();
}

/// Two GOOSE frames of one stream: a state change and the state after it.
fn goose_frames() -> Vec<Vec<u8>> {
    use iec61850_kit::common::{Instant, UtcTime};
    use iec61850_kit::proto::data::Value;
    use iec61850_kit::proto::ethernet::{ETHERTYPE_GOOSE, FrameHeader, MacAddr, VlanTag};
    use iec61850_kit::proto::goose::{Publisher, PublisherConfig, Retransmission};

    let cfg = PublisherConfig {
        header: FrameHeader {
            dst: MacAddr::GOOSE_BASE,
            src: MacAddr([2, 0, 0, 0, 0, 1]),
            vlan: Some(VlanTag::DEFAULT),
            ethertype: ETHERTYPE_GOOSE,
            appid: 1,
            reserved1: 0,
            reserved2: 0,
        },
        gocb_ref: "IED1LD0/LLN0$GO$gcbTrip".into(),
        dat_set: "IED1LD0/LLN0$dsTrip".into(),
        go_id: Some("IED1".into()),
        conf_rev: 1,
        retransmission: Retransmission::DEFAULT,
        simulation: false,
        nds_com: false,
    };
    let mut p = Publisher::new(cfg, &[Value::Boolean(false)], UtcTime::default()).unwrap();
    let mut out = Vec::new();
    p.on_timeout(Instant::ZERO).unwrap();
    out.push(p.poll_transmit().unwrap().to_vec());
    p.publish(Instant::ZERO.plus_millis(1), &[Value::Boolean(true)], UtcTime::default()).unwrap();
    out.push(p.poll_transmit().unwrap().to_vec());
    out
}

#[test]
fn mms_sniff_reads_a_real_association() {
    // The reference capture is an ICCP association — MMS over ACSE over presentation over
    // session over COTP over TPKT. Decoding it end to end is the check that the whole OSI
    // stack works on traffic nobody here produced.
    let Some(capture) = common::reference("pcap/mms.pcap") else { return };
    let o = run(&["mms", "sniff", capture.to_str().unwrap()]);
    assert!(o.ok, "{}", o.stderr);
    // The handshake, layer by layer.
    assert!(o.stdout.contains("COTP CR src-ref=0xb001 tpdu-size=1024"), "{}", o.stdout);
    assert!(o.stdout.contains("CP  contexts 1=2.2.1.0.1 3=1.0.9506.2.1"), "the negotiated contexts: {}", o.stdout);
    assert!(o.stdout.contains("AARE accepted"), "{}", o.stdout);
    assert!(o.stdout.contains("Initiate maxPDU=Some(32000)"), "{}", o.stdout);
    // And what the association carried.
    assert!(o.stdout.contains("identify AREVA T&D Corporation e-terracomm 2.3.1"), "the server names itself: {}", o.stdout);
    assert!(o.stdout.contains("data set of 19 member(s)"), "{}", o.stdout);
    assert!(o.stdout.contains("report KIRKLAND/EMS_ANALOG_ICCP_IN (19 values)"), "{}", o.stdout);
    assert!(o.stdout.contains("23 request(s), 23 response(s), 115 report(s), 823 value(s)"), "{}", o.stdout);
    assert!(!o.stdout.contains("did not decode"), "every PDU in the capture must decode: {}", o.stdout);
}

#[test]
fn scl_show_and_validate() {
    let dir = tempdir("scl");
    let file = dir.join("t.icd");
    std::fs::write(&file, GOOD_ICD).unwrap();
    let path = file.to_str().unwrap();

    let show = run(&["scl", "show", path]);
    assert!(show.ok, "{}", show.stderr);
    // The common data class beside each object: `DPC` is a controllable double point and
    // `ACT` a protection activation, and it is the first thing anyone reading an unfamiliar
    // file wants next to the name.
    for expected in ["IED IED1", "LD IED1LD0", "LN LLN0", "DO Tr [ACT]", "GSEControl gcbTrip", "DataSet dsTrip", "IED1LD0/PTRC1$ST$Tr$general"] {
        assert!(show.stdout.contains(expected), "missing {expected} in {}", show.stdout);
    }
    assert!(run(&["scl", "validate", path]).ok, "a conforming file must validate");

    // An APPID outside the GOOSE range is exactly the kind of engineering error a validator
    // exists to catch; the schema permits it.
    let bad = dir.join("bad.icd");
    std::fs::write(&bad, GOOD_ICD.replace("<P type=\"APPID\">0005</P>", "<P type=\"APPID\">4005</P>")).unwrap();
    let o = run(&["scl", "validate", bad.to_str().unwrap()]);
    assert!(!o.ok, "an out-of-range APPID must fail validation");
    assert!(o.stdout.contains("outside 0x0000-0x3FFF"), "{}", o.stdout);
    assert!(o.stdout.contains("error: AppidOutOfRange"), "findings carry a stable code: {}", o.stdout);

    // A warning alone does not fail the build unless it is asked to.
    let warned = dir.join("warn.icd");
    std::fs::write(&warned, GOOD_ICD.replace("<P type=\"VLAN-PRIORITY\">4</P>", "<P type=\"VLAN-PRIORITY\">0</P>")).unwrap();
    let o = run(&["scl", "validate", warned.to_str().unwrap()]);
    assert!(o.ok, "a warning is not an error: {} {}", o.stdout, o.stderr);
    assert!(o.stdout.contains("warning: VlanPriority"), "{}", o.stdout);
    let o = run(&["scl", "validate", warned.to_str().unwrap(), "--strict"]);
    assert!(!o.ok, "--strict promotes it: {}", o.stdout);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn scl_subs_resolves_a_subscriber_against_its_publishers() {
    let dir = tempdir("subs");
    let file = dir.join("bay.scd");
    std::fs::write(&file, SCD).unwrap();
    let path = file.to_str().unwrap();

    let o = run(&["scl", "subs", path, "IED2"]);
    assert!(o.ok, "{} {}", o.stdout, o.stderr);
    assert!(o.stdout.contains("1 GOOSE and 1 sampled-value stream"), "{}", o.stdout);
    assert!(o.stdout.contains("IED1LD0/LLN0$GO$gcbTrip from IED1"), "{}", o.stdout);
    assert!(o.stdout.contains("appid=0x0005 confRev=3"), "{}", o.stdout);
    assert!(o.stdout.contains("MU01 from IED1"), "{}", o.stdout);
    assert!(o.stdout.contains("rate=4000/s"), "{}", o.stdout);
    assert!(o.stdout.contains("[BI1]"), "the internal address the input is wired to: {}", o.stdout);

    // An ExtRef naming a publisher the file does not hold is a commissioning finding, not
    // something to swallow.
    let broken = dir.join("broken.scd");
    std::fs::write(&broken, SCD.replace("iedName=\"IED1\" ldInst=\"LD0\" lnClass=\"PTRC\"", "iedName=\"Ghost\" ldInst=\"LD0\" lnClass=\"PTRC\"")).unwrap();
    let o = run(&["scl", "subs", broken.to_str().unwrap(), "IED2"]);
    assert!(!o.ok);
    assert!(o.stdout.contains("unresolved") && o.stdout.contains("Ghost"), "{}", o.stdout);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn scl_validate_reports_dangling_types_from_the_openscd_corpus() {
    let Some(file) = common::reference("fixtures/openscd/valid2007B4.scd") else { return };
    let o = run(&["scl", "validate", file.to_str().unwrap()]);
    // The corpus is known to reference types it does not define; the tool must name them
    // rather than either crashing or silently accepting.
    assert!(!o.ok);
    assert!(o.stdout.contains("MissingLNodeType"), "{}", o.stdout);
    assert!(o.stdout.contains("not found"), "{}", o.stdout);
}

#[test]
fn sv_monitor_reads_the_channels_out_of_the_engineering_file() {
    // Without the file, an ASDU is 64 octets nobody can name. With it, the data set says
    // what each one means — which is the difference between a tool that reads 9-2LE and one
    // that reads whatever a merging unit was engineered to send.
    let dir = tempdir("svscd");
    let capture = dir.join("mu.pcap");
    let scd = dir.join("mu.scd");
    std::fs::write(&scd, MU_SCD).unwrap();
    let path = capture.to_str().unwrap();
    assert!(run(&["mu", path, "--frames", "200"]).ok);

    let o = run(&["sv", "monitor", path, "--scd", scd.to_str().unwrap()]);
    assert!(o.ok, "{}", o.stderr);
    assert!(o.stdout.contains("MU MULD0/LLN0.msvcb01"), "{}", o.stdout);
    assert!(o.stdout.contains("16 channels, 64 octets per ASDU"), "{}", o.stdout);
    assert!(o.stdout.contains("frames=200"), "{}", o.stdout);
    assert!(o.stdout.contains("gaps=0"), "{}", o.stdout);
    // Named channels, decoded values, and the quality word read as a quality.
    assert!(o.stdout.contains("LD0/TCTR1.AmpSv.instMag.i"), "{}", o.stdout);
    assert!(o.stdout.contains("LD0/TVTR3.VolSv.q"), "{}", o.stdout);
    assert!(o.stdout.matches("good").count() >= 8, "{}", o.stdout);

    // The stream in the file has to be the stream on the wire: a capture of something else
    // is reported as such rather than decoded into channels that are not there.
    let other = dir.join("other.pcap");
    assert!(run(&["mu", other.to_str().unwrap(), "--frames", "10", "--appid", "4009"]).ok);
    let miss = run(&["sv", "monitor", other.to_str().unwrap(), "--scd", scd.to_str().unwrap()]);
    assert!(miss.ok, "{}", miss.stderr);
    assert!(miss.stdout.contains("no sample of this stream in the capture"), "{}", miss.stdout);

    // `--ied` narrows it, and naming an IED that is not there is an error, not a silence.
    assert!(run(&["sv", "monitor", path, "--scd", scd.to_str().unwrap(), "--ied", "MU"]).ok);
    let bad = run(&["sv", "monitor", path, "--scd", scd.to_str().unwrap(), "--ied", "NOSUCH"]);
    assert!(!bad.ok && bad.stderr.contains("not found"), "{}", bad.stderr);
    assert!(!run(&["sv", "monitor", path, "--ied", "MU"]).ok, "--ied without --scd means nothing");
    std::fs::remove_dir_all(&dir).ok();
}

/// A merging unit as an engineering file describes it: the whole 9-2LE data set — four
/// currents and four voltages, each an `INT32` sample and a quality word — on the address
/// and APPID `ied mu` publishes to by default. This is what turns the ASDU's 64 octets into
/// sixteen named channels without a line of code knowing what 9-2LE is.
const MU_SCD: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" version="2007" revision="B" release="4">
  <Header id="t"/>
  <Communication>
    <SubNetwork name="process">
      <ConnectedAP iedName="MU" apName="P1">
        <SMV ldInst="LD0" cbName="msvcb01">
          <Address><P type="MAC-Address">01-0C-CD-04-00-00</P><P type="APPID">4000</P><P type="VLAN-PRIORITY">4</P></Address>
        </SMV>
      </ConnectedAP>
    </SubNetwork>
  </Communication>
  <IED name="MU" manufacturer="ACME" type="MergingUnit">
    <AccessPoint name="P1"><Server>
      <LDevice inst="LD0">
        <LN0 lnClass="LLN0" inst="" lnType="LLN0_T">
          <DataSet name="PhsMeas1">
            <FCDA ldInst="LD0" lnClass="TCTR" lnInst="1" doName="AmpSv" daName="instMag.i" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TCTR" lnInst="1" doName="AmpSv" daName="q" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TCTR" lnInst="2" doName="AmpSv" daName="instMag.i" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TCTR" lnInst="2" doName="AmpSv" daName="q" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TCTR" lnInst="3" doName="AmpSv" daName="instMag.i" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TCTR" lnInst="3" doName="AmpSv" daName="q" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TCTR" lnInst="4" doName="AmpSv" daName="instMag.i" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TCTR" lnInst="4" doName="AmpSv" daName="q" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TVTR" lnInst="1" doName="VolSv" daName="instMag.i" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TVTR" lnInst="1" doName="VolSv" daName="q" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TVTR" lnInst="2" doName="VolSv" daName="instMag.i" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TVTR" lnInst="2" doName="VolSv" daName="q" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TVTR" lnInst="3" doName="VolSv" daName="instMag.i" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TVTR" lnInst="3" doName="VolSv" daName="q" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TVTR" lnInst="4" doName="VolSv" daName="instMag.i" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TVTR" lnInst="4" doName="VolSv" daName="q" fc="MX"/>
          </DataSet>
          <SampledValueControl name="msvcb01" smvID="MU01" datSet="PhsMeas1" confRev="1" smpRate="80" nofASDU="1"/>
        </LN0>
        <LN lnClass="TCTR" inst="1" prefix="" lnType="TCTR_T"/>
        <LN lnClass="TCTR" inst="2" prefix="" lnType="TCTR_T"/>
        <LN lnClass="TCTR" inst="3" prefix="" lnType="TCTR_T"/>
        <LN lnClass="TCTR" inst="4" prefix="" lnType="TCTR_T"/>
        <LN lnClass="TVTR" inst="1" prefix="" lnType="TVTR_T"/>
        <LN lnClass="TVTR" inst="2" prefix="" lnType="TVTR_T"/>
        <LN lnClass="TVTR" inst="3" prefix="" lnType="TVTR_T"/>
        <LN lnClass="TVTR" inst="4" prefix="" lnType="TVTR_T"/>
      </LDevice>
    </Server></AccessPoint>
  </IED>
  <DataTypeTemplates>
    <LNodeType id="LLN0_T" lnClass="LLN0"><DO name="Mod" type="ENC_T"/></LNodeType>
    <LNodeType id="TCTR_T" lnClass="TCTR"><DO name="AmpSv" type="SAV_T"/></LNodeType>
    <LNodeType id="TVTR_T" lnClass="TVTR"><DO name="VolSv" type="SAV_T"/></LNodeType>
    <DOType id="ENC_T" cdc="ENC"><DA name="stVal" fc="ST" bType="Enum" type="Mod_E"/></DOType>
    <DOType id="SAV_T" cdc="SAV"><DA name="instMag" fc="MX" bType="Struct" type="AV_T"/><DA name="q" fc="MX" bType="Quality"/></DOType>
    <DAType id="AV_T"><BDA name="i" bType="INT32"/></DAType>
    <EnumType id="Mod_E"><EnumVal ord="1">on</EnumVal></EnumType>
  </DataTypeTemplates>
</SCL>"#;

const GOOD_ICD: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" version="2007" revision="B" release="4">
  <Header id="t"/>
  <Communication>
    <SubNetwork name="bus">
      <ConnectedAP iedName="IED1" apName="P1">
        <GSE ldInst="LD0" cbName="gcbTrip">
          <Address>
            <P type="MAC-Address">01-0C-CD-01-00-05</P>
            <P type="APPID">0005</P>
            <P type="VLAN-ID">001</P>
            <P type="VLAN-PRIORITY">4</P>
          </Address>
        </GSE>
      </ConnectedAP>
    </SubNetwork>
  </Communication>
  <IED name="IED1" manufacturer="ACME" type="Relay" configVersion="1.0">
    <AccessPoint name="P1"><Server>
      <LDevice inst="LD0">
        <LN0 lnClass="LLN0" inst="" lnType="LLN0_T">
          <DataSet name="dsTrip"><FCDA ldInst="LD0" lnClass="PTRC" lnInst="1" doName="Tr" daName="general" fc="ST"/></DataSet>
          <GSEControl name="gcbTrip" datSet="dsTrip" confRev="1" appID="IED1_Trip" type="GOOSE"/>
        </LN0>
        <LN lnClass="PTRC" inst="1" prefix="" lnType="PTRC_T"/>
      </LDevice>
    </Server></AccessPoint>
  </IED>
  <DataTypeTemplates>
    <LNodeType id="LLN0_T" lnClass="LLN0"><DO name="Mod" type="ENC_T"/></LNodeType>
    <LNodeType id="PTRC_T" lnClass="PTRC"><DO name="Tr" type="ACT_T"/></LNodeType>
    <DOType id="ENC_T" cdc="ENC"><DA name="stVal" fc="ST" bType="Enum" type="Mod_E"/></DOType>
    <DOType id="ACT_T" cdc="ACT"><DA name="general" fc="ST" bType="BOOLEAN"/></DOType>
    <EnumType id="Mod_E"><EnumVal ord="1">on</EnumVal></EnumType>
  </DataTypeTemplates>
</SCL>"#;

/// Two IEDs: a relay that publishes GOOSE and sampled values, and a subscriber wired to
/// both through `Inputs/ExtRef` — the shape of a real SCD, minus everything else.
const SCD: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" version="2007" revision="B" release="4">
  <Header id="t"/>
  <Communication>
    <SubNetwork name="bus">
      <ConnectedAP iedName="IED1" apName="P1">
        <GSE ldInst="LD0" cbName="gcbTrip">
          <Address><P type="MAC-Address">01-0C-CD-01-00-05</P><P type="APPID">0005</P></Address>
          <MinTime unit="s" multiplier="m">4</MinTime>
          <MaxTime unit="s" multiplier="m">1000</MaxTime>
        </GSE>
        <SMV ldInst="LD0" cbName="msvcb01">
          <Address><P type="MAC-Address">01-0C-CD-04-00-01</P><P type="APPID">4001</P></Address>
        </SMV>
      </ConnectedAP>
    </SubNetwork>
  </Communication>
  <IED name="IED1" manufacturer="ACME" type="Relay" configVersion="1.0">
    <AccessPoint name="P1"><Server>
      <LDevice inst="LD0">
        <LN0 lnClass="LLN0" inst="" lnType="LLN0_T">
          <DataSet name="dsTrip"><FCDA ldInst="LD0" lnClass="PTRC" lnInst="1" doName="Tr" daName="general" fc="ST"/></DataSet>
          <DataSet name="PhsMeas1">
            <FCDA ldInst="LD0" lnClass="TCTR" lnInst="1" doName="AmpSv" daName="instMag.i" fc="MX"/>
            <FCDA ldInst="LD0" lnClass="TCTR" lnInst="1" doName="AmpSv" daName="q" fc="MX"/>
          </DataSet>
          <GSEControl name="gcbTrip" datSet="dsTrip" confRev="3" appID="IED1_Trip" type="GOOSE"/>
          <SampledValueControl name="msvcb01" smvID="MU01" datSet="PhsMeas1" confRev="1" smpRate="80" nofASDU="1"/>
        </LN0>
        <LN lnClass="PTRC" inst="1" prefix="" lnType="PTRC_T"/>
        <LN lnClass="TCTR" inst="1" prefix="" lnType="TCTR_T"/>
      </LDevice>
    </Server></AccessPoint>
  </IED>
  <IED name="IED2" manufacturer="ACME" type="Merge">
    <AccessPoint name="P1"><Server>
      <LDevice inst="LD0">
        <LN0 lnClass="LLN0" inst="" lnType="LLN0_T">
          <Inputs>
            <ExtRef iedName="IED1" ldInst="LD0" lnClass="PTRC" lnInst="1" doName="Tr" daName="general"
                    serviceType="GOOSE" srcLDInst="LD0" srcCBName="gcbTrip" intAddr="BI1"/>
            <ExtRef iedName="IED1" ldInst="LD0" lnClass="TCTR" lnInst="1" doName="AmpSv" daName="instMag.i"
                    serviceType="SMV" srcLDInst="LD0" srcCBName="msvcb01"/>
          </Inputs>
        </LN0>
      </LDevice>
    </Server></AccessPoint>
  </IED>
  <DataTypeTemplates>
    <LNodeType id="LLN0_T" lnClass="LLN0"><DO name="Mod" type="ENC_T"/></LNodeType>
    <LNodeType id="PTRC_T" lnClass="PTRC"><DO name="Tr" type="ACT_T"/></LNodeType>
    <LNodeType id="TCTR_T" lnClass="TCTR"><DO name="AmpSv" type="SAV_T"/></LNodeType>
    <DOType id="ENC_T" cdc="ENC"><DA name="stVal" fc="ST" bType="Enum" type="Mod_E"/></DOType>
    <DOType id="ACT_T" cdc="ACT"><DA name="general" fc="ST" bType="BOOLEAN"/></DOType>
    <DOType id="SAV_T" cdc="SAV"><DA name="instMag" fc="MX" bType="Struct" type="AV_T"/><DA name="q" fc="MX" bType="Quality"/></DOType>
    <DAType id="AV_T"><BDA name="i" bType="INT32"/></DAType>
    <EnumType id="Mod_E"><EnumVal ord="1">on</EnumVal></EnumType>
  </DataTypeTemplates>
</SCL>"#;

#[test]
fn the_mms_client_subcommands_talk_to_a_server() {
    // The server is the crate's own association in the server role (`tests/common`), so
    // every `ied mms` client subcommand is covered in CI without a network or a device.
    let addr = common::spawn_mms_server(0);
    let id = run(&["mms", "identify", &addr]);
    assert!(id.ok, "{}", id.stderr);
    assert!(id.stdout.contains("vendor    hupe1980"), "{}", id.stdout);
    assert!(id.stdout.contains("max PDU"), "{}", id.stdout);

    let status = run(&["mms", "status", &common::spawn_mms_server(0)]);
    assert!(status.ok, "{}", status.stderr);
    assert!(status.stdout.contains("logical   state-changes-allowed"), "{}", status.stdout);
    assert!(status.stdout.contains("physical  operational"), "{}", status.stdout);
    assert!(status.stdout.contains("IEC 61850-8-1"), "{}", status.stdout);

    let browse = run(&["mms", "browse", &common::spawn_mms_server(0)]);
    assert!(browse.ok, "{}", browse.stderr);
    assert!(browse.stdout.contains("IED1LD0"), "{}", browse.stdout);
    assert!(browse.stdout.contains("MMXU1$MX$TotW$mag$f"), "{}", browse.stdout);
    assert!(browse.stdout.contains("data set LLN0$dsTrip"), "{}", browse.stdout);
    assert!(browse.stdout.contains("3 variables, 1 data sets"), "{}", browse.stdout);

    let read = run(&["mms", "read", &common::spawn_mms_server(0), "IED1LD0/MMXU1.TotW.mag.f", "--fc", "MX"]);
    assert!(read.ok, "{}", read.stderr);
    assert_eq!(read.stdout.trim(), "IED1LD0/MMXU1.TotW.mag.f = 1.5");

    let write = run(&["mms", "write", &common::spawn_mms_server(0), "IED1LD0/GGIO1.SPCSO1.stVal", "true", "--type", "bool", "--fc", "SP"]);
    assert!(write.ok, "{}", write.stderr);
    assert!(write.stdout.contains("<- true"), "{}", write.stdout);

    // A write without `--fc` is refused here rather than at the far end: the default that
    // suits a read is the one constraint a conforming server must refuse for a write.
    let no_fc = run(&["mms", "write", "127.0.0.1:1", "IED1LD0/GGIO1.SPCSO1.stVal", "true"]);
    assert!(!no_fc.ok);
    assert!(no_fc.stderr.contains("a write needs --fc"), "{}", no_fc.stderr);

    let reports = run(&["mms", "report", &common::spawn_mms_server(2), "--seconds", "2"]);
    assert!(reports.ok, "{}", reports.stderr);
    assert!(reports.stdout.contains("report 2 IED1LD0/LLN0$RP$urcb01"), "{}", reports.stdout);
}

#[test]
fn an_unreachable_server_is_an_error_not_a_panic() {
    let o = run(&["mms", "identify", "127.0.0.1:1", "--timeout", "1"]);
    assert!(!o.ok);
    assert!(o.stderr.starts_with("ied: "), "{}", o.stderr);
    assert!(!o.stderr.contains("panicked"), "{}", o.stderr);
}

#[test]
fn the_mms_report_and_control_subcommands_drive_a_control_block() {
    // `--rcb` enables the block, `--gi` asks for a general interrogation, and the report is
    // printed with its decoded fields rather than as a list of anonymous values.
    let addr = common::spawn_mms_server(0);
    let out = run(&["mms", "report", &addr, "--rcb", common::RCB_REFERENCE, "--gi", "--seconds", "2"]);
    assert!(out.ok, "{}", out.stderr);
    assert!(out.stdout.contains("enabled IED1LD0/LLN0$RP$urcb01"), "{}", out.stdout);
    assert!(out.stdout.contains("triggers: data change, quality change, GI"), "{}", out.stdout);
    assert!(out.stdout.contains("general interrogation requested"), "{}", out.stdout);
    assert!(out.stdout.contains("dataSet=IED1LD0/LLN0$dsTrip"), "{}", out.stdout);
    assert!(out.stdout.contains("2 of 2 members"), "{}", out.stdout);
    assert!(out.stdout.contains("(general interrogation)"), "{}", out.stdout);

    // `mms rcb` prints the configuration a commissioning engineer needs to see.
    let rcb = run(&["mms", "rcb", &common::spawn_mms_server(0), common::RCB_REFERENCE]);
    assert!(rcb.ok, "{}", rcb.stderr);
    assert!(rcb.stdout.contains("kind       unbuffered (RP)"), "{}", rcb.stdout);
    assert!(rcb.stdout.contains("DatSet     IED1LD0/LLN0$dsTrip"), "{}", rcb.stdout);
    assert!(rcb.stdout.contains("ConfRev    3"), "{}", rcb.stdout);

    // A direct control.
    let ctl = run(&["mms", "control", &common::spawn_mms_server(0), common::CONTROL_REFERENCE, "true", "--type", "bool"]);
    assert!(ctl.ok, "{}", ctl.stderr);
    assert!(ctl.stdout.contains("(accepted)"), "{}", ctl.stdout);
}

#[test]
fn a_refused_control_is_reported_with_its_cause() {
    let addr = common::spawn_mms_server_with(common::ServerBehaviour {
        enhanced_control: true,
        refuse_control: Some(iec61850_kit::client::AddCause::BlockedByInterlocking),
        ..common::ServerBehaviour::default()
    });
    let o = run(&["mms", "control", &addr, common::CONTROL_REFERENCE, "true", "--type", "bool", "--model", "direct-enhanced", "--timeout", "3"]);
    assert!(!o.ok, "a refused control must not exit zero: {}", o.stdout);
    assert!(o.stderr.contains("BlockedByInterlocking"), "{}", o.stderr);
    assert!(!o.stderr.contains("panicked"), "{}", o.stderr);
}

#[test]
fn the_file_log_type_and_setting_group_subcommands_talk_to_a_server() {
    // Each subcommand gets its own server: one association per client, and the harness
    // closes it when the client releases.
    let files = run(&["mms", "files", &common::spawn_mms_server(0)]);
    assert!(files.ok, "{files:?}", files = files.stderr);
    assert!(files.stdout.contains(common::FILE_REFERENCE), "{}", files.stdout);
    assert!(files.stdout.contains("1 file(s)"), "{}", files.stdout);

    let out = tempdir("get").join("rec0001.cfg");
    let get = run(&["mms", "get", &common::spawn_mms_server(0), common::FILE_REFERENCE, out.to_str().unwrap()]);
    assert!(get.ok, "{get:?}", get = get.stderr);
    assert_eq!(std::fs::read(&out).unwrap(), common::FILE_CONTENTS, "the file arrived whole");

    let ty = run(&["mms", "type", &common::spawn_mms_server(0), "IED1LD0/CSWI1.Pos.Oper", "--fc", "CO"]);
    assert!(ty.ok, "{ty:?}", ty = ty.stderr);
    for component in ["ctlVal", "origin", "ctlNum", "T", "Test", "Check"] {
        assert!(ty.stdout.contains(component), "`{component}` missing from:\n{}", ty.stdout);
    }

    let log = run(&["mms", "log", &common::spawn_mms_server(0), common::LOG_REFERENCE, "--lcb", common::LCB_REFERENCE]);
    assert!(log.ok, "{log:?}", log = log.stderr);
    assert!(log.stdout.contains("power up"), "{}", log.stdout);
    assert!(log.stdout.contains("2 entr"), "{}", log.stdout);

    let sg = run(&["mms", "sg", &common::spawn_mms_server(0), common::SGCB_REFERENCE, "--activate", "2"]);
    assert!(sg.ok, "{sg:?}", sg = sg.stderr);
    assert!(sg.stdout.contains("group 2 activated") && sg.stdout.contains("ActSG   2"), "{}", sg.stdout);
}

#[test]
fn sim_serves_an_engineering_file_as_a_real_server() {
    use std::io::{BufRead, BufReader};

    // The whole design in one command: the SCL file is the configuration, so a simulator is
    // the file plus a socket. `ied mms browse` is then run against it — one binary talking to
    // itself over a real association, with no device and no generated model.
    let dir = tempdir("sim");
    let scl = dir.join("relay.icd");
    std::fs::write(&scl, SIM_RELAY).expect("write the file");

    let mut child = Command::new(ied()).args(["sim", scl.to_str().unwrap(), "--port", "0"]).stdout(std::process::Stdio::piped()).spawn().expect("spawn sim");
    // The first line names the address it actually bound, which is how a test asks for port
    // zero and still finds it.
    let stdout = child.stdout.take().expect("stdout");
    let mut lines = BufReader::new(stdout).lines();
    let first = lines.next().expect("a line").expect("readable");
    let addr = first.split_whitespace().nth(2).expect("the address").to_string();
    assert!(first.starts_with("IED1 on "), "{first}");

    let browse = run(&["mms", "browse", &addr]);
    let read = run(&["mms", "read", &addr, "IED1LD0/MMXU1.TotW.mag.f", "--fc", "MX"]);
    let ty = run(&["mms", "type", &addr, "IED1LD0/PTRC1.Tr", "--fc", "ST"]);
    let _ = child.kill();
    let _ = child.wait();

    assert!(browse.ok, "{}", browse.stderr);
    assert!(browse.stdout.contains("IED1LD0"), "{}", browse.stdout);
    assert!(browse.stdout.contains("PTRC1$ST$Tr$general"), "{}", browse.stdout);
    assert!(read.ok && read.stdout.contains("IED1LD0/MMXU1.TotW.mag.f"), "{}{}", read.stdout, read.stderr);
    assert!(ty.ok && ty.stdout.contains("general"), "{}{}", ty.stdout, ty.stderr);
}

/// `--log-db` keeps the logs in a file: the server opens it before it serves, and a log read on
/// the running server is answered out of it.
#[test]
fn sim_keeps_its_log_in_the_file_it_is_given() {
    use std::io::{BufRead, BufReader};

    let dir = tempdir("sim-log");
    let scl = dir.join("relay.icd");
    std::fs::write(&scl, include_str!("fixtures/relay.icd")).expect("write the file");
    let db = dir.join("relay-log.redb");

    let mut child = Command::new(ied())
        .args(["sim", scl.to_str().unwrap(), "--port", "0", "--log-db", db.to_str().unwrap()])
        .stdout(std::process::Stdio::piped())
        .spawn()
        .expect("spawn sim");
    let stdout = child.stdout.take().expect("stdout");
    let lines: Vec<String> = BufReader::new(stdout).lines().map_while(Result::ok).take_while(|l| !l.starts_with("serving")).collect();
    let addr = lines.iter().find(|l| l.contains(" on ")).and_then(|l| l.split_whitespace().nth(2)).expect("the address").to_string();
    let log = run(&["mms", "log", &addr, "IED1LD0/LLN0$GeneralLog", "--lcb", "IED1LD0/LLN0.lcb01"]);
    let _ = child.kill();
    let _ = child.wait();

    assert!(lines.iter().any(|l| l.contains("log in") && l.contains("relay-log.redb")), "{lines:?}");
    assert!(db.is_file(), "the log file exists");
    assert!(log.ok, "{}{}", log.stdout, log.stderr);
}

const SIM_RELAY: &str = r#"<?xml version="1.0"?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" version="2007" revision="B" release="4">
  <Header id="sim"/>
  <IED name="IED1"><AccessPoint name="P1"><Server><LDevice inst="LD0">
    <LN0 lnClass="LLN0" inst="" lnType="LLN0_T"/>
    <LN lnClass="PTRC" inst="1" prefix="" lnType="PTRC_T"/>
    <LN lnClass="MMXU" inst="1" prefix="" lnType="MMXU_T"/>
  </LDevice></Server></AccessPoint></IED>
  <DataTypeTemplates>
    <LNodeType id="LLN0_T" lnClass="LLN0"/>
    <LNodeType id="PTRC_T" lnClass="PTRC"><DO name="Tr" type="ACT_T"/></LNodeType>
    <LNodeType id="MMXU_T" lnClass="MMXU"><DO name="TotW" type="MV_T"/></LNodeType>
    <DOType id="ACT_T" cdc="ACT"><DA name="general" fc="ST" bType="BOOLEAN"/><DA name="q" fc="ST" bType="Quality"/></DOType>
    <DOType id="MV_T" cdc="MV"><DA name="mag" fc="MX" bType="Struct" type="AV_T"/></DOType>
    <DAType id="AV_T"><BDA name="f" bType="FLOAT32"/></DAType>
  </DataTypeTemplates>
</SCL>"#;

// --- `ied scl edit` ---------------------------------------------------------------------------

const EDIT_FIXTURE: &str = include_str!("fixtures/edit.scd");

/// A scratch directory per test, so parallel tests do not share files.
fn scratch_file(name: &str, text: &str) -> (PathBuf, String) {
    let dir = std::env::temp_dir().join(format!("ied-scl-edit-{}-{name}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let file = dir.join("bay.scd");
    std::fs::write(&file, text).unwrap();
    (dir, file.to_str().unwrap().to_string())
}

#[test]
fn scl_edit_renames_to_stdout_and_leaves_the_file_alone() {
    let (dir, file) = scratch_file("stdout", EDIT_FIXTURE);
    let o = run(&["scl", "edit", &file, "rename-ied", "Pub", "Bay1"]);
    assert!(o.ok, "{}", o.stderr);
    assert!(o.stderr.contains("5 change(s)"), "{}", o.stderr);
    assert_eq!(o.stdout.lines().count(), EDIT_FIXTURE.lines().count());
    assert!(o.stdout.contains("<IED name=\"Bay1\">") && o.stdout.contains("<Val>Bay1LD0/LLN0$GO$gcb1</Val>"));
    assert_eq!(std::fs::read_to_string(&file).unwrap(), EDIT_FIXTURE);
    std::fs::remove_dir_all(dir).ok();
}

#[test]
fn scl_edit_refuses_input_above_the_size_limit() {
    let (dir, file) = scratch_file("size", EDIT_FIXTURE);
    let o = run(&["scl", "edit", &file, "rename-ied", "Pub", "Bay1", "--max-size", "100"]);
    assert!(!o.ok && o.stderr.contains("limit") && o.stdout.is_empty(), "{}", o.stderr);
    std::fs::remove_dir_all(dir).ok();
}

#[test]
fn scl_edit_refuses_an_edit_that_adds_a_validation_error_unless_forced() {
    let second = "\t\t\t\t<GSE ldInst=\"LD0\" cbName=\"gcb2\">\n\t\t\t\t\t<Address>\n\t\t\t\t\t\t<P type=\"MAC-Address\">01-0C-CD-01-00-01</P>\n\t\t\t\t\t\t<P type=\"APPID\">0002</P>\n\t\t\t\t\t</Address>\n\t\t\t\t</GSE>\n\t\t\t</ConnectedAP>\n\t\t\t<ConnectedAP iedName=\"Sub\"";
    let text = EDIT_FIXTURE.replacen("\t\t\t</ConnectedAP>\n\t\t\t<ConnectedAP iedName=\"Sub\"", second, 1).replacen(
        "\t\t\t\t\t\t</GSEControl>",
        "\t\t\t\t\t\t</GSEControl>\n\t\t\t\t\t\t<GSEControl name=\"gcb2\" datSet=\"ds1\" appID=\"g2\" confRev=\"1\"/>",
        1,
    );
    let (dir, file) = scratch_file("finding", &text);
    let out = dir.join("out.scd");
    let out = out.to_str().unwrap();
    let o = run(&["scl", "edit", &file, "gse", "Pub", "LD0", "gcb2", "--appid", "0001", "-o", out]);
    assert!(!o.ok && o.stderr.contains("DuplicateStream"), "{}", o.stderr);
    assert!(!std::path::Path::new(out).exists(), "nothing is written");
    let o = run(&["scl", "edit", &file, "gse", "Pub", "LD0", "gcb2", "--appid", "0001", "-o", out, "--force"]);
    assert!(o.ok, "{}", o.stderr);
    // The whole address is written: the VLAN fields the block lacked are added after APPID.
    let written = std::fs::read_to_string(out).unwrap();
    assert!(
        written
            .contains("<P type=\"APPID\">0001</P>\n\t\t\t\t\t\t<P type=\"VLAN-ID\">000</P>\n\t\t\t\t\t\t<P type=\"VLAN-PRIORITY\">4</P>\n\t\t\t\t\t</Address>"),
        "{written}"
    );
    std::fs::remove_dir_all(dir).ok();
}

#[test]
fn scl_edit_in_place_is_atomic() {
    let (dir, file) = scratch_file("inplace", EDIT_FIXTURE);
    let o = run(&["scl", "edit", &file, "ap-address", "Pub", "AP1", "IP", "10.0.0.9", "--in-place"]);
    assert!(o.ok, "{}", o.stderr);
    assert!(std::fs::read_to_string(&file).unwrap().contains("<P type=\"IP\">10.0.0.9</P>"));
    let left: Vec<_> = std::fs::read_dir(&dir).unwrap().flatten().map(|e| e.file_name()).collect();
    assert_eq!(left.len(), 1, "no temporary file is left behind: {left:?}");

    // A write that cannot happen leaves the original byte for byte.
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let before = std::fs::read(&file).unwrap();
        std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o500)).unwrap();
        let o = run(&["scl", "edit", &file, "ap-address", "Pub", "AP1", "IP", "10.0.0.10", "--in-place"]);
        std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
        assert!(!o.ok && o.stderr.contains("unchanged"), "{}", o.stderr);
        assert_eq!(std::fs::read(&file).unwrap(), before);
    }
    std::fs::remove_dir_all(dir).ok();
}

#[test]
fn scl_edit_dry_run_prints_a_diff_and_writes_nothing() {
    let (dir, file) = scratch_file("dry", EDIT_FIXTURE);
    let o = run(&["scl", "edit", &file, "rename-ied", "Sub", "Hmi", "--dry-run", "--in-place"]);
    assert!(o.ok, "{}", o.stderr);
    let removed = o.stdout.lines().filter(|l| l.starts_with('-') && !l.starts_with("---")).count();
    let added = o.stdout.lines().filter(|l| l.starts_with('+') && !l.starts_with("+++")).count();
    assert_eq!((removed, added), (4, 4), "{}", o.stdout);
    assert!(o.stdout.contains("+\t\t\t\t\t\t\t<IEDName>Hmi</IEDName>"), "{}", o.stdout);
    assert_eq!(std::fs::read_to_string(&file).unwrap(), EDIT_FIXTURE);
    std::fs::remove_dir_all(dir).ok();
}

#[test]
fn scl_edit_records_who_and_why_in_the_header_history() {
    let (dir, file) = scratch_file("history", EDIT_FIXTURE);
    let o = run(&["scl", "edit", &file, "ap-address", "Pub", "AP1", "IP", "10.0.0.9", "--who", "alice", "--why", "new subnet"]);
    assert!(o.ok, "{}", o.stderr);
    let items: Vec<_> = o.stdout.lines().filter(|l| l.contains("<Hitem ")).collect();
    assert_eq!(items.len(), 1, "{}", o.stdout);
    assert!(
        items[0].contains("version=\"1\" revision=\"A\"") && items[0].contains("who=\"alice\" what=\"set IP of Pub/AP1\" why=\"new subnet\""),
        "{}",
        items[0]
    );
    std::fs::remove_dir_all(dir).ok();
}

#[test]
fn scl_edit_reports_a_failed_edit_and_writes_nothing() {
    let (dir, file) = scratch_file("fail", EDIT_FIXTURE);
    for args in [vec!["rename-ied", "Nobody", "X"], vec!["rename-ied", "Pub", "Sub"], vec!["frobnicate"], vec!["rename-ied", "Pub"]] {
        let mut full = vec!["scl", "edit", file.as_str()];
        full.extend(&args);
        full.push("--in-place");
        let o = run(&full);
        assert!(!o.ok, "{args:?} should fail");
        assert_eq!(std::fs::read_to_string(&file).unwrap(), EDIT_FIXTURE, "{args:?}");
    }
    std::fs::remove_dir_all(dir).ok();
}

/// `ied mms put` sends a file to `ied sim --writable`, which stores it.
#[test]
fn put_sends_a_file_to_a_writable_simulator() {
    use std::io::{BufRead, BufReader};

    let dir = tempdir("put");
    let scl = dir.join("relay.icd");
    std::fs::write(&scl, include_str!("fixtures/relay.icd")).expect("write the file");
    let store = dir.join("store");
    std::fs::create_dir_all(&store).unwrap();
    let local = dir.join("settings.xml");
    std::fs::write(&local, b"<settings/>").unwrap();

    let mut child = Command::new(ied())
        .args(["sim", scl.to_str().unwrap(), "--port", "0", "--files", store.to_str().unwrap(), "--writable", "--password", "s3cret"])
        .stdout(std::process::Stdio::piped())
        .spawn()
        .expect("spawn sim");
    let stdout = child.stdout.take().expect("stdout");
    let lines: Vec<String> = BufReader::new(stdout).lines().map_while(Result::ok).take_while(|l| !l.starts_with("serving")).collect();
    let addr = lines.iter().find(|l| l.contains(" on ")).and_then(|l| l.split_whitespace().nth(2)).expect("the address").to_string();
    let refused = run(&["mms", "put", &addr, local.to_str().unwrap(), "settings.xml"]);
    let sent = run(&["mms", "put", &addr, local.to_str().unwrap(), "settings.xml", "--password", "s3cret"]);
    let _ = child.kill();
    let _ = child.wait();

    assert!(!refused.ok, "no password, no association: {}", refused.stdout);
    assert!(sent.ok, "{}{}", sent.stdout, sent.stderr);
    assert_eq!(std::fs::read(store.join("settings.xml")).unwrap(), b"<settings/>");
}

/// `ied sim` refuses a `--files` directory that would expose the served SCL file or the log.
#[test]
fn sim_refuses_a_file_store_that_would_expose_its_own_files() {
    let dir = tempdir("sim-guard");
    let scl = dir.join("relay.icd");
    std::fs::write(&scl, include_str!("fixtures/relay.icd")).unwrap();
    let store = dir.join("store");
    std::fs::create_dir_all(&store).unwrap();

    let o = run(&["sim", scl.to_str().unwrap(), "--port", "0", "--files", dir.to_str().unwrap()]);
    assert!(!o.ok && o.stderr.contains("would expose the SCL file"), "{}", o.stderr);

    let log = store.join("relay.redb");
    let o = run(&["sim", scl.to_str().unwrap(), "--port", "0", "--files", store.to_str().unwrap(), "--log-db", log.to_str().unwrap()]);
    assert!(!o.ok && o.stderr.contains("would expose the log"), "{}", o.stderr);
    assert!(!log.exists(), "refused before anything is opened");
    std::fs::remove_dir_all(&dir).ok();
}