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 functions of mozIStorageStatement |
michael@0 | 6 | |
michael@0 | 7 | function setup() |
michael@0 | 8 | { |
michael@0 | 9 | getOpenedDatabase().createTable("test", "id INTEGER PRIMARY KEY, name TEXT"); |
michael@0 | 10 | } |
michael@0 | 11 | |
michael@0 | 12 | function test_parameterCount_none() |
michael@0 | 13 | { |
michael@0 | 14 | var stmt = createStatement("SELECT * FROM test"); |
michael@0 | 15 | do_check_eq(0, stmt.parameterCount); |
michael@0 | 16 | stmt.reset(); |
michael@0 | 17 | stmt.finalize(); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | function test_parameterCount_one() |
michael@0 | 21 | { |
michael@0 | 22 | var stmt = createStatement("SELECT * FROM test WHERE id = ?1"); |
michael@0 | 23 | do_check_eq(1, stmt.parameterCount); |
michael@0 | 24 | stmt.reset(); |
michael@0 | 25 | stmt.finalize(); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | function test_getParameterName() |
michael@0 | 29 | { |
michael@0 | 30 | var stmt = createStatement("SELECT * FROM test WHERE id = :id"); |
michael@0 | 31 | do_check_eq(":id", stmt.getParameterName(0)); |
michael@0 | 32 | stmt.reset(); |
michael@0 | 33 | stmt.finalize(); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | function test_getParameterIndex_different() |
michael@0 | 37 | { |
michael@0 | 38 | var stmt = createStatement("SELECT * FROM test WHERE id = :id OR name = :name"); |
michael@0 | 39 | do_check_eq(0, stmt.getParameterIndex("id")); |
michael@0 | 40 | do_check_eq(1, stmt.getParameterIndex("name")); |
michael@0 | 41 | stmt.reset(); |
michael@0 | 42 | stmt.finalize(); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | function test_getParameterIndex_same() |
michael@0 | 46 | { |
michael@0 | 47 | var stmt = createStatement("SELECT * FROM test WHERE id = :test OR name = :test"); |
michael@0 | 48 | do_check_eq(0, stmt.getParameterIndex("test")); |
michael@0 | 49 | stmt.reset(); |
michael@0 | 50 | stmt.finalize(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | function test_columnCount() |
michael@0 | 54 | { |
michael@0 | 55 | var stmt = createStatement("SELECT * FROM test WHERE id = ?1 OR name = ?2"); |
michael@0 | 56 | do_check_eq(2, stmt.columnCount); |
michael@0 | 57 | stmt.reset(); |
michael@0 | 58 | stmt.finalize(); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | function test_getColumnName() |
michael@0 | 62 | { |
michael@0 | 63 | var stmt = createStatement("SELECT name, id FROM test"); |
michael@0 | 64 | do_check_eq("id", stmt.getColumnName(1)); |
michael@0 | 65 | do_check_eq("name", stmt.getColumnName(0)); |
michael@0 | 66 | stmt.reset(); |
michael@0 | 67 | stmt.finalize(); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | function test_getColumnIndex_same_case() |
michael@0 | 71 | { |
michael@0 | 72 | var stmt = createStatement("SELECT name, id FROM test"); |
michael@0 | 73 | do_check_eq(0, stmt.getColumnIndex("name")); |
michael@0 | 74 | do_check_eq(1, stmt.getColumnIndex("id")); |
michael@0 | 75 | stmt.reset(); |
michael@0 | 76 | stmt.finalize(); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | function test_getColumnIndex_different_case() |
michael@0 | 80 | { |
michael@0 | 81 | var stmt = createStatement("SELECT name, id FROM test"); |
michael@0 | 82 | try { |
michael@0 | 83 | do_check_eq(0, stmt.getColumnIndex("NaMe")); |
michael@0 | 84 | do_throw("should not get here"); |
michael@0 | 85 | } catch (e) { |
michael@0 | 86 | do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result); |
michael@0 | 87 | } |
michael@0 | 88 | try { |
michael@0 | 89 | do_check_eq(1, stmt.getColumnIndex("Id")); |
michael@0 | 90 | do_throw("should not get here"); |
michael@0 | 91 | } catch (e) { |
michael@0 | 92 | do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result); |
michael@0 | 93 | } |
michael@0 | 94 | stmt.reset(); |
michael@0 | 95 | stmt.finalize(); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | function test_state_ready() |
michael@0 | 99 | { |
michael@0 | 100 | var stmt = createStatement("SELECT name, id FROM test"); |
michael@0 | 101 | do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state); |
michael@0 | 102 | stmt.reset(); |
michael@0 | 103 | stmt.finalize(); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | function test_state_executing() |
michael@0 | 107 | { |
michael@0 | 108 | var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')"); |
michael@0 | 109 | stmt.execute(); |
michael@0 | 110 | stmt.execute(); |
michael@0 | 111 | stmt.finalize(); |
michael@0 | 112 | |
michael@0 | 113 | stmt = createStatement("SELECT name, id FROM test"); |
michael@0 | 114 | stmt.executeStep(); |
michael@0 | 115 | do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING, |
michael@0 | 116 | stmt.state); |
michael@0 | 117 | stmt.executeStep(); |
michael@0 | 118 | do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING, |
michael@0 | 119 | stmt.state); |
michael@0 | 120 | stmt.reset(); |
michael@0 | 121 | do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state); |
michael@0 | 122 | stmt.finalize(); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | function test_state_after_finalize() |
michael@0 | 126 | { |
michael@0 | 127 | var stmt = createStatement("SELECT name, id FROM test"); |
michael@0 | 128 | stmt.executeStep(); |
michael@0 | 129 | stmt.finalize(); |
michael@0 | 130 | do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_INVALID, stmt.state); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | function test_getColumnDecltype() |
michael@0 | 134 | { |
michael@0 | 135 | var stmt = createStatement("SELECT name, id FROM test"); |
michael@0 | 136 | do_check_eq("TEXT", stmt.getColumnDecltype(0)); |
michael@0 | 137 | do_check_eq("INTEGER", stmt.getColumnDecltype(1)); |
michael@0 | 138 | try { |
michael@0 | 139 | do_check_eq("GARBAGE", stmt.getColumnDecltype(2)); |
michael@0 | 140 | do_throw("should not get here"); |
michael@0 | 141 | } catch (e) { |
michael@0 | 142 | do_check_eq(Cr.NS_ERROR_ILLEGAL_VALUE, e.result); |
michael@0 | 143 | } |
michael@0 | 144 | stmt.finalize(); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | function test_failed_execute() |
michael@0 | 148 | { |
michael@0 | 149 | var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')"); |
michael@0 | 150 | stmt.execute(); |
michael@0 | 151 | stmt.finalize(); |
michael@0 | 152 | var id = getOpenedDatabase().lastInsertRowID; |
michael@0 | 153 | stmt = createStatement("INSERT INTO test(id, name) VALUES(:id, 'bar')"); |
michael@0 | 154 | stmt.params.id = id; |
michael@0 | 155 | try { |
michael@0 | 156 | // Should throw a constraint error |
michael@0 | 157 | stmt.execute(); |
michael@0 | 158 | do_throw("Should have seen a constraint error"); |
michael@0 | 159 | } |
michael@0 | 160 | catch (e) { |
michael@0 | 161 | do_check_eq(getOpenedDatabase().lastError, Ci.mozIStorageError.CONSTRAINT); |
michael@0 | 162 | } |
michael@0 | 163 | do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state); |
michael@0 | 164 | // Should succeed without needing to reset the statement manually |
michael@0 | 165 | stmt.finalize(); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | var tests = [test_parameterCount_none, test_parameterCount_one, |
michael@0 | 169 | test_getParameterName, test_getParameterIndex_different, |
michael@0 | 170 | test_getParameterIndex_same, test_columnCount, |
michael@0 | 171 | test_getColumnName, test_getColumnIndex_same_case, |
michael@0 | 172 | test_getColumnIndex_different_case, test_state_ready, |
michael@0 | 173 | test_state_executing, test_state_after_finalize, |
michael@0 | 174 | test_getColumnDecltype, |
michael@0 | 175 | test_failed_execute, |
michael@0 | 176 | ]; |
michael@0 | 177 | |
michael@0 | 178 | function run_test() |
michael@0 | 179 | { |
michael@0 | 180 | setup(); |
michael@0 | 181 | |
michael@0 | 182 | for (var i = 0; i < tests.length; i++) |
michael@0 | 183 | tests[i](); |
michael@0 | 184 | |
michael@0 | 185 | cleanup(); |
michael@0 | 186 | } |
michael@0 | 187 |