test_authpromptwrapper.js (5464B)
1 // NOTE: This tests code outside of Necko. The test still lives here because 2 // the contract is part of Necko. 3 4 // TODO: 5 // - HTTPS 6 // - Proxies 7 8 "use strict"; 9 10 const nsIAuthInformation = Ci.nsIAuthInformation; 11 const nsIAuthPromptAdapterFactory = Ci.nsIAuthPromptAdapterFactory; 12 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; 18 } 19 var adapter = Cc[contractID].getService(); 20 Assert.equal(adapter instanceof nsIAuthPromptAdapterFactory, true); 21 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); 26 27 const host = "www.mozilla.org"; 28 29 var info = { 30 username: "", 31 password: "", 32 domain: "", 33 34 flags: nsIAuthInformation.AUTH_HOST, 35 authenticationScheme: "basic", 36 realm: "secretrealm", 37 }; 38 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, 46 47 user: "foo\\bar", 48 pw: "bar", 49 50 scheme: "http", 51 52 QueryInterface: ChromeUtils.generateQI(["nsIAuthPrompt"]), 53 54 prompt: function ap1_prompt(title, text, realm) { 55 this.called |= CALLED_PROMPT; 56 this.doChecks(text, realm); 57 return this.rv; 58 }, 59 60 promptUsernameAndPassword: function ap1_promptUP( 61 title, 62 text, 63 realm, 64 savePW, 65 user, 66 pw 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; 73 }, 74 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; 80 }, 81 82 doChecks: function ap1_check(text, realm) { 83 Assert.equal(this.scheme + "://" + host + " (" + info.realm + ")", realm); 84 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); 96 } 97 // No explicit port in the URL; message should not contain -1 98 // for those cases 99 Assert.equal(text.indexOf("-1"), -1); 100 } 101 }, 102 }; 103 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, 109 }); 110 111 function do_tests(expectedRV) { 112 var prompt1; 113 var wrapper; 114 115 // 1: The simple case 116 prompt1 = new Prompt1(); 117 prompt1.rv = expectedRV; 118 wrapper = adapter.createAdapter(prompt1); 119 120 var rv = wrapper.promptAuth(chan, 0, info); 121 Assert.equal(rv, prompt1.rv); 122 Assert.equal(prompt1.called, CALLED_PROMPTUP); 123 124 if (rv) { 125 Assert.equal(info.domain, ""); 126 Assert.equal(info.username, prompt1.user); 127 Assert.equal(info.password, prompt1.pw); 128 } 129 130 info.domain = ""; 131 info.username = ""; 132 info.password = ""; 133 134 // 2: Only ask for a PW 135 prompt1 = new Prompt1(); 136 prompt1.rv = expectedRV; 137 info.flags |= nsIAuthInformation.ONLY_PASSWORD; 138 139 // Initialize the username so that the prompt can show it 140 info.username = prompt1.user; 141 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); 146 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); 151 } 152 153 info.flags &= ~nsIAuthInformation.ONLY_PASSWORD; 154 155 info.domain = ""; 156 info.username = ""; 157 info.password = ""; 158 159 // 3: user, pw and domain 160 prompt1 = new Prompt1(); 161 prompt1.rv = expectedRV; 162 info.flags |= nsIAuthInformation.NEED_DOMAIN; 163 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); 168 169 if (rv) { 170 Assert.equal(info.domain, "foo"); 171 Assert.equal(info.username, "bar"); 172 Assert.equal(info.password, prompt1.pw); 173 } 174 175 info.flags &= ~nsIAuthInformation.NEED_DOMAIN; 176 177 info.domain = ""; 178 info.username = ""; 179 info.password = ""; 180 181 // 4: username that doesn't contain a domain 182 prompt1 = new Prompt1(); 183 prompt1.rv = expectedRV; 184 info.flags |= nsIAuthInformation.NEED_DOMAIN; 185 186 prompt1.user = "foo"; 187 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); 192 193 if (rv) { 194 Assert.equal(info.domain, ""); 195 Assert.equal(info.username, prompt1.user); 196 Assert.equal(info.password, prompt1.pw); 197 } 198 199 info.flags &= ~nsIAuthInformation.NEED_DOMAIN; 200 201 info.domain = ""; 202 info.username = ""; 203 info.password = ""; 204 } 205 do_tests(true); 206 do_tests(false); 207 }