Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | // # Regression tests for tor Bug #2950, Make Permissions Manager memory-only |
michael@0 | 2 | // Ensures that permissions.sqlite file in profile directory is not written to, |
michael@0 | 3 | // even when we write a value to Firefox's permissions database. |
michael@0 | 4 | |
michael@0 | 5 | // The requisite test() function. |
michael@0 | 6 | function test() { |
michael@0 | 7 | |
michael@0 | 8 | // Needed because of asynchronous part later in the test. |
michael@0 | 9 | waitForExplicitFinish(); |
michael@0 | 10 | |
michael@0 | 11 | // Shortcut |
michael@0 | 12 | let Ci = Components.interfaces; |
michael@0 | 13 | |
michael@0 | 14 | // ## utility functions |
michael@0 | 15 | |
michael@0 | 16 | // __uri(spec)__. |
michael@0 | 17 | // Creates an nsIURI instance from a spec |
michael@0 | 18 | // (string address such as "http://torproject.org"). |
michael@0 | 19 | let uri = spec => Services.io.newURI(spec, null, null); |
michael@0 | 20 | |
michael@0 | 21 | // __setPermission(spec, key, value)__. |
michael@0 | 22 | // Sets the site permission of type key to value, for the site located at address spec. |
michael@0 | 23 | let setPermission = (spec, key, value) => SitePermissions.set(uri(spec), key, value); |
michael@0 | 24 | |
michael@0 | 25 | // __getPermission(spec, key)__. |
michael@0 | 26 | // Reads the site permission value for permission type key, for the site |
michael@0 | 27 | // located at address spec. |
michael@0 | 28 | let getPermission = (spec, key) => SitePermissions.get(uri(spec), key); |
michael@0 | 29 | |
michael@0 | 30 | // __profileDirPath__. |
michael@0 | 31 | // The Firefox Profile directory. Expected location of various persistent files. |
michael@0 | 32 | let profileDirPath = Services.dirsvc.get("ProfD", Components.interfaces.nsIFile).path; |
michael@0 | 33 | |
michael@0 | 34 | // __fileInProfile(fileName)__. |
michael@0 | 35 | // Returns an nsIFile instance corresponding to a file in the Profile directory. |
michael@0 | 36 | let fileInProfile = fileName => FileUtils.File(profileDirPath + "/" + fileName); |
michael@0 | 37 | |
michael@0 | 38 | // ## Now let's run the test. |
michael@0 | 39 | |
michael@0 | 40 | let SITE = "http://torproject.org", |
michael@0 | 41 | KEY = "popup"; |
michael@0 | 42 | |
michael@0 | 43 | let permissionsFile = fileInProfile("permissions.sqlite"), |
michael@0 | 44 | lastModifiedTime = null, |
michael@0 | 45 | newModifiedTime = null; |
michael@0 | 46 | if (permissionsFile.exists()) { |
michael@0 | 47 | lastModifiedTime = permissionsFile.lastModifiedTime; |
michael@0 | 48 | } |
michael@0 | 49 | // Read the original value of the permission. |
michael@0 | 50 | let originalValue = getPermission(SITE, KEY); |
michael@0 | 51 | |
michael@0 | 52 | // We need to delay by at least 1000 ms, because that's the granularity |
michael@0 | 53 | // of file time stamps, it seems. |
michael@0 | 54 | window.setTimeout( |
michael@0 | 55 | function () { |
michael@0 | 56 | // Set the permission to a new value. |
michael@0 | 57 | setPermission(SITE, KEY, (originalValue === 0) ? 1 : 0); |
michael@0 | 58 | // Now read back the permission value again. |
michael@0 | 59 | let newReadValue = getPermission(SITE, KEY); |
michael@0 | 60 | // Compare to confirm that the permission |
michael@0 | 61 | // value was successfully changed. |
michael@0 | 62 | isnot(newReadValue, originalValue, "Set a value in permissions db (perhaps in memory).");; |
michael@0 | 63 | // If file existed or now exists, get the current time stamp. |
michael@0 | 64 | if (permissionsFile.exists()) { |
michael@0 | 65 | newModifiedTime = permissionsFile.lastModifiedTime; |
michael@0 | 66 | } |
michael@0 | 67 | // If file was created or modified since we began this test, |
michael@0 | 68 | // then permissions db is not memory only. Complain! |
michael@0 | 69 | is(lastModifiedTime, newModifiedTime, "Don't write to permissions.sqlite file on disk."); |
michael@0 | 70 | // We are done with the test. |
michael@0 | 71 | finish(); |
michael@0 | 72 | }, 1100); |
michael@0 | 73 | |
michael@0 | 74 | } // test() |