extensions/cookie/test/unit/test_bug526789.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/cookie/test/unit/test_bug526789.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,249 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +function run_test() {
     1.8 +  var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
     1.9 +  var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
    1.10 +  var expiry = (Date.now() + 1000) * 1000;
    1.11 +
    1.12 +  cm.removeAll();
    1.13 +
    1.14 +  // Allow all cookies.
    1.15 +  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
    1.16 +
    1.17 +  // test that variants of 'baz.com' get normalized appropriately, but that
    1.18 +  // malformed hosts are rejected
    1.19 +  cm.add("baz.com", "/", "foo", "bar", false, false, true, expiry);
    1.20 +  do_check_eq(cm.countCookiesFromHost("baz.com"), 1);
    1.21 +  do_check_eq(cm.countCookiesFromHost("BAZ.com"), 1);
    1.22 +  do_check_eq(cm.countCookiesFromHost(".baz.com"), 1);
    1.23 +  do_check_eq(cm.countCookiesFromHost("baz.com."), 0);
    1.24 +  do_check_eq(cm.countCookiesFromHost(".baz.com."), 0);
    1.25 +  do_check_throws(function() {
    1.26 +    cm.countCookiesFromHost("baz.com..");
    1.27 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
    1.28 +  do_check_throws(function() {
    1.29 +    cm.countCookiesFromHost("baz..com");
    1.30 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
    1.31 +  do_check_throws(function() {
    1.32 +    cm.countCookiesFromHost("..baz.com");
    1.33 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
    1.34 +  cm.remove("BAZ.com.", "foo", "/", false);
    1.35 +  do_check_eq(cm.countCookiesFromHost("baz.com"), 1);
    1.36 +  cm.remove("baz.com", "foo", "/", false);
    1.37 +  do_check_eq(cm.countCookiesFromHost("baz.com"), 0);
    1.38 +
    1.39 +  // Test that 'baz.com' and 'baz.com.' are treated differently
    1.40 +  cm.add("baz.com.", "/", "foo", "bar", false, false, true, expiry);
    1.41 +  do_check_eq(cm.countCookiesFromHost("baz.com"), 0);
    1.42 +  do_check_eq(cm.countCookiesFromHost("BAZ.com"), 0);
    1.43 +  do_check_eq(cm.countCookiesFromHost(".baz.com"), 0);
    1.44 +  do_check_eq(cm.countCookiesFromHost("baz.com."), 1);
    1.45 +  do_check_eq(cm.countCookiesFromHost(".baz.com."), 1);
    1.46 +  cm.remove("baz.com", "foo", "/", false);
    1.47 +  do_check_eq(cm.countCookiesFromHost("baz.com."), 1);
    1.48 +  cm.remove("baz.com.", "foo", "/", false);
    1.49 +  do_check_eq(cm.countCookiesFromHost("baz.com."), 0);
    1.50 +
    1.51 +  // test that domain cookies are illegal for IP addresses, aliases such as
    1.52 +  // 'localhost', and eTLD's such as 'co.uk'
    1.53 +  cm.add("192.168.0.1", "/", "foo", "bar", false, false, true, expiry);
    1.54 +  do_check_eq(cm.countCookiesFromHost("192.168.0.1"), 1);
    1.55 +  do_check_eq(cm.countCookiesFromHost("192.168.0.1."), 0);
    1.56 +  do_check_throws(function() {
    1.57 +    cm.countCookiesFromHost(".192.168.0.1");
    1.58 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
    1.59 +  do_check_throws(function() {
    1.60 +    cm.countCookiesFromHost(".192.168.0.1.");
    1.61 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
    1.62 +
    1.63 +  cm.add("localhost", "/", "foo", "bar", false, false, true, expiry);
    1.64 +  do_check_eq(cm.countCookiesFromHost("localhost"), 1);
    1.65 +  do_check_eq(cm.countCookiesFromHost("localhost."), 0);
    1.66 +  do_check_throws(function() {
    1.67 +    cm.countCookiesFromHost(".localhost");
    1.68 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
    1.69 +  do_check_throws(function() {
    1.70 +    cm.countCookiesFromHost(".localhost.");
    1.71 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
    1.72 +
    1.73 +  cm.add("co.uk", "/", "foo", "bar", false, false, true, expiry);
    1.74 +  do_check_eq(cm.countCookiesFromHost("co.uk"), 1);
    1.75 +  do_check_eq(cm.countCookiesFromHost("co.uk."), 0);
    1.76 +  do_check_throws(function() {
    1.77 +    cm.countCookiesFromHost(".co.uk");
    1.78 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
    1.79 +  do_check_throws(function() {
    1.80 +    cm.countCookiesFromHost(".co.uk.");
    1.81 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
    1.82 +
    1.83 +  cm.removeAll();
    1.84 +
    1.85 +  // test that setting an empty or '.' http:// host results in a no-op
    1.86 +  var uri = NetUtil.newURI("http://baz.com/");
    1.87 +  var emptyuri = NetUtil.newURI("http:///");
    1.88 +  var doturi = NetUtil.newURI("http://./");
    1.89 +  do_check_eq(uri.asciiHost, "baz.com");
    1.90 +  do_check_eq(emptyuri.asciiHost, "");
    1.91 +  do_check_eq(doturi.asciiHost, ".");
    1.92 +  cs.setCookieString(emptyuri, null, "foo2=bar", null);
    1.93 +  do_check_eq(getCookieCount(), 0);
    1.94 +  cs.setCookieString(doturi, null, "foo3=bar", null);
    1.95 +  do_check_eq(getCookieCount(), 0);
    1.96 +  cs.setCookieString(uri, null, "foo=bar", null);
    1.97 +  do_check_eq(getCookieCount(), 1);
    1.98 +
    1.99 +  do_check_eq(cs.getCookieString(uri, null), "foo=bar");
   1.100 +  do_check_eq(cs.getCookieString(emptyuri, null), null);
   1.101 +  do_check_eq(cs.getCookieString(doturi, null), null);
   1.102 +
   1.103 +  do_check_eq(cm.countCookiesFromHost(""), 0);
   1.104 +  do_check_throws(function() {
   1.105 +    cm.countCookiesFromHost(".");
   1.106 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.107 +  do_check_throws(function() {
   1.108 +    cm.countCookiesFromHost("..");
   1.109 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.110 +
   1.111 +  var e = cm.getCookiesFromHost("");
   1.112 +  do_check_false(e.hasMoreElements());
   1.113 +  do_check_throws(function() {
   1.114 +    cm.getCookiesFromHost(".");
   1.115 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.116 +  do_check_throws(function() {
   1.117 +    cm.getCookiesFromHost("..");
   1.118 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.119 +
   1.120 +  e = cm.getCookiesFromHost("baz.com");
   1.121 +  do_check_true(e.hasMoreElements());
   1.122 +  do_check_eq(e.getNext().QueryInterface(Ci.nsICookie2).name, "foo");
   1.123 +  do_check_false(e.hasMoreElements());
   1.124 +  e = cm.getCookiesFromHost("");
   1.125 +  do_check_false(e.hasMoreElements());
   1.126 +  do_check_throws(function() {
   1.127 +    cm.getCookiesFromHost(".");
   1.128 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.129 +  do_check_throws(function() {
   1.130 +    cm.getCookiesFromHost("..");
   1.131 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.132 +
   1.133 +  cm.removeAll();
   1.134 +
   1.135 +  // test that an empty file:// host works
   1.136 +  emptyuri = NetUtil.newURI("file:///");
   1.137 +  do_check_eq(emptyuri.asciiHost, "");
   1.138 +  do_check_eq(NetUtil.newURI("file://./").asciiHost, "");
   1.139 +  do_check_eq(NetUtil.newURI("file://foo.bar/").asciiHost, "");
   1.140 +  cs.setCookieString(emptyuri, null, "foo2=bar", null);
   1.141 +  do_check_eq(getCookieCount(), 1);
   1.142 +  cs.setCookieString(emptyuri, null, "foo3=bar; domain=", null);
   1.143 +  do_check_eq(getCookieCount(), 2);
   1.144 +  cs.setCookieString(emptyuri, null, "foo4=bar; domain=.", null);
   1.145 +  do_check_eq(getCookieCount(), 2);
   1.146 +  cs.setCookieString(emptyuri, null, "foo5=bar; domain=bar.com", null);
   1.147 +  do_check_eq(getCookieCount(), 2);
   1.148 +
   1.149 +  do_check_eq(cs.getCookieString(emptyuri, null), "foo2=bar; foo3=bar");
   1.150 +
   1.151 +  do_check_eq(cm.countCookiesFromHost("baz.com"), 0);
   1.152 +  do_check_eq(cm.countCookiesFromHost(""), 2);
   1.153 +  do_check_throws(function() {
   1.154 +    cm.countCookiesFromHost(".");
   1.155 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.156 +
   1.157 +  e = cm.getCookiesFromHost("baz.com");
   1.158 +  do_check_false(e.hasMoreElements());
   1.159 +  e = cm.getCookiesFromHost("");
   1.160 +  do_check_true(e.hasMoreElements());
   1.161 +  e.getNext();
   1.162 +  do_check_true(e.hasMoreElements());
   1.163 +  e.getNext();
   1.164 +  do_check_false(e.hasMoreElements());
   1.165 +  do_check_throws(function() {
   1.166 +    cm.getCookiesFromHost(".");
   1.167 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.168 +
   1.169 +  cm.removeAll();
   1.170 +
   1.171 +  // test that an empty host to add() or remove() works,
   1.172 +  // but a host of '.' doesn't
   1.173 +  cm.add("", "/", "foo2", "bar", false, false, true, expiry);
   1.174 +  do_check_eq(getCookieCount(), 1);
   1.175 +  do_check_throws(function() {
   1.176 +    cm.add(".", "/", "foo3", "bar", false, false, true, expiry);
   1.177 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.178 +  do_check_eq(getCookieCount(), 1);
   1.179 +
   1.180 +  cm.remove("", "foo2", "/", false);
   1.181 +  do_check_eq(getCookieCount(), 0);
   1.182 +  do_check_throws(function() {
   1.183 +    cm.remove(".", "foo3", "/", false);
   1.184 +  }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.185 +
   1.186 +  // test that the 'domain' attribute accepts a leading dot for IP addresses,
   1.187 +  // aliases such as 'localhost', and eTLD's such as 'co.uk'; but that the
   1.188 +  // resulting cookie is for the exact host only.
   1.189 +  testDomainCookie("http://192.168.0.1/", "192.168.0.1");
   1.190 +  testDomainCookie("http://localhost/", "localhost");
   1.191 +  testDomainCookie("http://co.uk/", "co.uk");
   1.192 +
   1.193 +  // Test that trailing dots are treated differently for purposes of the
   1.194 +  // 'domain' attribute when using setCookieString.
   1.195 +  testTrailingDotCookie("http://192.168.0.1", "192.168.0.1");
   1.196 +  testTrailingDotCookie("http://localhost", "localhost");
   1.197 +  testTrailingDotCookie("http://foo.com", "foo.com");
   1.198 +
   1.199 +  cm.removeAll();
   1.200 +}
   1.201 +
   1.202 +function getCookieCount() {
   1.203 +  var count = 0;
   1.204 +  var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
   1.205 +  var enumerator = cm.enumerator;
   1.206 +  while (enumerator.hasMoreElements()) {
   1.207 +    if (!(enumerator.getNext() instanceof Ci.nsICookie2))
   1.208 +      throw new Error("not a cookie");
   1.209 +    ++count;
   1.210 +  }
   1.211 +  return count;
   1.212 +}
   1.213 +
   1.214 +function testDomainCookie(uriString, domain) {
   1.215 +  var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
   1.216 +  var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
   1.217 +
   1.218 +  cm.removeAll();
   1.219 +
   1.220 +  var uri = NetUtil.newURI(uriString);
   1.221 +  cs.setCookieString(uri, null, "foo=bar; domain=" + domain, null);
   1.222 +  var e = cm.getCookiesFromHost(domain);
   1.223 +  do_check_true(e.hasMoreElements());
   1.224 +  do_check_eq(e.getNext().QueryInterface(Ci.nsICookie2).host, domain);
   1.225 +  cm.removeAll();
   1.226 +
   1.227 +  cs.setCookieString(uri, null, "foo=bar; domain=." + domain, null);
   1.228 +  e = cm.getCookiesFromHost(domain);
   1.229 +  do_check_true(e.hasMoreElements());
   1.230 +  do_check_eq(e.getNext().QueryInterface(Ci.nsICookie2).host, domain);
   1.231 +  cm.removeAll();
   1.232 +}
   1.233 +
   1.234 +function testTrailingDotCookie(uriString, domain) {
   1.235 +  var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
   1.236 +  var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
   1.237 +
   1.238 +  cm.removeAll();
   1.239 +
   1.240 +  var uri = NetUtil.newURI(uriString);
   1.241 +  cs.setCookieString(uri, null, "foo=bar; domain=" + domain + ".", null);
   1.242 +  do_check_eq(cm.countCookiesFromHost(domain), 0);
   1.243 +  do_check_eq(cm.countCookiesFromHost(domain + "."), 0);
   1.244 +  cm.removeAll();
   1.245 +
   1.246 +  uri = NetUtil.newURI(uriString + ".");
   1.247 +  cs.setCookieString(uri, null, "foo=bar; domain=" + domain, null);
   1.248 +  do_check_eq(cm.countCookiesFromHost(domain), 0);
   1.249 +  do_check_eq(cm.countCookiesFromHost(domain + "."), 0);
   1.250 +  cm.removeAll();
   1.251 +}
   1.252 +

mercurial