Download dev/tests/web/t_fuzz.js from mchuca/ServerMonitor: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/mchuca/ServerMonitor/resolve/main/dev/tests/web/t_fuzz.js
- Command line
-
hf download hf://mchuca/ServerMonitor/dev/tests/web/t_fuzz.js
-
curl -L -o t_fuzz.js https://huggingface.co/mchuca/ServerMonitor/resolve/main/dev/tests/web/t_fuzz.js
2.77 kB
| // A broken frame must never throw (an exception in the parser would stop the whole monitor). node t_fuzz.js <project root> <frames file> | |
| const fs = require('fs'), path = require('path'), { check, done } = require('./lib'); | |
| const plat = require(path.join(process.argv[2], 'core/platform.js')), text = fs.readFileSync(process.argv[3], 'utf8'); | |
| const frames = text.split('@@SNAP_END').filter(f => f.includes('@@SNAP_BEGIN')).map(f => f.slice(f.indexOf('@@SNAP_BEGIN'))); | |
| let seed = 12345; const rnd = n => (seed = (seed * 1103515245 + 12345) & 0x7fffffff) % n; | |
| const junk = ['', ' ', '|', '||||||||||||', ':', '=', '@@S ', '@@S procs', '-1', 'NaN', '1e999', '0x10', '\u0000', '\t\t', 'a'.repeat(5000), '9'.repeat(400), '__proto__=1', '__proto__|1|2|3|4|5|6|7|8|9', 'constructor 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16', 'cpu', 'cpu0', '1 (a b) S', '%s%n', '\r']; | |
| function mutate(f) { | |
| let lines = f.split('\n'); | |
| for (let k = 0, n = 1 + rnd(12); k < n; ++k) { | |
| const i = rnd(lines.length), how = rnd(8); | |
| if (how === 0) lines.splice(i, 1 + rnd(20)); | |
| else if (how === 1) lines[i] = junk[rnd(junk.length)]; | |
| else if (how === 2) lines.splice(i, 0, junk[rnd(junk.length)]); | |
| else if (how === 3) lines[i] = lines[i].slice(0, rnd(lines[i].length + 1)); | |
| else if (how === 4) lines[i] = lines[i].replace(/\d+/g, () => junk[rnd(junk.length)]); | |
| else if (how === 5) lines[i] = lines[i].split(/([ |:=])/).sort(() => rnd(3) - 1).join(''); | |
| else if (how === 6) lines = lines.slice(0, rnd(lines.length)); | |
| else lines.splice(i, 0, lines[rnd(lines.length)]); | |
| } | |
| return lines.join(rnd(2) ? '\n' : '\r\n'); | |
| } | |
| let thrown = 0, first = '', n = 0, bad = 0; | |
| for (let round = 0; round < 3000; ++round) { | |
| const st = {}; | |
| for (let k = 0; k < 4; ++k) { | |
| const f = rnd(4) ? mutate(frames[rnd(frames.length)]) : frames[rnd(frames.length)]; | |
| try { | |
| const m = plat.parse(f, st, Date.now()); ++n; | |
| const flat = JSON.stringify(m); // what the page gets | |
| if (!Array.isArray(m.procs) || !Array.isArray(m.cores) || typeof m.cpuCount !== 'number' || !(m.cpuCount >= 0) || /"(cpuUsage|memPct|maxDiskPct|load1|netRxBps)":null/.test(flat)) ++bad, first ||= 'non-finite value: ' + flat.match(/"\w+":null/)?.[0]; | |
| } catch (e) { ++thrown, first ||= e.stack.split('\n').slice(0, 3).join(' | '); } | |
| } | |
| } | |
| check(`${n + thrown} mutated ${plat.ui.name} frames: the parser never throws`, !thrown, thrown ? `${thrown} exceptions, first: ${first}` : ''); | |
| check('main values stay finite numbers', !bad, bad ? `${bad} frames, first: ${first}` : ''); | |
| check('nothing leaked onto Object.prototype', ({}).polluted === undefined && Object.keys({}).length === 0 && ({})[1] === undefined); | |
| done(); | |