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 mozIStorageService except for |
michael@0 | 6 | // openUnsharedDatabase, which is tested by test_storage_service_unshared.js. |
michael@0 | 7 | |
michael@0 | 8 | const BACKUP_FILE_NAME = "test_storage.sqlite.backup"; |
michael@0 | 9 | |
michael@0 | 10 | function test_openSpecialDatabase_invalid_arg() |
michael@0 | 11 | { |
michael@0 | 12 | try { |
michael@0 | 13 | getService().openSpecialDatabase("abcd"); |
michael@0 | 14 | do_throw("We should not get here!"); |
michael@0 | 15 | } catch (e) { |
michael@0 | 16 | print(e); |
michael@0 | 17 | print("e.result is " + e.result); |
michael@0 | 18 | do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result); |
michael@0 | 19 | } |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | function test_openDatabase_null_file() |
michael@0 | 23 | { |
michael@0 | 24 | try { |
michael@0 | 25 | getService().openDatabase(null); |
michael@0 | 26 | do_throw("We should not get here!"); |
michael@0 | 27 | } catch (e) { |
michael@0 | 28 | print(e); |
michael@0 | 29 | print("e.result is " + e.result); |
michael@0 | 30 | do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result); |
michael@0 | 31 | } |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | function test_openUnsharedDatabase_null_file() |
michael@0 | 35 | { |
michael@0 | 36 | try { |
michael@0 | 37 | getService().openUnsharedDatabase(null); |
michael@0 | 38 | do_throw("We should not get here!"); |
michael@0 | 39 | } catch (e) { |
michael@0 | 40 | print(e); |
michael@0 | 41 | print("e.result is " + e.result); |
michael@0 | 42 | do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result); |
michael@0 | 43 | } |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | function test_openDatabase_file_DNE() |
michael@0 | 47 | { |
michael@0 | 48 | // the file should be created after calling |
michael@0 | 49 | var db = getTestDB(); |
michael@0 | 50 | do_check_false(db.exists()); |
michael@0 | 51 | getService().openDatabase(db); |
michael@0 | 52 | do_check_true(db.exists()); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | function test_openDatabase_file_exists() |
michael@0 | 56 | { |
michael@0 | 57 | // it should already exist from our last test |
michael@0 | 58 | var db = getTestDB(); |
michael@0 | 59 | do_check_true(db.exists()); |
michael@0 | 60 | getService().openDatabase(db); |
michael@0 | 61 | do_check_true(db.exists()); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | function test_corrupt_db_throws_with_openDatabase() |
michael@0 | 65 | { |
michael@0 | 66 | try { |
michael@0 | 67 | getDatabase(getCorruptDB()); |
michael@0 | 68 | do_throw("should not be here"); |
michael@0 | 69 | } |
michael@0 | 70 | catch (e) { |
michael@0 | 71 | do_check_eq(Cr.NS_ERROR_FILE_CORRUPTED, e.result); |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | function test_fake_db_throws_with_openDatabase() |
michael@0 | 76 | { |
michael@0 | 77 | try { |
michael@0 | 78 | getDatabase(getFakeDB()); |
michael@0 | 79 | do_throw("should not be here"); |
michael@0 | 80 | } |
michael@0 | 81 | catch (e) { |
michael@0 | 82 | do_check_eq(Cr.NS_ERROR_FILE_CORRUPTED, e.result); |
michael@0 | 83 | } |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | function test_backup_not_new_filename() |
michael@0 | 87 | { |
michael@0 | 88 | const fname = getTestDB().leafName; |
michael@0 | 89 | |
michael@0 | 90 | var backup = getService().backupDatabaseFile(getTestDB(), fname); |
michael@0 | 91 | do_check_neq(fname, backup.leafName); |
michael@0 | 92 | |
michael@0 | 93 | backup.remove(false); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | function test_backup_new_filename() |
michael@0 | 97 | { |
michael@0 | 98 | var backup = getService().backupDatabaseFile(getTestDB(), BACKUP_FILE_NAME); |
michael@0 | 99 | do_check_eq(BACKUP_FILE_NAME, backup.leafName); |
michael@0 | 100 | |
michael@0 | 101 | backup.remove(false); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | function test_backup_new_folder() |
michael@0 | 105 | { |
michael@0 | 106 | var parentDir = getTestDB().parent; |
michael@0 | 107 | parentDir.append("test_storage_temp"); |
michael@0 | 108 | if (parentDir.exists()) |
michael@0 | 109 | parentDir.remove(true); |
michael@0 | 110 | parentDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); |
michael@0 | 111 | do_check_true(parentDir.exists()); |
michael@0 | 112 | |
michael@0 | 113 | var backup = getService().backupDatabaseFile(getTestDB(), BACKUP_FILE_NAME, |
michael@0 | 114 | parentDir); |
michael@0 | 115 | do_check_eq(BACKUP_FILE_NAME, backup.leafName); |
michael@0 | 116 | do_check_true(parentDir.equals(backup.parent)); |
michael@0 | 117 | |
michael@0 | 118 | parentDir.remove(true); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | var tests = [ |
michael@0 | 122 | test_openSpecialDatabase_invalid_arg, |
michael@0 | 123 | test_openDatabase_null_file, |
michael@0 | 124 | test_openUnsharedDatabase_null_file, |
michael@0 | 125 | test_openDatabase_file_DNE, |
michael@0 | 126 | test_openDatabase_file_exists, |
michael@0 | 127 | test_corrupt_db_throws_with_openDatabase, |
michael@0 | 128 | test_fake_db_throws_with_openDatabase, |
michael@0 | 129 | test_backup_not_new_filename, |
michael@0 | 130 | test_backup_new_filename, |
michael@0 | 131 | test_backup_new_folder, |
michael@0 | 132 | ]; |
michael@0 | 133 | |
michael@0 | 134 | function run_test() |
michael@0 | 135 | { |
michael@0 | 136 | for (var i = 0; i < tests.length; i++) |
michael@0 | 137 | tests[i](); |
michael@0 | 138 | |
michael@0 | 139 | cleanup(); |
michael@0 | 140 | } |
michael@0 | 141 |