1 // NOTE: This tests code outside of Necko. The test still lives here because
2 // the contract is part of Necko.
8 const nsIAuthInformation = Components.interfaces.nsIAuthInformation;
9 const nsIAuthPromptAdapterFactory = Components.interfaces.nsIAuthPromptAdapterFactory;
12 const contractID = "@mozilla.org/network/authprompt-adapter-factory;1";
13 if (!(contractID in Components.classes)) {
14 print("No adapter factory found, skipping testing");
17 var adapter = Components.classes[contractID].getService();
18 do_check_eq(adapter instanceof nsIAuthPromptAdapterFactory, true);
20 // NOTE: xpconnect lets us get away with passing an empty object here
21 // For this part of the test, we only care that this function returns
23 do_check_neq(adapter.createAdapter({}), null);
25 const host = "www.mozilla.org";
32 flags: nsIAuthInformation.AUTH_HOST,
33 authenticationScheme: "basic",
37 const CALLED_PROMPT = 1 << 0;
38 const CALLED_PROMPTUP = 1 << 1;
39 const CALLED_PROMPTP = 1 << 2;
50 QueryInterface: function authprompt_qi(iid) {
51 if (iid.equals(Components.interfaces.nsISupports) ||
52 iid.equals(Components.interfaces.nsIAuthPrompt))
54 throw Components.results.NS_ERROR_NO_INTERFACE;
57 prompt: function ap1_prompt(title, text, realm, save, defaultText, result) {
58 this.called |= CALLED_PROMPT;
59 this.doChecks(text, realm);
63 promptUsernameAndPassword:
64 function ap1_promptUP(title, text, realm, savePW, user, pw)
66 this.called |= CALLED_PROMPTUP;
67 this.doChecks(text, realm);
68 user.value = this.user;
73 promptPassword: function ap1_promptPW(title, text, realm, save, pwd) {
74 this.called |= CALLED_PROMPTP;
75 this.doChecks(text, realm);
80 doChecks: function ap1_check(text, realm) {
81 do_check_eq(this.scheme + "://" + host + " (" + info.realm + ")", realm);
83 do_check_neq(text.indexOf(host), -1);
84 if (info.flags & nsIAuthInformation.ONLY_PASSWORD) {
85 // Should have the username in the text
86 do_check_neq(text.indexOf(info.username), -1);
88 // Make sure that we show the realm if we have one and that we don't
91 do_check_neq(text.indexOf(info.realm), -1);
93 do_check_eq(text.indexOf('""'), -1);
94 // No explicit port in the URL; message should not contain -1
96 do_check_eq(text.indexOf("-1"), -1);
102 // Also have to make up a channel
103 var ios = Components.classes["@mozilla.org/network/io-service;1"]
104 .getService(Components.interfaces.nsIIOService);
105 var chan = ios.newChannel("http://" + host, "", null);
107 function do_tests(expectedRV) {
111 // 1: The simple case
112 prompt1 = new Prompt1();
113 prompt1.rv = expectedRV;
114 wrapper = adapter.createAdapter(prompt1);
116 var rv = wrapper.promptAuth(chan, 0, info);
117 do_check_eq(rv, prompt1.rv);
118 do_check_eq(prompt1.called, CALLED_PROMPTUP);
121 do_check_eq(info.domain, "");
122 do_check_eq(info.username, prompt1.user);
123 do_check_eq(info.password, prompt1.pw);
130 // 2: Only ask for a PW
131 prompt1 = new Prompt1();
132 prompt1.rv = expectedRV;
133 info.flags |= nsIAuthInformation.ONLY_PASSWORD;
135 // Initialize the username so that the prompt can show it
136 info.username = prompt1.user;
138 wrapper = adapter.createAdapter(prompt1);
139 rv = wrapper.promptAuth(chan, 0, info);
140 do_check_eq(rv, prompt1.rv);
141 do_check_eq(prompt1.called, CALLED_PROMPTP);
144 do_check_eq(info.domain, "");
145 do_check_eq(info.username, prompt1.user); // we initialized this
146 do_check_eq(info.password, prompt1.pw);
149 info.flags &= ~nsIAuthInformation.ONLY_PASSWORD;
155 // 3: user, pw and domain
156 prompt1 = new Prompt1();
157 prompt1.rv = expectedRV;
158 info.flags |= nsIAuthInformation.NEED_DOMAIN;
160 wrapper = adapter.createAdapter(prompt1);
161 rv = wrapper.promptAuth(chan, 0, info);
162 do_check_eq(rv, prompt1.rv);
163 do_check_eq(prompt1.called, CALLED_PROMPTUP);
166 do_check_eq(info.domain, "foo");
167 do_check_eq(info.username, "bar");
168 do_check_eq(info.password, prompt1.pw);
171 info.flags &= ~nsIAuthInformation.NEED_DOMAIN;
177 // 4: username that doesn't contain a domain
178 prompt1 = new Prompt1();
179 prompt1.rv = expectedRV;
180 info.flags |= nsIAuthInformation.NEED_DOMAIN;
182 prompt1.user = "foo";
184 wrapper = adapter.createAdapter(prompt1);
185 rv = wrapper.promptAuth(chan, 0, info);
186 do_check_eq(rv, prompt1.rv);
187 do_check_eq(prompt1.called, CALLED_PROMPTUP);
190 do_check_eq(info.domain, "");
191 do_check_eq(info.username, prompt1.user);
192 do_check_eq(info.password, prompt1.pw);
195 info.flags &= ~nsIAuthInformation.NEED_DOMAIN;
202 var ftpchan = ios.newChannel("ftp://" + host, "", null);
204 prompt1 = new Prompt1();
205 prompt1.rv = expectedRV;
206 prompt1.scheme = "ftp";
208 wrapper = adapter.createAdapter(prompt1);
209 var rv = wrapper.promptAuth(ftpchan, 0, info);
210 do_check_eq(rv, prompt1.rv);
211 do_check_eq(prompt1.called, CALLED_PROMPTUP);
214 do_check_eq(info.domain, "");
215 do_check_eq(info.username, prompt1.user);
216 do_check_eq(info.password, prompt1.pw);