Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_authpromptwrapper.js
blob85bf690bca3837cb89c6815ebcd78eb0bcdab773
1 // NOTE: This tests code outside of Necko. The test still lives here because
2 // the contract is part of Necko.
4 // TODO:
5 // - HTTPS
6 // - Proxies
8 "use strict";
10 const nsIAuthInformation = Ci.nsIAuthInformation;
11 const nsIAuthPromptAdapterFactory = Ci.nsIAuthPromptAdapterFactory;
13 function run_test() {
14 const contractID = "@mozilla.org/network/authprompt-adapter-factory;1";
15 if (!(contractID in Cc)) {
16 print("No adapter factory found, skipping testing");
17 return;
19 var adapter = Cc[contractID].getService();
20 Assert.equal(adapter instanceof nsIAuthPromptAdapterFactory, true);
22 // NOTE: xpconnect lets us get away with passing an empty object here
23 // For this part of the test, we only care that this function returns
24 // success
25 Assert.notEqual(adapter.createAdapter({}), null);
27 const host = "www.mozilla.org";
29 var info = {
30 username: "",
31 password: "",
32 domain: "",
34 flags: nsIAuthInformation.AUTH_HOST,
35 authenticationScheme: "basic",
36 realm: "secretrealm",
39 const CALLED_PROMPT = 1 << 0;
40 const CALLED_PROMPTUP = 1 << 1;
41 const CALLED_PROMPTP = 1 << 2;
42 function Prompt1() {}
43 Prompt1.prototype = {
44 called: 0,
45 rv: true,
47 user: "foo\\bar",
48 pw: "bar",
50 scheme: "http",
52 QueryInterface: ChromeUtils.generateQI(["nsIAuthPrompt"]),
54 prompt: function ap1_prompt(title, text, realm) {
55 this.called |= CALLED_PROMPT;
56 this.doChecks(text, realm);
57 return this.rv;
60 promptUsernameAndPassword: function ap1_promptUP(
61 title,
62 text,
63 realm,
64 savePW,
65 user,
67 ) {
68 this.called |= CALLED_PROMPTUP;
69 this.doChecks(text, realm);
70 user.value = this.user;
71 pw.value = this.pw;
72 return this.rv;
75 promptPassword: function ap1_promptPW(title, text, realm, save, pwd) {
76 this.called |= CALLED_PROMPTP;
77 this.doChecks(text, realm);
78 pwd.value = this.pw;
79 return this.rv;
82 doChecks: function ap1_check(text, realm) {
83 Assert.equal(this.scheme + "://" + host + " (" + info.realm + ")", realm);
85 Assert.notEqual(text.indexOf(host), -1);
86 if (info.flags & nsIAuthInformation.ONLY_PASSWORD) {
87 // Should have the username in the text
88 Assert.notEqual(text.indexOf(info.username), -1);
89 } else {
90 // Make sure that we show the realm if we have one and that we don't
91 // show "" otherwise
92 if (info.realm != "") {
93 Assert.notEqual(text.indexOf(info.realm), -1);
94 } else {
95 Assert.equal(text.indexOf('""'), -1);
97 // No explicit port in the URL; message should not contain -1
98 // for those cases
99 Assert.equal(text.indexOf("-1"), -1);
104 // Also have to make up a channel
105 var uri = NetUtil.newURI("http://" + host);
106 var chan = NetUtil.newChannel({
107 uri,
108 loadUsingSystemPrincipal: true,
111 function do_tests(expectedRV) {
112 var prompt1;
113 var wrapper;
115 // 1: The simple case
116 prompt1 = new Prompt1();
117 prompt1.rv = expectedRV;
118 wrapper = adapter.createAdapter(prompt1);
120 var rv = wrapper.promptAuth(chan, 0, info);
121 Assert.equal(rv, prompt1.rv);
122 Assert.equal(prompt1.called, CALLED_PROMPTUP);
124 if (rv) {
125 Assert.equal(info.domain, "");
126 Assert.equal(info.username, prompt1.user);
127 Assert.equal(info.password, prompt1.pw);
130 info.domain = "";
131 info.username = "";
132 info.password = "";
134 // 2: Only ask for a PW
135 prompt1 = new Prompt1();
136 prompt1.rv = expectedRV;
137 info.flags |= nsIAuthInformation.ONLY_PASSWORD;
139 // Initialize the username so that the prompt can show it
140 info.username = prompt1.user;
142 wrapper = adapter.createAdapter(prompt1);
143 rv = wrapper.promptAuth(chan, 0, info);
144 Assert.equal(rv, prompt1.rv);
145 Assert.equal(prompt1.called, CALLED_PROMPTP);
147 if (rv) {
148 Assert.equal(info.domain, "");
149 Assert.equal(info.username, prompt1.user); // we initialized this
150 Assert.equal(info.password, prompt1.pw);
153 info.flags &= ~nsIAuthInformation.ONLY_PASSWORD;
155 info.domain = "";
156 info.username = "";
157 info.password = "";
159 // 3: user, pw and domain
160 prompt1 = new Prompt1();
161 prompt1.rv = expectedRV;
162 info.flags |= nsIAuthInformation.NEED_DOMAIN;
164 wrapper = adapter.createAdapter(prompt1);
165 rv = wrapper.promptAuth(chan, 0, info);
166 Assert.equal(rv, prompt1.rv);
167 Assert.equal(prompt1.called, CALLED_PROMPTUP);
169 if (rv) {
170 Assert.equal(info.domain, "foo");
171 Assert.equal(info.username, "bar");
172 Assert.equal(info.password, prompt1.pw);
175 info.flags &= ~nsIAuthInformation.NEED_DOMAIN;
177 info.domain = "";
178 info.username = "";
179 info.password = "";
181 // 4: username that doesn't contain a domain
182 prompt1 = new Prompt1();
183 prompt1.rv = expectedRV;
184 info.flags |= nsIAuthInformation.NEED_DOMAIN;
186 prompt1.user = "foo";
188 wrapper = adapter.createAdapter(prompt1);
189 rv = wrapper.promptAuth(chan, 0, info);
190 Assert.equal(rv, prompt1.rv);
191 Assert.equal(prompt1.called, CALLED_PROMPTUP);
193 if (rv) {
194 Assert.equal(info.domain, "");
195 Assert.equal(info.username, prompt1.user);
196 Assert.equal(info.password, prompt1.pw);
199 info.flags &= ~nsIAuthInformation.NEED_DOMAIN;
201 info.domain = "";
202 info.username = "";
203 info.password = "";
205 do_tests(true);
206 do_tests(false);