--- a/toolkit/components/passwordmgr/test/unit/test_storage_mozStorage.js
+++ b/toolkit/components/passwordmgr/test/unit/test_storage_mozStorage.js
@@ -1,20 +1,21 @@
/*
* This test interfaces directly with the mozStorage password storage module,
* bypassing the normal password manager usage.
*/
const ENCTYPE_BASE64 = 0;
const ENCTYPE_SDR = 1;
+const PERMISSION_SAVE_LOGINS = "login-saving";
// Current schema version used by storage-mozStorage.js. This will need to be
// kept in sync with the version there (or else the tests fail).
-const CURRENT_SCHEMA = 5;
+const CURRENT_SCHEMA = 6;
function* copyFile(aLeafName)
{
yield OS.File.copy(OS.Path.join(do_get_file("data").path, aLeafName),
OS.Path.join(OS.Constants.Path.profileDir, aLeafName));
}
function openDB(aLeafName)
@@ -56,20 +57,44 @@ function reloadStorage(aInputPathName, a
.initWithFile(inputFile);
return storage;
}
function checkStorageData(storage, ref_disabledHosts, ref_logins)
{
LoginTestUtils.assertLoginListsEqual(storage.getAllLogins(), ref_logins);
- LoginTestUtils.assertDisabledHostsEqual(storage.getAllDisabledHosts(),
+ LoginTestUtils.assertDisabledHostsEqual(getAllDisabledHostsFromPermissionManager(),
ref_disabledHosts);
}
+function getAllDisabledHostsFromPermissionManager() {
+ let disabledHosts = [];
+ let enumerator = Services.perms.enumerator;
+
+ while (enumerator.hasMoreElements()) {
+ let perm = enumerator.getNext();
+ if (perm.type == PERMISSION_SAVE_LOGINS && perm.capability == Services.perms.DENY_ACTION) {
+ disabledHosts.push(perm.principal.URI.prePath);
+ }
+ }
+
+ return disabledHosts;
+}
+
+function setLoginSavingEnabled(origin, enabled) {
+ let uri = Services.io.newURI(origin, null, null);
+
+ if (enabled) {
+ Services.perms.remove(uri, PERMISSION_SAVE_LOGINS);
+ } else {
+ Services.perms.add(uri, PERMISSION_SAVE_LOGINS, Services.perms.DENY_ACTION);
+ }
+}
+
add_task(function* test_execute()
{
const OUTDIR = OS.Constants.Path.profileDir;
try {
var isGUID = /^\{[0-9a-f\d]{8}-[0-9a-f\d]{4}-[0-9a-f\d]{4}-[0-9a-f\d]{4}-[0-9a-f\d]{12}\}$/;
@@ -84,16 +109,27 @@ function getGUIDforID(conn, id) {
function getEncTypeForID(conn, id) {
var stmt = conn.createStatement("SELECT encType from moz_logins WHERE id = " + id);
stmt.executeStep();
var encType = stmt.row.encType;
stmt.finalize();
return encType;
}
+function getAllDisabledHostsFromMozStorage(conn) {
+ let disabledHosts = [];
+ let stmt = conn.createStatement("SELECT hostname from moz_disabledHosts");
+
+ while (stmt.executeStep()) {
+ disabledHosts.push(stmt.row.hostname);
+ }
+
+ return disabledHosts;
+}
+
var storage;
var dbConnection;
var testnum = 0;
var testdesc = "Setup of nsLoginInfo test-users";
var nsLoginInfo = new Components.Constructor(
"@mozilla.org/login-manager/loginInfo;1",
Components.interfaces.nsILoginInfo);
do_check_true(nsLoginInfo != null);
@@ -124,16 +160,17 @@ testdesc = "Test downgrade from v999 sto
yield* copyFile("signons-v999.sqlite");
// Verify the schema version in the test file.
dbConnection = openDB("signons-v999.sqlite");
do_check_eq(999, dbConnection.schemaVersion);
dbConnection.close();
storage = reloadStorage(OUTDIR, "signons-v999.sqlite");
+setLoginSavingEnabled("https://disabled.net", false);
checkStorageData(storage, ["https://disabled.net"], [testuser1]);
// Check to make sure we downgraded the schema version.
dbConnection = openDB("signons-v999.sqlite");
do_check_eq(CURRENT_SCHEMA, dbConnection.schemaVersion);
dbConnection.close();
deleteFile(OUTDIR, "signons-v999.sqlite");
@@ -285,16 +322,19 @@ testdesc = "Test upgrade from v3->v4 sto
yield* copyFile("signons-v3.sqlite");
// Sanity check the test file.
dbConnection = openDB("signons-v3.sqlite");
do_check_eq(3, dbConnection.schemaVersion);
storage = reloadStorage(OUTDIR, "signons-v3.sqlite");
do_check_eq(CURRENT_SCHEMA, dbConnection.schemaVersion);
+// Remove old entry from permission manager.
+setLoginSavingEnabled("https://disabled.net", true);
+
// Check that timestamps and counts were initialized correctly
checkStorageData(storage, [], [testuser1, testuser2]);
var logins = storage.getAllLogins();
for (var i = 0; i < 2; i++) {
do_check_true(logins[i] instanceof Ci.nsILoginMetaInfo);
do_check_eq(1, logins[i].timesUsed);
LoginTestUtils.assertTimeIsAboutNow(logins[i].timeCreated);
@@ -366,33 +406,70 @@ yield copyFile("signons-v4v5.sqlite");
dbConnection = openDB("signons-v4v5.sqlite");
do_check_eq(4, dbConnection.schemaVersion);
do_check_true(dbConnection.tableExists("moz_deleted_logins"));
storage = reloadStorage(OUTDIR, "signons-v4v5.sqlite");
do_check_eq(CURRENT_SCHEMA, dbConnection.schemaVersion);
do_check_true(dbConnection.tableExists("moz_deleted_logins"));
+/* ========== 11 ========== */
+testnum++;
+testdesc = "Test upgrade from v5->v6 storage";
-/* ========== 11 ========== */
+yield* copyFile("signons-v5v6.sqlite");
+
+// Sanity check the test file.
+dbConnection = openDB("signons-v5v6.sqlite");
+do_check_eq(5, dbConnection.schemaVersion);
+do_check_true(dbConnection.tableExists("moz_disabledHosts"));
+
+// Initial disabled hosts inside signons-v5v6.sqlite
+var disabledHosts = [
+ "http://disabled1.example.com",
+ "http://大.net",
+ "http://xn--19g.com"
+];
+
+LoginTestUtils.assertDisabledHostsEqual(disabledHosts, getAllDisabledHostsFromMozStorage(dbConnection));
+
+// Reload storage
+storage = reloadStorage(OUTDIR, "signons-v5v6.sqlite");
+do_check_eq(CURRENT_SCHEMA, dbConnection.schemaVersion);
+
+// moz_disabledHosts should now be empty after migration.
+LoginTestUtils.assertDisabledHostsEqual([], getAllDisabledHostsFromMozStorage(dbConnection));
+
+// Get all the other hosts currently saved in the permission manager.
+let hostsInPermissionManager = getAllDisabledHostsFromPermissionManager();
+
+// All disabledHosts should have migrated to the permission manager
+LoginTestUtils.assertDisabledHostsEqual(disabledHosts, hostsInPermissionManager);
+
+// Remove all disabled hosts from the permission manager before test ends
+for (let host of disabledHosts) {
+ setLoginSavingEnabled(host, true);
+}
+
+/* ========== 12 ========== */
testnum++;
testdesc = "Create nsILoginInfo instances for testing with"
testuser1 = new nsLoginInfo;
testuser1.init("http://dummyhost.mozilla.org", "", null,
"dummydude", "itsasecret", "put_user_here", "put_pw_here");
/*
* ---------------------- DB Corruption ----------------------
* Try to initialize with a corrupt database file. This should create a backup
* file, then upon next use create a new database file.
*/
-/* ========== 12 ========== */
+/* ========== 13 ========== */
testnum++;
testdesc = "Corrupt database and backup"
const filename = "signons-c.sqlite";
const filepath = OS.Path.join(OS.Constants.Path.profileDir, filename);
yield OS.File.copy(do_get_file("data/corruptDB.sqlite").path, filepath);