toolkit/components/sqlite/tests/xpcshell/data/worker_sqlite_internal.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 importScripts("worker_sqlite_shared.js",
michael@0 5 "resource://gre/modules/workers/require.js");
michael@0 6
michael@0 7 self.onmessage = function onmessage(msg) {
michael@0 8 try {
michael@0 9 run_test();
michael@0 10 } catch (ex) {
michael@0 11 let {message, moduleStack, moduleName, lineNumber} = ex;
michael@0 12 let error = new Error(message, moduleName, lineNumber);
michael@0 13 error.stack = moduleStack;
michael@0 14 dump("Uncaught error: " + error + "\n");
michael@0 15 dump("Full stack: " + moduleStack + "\n");
michael@0 16 throw error;
michael@0 17 }
michael@0 18 };
michael@0 19
michael@0 20 let Sqlite;
michael@0 21
michael@0 22 let SQLITE_OK; /* Successful result */
michael@0 23 let SQLITE_ROW; /* sqlite3_step() has another row ready */
michael@0 24 let SQLITE_DONE; /* sqlite3_step() has finished executing */
michael@0 25
michael@0 26 function test_init() {
michael@0 27 do_print("Starting test_init");
michael@0 28 // Sqlite should be loaded.
michael@0 29 Sqlite = require("resource://gre/modules/sqlite/sqlite_internal.js");
michael@0 30 do_check_neq(typeof Sqlite, "undefined");
michael@0 31 do_check_neq(typeof Sqlite.Constants, "undefined");
michael@0 32 SQLITE_OK = Sqlite.Constants.SQLITE_OK;
michael@0 33 SQLITE_ROW = Sqlite.Constants.SQLITE_ROW;
michael@0 34 SQLITE_DONE = Sqlite.Constants.SQLITE_DONE;
michael@0 35 }
michael@0 36
michael@0 37 /**
michael@0 38 * Clean up the database.
michael@0 39 * @param {sqlite3_ptr} db A pointer to the database.
michael@0 40 */
michael@0 41 function cleanupDB(db) {
michael@0 42 withQuery(db, "DROP TABLE IF EXISTS TEST;", SQLITE_DONE);
michael@0 43 }
michael@0 44
michael@0 45 /**
michael@0 46 * Open and close sqlite3 database.
michael@0 47 * @param {String} open A name of the sqlite3 open function to be
michael@0 48 * used.
michael@0 49 * @param {Array} openArgs = [] Optional arguments to open function.
michael@0 50 * @param {Function} callback = null An optional callback to be run after the
michael@0 51 * database is opened but before it is
michael@0 52 * closed.
michael@0 53 */
michael@0 54 function withDB(open, openArgs = [], callback = null) {
michael@0 55 let db = Sqlite.Type.sqlite3_ptr.implementation();
michael@0 56 let dbPtr = db.address();
michael@0 57
michael@0 58 // Open database.
michael@0 59 let result = Sqlite[open].apply(Sqlite, ["data/test.db", dbPtr].concat(
michael@0 60 openArgs));
michael@0 61 do_check_eq(result, SQLITE_OK);
michael@0 62
michael@0 63 // Drop the test table if it already exists.
michael@0 64 cleanupDB(db);
michael@0 65
michael@0 66 try {
michael@0 67 if (callback) {
michael@0 68 callback(db);
michael@0 69 }
michael@0 70 } catch (ex) {
michael@0 71 do_check_true(false);
michael@0 72 throw ex;
michael@0 73 } finally {
michael@0 74 // Drop the test table if it still exists.
michael@0 75 cleanupDB(db);
michael@0 76 // Close data base.
michael@0 77 result = Sqlite.close(db);
michael@0 78 do_check_eq(result, SQLITE_OK);
michael@0 79 }
michael@0 80 }
michael@0 81
michael@0 82 /**
michael@0 83 * Execute an SQL query using sqlite3 API.
michael@0 84 * @param {sqlite3_ptr} db A pointer to the database.
michael@0 85 * @param {String} sql A SQL query string.
michael@0 86 * @param {Number} stepResult Expected result code after evaluating the
michael@0 87 * SQL statement.
michael@0 88 * @param {Function} bind An optional callback with SQL binding steps.
michael@0 89 * @param {Function} callback An optional callback that runs after the SQL
michael@0 90 * query completes.
michael@0 91 */
michael@0 92 function withQuery(db, sql, stepResult, bind, callback) {
michael@0 93 // Create an instance of a single SQL statement.
michael@0 94 let sqlStmt = Sqlite.Type.sqlite3_stmt_ptr.implementation();
michael@0 95 let sqlStmtPtr = sqlStmt.address();
michael@0 96
michael@0 97 // Unused portion of an SQL query.
michael@0 98 let unused = Sqlite.Type.cstring.implementation();
michael@0 99 let unusedPtr = unused.address();
michael@0 100
michael@0 101 // Compile an SQL statement.
michael@0 102 let result = Sqlite.prepare_v2(db, sql, sql.length, sqlStmtPtr, unusedPtr);
michael@0 103 do_check_eq(result, SQLITE_OK);
michael@0 104
michael@0 105 try {
michael@0 106 if (bind) {
michael@0 107 bind(sqlStmt);
michael@0 108 }
michael@0 109
michael@0 110 // Evaluate an SQL statement.
michael@0 111 result = Sqlite.step(sqlStmt);
michael@0 112 do_check_eq(result, stepResult);
michael@0 113
michael@0 114 if (callback) {
michael@0 115 callback(sqlStmt);
michael@0 116 }
michael@0 117 } catch (ex) {
michael@0 118 do_check_true(false);
michael@0 119 throw ex;
michael@0 120 } finally {
michael@0 121 // Destroy a prepared statement object.
michael@0 122 result = Sqlite.finalize(sqlStmt);
michael@0 123 do_check_eq(result, SQLITE_OK);
michael@0 124 }
michael@0 125 }
michael@0 126
michael@0 127 function test_open_close() {
michael@0 128 do_print("Starting test_open_close");
michael@0 129 do_check_eq(typeof Sqlite.open, "function");
michael@0 130 do_check_eq(typeof Sqlite.close, "function");
michael@0 131
michael@0 132 withDB("open");
michael@0 133 }
michael@0 134
michael@0 135 function test_open_v2_close() {
michael@0 136 do_print("Starting test_open_v2_close");
michael@0 137 do_check_eq(typeof Sqlite.open_v2, "function");
michael@0 138
michael@0 139 withDB("open_v2", [0x02, null]);
michael@0 140 }
michael@0 141
michael@0 142 function createTableOnOpen(db) {
michael@0 143 withQuery(db, "CREATE TABLE TEST(" +
michael@0 144 "ID INT PRIMARY KEY NOT NULL," +
michael@0 145 "FIELD1 INT," +
michael@0 146 "FIELD2 REAL," +
michael@0 147 "FIELD3 TEXT," +
michael@0 148 "FIELD4 TEXT," +
michael@0 149 "FIELD5 BLOB" +
michael@0 150 ");", SQLITE_DONE);
michael@0 151 }
michael@0 152
michael@0 153 function test_create_table() {
michael@0 154 do_print("Starting test_create_table");
michael@0 155 do_check_eq(typeof Sqlite.prepare_v2, "function");
michael@0 156 do_check_eq(typeof Sqlite.step, "function");
michael@0 157 do_check_eq(typeof Sqlite.finalize, "function");
michael@0 158
michael@0 159 withDB("open", [], createTableOnOpen);
michael@0 160 }
michael@0 161
michael@0 162 /**
michael@0 163 * Read column values after evaluating the SQL SELECT statement.
michael@0 164 * @param {sqlite3_stmt_ptr} sqlStmt A pointer to the SQL statement.
michael@0 165 */
michael@0 166 function onSqlite3Step(sqlStmt) {
michael@0 167 // Get an int value from a query result from the ID (column 0).
michael@0 168 let field = Sqlite.column_int(sqlStmt, 0);
michael@0 169 do_check_eq(field, 3);
michael@0 170
michael@0 171 // Get an int value from a query result from the column 1.
michael@0 172 field = Sqlite.column_int(sqlStmt, 1);
michael@0 173 do_check_eq(field, 2);
michael@0 174 // Get an int64 value from a query result from the column 1.
michael@0 175 field = Sqlite.column_int64(sqlStmt, 1);
michael@0 176 do_check_eq(field, 2);
michael@0 177
michael@0 178 // Get a double value from a query result from the column 2.
michael@0 179 field = Sqlite.column_double(sqlStmt, 2);
michael@0 180 do_check_eq(field, 1.2);
michael@0 181
michael@0 182 // Get a number of bytes of the value in the column 3.
michael@0 183 let bytes = Sqlite.column_bytes(sqlStmt, 3);
michael@0 184 do_check_eq(bytes, 4);
michael@0 185 // Get a text(cstring) value from a query result from the column 3.
michael@0 186 field = Sqlite.column_text(sqlStmt, 3);
michael@0 187 do_check_eq(field.readString(), "DATA");
michael@0 188
michael@0 189 // Get a number of bytes of the UTF-16 value in the column 4.
michael@0 190 bytes = Sqlite.column_bytes16(sqlStmt, 4);
michael@0 191 do_check_eq(bytes, 8);
michael@0 192 // Get a text16(wstring) value from a query result from the column 4.
michael@0 193 field = Sqlite.column_text16(sqlStmt, 4);
michael@0 194 do_check_eq(field.readString(), "TADA");
michael@0 195
michael@0 196 // Get a blob value from a query result from the column 5.
michael@0 197 field = Sqlite.column_blob(sqlStmt, 5);
michael@0 198 do_check_eq(ctypes.cast(field,
michael@0 199 Sqlite.Type.cstring.implementation).readString(), "BLOB");
michael@0 200 }
michael@0 201
michael@0 202 function test_insert_select() {
michael@0 203 do_print("Starting test_insert_select");
michael@0 204 do_check_eq(typeof Sqlite.column_int, "function");
michael@0 205 do_check_eq(typeof Sqlite.column_int64, "function");
michael@0 206 do_check_eq(typeof Sqlite.column_double, "function");
michael@0 207 do_check_eq(typeof Sqlite.column_bytes, "function");
michael@0 208 do_check_eq(typeof Sqlite.column_text, "function");
michael@0 209 do_check_eq(typeof Sqlite.column_text16, "function");
michael@0 210 do_check_eq(typeof Sqlite.column_blob, "function");
michael@0 211
michael@0 212 function onOpen(db) {
michael@0 213 createTableOnOpen(db);
michael@0 214 withQuery(db,
michael@0 215 "INSERT INTO TEST VALUES (3, 2, 1.2, \"DATA\", \"TADA\", \"BLOB\");",
michael@0 216 SQLITE_DONE);
michael@0 217 withQuery(db, "SELECT * FROM TEST;", SQLITE_ROW, null, onSqlite3Step);
michael@0 218 }
michael@0 219
michael@0 220 withDB("open", [], onOpen);
michael@0 221 }
michael@0 222
michael@0 223 function test_insert_bind_select() {
michael@0 224 do_print("Starting test_insert_bind_select");
michael@0 225 do_check_eq(typeof Sqlite.bind_int, "function");
michael@0 226 do_check_eq(typeof Sqlite.bind_int64, "function");
michael@0 227 do_check_eq(typeof Sqlite.bind_double, "function");
michael@0 228 do_check_eq(typeof Sqlite.bind_text, "function");
michael@0 229 do_check_eq(typeof Sqlite.bind_text16, "function");
michael@0 230 do_check_eq(typeof Sqlite.bind_blob, "function");
michael@0 231
michael@0 232 function onBind(sqlStmt) {
michael@0 233 // Bind an int value to the ID (column 0).
michael@0 234 let result = Sqlite.bind_int(sqlStmt, 1, 3);
michael@0 235 do_check_eq(result, SQLITE_OK);
michael@0 236
michael@0 237 // Bind an int64 value to the FIELD1 (column 1).
michael@0 238 result = Sqlite.bind_int64(sqlStmt, 2, 2);
michael@0 239 do_check_eq(result, SQLITE_OK);
michael@0 240
michael@0 241 // Bind a double value to the FIELD2 (column 2).
michael@0 242 result = Sqlite.bind_double(sqlStmt, 3, 1.2);
michael@0 243 do_check_eq(result, SQLITE_OK);
michael@0 244
michael@0 245 // Destructor.
michael@0 246 let destructor = Sqlite.Constants.SQLITE_TRANSIENT;
michael@0 247 // Bind a text value to the FIELD3 (column 3).
michael@0 248 result = Sqlite.bind_text(sqlStmt, 4, "DATA", 4, destructor);
michael@0 249 do_check_eq(result, SQLITE_OK);
michael@0 250
michael@0 251 // Bind a text16 value to the FIELD4 (column 4).
michael@0 252 result = Sqlite.bind_text16(sqlStmt, 5, "TADA", 8, destructor);
michael@0 253 do_check_eq(result, SQLITE_OK);
michael@0 254
michael@0 255 // Bind a blob value to the FIELD5 (column 5).
michael@0 256 result = Sqlite.bind_blob(sqlStmt, 6, ctypes.char.array()("BLOB"), 4,
michael@0 257 destructor);
michael@0 258 do_check_eq(result, SQLITE_OK);
michael@0 259 }
michael@0 260
michael@0 261 function onOpen(db) {
michael@0 262 createTableOnOpen(db);
michael@0 263 withQuery(db, "INSERT INTO TEST VALUES (?, ?, ?, ?, ?, ?);", SQLITE_DONE,
michael@0 264 onBind);
michael@0 265 withQuery(db, "SELECT * FROM TEST;", SQLITE_ROW, null, onSqlite3Step);
michael@0 266 }
michael@0 267
michael@0 268 withDB("open", [], onOpen);
michael@0 269 }
michael@0 270
michael@0 271 function run_test() {
michael@0 272 test_init();
michael@0 273 test_open_close();
michael@0 274 test_open_v2_close();
michael@0 275 test_create_table();
michael@0 276 test_insert_select();
michael@0 277 test_insert_bind_select();
michael@0 278 do_test_complete();
michael@0 279 }

mercurial