1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/cookie/test/unit/test_eviction.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,247 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +let test_generator = do_run_test(); 1.8 + 1.9 +function run_test() 1.10 +{ 1.11 + do_test_pending(); 1.12 + do_run_generator(test_generator); 1.13 +} 1.14 + 1.15 +function continue_test() 1.16 +{ 1.17 + do_run_generator(test_generator); 1.18 +} 1.19 + 1.20 +function repeat_test() 1.21 +{ 1.22 + // The test is probably going to fail because setting a batch of cookies took 1.23 + // a significant fraction of 'gPurgeAge'. Compensate by rerunning the 1.24 + // test with a larger purge age. 1.25 + do_check_true(gPurgeAge < 64); 1.26 + gPurgeAge *= 2; 1.27 + gShortExpiry *= 2; 1.28 + 1.29 + do_execute_soon(function() { 1.30 + test_generator.close(); 1.31 + test_generator = do_run_test(); 1.32 + do_run_generator(test_generator); 1.33 + }); 1.34 +} 1.35 + 1.36 +// Purge threshold, in seconds. 1.37 +let gPurgeAge = 1; 1.38 + 1.39 +// Short expiry age, in seconds. 1.40 +let gShortExpiry = 2; 1.41 + 1.42 +// Required delay to ensure a purge occurs, in milliseconds. This must be at 1.43 +// least gPurgeAge + 10%, and includes a little fuzz to account for timer 1.44 +// resolution and possible differences between PR_Now() and Date.now(). 1.45 +function get_purge_delay() 1.46 +{ 1.47 + return gPurgeAge * 1100 + 100; 1.48 +} 1.49 + 1.50 +// Required delay to ensure a cookie set with an expiry time 'gShortExpiry' into 1.51 +// the future will have expired. 1.52 +function get_expiry_delay() 1.53 +{ 1.54 + return gShortExpiry * 1000 + 100; 1.55 +} 1.56 + 1.57 +function do_run_test() 1.58 +{ 1.59 + // Set up a profile. 1.60 + let profile = do_get_profile(); 1.61 + 1.62 + // twiddle prefs to convenient values for this test 1.63 + Services.prefs.setIntPref("network.cookie.purgeAge", gPurgeAge); 1.64 + Services.prefs.setIntPref("network.cookie.maxNumber", 100); 1.65 + 1.66 + let expiry = Date.now() / 1000 + 1000; 1.67 + 1.68 + // eviction is performed based on two limits: when the total number of cookies 1.69 + // exceeds maxNumber + 10% (110), and when cookies are older than purgeAge 1.70 + // (1 second). purging is done when both conditions are satisfied, and only 1.71 + // those cookies are purged. 1.72 + 1.73 + // we test the following cases of eviction: 1.74 + // 1) excess and age are satisfied, but only some of the excess are old enough 1.75 + // to be purged. 1.76 + Services.cookiemgr.removeAll(); 1.77 + if (!set_cookies(0, 5, expiry)) { 1.78 + repeat_test(); 1.79 + return; 1.80 + } 1.81 + // Sleep a while, to make sure the first batch of cookies is older than 1.82 + // the second (timer resolution varies on different platforms). 1.83 + do_timeout(get_purge_delay(), continue_test); 1.84 + yield; 1.85 + if (!set_cookies(5, 111, expiry)) { 1.86 + repeat_test(); 1.87 + return; 1.88 + } 1.89 + 1.90 + // Fake a profile change, to ensure eviction affects the database correctly. 1.91 + do_close_profile(test_generator); 1.92 + yield; 1.93 + do_load_profile(); 1.94 + do_check_true(check_remaining_cookies(111, 5, 106)); 1.95 + 1.96 + // 2) excess and age are satisfied, and all of the excess are old enough 1.97 + // to be purged. 1.98 + Services.cookiemgr.removeAll(); 1.99 + if (!set_cookies(0, 10, expiry)) { 1.100 + repeat_test(); 1.101 + return; 1.102 + } 1.103 + do_timeout(get_purge_delay(), continue_test); 1.104 + yield; 1.105 + if (!set_cookies(10, 111, expiry)) { 1.106 + repeat_test(); 1.107 + return; 1.108 + } 1.109 + 1.110 + do_close_profile(test_generator); 1.111 + yield; 1.112 + do_load_profile(); 1.113 + do_check_true(check_remaining_cookies(111, 10, 101)); 1.114 + 1.115 + // 3) excess and age are satisfied, and more than the excess are old enough 1.116 + // to be purged. 1.117 + Services.cookiemgr.removeAll(); 1.118 + if (!set_cookies(0, 50, expiry)) { 1.119 + repeat_test(); 1.120 + return; 1.121 + } 1.122 + do_timeout(get_purge_delay(), continue_test); 1.123 + yield; 1.124 + if (!set_cookies(50, 111, expiry)) { 1.125 + repeat_test(); 1.126 + return; 1.127 + } 1.128 + 1.129 + do_close_profile(test_generator); 1.130 + yield; 1.131 + do_load_profile(); 1.132 + do_check_true(check_remaining_cookies(111, 50, 101)); 1.133 + 1.134 + // 4) excess but not age are satisfied. 1.135 + Services.cookiemgr.removeAll(); 1.136 + if (!set_cookies(0, 120, expiry)) { 1.137 + repeat_test(); 1.138 + return; 1.139 + } 1.140 + 1.141 + do_close_profile(test_generator); 1.142 + yield; 1.143 + do_load_profile(); 1.144 + do_check_true(check_remaining_cookies(120, 0, 120)); 1.145 + 1.146 + // 5) age but not excess are satisfied. 1.147 + Services.cookiemgr.removeAll(); 1.148 + if (!set_cookies(0, 20, expiry)) { 1.149 + repeat_test(); 1.150 + return; 1.151 + } 1.152 + do_timeout(get_purge_delay(), continue_test); 1.153 + yield; 1.154 + if (!set_cookies(20, 110, expiry)) { 1.155 + repeat_test(); 1.156 + return; 1.157 + } 1.158 + 1.159 + do_close_profile(test_generator); 1.160 + yield; 1.161 + do_load_profile(); 1.162 + do_check_true(check_remaining_cookies(110, 20, 110)); 1.163 + 1.164 + // 6) Excess and age are satisfied, but the cookie limit can be satisfied by 1.165 + // purging expired cookies. 1.166 + Services.cookiemgr.removeAll(); 1.167 + let shortExpiry = Math.floor(Date.now() / 1000) + gShortExpiry; 1.168 + if (!set_cookies(0, 20, shortExpiry)) { 1.169 + repeat_test(); 1.170 + return; 1.171 + } 1.172 + do_timeout(get_expiry_delay(), continue_test); 1.173 + yield; 1.174 + if (!set_cookies(20, 110, expiry)) { 1.175 + repeat_test(); 1.176 + return; 1.177 + } 1.178 + do_timeout(get_purge_delay(), continue_test); 1.179 + yield; 1.180 + if (!set_cookies(110, 111, expiry)) { 1.181 + repeat_test(); 1.182 + return; 1.183 + } 1.184 + 1.185 + do_close_profile(test_generator); 1.186 + yield; 1.187 + do_load_profile(); 1.188 + do_check_true(check_remaining_cookies(111, 20, 91)); 1.189 + 1.190 + do_finish_generator_test(test_generator); 1.191 +} 1.192 + 1.193 +// Set 'end - begin' total cookies, with consecutively increasing hosts numbered 1.194 +// 'begin' to 'end'. 1.195 +function set_cookies(begin, end, expiry) 1.196 +{ 1.197 + do_check_true(begin != end); 1.198 + 1.199 + let beginTime; 1.200 + for (let i = begin; i < end; ++i) { 1.201 + let host = "eviction." + i + ".tests"; 1.202 + Services.cookiemgr.add(host, "", "test", "eviction", false, false, false, 1.203 + expiry); 1.204 + 1.205 + if (i == begin) 1.206 + beginTime = get_creationTime(i); 1.207 + } 1.208 + 1.209 + let endTime = get_creationTime(end - 1); 1.210 + do_check_true(begin == end - 1 || endTime > beginTime); 1.211 + if (endTime - beginTime > gPurgeAge * 1000000) { 1.212 + // Setting cookies took an amount of time very close to the purge threshold. 1.213 + // Retry the test with a larger threshold. 1.214 + return false; 1.215 + } 1.216 + 1.217 + return true; 1.218 +} 1.219 + 1.220 +function get_creationTime(i) 1.221 +{ 1.222 + let host = "eviction." + i + ".tests"; 1.223 + let enumerator = Services.cookiemgr.getCookiesFromHost(host); 1.224 + do_check_true(enumerator.hasMoreElements()); 1.225 + let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); 1.226 + return cookie.creationTime; 1.227 +} 1.228 + 1.229 +// Test that 'aNumberToExpect' cookies remain after purging is complete, and 1.230 +// that the cookies that remain consist of the set expected given the number of 1.231 +// of older and newer cookies -- eviction should occur by order of lastAccessed 1.232 +// time, if both the limit on total cookies (maxNumber + 10%) and the purge age 1.233 +// + 10% are exceeded. 1.234 +function check_remaining_cookies(aNumberTotal, aNumberOld, aNumberToExpect) { 1.235 + var enumerator = Services.cookiemgr.enumerator; 1.236 + 1.237 + i = 0; 1.238 + while (enumerator.hasMoreElements()) { 1.239 + var cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); 1.240 + ++i; 1.241 + 1.242 + if (aNumberTotal != aNumberToExpect) { 1.243 + // make sure the cookie is one of the batch we expect was purged. 1.244 + var hostNumber = new Number(cookie.rawHost.split(".")[1]); 1.245 + if (hostNumber < (aNumberOld - aNumberToExpect)) break; 1.246 + } 1.247 + } 1.248 + 1.249 + return i == aNumberToExpect; 1.250 +}