rxerces 0.6.1 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,409 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe "XPath Validation Cache" do
6
+ let(:doc) { RXerces::XML::Document.parse('<root><item id="1"/><item id="2"/></root>') }
7
+
8
+ # Helper to generate unique XPath expressions that work with or without Xalan
9
+ # With Xalan: uses attribute predicates for more realistic expressions
10
+ # Without Xalan: uses unique element names
11
+ def unique_xpath(prefix, index)
12
+ if RXerces.xalan_enabled?
13
+ "//item[@id='#{prefix}_#{index}']"
14
+ else
15
+ "//#{prefix}#{index}"
16
+ end
17
+ end
18
+
19
+ before(:each) do
20
+ # Reset to default state before each test
21
+ RXerces.cache_xpath_validation = true
22
+ RXerces.xpath_validation_cache_max_size = 10_000
23
+ RXerces.clear_xpath_validation_cache
24
+ end
25
+
26
+ after(:all) do
27
+ # Restore defaults after all tests
28
+ RXerces.cache_xpath_validation = true
29
+ RXerces.xpath_validation_cache_max_size = 10_000
30
+ end
31
+
32
+ describe "configuration methods" do
33
+ describe ".cache_xpath_validation?" do
34
+ it "returns true by default" do
35
+ expect(RXerces.cache_xpath_validation?).to be true
36
+ end
37
+
38
+ it "returns false when caching is disabled" do
39
+ RXerces.cache_xpath_validation = false
40
+ expect(RXerces.cache_xpath_validation?).to be false
41
+ end
42
+ end
43
+
44
+ describe ".cache_xpath_validation=" do
45
+ it "enables caching when set to true" do
46
+ RXerces.cache_xpath_validation = false
47
+ RXerces.cache_xpath_validation = true
48
+ expect(RXerces.cache_xpath_validation?).to be true
49
+ end
50
+
51
+ it "disables caching when set to false" do
52
+ RXerces.cache_xpath_validation = false
53
+ expect(RXerces.cache_xpath_validation?).to be false
54
+ end
55
+
56
+ it "accepts truthy values" do
57
+ RXerces.cache_xpath_validation = 1
58
+ expect(RXerces.cache_xpath_validation?).to be true
59
+ end
60
+
61
+ it "accepts falsy values" do
62
+ RXerces.cache_xpath_validation = nil
63
+ expect(RXerces.cache_xpath_validation?).to be false
64
+ end
65
+ end
66
+
67
+ describe ".xpath_validation_cache_size" do
68
+ it "returns 0 when cache is empty" do
69
+ expect(RXerces.xpath_validation_cache_size).to eq(0)
70
+ end
71
+
72
+ it "increases after XPath queries" do
73
+ doc.xpath("//item")
74
+ expect(RXerces.xpath_validation_cache_size).to eq(1)
75
+ end
76
+
77
+ it "does not double-count repeated expressions" do
78
+ 3.times { doc.xpath("//item") }
79
+ expect(RXerces.xpath_validation_cache_size).to eq(1)
80
+ end
81
+
82
+ it "counts unique expressions" do
83
+ doc.xpath("//item")
84
+ if RXerces.xalan_enabled?
85
+ doc.xpath("//item[@id='1']")
86
+ else
87
+ doc.xpath("//root")
88
+ end
89
+ doc.xpath("/root/item")
90
+ expect(RXerces.xpath_validation_cache_size).to eq(3)
91
+ end
92
+ end
93
+
94
+ describe ".xpath_validation_cache_max_size" do
95
+ it "returns 10000 by default" do
96
+ expect(RXerces.xpath_validation_cache_max_size).to eq(10_000)
97
+ end
98
+ end
99
+
100
+ describe ".xpath_validation_cache_max_size=" do
101
+ it "sets the maximum cache size" do
102
+ RXerces.xpath_validation_cache_max_size = 5000
103
+ expect(RXerces.xpath_validation_cache_max_size).to eq(5000)
104
+ end
105
+
106
+ it "accepts large values" do
107
+ RXerces.xpath_validation_cache_max_size = 100_000
108
+ expect(RXerces.xpath_validation_cache_max_size).to eq(100_000)
109
+ end
110
+
111
+ it "accepts zero" do
112
+ RXerces.xpath_validation_cache_max_size = 0
113
+ expect(RXerces.xpath_validation_cache_max_size).to eq(0)
114
+ end
115
+
116
+ it "raises TypeError for non-integer values" do
117
+ expect { RXerces.xpath_validation_cache_max_size = "1000" }.to raise_error(TypeError)
118
+ expect { RXerces.xpath_validation_cache_max_size = 1.5 }.to raise_error(TypeError)
119
+ expect { RXerces.xpath_validation_cache_max_size = nil }.to raise_error(TypeError)
120
+ end
121
+
122
+ it "raises ArgumentError for negative values" do
123
+ expect { RXerces.xpath_validation_cache_max_size = -1 }.to raise_error(ArgumentError)
124
+ expect { RXerces.xpath_validation_cache_max_size = -100 }.to raise_error(ArgumentError)
125
+ end
126
+ end
127
+
128
+ describe ".clear_xpath_validation_cache" do
129
+ it "empties the cache" do
130
+ doc.xpath("//item")
131
+ if RXerces.xalan_enabled?
132
+ doc.xpath("//item[@id='1']")
133
+ else
134
+ doc.xpath("//root")
135
+ end
136
+ expect(RXerces.xpath_validation_cache_size).to be > 0
137
+
138
+ RXerces.clear_xpath_validation_cache
139
+ expect(RXerces.xpath_validation_cache_size).to eq(0)
140
+ end
141
+
142
+ it "returns nil" do
143
+ expect(RXerces.clear_xpath_validation_cache).to be_nil
144
+ end
145
+ end
146
+
147
+ describe ".xalan_enabled?" do
148
+ it "returns a boolean" do
149
+ expect([true, false]).to include(RXerces.xalan_enabled?)
150
+ end
151
+ end
152
+
153
+ describe ".xpath_max_length" do
154
+ it "returns 10000 by default" do
155
+ expect(RXerces.xpath_max_length).to eq(10_000)
156
+ end
157
+ end
158
+
159
+ describe ".xpath_max_length=" do
160
+ after(:each) do
161
+ # Restore default after each test
162
+ RXerces.xpath_max_length = 10_000
163
+ end
164
+
165
+ it "sets the maximum XPath expression length" do
166
+ RXerces.xpath_max_length = 5000
167
+ expect(RXerces.xpath_max_length).to eq(5000)
168
+ end
169
+
170
+ it "accepts zero to disable the limit" do
171
+ RXerces.xpath_max_length = 0
172
+ expect(RXerces.xpath_max_length).to eq(0)
173
+ end
174
+
175
+ it "raises TypeError for non-integer values" do
176
+ expect { RXerces.xpath_max_length = "1000" }.to raise_error(TypeError)
177
+ expect { RXerces.xpath_max_length = 1.5 }.to raise_error(TypeError)
178
+ expect { RXerces.xpath_max_length = nil }.to raise_error(TypeError)
179
+ end
180
+
181
+ it "raises ArgumentError for negative values" do
182
+ expect { RXerces.xpath_max_length = -1 }.to raise_error(ArgumentError)
183
+ expect { RXerces.xpath_max_length = -100 }.to raise_error(ArgumentError)
184
+ end
185
+
186
+ it "rejects XPath expressions exceeding the limit" do
187
+ RXerces.xpath_max_length = 50
188
+ expect {
189
+ doc.xpath("//" + "a" * 50)
190
+ }.to raise_error(ArgumentError, /too long/)
191
+ end
192
+
193
+ it "allows XPath expressions within the limit" do
194
+ RXerces.xpath_max_length = 100
195
+ expect { doc.xpath("//item") }.not_to raise_error
196
+ end
197
+
198
+ it "allows any length when set to zero" do
199
+ RXerces.xpath_max_length = 0
200
+ # This would normally exceed the default 10k limit
201
+ long_xpath = "//" + "a" * 15_000
202
+ # Should not raise "too long" error - verify by checking it doesn't match that pattern
203
+ begin
204
+ doc.xpath(long_xpath)
205
+ rescue ArgumentError => e
206
+ expect(e.message).not_to match(/too long/)
207
+ end
208
+ end
209
+ end
210
+ end
211
+
212
+ describe "caching behavior" do
213
+ it "caches validated expressions when enabled" do
214
+ RXerces.cache_xpath_validation = true
215
+ doc.xpath("//item")
216
+ expect(RXerces.xpath_validation_cache_size).to eq(1)
217
+ end
218
+
219
+ it "does not cache when disabled" do
220
+ RXerces.cache_xpath_validation = false
221
+ doc.xpath("//item")
222
+ expect(RXerces.xpath_validation_cache_size).to eq(0)
223
+ end
224
+
225
+ it "reuses cached validations for identical expressions" do
226
+ # This is implicitly tested by the fact that repeated queries
227
+ # don't increase cache size
228
+ xpath = RXerces.xalan_enabled? ? "//item[@id='1']" : "//item"
229
+ 5.times { doc.xpath(xpath) }
230
+ expect(RXerces.xpath_validation_cache_size).to eq(1)
231
+ end
232
+
233
+ it "caches expressions from different documents" do
234
+ doc2 = RXerces::XML::Document.parse('<data><value/></data>')
235
+
236
+ doc.xpath("//item")
237
+ doc2.xpath("//value")
238
+ doc.xpath("//item") # Should hit cache
239
+ doc2.xpath("//value") # Should hit cache
240
+
241
+ expect(RXerces.xpath_validation_cache_size).to eq(2)
242
+ end
243
+
244
+ it "caches expressions from node-level xpath calls" do
245
+ root = doc.root
246
+ root.xpath(".//item")
247
+ expect(RXerces.xpath_validation_cache_size).to eq(1)
248
+ end
249
+
250
+ it "shares cache between document and node xpath calls" do
251
+ doc.xpath("//item")
252
+ doc.root.xpath("//item") # Same expression, should reuse cache
253
+ expect(RXerces.xpath_validation_cache_size).to eq(1)
254
+ end
255
+
256
+ it "respects max cache size" do
257
+ RXerces.xpath_validation_cache_max_size = 3
258
+
259
+ doc.xpath("//a")
260
+ doc.xpath("//b")
261
+ doc.xpath("//c")
262
+ initial_size = RXerces.xpath_validation_cache_size
263
+
264
+ # Cache should be at max
265
+ expect(initial_size).to eq(3)
266
+
267
+ # Additional expressions should not increase size beyond max
268
+ doc.xpath("//d")
269
+ doc.xpath("//e")
270
+ expect(RXerces.xpath_validation_cache_size).to eq(3)
271
+ end
272
+
273
+ it "uses LRU eviction when cache is full" do
274
+ RXerces.xpath_validation_cache_max_size = 3
275
+
276
+ # Add 3 expressions: //a is oldest, //c is newest
277
+ doc.xpath("//a")
278
+ doc.xpath("//b")
279
+ doc.xpath("//c")
280
+
281
+ # Access //a again to make it most recently used
282
+ # Now order is: //a (newest), //c, //b (oldest)
283
+ doc.xpath("//a")
284
+
285
+ # Add //d, which should evict //b (least recently used)
286
+ doc.xpath("//d")
287
+ expect(RXerces.xpath_validation_cache_size).to eq(3)
288
+
289
+ # //a should still be cached (was accessed recently)
290
+ # We can verify by checking that accessing it doesn't change cache size
291
+ doc.xpath("//a")
292
+ expect(RXerces.xpath_validation_cache_size).to eq(3)
293
+
294
+ # //b was evicted, adding it again should evict //c (now oldest)
295
+ doc.xpath("//b")
296
+ expect(RXerces.xpath_validation_cache_size).to eq(3)
297
+
298
+ # Cache should now contain: //b, //a, //d (in MRU order)
299
+ # //c was evicted
300
+ end
301
+ end
302
+
303
+ describe "thread safety" do
304
+ it "handles concurrent xpath queries without errors" do
305
+ threads = 10.times.map do |i|
306
+ Thread.new do
307
+ 100.times do |j|
308
+ doc.xpath(unique_xpath("t#{i}", j))
309
+ end
310
+ end
311
+ end
312
+
313
+ expect { threads.each(&:join) }.not_to raise_error
314
+ end
315
+
316
+ it "handles concurrent cache configuration changes" do
317
+ threads = []
318
+
319
+ # Thread toggling cache on/off
320
+ threads << Thread.new do
321
+ 50.times do
322
+ RXerces.cache_xpath_validation = false
323
+ RXerces.cache_xpath_validation = true
324
+ end
325
+ end
326
+
327
+ # Threads doing xpath queries
328
+ 3.times do |i|
329
+ threads << Thread.new do
330
+ 50.times do |j|
331
+ doc.xpath(unique_xpath("c#{i}", j))
332
+ end
333
+ end
334
+ end
335
+
336
+ expect { threads.each(&:join) }.not_to raise_error
337
+ end
338
+
339
+ it "handles concurrent cache clearing" do
340
+ threads = []
341
+
342
+ # Thread clearing cache periodically
343
+ threads << Thread.new do
344
+ 20.times do
345
+ sleep(0.001)
346
+ RXerces.clear_xpath_validation_cache
347
+ end
348
+ end
349
+
350
+ # Threads doing xpath queries
351
+ 3.times do |i|
352
+ threads << Thread.new do
353
+ 100.times do |j|
354
+ doc.xpath(unique_xpath("r#{i}", j))
355
+ end
356
+ end
357
+ end
358
+
359
+ expect { threads.each(&:join) }.not_to raise_error
360
+ end
361
+
362
+ it "returns consistent cache size under concurrent access" do
363
+ # Fill cache with known expressions
364
+ 10.times { |i| doc.xpath("//item#{i}") }
365
+ initial_size = RXerces.xpath_validation_cache_size
366
+
367
+ # Read cache size from multiple threads
368
+ sizes = []
369
+ mutex = Mutex.new
370
+
371
+ threads = 5.times.map do
372
+ Thread.new do
373
+ 10.times do
374
+ size = RXerces.xpath_validation_cache_size
375
+ mutex.synchronize { sizes << size }
376
+ end
377
+ end
378
+ end
379
+
380
+ threads.each(&:join)
381
+
382
+ # All reads should return consistent values (either the initial
383
+ # size or values after any concurrent modifications)
384
+ expect(sizes).to all(be >= 0)
385
+ end
386
+ end
387
+
388
+ describe "interaction with validation" do
389
+ it "still validates expressions even when cached" do
390
+ # First call validates and caches
391
+ doc.xpath("//item")
392
+
393
+ # Invalid expression should still be rejected
394
+ expect {
395
+ doc.xpath("//item[@id=''] or 1=1")
396
+ }.to raise_error(ArgumentError)
397
+ end
398
+
399
+ it "caches valid expressions only" do
400
+ # Try an invalid expression
401
+ expect {
402
+ doc.xpath("")
403
+ }.to raise_error(ArgumentError)
404
+
405
+ # Cache should not have increased
406
+ expect(RXerces.xpath_validation_cache_size).to eq(0)
407
+ end
408
+ end
409
+ end