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 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | vim:set ts=2 sw=2 sts=2 et: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | // This file tests the functions of mozIStorageStatementWrapper |
michael@0 | 8 | |
michael@0 | 9 | function setup() |
michael@0 | 10 | { |
michael@0 | 11 | getOpenedDatabase().createTable("test", "id INTEGER PRIMARY KEY, val NONE," + |
michael@0 | 12 | "alt_val NONE"); |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * A convenience wrapper for do_check_eq. Calls do_check_eq on aActualVal |
michael@0 | 17 | * and aReturnedVal, with one caveat. |
michael@0 | 18 | * |
michael@0 | 19 | * Date objects are converted before parameter binding to PRTime's (microsecs |
michael@0 | 20 | * since epoch). They are not reconverted when retrieved from the database. |
michael@0 | 21 | * This function abstracts away this reconversion so that you can pass in, |
michael@0 | 22 | * for example: |
michael@0 | 23 | * |
michael@0 | 24 | * checkVal(new Date(), aReturnedVal) // this |
michael@0 | 25 | * checkVal(new Date().valueOf() * 1000.0, aReturnedVal) // instead of this |
michael@0 | 26 | * |
michael@0 | 27 | * Should any other types require conversion in the future, their conversions |
michael@0 | 28 | * may also be abstracted away here. |
michael@0 | 29 | * |
michael@0 | 30 | * @param aActualVal |
michael@0 | 31 | * the value inserted into the database |
michael@0 | 32 | * @param aReturnedVal |
michael@0 | 33 | * the value retrieved from the database |
michael@0 | 34 | */ |
michael@0 | 35 | function checkVal(aActualVal, aReturnedVal) |
michael@0 | 36 | { |
michael@0 | 37 | if (aActualVal instanceof Date) aActualVal = aActualVal.valueOf() * 1000.0; |
michael@0 | 38 | do_check_eq(aActualVal, aReturnedVal); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | /** |
michael@0 | 42 | * Removes all rows from our test table. |
michael@0 | 43 | */ |
michael@0 | 44 | function clearTable() |
michael@0 | 45 | { |
michael@0 | 46 | var stmt = createStatement("DELETE FROM test"); |
michael@0 | 47 | stmt.execute(); |
michael@0 | 48 | stmt.finalize(); |
michael@0 | 49 | ensureNumRows(0); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | /** |
michael@0 | 53 | * Ensures that the number of rows in our test table is equal to aNumRows. |
michael@0 | 54 | * Calls do_check_eq on aNumRows and the value retrieved by SELECT'ing COUNT(*). |
michael@0 | 55 | * |
michael@0 | 56 | * @param aNumRows |
michael@0 | 57 | * the number of rows our test table should contain |
michael@0 | 58 | */ |
michael@0 | 59 | function ensureNumRows(aNumRows) |
michael@0 | 60 | { |
michael@0 | 61 | var stmt = createStatement("SELECT COUNT(*) AS number FROM test"); |
michael@0 | 62 | do_check_true(stmt.step()); |
michael@0 | 63 | do_check_eq(aNumRows, stmt.row.number); |
michael@0 | 64 | stmt.reset(); |
michael@0 | 65 | stmt.finalize(); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Inserts aVal into our test table and checks that insertion was successful by |
michael@0 | 70 | * retrieving the newly inserted value from the database and comparing it |
michael@0 | 71 | * against aVal. aVal is bound to a single parameter. |
michael@0 | 72 | * |
michael@0 | 73 | * @param aVal |
michael@0 | 74 | * value to insert into our test table and check |
michael@0 | 75 | */ |
michael@0 | 76 | function insertAndCheckSingleParam(aVal) |
michael@0 | 77 | { |
michael@0 | 78 | clearTable(); |
michael@0 | 79 | |
michael@0 | 80 | var stmt = createStatement("INSERT INTO test (val) VALUES (:val)"); |
michael@0 | 81 | stmt.params.val = aVal; |
michael@0 | 82 | stmt.execute(); |
michael@0 | 83 | stmt.finalize(); |
michael@0 | 84 | |
michael@0 | 85 | ensureNumRows(1); |
michael@0 | 86 | |
michael@0 | 87 | stmt = createStatement("SELECT val FROM test WHERE id = 1"); |
michael@0 | 88 | do_check_true(stmt.step()); |
michael@0 | 89 | checkVal(aVal, stmt.row.val); |
michael@0 | 90 | stmt.reset(); |
michael@0 | 91 | stmt.finalize(); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | /** |
michael@0 | 95 | * Inserts aVal into our test table and checks that insertion was successful by |
michael@0 | 96 | * retrieving the newly inserted value from the database and comparing it |
michael@0 | 97 | * against aVal. aVal is bound to two separate parameters, both of which are |
michael@0 | 98 | * checked against aVal. |
michael@0 | 99 | * |
michael@0 | 100 | * @param aVal |
michael@0 | 101 | * value to insert into our test table and check |
michael@0 | 102 | */ |
michael@0 | 103 | function insertAndCheckMultipleParams(aVal) |
michael@0 | 104 | { |
michael@0 | 105 | clearTable(); |
michael@0 | 106 | |
michael@0 | 107 | var stmt = createStatement("INSERT INTO test (val, alt_val) " + |
michael@0 | 108 | "VALUES (:val, :val)"); |
michael@0 | 109 | stmt.params.val = aVal; |
michael@0 | 110 | stmt.execute(); |
michael@0 | 111 | stmt.finalize(); |
michael@0 | 112 | |
michael@0 | 113 | ensureNumRows(1); |
michael@0 | 114 | |
michael@0 | 115 | stmt = createStatement("SELECT val, alt_val FROM test WHERE id = 1"); |
michael@0 | 116 | do_check_true(stmt.step()); |
michael@0 | 117 | checkVal(aVal, stmt.row.val); |
michael@0 | 118 | checkVal(aVal, stmt.row.alt_val); |
michael@0 | 119 | stmt.reset(); |
michael@0 | 120 | stmt.finalize(); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | /** |
michael@0 | 124 | * A convenience function that prints out a description of aVal using |
michael@0 | 125 | * aVal.toString and aVal.toSource. Output is useful when the test fails. |
michael@0 | 126 | * |
michael@0 | 127 | * @param aVal |
michael@0 | 128 | * a value inserted or to be inserted into our test table |
michael@0 | 129 | */ |
michael@0 | 130 | function printValDesc(aVal) |
michael@0 | 131 | { |
michael@0 | 132 | try |
michael@0 | 133 | { |
michael@0 | 134 | var toSource = aVal.toSource(); |
michael@0 | 135 | } |
michael@0 | 136 | catch (exc) |
michael@0 | 137 | { |
michael@0 | 138 | toSource = ""; |
michael@0 | 139 | } |
michael@0 | 140 | print("Testing value: toString=" + aVal + |
michael@0 | 141 | (toSource ? " toSource=" + toSource : "")); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | function run_test() |
michael@0 | 145 | { |
michael@0 | 146 | setup(); |
michael@0 | 147 | |
michael@0 | 148 | // function JSValStorageStatementBinder in |
michael@0 | 149 | // storage/src/mozStorageStatementParams.cpp tells us that the following types |
michael@0 | 150 | // and only the following types are valid as statement parameters: |
michael@0 | 151 | var vals = [ |
michael@0 | 152 | 1337, // int |
michael@0 | 153 | 3.1337, // double |
michael@0 | 154 | "foo", // string |
michael@0 | 155 | true, // boolean |
michael@0 | 156 | null, // null |
michael@0 | 157 | new Date(), // Date object |
michael@0 | 158 | ]; |
michael@0 | 159 | |
michael@0 | 160 | vals.forEach(function (val) |
michael@0 | 161 | { |
michael@0 | 162 | printValDesc(val); |
michael@0 | 163 | print("Single parameter"); |
michael@0 | 164 | insertAndCheckSingleParam(val); |
michael@0 | 165 | print("Multiple parameters"); |
michael@0 | 166 | insertAndCheckMultipleParams(val) |
michael@0 | 167 | }); |
michael@0 | 168 | |
michael@0 | 169 | cleanup(); |
michael@0 | 170 | } |