Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | // Test cookie database migration from version 2 (Gecko 1.9.3) to the current |
michael@0 | 5 | // version, presently 4 (Gecko 2.0). |
michael@0 | 6 | |
michael@0 | 7 | let test_generator = do_run_test(); |
michael@0 | 8 | |
michael@0 | 9 | function run_test() { |
michael@0 | 10 | do_test_pending(); |
michael@0 | 11 | test_generator.next(); |
michael@0 | 12 | } |
michael@0 | 13 | |
michael@0 | 14 | function finish_test() { |
michael@0 | 15 | do_execute_soon(function() { |
michael@0 | 16 | test_generator.close(); |
michael@0 | 17 | do_test_finished(); |
michael@0 | 18 | }); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | function do_run_test() { |
michael@0 | 22 | // Set up a profile. |
michael@0 | 23 | let profile = do_get_profile(); |
michael@0 | 24 | |
michael@0 | 25 | // Create a schema 2 database. |
michael@0 | 26 | let schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); |
michael@0 | 27 | |
michael@0 | 28 | let now = Date.now() * 1000; |
michael@0 | 29 | let futureExpiry = Math.round(now / 1e6 + 1000); |
michael@0 | 30 | let pastExpiry = Math.round(now / 1e6 - 1000); |
michael@0 | 31 | |
michael@0 | 32 | // Populate it, with: |
michael@0 | 33 | // 1) Unexpired, unique cookies. |
michael@0 | 34 | for (let i = 0; i < 20; ++i) { |
michael@0 | 35 | let cookie = new Cookie("oh" + i, "hai", "foo.com", "/", |
michael@0 | 36 | futureExpiry, now, now + i, false, false, false); |
michael@0 | 37 | |
michael@0 | 38 | schema2db.insertCookie(cookie); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | // 2) Expired, unique cookies. |
michael@0 | 42 | for (let i = 20; i < 40; ++i) { |
michael@0 | 43 | let cookie = new Cookie("oh" + i, "hai", "bar.com", "/", |
michael@0 | 44 | pastExpiry, now, now + i, false, false, false); |
michael@0 | 45 | |
michael@0 | 46 | schema2db.insertCookie(cookie); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // 3) Many copies of the same cookie, some of which have expired and |
michael@0 | 50 | // some of which have not. |
michael@0 | 51 | for (let i = 40; i < 45; ++i) { |
michael@0 | 52 | let cookie = new Cookie("oh", "hai", "baz.com", "/", |
michael@0 | 53 | futureExpiry + i, now, now + i, false, false, false); |
michael@0 | 54 | |
michael@0 | 55 | schema2db.insertCookie(cookie); |
michael@0 | 56 | } |
michael@0 | 57 | for (let i = 45; i < 50; ++i) { |
michael@0 | 58 | let cookie = new Cookie("oh", "hai", "baz.com", "/", |
michael@0 | 59 | pastExpiry - i, now, now + i, false, false, false); |
michael@0 | 60 | |
michael@0 | 61 | schema2db.insertCookie(cookie); |
michael@0 | 62 | } |
michael@0 | 63 | for (let i = 50; i < 55; ++i) { |
michael@0 | 64 | let cookie = new Cookie("oh", "hai", "baz.com", "/", |
michael@0 | 65 | futureExpiry - i, now, now + i, false, false, false); |
michael@0 | 66 | |
michael@0 | 67 | schema2db.insertCookie(cookie); |
michael@0 | 68 | } |
michael@0 | 69 | for (let i = 55; i < 60; ++i) { |
michael@0 | 70 | let cookie = new Cookie("oh", "hai", "baz.com", "/", |
michael@0 | 71 | pastExpiry + i, now, now + i, false, false, false); |
michael@0 | 72 | |
michael@0 | 73 | schema2db.insertCookie(cookie); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | // Close it. |
michael@0 | 77 | schema2db.close(); |
michael@0 | 78 | schema2db = null; |
michael@0 | 79 | |
michael@0 | 80 | // Load the database, forcing migration to the current schema version. Then |
michael@0 | 81 | // test the expected set of cookies: |
michael@0 | 82 | // 1) All unexpired, unique cookies exist. |
michael@0 | 83 | do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20); |
michael@0 | 84 | |
michael@0 | 85 | // 2) All expired, unique cookies exist. |
michael@0 | 86 | do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20); |
michael@0 | 87 | |
michael@0 | 88 | // 3) Only one cookie remains, and it's the one with the highest expiration |
michael@0 | 89 | // time. |
michael@0 | 90 | do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 1); |
michael@0 | 91 | let enumerator = Services.cookiemgr.getCookiesFromHost("baz.com"); |
michael@0 | 92 | let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); |
michael@0 | 93 | do_check_eq(cookie.expiry, futureExpiry + 44); |
michael@0 | 94 | |
michael@0 | 95 | do_close_profile(test_generator); |
michael@0 | 96 | yield; |
michael@0 | 97 | |
michael@0 | 98 | // Open the database so we can execute some more schema 2 statements on it. |
michael@0 | 99 | schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); |
michael@0 | 100 | |
michael@0 | 101 | // Populate it with more cookies. |
michael@0 | 102 | for (let i = 60; i < 80; ++i) { |
michael@0 | 103 | let cookie = new Cookie("oh" + i, "hai", "foo.com", "/", |
michael@0 | 104 | futureExpiry, now, now + i, false, false, false); |
michael@0 | 105 | |
michael@0 | 106 | schema2db.insertCookie(cookie); |
michael@0 | 107 | } |
michael@0 | 108 | for (let i = 80; i < 100; ++i) { |
michael@0 | 109 | let cookie = new Cookie("oh" + i, "hai", "cat.com", "/", |
michael@0 | 110 | futureExpiry, now, now + i, false, false, false); |
michael@0 | 111 | |
michael@0 | 112 | schema2db.insertCookie(cookie); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | // Attempt to add a cookie with the same (name, host, path) values as another |
michael@0 | 116 | // cookie. This should succeed since we have a REPLACE clause for conflict on |
michael@0 | 117 | // the unique index. |
michael@0 | 118 | let cookie = new Cookie("oh", "hai", "baz.com", "/", |
michael@0 | 119 | futureExpiry, now, now + 100, false, false, false); |
michael@0 | 120 | |
michael@0 | 121 | schema2db.insertCookie(cookie); |
michael@0 | 122 | |
michael@0 | 123 | // Check that there is, indeed, a singular cookie for baz.com. |
michael@0 | 124 | do_check_eq(do_count_cookies_in_db(schema2db.db, "baz.com"), 1); |
michael@0 | 125 | |
michael@0 | 126 | // Close it. |
michael@0 | 127 | schema2db.close(); |
michael@0 | 128 | schema2db = null; |
michael@0 | 129 | |
michael@0 | 130 | // Back up the database, so we can test both asynchronous and synchronous |
michael@0 | 131 | // loading separately. |
michael@0 | 132 | let file = do_get_cookie_file(profile); |
michael@0 | 133 | let copy = profile.clone(); |
michael@0 | 134 | copy.append("cookies.sqlite.copy"); |
michael@0 | 135 | file.copyTo(null, copy.leafName); |
michael@0 | 136 | |
michael@0 | 137 | // Load the database asynchronously, forcing a purge of the newly-added |
michael@0 | 138 | // cookies. (Their baseDomain column will be NULL.) |
michael@0 | 139 | do_load_profile(test_generator); |
michael@0 | 140 | yield; |
michael@0 | 141 | |
michael@0 | 142 | // Test the expected set of cookies. |
michael@0 | 143 | do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20); |
michael@0 | 144 | do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20); |
michael@0 | 145 | do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0); |
michael@0 | 146 | do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0); |
michael@0 | 147 | |
michael@0 | 148 | do_close_profile(test_generator); |
michael@0 | 149 | yield; |
michael@0 | 150 | |
michael@0 | 151 | // Open the database and prove that they were deleted. |
michael@0 | 152 | schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); |
michael@0 | 153 | do_check_eq(do_count_cookies_in_db(schema2db.db), 40); |
michael@0 | 154 | do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20); |
michael@0 | 155 | do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20); |
michael@0 | 156 | schema2db.close(); |
michael@0 | 157 | |
michael@0 | 158 | // Copy the database back. |
michael@0 | 159 | file.remove(false); |
michael@0 | 160 | copy.copyTo(null, file.leafName); |
michael@0 | 161 | |
michael@0 | 162 | // Load the database host-at-a-time. |
michael@0 | 163 | do_load_profile(); |
michael@0 | 164 | |
michael@0 | 165 | // Test the expected set of cookies. |
michael@0 | 166 | do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20); |
michael@0 | 167 | do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20); |
michael@0 | 168 | do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0); |
michael@0 | 169 | do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0); |
michael@0 | 170 | |
michael@0 | 171 | do_close_profile(test_generator); |
michael@0 | 172 | yield; |
michael@0 | 173 | |
michael@0 | 174 | // Open the database and prove that they were deleted. |
michael@0 | 175 | schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); |
michael@0 | 176 | do_check_eq(do_count_cookies_in_db(schema2db.db), 40); |
michael@0 | 177 | do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20); |
michael@0 | 178 | do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20); |
michael@0 | 179 | schema2db.close(); |
michael@0 | 180 | |
michael@0 | 181 | // Copy the database back. |
michael@0 | 182 | file.remove(false); |
michael@0 | 183 | copy.copyTo(null, file.leafName); |
michael@0 | 184 | |
michael@0 | 185 | // Load the database synchronously, in its entirety. |
michael@0 | 186 | do_load_profile(); |
michael@0 | 187 | do_check_eq(do_count_cookies(), 40); |
michael@0 | 188 | |
michael@0 | 189 | // Test the expected set of cookies. |
michael@0 | 190 | do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20); |
michael@0 | 191 | do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20); |
michael@0 | 192 | do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 0); |
michael@0 | 193 | do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 0); |
michael@0 | 194 | |
michael@0 | 195 | do_close_profile(test_generator); |
michael@0 | 196 | yield; |
michael@0 | 197 | |
michael@0 | 198 | // Open the database and prove that they were deleted. |
michael@0 | 199 | schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2); |
michael@0 | 200 | do_check_eq(do_count_cookies_in_db(schema2db.db), 40); |
michael@0 | 201 | do_check_eq(do_count_cookies_in_db(schema2db.db, "foo.com"), 20); |
michael@0 | 202 | do_check_eq(do_count_cookies_in_db(schema2db.db, "bar.com"), 20); |
michael@0 | 203 | schema2db.close(); |
michael@0 | 204 | |
michael@0 | 205 | finish_test(); |
michael@0 | 206 | } |
michael@0 | 207 |