michael@0: // Copied from components/places/tests/unit/head_bookmarks.js michael@0: michael@0: const NS_APP_USER_PROFILE_50_DIR = "ProfD"; michael@0: const Ci = Components.interfaces; michael@0: const Cc = Components.classes; michael@0: const Cr = Components.results; michael@0: michael@0: // Various functions common to the tests. michael@0: const LoginTest = { michael@0: michael@0: /* michael@0: * initStorage michael@0: * michael@0: */ michael@0: initStorage : function (aOutputPathName, aOutputFileName, aExpectedError) { michael@0: var err = null; michael@0: michael@0: var newStorage = this.newStorage(); michael@0: michael@0: var outputFile = null; michael@0: if (aOutputFileName) { michael@0: var outputFile = Cc["@mozilla.org/file/local;1"]. michael@0: createInstance(Ci.nsILocalFile); michael@0: outputFile.initWithPath(aOutputPathName); michael@0: outputFile.append(aOutputFileName); michael@0: michael@0: // Delete any existing output file. This is primarily for Windows, michael@0: // where we can't rely on having deleted files in the last test run. michael@0: if (outputFile.exists()) michael@0: outputFile.remove(false); michael@0: } michael@0: michael@0: try { michael@0: newStorage.initWithFile(outputFile); michael@0: } catch (e) { michael@0: err = e; michael@0: } michael@0: michael@0: this.checkExpectedError(aExpectedError, err); michael@0: michael@0: return newStorage; michael@0: }, michael@0: michael@0: michael@0: /* michael@0: * reloadStorage michael@0: * michael@0: * Reinitialize a storage module with the specified input. michael@0: */ michael@0: reloadStorage : function (aInputPathName, aInputFileName, aExpectedError) { michael@0: var err = null; michael@0: var newStorage = this.newStorage(); michael@0: michael@0: var inputFile = null; michael@0: if (aInputFileName) { michael@0: var inputFile = Cc["@mozilla.org/file/local;1"]. michael@0: createInstance(Ci.nsILocalFile); michael@0: inputFile.initWithPath(aInputPathName); michael@0: inputFile.append(aInputFileName); michael@0: } michael@0: michael@0: try { michael@0: newStorage.initWithFile(inputFile); michael@0: } catch (e) { michael@0: err = e; michael@0: } michael@0: michael@0: if (aExpectedError) michael@0: this.checkExpectedError(aExpectedError, err); michael@0: else michael@0: do_check_true(err == null); michael@0: michael@0: return newStorage; michael@0: }, michael@0: michael@0: michael@0: /* michael@0: * checkExpectedError michael@0: * michael@0: * Checks to see if a thrown error was expected or not, and if it michael@0: * matches the expected value. michael@0: */ michael@0: checkExpectedError : function (aExpectedError, aActualError) { michael@0: if (aExpectedError) { michael@0: if (!aActualError) michael@0: throw "Test didn't throw as expected (" + aExpectedError + ")"; michael@0: michael@0: if (!aExpectedError.test(aActualError)) michael@0: throw "Test threw (" + aActualError + "), not (" + aExpectedError; michael@0: michael@0: // We got the expected error, so make a note in the test log. michael@0: dump("...that error was expected.\n\n"); michael@0: } else if (aActualError) { michael@0: throw "Test threw unexpected error: " + aActualError; michael@0: } michael@0: }, michael@0: michael@0: michael@0: /* michael@0: * checkStorageData michael@0: * michael@0: * Compare info from component to what we expected. michael@0: */ michael@0: checkStorageData : function (storage, ref_disabledHosts, ref_logins) { michael@0: this.checkLogins(ref_logins, storage.getAllLogins()); michael@0: this.checkDisabledHosts(ref_disabledHosts, storage.getAllDisabledHosts()); michael@0: }, michael@0: michael@0: /* michael@0: * checkLogins michael@0: * michael@0: * Check values of the logins list. michael@0: */ michael@0: checkLogins : function (expectedLogins, actualLogins) { michael@0: do_check_eq(expectedLogins.length, actualLogins.length); michael@0: for (let i = 0; i < expectedLogins.length; i++) { michael@0: let found = false; michael@0: for (let j = 0; !found && j < actualLogins.length; j++) { michael@0: found = expectedLogins[i].equals(actualLogins[j]); michael@0: } michael@0: do_check_true(found); michael@0: } michael@0: }, michael@0: michael@0: /* michael@0: * checkDisabledHosts michael@0: * michael@0: * Check values of the disabled list. michael@0: */ michael@0: checkDisabledHosts : function (expectedHosts, actualHosts) { michael@0: do_check_eq(expectedHosts.length, actualHosts.length); michael@0: for (let i = 0; i < expectedHosts.length; i++) { michael@0: let found = false; michael@0: for (let j = 0; !found && j < actualHosts.length; j++) { michael@0: found = (expectedHosts[i] == actualHosts[j]); michael@0: } michael@0: do_check_true(found); michael@0: } michael@0: }, michael@0: michael@0: /* michael@0: * countLinesInFile michael@0: * michael@0: * Counts the number of lines in the specified file. michael@0: */ michael@0: countLinesInFile : function (aPathName, aFileName) { michael@0: var inputFile = Cc["@mozilla.org/file/local;1"]. michael@0: createInstance(Ci.nsILocalFile); michael@0: inputFile.initWithPath(aPathName); michael@0: inputFile.append(aFileName); michael@0: if (inputFile.fileSize == 0) michael@0: return 0; michael@0: michael@0: var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]. michael@0: createInstance(Ci.nsIFileInputStream); michael@0: // init the stream as RD_ONLY, -1 == default permissions. michael@0: inputStream.init(inputFile, 0x01, -1, null); michael@0: var lineStream = inputStream.QueryInterface(Ci.nsILineInputStream); michael@0: michael@0: var line = { value : null }; michael@0: var lineCount = 1; // Empty files were dealt with above. michael@0: while (lineStream.readLine(line)) michael@0: lineCount++; michael@0: michael@0: return lineCount; michael@0: }, michael@0: michael@0: newStorage : function () { michael@0: michael@0: var storage = Cc["@mozilla.org/login-manager/storage/mozStorage;1"]. michael@0: createInstance(Ci.nsILoginManagerStorage); michael@0: if (!storage) michael@0: throw "Couldn't create storage instance."; michael@0: return storage; michael@0: }, michael@0: michael@0: openDB : function (filename) { michael@0: // nsIFile for the specified filename, in the profile dir. michael@0: var dbfile = PROFDIR.clone(); michael@0: dbfile.append(filename); michael@0: michael@0: var ss = Cc["@mozilla.org/storage/service;1"]. michael@0: getService(Ci.mozIStorageService); michael@0: var dbConnection = ss.openDatabase(dbfile); michael@0: michael@0: return dbConnection; michael@0: }, michael@0: michael@0: deleteFile : function (pathname, filename) { michael@0: var file = Cc["@mozilla.org/file/local;1"]. michael@0: createInstance(Ci.nsILocalFile); michael@0: file.initWithPath(pathname); michael@0: file.append(filename); michael@0: // Suppress failures, this happens in the mozstorage tests on Windows michael@0: // because the module may still be holding onto the DB. (We don't michael@0: // have a way to explicitly shutdown/GC the module). michael@0: try { michael@0: if (file.exists()) michael@0: file.remove(false); michael@0: } catch (e) {} michael@0: }, michael@0: michael@0: // Copies a file from our test data directory to the unit test profile. michael@0: copyFile : function (filename) { michael@0: var file = DATADIR.clone(); michael@0: file.append(filename); michael@0: michael@0: var profileFile = PROFDIR.clone(); michael@0: profileFile.append(filename); michael@0: michael@0: if (profileFile.exists()) michael@0: profileFile.remove(false); michael@0: michael@0: file.copyTo(PROFDIR, filename); michael@0: }, michael@0: michael@0: // Returns true if the timestamp is within 30 seconds of now. michael@0: is_about_now : function (timestamp) { michael@0: var delta = Math.abs(timestamp - Date.now()); michael@0: var seconds = 30 * 1000; michael@0: return delta < seconds; michael@0: } michael@0: }; michael@0: michael@0: // nsIFiles... michael@0: var PROFDIR = do_get_profile(); michael@0: var DATADIR = do_get_file("data/"); michael@0: // string versions... michael@0: var OUTDIR = PROFDIR.path; michael@0: michael@0: // Copy key3.db into the profile used for the unit tests. Need this so we can michael@0: // decrypt the encrypted logins stored in the various tests inputs. michael@0: LoginTest.copyFile("key3.db");