Bug 1058438 - Add test for disabledHost migration to sqlite v6 in Fennec. r=MattN draft
authorSaad Quadri <saad@saadquadri.com>
Tue, 09 Aug 2016 10:14:56 -0700
changeset 398745 9c6ec4ff8b286f79187633134b53819a701ae186
parent 398744 92c556c14aeee988d30985ac162493e0212b2909
child 527732 db18c7490d971ac0644d1908cce1b11bbc88e184
push id25613
push usersaad@saadquadri.com
push dateTue, 09 Aug 2016 17:16:57 +0000
reviewersMattN
bugs1058438
milestone51.0a1
Bug 1058438 - Add test for disabledHost migration to sqlite v6 in Fennec. r=MattN MozReview-Commit-ID: DRV3zmXc9yJ
toolkit/components/passwordmgr/test/unit/data/signons-v5v6.sqlite
toolkit/components/passwordmgr/test/unit/test_storage_mozStorage.js
new file mode 100644
index 0000000000000000000000000000000000000000..eb4ee6d01e0d9c89d4447515eab1a7beffc3c89c
GIT binary patch
literal 327680
zc%1Fs&2Af27zW^R+{Cq|EQ&-e*wk53OGzxN3)D&oLh3?<N!v7$TCs?nct|Y#M?8Tv
zw2f01+(@Nf0o(%;J65b<Q?3I~>NKf;TCuV!J&!bF`<!pid5`C0vh=;X<ysWxzHRL_
zDpBrQ&<zra;ImvV2!h$(kqOSEd&h-ypA)@fI(W0$VBz1)?32qu`Y;_l`S9^akN<qM
z|7iNB+TmY^KOCmtay0+|0000000000000000000000000000000002MBz>1oWj~oq
z98cGp)$pLvI^3+cc52PeX4u?bf7lK;2ih~sYo(?2Qf_7SX6b%zTz&5L>bbtF{l0wV
z;!O7Axx~Z4xpwwz)xn9<<0d*Q9+)NW$RE$9QrUaG1zj7QWVh9cnw3Vlxf@08wXjle
z3{H7*+?3-k9SCq*HlKS{mp}esCYAlBmuGowp7=+&@9l3jYSG48c`#Gzt(it#J~r2D
zwfW|BD*I_KT6%1>fsUEChI!NU`QyTcRJK@5JWWTHt$G-jRKt20h1GuPaLaUm#`UGo
z%cb0Kf9`6nnj5mRx?cLCw3fTGw(|AT+JoFzr3drz?ij3FFWq0Ck7|wZ=F33`2go1i
zr&8HMA@MYEmbli5-&Viut*SHBkQ~l?zI$ZcQE5-Vvw^U=jn$RA8>RRQUcSA0tGu$j
zo?9#3DK9OT@=M8?>_#CGoZN!W_w`y7ZdUfA*2(A1p(vZhp{Dp(-Y@*Tok(Tp<`TbL
zdbQ-UoAdJB)X*aT@68yoyqD`+z1=%D%H{c2e?fz{>$NR>zGJ@=?!{x?s)hCH$T8cM
zPUpwgUUhs$??JWqu-)4U8~wq?SKR3jHdHZoqxGMHqrRMzO}M-lRz_^XlfH7L6ZJAx
zNA;a1S>CNQcX}n~`#R?@I=8WB<7a<&vj2Jj000000000000000000000000000000
z00000003apW_w5S=-1%r@1s9@F8}}l00000000000000000000000000000000000
z@a}j&nGA}%QPjRsC>%5w7K+z*7PniCOH;`p-*0~L?D=9djIJb-L8;$Vt#v9}^{`r8
z3=b-ec0G)*$@GRx9{m;^{d4qt?*#w=00000000000000000000000000000000000
z0Nx?*CG&~hC~Ds*6ka@gzSs<-smsaYY12V-VWD_^XK}mLNM1>n(*5RYty9^mht=X@
zcu;Ay>tS3l8xPl=@NxhE000000000000000000000000000000000000ARv0@u@rE
z5C8xG00000000000000000000000000000000000FkzYa)SYk$000000000000000
z000000000000000000000001(uuOdFPB;Vr000000000000000000000000000000
z00000002x_CO&m190C9U00000000000000000000000000000000000046LGpSlwc
m0RR91000000000000000000000000000000000006ZRi_Q8}3a
--- 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);