summaryrefslogtreecommitdiffstats
path: root/toolkit/components/resistfingerprinting/tests/test_spoof_english.html
blob: 1ebc6277e916404460dc8789f82434bba85fad43 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1486258
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1486258</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1486258">Mozilla Bug 1486258</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>

<script type="text/javascript">
/* global SpecialPowers */

const { BrowserTestUtils } = ChromeUtils.import("resource://testing-common/BrowserTestUtils.jsm");
;

const gOrigAvailableLocales = Services.locale.availableLocales;
const gOrigRequestedLocales = Services.locale.requestedLocales;

const kDoNotSpoof = 1;
const kSpoofEnglish = 2;

async function runTest(locale) {
  return BrowserTestUtils.withNewTab("http://example.com/", browser => {
    return SpecialPowers.spawn(browser, [locale], _locale => {
      return {
        locale: new Intl.PluralRules(_locale).resolvedOptions().locale,
        weekday: new Date(0).toLocaleString(_locale, {weekday: "long"}),
        currency: Number(1000).toLocaleString(_locale, {currency: "USD"}),
      };
    });
  });
}

async function runSpoofTest(spoof) {
  ok(spoof == kDoNotSpoof ||
     spoof == kSpoofEnglish, "check supported parameter");

  await SpecialPowers.pushPrefEnv({
    "set": [
      ["privacy.spoof_english", spoof],
    ],
  });

  is(SpecialPowers.getBoolPref("javascript.use_us_english_locale", false),
     spoof == kSpoofEnglish,
     "use_us_english_locale");

  let results = await runTest(undefined);

  await SpecialPowers.popPrefEnv();

  return results;
}

async function setupLocale(locale) {
  Services.locale.availableLocales = [locale];
  Services.locale.requestedLocales = [locale];
}

async function clearLocale() {
  Services.locale.availableLocales = gOrigAvailableLocales;
  Services.locale.requestedLocales = gOrigRequestedLocales;
}

(async function() {
  SimpleTest.waitForExplicitFinish();

  // 1. preliminary for spoof test
  await SpecialPowers.pushPrefEnv({
    "set": [
      ["privacy.resistFingerprinting", true],
    ],
  });

  // 2. log English/German results
  let enResults = await runTest("en-US");
  is(enResults.locale, "en-US", "default locale");

  let deResults = await runTest("de");
  is(deResults.locale, "de", "default locale");

  // 3. set system locale to German
  await setupLocale("de");

  // 4. log non-spoofed results
  let nonSpoofedResults = await runSpoofTest(kDoNotSpoof);

  // 5. log spoofed results
  let spoofedResults = await runSpoofTest(kSpoofEnglish);

  // 6. compare spoofed/non-spoofed results
  for (let key in enResults) {
    isnot(enResults[key], deResults[key], "compare en/de result: " + key);
    is(nonSpoofedResults[key], deResults[key], "compare non-spoofed result: " + key);
    is(spoofedResults[key], enResults[key], "compare spoofed result: " + key);
  }

  // 7. restore default locale
  await clearLocale();

  SimpleTest.finish();
})();

</script>

</body>
</html>