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