test_clear_forgetAboutSite.js (3967B)
1 "use strict"; 2 3 const { ForgetAboutSite } = ChromeUtils.importESModule( 4 "resource://gre/modules/ForgetAboutSite.sys.mjs" 5 ); 6 7 var db; 8 var unregisterDefers = {}; 9 var userAgentID = "4fe01c2d-72ac-4c13-93d2-bb072caf461d"; 10 11 function promiseUnregister(keyID) { 12 return new Promise(r => (unregisterDefers[keyID] = r)); 13 } 14 15 function run_test() { 16 do_get_profile(true); 17 setPrefs({ 18 userAgentID, 19 }); 20 run_next_test(); 21 } 22 23 add_task(async function setup() { 24 db = PushServiceWebSocket.newPushDB(); 25 registerCleanupFunction(() => db.drop().then(() => db.close())); 26 27 // Active and expired subscriptions for a subdomain. The active subscription 28 // should be expired, then removed; the expired subscription should be 29 // removed immediately. 30 await putTestRecord(db, "active-sub", "https://sub.example.com/sub-page", 4); 31 await putTestRecord( 32 db, 33 "active-sub-b", 34 "https://sub.example.net/sub-page", 35 4 36 ); 37 await putTestRecord( 38 db, 39 "expired-sub", 40 "https://sub.example.com/yet-another-page", 41 0 42 ); 43 44 // Active subscriptions for another subdomain. Should be unsubscribed and 45 // dropped. 46 await putTestRecord(db, "active-1", "https://sub2.example.com/some-page", 8); 47 await putTestRecord( 48 db, 49 "active-2", 50 "https://sub3.example.com/another-page", 51 16 52 ); 53 await putTestRecord( 54 db, 55 "active-1-b", 56 "https://sub2.example.net/some-page", 57 8 58 ); 59 await putTestRecord( 60 db, 61 "active-2-b", 62 "https://sub3.example.net/another-page", 63 16 64 ); 65 66 // A privileged subscription with a real URL that should not be affected 67 // because its quota is set to `Infinity`. 68 await putTestRecord( 69 db, 70 "privileged", 71 "https://sub.example.com/real-url", 72 Infinity 73 ); 74 75 let handshakeDone; 76 let handshakePromise = new Promise(r => (handshakeDone = r)); 77 PushService.init({ 78 serverURI: "wss://push.example.org/", 79 db, 80 makeWebSocket(uri) { 81 return new MockWebSocket(uri, { 82 onHello() { 83 this.serverSendMsg( 84 JSON.stringify({ 85 messageType: "hello", 86 uaid: userAgentID, 87 status: 200, 88 use_webpush: true, 89 }) 90 ); 91 handshakeDone(); 92 }, 93 onUnregister(request) { 94 let resolve = unregisterDefers[request.channelID]; 95 equal( 96 typeof resolve, 97 "function", 98 "Dropped unexpected channel ID " + request.channelID 99 ); 100 delete unregisterDefers[request.channelID]; 101 equal(request.code, 200, "Expected manual unregister reason"); 102 resolve(); 103 this.serverSendMsg( 104 JSON.stringify({ 105 messageType: "unregister", 106 status: 200, 107 channelID: request.channelID, 108 }) 109 ); 110 }, 111 }); 112 }, 113 }); 114 // For cleared subscriptions, we only send unregister requests in the 115 // background and if we're connected. 116 await handshakePromise; 117 }); 118 119 add_task(async function test_forgetAboutRootDomain() { 120 let modifiedScopes = []; 121 let promiseForgetSubs = Promise.all([ 122 promiseUnregister("active-sub"), 123 promiseUnregister("active-1"), 124 promiseUnregister("active-2"), 125 promiseObserverNotification( 126 PushServiceComponent.subscriptionModifiedTopic, 127 (subject, data) => { 128 modifiedScopes.push(data); 129 return modifiedScopes.length == 2; 130 } 131 ), 132 ]); 133 134 await ForgetAboutSite.removeDataFromBaseDomain("example.com"); 135 await promiseForgetSubs; 136 137 deepEqual( 138 modifiedScopes.sort(compareAscending), 139 [ 140 "https://sub2.example.com/some-page", 141 "https://sub3.example.com/another-page", 142 ], 143 "Should fire modified notifications for entire domain" 144 ); 145 146 let remainingIDs = await getAllKeyIDs(db); 147 deepEqual( 148 remainingIDs, 149 ["active-1-b", "active-2-b", "active-sub-b", "privileged"], 150 "Should ignore privileged records with a real URL" 151 ); 152 });