test_Sanitizer_interrupted.js (3982B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 do_get_profile(); 7 8 // Test that interrupted sanitizations are properly tracked. 9 10 add_task(async function () { 11 const { Sanitizer } = ChromeUtils.importESModule( 12 "resource:///modules/Sanitizer.sys.mjs" 13 ); 14 15 Services.prefs.setBoolPref(Sanitizer.PREF_NEWTAB_SEGREGATION, false); 16 17 registerCleanupFunction(() => { 18 Services.prefs.clearUserPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN); 19 Services.prefs.clearUserPref(Sanitizer.PREF_SHUTDOWN_BRANCH + "formdata"); 20 Services.prefs.clearUserPref(Sanitizer.PREF_NEWTAB_SEGREGATION); 21 }); 22 Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, true); 23 Services.prefs.setBoolPref(Sanitizer.PREF_SHUTDOWN_BRANCH + "formdata", true); 24 25 await Sanitizer.onStartup(); 26 Assert.ok(Sanitizer.shouldSanitizeOnShutdown, "Should sanitize on shutdown"); 27 28 let pendingSanitizations = JSON.parse( 29 Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]") 30 ); 31 Assert.equal( 32 pendingSanitizations.length, 33 1, 34 "Should have 1 pending sanitization" 35 ); 36 Assert.equal( 37 pendingSanitizations[0].id, 38 "shutdown", 39 "Should be the shutdown sanitization" 40 ); 41 Assert.ok( 42 pendingSanitizations[0].itemsToClear.includes("formdata"), 43 "Pref has been setup" 44 ); 45 Assert.ok( 46 !pendingSanitizations[0].options.isShutdown, 47 "Shutdown option is not present" 48 ); 49 50 // Check the preference listeners. 51 Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, false); 52 pendingSanitizations = JSON.parse( 53 Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]") 54 ); 55 Assert.equal( 56 pendingSanitizations.length, 57 0, 58 "Should not have pending sanitizations" 59 ); 60 Assert.ok( 61 !Sanitizer.shouldSanitizeOnShutdown, 62 "Should not sanitize on shutdown" 63 ); 64 Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, true); 65 pendingSanitizations = JSON.parse( 66 Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]") 67 ); 68 Assert.equal( 69 pendingSanitizations.length, 70 1, 71 "Should have 1 pending sanitization" 72 ); 73 Assert.equal( 74 pendingSanitizations[0].id, 75 "shutdown", 76 "Should be the shutdown sanitization" 77 ); 78 79 Assert.ok( 80 pendingSanitizations[0].itemsToClear.includes("formdata"), 81 "Pending sanitizations should include formdata" 82 ); 83 Services.prefs.setBoolPref( 84 Sanitizer.PREF_SHUTDOWN_BRANCH + "formdata", 85 false 86 ); 87 pendingSanitizations = JSON.parse( 88 Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]") 89 ); 90 Assert.equal( 91 pendingSanitizations.length, 92 1, 93 "Should have 1 pending sanitization" 94 ); 95 Assert.ok( 96 !pendingSanitizations[0].itemsToClear.includes("formdata"), 97 "Pending sanitizations should have been updated" 98 ); 99 100 // Check a sanitization properly rebuilds the pref. 101 await Sanitizer.sanitize(["formdata"]); 102 pendingSanitizations = JSON.parse( 103 Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]") 104 ); 105 Assert.equal( 106 pendingSanitizations.length, 107 1, 108 "Should have 1 pending sanitization" 109 ); 110 Assert.equal( 111 pendingSanitizations[0].id, 112 "shutdown", 113 "Should be the shutdown sanitization" 114 ); 115 116 // Startup should run the pending one and setup a new shutdown sanitization. 117 Services.prefs.setBoolPref( 118 Sanitizer.PREF_SHUTDOWN_BRANCH + "formdata", 119 false 120 ); 121 await Sanitizer.onStartup(); 122 pendingSanitizations = JSON.parse( 123 Services.prefs.getStringPref(Sanitizer.PREF_PENDING_SANITIZATIONS, "[]") 124 ); 125 Assert.equal( 126 pendingSanitizations.length, 127 1, 128 "Should have 1 pending sanitization" 129 ); 130 Assert.equal( 131 pendingSanitizations[0].id, 132 "shutdown", 133 "Should be the shutdown sanitization" 134 ); 135 Assert.ok( 136 !pendingSanitizations[0].itemsToClear.includes("formdata"), 137 "Pref has been setup" 138 ); 139 });