test_ContextId_LegacyBackend.js (5136B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { _ContextId } = ChromeUtils.importESModule( 7 "moz-src:///browser/modules/ContextId.sys.mjs" 8 ); 9 10 const CONTEXT_ID_PREF = "browser.contextual-services.contextId"; 11 const CONTEXT_ID_TIMESTAMP_PREF = 12 "browser.contextual-services.contextId.timestamp-in-seconds"; 13 const CONTEXT_ID_ROTATION_DAYS_PREF = 14 "browser.contextual-services.contextId.rotation-in-days"; 15 const UUID_WITH_BRACES_REGEX = 16 /^\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}$/i; 17 const TEST_CONTEXT_ID_WITH_BRACES = "{decafbad-0cd1-0cd2-0cd3-decafbad1000}"; 18 19 do_get_profile(); 20 21 /** 22 * Test that if there's a pre-existing contextID, we can get it, and that a 23 * timestamp will be generated for it. 24 */ 25 add_task(async function test_get_existing() { 26 Services.prefs.setCharPref(CONTEXT_ID_PREF, TEST_CONTEXT_ID_WITH_BRACES); 27 Services.prefs.clearUserPref(CONTEXT_ID_TIMESTAMP_PREF); 28 Services.prefs.setIntPref(CONTEXT_ID_ROTATION_DAYS_PREF, 0); 29 30 let ContextId = new _ContextId(); 31 32 Assert.equal( 33 await ContextId.request(), 34 TEST_CONTEXT_ID_WITH_BRACES, 35 "Should have gotten the stored context ID" 36 ); 37 38 Assert.ok( 39 !Services.prefs.prefHasUserValue(CONTEXT_ID_TIMESTAMP_PREF), 40 "No timestamp was persisted." 41 ); 42 Assert.equal( 43 Services.prefs.getCharPref(CONTEXT_ID_PREF), 44 TEST_CONTEXT_ID_WITH_BRACES, 45 "The same context ID is still stored after requesting." 46 ); 47 48 Assert.equal( 49 await ContextId.request(), 50 TEST_CONTEXT_ID_WITH_BRACES, 51 "Should have gotten the same stored context ID back again." 52 ); 53 54 // We should be able to synchronously request the context ID in this 55 // configuration. 56 Assert.equal( 57 ContextId.requestSynchronously(), 58 TEST_CONTEXT_ID_WITH_BRACES, 59 "Got the stored context ID back synchronously." 60 ); 61 }); 62 63 /** 64 * Test that if there's not a pre-existing contextID, we will generate one, but 65 * no timestamp will be generated for it. 66 */ 67 add_task(async function test_generate() { 68 Services.prefs.clearUserPref(CONTEXT_ID_PREF); 69 Services.prefs.clearUserPref(CONTEXT_ID_TIMESTAMP_PREF); 70 71 let ContextId = new _ContextId(); 72 73 const generatedContextID = await ContextId.request(); 74 75 Assert.ok( 76 UUID_WITH_BRACES_REGEX.test(generatedContextID), 77 "Should have gotten a UUID generated for the context ID." 78 ); 79 80 Assert.ok( 81 !Services.prefs.prefHasUserValue(CONTEXT_ID_TIMESTAMP_PREF), 82 "No timestamp was persisted." 83 ); 84 85 Assert.equal( 86 await ContextId.request(), 87 generatedContextID, 88 "Should have gotten the same stored context ID back again." 89 ); 90 91 // We should be able to synchronously request the context ID in this 92 // configuration. 93 Assert.equal( 94 ContextId.requestSynchronously(), 95 generatedContextID, 96 "Got the stored context ID back synchronously." 97 ); 98 }); 99 100 /** 101 * Test that if we have a pre-existing context ID, and we (for some reason) 102 * have rotation period set to a non-zero value, and a creation timestamp 103 * exists, that the context ID does not rotate between requests. 104 */ 105 add_task(async function test_no_rotation() { 106 Services.prefs.setCharPref(CONTEXT_ID_PREF, TEST_CONTEXT_ID_WITH_BRACES); 107 Services.prefs.setIntPref(CONTEXT_ID_TIMESTAMP_PREF, 1); 108 Services.prefs.setIntPref(CONTEXT_ID_ROTATION_DAYS_PREF, 1); 109 // Let's say there's a 30 day rotation window. 110 const ROTATION_DAYS = 30; 111 Services.prefs.setIntPref(CONTEXT_ID_ROTATION_DAYS_PREF, ROTATION_DAYS); 112 113 let ContextId = new _ContextId(); 114 115 Assert.ok( 116 !ContextId.rotationEnabled, 117 "ContextId should report that rotation is not enabled." 118 ); 119 120 Assert.equal( 121 await ContextId.request(), 122 TEST_CONTEXT_ID_WITH_BRACES, 123 "Should have gotten the stored context ID" 124 ); 125 Assert.equal( 126 Services.prefs.getIntPref(CONTEXT_ID_TIMESTAMP_PREF), 127 1, 128 "The timestamp should not have changed." 129 ); 130 131 // We should be able to synchronously request the context ID in this 132 // configuration. 133 Assert.equal( 134 ContextId.requestSynchronously(), 135 TEST_CONTEXT_ID_WITH_BRACES, 136 "Got the stored context ID back synchronously." 137 ); 138 }); 139 140 /** 141 * Test that calling forceRotation is a no-op with the legacy backend. 142 */ 143 add_task(async function test_force_rotation() { 144 Services.prefs.setCharPref(CONTEXT_ID_PREF, TEST_CONTEXT_ID_WITH_BRACES); 145 Services.prefs.clearUserPref(CONTEXT_ID_TIMESTAMP_PREF); 146 147 let ContextId = new _ContextId(); 148 Assert.equal( 149 await ContextId.request(), 150 TEST_CONTEXT_ID_WITH_BRACES, 151 "Should have gotten the stored context ID" 152 ); 153 154 await ContextId.forceRotation(); 155 156 Assert.equal( 157 await ContextId.request(), 158 TEST_CONTEXT_ID_WITH_BRACES, 159 "Should have gotten the stored context ID" 160 ); 161 Assert.ok( 162 !Services.prefs.prefHasUserValue(CONTEXT_ID_TIMESTAMP_PREF), 163 "The timestamp should not have changed." 164 ); 165 166 // We should be able to synchronously request the context ID in this 167 // configuration. 168 Assert.equal( 169 ContextId.requestSynchronously(), 170 TEST_CONTEXT_ID_WITH_BRACES, 171 "Got the stored context ID back synchronously." 172 ); 173 });