forked from libxmljs/libxmljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathref_integrity.ts
More file actions
182 lines (145 loc) · 5.47 KB
/
ref_integrity.ts
File metadata and controls
182 lines (145 loc) · 5.47 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
import * as libxml from "../index";
import { XMLElement } from "../index";
if (!global.gc) {
throw new Error("must run with --expose_gc for ref integrity tests");
}
module.exports.gc = function (assert: any) {
var doc = libxml.Document();
doc.node("root")?.node("child")?.node("grandchild")?.parent()?.node("child2");
global.gc();
assert.ok(doc, "doc");
global.gc();
assert.ok(doc.root(), "root");
global.gc();
assert.equal("child", doc.root()?.childNodes()[0]?.name(), "child name");
assert.done();
};
module.exports.references = function (assert: any) {
var nodes = libxml.parseXml("<root> <child> <grandchildren/> </child> <child2/> </root>").childNodes();
global.gc();
assert.ok(nodes[0]?.doc());
assert.equal("child", nodes[1]?.name());
assert.done();
};
// test that double-freeing XmlNode's doesn't cause a segfault
module.exports.double_free = function (assert: any) {
var children = null;
// stick this portion of code into a self-executing function so
// its internal variables can be garbage collected
(function () {
var html = "<html><body><div><span></span></div></body></html>";
var doc = libxml.parseHtml(html);
doc.find("//div").forEach(function (tag) {
// provide a reference to childNodes so they are exposed as XmlNodes
// and therefore subject to V8's garbage collection
children = (tag as XMLElement).childNodes();
(tag as XMLElement).remove();
});
})();
global.gc();
//@ts-ignore
assert.ok(children[0].attrs());
assert.done();
};
module.exports.freed_namespace_unwrappable = function (assert: any) {
var doc = libxml.parseXml("<?xml version='1.0' encoding='UTF-8'?><root></root>");
var el: XMLElement | null = libxml.Element(doc, "foo");
var ns = el.namespace("bar", null);
el = null;
global.gc();
ns = null;
global.gc();
assert.done();
};
module.exports.unlinked_tree_persistence_parent_proxied_first = function (assert: any) {
var doc = makeDocument();
var parent_node: XMLElement | null = doc.get("//middle") as XMLElement;
var child_node = doc.get("//inner") as XMLElement;
parent_node.remove();
parent_node = null;
collectGarbage();
assert.equal("inner", child_node.name()); // works with >= v0.14.3
assert.done();
};
module.exports.unlinked_tree_proxied_leaf_persistent_ancestor_first = function (assert: any) {
var doc = makeDocument();
var ancestor: XMLElement | null = doc.get("//middle") as XMLElement;
var leaf = doc.get("//center") as XMLElement;
ancestor.remove();
ancestor = null;
collectGarbage();
assert.equal("center", leaf.name()); // fails with v0.14.3, v0.15
assert.done();
};
module.exports.unlinked_tree_proxied_leaf_persistent_descendant_first = function (assert: any) {
var doc = makeDocument();
var leaf = doc.get("//center") as XMLElement;
var ancestor: XMLElement | null = doc.get("//middle") as XMLElement;
ancestor.remove(); // make check here?
ancestor = null;
collectGarbage();
assert.equal("center", leaf.name());
assert.done();
};
module.exports.unlinked_tree_persistence_child_proxied_first = function (assert: any) {
var doc = makeDocument();
var child_node = doc.get("//inner") as XMLElement;
var parent_node: XMLElement | null = doc.get("//middle") as XMLElement;
parent_node.remove();
parent_node = null;
collectGarbage();
assert.equal("inner", child_node.name()); // fails with v0.14.3, v0.15
assert.done();
};
module.exports.unlinked_tree_leaf_persistence_with_proxied_ancestor = function (assert: any) {
var doc = makeDocument();
var proxied_ancestor = doc.get("//inner") as XMLElement;
var leaf = doc.get("//center");
(doc.get("//middle") as XMLElement).remove();
leaf = null;
collectGarbage();
leaf = proxied_ancestor.get(".//center") as XMLElement;
assert.equal("center", leaf.name());
assert.done();
};
module.exports.unlinked_tree_leaf_persistence_with_peer_proxy = function (assert: any) {
var doc = makeDocument();
var leaf: XMLElement | null = doc.get("//left") as XMLElement;
var peer = doc.get("//right") as XMLElement;
(doc.get("//middle") as XMLElement).remove();
leaf = null;
collectGarbage();
leaf = peer.parent()?.get("./left") as XMLElement;
assert.equal("left", leaf.name());
assert.done();
};
module.exports.set_text_clobbering_children = function (assert: any) {
var doc = libxml.parseHtml("<root><child><inner>old</inner></child></root>");
var child = doc.get("//child") as XMLElement;
var inner = doc.get("//inner") as XMLElement;
child.text("new");
assert.equal(doc, inner.parent());
assert.equal("old", inner.text());
assert.done();
};
function makeDocument() {
var body =
"<?xml version='1.0' encoding='UTF-8'?>\n" +
"<root><outer><middle><inner><left/><center/><right/></inner></middle></outer></root>";
return libxml.parseXml(body);
}
function collectGarbage(minCycles?: number, maxCycles?: number) {
minCycles = minCycles || 3;
maxCycles = maxCycles || 10;
var cycles = 0;
var freedRss = 0;
var usage = process.memoryUsage();
do {
global.gc();
var usageAfterGc = process.memoryUsage();
freedRss = usage.rss - usageAfterGc.rss;
usage = usageAfterGc;
cycles++;
} while (cycles < minCycles || (freedRss !== 0 && cycles < maxCycles));
return usage;
}