xml-sec 0.1.6

Pure Rust XML Security: XMLDSig, XMLEnc, C14N. Drop-in replacement for libxmlsec1.
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
//! Integration tests for C14N against xmllint reference outputs.
//!
//! Reference outputs generated by: `xmllint --c14n <file>` and `xmllint --exc-c14n <file>`
//! using libxml2 (system xmllint). These are the authoritative C14N implementations.

use xml_sec::c14n::{C14nAlgorithm, C14nMode, canonicalize, canonicalize_xml};

// ─── Helpers ────────────────────────────────────────────────────────────────

fn assert_c14n(xml: &[u8], mode: C14nMode, with_comments: bool, expected: &str) {
    let algo = C14nAlgorithm::new(mode, with_comments);
    let result = canonicalize_xml(xml, &algo).expect("canonicalize failed");
    let result_str = String::from_utf8(result).expect("invalid utf8");
    assert_eq!(
        result_str, expected,
        "\n--- GOT ---\n{result_str}\n--- EXPECTED ---\n{expected}"
    );
}

fn assert_exc_c14n(xml: &[u8], prefix_list: &str, expected: &str) {
    let algo = C14nAlgorithm::new(C14nMode::Exclusive1_0, false).with_prefix_list(prefix_list);
    let result = canonicalize_xml(xml, &algo).expect("canonicalize failed");
    let result_str = String::from_utf8(result).expect("invalid utf8");
    assert_eq!(
        result_str, expected,
        "\n--- GOT ---\n{result_str}\n--- EXPECTED ---\n{expected}"
    );
}

// ─── Simple namespace document (inclusive) ──────────────────────────────────
// Reference output verified against: xmllint --c14n

const SIMPLE_NS_XML: &[u8] =
    br#"<root xmlns:z="http://z.com" xmlns:a="http://a.com" xmlns="http://default.com" b="2" a="1">
  <a:child z:attr="val" plain="yes">
    <inner/>
  </a:child>
  <z:other xmlns:a="http://a-override.com"/>
</root>"#;

#[test]
fn simple_ns_inclusive() {
    // Reference from xmllint --c14n:
    // - ns decls sorted: xmlns= first, then xmlns:a, xmlns:z
    // - attrs sorted: a before b (no namespace, lexicographic)
    // - child: ns inherited, not redeclared; attrs sorted: plain, z:attr
    // - z:other: xmlns:a overridden, must redeclare
    assert_c14n(
        SIMPLE_NS_XML,
        C14nMode::Inclusive1_0,
        false,
        concat!(
            r#"<root xmlns="http://default.com" xmlns:a="http://a.com" xmlns:z="http://z.com" a="1" b="2">"#,
            "\n",
            r#"  <a:child plain="yes" z:attr="val">"#,
            "\n",
            r#"    <inner></inner>"#,
            "\n",
            r#"  </a:child>"#,
            "\n",
            r#"  <z:other xmlns:a="http://a-override.com"></z:other>"#,
            "\n",
            r#"</root>"#,
        ),
    );
}

#[test]
fn simple_ns_exclusive() {
    // Reference from xmllint --exc-c14n:
    // - root: only xmlns= (default ns used by root tag)
    // - a:child: xmlns:a, xmlns:z (used by a: prefix and z:attr)
    // - inner: no ns (no prefix used, inherits default from parent)
    // - z:other: xmlns:z only (a: not used by z:other)
    assert_exc_c14n(
        SIMPLE_NS_XML,
        "",
        concat!(
            r#"<root xmlns="http://default.com" a="1" b="2">"#,
            "\n",
            r#"  <a:child xmlns:a="http://a.com" xmlns:z="http://z.com" plain="yes" z:attr="val">"#,
            "\n",
            r#"    <inner></inner>"#,
            "\n",
            r#"  </a:child>"#,
            "\n",
            r#"  <z:other xmlns:z="http://z.com"></z:other>"#,
            "\n",
            r#"</root>"#,
        ),
    );
}

// ─── Merlin-like document ───────────────────────────────────────────────────
// Structure from merlin-c14n-three/signature.xml (data portion only)
// Reference from xmllint --c14n / --exc-c14n

const MERLIN_DATA_XML: &[u8] = br#"<?xml version="1.0" encoding="UTF-8"?>
<foo:Root xmlns:bar="http://example.org/bar" xmlns:baz="http://example.org/baz" xmlns:foo="http://example.org/foo" xmlns="http://example.org/" xml:lang="en-ie">
  <bar:Something>
    <foo:Nothing>
      <foo:Something>
        <bar:Something>
          <foo:Something>
            <foo:Nothing>
              <foo:Something>
                <baz:Something />
              </foo:Something>
            </foo:Nothing>
          </foo:Something>
        </bar:Something>
      </foo:Something>
    </foo:Nothing>
  </bar:Something>
</foo:Root>"#;

#[test]
fn merlin_data_inclusive() {
    // All 4 ns prefixes + default ns + xml:lang on root.
    // Inner elements inherit everything, no redeclarations.
    assert_c14n(
        MERLIN_DATA_XML,
        C14nMode::Inclusive1_0,
        false,
        concat!(
            r#"<foo:Root xmlns="http://example.org/" xmlns:bar="http://example.org/bar" xmlns:baz="http://example.org/baz" xmlns:foo="http://example.org/foo" xml:lang="en-ie">"#,
            "\n",
            "  <bar:Something>\n",
            "    <foo:Nothing>\n",
            "      <foo:Something>\n",
            "        <bar:Something>\n",
            "          <foo:Something>\n",
            "            <foo:Nothing>\n",
            "              <foo:Something>\n",
            "                <baz:Something></baz:Something>\n",
            "              </foo:Something>\n",
            "            </foo:Nothing>\n",
            "          </foo:Something>\n",
            "        </bar:Something>\n",
            "      </foo:Something>\n",
            "    </foo:Nothing>\n",
            "  </bar:Something>\n",
            "</foo:Root>",
        ),
    );
}

#[test]
fn merlin_data_exclusive() {
    // Exclusive: each element only declares what it visibly uses.
    // foo:Root uses foo: prefix → xmlns:foo
    // xml:lang is an attribute, always present on root
    // bar:Something uses bar: → xmlns:bar
    // foo:Nothing uses foo: → already rendered by parent? No — exc mode
    //   tracks per output ancestor, foo:Root rendered foo:, so foo:Nothing inherits
    // baz:Something uses baz: → xmlns:baz
    // Default ns (xmlns="http://example.org/") not used by any element prefix → NOT rendered
    assert_exc_c14n(
        MERLIN_DATA_XML,
        "",
        concat!(
            r#"<foo:Root xmlns:foo="http://example.org/foo" xml:lang="en-ie">"#,
            "\n",
            r#"  <bar:Something xmlns:bar="http://example.org/bar">"#,
            "\n",
            "    <foo:Nothing>\n",
            "      <foo:Something>\n",
            "        <bar:Something>\n",
            "          <foo:Something>\n",
            "            <foo:Nothing>\n",
            "              <foo:Something>\n",
            r#"                <baz:Something xmlns:baz="http://example.org/baz"></baz:Something>"#,
            "\n",
            "              </foo:Something>\n",
            "            </foo:Nothing>\n",
            "          </foo:Something>\n",
            "        </bar:Something>\n",
            "      </foo:Something>\n",
            "    </foo:Nothing>\n",
            "  </bar:Something>\n",
            "</foo:Root>",
        ),
    );
}

// ─── Attribute sorting with namespaced attributes ───────────────────────────
// Reference from xmllint --c14n

const ATTR_SORT_XML: &[u8] = br#"<root xmlns:ns2="http://ns2" xmlns:ns1="http://ns1">
  <elem ns2:b="2" ns1:a="1" ns2:a="3" ns1:b="4" plain="5"/>
</root>"#;

#[test]
fn attr_sort_inclusive() {
    // Attributes sorted by (namespace-uri, local-name):
    // - plain (no ns, "") → first
    // - ns1:a (http://ns1, a)
    // - ns1:b (http://ns1, b)
    // - ns2:a (http://ns2, a)
    // - ns2:b (http://ns2, b)
    assert_c14n(
        ATTR_SORT_XML,
        C14nMode::Inclusive1_0,
        false,
        concat!(
            r#"<root xmlns:ns1="http://ns1" xmlns:ns2="http://ns2">"#,
            "\n",
            r#"  <elem plain="5" ns1:a="1" ns1:b="4" ns2:a="3" ns2:b="2"></elem>"#,
            "\n",
            "</root>",
        ),
    );
}

#[test]
fn attr_sort_exclusive() {
    // In exclusive mode: root uses no ns → no decls on root.
    // elem uses ns1: and ns2: → declares both.
    assert_exc_c14n(
        ATTR_SORT_XML,
        "",
        concat!(
            "<root>\n",
            r#"  <elem xmlns:ns1="http://ns1" xmlns:ns2="http://ns2" plain="5" ns1:a="1" ns1:b="4" ns2:a="3" ns2:b="2"></elem>"#,
            "\n",
            "</root>",
        ),
    );
}

// ─── Comments and Processing Instructions ───────────────────────────────────

const COMMENTS_PI_XML: &[u8] = b"<!-- doc-level comment -->\n<?target data?>\n<root>\n  <!-- inner comment -->\n  <child></child>\n</root>\n<!-- trailing comment -->";

#[test]
fn comments_stripped() {
    // Comments are stripped entirely. PI is first output, no leading \n.
    // Trailing comment also stripped, no trailing \n.
    // The whitespace text node between stripped comment and PI is at
    // document level → ignored by C14N.
    assert_c14n(
        COMMENTS_PI_XML,
        C14nMode::Inclusive1_0,
        false,
        concat!(
            "<?target data?>\n",
            "<root>\n",
            "  \n",
            "  <child></child>\n",
            "</root>",
        ),
    );
}

#[test]
fn comments_preserved() {
    assert_c14n(
        COMMENTS_PI_XML,
        C14nMode::Inclusive1_0,
        true,
        concat!(
            "<!-- doc-level comment -->\n<?target data?>\n",
            "<root>\n",
            "  <!-- inner comment -->\n",
            "  <child></child>\n",
            "</root>\n<!-- trailing comment -->",
        ),
    );
}

// ─── Idempotency ────────────────────────────────────────────────────────────

#[test]
fn idempotency_inclusive() {
    let algo = C14nAlgorithm::new(C14nMode::Inclusive1_0, false);
    let pass1 = canonicalize_xml(MERLIN_DATA_XML, &algo).expect("pass1");
    let pass2 = canonicalize_xml(&pass1, &algo).expect("pass2");
    assert_eq!(pass1, pass2, "C14N must be idempotent");
}

#[test]
fn idempotency_exclusive() {
    let algo = C14nAlgorithm::new(C14nMode::Exclusive1_0, false);
    let pass1 = canonicalize_xml(MERLIN_DATA_XML, &algo).expect("pass1");
    let pass2 = canonicalize_xml(&pass1, &algo).expect("pass2");
    assert_eq!(pass1, pass2, "Exclusive C14N must be idempotent");
}

// ─── Empty document edge case ───────────────────────────────────────────────

#[test]
fn minimal_document() {
    assert_c14n(b"<r/>", C14nMode::Inclusive1_0, false, "<r></r>");
}

#[test]
fn cdata_flattened() {
    // CDATA sections should be replaced with escaped text content.
    assert_c14n(
        b"<r><![CDATA[a < b & c]]></r>",
        C14nMode::Inclusive1_0,
        false,
        "<r>a &lt; b &amp; c</r>",
    );
}

// ─── Prefix aliasing (multiple prefixes for same URI) ───────────────────────

#[test]
fn aliased_prefixes_exclusive() {
    // Two prefixes bound to the same URI. Each element/attribute must preserve
    // the lexical prefix actually used in the source, not an arbitrary one
    // returned by lookup_prefix().
    let xml = br#"<root xmlns:a="http://same" xmlns:b="http://same"><b:child a:x="1"/></root>"#;
    assert_exc_c14n(
        xml,
        "",
        concat!(
            "<root>",
            r#"<b:child xmlns:a="http://same" xmlns:b="http://same" a:x="1"></b:child>"#,
            "</root>",
        ),
    );
}

#[test]
fn aliased_prefixes_inclusive() {
    let xml = br#"<root xmlns:a="http://same" xmlns:b="http://same"><b:child a:x="1"/></root>"#;
    assert_c14n(
        xml,
        C14nMode::Inclusive1_0,
        false,
        concat!(
            r#"<root xmlns:a="http://same" xmlns:b="http://same">"#,
            r#"<b:child a:x="1"></b:child>"#,
            "</root>",
        ),
    );
}

// ─── Default namespace undeclaration ────────────────────────────────────────

#[test]
fn default_ns_undeclaration() {
    let xml = br#"<root xmlns="http://example.com"><child xmlns=""/></root>"#;
    assert_c14n(
        xml,
        C14nMode::Inclusive1_0,
        false,
        r#"<root xmlns="http://example.com"><child xmlns=""></child></root>"#,
    );
}

// ─── C14N 1.1 ───────────────────────────────────────────────────────────────

#[test]
fn c14n_1_1_full_document_matches_1_0() {
    // For full documents without xml:base fixup scenarios, C14N 1.1 output
    // is identical to C14N 1.0.
    let algo_10 = C14nAlgorithm::new(C14nMode::Inclusive1_0, false);
    let algo_11 = C14nAlgorithm::new(C14nMode::Inclusive1_1, false);
    let result_10 = canonicalize_xml(MERLIN_DATA_XML, &algo_10).expect("1.0");
    let result_11 = canonicalize_xml(MERLIN_DATA_XML, &algo_11).expect("1.1");
    assert_eq!(
        result_10, result_11,
        "C14N 1.1 full-document output should match 1.0"
    );
}

#[test]
fn c14n_1_1_with_comments() {
    assert_c14n(
        COMMENTS_PI_XML,
        C14nMode::Inclusive1_1,
        true,
        concat!(
            "<!-- doc-level comment -->\n<?target data?>\n",
            "<root>\n",
            "  <!-- inner comment -->\n",
            "  <child></child>\n",
            "</root>\n<!-- trailing comment -->",
        ),
    );
}

#[test]
fn c14n_1_1_idempotency() {
    let algo = C14nAlgorithm::new(C14nMode::Inclusive1_1, false);
    let pass1 = canonicalize_xml(MERLIN_DATA_XML, &algo).expect("pass1");
    let pass2 = canonicalize_xml(&pass1, &algo).expect("pass2");
    assert_eq!(pass1, pass2, "C14N 1.1 must be idempotent");
}

#[test]
fn c14n_1_1_xml_id_inherited_in_subset() {
    // C14N 1.1 specific: xml:id is propagated to document subsets.
    // Root has xml:id="doc1", subset includes only child — xml:id must appear.
    let xml = r#"<root xml:id="doc1" xmlns:a="http://a"><a:child>text</a:child></root>"#;
    let doc = roxmltree::Document::parse(xml).expect("parse");
    let algo = C14nAlgorithm::new(C14nMode::Inclusive1_1, false);

    let child = doc.root_element().first_element_child().expect("child");
    let child_id = child.id();
    // Include child and its text node
    let pred =
        |n: roxmltree::Node| n.id() == child_id || n.parent().is_some_and(|p| p.id() == child_id);

    let mut output = Vec::new();
    canonicalize(&doc, Some(&pred), &algo, &mut output).expect("c14n");
    let result = String::from_utf8(output).expect("utf8");

    assert!(
        result.contains(r#"xml:id="doc1""#),
        "xml:id should be inherited in C14N 1.1 subset; got: {result}"
    );
    // Also verify namespace is rendered
    assert!(
        result.contains(r#"xmlns:a="http://a""#),
        "namespace should be rendered; got: {result}"
    );
}

#[test]
fn c14n_1_1_xml_lang_and_id_both_inherited() {
    // Both xml:lang and xml:id should be inherited together.
    let xml = r#"<root xml:lang="en" xml:id="r1"><child/></root>"#;
    let doc = roxmltree::Document::parse(xml).expect("parse");
    let algo = C14nAlgorithm::new(C14nMode::Inclusive1_1, false);

    let child = doc.root_element().first_element_child().expect("child");
    let child_id = child.id();
    let pred = |n: roxmltree::Node| n.id() == child_id;

    let mut output = Vec::new();
    canonicalize(&doc, Some(&pred), &algo, &mut output).expect("c14n");
    let result = String::from_utf8(output).expect("utf8");

    assert!(result.contains(r#"xml:id="r1""#), "got: {result}");
    assert!(result.contains(r#"xml:lang="en""#), "got: {result}");
}

// ─── C14N 1.1 xml:base fixup ─────────────────────────────────────────────────

#[test]
fn c14n_1_1_xml_base_resolved_in_subset() {
    // Root has xml:base="http://example.com/", child has xml:base="sub/".
    // In a subset excluding root, child's xml:base should be resolved
    // to "http://example.com/sub/" (not raw "sub/").
    let xml = r#"<root xml:base="http://example.com/"><child xml:base="sub/">text</child></root>"#;
    let doc = roxmltree::Document::parse(xml).expect("parse");
    let algo = C14nAlgorithm::new(C14nMode::Inclusive1_1, false);

    let child = doc.root_element().first_element_child().expect("child");
    let child_id = child.id();
    let pred =
        |n: roxmltree::Node| n.id() == child_id || n.parent().is_some_and(|p| p.id() == child_id);

    let mut output = Vec::new();
    canonicalize(&doc, Some(&pred), &algo, &mut output).expect("c14n");
    let result = String::from_utf8(output).expect("utf8");

    assert!(
        result.contains(r#"xml:base="http://example.com/sub/""#),
        "xml:base should be resolved to absolute URI; got: {result}"
    );
}

#[test]
fn c14n_1_1_xml_base_inherited_resolved() {
    // Root has xml:base, middle has xml:base. Subset includes only leaf.
    // Inherited xml:base should be the fully resolved effective URI.
    let xml = r#"<a xml:base="http://ex.com/x/"><b xml:base="y/"><c/></b></a>"#;
    let doc = roxmltree::Document::parse(xml).expect("parse");
    let algo = C14nAlgorithm::new(C14nMode::Inclusive1_1, false);

    let a = doc.root_element();
    let b = a.first_element_child().expect("b");
    let c = b.first_element_child().expect("c");
    let c_id = c.id();
    let pred = |n: roxmltree::Node| n.id() == c_id;

    let mut output = Vec::new();
    canonicalize(&doc, Some(&pred), &algo, &mut output).expect("c14n");
    let result = String::from_utf8(output).expect("utf8");

    // Effective base: http://ex.com/x/ + y/ = http://ex.com/x/y/
    assert!(
        result.contains(r#"xml:base="http://ex.com/x/y/""#),
        "inherited xml:base should be resolved; got: {result}"
    );
}

#[test]
fn c14n_1_1_xml_base_dotdot_resolved() {
    // Relative xml:base with .. segment should be resolved.
    let xml = r#"<a xml:base="http://ex.com/a/b/"><b xml:base="../c/"><d/></b></a>"#;
    let doc = roxmltree::Document::parse(xml).expect("parse");
    let algo = C14nAlgorithm::new(C14nMode::Inclusive1_1, false);

    let a = doc.root_element();
    let b = a.first_element_child().expect("b");
    let d = b.first_element_child().expect("d");
    let d_id = d.id();
    let pred = |n: roxmltree::Node| n.id() == d_id;

    let mut output = Vec::new();
    canonicalize(&doc, Some(&pred), &algo, &mut output).expect("c14n");
    let result = String::from_utf8(output).expect("utf8");

    // http://ex.com/a/b/ + ../c/ = http://ex.com/a/c/
    assert!(
        result.contains(r#"xml:base="http://ex.com/a/c/""#),
        "xml:base with .. should be resolved; got: {result}"
    );
}

#[test]
fn c14n_1_0_xml_base_not_resolved() {
    // C14N 1.0 does NOT resolve xml:base — inherited value is raw.
    let xml = r#"<root xml:base="http://example.com/"><child xml:base="sub/">text</child></root>"#;
    let doc = roxmltree::Document::parse(xml).expect("parse");
    let algo = C14nAlgorithm::new(C14nMode::Inclusive1_0, false);

    let child = doc.root_element().first_element_child().expect("child");
    let child_id = child.id();
    let pred =
        |n: roxmltree::Node| n.id() == child_id || n.parent().is_some_and(|p| p.id() == child_id);

    let mut output = Vec::new();
    canonicalize(&doc, Some(&pred), &algo, &mut output).expect("c14n");
    let result = String::from_utf8(output).expect("utf8");

    // C14N 1.0: xml:base stays as raw "sub/". The parent's
    // xml:base="http://example.com/" is not copied onto the child because
    // the child already has its own xml:base attribute.
    assert!(
        result.contains(r#"xml:base="sub/""#),
        "C14N 1.0 should NOT resolve xml:base; got: {result}"
    );
}

// ─── Document subset: xmlns="" with excluded ancestor ───────────────────────

#[test]
fn subset_xmlns_empty_with_excluded_default_ns_ancestor() {
    // Scenario: root declares xmlns="http://u", child undeclares with xmlns="".
    // Node-set includes only child (root excluded).
    // parent_rendered is empty (root wasn't rendered), but child MUST emit
    // xmlns="" to undeclare the inherited default namespace from source tree.
    let xml = r#"<root xmlns="http://example.com"><child xmlns=""/></root>"#;
    let doc = roxmltree::Document::parse(xml).expect("parse");
    let algo = C14nAlgorithm::new(C14nMode::Inclusive1_0, false);

    let child_id = doc
        .root_element()
        .first_element_child()
        .expect("child")
        .id();

    let mut output = Vec::new();
    canonicalize(
        &doc,
        Some(&|node| node.id() == child_id),
        &algo,
        &mut output,
    )
    .expect("c14n");

    let result = String::from_utf8(output).expect("utf8");
    // child must emit xmlns="" even though parent_rendered has no default ns,
    // because the source tree has xmlns="http://example.com" in scope.
    assert!(
        result.contains(r#"xmlns="""#),
        "xmlns=\"\" must be emitted to undeclare inherited default ns. Got: {result}"
    );
}

#[test]
fn subset_no_spurious_xmlns_empty() {
    // Scenario: no default namespace anywhere in document.
    // Node-set includes only child. xmlns="" must NOT appear.
    let xml = r#"<root xmlns:a="http://a"><child/></root>"#;
    let doc = roxmltree::Document::parse(xml).expect("parse");
    let algo = C14nAlgorithm::new(C14nMode::Inclusive1_0, false);

    let child_id = doc
        .root_element()
        .first_element_child()
        .expect("child")
        .id();

    let mut output = Vec::new();
    canonicalize(
        &doc,
        Some(&|node| node.id() == child_id),
        &algo,
        &mut output,
    )
    .expect("c14n");

    let result = String::from_utf8(output).expect("utf8");
    assert!(
        !result.contains("xmlns="),
        "xmlns=\"\" must NOT appear when no default ns in scope. Got: {result}"
    );
}