netwerk/test/unit/test_authpromptwrapper.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 // NOTE: This tests code outside of Necko. The test still lives here because
michael@0 2 // the contract is part of Necko.
michael@0 3
michael@0 4 // TODO:
michael@0 5 // - HTTPS
michael@0 6 // - Proxies
michael@0 7
michael@0 8 const nsIAuthInformation = Components.interfaces.nsIAuthInformation;
michael@0 9 const nsIAuthPromptAdapterFactory = Components.interfaces.nsIAuthPromptAdapterFactory;
michael@0 10
michael@0 11 function run_test() {
michael@0 12 const contractID = "@mozilla.org/network/authprompt-adapter-factory;1";
michael@0 13 if (!(contractID in Components.classes)) {
michael@0 14 print("No adapter factory found, skipping testing");
michael@0 15 return;
michael@0 16 }
michael@0 17 var adapter = Components.classes[contractID].getService();
michael@0 18 do_check_eq(adapter instanceof nsIAuthPromptAdapterFactory, true);
michael@0 19
michael@0 20 // NOTE: xpconnect lets us get away with passing an empty object here
michael@0 21 // For this part of the test, we only care that this function returns
michael@0 22 // success
michael@0 23 do_check_neq(adapter.createAdapter({}), null);
michael@0 24
michael@0 25 const host = "www.mozilla.org";
michael@0 26
michael@0 27 var info = {
michael@0 28 username: "",
michael@0 29 password: "",
michael@0 30 domain: "",
michael@0 31
michael@0 32 flags: nsIAuthInformation.AUTH_HOST,
michael@0 33 authenticationScheme: "basic",
michael@0 34 realm: "secretrealm"
michael@0 35 };
michael@0 36
michael@0 37 const CALLED_PROMPT = 1 << 0;
michael@0 38 const CALLED_PROMPTUP = 1 << 1;
michael@0 39 const CALLED_PROMPTP = 1 << 2;
michael@0 40 function Prompt1() {}
michael@0 41 Prompt1.prototype = {
michael@0 42 called: 0,
michael@0 43 rv: true,
michael@0 44
michael@0 45 user: "foo\\bar",
michael@0 46 pw: "bar",
michael@0 47
michael@0 48 scheme: "http",
michael@0 49
michael@0 50 QueryInterface: function authprompt_qi(iid) {
michael@0 51 if (iid.equals(Components.interfaces.nsISupports) ||
michael@0 52 iid.equals(Components.interfaces.nsIAuthPrompt))
michael@0 53 return this;
michael@0 54 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 55 },
michael@0 56
michael@0 57 prompt: function ap1_prompt(title, text, realm, save, defaultText, result) {
michael@0 58 this.called |= CALLED_PROMPT;
michael@0 59 this.doChecks(text, realm);
michael@0 60 return this.rv;
michael@0 61 },
michael@0 62
michael@0 63 promptUsernameAndPassword:
michael@0 64 function ap1_promptUP(title, text, realm, savePW, user, pw)
michael@0 65 {
michael@0 66 this.called |= CALLED_PROMPTUP;
michael@0 67 this.doChecks(text, realm);
michael@0 68 user.value = this.user;
michael@0 69 pw.value = this.pw;
michael@0 70 return this.rv;
michael@0 71 },
michael@0 72
michael@0 73 promptPassword: function ap1_promptPW(title, text, realm, save, pwd) {
michael@0 74 this.called |= CALLED_PROMPTP;
michael@0 75 this.doChecks(text, realm);
michael@0 76 pwd.value = this.pw;
michael@0 77 return this.rv;
michael@0 78 },
michael@0 79
michael@0 80 doChecks: function ap1_check(text, realm) {
michael@0 81 do_check_eq(this.scheme + "://" + host + " (" + info.realm + ")", realm);
michael@0 82
michael@0 83 do_check_neq(text.indexOf(host), -1);
michael@0 84 if (info.flags & nsIAuthInformation.ONLY_PASSWORD) {
michael@0 85 // Should have the username in the text
michael@0 86 do_check_neq(text.indexOf(info.username), -1);
michael@0 87 } else {
michael@0 88 // Make sure that we show the realm if we have one and that we don't
michael@0 89 // show "" otherwise
michael@0 90 if (info.realm != "")
michael@0 91 do_check_neq(text.indexOf(info.realm), -1);
michael@0 92 else
michael@0 93 do_check_eq(text.indexOf('""'), -1);
michael@0 94 // No explicit port in the URL; message should not contain -1
michael@0 95 // for those cases
michael@0 96 do_check_eq(text.indexOf("-1"), -1);
michael@0 97 }
michael@0 98 }
michael@0 99 };
michael@0 100
michael@0 101
michael@0 102 // Also have to make up a channel
michael@0 103 var ios = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 104 .getService(Components.interfaces.nsIIOService);
michael@0 105 var chan = ios.newChannel("http://" + host, "", null);
michael@0 106
michael@0 107 function do_tests(expectedRV) {
michael@0 108 var prompt1;
michael@0 109 var wrapper;
michael@0 110
michael@0 111 // 1: The simple case
michael@0 112 prompt1 = new Prompt1();
michael@0 113 prompt1.rv = expectedRV;
michael@0 114 wrapper = adapter.createAdapter(prompt1);
michael@0 115
michael@0 116 var rv = wrapper.promptAuth(chan, 0, info);
michael@0 117 do_check_eq(rv, prompt1.rv);
michael@0 118 do_check_eq(prompt1.called, CALLED_PROMPTUP);
michael@0 119
michael@0 120 if (rv) {
michael@0 121 do_check_eq(info.domain, "");
michael@0 122 do_check_eq(info.username, prompt1.user);
michael@0 123 do_check_eq(info.password, prompt1.pw);
michael@0 124 }
michael@0 125
michael@0 126 info.domain = "";
michael@0 127 info.username = "";
michael@0 128 info.password = "";
michael@0 129
michael@0 130 // 2: Only ask for a PW
michael@0 131 prompt1 = new Prompt1();
michael@0 132 prompt1.rv = expectedRV;
michael@0 133 info.flags |= nsIAuthInformation.ONLY_PASSWORD;
michael@0 134
michael@0 135 // Initialize the username so that the prompt can show it
michael@0 136 info.username = prompt1.user;
michael@0 137
michael@0 138 wrapper = adapter.createAdapter(prompt1);
michael@0 139 rv = wrapper.promptAuth(chan, 0, info);
michael@0 140 do_check_eq(rv, prompt1.rv);
michael@0 141 do_check_eq(prompt1.called, CALLED_PROMPTP);
michael@0 142
michael@0 143 if (rv) {
michael@0 144 do_check_eq(info.domain, "");
michael@0 145 do_check_eq(info.username, prompt1.user); // we initialized this
michael@0 146 do_check_eq(info.password, prompt1.pw);
michael@0 147 }
michael@0 148
michael@0 149 info.flags &= ~nsIAuthInformation.ONLY_PASSWORD;
michael@0 150
michael@0 151 info.domain = "";
michael@0 152 info.username = "";
michael@0 153 info.password = "";
michael@0 154
michael@0 155 // 3: user, pw and domain
michael@0 156 prompt1 = new Prompt1();
michael@0 157 prompt1.rv = expectedRV;
michael@0 158 info.flags |= nsIAuthInformation.NEED_DOMAIN;
michael@0 159
michael@0 160 wrapper = adapter.createAdapter(prompt1);
michael@0 161 rv = wrapper.promptAuth(chan, 0, info);
michael@0 162 do_check_eq(rv, prompt1.rv);
michael@0 163 do_check_eq(prompt1.called, CALLED_PROMPTUP);
michael@0 164
michael@0 165 if (rv) {
michael@0 166 do_check_eq(info.domain, "foo");
michael@0 167 do_check_eq(info.username, "bar");
michael@0 168 do_check_eq(info.password, prompt1.pw);
michael@0 169 }
michael@0 170
michael@0 171 info.flags &= ~nsIAuthInformation.NEED_DOMAIN;
michael@0 172
michael@0 173 info.domain = "";
michael@0 174 info.username = "";
michael@0 175 info.password = "";
michael@0 176
michael@0 177 // 4: username that doesn't contain a domain
michael@0 178 prompt1 = new Prompt1();
michael@0 179 prompt1.rv = expectedRV;
michael@0 180 info.flags |= nsIAuthInformation.NEED_DOMAIN;
michael@0 181
michael@0 182 prompt1.user = "foo";
michael@0 183
michael@0 184 wrapper = adapter.createAdapter(prompt1);
michael@0 185 rv = wrapper.promptAuth(chan, 0, info);
michael@0 186 do_check_eq(rv, prompt1.rv);
michael@0 187 do_check_eq(prompt1.called, CALLED_PROMPTUP);
michael@0 188
michael@0 189 if (rv) {
michael@0 190 do_check_eq(info.domain, "");
michael@0 191 do_check_eq(info.username, prompt1.user);
michael@0 192 do_check_eq(info.password, prompt1.pw);
michael@0 193 }
michael@0 194
michael@0 195 info.flags &= ~nsIAuthInformation.NEED_DOMAIN;
michael@0 196
michael@0 197 info.domain = "";
michael@0 198 info.username = "";
michael@0 199 info.password = "";
michael@0 200
michael@0 201 // 5: FTP
michael@0 202 var ftpchan = ios.newChannel("ftp://" + host, "", null);
michael@0 203
michael@0 204 prompt1 = new Prompt1();
michael@0 205 prompt1.rv = expectedRV;
michael@0 206 prompt1.scheme = "ftp";
michael@0 207
michael@0 208 wrapper = adapter.createAdapter(prompt1);
michael@0 209 var rv = wrapper.promptAuth(ftpchan, 0, info);
michael@0 210 do_check_eq(rv, prompt1.rv);
michael@0 211 do_check_eq(prompt1.called, CALLED_PROMPTUP);
michael@0 212
michael@0 213 if (rv) {
michael@0 214 do_check_eq(info.domain, "");
michael@0 215 do_check_eq(info.username, prompt1.user);
michael@0 216 do_check_eq(info.password, prompt1.pw);
michael@0 217 }
michael@0 218
michael@0 219 info.domain = "";
michael@0 220 info.username = "";
michael@0 221 info.password = "";
michael@0 222 }
michael@0 223 do_tests(true);
michael@0 224 do_tests(false);
michael@0 225 }
michael@0 226

mercurial