test_cookies_profile_close.js (3075B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test that the cookie APIs behave sanely after 'profile-before-change'. 5 6 "use strict"; 7 8 add_task(async () => { 9 // Set up a profile. 10 do_get_profile(); 11 12 // Allow all cookies. 13 Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); 14 Services.prefs.setBoolPref( 15 "network.cookieJarSettings.unblocked_for_testing", 16 true 17 ); 18 Services.prefs.setBoolPref("dom.security.https_first", false); 19 20 // Start the cookieservice. 21 Services.cookies; 22 23 CookieXPCShellUtils.createServer({ hosts: ["foo.com"] }); 24 25 // Set a cookie. 26 let uri = NetUtil.newURI("http://foo.com"); 27 let channel = NetUtil.newChannel({ 28 uri, 29 loadUsingSystemPrincipal: true, 30 contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 31 }); 32 33 Services.scriptSecurityManager.createContentPrincipal(uri, {}); 34 35 await CookieXPCShellUtils.setCookieToDocument( 36 uri.spec, 37 "oh=hai; max-age=1000" 38 ); 39 40 let cookies = Services.cookies.cookies; 41 Assert.equal(cookies.length, 1); 42 let cookie = cookies[0]; 43 44 // Fire 'profile-before-change'. 45 do_close_profile(); 46 47 let promise = new _promise_observer("cookie-db-closed"); 48 49 // Check that the APIs behave appropriately. 50 Assert.equal( 51 await CookieXPCShellUtils.getCookieStringFromDocument("http://foo.com/"), 52 "" 53 ); 54 55 Assert.equal(Services.cookies.getCookieStringFromHttp(uri, channel), ""); 56 57 await CookieXPCShellUtils.setCookieToDocument(uri.spec, "oh2=hai"); 58 59 Services.cookies.setCookieStringFromHttp(uri, "oh3=hai", channel); 60 Assert.equal( 61 await CookieXPCShellUtils.getCookieStringFromDocument("http://foo.com/"), 62 "" 63 ); 64 65 do_check_throws(function () { 66 Services.cookies.removeAll(); 67 }, Cr.NS_ERROR_NOT_AVAILABLE); 68 69 do_check_throws(function () { 70 Services.cookies.cookies; 71 }, Cr.NS_ERROR_NOT_AVAILABLE); 72 73 do_check_throws(function () { 74 const cv = Services.cookies.add( 75 "foo.com", 76 "", 77 "oh4", 78 "hai", 79 false, 80 false, 81 false, 82 0, 83 {}, 84 Ci.nsICookie.SAMESITE_UNSET, 85 Ci.nsICookie.SCHEME_HTTPS 86 ); 87 Assert.equal(cv.result, Ci.nsICookieValidation.eOK, "Valid cookie"); 88 }, Cr.NS_ERROR_NOT_AVAILABLE); 89 90 do_check_throws(function () { 91 Services.cookies.remove("foo.com", "", "oh4", {}); 92 }, Cr.NS_ERROR_NOT_AVAILABLE); 93 94 do_check_throws(function () { 95 Services.cookies.cookieExists(cookie.host, cookie.path, cookie.name, {}); 96 }, Cr.NS_ERROR_NOT_AVAILABLE); 97 98 do_check_throws(function () { 99 Services.cookies.countCookiesFromHost("foo.com"); 100 }, Cr.NS_ERROR_NOT_AVAILABLE); 101 102 do_check_throws(function () { 103 Services.cookies.getCookiesFromHost("foo.com", {}); 104 }, Cr.NS_ERROR_NOT_AVAILABLE); 105 106 // Wait for the database to finish closing. 107 await promise; 108 109 // Load the profile and check that the API is available. 110 do_load_profile(); 111 Assert.ok( 112 Services.cookies.cookieExists(cookie.host, cookie.path, cookie.name, {}) 113 ); 114 Services.prefs.clearUserPref("dom.security.https_first"); 115 });