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 3 (prerelease Gecko 2.0) to the |
michael@0 | 5 | // current 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 3 database. |
michael@0 | 26 | let schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3); |
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 | schema3db.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 | schema3db.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 | schema3db.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 | schema3db.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 | schema3db.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 | schema3db.insertCookie(cookie); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | // Close it. |
michael@0 | 77 | schema3db.close(); |
michael@0 | 78 | schema3db = 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 3 statements on it. |
michael@0 | 99 | schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3); |
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", "cat.com", "/", |
michael@0 | 104 | futureExpiry, now, now + i, false, false, false); |
michael@0 | 105 | |
michael@0 | 106 | schema3db.insertCookie(cookie); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | // Close it. |
michael@0 | 110 | schema3db.close(); |
michael@0 | 111 | schema3db = null; |
michael@0 | 112 | |
michael@0 | 113 | // Load the database. The cookies added immediately prior will have a NULL |
michael@0 | 114 | // creationTime column. |
michael@0 | 115 | do_load_profile(); |
michael@0 | 116 | |
michael@0 | 117 | // Test the expected set of cookies. |
michael@0 | 118 | do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 20); |
michael@0 | 119 | let enumerator = Services.cookiemgr.getCookiesFromHost("cat.com"); |
michael@0 | 120 | let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2); |
michael@0 | 121 | do_check_eq(cookie.creationTime, 0); |
michael@0 | 122 | |
michael@0 | 123 | finish_test(); |
michael@0 | 124 | } |
michael@0 | 125 |