toolkit/components/passwordmgr/test/unit/head_common.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/passwordmgr/test/unit/head_common.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,233 @@
     1.4 +// Copied from components/places/tests/unit/head_bookmarks.js
     1.5 +
     1.6 +const NS_APP_USER_PROFILE_50_DIR = "ProfD";
     1.7 +const Ci = Components.interfaces;
     1.8 +const Cc = Components.classes;
     1.9 +const Cr = Components.results;
    1.10 +
    1.11 +// Various functions common to the tests.
    1.12 +const LoginTest = {
    1.13 +
    1.14 +  /*
    1.15 +   * initStorage
    1.16 +   *
    1.17 +   */
    1.18 +  initStorage : function (aOutputPathName, aOutputFileName, aExpectedError) {
    1.19 +    var err = null;
    1.20 +
    1.21 +    var newStorage = this.newStorage();
    1.22 +
    1.23 +    var outputFile = null;
    1.24 +    if (aOutputFileName) {
    1.25 +        var outputFile = Cc["@mozilla.org/file/local;1"].
    1.26 +                         createInstance(Ci.nsILocalFile);
    1.27 +        outputFile.initWithPath(aOutputPathName);
    1.28 +        outputFile.append(aOutputFileName);
    1.29 +
    1.30 +        // Delete any existing output file. This is primarily for Windows,
    1.31 +        // where we can't rely on having deleted files in the last test run.
    1.32 +        if (outputFile.exists())
    1.33 +            outputFile.remove(false);
    1.34 +    }
    1.35 +
    1.36 +    try {
    1.37 +        newStorage.initWithFile(outputFile);
    1.38 +    } catch (e) {
    1.39 +        err = e;
    1.40 +    }
    1.41 +
    1.42 +    this.checkExpectedError(aExpectedError, err);
    1.43 +
    1.44 +    return newStorage;
    1.45 +  },
    1.46 +
    1.47 +
    1.48 +  /*
    1.49 +   * reloadStorage
    1.50 +   *
    1.51 +   * Reinitialize a storage module with the specified input.
    1.52 +   */
    1.53 +  reloadStorage : function (aInputPathName, aInputFileName, aExpectedError) {
    1.54 +    var err = null;
    1.55 +    var newStorage = this.newStorage();
    1.56 +
    1.57 +    var inputFile = null;
    1.58 +    if (aInputFileName) {
    1.59 +        var inputFile  = Cc["@mozilla.org/file/local;1"].
    1.60 +                         createInstance(Ci.nsILocalFile);
    1.61 +        inputFile.initWithPath(aInputPathName);
    1.62 +        inputFile.append(aInputFileName);
    1.63 +    }
    1.64 +
    1.65 +    try {
    1.66 +        newStorage.initWithFile(inputFile);
    1.67 +    } catch (e) {
    1.68 +        err = e;
    1.69 +    }
    1.70 +
    1.71 +    if (aExpectedError)
    1.72 +        this.checkExpectedError(aExpectedError, err);
    1.73 +    else
    1.74 +        do_check_true(err == null);
    1.75 +
    1.76 +    return newStorage;
    1.77 +  },
    1.78 +
    1.79 +
    1.80 +  /*
    1.81 +   * checkExpectedError
    1.82 +   *
    1.83 +   * Checks to see if a thrown error was expected or not, and if it
    1.84 +   * matches the expected value.
    1.85 +   */
    1.86 +  checkExpectedError : function (aExpectedError, aActualError) {
    1.87 +    if (aExpectedError) {
    1.88 +        if (!aActualError)
    1.89 +            throw "Test didn't throw as expected (" + aExpectedError + ")";
    1.90 +
    1.91 +        if (!aExpectedError.test(aActualError))
    1.92 +            throw "Test threw (" + aActualError + "), not (" + aExpectedError;
    1.93 +
    1.94 +        // We got the expected error, so make a note in the test log.
    1.95 +        dump("...that error was expected.\n\n");
    1.96 +    } else if (aActualError) {
    1.97 +        throw "Test threw unexpected error: " + aActualError;
    1.98 +    }
    1.99 +  },
   1.100 +
   1.101 +
   1.102 +  /*
   1.103 +   * checkStorageData
   1.104 +   *
   1.105 +   * Compare info from component to what we expected.
   1.106 +   */
   1.107 +  checkStorageData : function (storage, ref_disabledHosts, ref_logins) {
   1.108 +    this.checkLogins(ref_logins, storage.getAllLogins());
   1.109 +    this.checkDisabledHosts(ref_disabledHosts, storage.getAllDisabledHosts());
   1.110 +  },
   1.111 +
   1.112 +  /*
   1.113 +   * checkLogins
   1.114 +   *
   1.115 +   * Check values of the logins list.
   1.116 +   */
   1.117 +  checkLogins : function (expectedLogins, actualLogins) {
   1.118 +    do_check_eq(expectedLogins.length, actualLogins.length);
   1.119 +    for (let i = 0; i < expectedLogins.length; i++) {
   1.120 +        let found = false;
   1.121 +        for (let j = 0; !found && j < actualLogins.length; j++) {
   1.122 +            found = expectedLogins[i].equals(actualLogins[j]);
   1.123 +        }
   1.124 +        do_check_true(found);
   1.125 +    }
   1.126 +  },
   1.127 +
   1.128 +  /*
   1.129 +   * checkDisabledHosts
   1.130 +   *
   1.131 +   * Check values of the disabled list.
   1.132 +   */
   1.133 +  checkDisabledHosts : function (expectedHosts, actualHosts) {
   1.134 +    do_check_eq(expectedHosts.length, actualHosts.length);
   1.135 +    for (let i = 0; i < expectedHosts.length; i++) {
   1.136 +        let found = false;
   1.137 +        for (let j = 0; !found && j < actualHosts.length; j++) {
   1.138 +            found = (expectedHosts[i] == actualHosts[j]);
   1.139 +        }
   1.140 +        do_check_true(found);
   1.141 +    }
   1.142 +  },
   1.143 +
   1.144 +  /*
   1.145 +   * countLinesInFile
   1.146 +   *
   1.147 +   * Counts the number of lines in the specified file.
   1.148 +   */
   1.149 +  countLinesInFile : function (aPathName,  aFileName) {
   1.150 +    var inputFile  = Cc["@mozilla.org/file/local;1"].
   1.151 +                     createInstance(Ci.nsILocalFile);
   1.152 +    inputFile.initWithPath(aPathName);
   1.153 +    inputFile.append(aFileName);
   1.154 +    if (inputFile.fileSize == 0)
   1.155 +      return 0;
   1.156 +
   1.157 +    var inputStream = Cc["@mozilla.org/network/file-input-stream;1"].
   1.158 +                      createInstance(Ci.nsIFileInputStream);
   1.159 +    // init the stream as RD_ONLY, -1 == default permissions.
   1.160 +    inputStream.init(inputFile, 0x01, -1, null);
   1.161 +    var lineStream = inputStream.QueryInterface(Ci.nsILineInputStream);
   1.162 +
   1.163 +    var line = { value : null };
   1.164 +    var lineCount = 1; // Empty files were dealt with above.
   1.165 +    while (lineStream.readLine(line))
   1.166 +        lineCount++;
   1.167 +
   1.168 +    return lineCount;
   1.169 +  },
   1.170 +
   1.171 +  newStorage : function () {
   1.172 +
   1.173 +    var storage = Cc["@mozilla.org/login-manager/storage/mozStorage;1"].
   1.174 +                  createInstance(Ci.nsILoginManagerStorage);
   1.175 +    if (!storage)
   1.176 +      throw "Couldn't create storage instance.";
   1.177 +    return storage;
   1.178 +  },
   1.179 +
   1.180 +  openDB : function (filename) {
   1.181 +    // nsIFile for the specified filename, in the profile dir.
   1.182 +    var dbfile = PROFDIR.clone();
   1.183 +    dbfile.append(filename);
   1.184 +
   1.185 +    var ss = Cc["@mozilla.org/storage/service;1"].
   1.186 +             getService(Ci.mozIStorageService);
   1.187 +    var dbConnection = ss.openDatabase(dbfile);
   1.188 +
   1.189 +    return dbConnection;
   1.190 +  },
   1.191 +
   1.192 +  deleteFile : function (pathname, filename) {
   1.193 +    var file = Cc["@mozilla.org/file/local;1"].
   1.194 +               createInstance(Ci.nsILocalFile);
   1.195 +    file.initWithPath(pathname);
   1.196 +    file.append(filename);
   1.197 +    // Suppress failures, this happens in the mozstorage tests on Windows
   1.198 +    // because the module may still be holding onto the DB. (We don't
   1.199 +    // have a way to explicitly shutdown/GC the module).
   1.200 +    try {
   1.201 +      if (file.exists())
   1.202 +        file.remove(false);
   1.203 +    } catch (e) {}
   1.204 +  },
   1.205 +
   1.206 +  // Copies a file from our test data directory to the unit test profile.
   1.207 +  copyFile : function (filename) {
   1.208 +    var file = DATADIR.clone();
   1.209 +    file.append(filename);
   1.210 +
   1.211 +    var profileFile = PROFDIR.clone();
   1.212 +    profileFile.append(filename);
   1.213 +
   1.214 +    if (profileFile.exists())
   1.215 +        profileFile.remove(false);
   1.216 +
   1.217 +    file.copyTo(PROFDIR, filename);
   1.218 +  },
   1.219 +
   1.220 +  // Returns true if the timestamp is within 30 seconds of now.
   1.221 +  is_about_now : function (timestamp) {
   1.222 +    var delta = Math.abs(timestamp - Date.now());
   1.223 +    var seconds = 30 * 1000;
   1.224 +    return delta < seconds;
   1.225 +  }
   1.226 +};
   1.227 +
   1.228 +// nsIFiles...
   1.229 +var PROFDIR = do_get_profile();
   1.230 +var DATADIR = do_get_file("data/");
   1.231 +// string versions...
   1.232 +var OUTDIR = PROFDIR.path;
   1.233 +
   1.234 +// Copy key3.db into the profile used for the unit tests. Need this so we can
   1.235 +// decrypt the encrypted logins stored in the various tests inputs.
   1.236 +LoginTest.copyFile("key3.db");

mercurial