winrm-rs 1.0.0

Async WinRM (WS-Management) client for Rust with NTLMv2, Basic, Kerberos, and Certificate authentication
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
//! Response parsers for shell IDs, command IDs, and output streams.

use super::namespaces::COMMAND_STATE_DONE;
use crate::error::SoapError;

/// Parsed output from a single WinRM Receive response.
///
/// Each Receive poll may return partial output. Callers should accumulate
/// [`stdout`](Self::stdout) and [`stderr`](Self::stderr) across polls and
/// stop when [`done`](Self::done) is `true`.
pub struct ReceiveOutput {
    /// Decoded stdout bytes from this poll (may be empty).
    pub stdout: Vec<u8>,
    /// Decoded stderr bytes from this poll (may be empty).
    pub stderr: Vec<u8>,
    /// Process exit code, present only when the command has finished.
    pub exit_code: Option<i32>,
    /// `true` when the server reports `CommandState/Done`, indicating no more
    /// output will follow.
    pub done: bool,
}

/// Extract the `ShellId` from a Create Shell response body.
///
/// Searches for a `<ShellId>` element (with or without namespace prefix) and
/// returns its text content. Returns [`SoapError::MissingElement`] if not found.
#[allow(unreachable_pub)] // re-exported under the `__internal` feature for fuzz targets
pub fn parse_shell_id(xml: &str) -> Result<String, SoapError> {
    extract_element_text(xml, "ShellId").ok_or_else(|| SoapError::MissingElement("ShellId".into()))
}

/// Extract the `CommandId` from an Execute Command response body.
///
/// Searches for a `<CommandId>` element (with or without namespace prefix) and
/// returns its text content. Returns [`SoapError::MissingElement`] if not found.
#[allow(unreachable_pub)] // re-exported under the `__internal` feature for fuzz targets
pub fn parse_command_id(xml: &str) -> Result<String, SoapError> {
    extract_element_text(xml, "CommandId")
        .ok_or_else(|| SoapError::MissingElement("CommandId".into()))
}

/// Parse a Receive response to extract stdout, stderr, exit code, and completion state.
///
/// Decodes base64 `<rsp:Stream>` elements for stdout and stderr, checks the
/// `CommandState` for the `Done` URI, and extracts the `ExitCode` if present.
/// Returns [`SoapError::ParseError`] if a stream contains invalid base64, or
/// a [`SoapError::Fault`] if the response body contains a SOAP fault.
#[allow(unreachable_pub)] // re-exported under the `__internal` feature for fuzz targets
pub fn parse_receive_output(xml: &str) -> Result<ReceiveOutput, SoapError> {
    use base64::Engine;
    use base64::engine::general_purpose::STANDARD as B64;

    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    let mut exit_code: Option<i32> = None;
    let mut done = false;

    // Parse Stream elements for stdout/stderr
    // Format: <rsp:Stream Name="stdout" CommandId="...">base64data</rsp:Stream>
    for (name, data) in extract_streams(xml) {
        let decoded = B64
            .decode(data.trim_ascii())
            .map_err(|e| SoapError::ParseError(format!("base64 decode: {e}")))?;
        match name.as_str() {
            "stdout" => stdout.extend_from_slice(&decoded),
            "stderr" => {
                if decoded.starts_with(b"#< CLIXML") {
                    stderr.extend_from_slice(&parse_clixml(&decoded));
                } else {
                    stderr.extend_from_slice(&decoded);
                }
            }
            _ => {}
        }
    }

    // Check CommandState for Done
    if xml.contains(COMMAND_STATE_DONE) {
        done = true;
    }

    // Extract ExitCode if present
    if let Some(code_str) = extract_element_text(xml, "ExitCode") {
        exit_code = code_str.parse().ok();
    }

    // Check for SOAP faults
    if let Some(fault) = extract_soap_fault(xml) {
        return Err(fault);
    }

    Ok(ReceiveOutput {
        stdout,
        stderr,
        exit_code,
        done,
    })
}

/// Parse a WS-Enumeration Enumerate response.
///
/// Returns the XML items (raw text between `<w:Items>` tags) and an optional
/// `EnumerationContext` for continuation via Pull.
pub(crate) fn parse_enumerate_response(xml: &str) -> Result<(String, Option<String>), SoapError> {
    check_soap_fault(xml)?;

    // Extract items — may be inside <w:Items>, <wsen:Items>, or <n:Items>
    let items = extract_element_text(xml, "Items").unwrap_or_default();

    // Extract EnumerationContext for Pull continuation
    let context = extract_element_text(xml, "EnumerationContext");

    // Check for EndOfSequence — means no more items
    let end_of_seq = xml.contains("EndOfSequence");

    let context = if end_of_seq { None } else { context };

    Ok((items, context))
}

/// Check a SOAP response for fault elements and return an error if found.
///
/// Scans the XML for `<Fault>` or `<s:Fault>` elements and extracts the
/// fault code and reason text. Returns `Ok(())` if no fault is present.
#[allow(unreachable_pub)] // re-exported under the `__internal` feature for fuzz targets
pub fn check_soap_fault(xml: &str) -> Result<(), SoapError> {
    if let Some(fault) = extract_soap_fault(xml) {
        return Err(fault);
    }
    Ok(())
}

/// Parse PowerShell CLIXML stderr into human-readable text.
///
/// PowerShell wraps errors in CLIXML format (`#< CLIXML\r\n<Objs>...`).
/// This function extracts the text from `<S S="Error">` tags and decodes
/// CLIXML escape sequences like `_x000D_` (CR) and `_x000A_` (LF).
fn parse_clixml(data: &[u8]) -> Vec<u8> {
    let text = String::from_utf8_lossy(data);
    let mut result = String::new();

    // Extract content from <S S="Error">...</S> tags
    let error_tag = "<S S=\"Error\">";
    let close_tag = "</S>";
    let mut search_from = 0;

    while let Some(start) = text[search_from..].find(error_tag) {
        let content_start = search_from + start + error_tag.len();
        if let Some(end) = text[content_start..].find(close_tag) {
            let fragment = &text[content_start..content_start + end];
            result.push_str(fragment);
            search_from = content_start + end + close_tag.len();
        } else {
            break;
        }
    }

    // Decode CLIXML escape sequences
    let result = result
        .replace("_x000D_", "\r")
        .replace("_x000A_", "\n")
        .replace("_x0009_", "\t")
        .replace("_x001B_", "\x1b");

    result.into_bytes()
}

// --- Helpers ---

/// Simple element text extraction by local name (namespace-agnostic).
///
/// Finds elements like `<prefix:Element>text</prefix:Element>` or `<Element>text</Element>`
/// regardless of namespace prefix.
fn extract_element_text(xml: &str, element: &str) -> Option<String> {
    // Search for ":Element>" or "<Element>" to handle any namespace prefix
    let suffixed = format!(":{element}>");
    let bare_open = format!("<{element}>");

    let mut search_from = 0;
    while search_from < xml.len() {
        let region = &xml[search_from..];

        // Find next occurrence of the element (with or without prefix)
        let (tag_content_start, ns_prefix) = if let Some(pos) = region.find(&suffixed) {
            // Found ":Element>" -- walk back to find the '<'
            let abs_pos = search_from + pos;
            let before = &xml[..abs_pos];
            let lt = before.rfind('<')?;
            // Make sure this is an opening tag, not a closing tag
            if xml[lt..].starts_with("</") {
                search_from = abs_pos + suffixed.len();
                continue;
            }
            let prefix = &xml[lt + 1..abs_pos];
            (abs_pos + suffixed.len(), Some(prefix.to_string()))
        } else if let Some(pos) = region.find(&bare_open) {
            (search_from + pos + bare_open.len(), None)
        } else {
            return None;
        };

        // Build closing tag pattern
        let close_tag = match &ns_prefix {
            Some(p) => format!("</{p}:{element}>"),
            None => format!("</{element}>"),
        };

        if let Some(end) = xml[tag_content_start..].find(&close_tag) {
            let text = xml[tag_content_start..tag_content_start + end].trim();
            if !text.is_empty() {
                return Some(text.to_string());
            }
        }

        search_from = tag_content_start;
    }
    None
}

/// Extract all Stream elements with their Name attribute and base64 content.
fn extract_streams(xml: &str) -> Vec<(String, String)> {
    let mut streams = Vec::new();
    let mut search_from = 0;

    let stream_tags = ["<rsp:Stream ", "<Stream "];

    while search_from < xml.len() {
        let found = stream_tags
            .iter()
            .filter_map(|tag| {
                xml[search_from..]
                    .find(tag)
                    .map(|pos| (search_from + pos, *tag))
            })
            .min_by_key(|(pos, _)| *pos);

        let Some((tag_start, _)) = found else {
            break;
        };

        let tag_region = &xml[tag_start..];

        // Find the end of the opening tag
        let Some(tag_end) = tag_region.find('>') else {
            break;
        };
        let opening_tag = &tag_region[..tag_end];

        // Extract Name attribute
        let name = extract_attribute(opening_tag, "Name").unwrap_or_default();

        // Find content between > and closing tag
        let content_start = tag_start + tag_end + 1;

        let close_tags = ["</rsp:Stream>", "</Stream>"];
        let close_pos = close_tags
            .iter()
            .filter_map(|close| xml[content_start..].find(close))
            .min();

        if let Some(end_offset) = close_pos {
            let content = &xml[content_start..content_start + end_offset];
            if !content.trim_ascii().is_empty() {
                streams.push((name, content.to_string()));
            }
            search_from = content_start + end_offset + 1;
        } else {
            break;
        }
    }

    streams
}

/// Extract an XML attribute value from a tag string.
fn extract_attribute(tag: &str, attr_name: &str) -> Option<String> {
    let pattern = format!("{attr_name}=\"");
    let start = tag.find(&pattern)? + pattern.len();
    let end = tag[start..].find('"')? + start;
    Some(tag[start..end].to_string())
}

/// Extract a SOAP fault from the response, if present.
fn extract_soap_fault(xml: &str) -> Option<SoapError> {
    // Check for Fault element
    let has_fault = xml.contains(":Fault>") || xml.contains("<Fault>");
    if !has_fault {
        return None;
    }

    // Prefer the subcode value (e.g. `w:TimedOut`) over the top-level
    // SOAP code (`s:Receiver`/`s:Sender`) because it carries the
    // actionable WS-Management error.
    let code = extract_subcode_value(xml)
        .or_else(|| extract_element_text(xml, "Value"))
        .or_else(|| extract_element_text(xml, "faultcode"))
        .unwrap_or_else(|| "unknown".into());
    let reason = extract_element_text(xml, "Text")
        .or_else(|| extract_element_text(xml, "Message"))
        .or_else(|| extract_element_text(xml, "faultstring"))
        .unwrap_or_else(|| "SOAP fault".into());

    Some(SoapError::Fault { code, reason })
}

/// Extract the `<s:Subcode><s:Value>…</s:Value></s:Subcode>` text,
/// falling back to the WSManFault `Code` attribute if present.
fn extract_subcode_value(xml: &str) -> Option<String> {
    // Look for <Subcode>…<Value>X</Value>…</Subcode>
    let sub_start = xml.find("Subcode>")?;
    let rest = &xml[sub_start..];
    let inner = extract_element_text(rest, "Value")?;
    if inner.is_empty() {
        return None;
    }
    Some(inner)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_shell_id_from_response() {
        let xml = r"<s:Envelope><s:Body><rsp:Shell>
            <rsp:ShellId>ABC-DEF-123</rsp:ShellId>
        </rsp:Shell></s:Body></s:Envelope>";
        let id = parse_shell_id(xml).unwrap();
        assert_eq!(id, "ABC-DEF-123");
    }

    #[test]
    fn parse_command_id_from_response() {
        let xml = r"<s:Envelope><s:Body><rsp:CommandResponse>
            <rsp:CommandId>CMD-456</rsp:CommandId>
        </rsp:CommandResponse></s:Body></s:Envelope>";
        let id = parse_command_id(xml).unwrap();
        assert_eq!(id, "CMD-456");
    }

    #[test]
    fn parse_receive_output_with_streams() {
        // "hello" base64 = "aGVsbG8="
        // "err" base64 = "ZXJy"
        let xml = r#"<s:Envelope><s:Body><rsp:ReceiveResponse>
            <rsp:Stream Name="stdout" CommandId="C1">aGVsbG8=</rsp:Stream>
            <rsp:Stream Name="stderr" CommandId="C1">ZXJy</rsp:Stream>
            <rsp:CommandState CommandId="C1" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
                <rsp:ExitCode>0</rsp:ExitCode>
            </rsp:CommandState>
        </rsp:ReceiveResponse></s:Body></s:Envelope>"#;

        let output = parse_receive_output(xml).unwrap();
        assert_eq!(output.stdout, b"hello");
        assert_eq!(output.stderr, b"err");
        assert_eq!(output.exit_code, Some(0));
        assert!(output.done);
    }

    #[test]
    fn parse_receive_output_not_done() {
        let xml = r#"<s:Envelope><s:Body><rsp:ReceiveResponse>
            <rsp:Stream Name="stdout" CommandId="C1">dGVzdA==</rsp:Stream>
            <rsp:CommandState CommandId="C1" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Running"/>
        </rsp:ReceiveResponse></s:Body></s:Envelope>"#;

        let output = parse_receive_output(xml).unwrap();
        assert_eq!(output.stdout, b"test");
        assert!(!output.done);
        assert!(output.exit_code.is_none());
    }

    #[test]
    fn detect_soap_fault() {
        let xml = r"<s:Envelope><s:Body><s:Fault>
            <s:Code><s:Value>s:Receiver</s:Value></s:Code>
            <s:Reason><s:Text>Access denied</s:Text></s:Reason>
        </s:Fault></s:Body></s:Envelope>";

        let result = check_soap_fault(xml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        match err {
            SoapError::Fault { code, reason } => {
                assert_eq!(code, "s:Receiver");
                assert_eq!(reason, "Access denied");
            }
            _ => panic!("expected SoapFault"),
        }
    }

    #[test]
    fn extract_attribute_works() {
        let tag = r#"<rsp:Stream Name="stdout" CommandId="C1""#;
        assert_eq!(extract_attribute(tag, "Name"), Some("stdout".into()));
        assert_eq!(extract_attribute(tag, "CommandId"), Some("C1".into()));
        assert_eq!(extract_attribute(tag, "Missing"), None);
    }

    #[test]
    fn parse_receive_output_with_soap_fault() {
        let xml = r"<s:Envelope><s:Body>
            <s:Fault>
                <s:Code><s:Value>s:Sender</s:Value></s:Code>
                <s:Reason><s:Text>Invalid input</s:Text></s:Reason>
            </s:Fault>
        </s:Body></s:Envelope>";
        let result = parse_receive_output(xml);
        assert!(result.is_err());
    }

    #[test]
    fn check_soap_fault_no_fault() {
        let xml = r"<s:Envelope><s:Body><Data>ok</Data></s:Body></s:Envelope>";
        assert!(check_soap_fault(xml).is_ok());
    }

    #[test]
    fn extract_element_text_closing_tag_first() {
        // Test where closing tag appears before opening tag
        let xml = r"</rsp:ShellId><rsp:ShellId>ABC</rsp:ShellId>";
        let result = parse_shell_id(xml).unwrap();
        assert_eq!(result, "ABC");
    }

    #[test]
    fn extract_element_text_empty_content() {
        let xml = r"<rsp:ShellId></rsp:ShellId>";
        assert!(parse_shell_id(xml).is_err());
    }

    #[test]
    fn extract_streams_empty_stream() {
        // Stream with empty content should be skipped
        let xml = r#"<rsp:Stream Name="stdout" CommandId="C1"></rsp:Stream>"#;
        let streams = extract_streams(xml);
        assert!(streams.is_empty());
    }

    #[test]
    fn parse_receive_output_non_base64_stream() {
        let xml = r#"<s:Envelope><s:Body><rsp:ReceiveResponse>
            <rsp:Stream Name="stdout" CommandId="C1">!!!not-base64!!!</rsp:Stream>
        </rsp:ReceiveResponse></s:Body></s:Envelope>"#;
        let result = parse_receive_output(xml);
        assert!(result.is_err());
    }

    #[test]
    fn soap_error_display() {
        let e = SoapError::MissingElement("ShellId".into());
        assert_eq!(format!("{e}"), "missing element: ShellId");

        let e = SoapError::ParseError("bad data".into());
        assert_eq!(format!("{e}"), "parse error: bad data");

        let e = SoapError::Fault {
            code: "s:Sender".into(),
            reason: "bad".into(),
        };
        assert_eq!(format!("{e}"), "SOAP fault [s:Sender]: bad");
    }

    #[test]
    fn extract_streams_bare_tag() {
        // Test with bare <Stream> (no namespace prefix)
        let xml = r#"<Stream Name="stdout">aGVsbG8=</Stream>"#;
        let streams = extract_streams(xml);
        assert_eq!(streams.len(), 1);
        assert_eq!(streams[0].0, "stdout");
    }

    #[test]
    fn detect_soap_fault_with_legacy_tags() {
        let xml = r"<Fault><faultcode>s:Client</faultcode><faultstring>oops</faultstring></Fault>";
        let result = check_soap_fault(xml);
        assert!(result.is_err());
    }

    #[test]
    fn extract_streams_bare_before_namespaced() {
        // Both <Stream and <rsp:Stream in same XML, bare tag appears first.
        let xml =
            r#"<Stream Name="stdout">aGVsbG8=</Stream><rsp:Stream Name="stderr">ZXJy</rsp:Stream>"#;
        let streams = extract_streams(xml);
        assert_eq!(streams.len(), 2);
        assert_eq!(streams[0].0, "stdout");
        assert_eq!(streams[1].0, "stderr");
    }

    #[test]
    fn extract_streams_namespaced_before_bare_close() {
        // <rsp:Stream> with content closed by </Stream> (bare close appearing before </rsp:Stream>)
        let xml = r#"<rsp:Stream Name="stdout">dGVzdA==</Stream></rsp:Stream>"#;
        let streams = extract_streams(xml);
        assert_eq!(streams.len(), 1);
        assert_eq!(streams[0].0, "stdout");
    }

    #[test]
    fn parse_receive_output_no_exit_code() {
        let xml = r#"<s:Envelope><s:Body><rsp:ReceiveResponse>
            <rsp:CommandState CommandId="C1" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done"/>
        </rsp:ReceiveResponse></s:Body></s:Envelope>"#;
        let output = parse_receive_output(xml).unwrap();
        assert!(output.done);
        assert!(output.exit_code.is_none());
        assert!(output.stdout.is_empty());
    }

    // --- Mutant-killing tests ---

    #[test]
    fn extract_element_text_skips_empty_finds_second() {
        let xml = r"<rsp:ShellId></rsp:ShellId><rsp:ShellId>FOUND</rsp:ShellId>";
        assert_eq!(parse_shell_id(xml).unwrap(), "FOUND");
    }

    #[test]
    fn extract_element_bare_element() {
        let xml = r"<ShellId>BARE-ID</ShellId>";
        assert_eq!(parse_shell_id(xml).unwrap(), "BARE-ID");
    }

    #[test]
    fn parse_receive_output_multiple_stdout_chunks() {
        // "hel" = aGVs, "lo" = bG8=
        let xml = r#"<s:Envelope><s:Body><rsp:ReceiveResponse>
            <rsp:Stream Name="stdout" CommandId="C1">aGVs</rsp:Stream>
            <rsp:Stream Name="stdout" CommandId="C1">bG8=</rsp:Stream>
            <rsp:CommandState CommandId="C1" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
                <rsp:ExitCode>0</rsp:ExitCode>
            </rsp:CommandState>
        </rsp:ReceiveResponse></s:Body></s:Envelope>"#;
        let output = parse_receive_output(xml).unwrap();
        assert_eq!(output.stdout, b"hello");
    }

    #[test]
    fn parse_receive_output_interleaved_streams() {
        // "AB" = QUI=, "err" = ZXJy, "CD" = Q0Q=
        let xml = r#"<s:Envelope><s:Body><rsp:ReceiveResponse>
            <rsp:Stream Name="stdout" CommandId="C1">QUI=</rsp:Stream>
            <rsp:Stream Name="stderr" CommandId="C1">ZXJy</rsp:Stream>
            <rsp:Stream Name="stdout" CommandId="C1">Q0Q=</rsp:Stream>
            <rsp:CommandState CommandId="C1" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
                <rsp:ExitCode>1</rsp:ExitCode>
            </rsp:CommandState>
        </rsp:ReceiveResponse></s:Body></s:Envelope>"#;
        let output = parse_receive_output(xml).unwrap();
        assert_eq!(output.stdout, b"ABCD");
        assert_eq!(output.stderr, b"err");
        assert_eq!(output.exit_code, Some(1));
    }

    #[test]
    fn extract_element_text_multiple_closing_before_opening() {
        let xml = r"</rsp:CommandId></rsp:CommandId><rsp:CommandId>REAL-CMD</rsp:CommandId>";
        assert_eq!(parse_command_id(xml).unwrap(), "REAL-CMD");
    }

    #[test]
    fn extract_streams_three_sequential() {
        // "A" = QQ==, "B" = Qg==, "C" = Qw==
        let xml = r#"<s:Envelope><s:Body><rsp:ReceiveResponse>
            <rsp:Stream Name="stdout" CommandId="C1">QQ==</rsp:Stream>
            <rsp:Stream Name="stderr" CommandId="C1">Qg==</rsp:Stream>
            <rsp:Stream Name="stdout" CommandId="C1">Qw==</rsp:Stream>
            <rsp:CommandState CommandId="C1" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
                <rsp:ExitCode>0</rsp:ExitCode>
            </rsp:CommandState>
        </rsp:ReceiveResponse></s:Body></s:Envelope>"#;
        let output = parse_receive_output(xml).unwrap();
        assert_eq!(output.stdout, b"AC");
        assert_eq!(output.stderr, b"B");
    }

    #[test]
    fn extract_element_text_trims_whitespace() {
        let xml = r"<rsp:ShellId>  TRIMMED  </rsp:ShellId>";
        assert_eq!(parse_shell_id(xml).unwrap(), "TRIMMED");
    }

    #[test]
    fn parse_receive_output_negative_exit_code() {
        let xml = r#"<s:Envelope><s:Body><rsp:ReceiveResponse>
            <rsp:CommandState CommandId="C1" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
                <rsp:ExitCode>-1</rsp:ExitCode>
            </rsp:CommandState>
        </rsp:ReceiveResponse></s:Body></s:Envelope>"#;
        let output = parse_receive_output(xml).unwrap();
        assert!(output.done);
        assert_eq!(output.exit_code, Some(-1));
    }

    #[test]
    fn parse_receive_output_non_numeric_exit_code() {
        let xml = r#"<s:Envelope><s:Body><rsp:ReceiveResponse>
            <rsp:CommandState CommandId="C1" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
                <rsp:ExitCode>notanumber</rsp:ExitCode>
            </rsp:CommandState>
        </rsp:ReceiveResponse></s:Body></s:Envelope>"#;
        let output = parse_receive_output(xml).unwrap();
        assert!(output.done);
        assert!(output.exit_code.is_none());
    }

    #[test]
    fn extract_element_text_at_end_of_string() {
        let xml = "<rsp:CommandId>VAL</rsp:CommandId>";
        assert_eq!(parse_command_id(xml).unwrap(), "VAL");
    }

    #[test]
    fn extract_element_bare_at_start_of_string() {
        let xml = "<CommandId>START-ID</CommandId>";
        assert_eq!(parse_command_id(xml).unwrap(), "START-ID");
    }

    #[test]
    fn extract_element_bare_with_prefix_text() {
        let xml = "X<ShellId>OFFSET-ID</ShellId>";
        assert_eq!(parse_shell_id(xml).unwrap(), "OFFSET-ID");
    }

    #[test]
    fn extract_streams_at_end_of_string() {
        // "ok" = b2s=
        let xml = r#"<rsp:Stream Name="stdout" CommandId="C1">b2s=</rsp:Stream>"#;
        let streams = extract_streams(xml);
        assert_eq!(streams.len(), 1);
        assert_eq!(streams[0].0, "stdout");
    }

    #[test]
    fn extract_streams_rsp_before_bare_picks_first() {
        // "A" = QQ==, "B" = Qg==
        let xml =
            r#"<rsp:Stream Name="stdout">QQ==</rsp:Stream> <Stream Name="stderr">Qg==</Stream>"#;
        let streams = extract_streams(xml);
        assert_eq!(streams.len(), 2);
        assert_eq!(streams[0].0, "stdout");
        assert_eq!(streams[0].1, "QQ==");
        assert_eq!(streams[1].0, "stderr");
        assert_eq!(streams[1].1, "Qg==");
    }

    #[test]
    fn extract_streams_close_tag_ordering() {
        // "X" = WA==
        let xml = r#"<rsp:Stream Name="stdout">WA==</Stream>extra</rsp:Stream>"#;
        let streams = extract_streams(xml);
        assert_eq!(streams.len(), 1);
        assert_eq!(streams[0].1, "WA==");
    }

    #[test]
    fn extract_streams_adjacent_streams_search_from_advance() {
        // "X" = WA==, "Y" = WQ==
        let xml = r#"<rsp:Stream Name="stdout">WA==</rsp:Stream><rsp:Stream Name="stderr">WQ==</rsp:Stream>"#;
        let streams = extract_streams(xml);
        assert_eq!(streams.len(), 2, "both adjacent streams must be found");
        assert_eq!(streams[0].0, "stdout");
        assert_eq!(streams[0].1, "WA==");
        assert_eq!(streams[1].0, "stderr");
        assert_eq!(streams[1].1, "WQ==");
    }

    #[test]
    fn extract_streams_three_adjacent_with_content() {
        // "A" = QQ==, "B" = Qg==, "C" = Qw==
        let xml = r#"<rsp:Stream Name="stdout">QQ==</rsp:Stream><rsp:Stream Name="stderr">Qg==</rsp:Stream><rsp:Stream Name="stdout">Qw==</rsp:Stream>"#;
        let streams = extract_streams(xml);
        assert_eq!(streams.len(), 3, "all three adjacent streams must be found");
        assert_eq!(streams[0].1, "QQ==");
        assert_eq!(streams[1].1, "Qg==");
        assert_eq!(streams[2].1, "Qw==");
    }

    #[test]
    fn extract_element_text_empty_then_filled_tight() {
        let xml = "<rsp:ShellId></rsp:ShellId><rsp:ShellId>OK</rsp:ShellId>";
        assert_eq!(parse_shell_id(xml).unwrap(), "OK");
    }

    #[test]
    fn parse_receive_output_stream_at_xml_end() {
        // "Z" = Wg==
        let xml = r#"<s:Envelope><s:Body><rsp:ReceiveResponse><rsp:Stream Name="stdout" CommandId="C1">Wg==</rsp:Stream><rsp:CommandState CommandId="C1" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done"><rsp:ExitCode>0</rsp:ExitCode></rsp:CommandState></rsp:ReceiveResponse></s:Body></s:Envelope>"#;
        let output = parse_receive_output(xml).unwrap();
        assert_eq!(output.stdout, b"Z");
        assert_eq!(output.exit_code, Some(0));
        assert!(output.done);
    }

    // --- Phase 8 tests ---

    #[test]
    fn extract_streams_long_content_with_second_stream() {
        let long_b64 = "QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB"; // 40 chars
        let xml = format!(
            r#"<rsp:Stream Name="stdout">{long_b64}</rsp:Stream><rsp:Stream Name="stderr">ZXJy</rsp:Stream>"#
        );
        let streams = extract_streams(&xml);
        assert_eq!(streams.len(), 2, "must find both streams with long content");
        assert_eq!(streams[0].0, "stdout");
        assert_eq!(streams[0].1, long_b64);
        assert_eq!(streams[1].0, "stderr");
        assert_eq!(streams[1].1, "ZXJy");
    }

    #[test]
    fn parse_receive_output_long_stdout_with_stderr() {
        let long_b64 = "QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB";
        let xml = format!(
            r#"<s:Envelope><s:Body><rsp:ReceiveResponse>
                <rsp:Stream Name="stdout" CommandId="C1">{long_b64}</rsp:Stream>
                <rsp:Stream Name="stderr" CommandId="C1">ZXJy</rsp:Stream>
                <rsp:CommandState CommandId="C1" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
                    <rsp:ExitCode>0</rsp:ExitCode>
                </rsp:CommandState>
            </rsp:ReceiveResponse></s:Body></s:Envelope>"#
        );
        let output = parse_receive_output(&xml).unwrap();
        assert_eq!(output.stdout.len(), 30, "should decode 30 bytes of stdout");
        assert_eq!(output.stderr, b"err", "should decode stderr correctly");
        assert_eq!(output.exit_code, Some(0));
    }

    #[test]
    fn parse_clixml_basic_error() {
        let input = b"#< CLIXML\r\n<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"><S S=\"Error\">Something went wrong</S></Objs>";
        let result = parse_clixml(input);
        assert_eq!(String::from_utf8_lossy(&result), "Something went wrong");
    }

    #[test]
    fn parse_clixml_escaped_newlines() {
        let input = b"#< CLIXML\r\n<Objs><S S=\"Error\">line1_x000D__x000A_line2</S></Objs>";
        let result = parse_clixml(input);
        assert_eq!(String::from_utf8_lossy(&result), "line1\r\nline2");
    }

    #[test]
    fn parse_clixml_multiple_errors() {
        let input = b"#< CLIXML\r\n<Objs><S S=\"Error\">err1</S><S S=\"Error\">err2</S></Objs>";
        let result = parse_clixml(input);
        assert_eq!(String::from_utf8_lossy(&result), "err1err2");
    }

    #[test]
    fn parse_clixml_not_clixml_passthrough() {
        // Non-CLIXML data should return empty (no <S S="Error"> tags)
        let input = b"plain error text without CLIXML";
        let result = parse_clixml(input);
        assert!(result.is_empty());
    }

    #[test]
    fn parse_receive_output_clixml_stderr() {
        use base64::Engine;
        use base64::engine::general_purpose::STANDARD as B64;

        let clixml = b"#< CLIXML\r\n<Objs><S S=\"Error\">PowerShell error_x000D__x000A_</S></Objs>";
        let encoded = B64.encode(clixml);
        let xml = format!(
            r#"<rsp:ReceiveResponse><rsp:Stream Name="stderr">{encoded}</rsp:Stream><rsp:CommandState State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done"><rsp:ExitCode>1</rsp:ExitCode></rsp:CommandState></rsp:ReceiveResponse>"#
        );
        let output = parse_receive_output(&xml).unwrap();
        assert_eq!(
            String::from_utf8_lossy(&output.stderr),
            "PowerShell error\r\n"
        );
        assert_eq!(output.exit_code, Some(1));
    }

    // Kills extract_subcode_value returning None
    #[test]
    fn extract_subcode_from_soap_fault() {
        let xml = r"<s:Fault>
            <s:Code><s:Value>s:Sender</s:Value>
            <s:Subcode><s:Value>wsa:DestinationUnreachable</s:Value></s:Subcode></s:Code>
            <s:Reason><s:Text>no route</s:Text></s:Reason></s:Fault>";
        let val = extract_subcode_value(xml);
        assert_eq!(val.as_deref(), Some("wsa:DestinationUnreachable"));
    }

    // Kills extract_element_text:180 — < → <= on search_from
    #[test]
    fn extract_element_text_exact_boundary() {
        // Element at the very end of the string
        let xml = "<Root><Name>val</Name></Root>";
        assert_eq!(extract_element_text(xml, "Name").as_deref(), Some("val"));
    }

    // Kills extract_streams:227 — < → <= on search_from
    #[test]
    fn extract_streams_at_string_boundary() {
        // Streams that extend to the end of the XML
        let xml = r#"<rsp:Stream Name="stdout">YWJD</rsp:Stream>"#;
        let streams = extract_streams(xml);
        assert_eq!(streams.len(), 1);
        assert_eq!(streams[0].0, "stdout");
        assert_eq!(streams[0].1, "YWJD");
    }

    // Tests parse_enumerate_response with and without continuation context
    #[test]
    fn parse_enumerate_response_with_items_and_end() {
        let xml = r#"<s:Envelope xmlns:wsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration">
            <s:Body><wsen:EnumerateResponse>
                <wsen:Items><data>hello</data></wsen:Items>
                <wsen:EndOfSequence/>
            </wsen:EnumerateResponse></s:Body></s:Envelope>"#;
        let (items, context) = parse_enumerate_response(xml).unwrap();
        assert!(items.contains("hello"));
        assert!(context.is_none(), "EndOfSequence means no continuation");
    }

    #[test]
    fn parse_enumerate_response_with_continuation() {
        let xml = r#"<s:Envelope xmlns:wsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration">
            <s:Body><wsen:EnumerateResponse>
                <wsen:Items><data>page1</data></wsen:Items>
                <wsen:EnumerationContext>ctx-123</wsen:EnumerationContext>
            </wsen:EnumerateResponse></s:Body></s:Envelope>"#;
        let (items, context) = parse_enumerate_response(xml).unwrap();
        assert!(items.contains("page1"));
        assert_eq!(context.as_deref(), Some("ctx-123"));
    }

    // parse_clixml with multiple error fragments to test offset arithmetic
    #[test]
    fn parse_clixml_consecutive_error_fragments() {
        let clixml = b"#< CLIXML\r\n<Objs><S S=\"Error\">aaa</S><S S=\"Error\">bbb</S></Objs>";
        let result = parse_clixml(clixml);
        assert_eq!(String::from_utf8_lossy(&result), "aaabbb");
    }
}