Skip to content

Commit 548fa01

Browse files
authoredMar 8, 2021
Merge pull request #2913 from ognjenjevremovic/test/time-tampering-tests
2 parents a4cc7c0 + 3a5c878 commit 548fa01

File tree

1 file changed

+101
-13
lines changed

1 file changed

+101
-13
lines changed
 

‎test/functions.js

+101-13
Original file line numberDiff line numberDiff line change
@@ -390,22 +390,66 @@
390390
var counter = 0;
391391
var incr = function(){ counter++; };
392392
var throttledIncr = _.throttle(incr, 100);
393-
var origNowFunc = _.now;
393+
var originalNowFunc = Date.now;
394+
var originalGetTimeFunc = Date.prototype.getTime;
394395

395396
throttledIncr();
396-
assert.strictEqual(counter, 1);
397-
_.now = function() {
398-
return new Date(2013, 0, 1, 1, 1, 1);
399-
};
397+
assert.strictEqual(counter, 1, 'incr was called immediately');
398+
399+
Date.prototype.getTime = function() {
400+
return +(new Date(2013, 0, 1, 1, 1, 1));
401+
}
402+
Date.now = function() {
403+
return +(new Date(2013, 0, 1, 1, 1, 1));
404+
}
400405

401406
_.delay(function() {
402407
throttledIncr();
403-
assert.strictEqual(counter, 2);
408+
assert.strictEqual(counter, 2, 'incr was throttled successfully, with tampered system time');
404409
done();
405-
_.now = origNowFunc;
410+
Date.now = originalNowFunc;
411+
Date.prototype.getTime = originalGetTimeFunc;
406412
}, 200);
407413
});
408414

415+
QUnit.test('throttle continues to function after system time is not accessible (or in invalid format)', function(assert) {
416+
assert.expect(3);
417+
var done = assert.async();
418+
var counter = 0;
419+
var incr = function(){ counter++; };
420+
var throttledIncr = _.throttle(incr, 100);
421+
var originalNowFunc = Date.now;
422+
var originalGetTimeFunc = Date.prototype.getTime;
423+
var originalValueOfFunc = Date.prototype.valueOf;
424+
425+
throttledIncr();
426+
assert.strictEqual(counter, 1, 'incr was called immediately');
427+
428+
Date.prototype.valueOf = function() {
429+
return null;
430+
}
431+
Date.prototype.getTime = function() {
432+
return null;
433+
}
434+
Date.now = function() {
435+
return null;
436+
}
437+
438+
_.delay(function() {
439+
throttledIncr();
440+
assert.strictEqual(counter, 2, 'incr was throttled successfully, with tampered system time');
441+
Date.now = originalNowFunc;
442+
Date.prototype.getTime = originalGetTimeFunc;
443+
Date.prototype.valueOf = originalValueOfFunc;
444+
}, 200);
445+
446+
_.delay(function() {
447+
throttledIncr();
448+
assert.strictEqual(counter, 3, 'incr was throttled successfully, after system time method restoration');
449+
done();
450+
}, 400);
451+
});
452+
409453
QUnit.test('throttle re-entrant', function(assert) {
410454
assert.expect(2);
411455
var done = assert.async();
@@ -542,26 +586,70 @@
542586
assert.expect(2);
543587
var done = assert.async();
544588
var counter = 0;
545-
var origNowFunc = _.now;
546589
var debouncedIncr = _.debounce(function(){
547590
counter++;
548591
}, 100, true);
592+
var originalNowFunc = Date.now;
593+
var originalGetTimeFunc = Date.prototype.getTime;
549594

550595
debouncedIncr();
551596
assert.strictEqual(counter, 1, 'incr was called immediately');
552597

553-
_.now = function() {
554-
return new Date(2013, 0, 1, 1, 1, 1);
555-
};
598+
Date.prototype.getTime = function() {
599+
return +(new Date(2013, 0, 1, 1, 1, 1));
600+
}
601+
Date.now = function() {
602+
return +(new Date(2013, 0, 1, 1, 1, 1));
603+
}
556604

557605
_.delay(function() {
558606
debouncedIncr();
559-
assert.strictEqual(counter, 2, 'incr was debounced successfully');
607+
assert.strictEqual(counter, 2, 'incr was debounced successfully, with tampered system time');
560608
done();
561-
_.now = origNowFunc;
609+
Date.now = originalNowFunc;
610+
Date.prototype.getTime = originalGetTimeFunc;
562611
}, 200);
563612
});
564613

614+
QUnit.test('debounce after system time is is not accessible (or in invalid format)', function(assert) {
615+
assert.expect(3);
616+
var done = assert.async();
617+
var counter = 0;
618+
var debouncedIncr = _.debounce(function(){
619+
counter++;
620+
}, 100, true);
621+
var originalNowFunc = Date.now;
622+
var originalGetTimeFunc = Date.prototype.getTime;
623+
var originalValueOfFunc = Date.prototype.valueOf;
624+
625+
debouncedIncr();
626+
assert.strictEqual(counter, 1, 'incr was called immediately');
627+
628+
Date.prototype.valueOf = function() {
629+
return null;
630+
};
631+
Date.prototype.getTime = function() {
632+
return null;
633+
};
634+
Date.now = function() {
635+
return null;
636+
};
637+
638+
_.delay(function() {
639+
debouncedIncr();
640+
assert.strictEqual(counter, 2, 'incr was debounced successfully, with tampered system time');
641+
Date.now = originalNowFunc;
642+
Date.prototype.getTime = originalGetTimeFunc;
643+
Date.prototype.valueOf = originalValueOfFunc;
644+
}, 200);
645+
646+
_.delay(function() {
647+
debouncedIncr();
648+
assert.strictEqual(counter, 3, 'incr was debounced successfully, after system time method restoration');
649+
done();
650+
}, 400);
651+
});
652+
565653
QUnit.test('debounce re-entrant', function(assert) {
566654
assert.expect(2);
567655
var done = assert.async();

0 commit comments

Comments
 (0)
Please sign in to comment.