netwerk/test/unit/test_authpromptwrapper.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_authpromptwrapper.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,226 @@
     1.4 +// NOTE: This tests code outside of Necko. The test still lives here because
     1.5 +// the contract is part of Necko.
     1.6 +
     1.7 +// TODO:
     1.8 +// - HTTPS
     1.9 +// - Proxies
    1.10 +
    1.11 +const nsIAuthInformation = Components.interfaces.nsIAuthInformation;
    1.12 +const nsIAuthPromptAdapterFactory = Components.interfaces.nsIAuthPromptAdapterFactory;
    1.13 +
    1.14 +function run_test() {
    1.15 +  const contractID = "@mozilla.org/network/authprompt-adapter-factory;1";
    1.16 +  if (!(contractID in Components.classes)) {
    1.17 +    print("No adapter factory found, skipping testing");
    1.18 +    return;
    1.19 +  }
    1.20 +  var adapter = Components.classes[contractID].getService();
    1.21 +  do_check_eq(adapter instanceof nsIAuthPromptAdapterFactory, true);
    1.22 +
    1.23 +  // NOTE: xpconnect lets us get away with passing an empty object here
    1.24 +  // For this part of the test, we only care that this function returns
    1.25 +  // success
    1.26 +  do_check_neq(adapter.createAdapter({}), null);
    1.27 +
    1.28 +  const host = "www.mozilla.org";
    1.29 +
    1.30 +  var info = {
    1.31 +    username: "",
    1.32 +    password: "",
    1.33 +    domain: "",
    1.34 +
    1.35 +    flags: nsIAuthInformation.AUTH_HOST,
    1.36 +    authenticationScheme: "basic",
    1.37 +    realm: "secretrealm"
    1.38 +  };
    1.39 +
    1.40 +  const CALLED_PROMPT = 1 << 0;
    1.41 +  const CALLED_PROMPTUP = 1 << 1;
    1.42 +  const CALLED_PROMPTP = 1 << 2;
    1.43 +  function Prompt1() {}
    1.44 +  Prompt1.prototype = {
    1.45 +    called: 0,
    1.46 +    rv: true,
    1.47 +
    1.48 +    user: "foo\\bar",
    1.49 +    pw: "bar",
    1.50 +
    1.51 +    scheme: "http",
    1.52 +
    1.53 +    QueryInterface: function authprompt_qi(iid) {
    1.54 +      if (iid.equals(Components.interfaces.nsISupports) ||
    1.55 +          iid.equals(Components.interfaces.nsIAuthPrompt))
    1.56 +        return this;
    1.57 +      throw Components.results.NS_ERROR_NO_INTERFACE;
    1.58 +    },
    1.59 +
    1.60 +    prompt: function ap1_prompt(title, text, realm, save, defaultText, result) {
    1.61 +      this.called |= CALLED_PROMPT;
    1.62 +      this.doChecks(text, realm);
    1.63 +      return this.rv;
    1.64 +    },
    1.65 +
    1.66 +    promptUsernameAndPassword:
    1.67 +      function ap1_promptUP(title, text, realm, savePW, user, pw)
    1.68 +    {
    1.69 +      this.called |= CALLED_PROMPTUP;
    1.70 +      this.doChecks(text, realm);
    1.71 +      user.value = this.user;
    1.72 +      pw.value = this.pw;
    1.73 +      return this.rv;
    1.74 +    },
    1.75 +
    1.76 +    promptPassword: function ap1_promptPW(title, text, realm, save, pwd) {
    1.77 +      this.called |= CALLED_PROMPTP;
    1.78 +      this.doChecks(text, realm);
    1.79 +      pwd.value = this.pw;
    1.80 +      return this.rv;
    1.81 +    },
    1.82 +
    1.83 +    doChecks: function ap1_check(text, realm) {
    1.84 +      do_check_eq(this.scheme + "://" + host + " (" + info.realm + ")", realm);
    1.85 +
    1.86 +      do_check_neq(text.indexOf(host), -1);
    1.87 +      if (info.flags & nsIAuthInformation.ONLY_PASSWORD) {
    1.88 +        // Should have the username in the text
    1.89 +        do_check_neq(text.indexOf(info.username), -1);
    1.90 +      } else {
    1.91 +        // Make sure that we show the realm if we have one and that we don't
    1.92 +        // show "" otherwise
    1.93 +        if (info.realm != "")
    1.94 +          do_check_neq(text.indexOf(info.realm), -1);
    1.95 +        else
    1.96 +          do_check_eq(text.indexOf('""'), -1);
    1.97 +        // No explicit port in the URL; message should not contain -1
    1.98 +        // for those cases
    1.99 +        do_check_eq(text.indexOf("-1"), -1);
   1.100 +      }
   1.101 +    }
   1.102 +  };
   1.103 +
   1.104 +
   1.105 +  // Also have to make up a channel
   1.106 +  var ios = Components.classes["@mozilla.org/network/io-service;1"]
   1.107 +                      .getService(Components.interfaces.nsIIOService);
   1.108 +  var chan = ios.newChannel("http://" + host, "", null);
   1.109 +
   1.110 +  function do_tests(expectedRV) {
   1.111 +    var prompt1;
   1.112 +    var wrapper;
   1.113 +
   1.114 +    // 1: The simple case
   1.115 +    prompt1 = new Prompt1();
   1.116 +    prompt1.rv = expectedRV;
   1.117 +    wrapper = adapter.createAdapter(prompt1);
   1.118 +
   1.119 +    var rv = wrapper.promptAuth(chan, 0, info);
   1.120 +    do_check_eq(rv, prompt1.rv);
   1.121 +    do_check_eq(prompt1.called, CALLED_PROMPTUP);
   1.122 +
   1.123 +    if (rv) {
   1.124 +      do_check_eq(info.domain, "");
   1.125 +      do_check_eq(info.username, prompt1.user);
   1.126 +      do_check_eq(info.password, prompt1.pw);
   1.127 +    }
   1.128 +
   1.129 +    info.domain = "";
   1.130 +    info.username = "";
   1.131 +    info.password = "";
   1.132 +
   1.133 +    // 2: Only ask for a PW
   1.134 +    prompt1 = new Prompt1();
   1.135 +    prompt1.rv = expectedRV;
   1.136 +    info.flags |= nsIAuthInformation.ONLY_PASSWORD;
   1.137 +
   1.138 +    // Initialize the username so that the prompt can show it
   1.139 +    info.username = prompt1.user;
   1.140 +
   1.141 +    wrapper = adapter.createAdapter(prompt1);
   1.142 +    rv = wrapper.promptAuth(chan, 0, info);
   1.143 +    do_check_eq(rv, prompt1.rv);
   1.144 +    do_check_eq(prompt1.called, CALLED_PROMPTP);
   1.145 +
   1.146 +    if (rv) {
   1.147 +      do_check_eq(info.domain, "");
   1.148 +      do_check_eq(info.username, prompt1.user); // we initialized this
   1.149 +      do_check_eq(info.password, prompt1.pw);
   1.150 +    }
   1.151 +
   1.152 +    info.flags &= ~nsIAuthInformation.ONLY_PASSWORD;
   1.153 +
   1.154 +    info.domain = "";
   1.155 +    info.username = "";
   1.156 +    info.password = "";
   1.157 +
   1.158 +    // 3: user, pw and domain
   1.159 +    prompt1 = new Prompt1();
   1.160 +    prompt1.rv = expectedRV;
   1.161 +    info.flags |= nsIAuthInformation.NEED_DOMAIN;
   1.162 +
   1.163 +    wrapper = adapter.createAdapter(prompt1);
   1.164 +    rv = wrapper.promptAuth(chan, 0, info);
   1.165 +    do_check_eq(rv, prompt1.rv);
   1.166 +    do_check_eq(prompt1.called, CALLED_PROMPTUP);
   1.167 +
   1.168 +    if (rv) {
   1.169 +      do_check_eq(info.domain, "foo");
   1.170 +      do_check_eq(info.username, "bar");
   1.171 +      do_check_eq(info.password, prompt1.pw);
   1.172 +    }
   1.173 +
   1.174 +    info.flags &= ~nsIAuthInformation.NEED_DOMAIN;
   1.175 +
   1.176 +    info.domain = "";
   1.177 +    info.username = "";
   1.178 +    info.password = "";
   1.179 +
   1.180 +    // 4: username that doesn't contain a domain
   1.181 +    prompt1 = new Prompt1();
   1.182 +    prompt1.rv = expectedRV;
   1.183 +    info.flags |= nsIAuthInformation.NEED_DOMAIN;
   1.184 +
   1.185 +    prompt1.user = "foo";
   1.186 +
   1.187 +    wrapper = adapter.createAdapter(prompt1);
   1.188 +    rv = wrapper.promptAuth(chan, 0, info);
   1.189 +    do_check_eq(rv, prompt1.rv);
   1.190 +    do_check_eq(prompt1.called, CALLED_PROMPTUP);
   1.191 +
   1.192 +    if (rv) {
   1.193 +      do_check_eq(info.domain, "");
   1.194 +      do_check_eq(info.username, prompt1.user);
   1.195 +      do_check_eq(info.password, prompt1.pw);
   1.196 +    }
   1.197 +
   1.198 +    info.flags &= ~nsIAuthInformation.NEED_DOMAIN;
   1.199 +
   1.200 +    info.domain = "";
   1.201 +    info.username = "";
   1.202 +    info.password = "";
   1.203 +
   1.204 +    // 5: FTP
   1.205 +    var ftpchan = ios.newChannel("ftp://" + host, "", null);
   1.206 +
   1.207 +    prompt1 = new Prompt1();
   1.208 +    prompt1.rv = expectedRV;
   1.209 +    prompt1.scheme = "ftp";
   1.210 +
   1.211 +    wrapper = adapter.createAdapter(prompt1);
   1.212 +    var rv = wrapper.promptAuth(ftpchan, 0, info);
   1.213 +    do_check_eq(rv, prompt1.rv);
   1.214 +    do_check_eq(prompt1.called, CALLED_PROMPTUP);
   1.215 +
   1.216 +    if (rv) {
   1.217 +      do_check_eq(info.domain, "");
   1.218 +      do_check_eq(info.username, prompt1.user);
   1.219 +      do_check_eq(info.password, prompt1.pw);
   1.220 +    }
   1.221 +
   1.222 +    info.domain = "";
   1.223 +    info.username = "";
   1.224 +    info.password = "";
   1.225 +  }
   1.226 +  do_tests(true);
   1.227 +  do_tests(false);
   1.228 +}
   1.229 +

mercurial