summaryrefslogtreecommitdiffstats
path: root/storage/test/unit/test_storage_connection.js
blob: e7175a96138c86c60484abafb3f5f4ed0ecd5212 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
/* 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 mozIStorageConnection

function fetchAllNames(conn) {
  let names = [];
  let stmt = conn.createStatement(`SELECT name FROM test ORDER BY name`);
  while (stmt.executeStep()) {
    names.push(stmt.getUTF8String(0));
  }
  stmt.finalize();
  return names;
}

// Test Functions

add_task(async function test_connectionReady_open() {
  // there doesn't seem to be a way for the connection to not be ready (unless
  // we close it with mozIStorageConnection::Close(), but we don't for this).
  // It can only fail if GetPath fails on the database file, or if we run out
  // of memory trying to use an in-memory database

  var msc = getOpenedDatabase();
  Assert.ok(msc.connectionReady);
});

add_task(async function test_connectionReady_closed() {
  // This also tests mozIStorageConnection::Close()

  var msc = getOpenedDatabase();
  msc.close();
  Assert.ok(!msc.connectionReady);
  gDBConn = null; // this is so later tests don't start to fail.
});

add_task(async function test_databaseFile() {
  var msc = getOpenedDatabase();
  Assert.ok(getTestDB().equals(msc.databaseFile));
});

add_task(async function test_tableExists_not_created() {
  var msc = getOpenedDatabase();
  Assert.ok(!msc.tableExists("foo"));
});

add_task(async function test_indexExists_not_created() {
  var msc = getOpenedDatabase();
  Assert.ok(!msc.indexExists("foo"));
});

add_task(async function test_temp_tableExists_and_indexExists() {
  var msc = getOpenedDatabase();
  msc.executeSimpleSQL(
    "CREATE TEMP TABLE test_temp(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)"
  );
  Assert.ok(msc.tableExists("test_temp"));

  msc.executeSimpleSQL("CREATE INDEX test_temp_ind ON test_temp (name)");
  Assert.ok(msc.indexExists("test_temp_ind"));

  msc.executeSimpleSQL("DROP INDEX test_temp_ind");
  msc.executeSimpleSQL("DROP TABLE test_temp");
});

add_task(async function test_createTable_not_created() {
  var msc = getOpenedDatabase();
  msc.createTable("test", "id INTEGER PRIMARY KEY, name TEXT");
  Assert.ok(msc.tableExists("test"));
});

add_task(async function test_indexExists_created() {
  var msc = getOpenedDatabase();
  msc.executeSimpleSQL("CREATE INDEX name_ind ON test (name)");
  Assert.ok(msc.indexExists("name_ind"));
});

add_task(async function test_createTable_already_created() {
  var msc = getOpenedDatabase();
  Assert.ok(msc.tableExists("test"));
  Assert.throws(
    () => msc.createTable("test", "id INTEGER PRIMARY KEY, name TEXT"),
    /NS_ERROR_FAILURE/
  );
});

add_task(async function test_attach_createTable_tableExists_indexExists() {
  var msc = getOpenedDatabase();
  var file = do_get_file("storage_attach.sqlite", true);
  var msc2 = getDatabase(file);
  msc.executeSimpleSQL("ATTACH DATABASE '" + file.path + "' AS sample");

  Assert.ok(!msc.tableExists("sample.test"));
  msc.createTable("sample.test", "id INTEGER PRIMARY KEY, name TEXT");
  Assert.ok(msc.tableExists("sample.test"));
  Assert.throws(
    () => msc.createTable("sample.test", "id INTEGER PRIMARY KEY, name TEXT"),
    /NS_ERROR_FAILURE/
  );

  Assert.ok(!msc.indexExists("sample.test_ind"));
  msc.executeSimpleSQL("CREATE INDEX sample.test_ind ON test (name)");
  Assert.ok(msc.indexExists("sample.test_ind"));

  msc.executeSimpleSQL("DETACH DATABASE sample");
  msc2.close();
  try {
    file.remove(false);
  } catch (e) {
    // Do nothing.
  }
});

add_task(async function test_lastInsertRowID() {
  var msc = getOpenedDatabase();
  msc.executeSimpleSQL("INSERT INTO test (name) VALUES ('foo')");
  Assert.equal(1, msc.lastInsertRowID);
});

add_task(async function test_transactionInProgress_no() {
  var msc = getOpenedDatabase();
  Assert.ok(!msc.transactionInProgress);
});

add_task(async function test_transactionInProgress_yes() {
  var msc = getOpenedDatabase();
  msc.beginTransaction();
  Assert.ok(msc.transactionInProgress);
  msc.commitTransaction();
  Assert.ok(!msc.transactionInProgress);

  msc.beginTransaction();
  Assert.ok(msc.transactionInProgress);
  msc.rollbackTransaction();
  Assert.ok(!msc.transactionInProgress);
});

add_task(async function test_commitTransaction_no_transaction() {
  var msc = getOpenedDatabase();
  Assert.ok(!msc.transactionInProgress);
  Assert.throws(() => msc.commitTransaction(), /NS_ERROR_UNEXPECTED/);
});

add_task(async function test_rollbackTransaction_no_transaction() {
  var msc = getOpenedDatabase();
  Assert.ok(!msc.transactionInProgress);
  Assert.throws(() => msc.rollbackTransaction(), /NS_ERROR_UNEXPECTED/);
});

add_task(async function test_get_schemaVersion_not_set() {
  Assert.equal(0, getOpenedDatabase().schemaVersion);
});

add_task(async function test_set_schemaVersion() {
  var msc = getOpenedDatabase();
  const version = 1;
  msc.schemaVersion = version;
  Assert.equal(version, msc.schemaVersion);
});

add_task(async function test_set_schemaVersion_same() {
  var msc = getOpenedDatabase();
  const version = 1;
  msc.schemaVersion = version; // should still work ok
  Assert.equal(version, msc.schemaVersion);
});

add_task(async function test_set_schemaVersion_negative() {
  var msc = getOpenedDatabase();
  const version = -1;
  msc.schemaVersion = version;
  Assert.equal(version, msc.schemaVersion);
});

add_task(async function test_createTable() {
  var temp = getTestDB().parent;
  temp.append("test_db_table");
  try {
    var con = Services.storage.openDatabase(temp);
    con.createTable("a", "");
  } catch (e) {
    if (temp.exists()) {
      try {
        temp.remove(false);
      } catch (e2) {
        // Do nothing.
      }
    }
    Assert.ok(
      e.result == Cr.NS_ERROR_NOT_INITIALIZED || e.result == Cr.NS_ERROR_FAILURE
    );
  } finally {
    if (con) {
      con.close();
    }
  }
});

add_task(async function test_defaultSynchronousAtNormal() {
  getOpenedDatabase();
  var stmt = createStatement("PRAGMA synchronous;");
  try {
    stmt.executeStep();
    Assert.equal(1, stmt.getInt32(0));
  } finally {
    stmt.reset();
    stmt.finalize();
  }
});

// must be ran before executeAsync tests
add_task(async function test_close_does_not_spin_event_loop() {
  // We want to make sure that the event loop on the calling thread does not
  // spin when close is called.
  let event = {
    ran: false,
    run() {
      this.ran = true;
    },
  };

  // Post the event before we call close, so it would run if the event loop was
  // spun during close.
  Services.tm.dispatchToMainThread(event);

  // Sanity check, then close the database.  Afterwards, we should not have ran!
  Assert.ok(!event.ran);
  getOpenedDatabase().close();
  Assert.ok(!event.ran);

  // Reset gDBConn so that later tests will get a new connection object.
  gDBConn = null;
});

add_task(
  async function test_asyncClose_succeeds_with_finalized_async_statement() {
    // XXX this test isn't perfect since we can't totally control when events will
    //     run.  If this paticular function fails randomly, it means we have a
    //     real bug.

    // We want to make sure we create a cached async statement to make sure that
    // when we finalize our statement, we end up finalizing the async one too so
    // close will succeed.
    let stmt = createStatement("SELECT * FROM test");
    stmt.executeAsync();
    stmt.finalize();

    await asyncClose(getOpenedDatabase());
    // Reset gDBConn so that later tests will get a new connection object.
    gDBConn = null;
  }
);

// Would assert on debug builds.
if (!AppConstants.DEBUG) {
  add_task(async function test_close_then_release_statement() {
    // Testing the behavior in presence of a bad client that finalizes
    // statements after the database has been closed (typically by
    // letting the gc finalize the statement).
    let db = getOpenedDatabase();
    let stmt = createStatement(
      "SELECT * FROM test -- test_close_then_release_statement"
    );
    db.close();
    stmt.finalize(); // Finalize too late - this should not crash

    // Reset gDBConn so that later tests will get a new connection object.
    gDBConn = null;
  });

  add_task(async function test_asyncClose_then_release_statement() {
    // Testing the behavior in presence of a bad client that finalizes
    // statements after the database has been async closed (typically by
    // letting the gc finalize the statement).
    let db = getOpenedDatabase();
    let stmt = createStatement(
      "SELECT * FROM test -- test_asyncClose_then_release_statement"
    );
    await asyncClose(db);
    stmt.finalize(); // Finalize too late - this should not crash

    // Reset gDBConn so that later tests will get a new connection object.
    gDBConn = null;
  });
}

// In debug builds this would cause a fatal assertion.
if (!AppConstants.DEBUG) {
  add_task(async function test_close_fails_with_async_statement_ran() {
    let stmt = createStatement("SELECT * FROM test");
    stmt.executeAsync();
    stmt.finalize();

    let db = getOpenedDatabase();
    Assert.throws(() => db.close(), /NS_ERROR_UNEXPECTED/);
    // Reset gDBConn so that later tests will get a new connection object.
    gDBConn = null;
  });
}

add_task(async function test_clone_optional_param() {
  let db1 = Services.storage.openUnsharedDatabase(getTestDB());
  let db2 = db1.clone();
  Assert.ok(db2.connectionReady);

  // A write statement should not fail here.
  let stmt = db2.createStatement("INSERT INTO test (name) VALUES (:name)");
  stmt.params.name = "dwitte";
  stmt.execute();
  stmt.finalize();

  // And a read statement should succeed.
  stmt = db2.createStatement("SELECT * FROM test");
  Assert.ok(stmt.executeStep());
  stmt.finalize();

  // Additionally check that it is a connection on the same database.
  Assert.ok(db1.databaseFile.equals(db2.databaseFile));

  db1.close();
  db2.close();
});

async function standardAsyncTest(promisedDB, name, shouldInit = false) {
  info("Performing standard async test " + name);

  let adb = await promisedDB;
  Assert.ok(adb instanceof Ci.mozIStorageAsyncConnection);
  Assert.ok(adb instanceof Ci.mozIStorageConnection);

  if (shouldInit) {
    let stmt = adb.createAsyncStatement("CREATE TABLE test(name TEXT)");
    await executeAsync(stmt);
    stmt.finalize();
  }

  // Generate a name to insert and fetch back
  name = "worker bee " + Math.random() + " (" + name + ")";

  let stmt = adb.createAsyncStatement("INSERT INTO test (name) VALUES (:name)");
  stmt.params.name = name;
  let result = await executeAsync(stmt);
  info("Request complete");
  stmt.finalize();
  Assert.ok(Components.isSuccessCode(result));
  info("Extracting data");
  stmt = adb.createAsyncStatement("SELECT * FROM test");
  let found = false;
  await executeAsync(stmt, function (results) {
    info("Data has been extracted");
    for (
      let row = results.getNextRow();
      row != null;
      row = results.getNextRow()
    ) {
      if (row.getResultByName("name") == name) {
        found = true;
        break;
      }
    }
  });
  Assert.ok(found);
  stmt.finalize();
  await asyncClose(adb);

  info("Standard async test " + name + " complete");
}

add_task(async function test_open_async() {
  await standardAsyncTest(openAsyncDatabase(getTestDB(), null), "default");
  await standardAsyncTest(openAsyncDatabase(getTestDB()), "no optional arg");
  await standardAsyncTest(
    openAsyncDatabase(getTestDB(), { shared: false, interruptible: true }),
    "non-default options"
  );
  await standardAsyncTest(
    openAsyncDatabase("memory"),
    "in-memory database",
    true
  );
  await standardAsyncTest(
    openAsyncDatabase("memory", { shared: false }),
    "in-memory database and options",
    true
  );

  info("Testing async opening with readonly option");
  const impliedReadOnlyOption = { ignoreLockingMode: true };

  let raised = false;
  let adb = await openAsyncDatabase(getTestDB(), impliedReadOnlyOption);
  let stmt = adb.createAsyncStatement("CREATE TABLE test(name TEXT)");
  try {
    await executeAsync(stmt); // This should throw
  } catch (e) {
    raised = true;
  } finally {
    if (stmt) {
      stmt.finalize();
    }
    if (adb) {
      await asyncClose(adb);
    }
  }

  Assert.ok(raised);
});

add_task(async function test_async_open_with_shared_cache() {
  info("Testing that opening with a shared cache doesn't break stuff");
  let adb = await openAsyncDatabase(getTestDB(), { shared: true });

  let stmt = adb.createAsyncStatement("INSERT INTO test (name) VALUES (:name)");
  stmt.params.name = "clockworker";
  let result = await executeAsync(stmt);
  info("Request complete");
  stmt.finalize();
  Assert.ok(Components.isSuccessCode(result));
  info("Extracting data");
  stmt = adb.createAsyncStatement("SELECT * FROM test");
  let found = false;
  await executeAsync(stmt, function (results) {
    info("Data has been extracted");
    for (
      let row = results.getNextRow();
      row != null;
      row = results.getNextRow()
    ) {
      if (row.getResultByName("name") == "clockworker") {
        found = true;
        break;
      }
    }
  });
  Assert.ok(found);
  stmt.finalize();
  await asyncClose(adb);
});

add_task(async function test_clone_trivial_async() {
  info("Open connection");
  let db = Services.storage.openDatabase(getTestDB());
  Assert.ok(db instanceof Ci.mozIStorageAsyncConnection);
  info("AsyncClone connection");
  let clone = await asyncClone(db, true);
  Assert.ok(clone instanceof Ci.mozIStorageAsyncConnection);
  Assert.ok(clone instanceof Ci.mozIStorageConnection);
  info("Close connection");
  await asyncClose(db);
  info("Close clone");
  await asyncClose(clone);
});

add_task(async function test_clone_no_optional_param_async() {
  "use strict";
  info("Testing async cloning");
  let adb1 = await openAsyncDatabase(getTestDB(), null);
  Assert.ok(adb1 instanceof Ci.mozIStorageAsyncConnection);
  Assert.ok(adb1 instanceof Ci.mozIStorageConnection);

  info("Cloning database");

  let adb2 = await asyncClone(adb1);
  info(
    "Testing that the cloned db is a mozIStorageAsyncConnection " +
      "and not a mozIStorageConnection"
  );
  Assert.ok(adb2 instanceof Ci.mozIStorageAsyncConnection);
  Assert.ok(adb2 instanceof Ci.mozIStorageConnection);

  info("Inserting data into source db");
  let stmt = adb1.createAsyncStatement(
    "INSERT INTO test (name) VALUES (:name)"
  );

  stmt.params.name = "yoric";
  let result = await executeAsync(stmt);
  info("Request complete");
  stmt.finalize();
  Assert.ok(Components.isSuccessCode(result));
  info("Extracting data from clone db");
  stmt = adb2.createAsyncStatement("SELECT * FROM test");
  let found = false;
  await executeAsync(stmt, function (results) {
    info("Data has been extracted");
    for (
      let row = results.getNextRow();
      row != null;
      row = results.getNextRow()
    ) {
      if (row.getResultByName("name") == "yoric") {
        found = true;
        break;
      }
    }
  });
  Assert.ok(found);
  stmt.finalize();
  info("Closing databases");
  await asyncClose(adb2);
  info("First db closed");

  await asyncClose(adb1);
  info("Second db closed");
});

add_task(async function test_clone_readonly() {
  let db1 = Services.storage.openUnsharedDatabase(getTestDB());
  let db2 = db1.clone(true);
  Assert.ok(db2.connectionReady);

  // A write statement should fail here.
  let stmt = db2.createStatement("INSERT INTO test (name) VALUES (:name)");
  stmt.params.name = "reed";
  expectError(Cr.NS_ERROR_FILE_READ_ONLY, () => stmt.execute());
  stmt.finalize();

  // And a read statement should succeed.
  stmt = db2.createStatement("SELECT * FROM test");
  Assert.ok(stmt.executeStep());
  stmt.finalize();

  db1.close();
  db2.close();
});

add_task(async function test_clone_shared_readonly() {
  let db1 = Services.storage.openDatabase(getTestDB());
  let db2 = db1.clone(true);
  Assert.ok(db2.connectionReady);

  let stmt = db2.createStatement("INSERT INTO test (name) VALUES (:name)");
  stmt.params.name = "parker";
  // TODO currently SQLite does not actually work correctly here.  The behavior
  //      we want is commented out, and the current behavior is being tested
  //      for.  Our IDL comments will have to be updated when this starts to
  //      work again.
  stmt.execute();
  // expectError(Components.results.NS_ERROR_FILE_READ_ONLY, () => stmt.execute());
  stmt.finalize();

  // And a read statement should succeed.
  stmt = db2.createStatement("SELECT * FROM test");
  Assert.ok(stmt.executeStep());
  stmt.finalize();

  db1.close();
  db2.close();
});

add_task(async function test_close_clone_fails() {
  let calls = ["openDatabase", "openUnsharedDatabase"];
  calls.forEach(function (methodName) {
    let db = Services.storage[methodName](getTestDB());
    db.close();
    expectError(Cr.NS_ERROR_NOT_INITIALIZED, () => db.clone());
  });
});

add_task(async function test_clone_copies_functions() {
  const FUNC_NAME = "test_func";
  let calls = ["openDatabase", "openUnsharedDatabase"];
  let functionMethods = ["createFunction"];
  calls.forEach(function (methodName) {
    [true, false].forEach(function (readOnly) {
      functionMethods.forEach(function (functionMethod) {
        let db1 = Services.storage[methodName](getTestDB());
        // Create a function for db1.
        db1[functionMethod](FUNC_NAME, 1, {
          onFunctionCall: () => 0,
          onStep: () => 0,
          onFinal: () => 0,
        });

        // Clone it, and make sure the function exists still.
        let db2 = db1.clone(readOnly);
        // Note: this would fail if the function did not exist.
        let stmt = db2.createStatement(
          "SELECT " + FUNC_NAME + "(id) FROM test"
        );
        stmt.finalize();
        db1.close();
        db2.close();
      });
    });
  });
});

add_task(async function test_clone_copies_overridden_functions() {
  const FUNC_NAME = "lower";
  function test_func() {
    this.called = false;
  }
  test_func.prototype = {
    onFunctionCall() {
      this.called = true;
    },
    onStep() {
      this.called = true;
    },
    onFinal: () => 0,
  };

  let calls = ["openDatabase", "openUnsharedDatabase"];
  let functionMethods = ["createFunction"];
  calls.forEach(function (methodName) {
    [true, false].forEach(function (readOnly) {
      functionMethods.forEach(function (functionMethod) {
        let db1 = Services.storage[methodName](getTestDB());
        // Create a function for db1.
        let func = new test_func();
        db1[functionMethod](FUNC_NAME, 1, func);
        Assert.ok(!func.called);

        // Clone it, and make sure the function gets called.
        let db2 = db1.clone(readOnly);
        let stmt = db2.createStatement(
          "SELECT " + FUNC_NAME + "(id) FROM test"
        );
        stmt.executeStep();
        Assert.ok(func.called);
        stmt.finalize();
        db1.close();
        db2.close();
      });
    });
  });
});

add_task(async function test_clone_copies_pragmas() {
  const PRAGMAS = [
    { name: "cache_size", value: 500, copied: true },
    { name: "temp_store", value: 2, copied: true },
    { name: "foreign_keys", value: 1, copied: true },
    { name: "journal_size_limit", value: 524288, copied: true },
    { name: "synchronous", value: 2, copied: true },
    { name: "wal_autocheckpoint", value: 16, copied: true },
    { name: "busy_timeout", value: 50, copied: true },
    { name: "ignore_check_constraints", value: 1, copied: false },
  ];

  let db1 = Services.storage.openUnsharedDatabase(getTestDB());

  // Sanity check initial values are different from enforced ones.
  PRAGMAS.forEach(function (pragma) {
    let stmt = db1.createStatement("PRAGMA " + pragma.name);
    Assert.ok(stmt.executeStep());
    Assert.notEqual(pragma.value, stmt.getInt32(0));
    stmt.finalize();
  });
  // Execute pragmas.
  PRAGMAS.forEach(function (pragma) {
    db1.executeSimpleSQL("PRAGMA " + pragma.name + " = " + pragma.value);
  });

  let db2 = db1.clone();
  Assert.ok(db2.connectionReady);

  // Check cloned connection inherited pragma values.
  PRAGMAS.forEach(function (pragma) {
    let stmt = db2.createStatement("PRAGMA " + pragma.name);
    Assert.ok(stmt.executeStep());
    let validate = pragma.copied ? "equal" : "notEqual";
    Assert[validate](pragma.value, stmt.getInt32(0));
    stmt.finalize();
  });

  db1.close();
  db2.close();
});

add_task(async function test_readonly_clone_copies_pragmas() {
  const PRAGMAS = [
    { name: "cache_size", value: 500, copied: true },
    { name: "temp_store", value: 2, copied: true },
    { name: "foreign_keys", value: 1, copied: false },
    { name: "journal_size_limit", value: 524288, copied: false },
    { name: "synchronous", value: 2, copied: false },
    { name: "wal_autocheckpoint", value: 16, copied: false },
    { name: "busy_timeout", value: 50, copied: false },
    { name: "ignore_check_constraints", value: 1, copied: false },
  ];

  let db1 = Services.storage.openUnsharedDatabase(getTestDB());

  // Sanity check initial values are different from enforced ones.
  PRAGMAS.forEach(function (pragma) {
    let stmt = db1.createStatement("PRAGMA " + pragma.name);
    Assert.ok(stmt.executeStep());
    Assert.notEqual(pragma.value, stmt.getInt32(0));
    stmt.finalize();
  });
  // Execute pragmas.
  PRAGMAS.forEach(function (pragma) {
    db1.executeSimpleSQL("PRAGMA " + pragma.name + " = " + pragma.value);
  });

  let db2 = db1.clone(true);
  Assert.ok(db2.connectionReady);

  // Check cloned connection inherited pragma values.
  PRAGMAS.forEach(function (pragma) {
    let stmt = db2.createStatement("PRAGMA " + pragma.name);
    Assert.ok(stmt.executeStep());
    let validate = pragma.copied ? "equal" : "notEqual";
    Assert[validate](pragma.value, stmt.getInt32(0));
    stmt.finalize();
  });

  db1.close();
  db2.close();
});

add_task(async function test_clone_attach_database() {
  let db1 = Services.storage.openUnsharedDatabase(getTestDB());

  let c = 0;
  function attachDB(conn, name) {
    let file = Services.dirsvc.get("ProfD", Ci.nsIFile);
    file.append("test_storage_" + ++c + ".sqlite");
    let db = Services.storage.openUnsharedDatabase(file);
    conn.executeSimpleSQL(
      `ATTACH DATABASE '${db.databaseFile.path}' AS ${name}`
    );
    db.executeSimpleSQL(`CREATE TABLE test_${name}(name TEXT);`);
    db.close();
  }
  attachDB(db1, "attached_1");
  attachDB(db1, "attached_2");
  db1.executeSimpleSQL(`
    CREATE TEMP TRIGGER test_temp_afterinsert_trigger
    AFTER DELETE ON test_attached_1 FOR EACH ROW
    BEGIN
      INSERT INTO test(name) VALUES(OLD.name);
    END`);

  // These should not throw.
  let stmt = db1.createStatement("SELECT * FROM attached_1.sqlite_master");
  stmt.finalize();
  stmt = db1.createStatement("SELECT * FROM attached_2.sqlite_master");
  stmt.finalize();
  db1.executeSimpleSQL("INSERT INTO test_attached_1(name) VALUES('asuth')");
  db1.executeSimpleSQL("DELETE FROM test_attached_1");
  Assert.ok(fetchAllNames(db1).includes("asuth"));

  // R/W clone.
  let db2 = db1.clone();
  Assert.ok(db2.connectionReady);

  // These should not throw.
  stmt = db2.createStatement("SELECT * FROM attached_1.sqlite_master");
  stmt.finalize();
  stmt = db2.createStatement("SELECT * FROM attached_2.sqlite_master");
  stmt.finalize();
  db2.executeSimpleSQL("INSERT INTO test_attached_1(name) VALUES('past')");
  db2.executeSimpleSQL("DELETE FROM test_attached_1");
  let newNames = fetchAllNames(db2);
  Assert.ok(newNames.includes("past"));
  Assert.deepEqual(fetchAllNames(db1), newNames);

  // R/O clone.
  let db3 = db1.clone(true);
  Assert.ok(db3.connectionReady);

  // These should not throw.
  stmt = db3.createStatement("SELECT * FROM attached_1.sqlite_master");
  stmt.finalize();
  stmt = db3.createStatement("SELECT * FROM attached_2.sqlite_master");
  stmt.finalize();

  db1.close();
  db2.close();
  db3.close();
});

add_task(async function test_async_clone_with_temp_trigger_and_table() {
  info("Open connection");
  let db = Services.storage.openDatabase(getTestDB());
  Assert.ok(db instanceof Ci.mozIStorageAsyncConnection);

  info("Set up tables on original connection");
  let createQueries = [
    `CREATE TEMP TABLE test_temp(name TEXT)`,
    `CREATE INDEX test_temp_idx ON test_temp(name)`,
    `CREATE TEMP TRIGGER test_temp_afterdelete_trigger
     AFTER DELETE ON test_temp FOR EACH ROW
     BEGIN
       INSERT INTO test(name) VALUES(OLD.name);
     END`,
  ];
  for (let query of createQueries) {
    let stmt = db.createAsyncStatement(query);
    await executeAsync(stmt);
    stmt.finalize();
  }

  info("Create read-write clone with temp tables");
  let readWriteClone = await asyncClone(db, false);
  Assert.ok(readWriteClone instanceof Ci.mozIStorageAsyncConnection);

  info("Insert into temp table on read-write clone");
  let insertStmt = readWriteClone.createAsyncStatement(`
    INSERT INTO test_temp(name) VALUES('mak'), ('standard8'), ('markh')`);
  await executeAsync(insertStmt);
  insertStmt.finalize();

  info("Fire temp trigger on read-write clone");
  let deleteStmt = readWriteClone.createAsyncStatement(`
    DELETE FROM test_temp`);
  await executeAsync(deleteStmt);
  deleteStmt.finalize();

  info("Read from original connection");
  let names = fetchAllNames(db);
  Assert.ok(names.includes("mak"));
  Assert.ok(names.includes("standard8"));
  Assert.ok(names.includes("markh"));

  info("Create read-only clone");
  let readOnlyClone = await asyncClone(db, true);
  Assert.ok(readOnlyClone instanceof Ci.mozIStorageAsyncConnection);

  info("Read-only clone shouldn't have temp entities");
  let badStmt = readOnlyClone.createAsyncStatement(`SELECT 1 FROM test_temp`);
  await Assert.rejects(executeAsync(badStmt), Ci.mozIStorageError);
  badStmt.finalize();

  info("Clean up");
  for (let conn of [db, readWriteClone, readOnlyClone]) {
    await asyncClose(conn);
  }
});

add_task(async function test_sync_clone_in_transaction() {
  info("Open connection");
  let db = Services.storage.openDatabase(getTestDB());
  Assert.ok(db instanceof Ci.mozIStorageAsyncConnection);

  info("Begin transaction on main connection");
  db.beginTransaction();

  info("Create temp table and trigger in transaction");
  let createQueries = [
    `CREATE TEMP TABLE test_temp(name TEXT)`,
    `CREATE TEMP TRIGGER test_temp_afterdelete_trigger
     AFTER DELETE ON test_temp FOR EACH ROW
     BEGIN
       INSERT INTO test(name) VALUES(OLD.name);
     END`,
  ];
  for (let query of createQueries) {
    db.executeSimpleSQL(query);
  }

  info("Clone main connection while transaction is in progress");
  let clone = db.clone(/* aReadOnly */ false);

  // Dropping the table also drops `test_temp_afterdelete_trigger`.
  info("Drop temp table on main connection");
  db.executeSimpleSQL(`DROP TABLE test_temp`);

  info("Commit transaction");
  db.commitTransaction();

  info("Clone connection should still have temp entities");
  let readTempStmt = clone.createStatement(`SELECT 1 FROM test_temp`);
  readTempStmt.execute();
  readTempStmt.finalize();

  info("Clean up");

  db.close();
  clone.close();
});

add_task(async function test_sync_clone_with_function() {
  info("Open connection");
  let db = Services.storage.openDatabase(getTestDB());
  Assert.ok(db instanceof Ci.mozIStorageAsyncConnection);

  info("Create SQL function");
  function storeLastInsertedNameFunc() {
    this.name = null;
  }
  storeLastInsertedNameFunc.prototype = {
    onFunctionCall(args) {
      this.name = args.getUTF8String(0);
    },
  };
  let func = new storeLastInsertedNameFunc();
  db.createFunction("store_last_inserted_name", 1, func);

  info("Create temp trigger on main connection");
  db.executeSimpleSQL(`
    CREATE TEMP TRIGGER test_afterinsert_trigger
    AFTER INSERT ON test FOR EACH ROW
    BEGIN
      SELECT store_last_inserted_name(NEW.name);
    END`);

  info("Clone main connection");
  let clone = db.clone(/* aReadOnly */ false);

  info("Write to clone");
  clone.executeSimpleSQL(`INSERT INTO test(name) VALUES('kit')`);

  Assert.equal(func.name, "kit");

  info("Clean up");
  db.close();
  clone.close();
});

add_task(async function test_defaultTransactionType() {
  info("Open connection");
  let db = Services.storage.openDatabase(getTestDB());
  Assert.ok(db instanceof Ci.mozIStorageAsyncConnection);

  info("Verify default transaction type");
  Assert.equal(
    db.defaultTransactionType,
    Ci.mozIStorageConnection.TRANSACTION_DEFERRED
  );

  info("Test other transaction types");
  for (let type of [
    Ci.mozIStorageConnection.TRANSACTION_IMMEDIATE,
    Ci.mozIStorageConnection.TRANSACTION_EXCLUSIVE,
  ]) {
    db.defaultTransactionType = type;
    Assert.equal(db.defaultTransactionType, type);
  }

  info("Should reject unknown transaction types");
  Assert.throws(
    () =>
      (db.defaultTransactionType =
        Ci.mozIStorageConnection.TRANSACTION_DEFAULT),
    /NS_ERROR_ILLEGAL_VALUE/
  );

  db.defaultTransactionType = Ci.mozIStorageConnection.TRANSACTION_IMMEDIATE;

  info("Clone should inherit default transaction type");
  let clone = await asyncClone(db, true);
  Assert.ok(clone instanceof Ci.mozIStorageAsyncConnection);
  Assert.equal(
    clone.defaultTransactionType,
    Ci.mozIStorageConnection.TRANSACTION_IMMEDIATE
  );

  info("Begin immediate transaction on main connection");
  db.beginTransaction();

  info("Queue immediate transaction on clone");
  let stmts = [
    clone.createAsyncStatement(`BEGIN IMMEDIATE TRANSACTION`),
    clone.createAsyncStatement(`DELETE FROM test WHERE name = 'new'`),
    clone.createAsyncStatement(`COMMIT`),
  ];
  let promiseStmtsRan = stmts.map(stmt => executeAsync(stmt));

  info("Commit immediate transaction on main connection");
  db.executeSimpleSQL(`INSERT INTO test(name) VALUES('new')`);
  db.commitTransaction();

  info("Wait for transaction to succeed on clone");
  await Promise.all(promiseStmtsRan);

  info("Clean up");
  for (let stmt of stmts) {
    stmt.finalize();
  }
  await asyncClose(clone);
  await asyncClose(db);
});

add_task(async function test_variableLimit() {
  info("Open connection");
  let db = Services.storage.openDatabase(getTestDB());
  Assert.equal(db.variableLimit, 32766, "Should return default limit");
  await asyncClose(db);
});

add_task(async function test_getInterface() {
  let db = getOpenedDatabase();
  let target = db
    .QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIEventTarget);
  // Just check that target is non-null.  Other tests will ensure that it has
  // the correct value.
  Assert.ok(target != null);

  await asyncClose(db);
  gDBConn = null;
});