| 1 |
use libxml::parser::Parser; |
| 2 |
use libxml::tree::c14n::{CanonicalizationMode, CanonicalizationOptions}; |
| 3 |
|
| 4 |
fn assert_eq_lines(seen: &str, expected: &str) { |
| 5 |
let lines_iter = seen.lines().zip(expected.lines()); |
| 6 |
|
| 7 |
for (seen_line, expected_line) in lines_iter { |
| 8 |
assert_eq!(seen_line, expected_line); |
| 9 |
} |
| 10 |
|
| 11 |
assert_eq!(seen.lines().count(), expected.lines().count()); |
| 12 |
} |
| 13 |
|
| 14 |
fn canonicalize_xml(input: &str, opts: CanonicalizationOptions) -> String { |
| 15 |
let parser = Parser::default(); |
| 16 |
let doc = parser.parse_string(input).unwrap(); |
| 17 |
|
| 18 |
doc.canonicalize(opts, None).unwrap() |
| 19 |
} |
| 20 |
|
| 21 |
#[test] |
| 22 |
fn canonical_1_1_example_3_1_no_comment() { |
| 23 |
let input = include_str!("resources/canonical_1_1/3_1_input.xml"); |
| 24 |
let expected = include_str!("resources/canonical_1_1/3_1_output_no_comment.xml"); |
| 25 |
|
| 26 |
let canonicalized = canonicalize_xml( |
| 27 |
input, |
| 28 |
CanonicalizationOptions { |
| 29 |
mode: CanonicalizationMode::Canonical1_1, |
| 30 |
with_comments: false, |
| 31 |
inclusive_ns_prefixes: vec![], |
| 32 |
}, |
| 33 |
); |
| 34 |
|
| 35 |
assert_eq_lines(&canonicalized, expected); |
| 36 |
} |
| 37 |
|
| 38 |
#[test] |
| 39 |
fn canonical_1_1_example_3_2() { |
| 40 |
let input = include_str!("resources/canonical_1_1/3_2_input.xml"); |
| 41 |
let expected = include_str!("resources/canonical_1_1/3_2_output.xml"); |
| 42 |
|
| 43 |
let canonicalized = canonicalize_xml( |
| 44 |
input, |
| 45 |
CanonicalizationOptions { |
| 46 |
mode: CanonicalizationMode::Canonical1_1, |
| 47 |
with_comments: true, |
| 48 |
inclusive_ns_prefixes: vec![], |
| 49 |
}, |
| 50 |
); |
| 51 |
|
| 52 |
|
| 53 |
assert_eq_lines(&canonicalized, expected.trim()) |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn canonical_exclusive_example_1() { |
| 58 |
let input = include_str!("resources/canonical_exclusive/1_input.xml"); |
| 59 |
let expected = include_str!("resources/canonical_exclusive/1_output.xml"); |
| 60 |
|
| 61 |
let canonicalized = canonicalize_xml( |
| 62 |
input, |
| 63 |
CanonicalizationOptions { |
| 64 |
mode: CanonicalizationMode::ExclusiveCanonical1_0, |
| 65 |
with_comments: true, |
| 66 |
inclusive_ns_prefixes: vec![], |
| 67 |
}, |
| 68 |
); |
| 69 |
|
| 70 |
|
| 71 |
assert_eq_lines(&canonicalized, expected.trim()) |
| 72 |
} |
| 73 |
|
| 74 |
#[test] |
| 75 |
fn canonical_exclusive_example_2() { |
| 76 |
let input = include_str!("resources/canonical_exclusive/2_input.xml"); |
| 77 |
let expected = include_str!("resources/canonical_exclusive/2_output.xml"); |
| 78 |
|
| 79 |
let canonicalized = canonicalize_xml( |
| 80 |
input, |
| 81 |
CanonicalizationOptions { |
| 82 |
mode: CanonicalizationMode::ExclusiveCanonical1_0, |
| 83 |
with_comments: true, |
| 84 |
inclusive_ns_prefixes: ["stay1".to_string(), "stay2".to_string()].to_vec(), |
| 85 |
}, |
| 86 |
); |
| 87 |
|
| 88 |
|
| 89 |
assert_eq_lines(&canonicalized, expected.trim()) |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn test_c14n_node() { |
| 94 |
let xml = "<a><b><c></c></b></a>"; |
| 95 |
let doc = Parser::default().parse_string(xml).unwrap(); |
| 96 |
let mut node = doc.as_node().findnodes("//b").unwrap().pop().unwrap(); |
| 97 |
|
| 98 |
let c14n = node.canonicalize(opts()).unwrap(); |
| 99 |
|
| 100 |
assert_eq!("<b><c></c></b>", c14n) |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn test_c14n_modes() { |
| 105 |
|
| 106 |
|
| 107 |
let doc1 = Parser::default() |
| 108 |
.parse_string( |
| 109 |
r#" |
| 110 |
<n0:local xmlns:n0="http://foobar.org" xmlns:n3="ftp://example.org"> |
| 111 |
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en"> |
| 112 |
<n3:stuff xmlns:n3="ftp://example.org"/> |
| 113 |
</n1:elem2> |
| 114 |
</n0:local> |
| 115 |
|
| 116 |
"#, |
| 117 |
) |
| 118 |
.unwrap(); |
| 119 |
|
| 120 |
let mut node1 = doc1 |
| 121 |
.as_node() |
| 122 |
.at_xpath("//n1:elem2", &[("n1", "http://example.net")]) |
| 123 |
.unwrap() |
| 124 |
.unwrap(); |
| 125 |
|
| 126 |
let doc2 = Parser::default() |
| 127 |
.parse_string( |
| 128 |
r#" |
| 129 |
<n2:pdu xmlns:n1="http://example.com" |
| 130 |
xmlns:n2="http://foo.example" |
| 131 |
xmlns:n4="http://foo.example" |
| 132 |
xml:lang="fr" |
| 133 |
xml:space="retain"> |
| 134 |
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en"> |
| 135 |
<n3:stuff xmlns:n3="ftp://example.org"/> |
| 136 |
<n4:stuff /> |
| 137 |
</n1:elem2> |
| 138 |
</n2:pdu> |
| 139 |
"#, |
| 140 |
) |
| 141 |
.unwrap(); |
| 142 |
let mut node2 = doc2 |
| 143 |
.as_node() |
| 144 |
.at_xpath("//n1:elem2", &[("n1", "http://example.net")]) |
| 145 |
.unwrap() |
| 146 |
.unwrap(); |
| 147 |
|
| 148 |
let expected = r#" |
| 149 |
<n1:elem2 xmlns:n0="http://foobar.org" xmlns:n1="http://example.net" xmlns:n3="ftp://example.org" xml:lang="en"> |
| 150 |
<n3:stuff></n3:stuff> |
| 151 |
</n1:elem2> |
| 152 |
"#.trim(); |
| 153 |
let c14n = node1.canonicalize(opts()).unwrap(); |
| 154 |
assert_eq_lines(expected, &c14n); |
| 155 |
|
| 156 |
let expected = r#" |
| 157 |
<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en" xml:space="retain"> |
| 158 |
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff> |
| 159 |
<n4:stuff></n4:stuff> |
| 160 |
</n1:elem2> |
| 161 |
"#.trim(); |
| 162 |
let c14n = node2.canonicalize(opts()).unwrap(); |
| 163 |
assert_eq_lines(expected, &c14n); |
| 164 |
|
| 165 |
let opts = CanonicalizationOptions { |
| 166 |
mode: CanonicalizationMode::Canonical1_0, |
| 167 |
..Default::default() |
| 168 |
}; |
| 169 |
let c14n = node2.canonicalize(opts).unwrap(); |
| 170 |
assert_eq_lines(expected, &c14n); |
| 171 |
|
| 172 |
let expected = r#" |
| 173 |
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en"> |
| 174 |
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff> |
| 175 |
</n1:elem2> |
| 176 |
"# |
| 177 |
.trim(); |
| 178 |
let c14n = node1 |
| 179 |
.canonicalize(CanonicalizationOptions { |
| 180 |
mode: CanonicalizationMode::ExclusiveCanonical1_0, |
| 181 |
..Default::default() |
| 182 |
}) |
| 183 |
.unwrap(); |
| 184 |
|
| 185 |
assert_eq_lines(expected, &c14n); |
| 186 |
|
| 187 |
let expected = r#" |
| 188 |
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en"> |
| 189 |
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff> |
| 190 |
<n4:stuff xmlns:n4="http://foo.example"></n4:stuff> |
| 191 |
</n1:elem2> |
| 192 |
"# |
| 193 |
.trim(); |
| 194 |
let c14n = node2 |
| 195 |
.canonicalize(CanonicalizationOptions { |
| 196 |
mode: CanonicalizationMode::ExclusiveCanonical1_0, |
| 197 |
..Default::default() |
| 198 |
}) |
| 199 |
.unwrap(); |
| 200 |
assert_eq_lines(expected, &c14n); |
| 201 |
|
| 202 |
let expected = r#" |
| 203 |
<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xml:lang="en"> |
| 204 |
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff> |
| 205 |
<n4:stuff xmlns:n4="http://foo.example"></n4:stuff> |
| 206 |
</n1:elem2> |
| 207 |
"# |
| 208 |
.trim(); |
| 209 |
let c14n = node2 |
| 210 |
.canonicalize(CanonicalizationOptions { |
| 211 |
mode: CanonicalizationMode::ExclusiveCanonical1_0, |
| 212 |
inclusive_ns_prefixes: vec!["n2".into()], |
| 213 |
..Default::default() |
| 214 |
}) |
| 215 |
.unwrap(); |
| 216 |
assert_eq_lines(expected, &c14n); |
| 217 |
|
| 218 |
let expected = r#" |
| 219 |
<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en"> |
| 220 |
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff> |
| 221 |
<n4:stuff></n4:stuff> |
| 222 |
</n1:elem2> |
| 223 |
"#.trim(); |
| 224 |
let c14n = node2 |
| 225 |
.canonicalize(CanonicalizationOptions { |
| 226 |
mode: CanonicalizationMode::ExclusiveCanonical1_0, |
| 227 |
inclusive_ns_prefixes: vec!["n2".into(), "n4".into()], |
| 228 |
..Default::default() |
| 229 |
}) |
| 230 |
.unwrap(); |
| 231 |
assert_eq_lines(expected, &c14n); |
| 232 |
|
| 233 |
let expected = r#" |
| 234 |
<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en" xml:space="retain"> |
| 235 |
<n3:stuff xmlns:n3="ftp://example.org"></n3:stuff> |
| 236 |
<n4:stuff></n4:stuff> |
| 237 |
</n1:elem2> |
| 238 |
"#.trim(); |
| 239 |
let c14n = node2 |
| 240 |
.canonicalize(CanonicalizationOptions { |
| 241 |
mode: CanonicalizationMode::Canonical1_1, |
| 242 |
..Default::default() |
| 243 |
}) |
| 244 |
.unwrap(); |
| 245 |
assert_eq_lines(expected, &c14n); |
| 246 |
} |
| 247 |
|
| 248 |
fn opts() -> CanonicalizationOptions { |
| 249 |
CanonicalizationOptions { |
| 250 |
mode: CanonicalizationMode::Canonical1_1, |
| 251 |
with_comments: false, |
| 252 |
inclusive_ns_prefixes: vec![], |
| 253 |
} |
| 254 |
} |