2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-06-12 07:59:26 +04:00
|
|
|
|
2008-01-30 02:34:19 +03:00
|
|
|
// This file tests the functions of mozIStorageService except for
|
|
|
|
// openUnsharedDatabase, which is tested by test_storage_service_unshared.js.
|
2007-06-12 07:59:26 +04:00
|
|
|
|
2008-03-25 01:14:38 +03:00
|
|
|
const BACKUP_FILE_NAME = "test_storage.sqlite.backup";
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_openSpecialDatabase_invalid_arg() {
|
2007-06-12 07:59:26 +04:00
|
|
|
try {
|
2017-11-30 15:04:16 +03:00
|
|
|
Services.storage.openSpecialDatabase("abcd");
|
2007-06-12 07:59:26 +04:00
|
|
|
do_throw("We should not get here!");
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
print("e.result is " + e.result);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Cr.NS_ERROR_INVALID_ARG, e.result);
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_openDatabase_null_file() {
|
2010-08-03 01:29:59 +04:00
|
|
|
try {
|
2017-11-30 15:04:16 +03:00
|
|
|
Services.storage.openDatabase(null);
|
2010-08-03 01:29:59 +04:00
|
|
|
do_throw("We should not get here!");
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
print("e.result is " + e.result);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Cr.NS_ERROR_INVALID_ARG, e.result);
|
2010-08-03 01:29:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_openUnsharedDatabase_null_file() {
|
2011-07-02 02:53:41 +04:00
|
|
|
try {
|
2017-11-30 15:04:16 +03:00
|
|
|
Services.storage.openUnsharedDatabase(null);
|
2011-07-02 02:53:41 +04:00
|
|
|
do_throw("We should not get here!");
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
print("e.result is " + e.result);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Cr.NS_ERROR_INVALID_ARG, e.result);
|
2011-07-02 02:53:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_openDatabase_file_DNE() {
|
2007-06-12 07:59:26 +04:00
|
|
|
// the file should be created after calling
|
|
|
|
var db = getTestDB();
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(!db.exists());
|
2017-11-30 15:04:16 +03:00
|
|
|
Services.storage.openDatabase(db);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(db.exists());
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_openDatabase_file_exists() {
|
2007-06-12 07:59:26 +04:00
|
|
|
// it should already exist from our last test
|
|
|
|
var db = getTestDB();
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(db.exists());
|
2017-11-30 15:04:16 +03:00
|
|
|
Services.storage.openDatabase(db);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(db.exists());
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_corrupt_db_throws_with_openDatabase() {
|
2008-03-25 01:14:38 +03:00
|
|
|
try {
|
|
|
|
getDatabase(getCorruptDB());
|
|
|
|
do_throw("should not be here");
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Cr.NS_ERROR_FILE_CORRUPTED, e.result);
|
2008-03-25 01:14:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_fake_db_throws_with_openDatabase() {
|
2011-11-09 18:06:40 +04:00
|
|
|
try {
|
|
|
|
getDatabase(getFakeDB());
|
|
|
|
do_throw("should not be here");
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Cr.NS_ERROR_FILE_CORRUPTED, e.result);
|
2011-11-09 18:06:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_backup_not_new_filename() {
|
2008-03-25 01:14:38 +03:00
|
|
|
const fname = getTestDB().leafName;
|
|
|
|
|
2017-11-30 15:04:16 +03:00
|
|
|
var backup = Services.storage.backupDatabaseFile(getTestDB(), fname);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.notEqual(fname, backup.leafName);
|
2008-03-25 01:14:38 +03:00
|
|
|
|
|
|
|
backup.remove(false);
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_backup_new_filename() {
|
2017-11-30 15:04:16 +03:00
|
|
|
var backup = Services.storage.backupDatabaseFile(getTestDB(), BACKUP_FILE_NAME);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(BACKUP_FILE_NAME, backup.leafName);
|
2015-12-04 19:00:03 +03:00
|
|
|
|
2008-03-25 01:14:38 +03:00
|
|
|
backup.remove(false);
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_backup_new_folder() {
|
2008-03-25 01:14:38 +03:00
|
|
|
var parentDir = getTestDB().parent;
|
|
|
|
parentDir.append("test_storage_temp");
|
|
|
|
if (parentDir.exists())
|
|
|
|
parentDir.remove(true);
|
2015-12-04 19:00:03 +03:00
|
|
|
parentDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(parentDir.exists());
|
2008-03-25 01:14:38 +03:00
|
|
|
|
2017-11-30 15:04:16 +03:00
|
|
|
var backup = Services.storage.backupDatabaseFile(getTestDB(), BACKUP_FILE_NAME,
|
|
|
|
parentDir);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(BACKUP_FILE_NAME, backup.leafName);
|
|
|
|
Assert.ok(parentDir.equals(backup.parent));
|
2008-03-25 01:14:38 +03:00
|
|
|
|
|
|
|
parentDir.remove(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = [
|
|
|
|
test_openSpecialDatabase_invalid_arg,
|
2010-08-03 01:29:59 +04:00
|
|
|
test_openDatabase_null_file,
|
2011-07-02 02:53:41 +04:00
|
|
|
test_openUnsharedDatabase_null_file,
|
2010-08-03 01:29:59 +04:00
|
|
|
test_openDatabase_file_DNE,
|
2008-03-25 01:14:38 +03:00
|
|
|
test_openDatabase_file_exists,
|
|
|
|
test_corrupt_db_throws_with_openDatabase,
|
2011-11-09 18:06:40 +04:00
|
|
|
test_fake_db_throws_with_openDatabase,
|
2008-03-25 01:14:38 +03:00
|
|
|
test_backup_not_new_filename,
|
|
|
|
test_backup_new_filename,
|
|
|
|
test_backup_new_folder,
|
|
|
|
];
|
2007-06-12 07:59:26 +04:00
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function run_test() {
|
2015-12-04 19:00:03 +03:00
|
|
|
for (var i = 0; i < tests.length; i++) {
|
2007-06-12 07:59:26 +04:00
|
|
|
tests[i]();
|
2015-12-04 19:00:03 +03:00
|
|
|
}
|
|
|
|
|
2007-06-12 07:59:26 +04:00
|
|
|
cleanup();
|
|
|
|
}
|