const expect = require('expect.js');
const nthCheck = require('../../../lib');
const compile = nthCheck.compile;

describe("CVE-2021-3803", () => {
    it("test for CVE-2021-3803", () => {
	const l = 1024;
	const inc = 2;
	var time_cost = [];
	var oldi = 0;
	for(var i = 1; i <= l; i=i*inc) {
	    var time = Date.now();
	    var attack_str = '2n' + ' '.repeat(i*10000)+"!";
	    try {
		nthCheck.parse(attack_str) 
	    }
	    catch(err) {
		time_cost[i] = Date.now() - time;
		console.log("attack_str.length: " + attack_str.length + ": " + time_cost[i]+" ms")
		if(i>1 && time_cost[oldi] > 2 && time_cost[i] > 2)
		    if(time_cost[i] > time_cost[oldi]*(inc+0.5))
			return 1;
	    }
	    oldi = i;
	}
    })
})
