forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1272239 - P3. Testcase, test gethash. r=francois
--HG-- extra : rebase_source : df9a20cc3ca80df5bf630a4ccb3a16adc4511ac5
- Loading branch information
Showing
8 changed files
with
382 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#styleBad { visibility: hidden; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
toolkit/components/url-classifier/tests/mochitest/gethash.sjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
const CC = Components.Constructor; | ||
const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", | ||
"nsIBinaryInputStream", | ||
"setInputStream"); | ||
|
||
function handleRequest(request, response) | ||
{ | ||
var query = {}; | ||
request.queryString.split('&').forEach(function (val) { | ||
var idx = val.indexOf('='); | ||
query[val.slice(0, idx)] = unescape(val.slice(idx + 1)); | ||
}); | ||
|
||
// Store fullhash in the server side. | ||
if ("list" in query && "fullhash" in query) { | ||
// In the server side we will store: | ||
// 1. All the full hashes for a given list | ||
// 2. All the lists we have right now | ||
// data is separate by '\n' | ||
let list = query["list"]; | ||
let hashes = getState(list); | ||
|
||
let hash = base64ToString(query["fullhash"]); | ||
hashes += hash + "\n"; | ||
setState(list, hashes); | ||
|
||
let lists = getState("lists"); | ||
if (lists.indexOf(list) == -1) { | ||
lists += list + "\n"; | ||
setState("lists", lists); | ||
} | ||
|
||
return; | ||
} | ||
|
||
var body = new BinaryInputStream(request.bodyInputStream); | ||
var avail; | ||
var bytes = []; | ||
|
||
while ((avail = body.available()) > 0) { | ||
Array.prototype.push.apply(bytes, body.readByteArray(avail)); | ||
} | ||
|
||
var responseBody = parseV2Request(bytes); | ||
|
||
response.setHeader("Content-Type", "text/plain", false); | ||
response.write(responseBody); | ||
|
||
} | ||
|
||
function parseV2Request(bytes) { | ||
var request = String.fromCharCode.apply(this, bytes); | ||
var [HEADER, PREFIXES] = request.split("\n"); | ||
var [PREFIXSIZE, LENGTH] = HEADER.split(":").map(val => { | ||
return parseInt(val); | ||
}); | ||
|
||
var ret = ""; | ||
for(var start = 0; start < LENGTH; start += PREFIXSIZE) { | ||
getState("lists").split("\n").forEach(function(list) { | ||
var completions = getState(list).split("\n"); | ||
|
||
for (var completion of completions) { | ||
if (completion.indexOf(PREFIXES.substr(start, PREFIXSIZE)) == 0) { | ||
ret += list + ":" + "1" + ":" + "32" + "\n"; | ||
ret += completion; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
/* Convert Base64 data to a string */ | ||
const toBinaryTable = [ | ||
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | ||
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | ||
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, | ||
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, | ||
-1, 0, 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,-1, -1,-1,-1,-1, | ||
-1,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,-1, -1,-1,-1,-1 | ||
]; | ||
const base64Pad = '='; | ||
|
||
function base64ToString(data) { | ||
var result = ''; | ||
var leftbits = 0; // number of bits decoded, but yet to be appended | ||
var leftdata = 0; // bits decoded, but yet to be appended | ||
|
||
// Convert one by one. | ||
for (var i = 0; i < data.length; i++) { | ||
var c = toBinaryTable[data.charCodeAt(i) & 0x7f]; | ||
var padding = (data[i] == base64Pad); | ||
// Skip illegal characters and whitespace | ||
if (c == -1) continue; | ||
|
||
// Collect data into leftdata, update bitcount | ||
leftdata = (leftdata << 6) | c; | ||
leftbits += 6; | ||
|
||
// If we have 8 or more bits, append 8 bits to the result | ||
if (leftbits >= 8) { | ||
leftbits -= 8; | ||
// Append if not padding. | ||
if (!padding) | ||
result += String.fromCharCode((leftdata >> leftbits) & 0xff); | ||
leftdata &= (1 << leftbits) - 1; | ||
} | ||
} | ||
|
||
// If there are any bits left, the base64 string was corrupted | ||
if (leftbits) | ||
throw Components.Exception('Corrupted base64 string'); | ||
|
||
return result; | ||
} |
62 changes: 62 additions & 0 deletions
62
toolkit/components/url-classifier/tests/mochitest/gethashFrame.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<html> | ||
<head> | ||
<title></title> | ||
|
||
<script type="text/javascript"> | ||
|
||
var scriptItem = "untouched"; | ||
|
||
function checkLoads() { | ||
|
||
var title = document.getElementById("title"); | ||
title.innerHTML = window.parent.shouldLoad ? | ||
"The following should be hidden:" : | ||
"The following should not be hidden:" | ||
|
||
if (window.parent.shouldLoad) { | ||
window.parent.is(scriptItem, "loaded malware javascript!", "Should load bad javascript"); | ||
} else { | ||
window.parent.is(scriptItem, "untouched", "Should not load bad javascript"); | ||
} | ||
|
||
var elt = document.getElementById("styleImport"); | ||
var style = document.defaultView.getComputedStyle(elt, ""); | ||
window.parent.isnot(style.visibility, "visible", "Should load clean css"); | ||
|
||
// Make sure the css did not load. | ||
elt = document.getElementById("styleCheck"); | ||
style = document.defaultView.getComputedStyle(elt, ""); | ||
if (window.parent.shouldLoad) { | ||
window.parent.isnot(style.visibility, "visible", "Should load bad css"); | ||
} else { | ||
window.parent.isnot(style.visibility, "hidden", "Should not load bad css"); | ||
} | ||
|
||
elt = document.getElementById("styleBad"); | ||
style = document.defaultView.getComputedStyle(elt, ""); | ||
if (window.parent.shouldLoad) { | ||
window.parent.isnot(style.visibility, "visible", "Should import bad css"); | ||
} else { | ||
window.parent.isnot(style.visibility, "hidden", "Should not import bad css"); | ||
} | ||
} | ||
|
||
</script> | ||
|
||
<!-- Try loading from a malware javascript URI --> | ||
<script type="text/javascript" src="http://malware.example.com/tests/toolkit/components/url-classifier/tests/mochitest/evil.js"></script> | ||
|
||
<!-- Try loading from an uwanted software css URI --> | ||
<link rel="stylesheet" type="text/css" href="http://unwanted.example.com/tests/toolkit/components/url-classifier/tests/mochitest/evil.css"></link> | ||
|
||
<!-- Try loading a marked-as-malware css through an @import from a clean URI --> | ||
<link rel="stylesheet" type="text/css" href="import.css"></link> | ||
</head> | ||
|
||
<body onload="checkLoads()"> | ||
<div id="title"></div> | ||
<div id="styleCheck">STYLE EVIL</div> | ||
<div id="styleBad">STYLE BAD</div> | ||
<div id="styleImport">STYLE IMPORT</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
/* malware.example.com is in the malware database. | ||
classifierBad.css does not actually exist. */ | ||
@import url("http://malware.example.com/tests/docshell/test/classifierBad.css"); | ||
/* malware.example.com is in the malware database. */ | ||
@import url("http://malware.example.com/tests/toolkit/components/url-classifier/tests/mochitest/bad.css"); | ||
#styleImport { visibility: hidden; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.