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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1975198 - Test lookup and completion telemetry</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/ChromeTask.js"></script>
<script src="/tests/SimpleTest/GleanTest.js"></script>
<script type="text/javascript" src="classifierHelper.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<iframe id="testFrame" onload=""></iframe>
<script src="head.js"></script>
<script class="testbody" type="text/javascript">
const MALWARE_LIST = "test-malware-simple";
const MALWARE_HOST = "malware.example.com/";
const UNWANTED_LIST = "test-unwanted-simple";
const UNWANTED_HOST = "unwanted.example.com/";
const GETHASH_URL = "http://mochi.test:8888/tests/toolkit/components/url-classifier/tests/mochitest/gethash.sjs";
async function gleanResetTestValues() {
return SpecialPowers.spawnChrome([], async () => {
await Services.fog.testFlushAllChildren();
Services.fog.testResetFOG();
});
};
function loadTestFrame(id) {
return new Promise(function(resolve) {
var iframe = document.getElementById(id);
iframe.setAttribute("src", "telemetryFrame.html");
iframe.onload = function() {
resolve();
};
});
}
function addPrefixToDB(list, url) {
var testData = [{ db: list, url, len: 4 }];
return classifierHelper.addUrlToDB(testData)
.catch(function(err) {
ok(false, "Couldn't update classifier. Error code: " + err);
// Abort test.
SimpleTest.finish();
});
}
add_setup(async function setup() {
await SpecialPowers.pushPrefEnv({"set": [
["browser.safebrowsing.malware.enabled", true],
["urlclassifier.malwareTable", "test-malware-simple,test-unwanted-simple"],
["network.predictor.enabled", false],
["urlclassifier.gethash.timeout_ms", 30000],
]});
await classifierHelper.waitForInit();
// Reset the state on the completion server.
await resetStateOnCompletionServer(GETHASH_URL, MALWARE_LIST);
await resetStateOnCompletionServer(GETHASH_URL, MALWARE_LIST);
await resetStateOnCompletionServer(GETHASH_URL, "lists");
await resetStateOnCompletionServer(GETHASH_URL, "counter");
await classifierHelper.allowCompletion([
MALWARE_LIST, UNWANTED_LIST], GETHASH_URL);
});
add_task(async function test_telemetry() {
info(" Cleaning up the glean test values");
await gleanResetTestValues();
info(" Adding prefixes to the database to allow lookup");
await addPrefixToDB(MALWARE_LIST, MALWARE_HOST);
await addPrefixToDB(UNWANTED_LIST, UNWANTED_HOST);
info(" Adding completion for malware to the server");
await addCompletionToServer(MALWARE_LIST, MALWARE_HOST, GETHASH_URL);
info(" Loading the test frame");
await loadTestFrame("testFrame");
info(" Getting the glean test values");
let lookupHitMalware = await GleanTest.urlclassifier.lookupHit[MALWARE_LIST].testGetValue();
let lookupHitUnwanted = await GleanTest.urlclassifier.lookupHit[UNWANTED_LIST].testGetValue();
let lookupMissMalware = await GleanTest.urlclassifier.lookupMiss[MALWARE_LIST].testGetValue();
let lookupMissUnwanted = await GleanTest.urlclassifier.lookupMiss[UNWANTED_LIST].testGetValue();
// The test frame loads one malware URL and one unwanted URL. Verifying
// the lookup hits. The conditions use >= to tolerate different speculative
// load timings.
ok(lookupHitMalware >= 1, "Lookup hit should be at least 1 for malware");
ok(lookupHitUnwanted >= 1, "Lookup hit should be at least 1 for unwanted");
// Loading the test frame also makes requests to URLs that are not in the
// database. Verifying the lookup misses are not null.
ok(lookupMissMalware > 0, "Lookup miss should be greater than 0 for malware");
ok(lookupMissUnwanted > 0, "Lookup miss should be greater than 0 for unwanted");
// Verifying the completion. There should be one confirmed for malware and one
// not confirmed for malware.
let completion_events = await GleanTest.urlclassifier.completion.testGetValue();
ok(completion_events.length >= 2, "There should be at least 2 completion events");
for (let event of completion_events) {
if (event.extra.table_name == MALWARE_LIST) {
is(event.extra.hit, "true", "The completion event for malware should be a hit");
} else if (event.extra.table_name == UNWANTED_LIST) {
is(event.extra.hit, "false", "The completion event for unwanted should be a miss");
} else {
ok(false, "Got unexpected table name in the completion event: " + event.extra.table_name);
}
}
});
</script>
</pre>
</body>
</html>
|