test_schema_15_migration.js (4581B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test cookie database schema 15 5 "use strict"; 6 7 add_task(async function test_schema_15_migration() { 8 // Set up a profile. 9 let profile = do_get_profile(); 10 11 // Start the cookieservice, to force creation of a database. 12 Services.cookies.sessionCookies; 13 14 // Close the profile. 15 await promise_close_profile(); 16 17 // Remove the cookie file in order to create another database file. 18 do_get_cookie_file(profile).remove(false); 19 20 // Create a schema 14 database. 21 let schema14db = new CookieDatabaseConnection( 22 do_get_cookie_file(profile), 23 14 24 ); 25 26 let now = Math.round(Date.now() / 1000); 27 28 // This cookie will be updated from NONE/LAX to UNSET 29 let cookie = new Cookie( 30 "test0", 31 "Some data", 32 "foo.com", 33 "/", 34 now, 35 now, 36 now, 37 false, 38 false, 39 false, 40 false, 41 {}, 42 Ci.nsICookie.SAMESITE_NONE, 43 Ci.nsICookie.SCHEME_UNSET 44 ); 45 cookie.sameSite = Ci.nsICookie.SAMESITE_LAX; 46 schema14db.insertCookie(cookie); 47 48 schema14db.insertCookie( 49 new Cookie( 50 "test1", 51 "Some data", 52 "foo.com", 53 "/", 54 now, 55 now, 56 now, 57 false, 58 false, 59 false, 60 false, 61 {}, 62 Ci.nsICookie.SAMESITE_UNSET, 63 Ci.nsICookie.SCHEME_UNSET 64 ) 65 ); 66 67 schema14db.insertCookie( 68 new Cookie( 69 "test2", 70 "Some data", 71 "foo.com", 72 "/", 73 now, 74 now, 75 now, 76 false, 77 false, 78 false, 79 false, 80 {}, 81 Ci.nsICookie.SAMESITE_LAX, 82 Ci.nsICookie.SCHEME_UNSET 83 ) 84 ); 85 86 schema14db.insertCookie( 87 new Cookie( 88 "test3", 89 "Some data", 90 "foo.com", 91 "/", 92 now, 93 now, 94 now, 95 false, 96 false, 97 false, 98 false, 99 {}, 100 Ci.nsICookie.SAMESITE_STRICT, 101 Ci.nsICookie.SCHEME_UNSET 102 ) 103 ); 104 105 schema14db.insertCookie( 106 new Cookie( 107 "test4", 108 "Some data", 109 "foo.com", 110 "/", 111 now, 112 now, 113 now, 114 false, 115 false, 116 false, 117 false, 118 {}, 119 Ci.nsICookie.SAMESITE_NONE, 120 Ci.nsICookie.SCHEME_UNSET 121 ) 122 ); 123 124 // This cookie will be updated from NONE/LAX to UNSET 125 cookie = new Cookie( 126 "test5", 127 "Some data", 128 "foo.com", 129 "/", 130 now, 131 now, 132 now, 133 false, 134 false, 135 false, 136 false, 137 {}, 138 Ci.nsICookie.SAMESITE_NONE, 139 Ci.nsICookie.SCHEME_UNSET 140 ); 141 cookie.sameSite = Ci.nsICookie.SAMESITE_LAX; 142 schema14db.insertCookie(cookie); 143 144 schema14db.close(); 145 schema14db = null; 146 147 // Check if we have the right entries 148 { 149 const dbConnection = Services.storage.openDatabase( 150 do_get_cookie_file(profile) 151 ); 152 const stmt = dbConnection.createStatement( 153 "SELECT name, sameSite FROM moz_cookies" 154 ); 155 156 const results = []; 157 while (stmt.executeStep()) { 158 results.push({ name: stmt.getString(0), sameSite: stmt.getInt32(1) }); 159 } 160 161 Assert.deepEqual(results, [ 162 { name: "test0", sameSite: Ci.nsICookie.SAMESITE_LAX }, 163 { name: "test1", sameSite: Ci.nsICookie.SAMESITE_UNSET }, 164 { name: "test2", sameSite: Ci.nsICookie.SAMESITE_LAX }, 165 { name: "test3", sameSite: Ci.nsICookie.SAMESITE_STRICT }, 166 { name: "test4", sameSite: Ci.nsICookie.SAMESITE_NONE }, 167 { name: "test5", sameSite: Ci.nsICookie.SAMESITE_LAX }, 168 ]); 169 170 stmt.finalize(); 171 dbConnection.close(); 172 } 173 174 // Reload profile. 175 await promise_load_profile(); 176 177 // Assert inserted cookies are in the db and correctly handled by services. 178 Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 6); 179 180 // Check if the time was reset 181 { 182 const dbConnection = Services.storage.openDatabase( 183 do_get_cookie_file(profile) 184 ); 185 const stmt = dbConnection.createStatement( 186 "SELECT name, sameSite FROM moz_cookies" 187 ); 188 189 const results = []; 190 while (stmt.executeStep()) { 191 results.push({ name: stmt.getString(0), sameSite: stmt.getInt32(1) }); 192 } 193 194 Assert.deepEqual(results, [ 195 { name: "test0", sameSite: Ci.nsICookie.SAMESITE_UNSET }, 196 { name: "test1", sameSite: Ci.nsICookie.SAMESITE_UNSET }, 197 { name: "test2", sameSite: Ci.nsICookie.SAMESITE_LAX }, 198 { name: "test3", sameSite: Ci.nsICookie.SAMESITE_STRICT }, 199 { name: "test4", sameSite: Ci.nsICookie.SAMESITE_NONE }, 200 { name: "test5", sameSite: Ci.nsICookie.SAMESITE_UNSET }, 201 ]); 202 203 stmt.finalize(); 204 dbConnection.close(); 205 } 206 207 // Cleanup 208 Services.cookies.removeAll(); 209 do_close_profile(); 210 });