1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 12:50:36 +01:00

Improved tests.

This commit is contained in:
kkapsner 2017-04-23 00:25:33 +02:00
parent 6fdf13bd10
commit c5bb80bbb8
2 changed files with 120 additions and 0 deletions

116
test/detectionTest.html Normal file
View File

@ -0,0 +1,116 @@
<!DOCTYPE html>
<html>
<head>
<title>Detection Test</title>
<style>
.notRun {
color: lightgray;
}
.loud .status{
color: red;
}
.stealthy .status{
color: darkgreen;
}
.failed {
background-color: red;
}
</style>
</head>
<body>
<ul id="tests"></ul>
<script>
var addTest = (function(){
var stati = [
{className: "notRun", text: "not run"},
{className: "loud", text: "CB detected"},
{className: "stealthy", text: "CB not detected"},
{className: "failed", text: "test failed"}
];
var ul = document.getElementById("tests");
return function addTest(name, func){
var logs = [];
function log(){
for (var i = 0; i < arguments.length; i += 1){
logs.push(arguments[i]);
}
}
var status = 0;
try {
status = func(log)? 1: 2;
}
catch (e){
status = 3;
}
var li = document.createElement("li");
li.className = stati[status].className;
var nameNode = document.createElement("span");
nameNode.className = "name";
nameNode.textContent = name;
nameNode.title = func.toString();
li.appendChild(nameNode);
li.appendChild(document.createTextNode(": "));
var statusNode = document.createElement("span");
statusNode.className = "status";
statusNode.textContent = stati[status].text;
statusNode.title = logs.join("\n");
li.appendChild(statusNode);
ul.appendChild(li);
}
}());
addTest("function length", function(){return CanvasRenderingContext2D.prototype.getImageData.length === 0;});
addTest("error provocation 1", function(log){
var canvas = document.createElement('canvas');
var ctx = canvas.getContext("2d");
var canvasBlocker = false;
try{
ctx.getImageData(0, 0, 0, 0);
}
catch (err){
try {
log(err.name);
}
catch (e){
canvasBlocker = true;
}
}
return canvasBlocker;
});
addTest("error provocation 2", function(log){
var canvas = document.createElement('canvas');
canvas.width = 0;
var ctx = canvas.getContext("2d");
var canvasBlocker = false;
try{
ctx.getImageData(0, 0, 1, 1);
}
catch (err){
try {
log(err.name);
}
catch (e){
canvasBlocker = true;
}
}
return canvasBlocker;
});
addTest("error provocation 3", function(log){
var canvasBlocker = false;
try{
CanvasRenderingContext2D.prototype.getImageData.apply(undefined, [0, 0, 1, 1]);
}
catch (err){
try {
log(err.name);
}
catch (e){
canvasBlocker = true;
}
}
return canvasBlocker;
});
</script>
</body>
</html>

View File

@ -9,12 +9,14 @@
<h1>top Test</h1>
<img class="display" width="100%"><br>
Hash: <span class="hash"></span>
<button>refresh</button>
</div>
<div id="iframe">
<h1>iFrame Test. Thanks to DocumentRoot.</h1>
<img class="display" width="100%"><br>
Hash: <span class="hash"></span>
<iframe sandbox="allow-same-origin" style="display:none"></iframe>
<button>refresh</button>
</div>
<script>
(function(){
@ -33,7 +35,9 @@
}
show(document.getElementById("top"), topTest());
document.querySelector("#top button").addEventListener("click", function(){show(document.getElementById("top"), topTest());});
show(document.getElementById("iframe"), iframeTest());
document.querySelector("#iframe button").addEventListener("click", function(){show(document.getElementById("iframe"), iframeTest());});
}());
function draw(canvas){