Bug 574454 - Cleanup nsNativeThemeWin's GetMinimumWidgetSize a bit. r=roc.
[mozilla-central.git] / netwerk / test / unit / test_authpromptwrapper.js
blobc54104fd50f5b160be29bf538ce16dd29f473592
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 const nsIAuthInformation = Components.interfaces.nsIAuthInformation;
9 const nsIAuthPromptAdapterFactory = Components.interfaces.nsIAuthPromptAdapterFactory;
11 function run_test() {
12   const contractID = "@mozilla.org/network/authprompt-adapter-factory;1";
13   if (!(contractID in Components.classes)) {
14     print("No adapter factory found, skipping testing");
15     return;
16   }
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
22   // success
23   do_check_neq(adapter.createAdapter({}), null);
25   const host = "www.mozilla.org";
27   var info = {
28     username: "",
29     password: "",
30     domain: "",
32     flags: nsIAuthInformation.AUTH_HOST,
33     authenticationScheme: "basic",
34     realm: "secretrealm"
35   };
37   const CALLED_PROMPT = 1 << 0;
38   const CALLED_PROMPTUP = 1 << 1;
39   const CALLED_PROMPTP = 1 << 2;
40   function Prompt1() {}
41   Prompt1.prototype = {
42     called: 0,
43     rv: true,
45     user: "foo\\bar",
46     pw: "bar",
48     scheme: "http",
50     QueryInterface: function authprompt_qi(iid) {
51       if (iid.equals(Components.interfaces.nsISupports) ||
52           iid.equals(Components.interfaces.nsIAuthPrompt))
53         return this;
54       throw Components.results.NS_ERROR_NO_INTERFACE;
55     },
57     prompt: function ap1_prompt(title, text, realm, save, defaultText, result) {
58       this.called |= CALLED_PROMPT;
59       this.doChecks(text, realm);
60       return this.rv;
61     },
63     promptUsernameAndPassword:
64       function ap1_promptUP(title, text, realm, savePW, user, pw)
65     {
66       this.called |= CALLED_PROMPTUP;
67       this.doChecks(text, realm);
68       user.value = this.user;
69       pw.value = this.pw;
70       return this.rv;
71     },
73     promptPassword: function ap1_promptPW(title, text, realm, save, pwd) {
74       this.called |= CALLED_PROMPTP;
75       this.doChecks(text, realm);
76       pwd.value = this.pw;
77       return this.rv;
78     },
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);
87       } else {
88         // Make sure that we show the realm if we have one and that we don't
89         // show "" otherwise
90         if (info.realm != "")
91           do_check_neq(text.indexOf(info.realm), -1);
92         else
93           do_check_eq(text.indexOf('""'), -1);
94         // No explicit port in the URL; message should not contain -1
95         // for those cases
96         do_check_eq(text.indexOf("-1"), -1);
97       }
98     }
99   };
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) {
108     var prompt1;
109     var wrapper;
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);
120     if (rv) {
121       do_check_eq(info.domain, "");
122       do_check_eq(info.username, prompt1.user);
123       do_check_eq(info.password, prompt1.pw);
124     }
126     info.domain = "";
127     info.username = "";
128     info.password = "";
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);
143     if (rv) {
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);
147     }
149     info.flags &= ~nsIAuthInformation.ONLY_PASSWORD;
151     info.domain = "";
152     info.username = "";
153     info.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);
165     if (rv) {
166       do_check_eq(info.domain, "foo");
167       do_check_eq(info.username, "bar");
168       do_check_eq(info.password, prompt1.pw);
169     }
171     info.flags &= ~nsIAuthInformation.NEED_DOMAIN;
173     info.domain = "";
174     info.username = "";
175     info.password = "";
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);
189     if (rv) {
190       do_check_eq(info.domain, "");
191       do_check_eq(info.username, prompt1.user);
192       do_check_eq(info.password, prompt1.pw);
193     }
195     info.flags &= ~nsIAuthInformation.NEED_DOMAIN;
197     info.domain = "";
198     info.username = "";
199     info.password = "";
201     // 5: FTP
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);
213     if (rv) {
214       do_check_eq(info.domain, "");
215       do_check_eq(info.username, prompt1.user);
216       do_check_eq(info.password, prompt1.pw);
217     }
219     info.domain = "";
220     info.username = "";
221     info.password = "";
222   }
223   do_tests(true);
224   do_tests(false);