summaryrefslogtreecommitdiffstats
path: root/storage/test/unit/test_storage_service.js
blob: 68fcb15423e676882a97962bc3d678a5f2e14540 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* 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
// openSpecialDatabase, which is tested by test_storage_service_special.js and
// openUnsharedDatabase, which is tested by test_storage_service_unshared.js.

const BACKUP_FILE_NAME = "test_storage.sqlite.backup";

function test_openDatabase_null_file() {
  try {
    Services.storage.openDatabase(null);
    do_throw("We should not get here!");
  } catch (e) {
    print(e);
    print("e.result is " + e.result);
    Assert.equal(Cr.NS_ERROR_INVALID_ARG, e.result);
  }
}

function test_openDatabase_file_DNE() {
  // the file should be created after calling

  let histogram = TelemetryTestUtils.getAndClearKeyedHistogram(OPEN_HISTOGRAM);

  var db = getTestDB();
  Assert.ok(!db.exists());
  Services.storage.openDatabase(db);
  Assert.ok(db.exists());

  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    db.leafName,
    TELEMETRY_VALUES.success,
    1
  );
}

function test_openDatabase_file_exists() {
  // it should already exist from our last test

  let histogram = TelemetryTestUtils.getAndClearKeyedHistogram(OPEN_HISTOGRAM);

  var db = getTestDB();
  Assert.ok(db.exists());
  Services.storage.openDatabase(db);
  Assert.ok(db.exists());

  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    db.leafName,
    TELEMETRY_VALUES.success,
    1
  );
}

function test_corrupt_db_throws_with_openDatabase() {
  let histogram = TelemetryTestUtils.getAndClearKeyedHistogram(OPEN_HISTOGRAM);

  let db = getCorruptDB();

  try {
    getDatabase(db);
    do_throw("should not be here");
  } catch (e) {
    Assert.equal(Cr.NS_ERROR_FILE_CORRUPTED, e.result);
  }

  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    db.leafName,
    TELEMETRY_VALUES.corrupt,
    1
  );
}

function test_fake_db_throws_with_openDatabase() {
  let histogram = TelemetryTestUtils.getAndClearKeyedHistogram(OPEN_HISTOGRAM);

  let db = getFakeDB();

  try {
    getDatabase(db);
    do_throw("should not be here");
  } catch (e) {
    Assert.equal(Cr.NS_ERROR_FILE_CORRUPTED, e.result);
  }

  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    db.leafName,
    TELEMETRY_VALUES.corrupt,
    1
  );
}

function test_backup_not_new_filename() {
  const fname = getTestDB().leafName;

  var backup = Services.storage.backupDatabaseFile(getTestDB(), fname);
  Assert.notEqual(fname, backup.leafName);

  backup.remove(false);
}

function test_backup_new_filename() {
  var backup = Services.storage.backupDatabaseFile(
    getTestDB(),
    BACKUP_FILE_NAME
  );
  Assert.equal(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);
  Assert.ok(parentDir.exists());

  var backup = Services.storage.backupDatabaseFile(
    getTestDB(),
    BACKUP_FILE_NAME,
    parentDir
  );
  Assert.equal(BACKUP_FILE_NAME, backup.leafName);
  Assert.ok(parentDir.equals(backup.parent));

  parentDir.remove(true);
}

function test_openDatabase_directory() {
  let dir = getTestDB().parent;
  dir.append("test_storage_temp");
  if (dir.exists()) {
    dir.remove(true);
  }
  dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
  Assert.ok(dir.exists());

  let histogram = TelemetryTestUtils.getAndClearKeyedHistogram(OPEN_HISTOGRAM);

  try {
    getDatabase(dir);
    do_throw("should not be here");
  } catch (e) {
    Assert.equal(Cr.NS_ERROR_FILE_ACCESS_DENIED, e.result);
  }

  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    dir.leafName,
    TELEMETRY_VALUES.access,
    1
  );

  dir.remove(true);
}

function test_read_gooddb() {
  let file = do_get_file("goodDB.sqlite");
  let db = getDatabase(file);

  let histogram = TelemetryTestUtils.getAndClearKeyedHistogram(QUERY_HISTOGRAM);

  db.executeSimpleSQL("SELECT * FROM Foo;");

  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    file.leafName,
    TELEMETRY_VALUES.success,
    1
  );

  histogram.clear();

  let stmt = db.createStatement("SELECT id from Foo");

  while (true) {
    if (!stmt.executeStep()) {
      break;
    }
  }

  stmt.finalize();

  // A single statement should count as a single access.
  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    file.leafName,
    TELEMETRY_VALUES.success,
    1
  );

  histogram.clear();

  Assert.throws(
    () => db.executeSimpleSQL("INSERT INTO Foo (rowid) VALUES ('test');"),
    /NS_ERROR_FAILURE/,
    "Executing sql should fail."
  );
  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    file.leafName,
    TELEMETRY_VALUES.misuse,
    1
  );
}

function test_read_baddb() {
  let file = do_get_file("baddataDB.sqlite");
  let db = getDatabase(file);

  let histogram = TelemetryTestUtils.getAndClearKeyedHistogram(QUERY_HISTOGRAM);

  Assert.throws(
    () => db.executeSimpleSQL("SELECT * FROM Foo"),
    /NS_ERROR_FILE_CORRUPTED/,
    "Executing sql should fail."
  );

  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    file.leafName,
    TELEMETRY_VALUES.corrupt,
    1
  );

  histogram.clear();

  let stmt = db.createStatement("SELECT * FROM Foo");
  Assert.throws(
    () => stmt.executeStep(),
    /NS_ERROR_FILE_CORRUPTED/,
    "Executing a statement should fail."
  );

  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    file.leafName,
    TELEMETRY_VALUES.corrupt,
    1
  );
}

function test_busy_telemetry() {
  // Thunderbird doesn't have one or more of the probes used in this test.
  // Ensure the data is collected anyway.
  Services.prefs.setBoolPref(
    "toolkit.telemetry.testing.overrideProductsCheck",
    true
  );

  let file = do_get_file("goodDB.sqlite");
  let conn1 = Services.storage.openUnsharedDatabase(file);
  let conn2 = Services.storage.openUnsharedDatabase(file);

  conn1.beginTransaction();
  conn1.executeSimpleSQL("CREATE TABLE test (id INTEGER PRIMARY KEY)");

  let histogram = TelemetryTestUtils.getAndClearKeyedHistogram(QUERY_HISTOGRAM);
  Assert.throws(
    () =>
      conn2.executeSimpleSQL("CREATE TABLE test_busy (id INTEGER PRIMARY KEY)"),
    /NS_ERROR_STORAGE_BUSY/,
    "Nested transaction on second connection should fail"
  );
  TelemetryTestUtils.assertKeyedHistogramValue(
    histogram,
    file.leafName,
    TELEMETRY_VALUES.busy,
    1
  );

  conn1.rollbackTransaction();
}

var tests = [
  test_openDatabase_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,
  test_openDatabase_directory,
  test_read_gooddb,
  test_read_baddb,
  test_busy_telemetry,
];

function run_test() {
  // Thunderbird doesn't have one or more of the probes used in this test.
  // Ensure the data is collected anyway.
  Services.prefs.setBoolPref(
    "toolkit.telemetry.testing.overrideProductsCheck",
    true
  );

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

  cleanup();
}