|
1 // Copied from components/places/tests/unit/head_bookmarks.js |
|
2 |
|
3 const NS_APP_USER_PROFILE_50_DIR = "ProfD"; |
|
4 const Ci = Components.interfaces; |
|
5 const Cc = Components.classes; |
|
6 const Cr = Components.results; |
|
7 |
|
8 // Various functions common to the tests. |
|
9 const LoginTest = { |
|
10 |
|
11 /* |
|
12 * initStorage |
|
13 * |
|
14 */ |
|
15 initStorage : function (aOutputPathName, aOutputFileName, aExpectedError) { |
|
16 var err = null; |
|
17 |
|
18 var newStorage = this.newStorage(); |
|
19 |
|
20 var outputFile = null; |
|
21 if (aOutputFileName) { |
|
22 var outputFile = Cc["@mozilla.org/file/local;1"]. |
|
23 createInstance(Ci.nsILocalFile); |
|
24 outputFile.initWithPath(aOutputPathName); |
|
25 outputFile.append(aOutputFileName); |
|
26 |
|
27 // Delete any existing output file. This is primarily for Windows, |
|
28 // where we can't rely on having deleted files in the last test run. |
|
29 if (outputFile.exists()) |
|
30 outputFile.remove(false); |
|
31 } |
|
32 |
|
33 try { |
|
34 newStorage.initWithFile(outputFile); |
|
35 } catch (e) { |
|
36 err = e; |
|
37 } |
|
38 |
|
39 this.checkExpectedError(aExpectedError, err); |
|
40 |
|
41 return newStorage; |
|
42 }, |
|
43 |
|
44 |
|
45 /* |
|
46 * reloadStorage |
|
47 * |
|
48 * Reinitialize a storage module with the specified input. |
|
49 */ |
|
50 reloadStorage : function (aInputPathName, aInputFileName, aExpectedError) { |
|
51 var err = null; |
|
52 var newStorage = this.newStorage(); |
|
53 |
|
54 var inputFile = null; |
|
55 if (aInputFileName) { |
|
56 var inputFile = Cc["@mozilla.org/file/local;1"]. |
|
57 createInstance(Ci.nsILocalFile); |
|
58 inputFile.initWithPath(aInputPathName); |
|
59 inputFile.append(aInputFileName); |
|
60 } |
|
61 |
|
62 try { |
|
63 newStorage.initWithFile(inputFile); |
|
64 } catch (e) { |
|
65 err = e; |
|
66 } |
|
67 |
|
68 if (aExpectedError) |
|
69 this.checkExpectedError(aExpectedError, err); |
|
70 else |
|
71 do_check_true(err == null); |
|
72 |
|
73 return newStorage; |
|
74 }, |
|
75 |
|
76 |
|
77 /* |
|
78 * checkExpectedError |
|
79 * |
|
80 * Checks to see if a thrown error was expected or not, and if it |
|
81 * matches the expected value. |
|
82 */ |
|
83 checkExpectedError : function (aExpectedError, aActualError) { |
|
84 if (aExpectedError) { |
|
85 if (!aActualError) |
|
86 throw "Test didn't throw as expected (" + aExpectedError + ")"; |
|
87 |
|
88 if (!aExpectedError.test(aActualError)) |
|
89 throw "Test threw (" + aActualError + "), not (" + aExpectedError; |
|
90 |
|
91 // We got the expected error, so make a note in the test log. |
|
92 dump("...that error was expected.\n\n"); |
|
93 } else if (aActualError) { |
|
94 throw "Test threw unexpected error: " + aActualError; |
|
95 } |
|
96 }, |
|
97 |
|
98 |
|
99 /* |
|
100 * checkStorageData |
|
101 * |
|
102 * Compare info from component to what we expected. |
|
103 */ |
|
104 checkStorageData : function (storage, ref_disabledHosts, ref_logins) { |
|
105 this.checkLogins(ref_logins, storage.getAllLogins()); |
|
106 this.checkDisabledHosts(ref_disabledHosts, storage.getAllDisabledHosts()); |
|
107 }, |
|
108 |
|
109 /* |
|
110 * checkLogins |
|
111 * |
|
112 * Check values of the logins list. |
|
113 */ |
|
114 checkLogins : function (expectedLogins, actualLogins) { |
|
115 do_check_eq(expectedLogins.length, actualLogins.length); |
|
116 for (let i = 0; i < expectedLogins.length; i++) { |
|
117 let found = false; |
|
118 for (let j = 0; !found && j < actualLogins.length; j++) { |
|
119 found = expectedLogins[i].equals(actualLogins[j]); |
|
120 } |
|
121 do_check_true(found); |
|
122 } |
|
123 }, |
|
124 |
|
125 /* |
|
126 * checkDisabledHosts |
|
127 * |
|
128 * Check values of the disabled list. |
|
129 */ |
|
130 checkDisabledHosts : function (expectedHosts, actualHosts) { |
|
131 do_check_eq(expectedHosts.length, actualHosts.length); |
|
132 for (let i = 0; i < expectedHosts.length; i++) { |
|
133 let found = false; |
|
134 for (let j = 0; !found && j < actualHosts.length; j++) { |
|
135 found = (expectedHosts[i] == actualHosts[j]); |
|
136 } |
|
137 do_check_true(found); |
|
138 } |
|
139 }, |
|
140 |
|
141 /* |
|
142 * countLinesInFile |
|
143 * |
|
144 * Counts the number of lines in the specified file. |
|
145 */ |
|
146 countLinesInFile : function (aPathName, aFileName) { |
|
147 var inputFile = Cc["@mozilla.org/file/local;1"]. |
|
148 createInstance(Ci.nsILocalFile); |
|
149 inputFile.initWithPath(aPathName); |
|
150 inputFile.append(aFileName); |
|
151 if (inputFile.fileSize == 0) |
|
152 return 0; |
|
153 |
|
154 var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]. |
|
155 createInstance(Ci.nsIFileInputStream); |
|
156 // init the stream as RD_ONLY, -1 == default permissions. |
|
157 inputStream.init(inputFile, 0x01, -1, null); |
|
158 var lineStream = inputStream.QueryInterface(Ci.nsILineInputStream); |
|
159 |
|
160 var line = { value : null }; |
|
161 var lineCount = 1; // Empty files were dealt with above. |
|
162 while (lineStream.readLine(line)) |
|
163 lineCount++; |
|
164 |
|
165 return lineCount; |
|
166 }, |
|
167 |
|
168 newStorage : function () { |
|
169 |
|
170 var storage = Cc["@mozilla.org/login-manager/storage/mozStorage;1"]. |
|
171 createInstance(Ci.nsILoginManagerStorage); |
|
172 if (!storage) |
|
173 throw "Couldn't create storage instance."; |
|
174 return storage; |
|
175 }, |
|
176 |
|
177 openDB : function (filename) { |
|
178 // nsIFile for the specified filename, in the profile dir. |
|
179 var dbfile = PROFDIR.clone(); |
|
180 dbfile.append(filename); |
|
181 |
|
182 var ss = Cc["@mozilla.org/storage/service;1"]. |
|
183 getService(Ci.mozIStorageService); |
|
184 var dbConnection = ss.openDatabase(dbfile); |
|
185 |
|
186 return dbConnection; |
|
187 }, |
|
188 |
|
189 deleteFile : function (pathname, filename) { |
|
190 var file = Cc["@mozilla.org/file/local;1"]. |
|
191 createInstance(Ci.nsILocalFile); |
|
192 file.initWithPath(pathname); |
|
193 file.append(filename); |
|
194 // Suppress failures, this happens in the mozstorage tests on Windows |
|
195 // because the module may still be holding onto the DB. (We don't |
|
196 // have a way to explicitly shutdown/GC the module). |
|
197 try { |
|
198 if (file.exists()) |
|
199 file.remove(false); |
|
200 } catch (e) {} |
|
201 }, |
|
202 |
|
203 // Copies a file from our test data directory to the unit test profile. |
|
204 copyFile : function (filename) { |
|
205 var file = DATADIR.clone(); |
|
206 file.append(filename); |
|
207 |
|
208 var profileFile = PROFDIR.clone(); |
|
209 profileFile.append(filename); |
|
210 |
|
211 if (profileFile.exists()) |
|
212 profileFile.remove(false); |
|
213 |
|
214 file.copyTo(PROFDIR, filename); |
|
215 }, |
|
216 |
|
217 // Returns true if the timestamp is within 30 seconds of now. |
|
218 is_about_now : function (timestamp) { |
|
219 var delta = Math.abs(timestamp - Date.now()); |
|
220 var seconds = 30 * 1000; |
|
221 return delta < seconds; |
|
222 } |
|
223 }; |
|
224 |
|
225 // nsIFiles... |
|
226 var PROFDIR = do_get_profile(); |
|
227 var DATADIR = do_get_file("data/"); |
|
228 // string versions... |
|
229 var OUTDIR = PROFDIR.path; |
|
230 |
|
231 // Copy key3.db into the profile used for the unit tests. Need this so we can |
|
232 // decrypt the encrypted logins stored in the various tests inputs. |
|
233 LoginTest.copyFile("key3.db"); |