tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_large_pref.js (3079B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // Large preferences should not be set in the child process.
      6 // Non-string preferences are not tested here, because their behavior
      7 // should not be affected by this filtering.
      8 //
      9 
     10 function isParentProcess() {
     11  return Services.appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
     12 }
     13 
     14 function makeBuffer(length) {
     15  let string = "x";
     16  while (string.length < length) {
     17    string = string + string;
     18  }
     19  if (string.length > length) {
     20    string = string.substring(length - string.length);
     21  }
     22  return string;
     23 }
     24 
     25 // from prefapi.h
     26 const MAX_ADVISABLE_PREF_LENGTH = 4 * 1024;
     27 
     28 const largeString = makeBuffer(MAX_ADVISABLE_PREF_LENGTH + 1);
     29 const smallString = makeBuffer(4);
     30 
     31 const testValues = [
     32  { name: "None", value: undefined },
     33  { name: "Small", value: smallString },
     34  { name: "Large", value: largeString },
     35 ];
     36 
     37 function prefName(def, user) {
     38  return "Test.IPC.default" + def.name + "User" + user.name;
     39 }
     40 
     41 function expectedPrefValue(def, user) {
     42  if (user.value) {
     43    return user.value;
     44  }
     45  return def.value;
     46 }
     47 
     48 function run_test() {
     49  const pb = Services.prefs;
     50  let defaultBranch = pb.getDefaultBranch("");
     51 
     52  let isParent = isParentProcess();
     53  if (isParent) {
     54    // Preferences with large values will still appear in the shared memory
     55    // snapshot that we share with all processes. They should not, however, be
     56    // sent with the list of changes on top of the snapshot.
     57    //
     58    // So, make sure we've generated the initial snapshot before we set the
     59    // preference values by launching a child process with an empty test.
     60    sendCommand("");
     61 
     62    // Set all combinations of none, small and large, for default and user prefs.
     63    for (let def of testValues) {
     64      for (let user of testValues) {
     65        let currPref = prefName(def, user);
     66        if (def.value) {
     67          defaultBranch.setCharPref(currPref, def.value);
     68        }
     69        if (user.value) {
     70          pb.setCharPref(currPref, user.value);
     71        }
     72      }
     73    }
     74 
     75    run_test_in_child("test_large_pref.js");
     76  }
     77 
     78  // Check that each preference is set or not set, as appropriate.
     79  for (let def of testValues) {
     80    for (let user of testValues) {
     81      if (!def.value && !user.value) {
     82        continue;
     83      }
     84      let pref_name = prefName(def, user);
     85      if (isParent || (def.name != "Large" && user.name != "Large")) {
     86        Assert.equal(pb.getCharPref(pref_name), expectedPrefValue(def, user));
     87      } else {
     88        // This is the child, and either the default or user value is
     89        // large, so the preference should not be set.
     90        let prefExists;
     91        try {
     92          let val = pb.getCharPref(pref_name);
     93          prefExists = val.length > 128;
     94        } catch (e) {
     95          prefExists = false;
     96        }
     97        ok(
     98          !prefExists,
     99          "Pref " + pref_name + " should not be set in the child"
    100        );
    101      }
    102    }
    103  }
    104 }