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