storage/test/unit/test_storage_connection.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 // This file tests the functions of mozIStorageConnection
michael@0 6
michael@0 7 ////////////////////////////////////////////////////////////////////////////////
michael@0 8 //// Test Functions
michael@0 9
michael@0 10 function asyncClone(db, readOnly) {
michael@0 11 let deferred = Promise.defer();
michael@0 12 db.asyncClone(readOnly, function(status, db2) {
michael@0 13 if (Components.isSuccessCode(status)) {
michael@0 14 deferred.resolve(db2);
michael@0 15 } else {
michael@0 16 deferred.reject(status);
michael@0 17 }
michael@0 18 });
michael@0 19 return deferred.promise;
michael@0 20 }
michael@0 21
michael@0 22 function asyncClose(db) {
michael@0 23 let deferred = Promise.defer();
michael@0 24 db.asyncClose(function(status) {
michael@0 25 if (Components.isSuccessCode(status)) {
michael@0 26 deferred.resolve();
michael@0 27 } else {
michael@0 28 deferred.reject(status);
michael@0 29 }
michael@0 30 });
michael@0 31 return deferred.promise;
michael@0 32 }
michael@0 33
michael@0 34 function openAsyncDatabase(file, options) {
michael@0 35 let deferred = Promise.defer();
michael@0 36 let properties;
michael@0 37 if (options) {
michael@0 38 properties = Cc["@mozilla.org/hash-property-bag;1"].
michael@0 39 createInstance(Ci.nsIWritablePropertyBag);
michael@0 40 for (let k in options) {
michael@0 41 properties.setProperty(k, options[k]);
michael@0 42 }
michael@0 43 }
michael@0 44 getService().openAsyncDatabase(file, properties, function(status, db) {
michael@0 45 if (Components.isSuccessCode(status)) {
michael@0 46 deferred.resolve(db.QueryInterface(Ci.mozIStorageAsyncConnection));
michael@0 47 } else {
michael@0 48 deferred.reject(status);
michael@0 49 }
michael@0 50 });
michael@0 51 return deferred.promise;
michael@0 52 }
michael@0 53
michael@0 54 function executeAsync(statement, onResult) {
michael@0 55 let deferred = Promise.defer();
michael@0 56 statement.executeAsync({
michael@0 57 handleError: function(error) {
michael@0 58 deferred.reject(error);
michael@0 59 },
michael@0 60 handleResult: function(result) {
michael@0 61 if (onResult) {
michael@0 62 onResult(result);
michael@0 63 }
michael@0 64 },
michael@0 65 handleCompletion: function(result) {
michael@0 66 deferred.resolve(result);
michael@0 67 }
michael@0 68 });
michael@0 69 return deferred.promise;
michael@0 70 }
michael@0 71
michael@0 72 add_task(function test_connectionReady_open()
michael@0 73 {
michael@0 74 // there doesn't seem to be a way for the connection to not be ready (unless
michael@0 75 // we close it with mozIStorageConnection::Close(), but we don't for this).
michael@0 76 // It can only fail if GetPath fails on the database file, or if we run out
michael@0 77 // of memory trying to use an in-memory database
michael@0 78
michael@0 79 var msc = getOpenedDatabase();
michael@0 80 do_check_true(msc.connectionReady);
michael@0 81 });
michael@0 82
michael@0 83 add_task(function test_connectionReady_closed()
michael@0 84 {
michael@0 85 // This also tests mozIStorageConnection::Close()
michael@0 86
michael@0 87 var msc = getOpenedDatabase();
michael@0 88 msc.close();
michael@0 89 do_check_false(msc.connectionReady);
michael@0 90 gDBConn = null; // this is so later tests don't start to fail.
michael@0 91 });
michael@0 92
michael@0 93 add_task(function test_databaseFile()
michael@0 94 {
michael@0 95 var msc = getOpenedDatabase();
michael@0 96 do_check_true(getTestDB().equals(msc.databaseFile));
michael@0 97 });
michael@0 98
michael@0 99 add_task(function test_tableExists_not_created()
michael@0 100 {
michael@0 101 var msc = getOpenedDatabase();
michael@0 102 do_check_false(msc.tableExists("foo"));
michael@0 103 });
michael@0 104
michael@0 105 add_task(function test_indexExists_not_created()
michael@0 106 {
michael@0 107 var msc = getOpenedDatabase();
michael@0 108 do_check_false(msc.indexExists("foo"));
michael@0 109 });
michael@0 110
michael@0 111 add_task(function test_temp_tableExists_and_indexExists()
michael@0 112 {
michael@0 113 var msc = getOpenedDatabase();
michael@0 114 msc.executeSimpleSQL("CREATE TEMP TABLE test_temp(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
michael@0 115 do_check_true(msc.tableExists("test_temp"));
michael@0 116
michael@0 117 msc.executeSimpleSQL("CREATE INDEX test_temp_ind ON test_temp (name)");
michael@0 118 do_check_true(msc.indexExists("test_temp_ind"));
michael@0 119
michael@0 120 msc.executeSimpleSQL("DROP INDEX test_temp_ind");
michael@0 121 msc.executeSimpleSQL("DROP TABLE test_temp");
michael@0 122 });
michael@0 123
michael@0 124 add_task(function test_createTable_not_created()
michael@0 125 {
michael@0 126 var msc = getOpenedDatabase();
michael@0 127 msc.createTable("test", "id INTEGER PRIMARY KEY, name TEXT");
michael@0 128 do_check_true(msc.tableExists("test"));
michael@0 129 });
michael@0 130
michael@0 131 add_task(function test_indexExists_created()
michael@0 132 {
michael@0 133 var msc = getOpenedDatabase();
michael@0 134 msc.executeSimpleSQL("CREATE INDEX name_ind ON test (name)");
michael@0 135 do_check_true(msc.indexExists("name_ind"));
michael@0 136 });
michael@0 137
michael@0 138 add_task(function test_createTable_already_created()
michael@0 139 {
michael@0 140 var msc = getOpenedDatabase();
michael@0 141 do_check_true(msc.tableExists("test"));
michael@0 142 try {
michael@0 143 msc.createTable("test", "id INTEGER PRIMARY KEY, name TEXT");
michael@0 144 do_throw("We shouldn't get here!");
michael@0 145 } catch (e) {
michael@0 146 do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
michael@0 147 }
michael@0 148 });
michael@0 149
michael@0 150 add_task(function test_attach_createTable_tableExists_indexExists()
michael@0 151 {
michael@0 152 var msc = getOpenedDatabase();
michael@0 153 var file = do_get_file("storage_attach.sqlite", true);
michael@0 154 var msc2 = getDatabase(file);
michael@0 155 msc.executeSimpleSQL("ATTACH DATABASE '" + file.path + "' AS sample");
michael@0 156
michael@0 157 do_check_false(msc.tableExists("sample.test"));
michael@0 158 msc.createTable("sample.test", "id INTEGER PRIMARY KEY, name TEXT");
michael@0 159 do_check_true(msc.tableExists("sample.test"));
michael@0 160 try {
michael@0 161 msc.createTable("sample.test", "id INTEGER PRIMARY KEY, name TEXT");
michael@0 162 do_throw("We shouldn't get here!");
michael@0 163 } catch (e if e.result == Components.results.NS_ERROR_FAILURE) {
michael@0 164 // we expect to fail because this table should exist already.
michael@0 165 }
michael@0 166
michael@0 167 do_check_false(msc.indexExists("sample.test_ind"));
michael@0 168 msc.executeSimpleSQL("CREATE INDEX sample.test_ind ON test (name)");
michael@0 169 do_check_true(msc.indexExists("sample.test_ind"));
michael@0 170
michael@0 171 msc.executeSimpleSQL("DETACH DATABASE sample");
michael@0 172 msc2.close();
michael@0 173 try { file.remove(false); } catch(e) { }
michael@0 174 });
michael@0 175
michael@0 176 add_task(function test_lastInsertRowID()
michael@0 177 {
michael@0 178 var msc = getOpenedDatabase();
michael@0 179 msc.executeSimpleSQL("INSERT INTO test (name) VALUES ('foo')");
michael@0 180 do_check_eq(1, msc.lastInsertRowID);
michael@0 181 });
michael@0 182
michael@0 183 add_task(function test_transactionInProgress_no()
michael@0 184 {
michael@0 185 var msc = getOpenedDatabase();
michael@0 186 do_check_false(msc.transactionInProgress);
michael@0 187 });
michael@0 188
michael@0 189 add_task(function test_transactionInProgress_yes()
michael@0 190 {
michael@0 191 var msc = getOpenedDatabase();
michael@0 192 msc.beginTransaction();
michael@0 193 do_check_true(msc.transactionInProgress);
michael@0 194 msc.commitTransaction();
michael@0 195 do_check_false(msc.transactionInProgress);
michael@0 196
michael@0 197 msc.beginTransaction();
michael@0 198 do_check_true(msc.transactionInProgress);
michael@0 199 msc.rollbackTransaction();
michael@0 200 do_check_false(msc.transactionInProgress);
michael@0 201 });
michael@0 202
michael@0 203 add_task(function test_commitTransaction_no_transaction()
michael@0 204 {
michael@0 205 var msc = getOpenedDatabase();
michael@0 206 do_check_false(msc.transactionInProgress);
michael@0 207 try {
michael@0 208 msc.commitTransaction();
michael@0 209 do_throw("We should not get here!");
michael@0 210 } catch (e) {
michael@0 211 do_check_eq(Cr.NS_ERROR_UNEXPECTED, e.result);
michael@0 212 }
michael@0 213 });
michael@0 214
michael@0 215 add_task(function test_rollbackTransaction_no_transaction()
michael@0 216 {
michael@0 217 var msc = getOpenedDatabase();
michael@0 218 do_check_false(msc.transactionInProgress);
michael@0 219 try {
michael@0 220 msc.rollbackTransaction();
michael@0 221 do_throw("We should not get here!");
michael@0 222 } catch (e) {
michael@0 223 do_check_eq(Cr.NS_ERROR_UNEXPECTED, e.result);
michael@0 224 }
michael@0 225 });
michael@0 226
michael@0 227 add_task(function test_get_schemaVersion_not_set()
michael@0 228 {
michael@0 229 do_check_eq(0, getOpenedDatabase().schemaVersion);
michael@0 230 });
michael@0 231
michael@0 232 add_task(function test_set_schemaVersion()
michael@0 233 {
michael@0 234 var msc = getOpenedDatabase();
michael@0 235 const version = 1;
michael@0 236 msc.schemaVersion = version;
michael@0 237 do_check_eq(version, msc.schemaVersion);
michael@0 238 });
michael@0 239
michael@0 240 add_task(function test_set_schemaVersion_same()
michael@0 241 {
michael@0 242 var msc = getOpenedDatabase();
michael@0 243 const version = 1;
michael@0 244 msc.schemaVersion = version; // should still work ok
michael@0 245 do_check_eq(version, msc.schemaVersion);
michael@0 246 });
michael@0 247
michael@0 248 add_task(function test_set_schemaVersion_negative()
michael@0 249 {
michael@0 250 var msc = getOpenedDatabase();
michael@0 251 const version = -1;
michael@0 252 msc.schemaVersion = version;
michael@0 253 do_check_eq(version, msc.schemaVersion);
michael@0 254 });
michael@0 255
michael@0 256 add_task(function test_createTable(){
michael@0 257 var temp = getTestDB().parent;
michael@0 258 temp.append("test_db_table");
michael@0 259 try {
michael@0 260 var con = getService().openDatabase(temp);
michael@0 261 con.createTable("a","");
michael@0 262 } catch (e) {
michael@0 263 if (temp.exists()) try {
michael@0 264 temp.remove(false);
michael@0 265 } catch (e2) {}
michael@0 266 do_check_true(e.result==Cr.NS_ERROR_NOT_INITIALIZED ||
michael@0 267 e.result==Cr.NS_ERROR_FAILURE);
michael@0 268 } finally {
michael@0 269 if (con) {
michael@0 270 con.close();
michael@0 271 }
michael@0 272 }
michael@0 273 });
michael@0 274
michael@0 275 add_task(function test_defaultSynchronousAtNormal()
michael@0 276 {
michael@0 277 var msc = getOpenedDatabase();
michael@0 278 var stmt = createStatement("PRAGMA synchronous;");
michael@0 279 try {
michael@0 280 stmt.executeStep();
michael@0 281 do_check_eq(1, stmt.getInt32(0));
michael@0 282 }
michael@0 283 finally {
michael@0 284 stmt.reset();
michael@0 285 stmt.finalize();
michael@0 286 }
michael@0 287 });
michael@0 288
michael@0 289 // must be ran before executeAsync tests
michael@0 290 add_task(function test_close_does_not_spin_event_loop()
michael@0 291 {
michael@0 292 // We want to make sure that the event loop on the calling thread does not
michael@0 293 // spin when close is called.
michael@0 294 let event = {
michael@0 295 ran: false,
michael@0 296 run: function()
michael@0 297 {
michael@0 298 this.ran = true;
michael@0 299 },
michael@0 300 };
michael@0 301
michael@0 302 // Post the event before we call close, so it would run if the event loop was
michael@0 303 // spun during close.
michael@0 304 let thread = Cc["@mozilla.org/thread-manager;1"].
michael@0 305 getService(Ci.nsIThreadManager).
michael@0 306 currentThread;
michael@0 307 thread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
michael@0 308
michael@0 309 // Sanity check, then close the database. Afterwards, we should not have ran!
michael@0 310 do_check_false(event.ran);
michael@0 311 getOpenedDatabase().close();
michael@0 312 do_check_false(event.ran);
michael@0 313
michael@0 314 // Reset gDBConn so that later tests will get a new connection object.
michael@0 315 gDBConn = null;
michael@0 316 });
michael@0 317
michael@0 318 add_task(function test_asyncClose_succeeds_with_finalized_async_statement()
michael@0 319 {
michael@0 320 // XXX this test isn't perfect since we can't totally control when events will
michael@0 321 // run. If this paticular function fails randomly, it means we have a
michael@0 322 // real bug.
michael@0 323
michael@0 324 // We want to make sure we create a cached async statement to make sure that
michael@0 325 // when we finalize our statement, we end up finalizing the async one too so
michael@0 326 // close will succeed.
michael@0 327 let stmt = createStatement("SELECT * FROM test");
michael@0 328 stmt.executeAsync();
michael@0 329 stmt.finalize();
michael@0 330
michael@0 331 yield asyncClose(getOpenedDatabase());
michael@0 332 // Reset gDBConn so that later tests will get a new connection object.
michael@0 333 gDBConn = null;
michael@0 334 });
michael@0 335
michael@0 336 add_task(function test_close_then_release_statement() {
michael@0 337 // Testing the behavior in presence of a bad client that finalizes
michael@0 338 // statements after the database has been closed (typically by
michael@0 339 // letting the gc finalize the statement).
michael@0 340 let db = getOpenedDatabase();
michael@0 341 let stmt = createStatement("SELECT * FROM test -- test_close_then_release_statement");
michael@0 342 db.close();
michael@0 343 stmt.finalize(); // Finalize too late - this should not crash
michael@0 344
michael@0 345 // Reset gDBConn so that later tests will get a new connection object.
michael@0 346 gDBConn = null;
michael@0 347 });
michael@0 348
michael@0 349 add_task(function test_asyncClose_then_release_statement() {
michael@0 350 // Testing the behavior in presence of a bad client that finalizes
michael@0 351 // statements after the database has been async closed (typically by
michael@0 352 // letting the gc finalize the statement).
michael@0 353 let db = getOpenedDatabase();
michael@0 354 let stmt = createStatement("SELECT * FROM test -- test_asyncClose_then_release_statement");
michael@0 355 yield asyncClose(db);
michael@0 356 stmt.finalize(); // Finalize too late - this should not crash
michael@0 357
michael@0 358 // Reset gDBConn so that later tests will get a new connection object.
michael@0 359 gDBConn = null;
michael@0 360 });
michael@0 361
michael@0 362 add_task(function test_close_fails_with_async_statement_ran()
michael@0 363 {
michael@0 364 let deferred = Promise.defer();
michael@0 365 let stmt = createStatement("SELECT * FROM test");
michael@0 366 stmt.executeAsync();
michael@0 367 stmt.finalize();
michael@0 368
michael@0 369 let db = getOpenedDatabase();
michael@0 370 try {
michael@0 371 db.close();
michael@0 372 do_throw("should have thrown");
michael@0 373 }
michael@0 374 catch (e) {
michael@0 375 do_check_eq(e.result, Cr.NS_ERROR_UNEXPECTED);
michael@0 376 }
michael@0 377 finally {
michael@0 378 // Clean up after ourselves.
michael@0 379 db.asyncClose(function() {
michael@0 380 // Reset gDBConn so that later tests will get a new connection object.
michael@0 381 gDBConn = null;
michael@0 382 deferred.resolve();
michael@0 383 });
michael@0 384 }
michael@0 385 yield deferred.promise;
michael@0 386 });
michael@0 387
michael@0 388 add_task(function test_clone_optional_param()
michael@0 389 {
michael@0 390 let db1 = getService().openUnsharedDatabase(getTestDB());
michael@0 391 let db2 = db1.clone();
michael@0 392 do_check_true(db2.connectionReady);
michael@0 393
michael@0 394 // A write statement should not fail here.
michael@0 395 let stmt = db2.createStatement("INSERT INTO test (name) VALUES (:name)");
michael@0 396 stmt.params.name = "dwitte";
michael@0 397 stmt.execute();
michael@0 398 stmt.finalize();
michael@0 399
michael@0 400 // And a read statement should succeed.
michael@0 401 stmt = db2.createStatement("SELECT * FROM test");
michael@0 402 do_check_true(stmt.executeStep());
michael@0 403 stmt.finalize();
michael@0 404
michael@0 405 // Additionally check that it is a connection on the same database.
michael@0 406 do_check_true(db1.databaseFile.equals(db2.databaseFile));
michael@0 407
michael@0 408 db1.close();
michael@0 409 db2.close();
michael@0 410 });
michael@0 411
michael@0 412 function standardAsyncTest(promisedDB, name, shouldInit = false) {
michael@0 413 do_print("Performing standard async test " + name);
michael@0 414
michael@0 415 let adb = yield promisedDB;
michael@0 416 do_check_true(adb instanceof Ci.mozIStorageAsyncConnection);
michael@0 417 do_check_false(adb instanceof Ci.mozIStorageConnection);
michael@0 418
michael@0 419 if (shouldInit) {
michael@0 420 let stmt = adb.createAsyncStatement("CREATE TABLE test(name TEXT)");
michael@0 421 yield executeAsync(stmt);
michael@0 422 stmt.finalize();
michael@0 423 }
michael@0 424
michael@0 425 // Generate a name to insert and fetch back
michael@0 426 name = "worker bee " + Math.random() + " (" + name + ")";
michael@0 427
michael@0 428 let stmt = adb.createAsyncStatement("INSERT INTO test (name) VALUES (:name)");
michael@0 429 stmt.params.name = name;
michael@0 430 let result = yield executeAsync(stmt);
michael@0 431 do_print("Request complete");
michael@0 432 stmt.finalize();
michael@0 433 do_check_true(Components.isSuccessCode(result));
michael@0 434 do_print("Extracting data");
michael@0 435 stmt = adb.createAsyncStatement("SELECT * FROM test");
michael@0 436 let found = false;
michael@0 437 yield executeAsync(stmt, function(result) {
michael@0 438 do_print("Data has been extracted");
michael@0 439
michael@0 440 for (let row = result.getNextRow(); row != null; row = result.getNextRow()) {
michael@0 441 if (row.getResultByName("name") == name) {
michael@0 442 found = true;
michael@0 443 break;
michael@0 444 }
michael@0 445 }
michael@0 446 });
michael@0 447 do_check_true(found);
michael@0 448 stmt.finalize();
michael@0 449 yield asyncClose(adb);
michael@0 450
michael@0 451 do_print("Standard async test " + name + " complete");
michael@0 452 }
michael@0 453
michael@0 454 add_task(function test_open_async() {
michael@0 455 yield standardAsyncTest(openAsyncDatabase(getTestDB(), null), "default");
michael@0 456 yield standardAsyncTest(openAsyncDatabase(getTestDB()), "no optional arg");
michael@0 457 yield standardAsyncTest(openAsyncDatabase(getTestDB(),
michael@0 458 {shared: false, growthIncrement: 54}), "non-default options");
michael@0 459 yield standardAsyncTest(openAsyncDatabase("memory"),
michael@0 460 "in-memory database", true);
michael@0 461 yield standardAsyncTest(openAsyncDatabase("memory",
michael@0 462 {shared: false, growthIncrement: 54}),
michael@0 463 "in-memory database and options", true);
michael@0 464
michael@0 465 do_print("Testing async opening with bogus options 1");
michael@0 466 let raised = false;
michael@0 467 let adb = null;
michael@0 468 try {
michael@0 469 adb = yield openAsyncDatabase(getTestDB(), {shared: "forty-two"});
michael@0 470 } catch (ex) {
michael@0 471 raised = true;
michael@0 472 } finally {
michael@0 473 if (adb) {
michael@0 474 yield asyncClose(adb);
michael@0 475 }
michael@0 476 }
michael@0 477 do_check_true(raised);
michael@0 478
michael@0 479 do_print("Testing async opening with bogus options 2");
michael@0 480 raised = false;
michael@0 481 adb = null;
michael@0 482 try {
michael@0 483 adb = yield openAsyncDatabase(getTestDB(), {growthIncrement: "forty-two"});
michael@0 484 } catch (ex) {
michael@0 485 raised = true;
michael@0 486 } finally {
michael@0 487 if (adb) {
michael@0 488 yield asyncClose(adb);
michael@0 489 }
michael@0 490 }
michael@0 491 do_check_true(raised);
michael@0 492 });
michael@0 493
michael@0 494
michael@0 495 add_task(function test_async_open_with_shared_cache() {
michael@0 496 do_print("Testing that opening with a shared cache doesn't break stuff");
michael@0 497 let adb = yield openAsyncDatabase(getTestDB(), {shared: true});
michael@0 498
michael@0 499 let stmt = adb.createAsyncStatement("INSERT INTO test (name) VALUES (:name)");
michael@0 500 stmt.params.name = "clockworker";
michael@0 501 let result = yield executeAsync(stmt);
michael@0 502 do_print("Request complete");
michael@0 503 stmt.finalize();
michael@0 504 do_check_true(Components.isSuccessCode(result));
michael@0 505 do_print("Extracting data");
michael@0 506 stmt = adb.createAsyncStatement("SELECT * FROM test");
michael@0 507 let found = false;
michael@0 508 yield executeAsync(stmt, function(result) {
michael@0 509 do_print("Data has been extracted");
michael@0 510
michael@0 511 for (let row = result.getNextRow(); row != null; row = result.getNextRow()) {
michael@0 512 if (row.getResultByName("name") == "clockworker") {
michael@0 513 found = true;
michael@0 514 break;
michael@0 515 }
michael@0 516 }
michael@0 517 });
michael@0 518 do_check_true(found);
michael@0 519 stmt.finalize();
michael@0 520 yield asyncClose(adb);
michael@0 521 });
michael@0 522
michael@0 523 add_task(function test_clone_trivial_async()
michael@0 524 {
michael@0 525 let db1 = getService().openDatabase(getTestDB());
michael@0 526 do_print("Opened adb1");
michael@0 527 do_check_true(db1 instanceof Ci.mozIStorageAsyncConnection);
michael@0 528 let adb2 = yield asyncClone(db1, true);
michael@0 529 do_check_true(adb2 instanceof Ci.mozIStorageAsyncConnection);
michael@0 530 do_print("Cloned to adb2");
michael@0 531 db1.close();
michael@0 532 do_print("Closed db1");
michael@0 533 yield asyncClose(adb2);
michael@0 534 });
michael@0 535
michael@0 536 add_task(function test_clone_no_optional_param_async()
michael@0 537 {
michael@0 538 "use strict";
michael@0 539 do_print("Testing async cloning");
michael@0 540 let adb1 = yield openAsyncDatabase(getTestDB(), null);
michael@0 541 do_check_true(adb1 instanceof Ci.mozIStorageAsyncConnection);
michael@0 542
michael@0 543 do_print("Cloning database");
michael@0 544 do_check_true(Components.isSuccessCode(result));
michael@0 545
michael@0 546 let adb2 = yield asyncClone(adb1);
michael@0 547 do_print("Testing that the cloned db is a mozIStorageAsyncConnection " +
michael@0 548 "and not a mozIStorageConnection");
michael@0 549 do_check_true(adb2 instanceof Ci.mozIStorageAsyncConnection);
michael@0 550 do_check_false(adb2 instanceof Ci.mozIStorageConnection);
michael@0 551
michael@0 552 do_print("Inserting data into source db");
michael@0 553 let stmt = adb1.
michael@0 554 createAsyncStatement("INSERT INTO test (name) VALUES (:name)");
michael@0 555
michael@0 556 stmt.params.name = "yoric";
michael@0 557 let result = yield executeAsync(stmt);
michael@0 558 do_print("Request complete");
michael@0 559 stmt.finalize();
michael@0 560 do_check_true(Components.isSuccessCode(result));
michael@0 561 do_print("Extracting data from clone db");
michael@0 562 stmt = adb2.createAsyncStatement("SELECT * FROM test");
michael@0 563 let found = false;
michael@0 564 yield executeAsync(stmt, function(result) {
michael@0 565 do_print("Data has been extracted");
michael@0 566
michael@0 567 for (let row = result.getNextRow(); row != null; row = result.getNextRow()) {
michael@0 568 if (row.getResultByName("name") == "yoric") {
michael@0 569 found = true;
michael@0 570 break;
michael@0 571 }
michael@0 572 }
michael@0 573 });
michael@0 574 do_check_true(found);
michael@0 575 stmt.finalize();
michael@0 576 do_print("Closing databases");
michael@0 577 yield asyncClose(adb2);
michael@0 578 do_print("First db closed");
michael@0 579
michael@0 580 yield asyncClose(adb1);
michael@0 581 do_print("Second db closed");
michael@0 582 });
michael@0 583
michael@0 584 add_task(function test_clone_readonly()
michael@0 585 {
michael@0 586 let db1 = getService().openUnsharedDatabase(getTestDB());
michael@0 587 let db2 = db1.clone(true);
michael@0 588 do_check_true(db2.connectionReady);
michael@0 589
michael@0 590 // A write statement should fail here.
michael@0 591 let stmt = db2.createStatement("INSERT INTO test (name) VALUES (:name)");
michael@0 592 stmt.params.name = "reed";
michael@0 593 expectError(Cr.NS_ERROR_FILE_READ_ONLY, function() stmt.execute());
michael@0 594 stmt.finalize();
michael@0 595
michael@0 596 // And a read statement should succeed.
michael@0 597 stmt = db2.createStatement("SELECT * FROM test");
michael@0 598 do_check_true(stmt.executeStep());
michael@0 599 stmt.finalize();
michael@0 600
michael@0 601 db1.close();
michael@0 602 db2.close();
michael@0 603 });
michael@0 604
michael@0 605 add_task(function test_clone_shared_readonly()
michael@0 606 {
michael@0 607 let db1 = getService().openDatabase(getTestDB());
michael@0 608 let db2 = db1.clone(true);
michael@0 609 do_check_true(db2.connectionReady);
michael@0 610
michael@0 611 let stmt = db2.createStatement("INSERT INTO test (name) VALUES (:name)");
michael@0 612 stmt.params.name = "parker";
michael@0 613 // TODO currently SQLite does not actually work correctly here. The behavior
michael@0 614 // we want is commented out, and the current behavior is being tested
michael@0 615 // for. Our IDL comments will have to be updated when this starts to
michael@0 616 // work again.
michael@0 617 stmt.execute();
michael@0 618 // expectError(Components.results.NS_ERROR_FILE_READ_ONLY, function() stmt.execute());
michael@0 619 stmt.finalize();
michael@0 620
michael@0 621 // And a read statement should succeed.
michael@0 622 stmt = db2.createStatement("SELECT * FROM test");
michael@0 623 do_check_true(stmt.executeStep());
michael@0 624 stmt.finalize();
michael@0 625
michael@0 626 db1.close();
michael@0 627 db2.close();
michael@0 628 });
michael@0 629
michael@0 630 add_task(function test_close_clone_fails()
michael@0 631 {
michael@0 632 let calls = [
michael@0 633 "openDatabase",
michael@0 634 "openUnsharedDatabase",
michael@0 635 ];
michael@0 636 calls.forEach(function(methodName) {
michael@0 637 let db = getService()[methodName](getTestDB());
michael@0 638 db.close();
michael@0 639 expectError(Cr.NS_ERROR_NOT_INITIALIZED, function() db.clone());
michael@0 640 });
michael@0 641 });
michael@0 642
michael@0 643 add_task(function test_memory_clone_fails()
michael@0 644 {
michael@0 645 let db = getService().openSpecialDatabase("memory");
michael@0 646 db.close();
michael@0 647 expectError(Cr.NS_ERROR_NOT_INITIALIZED, function() db.clone());
michael@0 648 });
michael@0 649
michael@0 650 add_task(function test_clone_copies_functions()
michael@0 651 {
michael@0 652 const FUNC_NAME = "test_func";
michael@0 653 let calls = [
michael@0 654 "openDatabase",
michael@0 655 "openUnsharedDatabase",
michael@0 656 ];
michael@0 657 let functionMethods = [
michael@0 658 "createFunction",
michael@0 659 "createAggregateFunction",
michael@0 660 ];
michael@0 661 calls.forEach(function(methodName) {
michael@0 662 [true, false].forEach(function(readOnly) {
michael@0 663 functionMethods.forEach(function(functionMethod) {
michael@0 664 let db1 = getService()[methodName](getTestDB());
michael@0 665 // Create a function for db1.
michael@0 666 db1[functionMethod](FUNC_NAME, 1, {
michael@0 667 onFunctionCall: function() 0,
michael@0 668 onStep: function() 0,
michael@0 669 onFinal: function() 0,
michael@0 670 });
michael@0 671
michael@0 672 // Clone it, and make sure the function exists still.
michael@0 673 let db2 = db1.clone(readOnly);
michael@0 674 // Note: this would fail if the function did not exist.
michael@0 675 let stmt = db2.createStatement("SELECT " + FUNC_NAME + "(id) FROM test");
michael@0 676 stmt.finalize();
michael@0 677 db1.close();
michael@0 678 db2.close();
michael@0 679 });
michael@0 680 });
michael@0 681 });
michael@0 682 });
michael@0 683
michael@0 684 add_task(function test_clone_copies_overridden_functions()
michael@0 685 {
michael@0 686 const FUNC_NAME = "lower";
michael@0 687 function test_func() {
michael@0 688 this.called = false;
michael@0 689 }
michael@0 690 test_func.prototype = {
michael@0 691 onFunctionCall: function() {
michael@0 692 this.called = true;
michael@0 693 },
michael@0 694 onStep: function() {
michael@0 695 this.called = true;
michael@0 696 },
michael@0 697 onFinal: function() 0,
michael@0 698 };
michael@0 699
michael@0 700 let calls = [
michael@0 701 "openDatabase",
michael@0 702 "openUnsharedDatabase",
michael@0 703 ];
michael@0 704 let functionMethods = [
michael@0 705 "createFunction",
michael@0 706 "createAggregateFunction",
michael@0 707 ];
michael@0 708 calls.forEach(function(methodName) {
michael@0 709 [true, false].forEach(function(readOnly) {
michael@0 710 functionMethods.forEach(function(functionMethod) {
michael@0 711 let db1 = getService()[methodName](getTestDB());
michael@0 712 // Create a function for db1.
michael@0 713 let func = new test_func();
michael@0 714 db1[functionMethod](FUNC_NAME, 1, func);
michael@0 715 do_check_false(func.called);
michael@0 716
michael@0 717 // Clone it, and make sure the function gets called.
michael@0 718 let db2 = db1.clone(readOnly);
michael@0 719 let stmt = db2.createStatement("SELECT " + FUNC_NAME + "(id) FROM test");
michael@0 720 stmt.executeStep();
michael@0 721 do_check_true(func.called);
michael@0 722 stmt.finalize();
michael@0 723 db1.close();
michael@0 724 db2.close();
michael@0 725 });
michael@0 726 });
michael@0 727 });
michael@0 728 });
michael@0 729
michael@0 730 add_task(function test_clone_copies_pragmas()
michael@0 731 {
michael@0 732 const PRAGMAS = [
michael@0 733 { name: "cache_size", value: 500, copied: true },
michael@0 734 { name: "temp_store", value: 2, copied: true },
michael@0 735 { name: "foreign_keys", value: 1, copied: true },
michael@0 736 { name: "journal_size_limit", value: 524288, copied: true },
michael@0 737 { name: "synchronous", value: 2, copied: true },
michael@0 738 { name: "wal_autocheckpoint", value: 16, copied: true },
michael@0 739 { name: "ignore_check_constraints", value: 1, copied: false },
michael@0 740 ];
michael@0 741
michael@0 742 let db1 = getService().openUnsharedDatabase(getTestDB());
michael@0 743
michael@0 744 // Sanity check initial values are different from enforced ones.
michael@0 745 PRAGMAS.forEach(function (pragma) {
michael@0 746 let stmt = db1.createStatement("PRAGMA " + pragma.name);
michael@0 747 do_check_true(stmt.executeStep());
michael@0 748 do_check_neq(pragma.value, stmt.getInt32(0));
michael@0 749 stmt.finalize();
michael@0 750 });
michael@0 751 // Execute pragmas.
michael@0 752 PRAGMAS.forEach(function (pragma) {
michael@0 753 db1.executeSimpleSQL("PRAGMA " + pragma.name + " = " + pragma.value);
michael@0 754 });
michael@0 755
michael@0 756 let db2 = db1.clone();
michael@0 757 do_check_true(db2.connectionReady);
michael@0 758
michael@0 759 // Check cloned connection inherited pragma values.
michael@0 760 PRAGMAS.forEach(function (pragma) {
michael@0 761 let stmt = db2.createStatement("PRAGMA " + pragma.name);
michael@0 762 do_check_true(stmt.executeStep());
michael@0 763 let validate = pragma.copied ? do_check_eq : do_check_neq;
michael@0 764 validate(pragma.value, stmt.getInt32(0));
michael@0 765 stmt.finalize();
michael@0 766 });
michael@0 767
michael@0 768 db1.close();
michael@0 769 db2.close();
michael@0 770 });
michael@0 771
michael@0 772 add_task(function test_readonly_clone_copies_pragmas()
michael@0 773 {
michael@0 774 const PRAGMAS = [
michael@0 775 { name: "cache_size", value: 500, copied: true },
michael@0 776 { name: "temp_store", value: 2, copied: true },
michael@0 777 { name: "foreign_keys", value: 1, copied: false },
michael@0 778 { name: "journal_size_limit", value: 524288, copied: false },
michael@0 779 { name: "synchronous", value: 2, copied: false },
michael@0 780 { name: "wal_autocheckpoint", value: 16, copied: false },
michael@0 781 { name: "ignore_check_constraints", value: 1, copied: false },
michael@0 782 ];
michael@0 783
michael@0 784 let db1 = getService().openUnsharedDatabase(getTestDB());
michael@0 785
michael@0 786 // Sanity check initial values are different from enforced ones.
michael@0 787 PRAGMAS.forEach(function (pragma) {
michael@0 788 let stmt = db1.createStatement("PRAGMA " + pragma.name);
michael@0 789 do_check_true(stmt.executeStep());
michael@0 790 do_check_neq(pragma.value, stmt.getInt32(0));
michael@0 791 stmt.finalize();
michael@0 792 });
michael@0 793 // Execute pragmas.
michael@0 794 PRAGMAS.forEach(function (pragma) {
michael@0 795 db1.executeSimpleSQL("PRAGMA " + pragma.name + " = " + pragma.value);
michael@0 796 });
michael@0 797
michael@0 798 let db2 = db1.clone(true);
michael@0 799 do_check_true(db2.connectionReady);
michael@0 800
michael@0 801 // Check cloned connection inherited pragma values.
michael@0 802 PRAGMAS.forEach(function (pragma) {
michael@0 803 let stmt = db2.createStatement("PRAGMA " + pragma.name);
michael@0 804 do_check_true(stmt.executeStep());
michael@0 805 let validate = pragma.copied ? do_check_eq : do_check_neq;
michael@0 806 validate(pragma.value, stmt.getInt32(0));
michael@0 807 stmt.finalize();
michael@0 808 });
michael@0 809
michael@0 810 db1.close();
michael@0 811 db2.close();
michael@0 812 });
michael@0 813
michael@0 814 add_task(function test_getInterface()
michael@0 815 {
michael@0 816 let db = getOpenedDatabase();
michael@0 817 let target = db.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 818 .getInterface(Ci.nsIEventTarget);
michael@0 819 // Just check that target is non-null. Other tests will ensure that it has
michael@0 820 // the correct value.
michael@0 821 do_check_true(target != null);
michael@0 822
michael@0 823 yield asyncClose(db);
michael@0 824 gDBConn = null;
michael@0 825 });
michael@0 826
michael@0 827
michael@0 828 function run_test()
michael@0 829 {
michael@0 830 run_next_test();
michael@0 831 }

mercurial