storage/test/unit/test_connection_executeSimpleSQLAsync.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 /*
michael@0 6 * This file tests the functionality of
michael@0 7 * mozIStorageAsyncConnection::executeSimpleSQLAsync.
michael@0 8 */
michael@0 9
michael@0 10 const INTEGER = 1;
michael@0 11 const TEXT = "this is test text";
michael@0 12 const REAL = 3.23;
michael@0 13
michael@0 14 function asyncClose(db) {
michael@0 15 let deferred = Promise.defer();
michael@0 16 db.asyncClose(function(status) {
michael@0 17 if (Components.isSuccessCode(status)) {
michael@0 18 deferred.resolve();
michael@0 19 } else {
michael@0 20 deferred.reject(status);
michael@0 21 }
michael@0 22 });
michael@0 23 return deferred.promise;
michael@0 24 }
michael@0 25
michael@0 26 function openAsyncDatabase(file) {
michael@0 27 let deferred = Promise.defer();
michael@0 28 getService().openAsyncDatabase(file, null, function(status, db) {
michael@0 29 if (Components.isSuccessCode(status)) {
michael@0 30 deferred.resolve(db.QueryInterface(Ci.mozIStorageAsyncConnection));
michael@0 31 } else {
michael@0 32 deferred.reject(status);
michael@0 33 }
michael@0 34 });
michael@0 35 return deferred.promise;
michael@0 36 }
michael@0 37
michael@0 38 function executeSimpleSQLAsync(db, query, onResult) {
michael@0 39 let deferred = Promise.defer();
michael@0 40 db.executeSimpleSQLAsync(query, {
michael@0 41 handleError: function(error) {
michael@0 42 deferred.reject(error);
michael@0 43 },
michael@0 44 handleResult: function(result) {
michael@0 45 if (onResult) {
michael@0 46 onResult(result);
michael@0 47 } else {
michael@0 48 do_throw("No results were expected");
michael@0 49 }
michael@0 50 },
michael@0 51 handleCompletion: function(result) {
michael@0 52 deferred.resolve(result);
michael@0 53 }
michael@0 54 });
michael@0 55 return deferred.promise;
michael@0 56 }
michael@0 57
michael@0 58 add_task(function test_create_and_add() {
michael@0 59 let adb = yield openAsyncDatabase(getTestDB());
michael@0 60
michael@0 61 let completion = yield executeSimpleSQLAsync(adb,
michael@0 62 "CREATE TABLE test (id INTEGER, string TEXT, number REAL)");
michael@0 63
michael@0 64 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
michael@0 65
michael@0 66 completion = yield executeSimpleSQLAsync(adb,
michael@0 67 "INSERT INTO test (id, string, number) " +
michael@0 68 "VALUES (" + INTEGER + ", \"" + TEXT + "\", " + REAL + ")");
michael@0 69
michael@0 70 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
michael@0 71
michael@0 72 let result = null;
michael@0 73
michael@0 74 completion = yield executeSimpleSQLAsync(adb,
michael@0 75 "SELECT string, number FROM test WHERE id = 1",
michael@0 76 function(aResultSet) {
michael@0 77 result = aResultSet.getNextRow();
michael@0 78 do_check_eq(2, result.numEntries);
michael@0 79 do_check_eq(TEXT, result.getString(0));
michael@0 80 do_check_eq(REAL, result.getDouble(1));
michael@0 81 }
michael@0 82 );
michael@0 83
michael@0 84 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
michael@0 85 do_check_neq(result, null);
michael@0 86 result = null;
michael@0 87
michael@0 88 yield executeSimpleSQLAsync(adb, "SELECT COUNT(0) FROM test",
michael@0 89 function(aResultSet) {
michael@0 90 result = aResultSet.getNextRow();
michael@0 91 do_check_eq(1, result.getInt32(0));
michael@0 92 });
michael@0 93
michael@0 94 do_check_neq(result, null);
michael@0 95
michael@0 96 yield asyncClose(adb);
michael@0 97 });
michael@0 98
michael@0 99
michael@0 100 add_task(function test_asyncClose_does_not_complete_before_statement() {
michael@0 101 let adb = yield openAsyncDatabase(getTestDB());
michael@0 102 let executed = false;
michael@0 103
michael@0 104 let reason = yield executeSimpleSQLAsync(adb, "SELECT * FROM test",
michael@0 105 function(aResultSet) {
michael@0 106 let result = aResultSet.getNextRow();
michael@0 107
michael@0 108 do_check_neq(result, null);
michael@0 109 do_check_eq(3, result.numEntries);
michael@0 110 do_check_eq(INTEGER, result.getInt32(0));
michael@0 111 do_check_eq(TEXT, result.getString(1));
michael@0 112 do_check_eq(REAL, result.getDouble(2));
michael@0 113 executed = true;
michael@0 114 }
michael@0 115 );
michael@0 116
michael@0 117 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, reason);
michael@0 118
michael@0 119 // Ensure that the statement executed to completion.
michael@0 120 do_check_true(executed);
michael@0 121
michael@0 122 yield asyncClose(adb);
michael@0 123 });
michael@0 124
michael@0 125 ////////////////////////////////////////////////////////////////////////////////
michael@0 126 //// Test Runner
michael@0 127
michael@0 128 function run_test()
michael@0 129 {
michael@0 130 run_next_test();
michael@0 131 }

mercurial