storage/test/unit/test_sqlite_secure_delete.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/unit/test_sqlite_secure_delete.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/*-*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + *vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * This file tests to make sure that SQLite was compiled with
    1.12 + * SQLITE_SECURE_DELETE=1.
    1.13 + */
    1.14 +
    1.15 +////////////////////////////////////////////////////////////////////////////////
    1.16 +//// Helper Methods
    1.17 +
    1.18 +/**
    1.19 + * Reads the contents of a file and returns it as a string.
    1.20 + *
    1.21 + * @param aFile
    1.22 + *        The file to return from.
    1.23 + * @return the contents of the file in the form of a string.
    1.24 + */
    1.25 +function getFileContents(aFile)
    1.26 +{
    1.27 +  let fstream = Cc["@mozilla.org/network/file-input-stream;1"].
    1.28 +                createInstance(Ci.nsIFileInputStream);
    1.29 +  fstream.init(aFile, -1, 0, 0);
    1.30 +
    1.31 +  let bstream = Cc["@mozilla.org/binaryinputstream;1"].
    1.32 +                createInstance(Ci.nsIBinaryInputStream);
    1.33 +  bstream.setInputStream(fstream);
    1.34 +  return bstream.readBytes(bstream.available());
    1.35 +}
    1.36 +
    1.37 +////////////////////////////////////////////////////////////////////////////////
    1.38 +//// Tests
    1.39 +
    1.40 +function test_delete_removes_data()
    1.41 +{
    1.42 +  const TEST_STRING = "SomeRandomStringToFind";
    1.43 +
    1.44 +  let file = getTestDB();
    1.45 +  let db = getService().openDatabase(file);
    1.46 +
    1.47 +  // Create the table and insert the data.
    1.48 +  db.createTable("test", "data TEXT");
    1.49 +  let stmt = db.createStatement("INSERT INTO test VALUES(:data)");
    1.50 +  stmt.params.data = TEST_STRING;
    1.51 +  try {
    1.52 +    stmt.execute();
    1.53 +  }
    1.54 +  finally {
    1.55 +    stmt.finalize();
    1.56 +  }
    1.57 +
    1.58 +  // Make sure this test is actually testing what it thinks by making sure the
    1.59 +  // string shows up in the database.  Because the previous statement was
    1.60 +  // automatically wrapped in a transaction, the contents are already on disk.
    1.61 +  let contents = getFileContents(file);
    1.62 +  do_check_neq(-1, contents.indexOf(TEST_STRING));
    1.63 +
    1.64 +  // Delete the data, and then close the database.
    1.65 +  stmt = db.createStatement("DELETE FROM test WHERE data = :data");
    1.66 +  stmt.params.data = TEST_STRING;
    1.67 +  try {
    1.68 +    stmt.execute();
    1.69 +  }
    1.70 +  finally {
    1.71 +    stmt.finalize();
    1.72 +  }
    1.73 +  db.close();
    1.74 +
    1.75 +  // Check the file to see if the string can be found.
    1.76 +  contents = getFileContents(file);
    1.77 +  do_check_eq(-1, contents.indexOf(TEST_STRING));
    1.78 +
    1.79 +  run_next_test();
    1.80 +}
    1.81 +
    1.82 +////////////////////////////////////////////////////////////////////////////////
    1.83 +//// Test Runner
    1.84 +
    1.85 +[
    1.86 +  test_delete_removes_data,
    1.87 + ].forEach(add_test);
    1.88 +
    1.89 +function run_test()
    1.90 +{
    1.91 +  cleanup();
    1.92 +  run_next_test();
    1.93 +}

mercurial