storage/test/unit/test_storage_function.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 // This file tests the custom functions
     7 var testNums = [1, 2, 3, 4];
     9 function setup()
    10 {
    11   getOpenedDatabase().createTable("function_tests", "id INTEGER PRIMARY KEY");
    13   var stmt = createStatement("INSERT INTO function_tests (id) VALUES(?1)");
    14   for(var i = 0; i < testNums.length; ++i) {
    15     stmt.bindByIndex(0, testNums[i]);
    16     stmt.execute();
    17   }
    18   stmt.reset();
    19   stmt.finalize();
    20 }
    22 var testSquareFunction = {
    23   calls: 0,
    25   onFunctionCall: function(val) {
    26     ++this.calls;
    27     return val.getInt32(0) * val.getInt32(0);
    28   }
    29 };
    31 function test_function_registration()
    32 {
    33   var msc = getOpenedDatabase();
    34   msc.createFunction("test_square", 1, testSquareFunction);
    35 }
    37 function test_function_no_double_registration()
    38 {
    39   var msc = getOpenedDatabase();
    40   try {
    41     msc.createFunction("test_square", 2, testSquareFunction);
    42     do_throw("We shouldn't get here!");
    43   } catch (e) {
    44     do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
    45   }
    46 }
    48 function test_function_removal()
    49 {
    50   var msc = getOpenedDatabase();
    51   msc.removeFunction("test_square");
    52   // Should be Ok now
    53   msc.createFunction("test_square", 1, testSquareFunction);
    54 }
    56 function test_function_aliases()
    57 {
    58   var msc = getOpenedDatabase();
    59   msc.createFunction("test_square2", 1, testSquareFunction);
    60 }
    62 function test_function_call()
    63 {
    64   var stmt = createStatement("SELECT test_square(id) FROM function_tests");
    65   while(stmt.executeStep());
    66   do_check_eq(testNums.length, testSquareFunction.calls);
    67   testSquareFunction.calls = 0;
    68   stmt.finalize();
    69 }
    71 function test_function_result()
    72 {
    73   var stmt = createStatement("SELECT test_square(42) FROM function_tests");
    74   stmt.executeStep();
    75   do_check_eq(42*42, stmt.getInt32(0));
    76   testSquareFunction.calls = 0;
    77   stmt.finalize();
    78 }
    80 var tests = [test_function_registration, test_function_no_double_registration,
    81              test_function_removal, test_function_aliases, test_function_call,
    82              test_function_result];
    84 function run_test()
    85 {
    86   setup();
    88   for (var i = 0; i < tests.length; i++) {
    89     tests[i]();
    90   }
    92   cleanup();
    93 }

mercurial