Bug 1274226 - Verify the completion request in test_hashcompleter.js. r=francois.
authorHenry Chang <hchang@mozilla.com>
Mon, 23 May 2016 04:03:00 +0200
changeset 338502 f03b60a446f193327a21e0e2ca9892538b44b2f9
parent 338501 fb00a4b41a9f73db66848423ac837c06e28f08f6
child 338503 ec1dd8da2504f315dfeb22b0958398e16331c499
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfrancois
bugs1274226
milestone49.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1274226 - Verify the completion request in test_hashcompleter.js. r=francois.
toolkit/components/url-classifier/tests/unit/test_hashcompleter.js
--- a/toolkit/components/url-classifier/tests/unit/test_hashcompleter.js
+++ b/toolkit/components/url-classifier/tests/unit/test_hashcompleter.js
@@ -111,16 +111,69 @@ var multipleResponsesCompletionSet = [
         hash: "123478",
         table: "test2",
         chunkId: 4,
       }
     ],
   }
 ];
 
+function buildCompletionRequest(aCompletionSet) {
+  let prefixes = [];
+  let prefixSet = new Set();
+  aCompletionSet.forEach(s => {
+    let prefix = s.hash.substring(0, 4);
+    if (prefixSet.has(prefix)) {
+      return;
+    }
+    prefixSet.add(prefix);
+    prefixes.push(prefix);
+  });
+  return 4 + ":" + (4 * prefixes.length) + "\n" + prefixes.join("");
+}
+
+function parseCompletionRequest(aRequest) {
+  // Format: [partial_length]:[num_of_prefix * partial_length]\n[prefixes_data]
+
+  let tokens = /(\d):(\d+)/.exec(aRequest);
+  if (tokens.length < 3) {
+    dump("Request format error.");
+    return null;
+  }
+
+  let partialLength = parseInt(tokens[1]);
+  let payloadLength = parseInt(tokens[2]);
+
+  let payloadStart = tokens[1].length + // partial length
+                     1 +                // ':'
+                     tokens[2].length + // payload length
+                     1;                 // '\n'
+
+  let prefixSet = [];
+  for (let i = payloadStart; i < aRequest.length; i += partialLength) {
+    let prefix = aRequest.substr(i, partialLength);
+    if (prefix.length !== partialLength) {
+      dump("Header info not correct: " + aRequest.substr(0, payloadStart));
+      return null;
+    }
+    prefixSet.push(prefix);
+  }
+  prefixSet.sort();
+
+  return prefixSet;
+}
+
+// Compare the requests in string format.
+function compareCompletionRequest(aRequest1, aRequest2) {
+  let prefixSet1 = parseCompletionRequest(aRequest1);
+  let prefixSet2 = parseCompletionRequest(aRequest2);
+
+  return equal(JSON.stringify(prefixSet1), JSON.stringify(prefixSet2));
+}
+
 // The fifth completion set is added at runtime by getRandomCompletionSet.
 // Each completion in the set only has one response and its purpose is to
 // provide an easy way to test the HashCompleter handling an arbitrarily large
 // completion set (determined by SIZE_OF_RANDOM_SET).
 const SIZE_OF_RANDOM_SET = 16;
 function getRandomCompletionSet(forceServerError) {
   let completionSet = [];
   let hashPrefixes = [];
@@ -251,16 +304,20 @@ function hashCompleterServer(aRequest, a
   let stream = aRequest.bodyInputStream;
   let wrapperStream = Cc["@mozilla.org/binaryinputstream;1"].
                         createInstance(Ci.nsIBinaryInputStream);
   wrapperStream.setInputStream(stream);
 
   let len = stream.available();
   let data = wrapperStream.readBytes(len);
 
+  // Check if we got the expected completion request.
+  let expectedRequest = buildCompletionRequest(completionSets[currentCompletionSet]);
+  compareCompletionRequest(data, expectedRequest);
+
   // To avoid a response with duplicate hash completions, we keep track of all
   // completed hash prefixes so far.
   let completedHashes = [];
   let responseText = "";
 
   function responseForCompletion(x) {
     return x.table + ":" + x.chunkId + ":" + x.hash.length + "\n" + x.hash;
   }