test_discovery.js (4639B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ 3 */ 4 5 "use strict"; 6 7 // ClientID fails without... 8 do_get_profile(); 9 10 const { TestUtils } = ChromeUtils.importESModule( 11 "resource://testing-common/TestUtils.sys.mjs" 12 ); 13 const { ClientID } = ChromeUtils.importESModule( 14 "resource://gre/modules/ClientID.sys.mjs" 15 ); 16 const { Discovery } = ChromeUtils.importESModule( 17 "resource:///modules/Discovery.sys.mjs" 18 ); 19 const { ContextualIdentityService } = ChromeUtils.importESModule( 20 "resource://gre/modules/ContextualIdentityService.sys.mjs" 21 ); 22 23 const TAAR_COOKIE_NAME = "taarId"; 24 25 add_task(async function test_discovery() { 26 let uri = Services.io.newURI("https://example.com/foobar"); 27 28 // Ensure the prefs we need 29 Services.prefs.setBoolPref("browser.discovery.enabled", true); 30 Services.prefs.setBoolPref("browser.discovery.containers.enabled", true); 31 Services.prefs.setBoolPref("datareporting.healthreport.uploadEnabled", true); 32 Services.prefs.setCharPref("browser.discovery.sites", uri.host); 33 34 registerCleanupFunction(() => { 35 Services.prefs.clearUserPref("browser.discovery.enabled"); 36 Services.prefs.clearUserPref("browser.discovery.containers.enabled"); 37 Services.prefs.clearUserPref("browser.discovery.sites"); 38 Services.prefs.clearUserPref("datareporting.healthreport.uploadEnabled"); 39 }); 40 41 // This is normally initialized by telemetry, force id creation. This results 42 // in Discovery setting the cookie. 43 await ClientID.getClientID(); 44 await Discovery.update(); 45 46 ok( 47 Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {}), 48 "cookie exists" 49 ); 50 ok( 51 !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, { 52 privateBrowsingId: 1, 53 }), 54 "no private cookie exists" 55 ); 56 ContextualIdentityService.getPublicIdentities().forEach(identity => { 57 let { userContextId } = identity; 58 equal( 59 Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, { 60 userContextId, 61 }), 62 identity.public, 63 "cookie exists" 64 ); 65 }); 66 67 // Test the addition of a new container. 68 let changed = TestUtils.topicObserved("cookie-changed", subject => { 69 let cookie = subject 70 .QueryInterface(Ci.nsICookieNotification) 71 .cookie.QueryInterface(Ci.nsICookie); 72 equal(cookie.name, TAAR_COOKIE_NAME, "taar cookie exists"); 73 equal(cookie.host, uri.host, "cookie exists for host"); 74 equal( 75 cookie.originAttributes.userContextId, 76 container.userContextId, 77 "cookie userContextId is correct" 78 ); 79 return true; 80 }); 81 let container = ContextualIdentityService.create( 82 "New Container", 83 "Icon", 84 "Color" 85 ); 86 await changed; 87 88 // Test disabling 89 Discovery.enabled = false; 90 // Wait for the update to remove the cookie. 91 await TestUtils.waitForCondition(() => { 92 return !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {}); 93 }); 94 95 ContextualIdentityService.getPublicIdentities().forEach(identity => { 96 let { userContextId } = identity; 97 ok( 98 !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, { 99 userContextId, 100 }), 101 "no cookie exists" 102 ); 103 }); 104 105 // turn off containers 106 Services.prefs.setBoolPref("browser.discovery.containers.enabled", false); 107 108 Discovery.enabled = true; 109 await TestUtils.waitForCondition(() => { 110 return Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {}); 111 }); 112 // make sure we did not set cookies on containers 113 ContextualIdentityService.getPublicIdentities().forEach(identity => { 114 let { userContextId } = identity; 115 ok( 116 !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, { 117 userContextId, 118 }), 119 "no cookie exists" 120 ); 121 }); 122 123 // Make sure clientId changes update discovery 124 changed = TestUtils.topicObserved("cookie-changed", subject => { 125 let notification = subject.QueryInterface(Ci.nsICookieNotification); 126 if (notification.action != Ci.nsICookieNotification.COOKIE_ADDED) { 127 return false; 128 } 129 let cookie = notification.cookie.QueryInterface(Ci.nsICookie); 130 equal(cookie.name, TAAR_COOKIE_NAME, "taar cookie exists"); 131 equal(cookie.host, uri.host, "cookie exists for host"); 132 return true; 133 }); 134 await ClientID.resetIdentifiers(); 135 await ClientID.getClientID(); 136 await changed; 137 138 // Make sure disabling telemetry disables discovery. 139 Services.prefs.setBoolPref("datareporting.healthreport.uploadEnabled", false); 140 await TestUtils.waitForCondition(() => { 141 return !Services.cookies.cookieExists(uri.host, "/", TAAR_COOKIE_NAME, {}); 142 }); 143 });