forked from node-saml/xml-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc14n-non-exclusive-unit-tests.spec.ts
More file actions
211 lines (164 loc) · 8.37 KB
/
c14n-non-exclusive-unit-tests.spec.ts
File metadata and controls
211 lines (164 loc) · 8.37 KB
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
import { expect } from "chai";
import { C14nCanonicalization } from "../src/c14n-canonicalization";
import * as xmldom from "@xmldom/xmldom";
import * as xpath from "xpath";
import * as utils from "../src/utils";
const test_C14nCanonicalization = function (xml, xpathArg, expected) {
const doc = new xmldom.DOMParser().parseFromString(xml);
const elem = xpath.select1(xpathArg, doc);
const can = new C14nCanonicalization();
const result = can
// @ts-expect-error FIXME
.process(elem, {
ancestorNamespaces: utils.findAncestorNs(doc, xpathArg),
})
.toString();
expect(result).to.equal(expected);
};
const test_findAncestorNs = function (xml, xpath, expected) {
const doc = new xmldom.DOMParser().parseFromString(xml);
const result = utils.findAncestorNs(doc, xpath);
expect(result).to.deep.equal(expected);
};
describe("C14N non-exclusive canonicalization tests", function () {
it("findAncestorNs: Correctly picks up root ancestor namespace", function () {
const xml = "<root xmlns:aaa='bbb'><child1><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = [{ prefix: "aaa", namespaceURI: "bbb" }];
test_findAncestorNs(xml, xpath, expected);
});
it("findAncestorNs: Correctly picks up intermediate ancestor namespace", function () {
const xml = "<root><child1 xmlns:aaa='bbb'><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = [{ prefix: "aaa", namespaceURI: "bbb" }];
test_findAncestorNs(xml, xpath, expected);
});
it("findAncestorNs: Correctly picks up multiple ancestor namespaces declared in the one same element", function () {
const xml = "<root xmlns:aaa='bbb' xmlns:ccc='ddd'><child1><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = [
{ prefix: "aaa", namespaceURI: "bbb" },
{ prefix: "ccc", namespaceURI: "ddd" },
];
test_findAncestorNs(xml, xpath, expected);
});
it("findAncestorNs: Correctly picks up multiple ancestor namespaces scattered among depth", function () {
const xml = "<root xmlns:aaa='bbb'><child1 xmlns:ccc='ddd'><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = [
{ prefix: "ccc", namespaceURI: "ddd" },
{ prefix: "aaa", namespaceURI: "bbb" },
];
test_findAncestorNs(xml, xpath, expected);
});
it("findAncestorNs: Correctly picks up multiple ancestor namespaces without duplicate", function () {
const xml = "<root xmlns:ccc='bbb'><child1 xmlns:ccc='bbb'><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = [{ prefix: "ccc", namespaceURI: "bbb" }];
test_findAncestorNs(xml, xpath, expected);
});
it("findAncestorNs: Correctly eliminates duplicate prefix", function () {
const xml = "<root xmlns:ccc='bbb'><child1 xmlns:ccc='AAA'><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = [{ prefix: "ccc", namespaceURI: "AAA" }];
test_findAncestorNs(xml, xpath, expected);
});
it("findAncestorNs: Exclude namespace which is already declared with same prefix on target node", function () {
const xml =
"<root xmlns:ccc='bbb'><child1 xmlns:ccc='AAA'><child2 xmlns:ccc='AAA'></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = [];
test_findAncestorNs(xml, xpath, expected);
});
it("findAncestorNs: Ignores namespace declared in the target xpath node", function () {
const xml = "<root xmlns:aaa='bbb'><child1><child2 xmlns:ccc='ddd'></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = [{ prefix: "aaa", namespaceURI: "bbb" }];
test_findAncestorNs(xml, xpath, expected);
});
it("findAncestorNs: Should find namespace without prefix", function () {
const xml =
"<root xmlns='bbb'><child1><ds:child2 xmlns:ds='ddd'><ds:child3></ds:child3></ds:child2></child1></root>";
const xpath = "//*[local-name()='child2']";
const expected = [{ prefix: "", namespaceURI: "bbb" }];
test_findAncestorNs(xml, xpath, expected);
});
it("findAncestorNs: Should not find namespace when both has no prefix", function () {
const xml = "<root xmlns='bbb'><child1><child2 xmlns='ddd'></child2></child1></root>";
const xpath = "//*[local-name()='child2']";
const expected = [];
test_findAncestorNs(xml, xpath, expected);
});
// Tests for c14nCanonicalization
it("C14n: Correctly picks up root ancestor namespace", function () {
const xml = "<root xmlns:aaa='bbb'><child1><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = '<child2 xmlns:aaa="bbb"></child2>';
test_C14nCanonicalization(xml, xpath, expected);
});
it("C14n: Correctly picks up intermediate ancestor namespace", function () {
const xml = "<root><child1 xmlns:aaa='bbb'><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = '<child2 xmlns:aaa="bbb"></child2>';
test_C14nCanonicalization(xml, xpath, expected);
});
it("C14n: Correctly picks up multiple ancestor namespaces declared in the one same element", function () {
const xml = "<root xmlns:aaa='bbb' xmlns:ccc='ddd'><child1><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = '<child2 xmlns:aaa="bbb" xmlns:ccc="ddd"></child2>';
test_C14nCanonicalization(xml, xpath, expected);
});
it("C14n: Correctly picks up multiple ancestor namespaces scattered among depth", function () {
const xml = "<root xmlns:aaa='bbb'><child1 xmlns:ccc='ddd'><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = '<child2 xmlns:aaa="bbb" xmlns:ccc="ddd"></child2>';
test_C14nCanonicalization(xml, xpath, expected);
});
it("C14n: Correctly picks up multiple ancestor namespaces without duplicate", function () {
const xml = "<root xmlns:ccc='bbb'><child1 xmlns:ccc='bbb'><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = '<child2 xmlns:ccc="bbb"></child2>';
test_C14nCanonicalization(xml, xpath, expected);
});
it("C14n: Correctly eliminates duplicate prefix", function () {
const xml = "<root xmlns:ccc='bbb'><child1 xmlns:ccc='AAA'><child2></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = '<child2 xmlns:ccc="AAA"></child2>';
test_C14nCanonicalization(xml, xpath, expected);
});
it("C14n: Exclude namespace which is already declared with same prefix on target node", function () {
const xml =
"<root xmlns:ccc='bbb'><child1 xmlns:ccc='AAA'><child2 xmlns:ccc='AAA'></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = '<child2 xmlns:ccc="AAA"></child2>';
test_C14nCanonicalization(xml, xpath, expected);
});
it("C14n: Preserve namespace declared in the target xpath node", function () {
const xml = '<root xmlns:aaa="bbb"><child1><child2 xmlns:ccc="ddd"></child2></child1></root>';
const xpath = "/root/child1/child2";
const expected = '<child2 xmlns:aaa="bbb" xmlns:ccc="ddd"></child2>';
test_C14nCanonicalization(xml, xpath, expected);
});
it("C14n: Don't redeclare an attribute's namespace prefix if already in scope", function () {
const xml =
"<root xmlns:aaa='bbb'><child1><child2 xmlns:aaa='bbb' aaa:foo='bar'></child2></child1></root>";
const xpath = "/root/child1/child2";
const expected = '<child2 xmlns:aaa="bbb" aaa:foo="bar"></child2>';
test_C14nCanonicalization(xml, xpath, expected);
});
it("C14n: Don't declare an attribute's namespace prefix if in scope from parent", function () {
const xml =
"<root xmlns:aaa='bbb'><child1><child2><child3 aaa:foo='bar'></child3></child2></child1></root>";
const xpath = "/root/child1";
const expected =
'<child1 xmlns:aaa="bbb"><child2><child3 aaa:foo="bar"></child3></child2></child1>';
test_C14nCanonicalization(xml, xpath, expected);
});
it("C14n: should not has colon when parent namespace has no prefix", function () {
const xml =
"<root xmlns='bbb'><child1><cc:child2 xmlns:cc='ddd'><cc:child3></cc:child3></cc:child2></child1></root>";
const xpath = "//*[local-name()='child3']";
const expected = '<cc:child3 xmlns="bbb" xmlns:cc="ddd"></cc:child3>';
test_C14nCanonicalization(xml, xpath, expected);
});
});