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