|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 // This tests are for a sessionstore.js atomic backup. |
|
5 // Each test will wait for a write to the Session Store |
|
6 // before executing. |
|
7 |
|
8 let tmp = {}; |
|
9 Cu.import("resource://gre/modules/osfile.jsm", tmp); |
|
10 Cu.import("resource:///modules/sessionstore/SessionFile.jsm", tmp); |
|
11 |
|
12 const {OS, SessionFile} = tmp; |
|
13 |
|
14 const PREF_SS_INTERVAL = "browser.sessionstore.interval"; |
|
15 // Full paths for sessionstore.js and sessionstore.bak. |
|
16 const path = OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.js"); |
|
17 const backupPath = OS.Path.join(OS.Constants.Path.profileDir, |
|
18 "sessionstore.bak"); |
|
19 |
|
20 // A text decoder. |
|
21 let gDecoder = new TextDecoder(); |
|
22 // Global variables that contain sessionstore.js and sessionstore.bak data for |
|
23 // comparison between tests. |
|
24 let gSSData; |
|
25 let gSSBakData; |
|
26 |
|
27 |
|
28 |
|
29 add_task(function* testAfterFirstWrite() { |
|
30 // Ensure sessionstore.bak is not created. We start with a clean |
|
31 // profile so there was nothing to move to sessionstore.bak before |
|
32 // initially writing sessionstore.js |
|
33 let ssExists = yield OS.File.exists(path); |
|
34 let ssBackupExists = yield OS.File.exists(backupPath); |
|
35 ok(ssExists, "sessionstore.js should exist."); |
|
36 ok(!ssBackupExists, "sessionstore.bak should not have been created, yet"); |
|
37 |
|
38 // Save sessionstore.js data to compare to the sessionstore.bak data in the |
|
39 // next test. |
|
40 let array = yield OS.File.read(path); |
|
41 gSSData = gDecoder.decode(array); |
|
42 |
|
43 // Manually move to the backup since the first write has already happened |
|
44 // and a backup would not be triggered again. |
|
45 yield OS.File.move(path, backupPath); |
|
46 |
|
47 yield forceSaveState(); |
|
48 }); |
|
49 |
|
50 add_task(function* testReadBackup() { |
|
51 // Ensure sessionstore.bak is finally created. |
|
52 let ssExists = yield OS.File.exists(path); |
|
53 let ssBackupExists = yield OS.File.exists(backupPath); |
|
54 ok(ssExists, "sessionstore.js exists."); |
|
55 ok(ssBackupExists, "sessionstore.bak should now be created."); |
|
56 |
|
57 // Read sessionstore.bak data. |
|
58 let array = yield OS.File.read(backupPath); |
|
59 gSSBakData = gDecoder.decode(array); |
|
60 |
|
61 // Make sure that the sessionstore.bak is identical to the last |
|
62 // sessionstore.js. |
|
63 is(gSSBakData, gSSData, "sessionstore.js is backed up correctly."); |
|
64 |
|
65 // Read latest sessionstore.js. |
|
66 array = yield OS.File.read(path); |
|
67 gSSData = gDecoder.decode(array); |
|
68 |
|
69 // Read sessionstore.js with SessionFile.read. |
|
70 let ssDataRead = yield SessionFile.read(); |
|
71 is(ssDataRead, gSSData, "SessionFile.read read sessionstore.js correctly."); |
|
72 |
|
73 // Remove sessionstore.js to test fallback onto sessionstore.bak. |
|
74 yield OS.File.remove(path); |
|
75 ssExists = yield OS.File.exists(path); |
|
76 ok(!ssExists, "sessionstore.js should be removed now."); |
|
77 |
|
78 // Read sessionstore.bak with SessionFile.read. |
|
79 ssDataRead = yield SessionFile.read(); |
|
80 is(ssDataRead, gSSBakData, |
|
81 "SessionFile.read read sessionstore.bak correctly."); |
|
82 |
|
83 yield forceSaveState(); |
|
84 }); |
|
85 |
|
86 add_task(function* testBackupUnchanged() { |
|
87 // Ensure sessionstore.bak is backed up only once. |
|
88 |
|
89 // Read sessionstore.bak data. |
|
90 let array = yield OS.File.read(backupPath); |
|
91 let ssBakData = gDecoder.decode(array); |
|
92 // Ensure the sessionstore.bak did not change. |
|
93 is(ssBakData, gSSBakData, "sessionstore.bak is unchanged."); |
|
94 }); |
|
95 |
|
96 add_task(function* cleanup() { |
|
97 // Cleaning up after the test: removing the sessionstore.bak file. |
|
98 yield OS.File.remove(backupPath); |
|
99 }); |