summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_urlQueryStringStripping_nimbus.js
blob: e5f256b870f82d751f9489dc4385e32515b9ea7a (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* vim: set ts=2 et sw=2 tw=80: */
/* 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/. */

"use strict";

/**
 * Simplified version of browser_urlQueryStripping.js to test that the Nimbus
 * integration works correctly in both normal and private browsing.
 */

const { ExperimentFakes } = ChromeUtils.importESModule(
  "resource://testing-common/NimbusTestUtils.sys.mjs"
);

const TEST_URI = TEST_DOMAIN + TEST_PATH + "file_stripping.html";
const TEST_QUERY_STRING = "paramToStrip1=123&paramToKeep=456";
const TEST_QUERY_STRING_STRIPPED = "paramToKeep=456";
const TEST_URI_WITH_QUERY = TEST_URI + "?" + TEST_QUERY_STRING;

let listService;

async function waitForListServiceInit(strippingEnabled) {
  info("Waiting for nsIURLQueryStrippingListService to be initialized.");
  let isInitialized = await listService.testWaitForInit();
  is(
    isInitialized,
    strippingEnabled,
    "nsIURLQueryStrippingListService should be initialized when the feature is enabled."
  );
}

/**
 * Set a list of prefs on the default branch and restore the original values on test end.
 * @param {*} prefs - Key value pairs in an array.
 */
function setDefaultPrefs(prefs) {
  let originalValues = new Map();
  let defaultPrefs = Services.prefs.getDefaultBranch("");

  let prefValueToSetter = prefValue => {
    let type = typeof prefValue;
    if (type == "string") {
      return defaultPrefs.setStringPref;
    }
    if (type == "boolean") {
      return defaultPrefs.setBoolPref;
    }
    throw new Error("unexpected pref type");
  };

  prefs.forEach(([key, value]) => {
    prefValueToSetter(value)(key, value);
    originalValues.set(key, value);
  });

  registerCleanupFunction(function () {
    prefs.forEach(([key, value]) => {
      prefValueToSetter(value)(key, originalValues.get(key));
    });
  });
}

add_setup(async function () {
  // Disable the feature via the default pref. This is required so we can set
  // user values via Nimbus.
  setDefaultPrefs([
    ["privacy.query_stripping.enabled", false],
    ["privacy.query_stripping.enabled.pbmode", false],
    ["privacy.query_stripping.strip_list", ""],
    ["privacy.query_stripping.strip_on_share.enabled", false],
  ]);

  await SpecialPowers.pushPrefEnv({
    set: [["privacy.query_stripping.listService.logLevel", "Debug"]],
  });

  // Get the list service so we can wait for it to be fully initialized before running tests.
  listService = Cc["@mozilla.org/query-stripping-list-service;1"].getService(
    Ci.nsIURLQueryStrippingListService
  );
  // Here we don't care about the actual enabled state, we just want any init to be done so we get reliable starting conditions.
  await listService.testWaitForInit();
});

add_task(async function test() {
  let [normalWindow, pbWindow] = await Promise.all([
    BrowserTestUtils.openNewBrowserWindow(),
    BrowserTestUtils.openNewBrowserWindow({ private: true }),
  ]);

  for (let enableStripPBM of [false, true]) {
    for (let enableStrip of [false, true]) {
      let doExperimentCleanup = await ExperimentFakes.enrollWithFeatureConfig({
        featureId: "queryStripping",
        value: {
          enabledNormalBrowsing: enableStrip,
          enabledPrivateBrowsing: enableStripPBM,
          stripList: "paramToStrip1 paramToStrip2",
        },
      });

      for (let testPBM of [false, true]) {
        let shouldStrip =
          (testPBM && enableStripPBM) || (!testPBM && enableStrip);
        let expectedQueryString = shouldStrip
          ? TEST_QUERY_STRING_STRIPPED
          : TEST_QUERY_STRING;

        info(
          "Test stripping " +
            JSON.stringify({
              enableStripPBM,
              enableStrip,
              testPBM,
              expectedQueryString,
            })
        );

        await waitForListServiceInit(enableStripPBM || enableStrip);

        let tabBrowser = testPBM ? pbWindow.gBrowser : normalWindow.gBrowser;
        await BrowserTestUtils.withNewTab(
          { gBrowser: tabBrowser, url: TEST_URI_WITH_QUERY },
          async browser => {
            is(
              browser.currentURI.query,
              expectedQueryString,
              "Correct query string"
            );
          }
        );
      }

      await doExperimentCleanup();
    }
  }

  // Cleanup
  await Promise.all([
    BrowserTestUtils.closeWindow(normalWindow),
    BrowserTestUtils.closeWindow(pbWindow),
  ]);
});