tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_sqlite_secure_delete.js (2188B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
      2 *vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
      3 * This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /**
      8 * This file tests to make sure that SQLite was compiled with
      9 * SQLITE_SECURE_DELETE=1.
     10 */
     11 
     12 // Helper Methods
     13 
     14 /**
     15 * Reads the contents of a file and returns it as a string.
     16 *
     17 * @param aFile
     18 *        The file to return from.
     19 * @return the contents of the file in the form of a string.
     20 */
     21 function getFileContents(aFile) {
     22  let fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
     23    Ci.nsIFileInputStream
     24  );
     25  fstream.init(aFile, -1, 0, 0);
     26 
     27  let bstream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
     28    Ci.nsIBinaryInputStream
     29  );
     30  bstream.setInputStream(fstream);
     31  return bstream.readBytes(bstream.available());
     32 }
     33 
     34 // Tests
     35 
     36 add_test(function test_delete_removes_data() {
     37  const TEST_STRING = "SomeRandomStringToFind";
     38 
     39  let file = getTestDB();
     40  let db = Services.storage.openDatabase(file);
     41 
     42  // Create the table and insert the data.
     43  db.createTable("test", "data TEXT");
     44  let stmt = db.createStatement("INSERT INTO test VALUES(:data)");
     45  stmt.params.data = TEST_STRING;
     46  try {
     47    stmt.execute();
     48  } finally {
     49    stmt.finalize();
     50  }
     51 
     52  // Make sure this test is actually testing what it thinks by making sure the
     53  // string shows up in the database.  Because the previous statement was
     54  // automatically wrapped in a transaction, the contents are already on disk.
     55  let contents = getFileContents(file);
     56  Assert.notEqual(-1, contents.indexOf(TEST_STRING));
     57 
     58  // Delete the data, and then close the database.
     59  stmt = db.createStatement("DELETE FROM test WHERE data = :data");
     60  stmt.params.data = TEST_STRING;
     61  try {
     62    stmt.execute();
     63  } finally {
     64    stmt.finalize();
     65  }
     66  db.close();
     67 
     68  // Check the file to see if the string can be found.
     69  contents = getFileContents(file);
     70  Assert.equal(-1, contents.indexOf(TEST_STRING));
     71 
     72  run_next_test();
     73 });
     74 
     75 function run_test() {
     76  cleanup();
     77  run_next_test();
     78 }