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.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 /*
     6  * This file tests the functionality of
     7  * mozIStorageAsyncConnection::executeSimpleSQLAsync.
     8  */
    10 const INTEGER = 1;
    11 const TEXT = "this is test text";
    12 const REAL = 3.23;
    14 function asyncClose(db) {
    15   let deferred = Promise.defer();
    16   db.asyncClose(function(status) {
    17     if (Components.isSuccessCode(status)) {
    18       deferred.resolve();
    19     } else {
    20       deferred.reject(status);
    21     }
    22   });
    23   return deferred.promise;
    24 }
    26 function openAsyncDatabase(file) {
    27   let deferred = Promise.defer();
    28   getService().openAsyncDatabase(file, null, function(status, db) {
    29     if (Components.isSuccessCode(status)) {
    30       deferred.resolve(db.QueryInterface(Ci.mozIStorageAsyncConnection));
    31     } else {
    32       deferred.reject(status);
    33     }
    34   });
    35   return deferred.promise;
    36 }
    38 function executeSimpleSQLAsync(db, query, onResult) {
    39   let deferred = Promise.defer();
    40   db.executeSimpleSQLAsync(query, {
    41     handleError: function(error) {
    42       deferred.reject(error);
    43     },
    44     handleResult: function(result) {
    45       if (onResult) {
    46         onResult(result);
    47       } else {
    48         do_throw("No results were expected");
    49       }
    50     },
    51     handleCompletion: function(result) {
    52       deferred.resolve(result);
    53     }
    54   });
    55   return deferred.promise;
    56 }
    58 add_task(function test_create_and_add() {
    59   let adb = yield openAsyncDatabase(getTestDB());
    61   let completion = yield executeSimpleSQLAsync(adb,
    62     "CREATE TABLE test (id INTEGER, string TEXT, number REAL)");
    64   do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
    66   completion = yield executeSimpleSQLAsync(adb,
    67     "INSERT INTO test (id, string, number) " +
    68     "VALUES (" + INTEGER + ", \"" + TEXT + "\", " + REAL + ")");
    70   do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
    72   let result = null;
    74   completion = yield executeSimpleSQLAsync(adb,
    75     "SELECT string, number FROM test WHERE id = 1",
    76     function(aResultSet) {
    77       result = aResultSet.getNextRow();
    78       do_check_eq(2, result.numEntries);
    79       do_check_eq(TEXT, result.getString(0));
    80       do_check_eq(REAL, result.getDouble(1));
    81     }
    82   );
    84   do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, completion);
    85   do_check_neq(result, null);
    86   result = null;
    88   yield executeSimpleSQLAsync(adb, "SELECT COUNT(0) FROM test",
    89     function(aResultSet) {
    90       result = aResultSet.getNextRow();
    91       do_check_eq(1, result.getInt32(0));
    92     });
    94   do_check_neq(result, null);
    96   yield asyncClose(adb);
    97 });
   100 add_task(function test_asyncClose_does_not_complete_before_statement() {
   101   let adb = yield openAsyncDatabase(getTestDB());
   102   let executed = false;
   104   let reason = yield executeSimpleSQLAsync(adb, "SELECT * FROM test",
   105     function(aResultSet) {
   106       let result = aResultSet.getNextRow();
   108       do_check_neq(result, null);
   109       do_check_eq(3, result.numEntries);
   110       do_check_eq(INTEGER, result.getInt32(0));
   111       do_check_eq(TEXT, result.getString(1));
   112       do_check_eq(REAL, result.getDouble(2));
   113       executed = true;
   114     }
   115   );
   117   do_check_eq(Ci.mozIStorageStatementCallback.REASON_FINISHED, reason);
   119   // Ensure that the statement executed to completion.
   120   do_check_true(executed);
   122   yield asyncClose(adb);
   123 });
   125 ////////////////////////////////////////////////////////////////////////////////
   126 //// Test Runner
   128 function run_test()
   129 {
   130   run_next_test();
   131 }

mercurial