forked from xpol/lua-rapidjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_encode_lib_spec.lua
More file actions
303 lines (256 loc) · 9.88 KB
/
Copy pathfuzz_encode_lib_spec.lua
File metadata and controls
303 lines (256 loc) · 9.88 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
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
require 'busted.runner'()
describe('tools.fuzz_encode_lib', function()
local fuzz = require('tools.fuzz_encode_lib')
local rapidjson = require('rapidjson')
describe('parse_config', function()
it('uses production defaults', function()
local cfg = fuzz.parse_config({})
assert.are.equal(3600, cfg.duration)
assert.are.equal(5, cfg.interval)
assert.are.equal(1, cfg.workers)
assert.are.equal(1, cfg.worker_id)
assert.are.equal(true, cfg.sort_keys)
assert.are.equal('number', type(cfg.seed))
end)
it('accepts numeric and boolean overrides', function()
local cfg = fuzz.parse_config({
DURATION = '12',
INTERVAL = '3',
WORKERS = '2',
WORKER_ID = '2',
SEED = '99',
SORT_KEYS = '0',
})
assert.are.equal(12, cfg.duration)
assert.are.equal(3, cfg.interval)
assert.are.equal(2, cfg.workers)
assert.are.equal(2, cfg.worker_id)
assert.are.equal(99, cfg.seed)
assert.are.equal(false, cfg.sort_keys)
end)
it('treats numeric zero as disabling sorted keys', function()
local cfg = fuzz.parse_config({ SORT_KEYS = 0 })
assert.are.equal(false, cfg.sort_keys)
end)
end)
describe('env_from_args', function()
it('turns KEY=VALUE args into config environment entries', function()
local env = fuzz.env_from_args({
'DURATION=2',
'INTERVAL=1',
'SEED=123',
'WORKERS=1',
})
assert.are.equal('2', env.DURATION)
assert.are.equal('1', env.INTERVAL)
assert.are.equal('123', env.SEED)
assert.are.equal('1', env.WORKERS)
end)
end)
describe('new_rng', function()
it('is deterministic for the same seed', function()
local a = fuzz.new_rng(123)
local b = fuzz.new_rng(123)
assert.are.equal(a:int(1, 1000000), b:int(1, 1000000))
assert.are.equal(a:int(1, 1000000), b:int(1, 1000000))
assert.are.equal(a:bool(), b:bool())
end)
end)
describe('format_summary', function()
it('formats the progress counters', function()
local line = fuzz.format_summary({
elapsed = 5,
total = 100,
encoded = 99,
encode_errors = 1,
validation_failures = 0,
rate = 20,
seed = 123,
last_case_id = 100,
worker_id = 1,
})
assert.matches('worker=1', line, 1, true)
assert.matches('elapsed=5s', line, 1, true)
assert.matches('total=100', line, 1, true)
assert.matches('encoded=99', line, 1, true)
assert.matches('encode_errors=1', line, 1, true)
assert.matches('validation_failures=0', line, 1, true)
assert.matches('rate=20.00/s', line, 1, true)
assert.matches('seed=123', line, 1, true)
assert.matches('last_case=100', line, 1, true)
end)
end)
describe('generate_case', function()
it('generates deterministic schema-guided cases with selected metadata', function()
local a = fuzz.generate_case(fuzz.new_rng(321), 1, rapidjson)
local b = fuzz.generate_case(fuzz.new_rng(321), 1, rapidjson)
assert.are.same(a.value, b.value)
assert.are.same(a.expected, b.expected)
assert.are.equal('number', type(a.id))
assert.are.equal('string', type(a.schema))
assert.are.equal('schema_guided', a.kind)
assert.are.equal('object', a.expected.top_level_kind)
assert.are.equal('table', type(a.value.fuzz))
assert.is_true(#a.expected.objects >= 1)
assert.is_true(#a.expected.arrays >= 1)
assert.is_true(#a.expected.scalars >= 1)
end)
it('adds pure recursive random cases with nested objects and arrays', function()
local case = fuzz.generate_case(fuzz.new_rng(98765), 3, rapidjson)
assert.are.equal('recursive_random', case.kind)
assert.are.equal('recursive_random', case.schema)
assert.are.equal('table', type(case.value))
assert.are.equal('table', type(case.value.random))
assert.are.equal('table', type(case.expected.random))
assert.is_true(case.expected.random.max_depth >= 3)
assert.is_true(case.expected.random.object_count >= 2)
assert.is_true(case.expected.random.array_count >= 1)
assert.is_true(#case.expected.objects >= case.expected.random.object_count)
assert.is_true(#case.expected.arrays >= case.expected.random.array_count)
end)
it('tracks recursive random arrays from the generated core', function()
local case = fuzz.generate_case(fuzz.new_rng(98765), 3, rapidjson)
local saw_core_array = false
for _, entry in ipairs(case.expected.arrays) do
if entry.path:match('^%$%.random') then
saw_core_array = true
end
end
assert.is_true(saw_core_array)
end)
it('emits rapidjson null sentinels that round-trip as JSON null', function()
local case = fuzz.generate_case(fuzz.new_rng(100), 10, rapidjson)
assert.are.equal('paginated_list', case.schema)
assert.are.equal(rapidjson.null, case.value.links.previous)
local encoded = rapidjson.encode(case.value)
local decoded = rapidjson.decode(encoded)
assert.matches('"previous":null', encoded, 1, true)
assert.are.equal(rapidjson.null, decoded.links.previous)
end)
it('requires a real rapidjson null sentinel', function()
assert.has_error(function()
fuzz.generate_case(fuzz.new_rng(1), 1, {})
end, 'rapidjson.null is required')
end)
it('rejects fake table null sentinels', function()
assert.has_error(function()
fuzz.generate_case(fuzz.new_rng(1), 1, { null = {} })
end, 'rapidjson.null is required')
end)
it('runs pure recursive random cases at least as often as schema-guided cases', function()
local rng = fuzz.new_rng(1)
local seen = {}
local counts = {
schema_guided = 0,
recursive_random = 0,
}
for case_id = 1, 30 do
local case = fuzz.generate_case(rng, case_id, rapidjson)
counts[case.kind] = counts[case.kind] + 1
if case.kind == 'schema_guided' then
seen[case.schema] = true
end
end
assert.is_true(counts.recursive_random >= counts.schema_guided)
assert.are.equal(10, counts.schema_guided)
assert.are.equal(20, counts.recursive_random)
assert.is_true(seen.llm_response)
assert.is_true(seen.github_issue)
assert.is_true(seen.social_feed)
assert.is_true(seen.paginated_list)
assert.is_true(seen.metadata_config)
end)
end)
describe('validate_encoded_case', function()
it('accepts a generated case encoded with sorted keys', function()
local case = fuzz.generate_case(fuzz.new_rng(77), 1, rapidjson)
local json = rapidjson.encode(case.value, { sort_keys = true })
local ok, err = fuzz.validate_encoded_case(rapidjson, case, json)
assert.is_true(ok)
assert.is_nil(err)
end)
it('rejects unsorted encoded object keys for tracked objects', function()
local case = {
id = 1,
kind = 'manual',
schema = 'manual',
value = { b = 1, a = 2 },
expected = {
top_level_kind = 'object',
objects = {
{ path = '$', key_count = 2, keys = { 'a', 'b' } },
},
arrays = {},
scalars = {},
},
}
local ok, err = fuzz.validate_encoded_case(rapidjson, case, '{"b":1,"a":2}')
assert.is_false(ok)
assert.matches('key order', err, 1, true)
end)
it('rejects unsorted nested object keys for tracked object paths', function()
local case = {
id = 2,
kind = 'manual',
schema = 'manual',
value = { a = { b = 1, a = 2 } },
expected = {
top_level_kind = 'object',
objects = {
{ path = '$.a', key_count = 2, keys = { 'a', 'b' } },
},
arrays = {},
scalars = {},
},
}
local ok, err = fuzz.validate_encoded_case(rapidjson, case, '{"a":{"b":1,"a":2}}')
assert.is_false(ok)
assert.matches('key order', err, 1, true)
end)
it('validates recursive_random core metadata after encode and decode', function()
local case = fuzz.generate_case(fuzz.new_rng(98765), 2, rapidjson)
local json = rapidjson.encode(case.value, { sort_keys = true })
assert.are.equal('recursive_random', case.kind)
assert.are.equal('recursive_random', case.schema)
assert.are.equal('table', type(case.expected.random))
local ok, err = fuzz.validate_encoded_case(rapidjson, case, json)
assert.is_true(ok)
assert.is_nil(err)
end)
it('returns decode diagnostics when JSON cannot be decoded', function()
local ok, err = fuzz.validate_encoded_case(rapidjson, { expected = {} }, '{"a":}')
assert.is_false(ok)
assert.matches('decode failed:', err, 1, true)
end)
end)
describe('format_failure', function()
it('is reproducible and includes fuzz failure diagnostics', function()
local case = {
id = 42,
kind = 'manual',
schema = 'manual_schema',
value = { b = 1, a = { true, rapidjson.null } },
}
local details = {
seed = 12345,
worker_id = 2,
case = case,
reason = 'key order mismatch at $',
json = '{"b":1,"a":[true,null]}',
}
local first = fuzz.format_failure(details)
local second = fuzz.format_failure(details)
assert.are.equal(first, second)
assert.matches('FUZZ FAILURE', first, 1, true)
assert.matches('seed=12345', first, 1, true)
assert.matches('worker=2', first, 1, true)
assert.matches('case=42', first, 1, true)
assert.matches('kind=manual', first, 1, true)
assert.matches('schema=manual_schema', first, 1, true)
assert.matches('reason=key order mismatch at $', first, 1, true)
assert.matches('value=', first, 1, true)
assert.matches('"a"', first, 1, true)
assert.matches('json={"b":1,"a":[true,null]}', first, 1, true)
end)
end)
end)