storage/test/unit/test_connection_executeAsync.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 mozIStorageConnection::executeAsync for
michael@0 7 * both mozIStorageStatement and mozIStorageAsyncStatement.
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 const BLOB = [1, 2];
michael@0 14
michael@0 15 function test_create_and_add()
michael@0 16 {
michael@0 17 getOpenedDatabase().executeSimpleSQL(
michael@0 18 "CREATE TABLE test (" +
michael@0 19 "id INTEGER, " +
michael@0 20 "string TEXT, " +
michael@0 21 "number REAL, " +
michael@0 22 "nuller NULL, " +
michael@0 23 "blober BLOB" +
michael@0 24 ")"
michael@0 25 );
michael@0 26
michael@0 27 let stmts = [];
michael@0 28 stmts[0] = getOpenedDatabase().createStatement(
michael@0 29 "INSERT INTO test (id, string, number, nuller, blober) VALUES (?, ?, ?, ?, ?)"
michael@0 30 );
michael@0 31 stmts[0].bindByIndex(0, INTEGER);
michael@0 32 stmts[0].bindByIndex(1, TEXT);
michael@0 33 stmts[0].bindByIndex(2, REAL);
michael@0 34 stmts[0].bindByIndex(3, null);
michael@0 35 stmts[0].bindBlobByIndex(4, BLOB, BLOB.length);
michael@0 36 stmts[1] = getOpenedDatabase().createAsyncStatement(
michael@0 37 "INSERT INTO test (string, number, nuller, blober) VALUES (?, ?, ?, ?)"
michael@0 38 );
michael@0 39 stmts[1].bindByIndex(0, TEXT);
michael@0 40 stmts[1].bindByIndex(1, REAL);
michael@0 41 stmts[1].bindByIndex(2, null);
michael@0 42 stmts[1].bindBlobByIndex(3, BLOB, BLOB.length);
michael@0 43
michael@0 44 getOpenedDatabase().executeAsync(stmts, stmts.length, {
michael@0 45 handleResult: function(aResultSet)
michael@0 46 {
michael@0 47 dump("handleResult("+aResultSet+")\n");
michael@0 48 do_throw("unexpected results obtained!");
michael@0 49 },
michael@0 50 handleError: function(aError)
michael@0 51 {
michael@0 52 dump("handleError("+aError.result+")\n");
michael@0 53 do_throw("unexpected error!");
michael@0 54 },
michael@0 55 handleCompletion: function(aReason)
michael@0 56 {
michael@0 57 dump("handleCompletion("+aReason+")\n");
michael@0 58 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, aReason);
michael@0 59
michael@0 60 // Check that the result is in the table
michael@0 61 let stmt = getOpenedDatabase().createStatement(
michael@0 62 "SELECT string, number, nuller, blober FROM test WHERE id = ?"
michael@0 63 );
michael@0 64 stmt.bindByIndex(0, INTEGER);
michael@0 65 try {
michael@0 66 do_check_true(stmt.executeStep());
michael@0 67 do_check_eq(TEXT, stmt.getString(0));
michael@0 68 do_check_eq(REAL, stmt.getDouble(1));
michael@0 69 do_check_true(stmt.getIsNull(2));
michael@0 70 let count = { value: 0 };
michael@0 71 let blob = { value: null };
michael@0 72 stmt.getBlob(3, count, blob);
michael@0 73 do_check_eq(BLOB.length, count.value);
michael@0 74 for (let i = 0; i < BLOB.length; i++)
michael@0 75 do_check_eq(BLOB[i], blob.value[i]);
michael@0 76 }
michael@0 77 finally {
michael@0 78 stmt.finalize();
michael@0 79 }
michael@0 80
michael@0 81 // Make sure we have two rows in the table
michael@0 82 stmt = getOpenedDatabase().createStatement(
michael@0 83 "SELECT COUNT(1) FROM test"
michael@0 84 );
michael@0 85 try {
michael@0 86 do_check_true(stmt.executeStep());
michael@0 87 do_check_eq(2, stmt.getInt32(0));
michael@0 88 }
michael@0 89 finally {
michael@0 90 stmt.finalize();
michael@0 91 }
michael@0 92
michael@0 93 // Run the next test.
michael@0 94 run_next_test();
michael@0 95 }
michael@0 96 });
michael@0 97 stmts[0].finalize();
michael@0 98 stmts[1].finalize();
michael@0 99 }
michael@0 100
michael@0 101 function test_multiple_bindings_on_statements()
michael@0 102 {
michael@0 103 // This tests to make sure that we pass all the statements multiply bound
michael@0 104 // parameters when we call executeAsync.
michael@0 105 const AMOUNT_TO_ADD = 5;
michael@0 106 const ITERATIONS = 5;
michael@0 107
michael@0 108 let stmts = [];
michael@0 109 let db = getOpenedDatabase();
michael@0 110 let sqlString = "INSERT INTO test (id, string, number, nuller, blober) " +
michael@0 111 "VALUES (:int, :text, :real, :null, :blob)";
michael@0 112 // We run the same statement twice, and should insert 2 * AMOUNT_TO_ADD.
michael@0 113 for (let i = 0; i < ITERATIONS; i++) {
michael@0 114 // alternate the type of statement we create
michael@0 115 if (i % 2)
michael@0 116 stmts[i] = db.createStatement(sqlString);
michael@0 117 else
michael@0 118 stmts[i] = db.createAsyncStatement(sqlString);
michael@0 119
michael@0 120 let params = stmts[i].newBindingParamsArray();
michael@0 121 for (let j = 0; j < AMOUNT_TO_ADD; j++) {
michael@0 122 let bp = params.newBindingParams();
michael@0 123 bp.bindByName("int", INTEGER);
michael@0 124 bp.bindByName("text", TEXT);
michael@0 125 bp.bindByName("real", REAL);
michael@0 126 bp.bindByName("null", null);
michael@0 127 bp.bindBlobByName("blob", BLOB, BLOB.length);
michael@0 128 params.addParams(bp);
michael@0 129 }
michael@0 130 stmts[i].bindParameters(params);
michael@0 131 }
michael@0 132
michael@0 133 // Get our current number of rows in the table.
michael@0 134 let currentRows = 0;
michael@0 135 let countStmt = getOpenedDatabase().createStatement(
michael@0 136 "SELECT COUNT(1) AS count FROM test"
michael@0 137 );
michael@0 138 try {
michael@0 139 do_check_true(countStmt.executeStep());
michael@0 140 currentRows = countStmt.row.count;
michael@0 141 }
michael@0 142 finally {
michael@0 143 countStmt.reset();
michael@0 144 }
michael@0 145
michael@0 146 // Execute asynchronously.
michael@0 147 getOpenedDatabase().executeAsync(stmts, stmts.length, {
michael@0 148 handleResult: function(aResultSet)
michael@0 149 {
michael@0 150 do_throw("Unexpected call to handleResult!");
michael@0 151 },
michael@0 152 handleError: function(aError)
michael@0 153 {
michael@0 154 print("Error code " + aError.result + " with message '" +
michael@0 155 aError.message + "' returned.");
michael@0 156 do_throw("Unexpected error!");
michael@0 157 },
michael@0 158 handleCompletion: function(aReason)
michael@0 159 {
michael@0 160 print("handleCompletion(" + aReason +
michael@0 161 ") for test_multiple_bindings_on_statements");
michael@0 162 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, aReason);
michael@0 163
michael@0 164 // Check to make sure we added all of our rows.
michael@0 165 try {
michael@0 166 do_check_true(countStmt.executeStep());
michael@0 167 do_check_eq(currentRows + (ITERATIONS * AMOUNT_TO_ADD),
michael@0 168 countStmt.row.count);
michael@0 169 }
michael@0 170 finally {
michael@0 171 countStmt.finalize();
michael@0 172 }
michael@0 173
michael@0 174 // Run the next test.
michael@0 175 run_next_test();
michael@0 176 }
michael@0 177 });
michael@0 178 stmts.forEach(function(stmt) stmt.finalize());
michael@0 179 }
michael@0 180
michael@0 181 function test_asyncClose_does_not_complete_before_statements()
michael@0 182 {
michael@0 183 let stmt = createStatement("SELECT * FROM sqlite_master");
michael@0 184 let executed = false;
michael@0 185 stmt.executeAsync({
michael@0 186 handleResult: function(aResultSet)
michael@0 187 {
michael@0 188 },
michael@0 189 handleError: function(aError)
michael@0 190 {
michael@0 191 print("Error code " + aError.result + " with message '" +
michael@0 192 aError.message + "' returned.");
michael@0 193 do_throw("Unexpected error!");
michael@0 194 },
michael@0 195 handleCompletion: function(aReason)
michael@0 196 {
michael@0 197 print("handleCompletion(" + aReason +
michael@0 198 ") for test_asyncClose_does_not_complete_before_statements");
michael@0 199 do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, aReason);
michael@0 200 executed = true;
michael@0 201 }
michael@0 202 });
michael@0 203 stmt.finalize();
michael@0 204
michael@0 205 getOpenedDatabase().asyncClose(function() {
michael@0 206 // Ensure that the statement executed to completion.
michael@0 207 do_check_true(executed);
michael@0 208
michael@0 209 // Reset gDBConn so that later tests will get a new connection object.
michael@0 210 gDBConn = null;
michael@0 211 run_next_test();
michael@0 212 });
michael@0 213 }
michael@0 214
michael@0 215 function test_asyncClose_does_not_throw_no_callback()
michael@0 216 {
michael@0 217 getOpenedDatabase().asyncClose();
michael@0 218
michael@0 219 // Reset gDBConn so that later tests will get a new connection object.
michael@0 220 gDBConn = null;
michael@0 221 run_next_test();
michael@0 222 }
michael@0 223
michael@0 224 function test_double_asyncClose_throws()
michael@0 225 {
michael@0 226 let conn = getOpenedDatabase();
michael@0 227 conn.asyncClose();
michael@0 228 try {
michael@0 229 conn.asyncClose();
michael@0 230 do_throw("should have thrown");
michael@0 231 // There is a small race condition here, which can cause either of
michael@0 232 // Cr.NS_ERROR_NOT_INITIALIZED or Cr.NS_ERROR_UNEXPECTED to be thrown.
michael@0 233 } catch (e if "result" in e && e.result == Cr.NS_ERROR_NOT_INITIALIZED) {
michael@0 234 do_print("NS_ERROR_NOT_INITIALIZED");
michael@0 235 } catch (e if "result" in e && e.result == Cr.NS_ERROR_UNEXPECTED) {
michael@0 236 do_print("NS_ERROR_UNEXPECTED");
michael@0 237 } catch (e) {
michael@0 238 }
michael@0 239
michael@0 240 // Reset gDBConn so that later tests will get a new connection object.
michael@0 241 gDBConn = null;
michael@0 242 run_next_test();
michael@0 243 }
michael@0 244
michael@0 245 ////////////////////////////////////////////////////////////////////////////////
michael@0 246 //// Test Runner
michael@0 247
michael@0 248 [
michael@0 249 test_create_and_add,
michael@0 250 test_multiple_bindings_on_statements,
michael@0 251 test_asyncClose_does_not_complete_before_statements,
michael@0 252 test_asyncClose_does_not_throw_no_callback,
michael@0 253 test_double_asyncClose_throws,
michael@0 254 ].forEach(add_test);
michael@0 255
michael@0 256 function run_test()
michael@0 257 {
michael@0 258 cleanup();
michael@0 259 run_next_test();
michael@0 260 }

mercurial