summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/unit/test_Sanitizer_interrupted.js
blob: c8e7130ac0c10ba4fe67e7c397d6d3f6993ce763 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

do_get_profile();

// Test that interrupted sanitizations are properly tracked.

add_task(async function () {
  const { Sanitizer } = ChromeUtils.importESModule(
    "resource:///modules/Sanitizer.sys.mjs"
  );

  Services.prefs.setBoolPref(Sanitizer.PREF_NEWTAB_SEGREGATION, false);

  registerCleanupFunction(() => {
    Services.prefs.clearUserPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN);
    Services.prefs.clearUserPref(Sanitizer.PREF_SHUTDOWN_BRANCH + "formdata");
    Services.prefs.clearUserPref(Sanitizer.PREF_NEWTAB_SEGREGATION);
  });
  Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, true);
  Services.prefs.setBoolPref(Sanitizer.PREF_SHUTDOWN_BRANCH + "formdata", true);

  await Sanitizer.onStartup();
  Assert.ok(Sanitizer.shouldSanitizeOnShutdown, "Should sanitize on shutdown");

  let pendingSanitizations = JSON.parse(
    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
  );
  Assert.equal(
    pendingSanitizations.length,
    1,
    "Should have 1 pending sanitization"
  );
  Assert.equal(
    pendingSanitizations[0].id,
    "shutdown",
    "Should be the shutdown sanitization"
  );
  Assert.ok(
    pendingSanitizations[0].itemsToClear.includes("formdata"),
    "Pref has been setup"
  );
  Assert.ok(
    !pendingSanitizations[0].options.isShutdown,
    "Shutdown option is not present"
  );

  // Check the preference listeners.
  Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, false);
  pendingSanitizations = JSON.parse(
    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
  );
  Assert.equal(
    pendingSanitizations.length,
    0,
    "Should not have pending sanitizations"
  );
  Assert.ok(
    !Sanitizer.shouldSanitizeOnShutdown,
    "Should not sanitize on shutdown"
  );
  Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, true);
  pendingSanitizations = JSON.parse(
    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
  );
  Assert.equal(
    pendingSanitizations.length,
    1,
    "Should have 1 pending sanitization"
  );
  Assert.equal(
    pendingSanitizations[0].id,
    "shutdown",
    "Should be the shutdown sanitization"
  );

  Assert.ok(
    pendingSanitizations[0].itemsToClear.includes("formdata"),
    "Pending sanitizations should include formdata"
  );
  Services.prefs.setBoolPref(
    Sanitizer.PREF_SHUTDOWN_BRANCH + "formdata",
    false
  );
  pendingSanitizations = JSON.parse(
    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
  );
  Assert.equal(
    pendingSanitizations.length,
    1,
    "Should have 1 pending sanitization"
  );
  Assert.ok(
    !pendingSanitizations[0].itemsToClear.includes("formdata"),
    "Pending sanitizations should have been updated"
  );

  // Check a sanitization properly rebuilds the pref.
  await Sanitizer.sanitize(["formdata"]);
  pendingSanitizations = JSON.parse(
    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
  );
  Assert.equal(
    pendingSanitizations.length,
    1,
    "Should have 1 pending sanitization"
  );
  Assert.equal(
    pendingSanitizations[0].id,
    "shutdown",
    "Should be the shutdown sanitization"
  );

  // Startup should run the pending one and setup a new shutdown sanitization.
  Services.prefs.setBoolPref(
    Sanitizer.PREF_SHUTDOWN_BRANCH + "formdata",
    false
  );
  await Sanitizer.onStartup();
  pendingSanitizations = JSON.parse(
    Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]")
  );
  Assert.equal(
    pendingSanitizations.length,
    1,
    "Should have 1 pending sanitization"
  );
  Assert.equal(
    pendingSanitizations[0].id,
    "shutdown",
    "Should be the shutdown sanitization"
  );
  Assert.ok(
    !pendingSanitizations[0].itemsToClear.includes("formdata"),
    "Pref has been setup"
  );
});