test_IPPStartupCache.js (4238B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { IPPStartupCacheSingleton } = ChromeUtils.importESModule( 8 "moz-src:///browser/components/ipprotection/IPPStartupCache.sys.mjs" 9 ); 10 11 /** 12 * Test the disabled cache 13 */ 14 add_task(async function test_IPPStartupCache_disabled() { 15 // By default the cache is not active. 16 Services.prefs.setBoolPref("browser.ipProtection.cacheDisabled", true); 17 const cache = new IPPStartupCacheSingleton(); 18 cache.init(); 19 20 Assert.ok( 21 cache.isStartupCompleted, 22 "In XPCShell mode the cache is not active" 23 ); 24 }); 25 26 /** 27 * Test the enabled cache 28 */ 29 add_task(async function test_IPPStartupCache_enabled() { 30 // By default the cache is not active. 31 Services.prefs.setBoolPref("browser.ipProtection.cacheDisabled", false); 32 33 // Default state is UNINITIALIZED 34 { 35 const cache = new IPPStartupCacheSingleton(); 36 cache.init(); 37 38 Assert.ok( 39 !cache.isStartupCompleted, 40 "In XPCShell mode the cache is active" 41 ); 42 Assert.equal( 43 cache.state, 44 IPProtectionStates.UNINITIALIZED, 45 "The state is unitialized" 46 ); 47 } 48 49 // Fetch the cached state 50 { 51 Services.prefs.setCharPref( 52 "browser.ipProtection.stateCache", 53 IPProtectionStates.READY 54 ); 55 56 const cache = new IPPStartupCacheSingleton(); 57 cache.init(); 58 59 Assert.ok( 60 !cache.isStartupCompleted, 61 "In XPCShell mode the cache is active" 62 ); 63 Assert.equal(cache.state, IPProtectionStates.READY, "The state is READY"); 64 } 65 66 // Invalid cache means UNINITIALIZED 67 { 68 Services.prefs.setCharPref( 69 "browser.ipProtection.stateCache", 70 "Hello World!" 71 ); 72 73 const cache = new IPPStartupCacheSingleton(); 74 cache.init(); 75 76 Assert.ok( 77 !cache.isStartupCompleted, 78 "In XPCShell mode the cache is active" 79 ); 80 Assert.equal( 81 cache.state, 82 IPProtectionStates.UNINITIALIZED, 83 "The state is unitialized" 84 ); 85 } 86 }); 87 88 /** 89 * Cache the entitlement 90 */ 91 add_task(async function test_IPPStartupCache_enabled() { 92 // By default the cache is not active. 93 Services.prefs.setBoolPref("browser.ipProtection.cacheDisabled", false); 94 95 // Default entitlement is null 96 { 97 const cache = new IPPStartupCacheSingleton(); 98 cache.init(); 99 100 Assert.ok( 101 !cache.isStartupCompleted, 102 "In XPCShell mode the cache is active" 103 ); 104 Assert.equal(cache.entitlement, null, "Null entitlement"); 105 } 106 107 // A JSON object for entitlement 108 { 109 Services.prefs.setCharPref( 110 "browser.ipProtection.entitlementCache", 111 '{"a": 42}' 112 ); 113 114 const cache = new IPPStartupCacheSingleton(); 115 cache.init(); 116 117 Assert.ok( 118 !cache.isStartupCompleted, 119 "In XPCShell mode the cache is active" 120 ); 121 Assert.deepEqual( 122 cache.entitlement, 123 { a: 42 }, 124 "A valid entitlement object" 125 ); 126 } 127 128 // Invalid JSON 129 { 130 Services.prefs.setCharPref( 131 "browser.ipProtection.entitlementCache", 132 '{"a": 42}}}}' 133 ); 134 135 const cache = new IPPStartupCacheSingleton(); 136 cache.init(); 137 138 Assert.ok( 139 !cache.isStartupCompleted, 140 "In XPCShell mode the cache is active" 141 ); 142 Assert.equal(cache.entitlement, null, "Null entitlement"); 143 } 144 145 // Setter 146 { 147 const cache = new IPPStartupCacheSingleton(); 148 cache.init(); 149 150 Assert.ok( 151 !cache.isStartupCompleted, 152 "In XPCShell mode the cache is active" 153 ); 154 Assert.equal(cache.entitlement, null, "Null entitlement"); 155 156 cache.storeEntitlement(42); 157 Assert.equal( 158 Services.prefs.getCharPref("browser.ipProtection.entitlementCache", ""), 159 "42", 160 "The cache is correctly stored (number)" 161 ); 162 163 cache.storeEntitlement(null); 164 Assert.equal( 165 Services.prefs.getCharPref("browser.ipProtection.entitlementCache", ""), 166 "null", 167 "The cache is correctly stored (null)" 168 ); 169 170 cache.storeEntitlement({ a: 42 }); 171 Assert.equal( 172 Services.prefs.getCharPref("browser.ipProtection.entitlementCache", ""), 173 '{"a":42}', 174 "The cache is correctly stored (obj)" 175 ); 176 } 177 });