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 mozIStorageValueArray |
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 | "number REAL, nuller NULL, blobber BLOB"); |
michael@0 | 11 | |
michael@0 | 12 | var stmt = createStatement("INSERT INTO test (name, number, blobber) " + |
michael@0 | 13 | "VALUES (?1, ?2, ?3)"); |
michael@0 | 14 | stmt.bindByIndex(0, "foo"); |
michael@0 | 15 | stmt.bindByIndex(1, 2.34); |
michael@0 | 16 | stmt.bindBlobByIndex(2, [], 0); |
michael@0 | 17 | stmt.execute(); |
michael@0 | 18 | |
michael@0 | 19 | stmt.bindByIndex(0, ""); |
michael@0 | 20 | stmt.bindByIndex(1, 1.23); |
michael@0 | 21 | stmt.bindBlobByIndex(2, [1, 2], 2); |
michael@0 | 22 | stmt.execute(); |
michael@0 | 23 | |
michael@0 | 24 | stmt.reset(); |
michael@0 | 25 | stmt.finalize(); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | function test_getIsNull_for_null() |
michael@0 | 29 | { |
michael@0 | 30 | var stmt = createStatement("SELECT nuller, blobber FROM test WHERE id = ?1"); |
michael@0 | 31 | stmt.bindByIndex(0, 1); |
michael@0 | 32 | do_check_true(stmt.executeStep()); |
michael@0 | 33 | |
michael@0 | 34 | do_check_true(stmt.getIsNull(0)); // null field |
michael@0 | 35 | do_check_true(stmt.getIsNull(1)); // data is null if size is 0 |
michael@0 | 36 | stmt.reset(); |
michael@0 | 37 | stmt.finalize(); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | function test_getIsNull_for_non_null() |
michael@0 | 41 | { |
michael@0 | 42 | var stmt = createStatement("SELECT name, blobber FROM test WHERE id = ?1"); |
michael@0 | 43 | stmt.bindByIndex(0, 2); |
michael@0 | 44 | do_check_true(stmt.executeStep()); |
michael@0 | 45 | |
michael@0 | 46 | do_check_false(stmt.getIsNull(0)); |
michael@0 | 47 | do_check_false(stmt.getIsNull(1)); |
michael@0 | 48 | stmt.reset(); |
michael@0 | 49 | stmt.finalize(); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | function test_value_type_null() |
michael@0 | 53 | { |
michael@0 | 54 | var stmt = createStatement("SELECT nuller FROM test WHERE id = ?1"); |
michael@0 | 55 | stmt.bindByIndex(0, 1); |
michael@0 | 56 | do_check_true(stmt.executeStep()); |
michael@0 | 57 | |
michael@0 | 58 | do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_NULL, |
michael@0 | 59 | stmt.getTypeOfIndex(0)); |
michael@0 | 60 | stmt.reset(); |
michael@0 | 61 | stmt.finalize(); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | function test_value_type_integer() |
michael@0 | 65 | { |
michael@0 | 66 | var stmt = createStatement("SELECT id FROM test WHERE id = ?1"); |
michael@0 | 67 | stmt.bindByIndex(0, 1); |
michael@0 | 68 | do_check_true(stmt.executeStep()); |
michael@0 | 69 | |
michael@0 | 70 | do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_INTEGER, |
michael@0 | 71 | stmt.getTypeOfIndex(0)); |
michael@0 | 72 | stmt.reset(); |
michael@0 | 73 | stmt.finalize(); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | function test_value_type_float() |
michael@0 | 77 | { |
michael@0 | 78 | var stmt = createStatement("SELECT number FROM test WHERE id = ?1"); |
michael@0 | 79 | stmt.bindByIndex(0, 1); |
michael@0 | 80 | do_check_true(stmt.executeStep()); |
michael@0 | 81 | |
michael@0 | 82 | do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_FLOAT, |
michael@0 | 83 | stmt.getTypeOfIndex(0)); |
michael@0 | 84 | stmt.reset(); |
michael@0 | 85 | stmt.finalize(); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | function test_value_type_text() |
michael@0 | 89 | { |
michael@0 | 90 | var stmt = createStatement("SELECT name FROM test WHERE id = ?1"); |
michael@0 | 91 | stmt.bindByIndex(0, 1); |
michael@0 | 92 | do_check_true(stmt.executeStep()); |
michael@0 | 93 | |
michael@0 | 94 | do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_TEXT, |
michael@0 | 95 | stmt.getTypeOfIndex(0)); |
michael@0 | 96 | stmt.reset(); |
michael@0 | 97 | stmt.finalize(); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | function test_value_type_blob() |
michael@0 | 101 | { |
michael@0 | 102 | var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1"); |
michael@0 | 103 | stmt.bindByIndex(0, 2); |
michael@0 | 104 | do_check_true(stmt.executeStep()); |
michael@0 | 105 | |
michael@0 | 106 | do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_BLOB, |
michael@0 | 107 | stmt.getTypeOfIndex(0)); |
michael@0 | 108 | stmt.reset(); |
michael@0 | 109 | stmt.finalize(); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | function test_numEntries_one() |
michael@0 | 113 | { |
michael@0 | 114 | var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1"); |
michael@0 | 115 | stmt.bindByIndex(0, 2); |
michael@0 | 116 | do_check_true(stmt.executeStep()); |
michael@0 | 117 | |
michael@0 | 118 | do_check_eq(1, stmt.numEntries); |
michael@0 | 119 | stmt.reset(); |
michael@0 | 120 | stmt.finalize(); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | function test_numEntries_all() |
michael@0 | 124 | { |
michael@0 | 125 | var stmt = createStatement("SELECT * FROM test WHERE id = ?1"); |
michael@0 | 126 | stmt.bindByIndex(0, 2); |
michael@0 | 127 | do_check_true(stmt.executeStep()); |
michael@0 | 128 | |
michael@0 | 129 | do_check_eq(5, stmt.numEntries); |
michael@0 | 130 | stmt.reset(); |
michael@0 | 131 | stmt.finalize(); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | function test_getInt() |
michael@0 | 135 | { |
michael@0 | 136 | var stmt = createStatement("SELECT id FROM test WHERE id = ?1"); |
michael@0 | 137 | stmt.bindByIndex(0, 2); |
michael@0 | 138 | do_check_true(stmt.executeStep()); |
michael@0 | 139 | |
michael@0 | 140 | do_check_eq(2, stmt.getInt32(0)); |
michael@0 | 141 | do_check_eq(2, stmt.getInt64(0)); |
michael@0 | 142 | stmt.reset(); |
michael@0 | 143 | stmt.finalize(); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | function test_getDouble() |
michael@0 | 147 | { |
michael@0 | 148 | var stmt = createStatement("SELECT number FROM test WHERE id = ?1"); |
michael@0 | 149 | stmt.bindByIndex(0, 2); |
michael@0 | 150 | do_check_true(stmt.executeStep()); |
michael@0 | 151 | |
michael@0 | 152 | do_check_eq(1.23, stmt.getDouble(0)); |
michael@0 | 153 | stmt.reset(); |
michael@0 | 154 | stmt.finalize(); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | function test_getUTF8String() |
michael@0 | 158 | { |
michael@0 | 159 | var stmt = createStatement("SELECT name FROM test WHERE id = ?1"); |
michael@0 | 160 | stmt.bindByIndex(0, 1); |
michael@0 | 161 | do_check_true(stmt.executeStep()); |
michael@0 | 162 | |
michael@0 | 163 | do_check_eq("foo", stmt.getUTF8String(0)); |
michael@0 | 164 | stmt.reset(); |
michael@0 | 165 | stmt.finalize(); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | function test_getString() |
michael@0 | 169 | { |
michael@0 | 170 | var stmt = createStatement("SELECT name FROM test WHERE id = ?1"); |
michael@0 | 171 | stmt.bindByIndex(0, 2); |
michael@0 | 172 | do_check_true(stmt.executeStep()); |
michael@0 | 173 | |
michael@0 | 174 | do_check_eq("", stmt.getString(0)); |
michael@0 | 175 | stmt.reset(); |
michael@0 | 176 | stmt.finalize(); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | function test_getBlob() |
michael@0 | 180 | { |
michael@0 | 181 | var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1"); |
michael@0 | 182 | stmt.bindByIndex(0, 2); |
michael@0 | 183 | do_check_true(stmt.executeStep()); |
michael@0 | 184 | |
michael@0 | 185 | var count = { value: 0 }; |
michael@0 | 186 | var arr = { value: null }; |
michael@0 | 187 | stmt.getBlob(0, count, arr); |
michael@0 | 188 | do_check_eq(2, count.value); |
michael@0 | 189 | do_check_eq(1, arr.value[0]); |
michael@0 | 190 | do_check_eq(2, arr.value[1]); |
michael@0 | 191 | stmt.reset(); |
michael@0 | 192 | stmt.finalize(); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | var tests = [test_getIsNull_for_null, test_getIsNull_for_non_null, |
michael@0 | 196 | test_value_type_null, test_value_type_integer, |
michael@0 | 197 | test_value_type_float, test_value_type_text, test_value_type_blob, |
michael@0 | 198 | test_numEntries_one, test_numEntries_all, test_getInt, |
michael@0 | 199 | test_getDouble, test_getUTF8String, test_getString, test_getBlob]; |
michael@0 | 200 | |
michael@0 | 201 | function run_test() |
michael@0 | 202 | { |
michael@0 | 203 | setup(); |
michael@0 | 204 | |
michael@0 | 205 | for (var i = 0; i < tests.length; i++) |
michael@0 | 206 | tests[i](); |
michael@0 | 207 | |
michael@0 | 208 | cleanup(); |
michael@0 | 209 | } |
michael@0 | 210 |