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";
|
|
|
|
|
2007-06-12 07:59:26 +04:00
|
|
|
function test_openSpecialDatabase_invalid_arg()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
getService().openSpecialDatabase("abcd");
|
|
|
|
do_throw("We should not get here!");
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
print("e.result is " + e.result);
|
|
|
|
do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-03 01:29:59 +04:00
|
|
|
function test_openDatabase_null_file()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
getService().openDatabase(null);
|
|
|
|
do_throw("We should not get here!");
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
print("e.result is " + e.result);
|
|
|
|
do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 02:53:41 +04:00
|
|
|
function test_openUnsharedDatabase_null_file()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
getService().openUnsharedDatabase(null);
|
|
|
|
do_throw("We should not get here!");
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
print("e.result is " + e.result);
|
|
|
|
do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-12 07:59:26 +04:00
|
|
|
function test_openDatabase_file_DNE()
|
|
|
|
{
|
|
|
|
// the file should be created after calling
|
|
|
|
var db = getTestDB();
|
|
|
|
do_check_false(db.exists());
|
|
|
|
getService().openDatabase(db);
|
|
|
|
do_check_true(db.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_openDatabase_file_exists()
|
|
|
|
{
|
|
|
|
// it should already exist from our last test
|
|
|
|
var db = getTestDB();
|
|
|
|
do_check_true(db.exists());
|
|
|
|
getService().openDatabase(db);
|
|
|
|
do_check_true(db.exists());
|
|
|
|
}
|
|
|
|
|
2008-03-25 01:14:38 +03:00
|
|
|
function test_corrupt_db_throws_with_openDatabase()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
getDatabase(getCorruptDB());
|
|
|
|
do_throw("should not be here");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(Cr.NS_ERROR_FILE_CORRUPTED, e.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-09 18:06:40 +04:00
|
|
|
function test_fake_db_throws_with_openDatabase()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
getDatabase(getFakeDB());
|
|
|
|
do_throw("should not be here");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(Cr.NS_ERROR_FILE_CORRUPTED, e.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-25 01:14:38 +03:00
|
|
|
function test_backup_not_new_filename()
|
|
|
|
{
|
|
|
|
const fname = getTestDB().leafName;
|
|
|
|
|
|
|
|
var backup = getService().backupDatabaseFile(getTestDB(), fname);
|
|
|
|
do_check_neq(fname, backup.leafName);
|
|
|
|
|
|
|
|
backup.remove(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_backup_new_filename()
|
|
|
|
{
|
|
|
|
var backup = getService().backupDatabaseFile(getTestDB(), BACKUP_FILE_NAME);
|
|
|
|
do_check_eq(BACKUP_FILE_NAME, backup.leafName);
|
2015-12-04 19:00:03 +03:00
|
|
|
|
2008-03-25 01:14:38 +03:00
|
|
|
backup.remove(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_backup_new_folder()
|
|
|
|
{
|
|
|
|
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);
|
2008-03-25 01:14:38 +03:00
|
|
|
do_check_true(parentDir.exists());
|
|
|
|
|
|
|
|
var backup = getService().backupDatabaseFile(getTestDB(), BACKUP_FILE_NAME,
|
|
|
|
parentDir);
|
|
|
|
do_check_eq(BACKUP_FILE_NAME, backup.leafName);
|
|
|
|
do_check_true(parentDir.equals(backup.parent));
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|