tbb-tests/browser_tor_bug2950.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tbb-tests/browser_tor_bug2950.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +// # Regression tests for tor Bug #2950, Make Permissions Manager memory-only
     1.5 +// Ensures that permissions.sqlite file in profile directory is not written to,
     1.6 +// even when we write a value to Firefox's permissions database.
     1.7 +
     1.8 +// The requisite test() function.
     1.9 +function test() {
    1.10 +
    1.11 +// Needed because of asynchronous part later in the test.
    1.12 +waitForExplicitFinish();
    1.13 +
    1.14 +// Shortcut
    1.15 +let Ci = Components.interfaces;
    1.16 +
    1.17 +// ## utility functions
    1.18 +
    1.19 +// __uri(spec)__.
    1.20 +// Creates an nsIURI instance from a spec
    1.21 +// (string address such as "http://torproject.org").
    1.22 +let uri = spec => Services.io.newURI(spec, null, null);
    1.23 +
    1.24 +// __setPermission(spec, key, value)__.
    1.25 +// Sets the site permission of type key to value, for the site located at address spec.
    1.26 +let setPermission = (spec, key, value) => SitePermissions.set(uri(spec), key, value);
    1.27 +
    1.28 +// __getPermission(spec, key)__.
    1.29 +// Reads the site permission value for permission type key, for the site
    1.30 +// located at address spec.
    1.31 +let getPermission = (spec, key) => SitePermissions.get(uri(spec), key);
    1.32 +
    1.33 +// __profileDirPath__.
    1.34 +// The Firefox Profile directory. Expected location of various persistent files.
    1.35 +let profileDirPath = Services.dirsvc.get("ProfD", Components.interfaces.nsIFile).path;
    1.36 +
    1.37 +// __fileInProfile(fileName)__.
    1.38 +// Returns an nsIFile instance corresponding to a file in the Profile directory.
    1.39 +let fileInProfile = fileName => FileUtils.File(profileDirPath + "/" + fileName);
    1.40 +
    1.41 +// ## Now let's run the test.
    1.42 +
    1.43 +let SITE = "http://torproject.org",
    1.44 +    KEY = "popup";
    1.45 +
    1.46 +let permissionsFile = fileInProfile("permissions.sqlite"),
    1.47 +                      lastModifiedTime = null,
    1.48 +                      newModifiedTime = null;
    1.49 +if (permissionsFile.exists()) {
    1.50 +  lastModifiedTime = permissionsFile.lastModifiedTime;
    1.51 +}
    1.52 +// Read the original value of the permission.
    1.53 +let originalValue = getPermission(SITE, KEY);
    1.54 +
    1.55 +// We need to delay by at least 1000 ms, because that's the granularity
    1.56 +// of file time stamps, it seems.
    1.57 +window.setTimeout(
    1.58 +  function () {
    1.59 +    // Set the permission to a new value.
    1.60 +    setPermission(SITE, KEY, (originalValue === 0) ? 1 : 0);
    1.61 +    // Now read back the permission value again.
    1.62 +    let newReadValue = getPermission(SITE, KEY);
    1.63 +    // Compare to confirm that the permission
    1.64 +    // value was successfully changed.
    1.65 +    isnot(newReadValue, originalValue, "Set a value in permissions db (perhaps in memory).");;
    1.66 +    // If file existed or now exists, get the current time stamp.
    1.67 +    if (permissionsFile.exists()) {
    1.68 +    	newModifiedTime = permissionsFile.lastModifiedTime;
    1.69 +    }
    1.70 +    // If file was created or modified since we began this test,
    1.71 +    // then permissions db is not memory only. Complain!
    1.72 +    is(lastModifiedTime, newModifiedTime, "Don't write to permissions.sqlite file on disk.");
    1.73 +    // We are done with the test.
    1.74 +    finish();
    1.75 +  }, 1100);
    1.76 +
    1.77 +} // test()

mercurial