test_bug1683176.js (2479B)
1 // Test bug 1683176 2 // 3 // Summary: 4 // Test the case when a channel is cancelled when "negotiate" authentication 5 // is processing. 6 // 7 8 "use strict"; 9 10 let prefs; 11 let httpserv; 12 13 const { HttpServer } = ChromeUtils.importESModule( 14 "resource://testing-common/httpd.sys.mjs" 15 ); 16 17 ChromeUtils.defineLazyGetter(this, "URL", function () { 18 return "http://localhost:" + httpserv.identity.primaryPort; 19 }); 20 21 function makeChan(url, loadingUrl) { 22 var principal = Services.scriptSecurityManager.createContentPrincipal( 23 Services.io.newURI(loadingUrl), 24 {} 25 ); 26 return NetUtil.newChannel({ 27 uri: url, 28 loadingPrincipal: principal, 29 securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, 30 contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER, 31 }); 32 } 33 34 function authHandler(metadata, response) { 35 var body = "blablabla"; 36 37 response.seizePower(); 38 response.write("HTTP/1.1 401 Unauthorized\r\n"); 39 response.write("WWW-Authenticate: Negotiate\r\n"); 40 response.write("WWW-Authenticate: Basic realm=test\r\n"); 41 response.write("\r\n"); 42 response.write(body); 43 response.finish(); 44 } 45 46 function setup() { 47 prefs = Services.prefs; 48 49 prefs.setIntPref("network.auth.subresource-http-auth-allow", 2); 50 prefs.setStringPref("network.negotiate-auth.trusted-uris", "localhost"); 51 52 httpserv = new HttpServer(); 53 httpserv.registerPathHandler("/auth", authHandler); 54 httpserv.start(-1); 55 } 56 57 setup(); 58 registerCleanupFunction(async () => { 59 prefs.clearUserPref("network.auth.subresource-http-auth-allow"); 60 prefs.clearUserPref("network.negotiate-auth.trusted-uris"); 61 await httpserv.stop(); 62 }); 63 64 function channelOpenPromise(chan) { 65 return new Promise(resolve => { 66 let topic = "http-on-transaction-suspended-authentication"; 67 let observer = { 68 QueryInterface: ChromeUtils.generateQI(["nsIObserver"]), 69 observe(aSubject, aTopic) { 70 if (aTopic == topic) { 71 Services.obs.removeObserver(observer, topic); 72 let channel = aSubject.QueryInterface(Ci.nsIChannel); 73 channel.cancel(Cr.NS_BINDING_ABORTED); 74 resolve(); 75 } 76 }, 77 }; 78 Services.obs.addObserver(observer, topic); 79 80 chan.asyncOpen(new ChannelListener(finish, null, CL_EXPECT_FAILURE)); 81 function finish() { 82 resolve(); 83 } 84 }); 85 } 86 87 add_task(async function testCancelAuthentication() { 88 let chan = makeChan(URL + "/auth", URL); 89 await channelOpenPromise(chan); 90 Assert.equal(chan.status, Cr.NS_BINDING_ABORTED); 91 });