Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
1 /*
2 * $_
3 *
4 * Returns the element with the specified |name| attribute.
5 */
6 function $_(formNum, name) {
7 var form = document.getElementById("form" + formNum);
8 if (!form) {
9 logWarning("$_ couldn't find requested form " + formNum);
10 return null;
11 }
13 var element = form.elements.namedItem(name);
14 if (!element) {
15 logWarning("$_ couldn't find requested element " + name);
16 return null;
17 }
19 // Note that namedItem is a bit stupid, and will prefer an
20 // |id| attribute over a |name| attribute when looking for
21 // the element. Login Mananger happens to use .namedItem
22 // anyway, but let's rigorously check it here anyway so
23 // that we don't end up with tests that mistakenly pass.
25 if (element.getAttribute("name") != name) {
26 logWarning("$_ got confused.");
27 return null;
28 }
30 return element;
31 }
34 /*
35 * checkForm
36 *
37 * Check a form for expected values. If an argument is null, a field's
38 * expected value will be the default value.
39 *
40 * <form id="form#">
41 * checkForm(#, "foo");
42 */
43 function checkForm(formNum, val1, val2, val3) {
44 var e, form = document.getElementById("form" + formNum);
45 ok(form, "Locating form " + formNum);
47 var numToCheck = arguments.length - 1;
49 if (!numToCheck--)
50 return;
51 e = form.elements[0];
52 if (val1 == null)
53 is(e.value, e.defaultValue, "Test default value of field " + e.name +
54 " in form " + formNum);
55 else
56 is(e.value, val1, "Test value of field " + e.name +
57 " in form " + formNum);
60 if (!numToCheck--)
61 return;
62 e = form.elements[1];
63 if (val2 == null)
64 is(e.value, e.defaultValue, "Test default value of field " + e.name +
65 " in form " + formNum);
66 else
67 is(e.value, val2, "Test value of field " + e.name +
68 " in form " + formNum);
71 if (!numToCheck--)
72 return;
73 e = form.elements[2];
74 if (val3 == null)
75 is(e.value, e.defaultValue, "Test default value of field " + e.name +
76 " in form " + formNum);
77 else
78 is(e.value, val3, "Test value of field " + e.name +
79 " in form " + formNum);
81 }
84 /*
85 * checkUnmodifiedForm
86 *
87 * Check a form for unmodified values from when page was loaded.
88 *
89 * <form id="form#">
90 * checkUnmodifiedForm(#);
91 */
92 function checkUnmodifiedForm(formNum) {
93 var form = document.getElementById("form" + formNum);
94 ok(form, "Locating form " + formNum);
96 for (var i = 0; i < form.elements.length; i++) {
97 var ele = form.elements[i];
99 // No point in checking form submit/reset buttons.
100 if (ele.type == "submit" || ele.type == "reset")
101 continue;
103 is(ele.value, ele.defaultValue, "Test to default value of field " +
104 ele.name + " in form " + formNum);
105 }
106 }
109 // Mochitest gives us a sendKey(), but it's targeted to a specific element.
110 // This basically sends an untargeted key event, to whatever's focused.
111 function doKey(aKey, modifier) {
112 var keyName = "DOM_VK_" + aKey.toUpperCase();
113 var key = KeyEvent[keyName];
115 // undefined --> null
116 if (!modifier)
117 modifier = null;
119 // Window utils for sending fake sey events.
120 var wutils = SpecialPowers.wrap(window).
121 QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
122 getInterface(SpecialPowers.Ci.nsIDOMWindowUtils);
124 if (wutils.sendKeyEvent("keydown", key, 0, modifier)) {
125 wutils.sendKeyEvent("keypress", key, 0, modifier);
126 }
127 wutils.sendKeyEvent("keyup", key, 0, modifier);
128 }
130 // Init with a common login
131 function commonInit() {
132 var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"].
133 getService(SpecialPowers.Ci.nsILoginManager);
134 ok(pwmgr != null, "Access LoginManager");
137 // Check that initial state has no logins
138 var logins = pwmgr.getAllLogins();
139 if (logins.length) {
140 //todo(false, "Warning: wasn't expecting logins to be present.");
141 pwmgr.removeAllLogins();
142 }
143 var disabledHosts = pwmgr.getAllDisabledHosts();
144 if (disabledHosts.length) {
145 //todo(false, "Warning: wasn't expecting disabled hosts to be present.");
146 for (var host of disabledHosts)
147 pwmgr.setLoginSavingEnabled(host, true);
148 }
150 // Add a login that's used in multiple tests
151 var login = SpecialPowers.Cc["@mozilla.org/login-manager/loginInfo;1"].
152 createInstance(SpecialPowers.Ci.nsILoginInfo);
153 login.init("http://mochi.test:8888", "http://mochi.test:8888", null,
154 "testuser", "testpass", "uname", "pword");
155 pwmgr.addLogin(login);
157 // Last sanity check
158 logins = pwmgr.getAllLogins();
159 is(logins.length, 1, "Checking for successful init login");
160 disabledHosts = pwmgr.getAllDisabledHosts();
161 is(disabledHosts.length, 0, "Checking for no disabled hosts");
162 }
164 const masterPassword = "omgsecret!";
166 function enableMasterPassword() {
167 setMasterPassword(true);
168 }
170 function disableMasterPassword() {
171 setMasterPassword(false);
172 }
174 function setMasterPassword(enable) {
175 var oldPW, newPW;
176 if (enable) {
177 oldPW = "";
178 newPW = masterPassword;
179 } else {
180 oldPW = masterPassword;
181 newPW = "";
182 }
183 // Set master password. Note that this does not log you in, so the next
184 // invocation of pwmgr can trigger a MP prompt.
186 var pk11db = Cc["@mozilla.org/security/pk11tokendb;1"].
187 getService(Ci.nsIPK11TokenDB)
188 var token = pk11db.findTokenByName("");
189 ok(true, "change from " + oldPW + " to " + newPW);
190 token.changePassword(oldPW, newPW);
191 }
193 function logoutMasterPassword() {
194 var sdr = Cc["@mozilla.org/security/sdr;1"].
195 getService(Ci.nsISecretDecoderRing);
196 sdr.logoutAndTeardown();
197 }
199 function dumpLogins(pwmgr) {
200 var logins = pwmgr.getAllLogins();
201 ok(true, "----- dumpLogins: have " + logins.length + " logins. -----");
202 for (var i = 0; i < logins.length; i++)
203 dumpLogin("login #" + i + " --- ", logins[i]);
204 }
206 function dumpLogin(label, login) {
207 loginText = "";
208 loginText += "host: ";
209 loginText += login.hostname;
210 loginText += " / formURL: ";
211 loginText += login.formSubmitURL;
212 loginText += " / realm: ";
213 loginText += login.httpRealm;
214 loginText += " / user: ";
215 loginText += login.username;
216 loginText += " / pass: ";
217 loginText += login.password;
218 loginText += " / ufield: ";
219 loginText += login.usernameField;
220 loginText += " / pfield: ";
221 loginText += login.passwordField;
222 ok(true, label + loginText);
223 }