summaryrefslogtreecommitdiffstats
path: root/storage/test/unit/test_storage_service.js
blob: 9cf46620ed147550ba2047919e586af886f7cf7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* 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/. */

// This file tests the functions of mozIStorageService except for
// openUnsharedDatabase, which is tested by test_storage_service_unshared.js.

const BACKUP_FILE_NAME = "test_storage.sqlite.backup";

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);
  }
}

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);
  }
}

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);
  }
}

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());
}

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);
  }
}

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);
  }
}

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);

  backup.remove(false);
}

function test_backup_new_folder()
{
  var parentDir = getTestDB().parent;
  parentDir.append("test_storage_temp");
  if (parentDir.exists())
    parentDir.remove(true);
  parentDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
  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,
  test_openDatabase_null_file,
  test_openUnsharedDatabase_null_file,
  test_openDatabase_file_DNE,
  test_openDatabase_file_exists,
  test_corrupt_db_throws_with_openDatabase,
  test_fake_db_throws_with_openDatabase,
  test_backup_not_new_filename,
  test_backup_new_filename,
  test_backup_new_folder,
];

function run_test()
{
  for (var i = 0; i < tests.length; i++) {
    tests[i]();
  }

  cleanup();
}