|
1 /* Any copyright is dedicated to the public domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 // Test that auth prompt works. |
|
5 "use strict"; |
|
6 |
|
7 SimpleTest.waitForExplicitFinish(); |
|
8 browserElementTestHelpers.setEnabledPref(true); |
|
9 browserElementTestHelpers.addPermission(); |
|
10 |
|
11 function testFail(msg) { |
|
12 ok(false, JSON.stringify(msg)); |
|
13 } |
|
14 |
|
15 var iframe; |
|
16 |
|
17 function runTest() { |
|
18 iframe = document.createElement('iframe'); |
|
19 SpecialPowers.wrap(iframe).mozbrowser = true; |
|
20 document.body.appendChild(iframe); |
|
21 |
|
22 // Wait for the initial load to finish, then navigate the page, then start test |
|
23 // by loading SJS with http 401 response. |
|
24 iframe.addEventListener('mozbrowserloadend', function loadend() { |
|
25 iframe.removeEventListener('mozbrowserloadend', loadend); |
|
26 iframe.addEventListener('mozbrowserusernameandpasswordrequired', testHttpAuthCancel); |
|
27 SimpleTest.executeSoon(function() { |
|
28 // Use absolute path because we need to specify host. |
|
29 iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs'; |
|
30 }); |
|
31 }); |
|
32 } |
|
33 |
|
34 function testHttpAuthCancel(e) { |
|
35 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testHttpAuthCancel); |
|
36 // Will cancel authentication, but prompt should not be shown again. Instead, |
|
37 // we will be led to fail message |
|
38 iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail); |
|
39 iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) { |
|
40 iframe.removeEventListener("mozbrowsertitlechange", onTitleChange); |
|
41 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail); |
|
42 is(e.detail, 'http auth failed', 'expected authentication to fail'); |
|
43 iframe.addEventListener('mozbrowserusernameandpasswordrequired', testHttpAuth); |
|
44 SimpleTest.executeSoon(function() { |
|
45 // Use absolute path because we need to specify host. |
|
46 iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs'; |
|
47 }); |
|
48 }); |
|
49 |
|
50 is(e.detail.realm, 'http_realm', 'expected realm matches'); |
|
51 is(e.detail.host, 'http://test', 'expected host matches'); |
|
52 e.preventDefault(); |
|
53 |
|
54 SimpleTest.executeSoon(function() { |
|
55 e.detail.cancel(); |
|
56 }); |
|
57 } |
|
58 |
|
59 function testHttpAuth(e) { |
|
60 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testHttpAuth); |
|
61 |
|
62 // Will authenticate with correct password, prompt should not be |
|
63 // called again. |
|
64 iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail); |
|
65 iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) { |
|
66 iframe.removeEventListener("mozbrowsertitlechange", onTitleChange); |
|
67 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail); |
|
68 is(e.detail, 'http auth success', 'expect authentication to succeed'); |
|
69 SimpleTest.executeSoon(testAuthJarNoInterfere); |
|
70 }); |
|
71 |
|
72 is(e.detail.realm, 'http_realm', 'expected realm matches'); |
|
73 is(e.detail.host, 'http://test', 'expected host matches'); |
|
74 e.preventDefault(); |
|
75 |
|
76 SimpleTest.executeSoon(function() { |
|
77 e.detail.authenticate("httpuser", "httppass"); |
|
78 }); |
|
79 } |
|
80 |
|
81 function testAuthJarNoInterfere(e) { |
|
82 var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1'] |
|
83 .getService(SpecialPowers.Ci.nsIHttpAuthManager); |
|
84 var secMan = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"] |
|
85 .getService(SpecialPowers.Ci.nsIScriptSecurityManager); |
|
86 var ioService = SpecialPowers.Cc["@mozilla.org/network/io-service;1"] |
|
87 .getService(SpecialPowers.Ci.nsIIOService); |
|
88 var uri = ioService.newURI("http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs", null, null); |
|
89 |
|
90 // Set a bunch of auth data that should not conflict with the correct auth data already |
|
91 // stored in the cache. |
|
92 var principal = secMan.getAppCodebasePrincipal(uri, 1, false); |
|
93 authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm', |
|
94 'tests/dom/browser-element/mochitest/file_http_401_response.sjs', |
|
95 '', 'httpuser', 'wrongpass', false, principal); |
|
96 principal = secMan.getAppCodebasePrincipal(uri, 1, true); |
|
97 authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm', |
|
98 'tests/dom/browser-element/mochitest/file_http_401_response.sjs', |
|
99 '', 'httpuser', 'wrongpass', false, principal); |
|
100 principal = secMan.getAppCodebasePrincipal(uri, secMan.NO_APP_ID, false); |
|
101 authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm', |
|
102 'tests/dom/browser-element/mochitest/file_http_401_response.sjs', |
|
103 '', 'httpuser', 'wrongpass', false, principal); |
|
104 |
|
105 // Will authenticate with correct password, prompt should not be |
|
106 // called again. |
|
107 iframe.addEventListener("mozbrowserusernameandpasswordrequired", testFail); |
|
108 iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) { |
|
109 iframe.removeEventListener("mozbrowsertitlechange", onTitleChange); |
|
110 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", testFail); |
|
111 is(e.detail, 'http auth success', 'expected authentication success'); |
|
112 SimpleTest.executeSoon(testAuthJarInterfere); |
|
113 }); |
|
114 |
|
115 // Once more with feeling. Ensure that our new auth data doesn't interfere with this mozbrowser's |
|
116 // auth data. |
|
117 iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs'; |
|
118 } |
|
119 |
|
120 function testAuthJarInterfere(e) { |
|
121 var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1'] |
|
122 .getService(SpecialPowers.Ci.nsIHttpAuthManager); |
|
123 var secMan = SpecialPowers.Cc["@mozilla.org/scriptsecuritymanager;1"] |
|
124 .getService(SpecialPowers.Ci.nsIScriptSecurityManager); |
|
125 var ioService = SpecialPowers.Cc["@mozilla.org/network/io-service;1"] |
|
126 .getService(SpecialPowers.Ci.nsIIOService); |
|
127 var uri = ioService.newURI("http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs", null, null); |
|
128 |
|
129 // Set some auth data that should overwrite the successful stored details. |
|
130 var principal = secMan.getAppCodebasePrincipal(uri, secMan.NO_APP_ID, true); |
|
131 authMgr.setAuthIdentity('http', 'test', -1, 'basic', 'http_realm', |
|
132 'tests/dom/browser-element/mochitest/file_http_401_response.sjs', |
|
133 '', 'httpuser', 'wrongpass', false, principal); |
|
134 |
|
135 // Will authenticate with correct password, prompt should not be |
|
136 // called again. |
|
137 var gotusernamepasswordrequired = false; |
|
138 function onUserNameAndPasswordRequired() { |
|
139 gotusernamepasswordrequired = true; |
|
140 } |
|
141 iframe.addEventListener("mozbrowserusernameandpasswordrequired", |
|
142 onUserNameAndPasswordRequired); |
|
143 iframe.addEventListener("mozbrowsertitlechange", function onTitleChange(e) { |
|
144 iframe.removeEventListener("mozbrowsertitlechange", onTitleChange); |
|
145 iframe.removeEventListener("mozbrowserusernameandpasswordrequired", |
|
146 onUserNameAndPasswordRequired); |
|
147 ok(gotusernamepasswordrequired, |
|
148 "Should have dispatched mozbrowserusernameandpasswordrequired event"); |
|
149 testFinish(); |
|
150 }); |
|
151 |
|
152 // Once more with feeling. Ensure that our new auth data interferes with this mozbrowser's |
|
153 // auth data. |
|
154 iframe.src = 'http://test/tests/dom/browser-element/mochitest/file_http_401_response.sjs'; |
|
155 } |
|
156 |
|
157 function testFinish() { |
|
158 // Clear login information stored in password manager. |
|
159 var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1'] |
|
160 .getService(SpecialPowers.Ci.nsIHttpAuthManager); |
|
161 authMgr.clearAll(); |
|
162 |
|
163 var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"] |
|
164 .getService(SpecialPowers.Ci.nsILoginManager); |
|
165 pwmgr.removeAllLogins(); |
|
166 |
|
167 SimpleTest.finish(); |
|
168 } |
|
169 |
|
170 addEventListener('testready', runTest); |