michael@0: // NOTE: This tests code outside of Necko. The test still lives here because michael@0: // the contract is part of Necko. michael@0: michael@0: // TODO: michael@0: // - HTTPS michael@0: // - Proxies michael@0: michael@0: const nsIAuthInformation = Components.interfaces.nsIAuthInformation; michael@0: const nsIAuthPromptAdapterFactory = Components.interfaces.nsIAuthPromptAdapterFactory; michael@0: michael@0: function run_test() { michael@0: const contractID = "@mozilla.org/network/authprompt-adapter-factory;1"; michael@0: if (!(contractID in Components.classes)) { michael@0: print("No adapter factory found, skipping testing"); michael@0: return; michael@0: } michael@0: var adapter = Components.classes[contractID].getService(); michael@0: do_check_eq(adapter instanceof nsIAuthPromptAdapterFactory, true); michael@0: michael@0: // NOTE: xpconnect lets us get away with passing an empty object here michael@0: // For this part of the test, we only care that this function returns michael@0: // success michael@0: do_check_neq(adapter.createAdapter({}), null); michael@0: michael@0: const host = "www.mozilla.org"; michael@0: michael@0: var info = { michael@0: username: "", michael@0: password: "", michael@0: domain: "", michael@0: michael@0: flags: nsIAuthInformation.AUTH_HOST, michael@0: authenticationScheme: "basic", michael@0: realm: "secretrealm" michael@0: }; michael@0: michael@0: const CALLED_PROMPT = 1 << 0; michael@0: const CALLED_PROMPTUP = 1 << 1; michael@0: const CALLED_PROMPTP = 1 << 2; michael@0: function Prompt1() {} michael@0: Prompt1.prototype = { michael@0: called: 0, michael@0: rv: true, michael@0: michael@0: user: "foo\\bar", michael@0: pw: "bar", michael@0: michael@0: scheme: "http", michael@0: michael@0: QueryInterface: function authprompt_qi(iid) { michael@0: if (iid.equals(Components.interfaces.nsISupports) || michael@0: iid.equals(Components.interfaces.nsIAuthPrompt)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: michael@0: prompt: function ap1_prompt(title, text, realm, save, defaultText, result) { michael@0: this.called |= CALLED_PROMPT; michael@0: this.doChecks(text, realm); michael@0: return this.rv; michael@0: }, michael@0: michael@0: promptUsernameAndPassword: michael@0: function ap1_promptUP(title, text, realm, savePW, user, pw) michael@0: { michael@0: this.called |= CALLED_PROMPTUP; michael@0: this.doChecks(text, realm); michael@0: user.value = this.user; michael@0: pw.value = this.pw; michael@0: return this.rv; michael@0: }, michael@0: michael@0: promptPassword: function ap1_promptPW(title, text, realm, save, pwd) { michael@0: this.called |= CALLED_PROMPTP; michael@0: this.doChecks(text, realm); michael@0: pwd.value = this.pw; michael@0: return this.rv; michael@0: }, michael@0: michael@0: doChecks: function ap1_check(text, realm) { michael@0: do_check_eq(this.scheme + "://" + host + " (" + info.realm + ")", realm); michael@0: michael@0: do_check_neq(text.indexOf(host), -1); michael@0: if (info.flags & nsIAuthInformation.ONLY_PASSWORD) { michael@0: // Should have the username in the text michael@0: do_check_neq(text.indexOf(info.username), -1); michael@0: } else { michael@0: // Make sure that we show the realm if we have one and that we don't michael@0: // show "" otherwise michael@0: if (info.realm != "") michael@0: do_check_neq(text.indexOf(info.realm), -1); michael@0: else michael@0: do_check_eq(text.indexOf('""'), -1); michael@0: // No explicit port in the URL; message should not contain -1 michael@0: // for those cases michael@0: do_check_eq(text.indexOf("-1"), -1); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: michael@0: // Also have to make up a channel michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: var chan = ios.newChannel("http://" + host, "", null); michael@0: michael@0: function do_tests(expectedRV) { michael@0: var prompt1; michael@0: var wrapper; michael@0: michael@0: // 1: The simple case michael@0: prompt1 = new Prompt1(); michael@0: prompt1.rv = expectedRV; michael@0: wrapper = adapter.createAdapter(prompt1); michael@0: michael@0: var rv = wrapper.promptAuth(chan, 0, info); michael@0: do_check_eq(rv, prompt1.rv); michael@0: do_check_eq(prompt1.called, CALLED_PROMPTUP); michael@0: michael@0: if (rv) { michael@0: do_check_eq(info.domain, ""); michael@0: do_check_eq(info.username, prompt1.user); michael@0: do_check_eq(info.password, prompt1.pw); michael@0: } michael@0: michael@0: info.domain = ""; michael@0: info.username = ""; michael@0: info.password = ""; michael@0: michael@0: // 2: Only ask for a PW michael@0: prompt1 = new Prompt1(); michael@0: prompt1.rv = expectedRV; michael@0: info.flags |= nsIAuthInformation.ONLY_PASSWORD; michael@0: michael@0: // Initialize the username so that the prompt can show it michael@0: info.username = prompt1.user; michael@0: michael@0: wrapper = adapter.createAdapter(prompt1); michael@0: rv = wrapper.promptAuth(chan, 0, info); michael@0: do_check_eq(rv, prompt1.rv); michael@0: do_check_eq(prompt1.called, CALLED_PROMPTP); michael@0: michael@0: if (rv) { michael@0: do_check_eq(info.domain, ""); michael@0: do_check_eq(info.username, prompt1.user); // we initialized this michael@0: do_check_eq(info.password, prompt1.pw); michael@0: } michael@0: michael@0: info.flags &= ~nsIAuthInformation.ONLY_PASSWORD; michael@0: michael@0: info.domain = ""; michael@0: info.username = ""; michael@0: info.password = ""; michael@0: michael@0: // 3: user, pw and domain michael@0: prompt1 = new Prompt1(); michael@0: prompt1.rv = expectedRV; michael@0: info.flags |= nsIAuthInformation.NEED_DOMAIN; michael@0: michael@0: wrapper = adapter.createAdapter(prompt1); michael@0: rv = wrapper.promptAuth(chan, 0, info); michael@0: do_check_eq(rv, prompt1.rv); michael@0: do_check_eq(prompt1.called, CALLED_PROMPTUP); michael@0: michael@0: if (rv) { michael@0: do_check_eq(info.domain, "foo"); michael@0: do_check_eq(info.username, "bar"); michael@0: do_check_eq(info.password, prompt1.pw); michael@0: } michael@0: michael@0: info.flags &= ~nsIAuthInformation.NEED_DOMAIN; michael@0: michael@0: info.domain = ""; michael@0: info.username = ""; michael@0: info.password = ""; michael@0: michael@0: // 4: username that doesn't contain a domain michael@0: prompt1 = new Prompt1(); michael@0: prompt1.rv = expectedRV; michael@0: info.flags |= nsIAuthInformation.NEED_DOMAIN; michael@0: michael@0: prompt1.user = "foo"; michael@0: michael@0: wrapper = adapter.createAdapter(prompt1); michael@0: rv = wrapper.promptAuth(chan, 0, info); michael@0: do_check_eq(rv, prompt1.rv); michael@0: do_check_eq(prompt1.called, CALLED_PROMPTUP); michael@0: michael@0: if (rv) { michael@0: do_check_eq(info.domain, ""); michael@0: do_check_eq(info.username, prompt1.user); michael@0: do_check_eq(info.password, prompt1.pw); michael@0: } michael@0: michael@0: info.flags &= ~nsIAuthInformation.NEED_DOMAIN; michael@0: michael@0: info.domain = ""; michael@0: info.username = ""; michael@0: info.password = ""; michael@0: michael@0: // 5: FTP michael@0: var ftpchan = ios.newChannel("ftp://" + host, "", null); michael@0: michael@0: prompt1 = new Prompt1(); michael@0: prompt1.rv = expectedRV; michael@0: prompt1.scheme = "ftp"; michael@0: michael@0: wrapper = adapter.createAdapter(prompt1); michael@0: var rv = wrapper.promptAuth(ftpchan, 0, info); michael@0: do_check_eq(rv, prompt1.rv); michael@0: do_check_eq(prompt1.called, CALLED_PROMPTUP); michael@0: michael@0: if (rv) { michael@0: do_check_eq(info.domain, ""); michael@0: do_check_eq(info.username, prompt1.user); michael@0: do_check_eq(info.password, prompt1.pw); michael@0: } michael@0: michael@0: info.domain = ""; michael@0: info.username = ""; michael@0: info.password = ""; michael@0: } michael@0: do_tests(true); michael@0: do_tests(false); michael@0: } michael@0: