summaryrefslogtreecommitdiffstats
path: root/modules/libpref/test/unit_ipc/test_large_pref.js
blob: 79c1330c366d9e2609c862c441c5ebf5be15fb0c (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Large preferences should not be set in the child process.
// Non-string preferences are not tested here, because their behavior
// should not be affected by this filtering.
//

function isParentProcess() {
  return Services.appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
}

function makeBuffer(length) {
  let string = "x";
  while (string.length < length) {
    string = string + string;
  }
  if (string.length > length) {
    string = string.substring(length - string.length);
  }
  return string;
}

// from prefapi.h
const MAX_ADVISABLE_PREF_LENGTH = 4 * 1024;

const largeString = makeBuffer(MAX_ADVISABLE_PREF_LENGTH + 1);
const smallString = makeBuffer(4);

const testValues = [
  { name: "None", value: undefined },
  { name: "Small", value: smallString },
  { name: "Large", value: largeString },
];

function prefName(def, user) {
  return "Test.IPC.default" + def.name + "User" + user.name;
}

function expectedPrefValue(def, user) {
  if (user.value) {
    return user.value;
  }
  return def.value;
}

function run_test() {
  const pb = Services.prefs;
  let defaultBranch = pb.getDefaultBranch("");

  let isParent = isParentProcess();
  if (isParent) {
    // Preferences with large values will still appear in the shared memory
    // snapshot that we share with all processes. They should not, however, be
    // sent with the list of changes on top of the snapshot.
    //
    // So, make sure we've generated the initial snapshot before we set the
    // preference values by launching a child process with an empty test.
    sendCommand("");

    // Set all combinations of none, small and large, for default and user prefs.
    for (let def of testValues) {
      for (let user of testValues) {
        let currPref = prefName(def, user);
        if (def.value) {
          defaultBranch.setCharPref(currPref, def.value);
        }
        if (user.value) {
          pb.setCharPref(currPref, user.value);
        }
      }
    }

    run_test_in_child("test_large_pref.js");
  }

  // Check that each preference is set or not set, as appropriate.
  for (let def of testValues) {
    for (let user of testValues) {
      if (!def.value && !user.value) {
        continue;
      }
      let pref_name = prefName(def, user);
      if (isParent || (def.name != "Large" && user.name != "Large")) {
        Assert.equal(pb.getCharPref(pref_name), expectedPrefValue(def, user));
      } else {
        // This is the child, and either the default or user value is
        // large, so the preference should not be set.
        let prefExists;
        try {
          let val = pb.getCharPref(pref_name);
          prefExists = val.length > 128;
        } catch (e) {
          prefExists = false;
        }
        ok(
          !prefExists,
          "Pref " + pref_name + " should not be set in the child"
        );
      }
    }
  }
}