dom/plugins/test/mochitest/test_clear_site_data.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/test/mochitest/test_clear_site_data.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,208 @@
     1.4 +<html>
     1.5 +<head>
     1.6 +  <title>NPAPI ClearSiteData/GetSitesWithData Functionality</title>
     1.7 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     1.8 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"></script>
     1.9 +  <script type="application/javascript" src="utils.js"></script>
    1.10 +</head>
    1.11 +<body>
    1.12 +  <script type="application/javascript">
    1.13 +    setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED);
    1.14 +  </script>
    1.15 +
    1.16 +  <embed id="plugin1" type="application/x-test" width="200" height="200"></embed>
    1.17 +
    1.18 +  <script class="testbody" type="application/javascript">
    1.19 +    SimpleTest.waitForExplicitFinish();
    1.20 +
    1.21 +    const pluginHostIface = Components.interfaces.nsIPluginHost;
    1.22 +    var pluginHost = Components.classes["@mozilla.org/plugin/host;1"].
    1.23 +                     getService(pluginHostIface);
    1.24 +    const FLAG_CLEAR_ALL = pluginHostIface.FLAG_CLEAR_ALL;
    1.25 +    const FLAG_CLEAR_CACHE = pluginHostIface.FLAG_CLEAR_CACHE;
    1.26 +
    1.27 +    var p = document.getElementById("plugin1");
    1.28 +
    1.29 +    // Since we're running with chrome permissions, accessing the plugin wont
    1.30 +    // synchronously spawn it -- wait for the async spawning to finish.
    1.31 +    SimpleTest.executeSoon(function() {
    1.32 +      // Make sure clearing by timerange is supported.
    1.33 +      p.setSitesWithDataCapabilities(true);
    1.34 +
    1.35 +      ok(PluginUtils.withTestPlugin(runTest), "Test plugin found");
    1.36 +      SimpleTest.finish();
    1.37 +    });
    1.38 +
    1.39 +    function stored(needles) {
    1.40 +      var something = pluginHost.siteHasData(this.pluginTag, null);
    1.41 +      if (!needles)
    1.42 +        return something;
    1.43 +
    1.44 +      if (!something)
    1.45 +        return false;
    1.46 +
    1.47 +      for (var i = 0; i < needles.length; ++i) {
    1.48 +        if (!pluginHost.siteHasData(this.pluginTag, needles[i]))
    1.49 +          return false;
    1.50 +      }
    1.51 +      return true;
    1.52 +    }
    1.53 +
    1.54 +    function checkThrows(fn, result) {
    1.55 +      try {
    1.56 +        fn();
    1.57 +        throw new Error("bad exception");
    1.58 +      } catch (e) {
    1.59 +        is(e.result, result, "Correct exception thrown");
    1.60 +      }
    1.61 +    }
    1.62 +
    1.63 +    function runTest(pluginTag) {
    1.64 +      this.pluginTag = pluginTag;
    1.65 +
    1.66 +      p.setSitesWithData(
    1.67 +        "foo.com:0:5," +
    1.68 +        "foo.com:0:7," +
    1.69 +        "bar.com:0:10," +
    1.70 +        "baz.com:0:10," +
    1.71 +        "foo.com:1:7," +
    1.72 +        "qux.com:1:5," +
    1.73 +        "quz.com:1:8"
    1.74 +      );
    1.75 +
    1.76 +      ok(stored(["foo.com","bar.com","baz.com","qux.com","quz.com"]),
    1.77 +        "Data stored for sites");
    1.78 +
    1.79 +      // Clear nothing.
    1.80 +      pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_ALL, 4);
    1.81 +      ok(stored(["foo.com","bar.com","baz.com","qux.com","quz.com"]),
    1.82 +         "Data stored for sites");
    1.83 +
    1.84 +      pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_CACHE, 4);
    1.85 +      ok(stored(["foo.com","bar.com","baz.com","qux.com","quz.com"]),
    1.86 +         "Data stored for sites");
    1.87 +
    1.88 +      // Clear cache data 5 seconds or older.
    1.89 +      pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_CACHE, 5);
    1.90 +      ok(stored(["foo.com","bar.com","baz.com","quz.com"]),
    1.91 +         "Data stored for sites");
    1.92 +      ok(!stored(["qux.com"]), "Data cleared for qux.com");
    1.93 +
    1.94 +      // Clear cache data for foo.com, but leave non-cache data.
    1.95 +      pluginHost.clearSiteData(pluginTag, "foo.com", FLAG_CLEAR_CACHE, 20);
    1.96 +      ok(stored(["foo.com","bar.com","baz.com","quz.com"]),
    1.97 +         "Data stored for sites");
    1.98 +
    1.99 +      // Clear all data 7 seconds or older.
   1.100 +      pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_ALL, 7);
   1.101 +      ok(stored(["bar.com","baz.com","quz.com"]), "Data stored for sites");
   1.102 +      ok(!stored(["foo.com"]), "Data cleared for foo.com");
   1.103 +      ok(!stored(["qux.com"]), "Data cleared for qux.com");
   1.104 +
   1.105 +      // Clear all cache data.
   1.106 +      pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_CACHE, 20);
   1.107 +      ok(stored(["bar.com","baz.com"]), "Data stored for sites");
   1.108 +      ok(!stored(["quz.com"]), "Data cleared for quz.com");
   1.109 +
   1.110 +      // Clear all data for bar.com.
   1.111 +      pluginHost.clearSiteData(pluginTag, "bar.com", FLAG_CLEAR_ALL, 20);
   1.112 +      ok(stored(["baz.com"]), "Data stored for baz.com");
   1.113 +      ok(!stored(["bar.com"]), "Data cleared for bar.com");
   1.114 +
   1.115 +      // Disable clearing by age.
   1.116 +      p.setSitesWithDataCapabilities(false);
   1.117 +
   1.118 +      // Attempt to clear data by age.
   1.119 +      checkThrows(function() {
   1.120 +        pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_ALL, 20);
   1.121 +      }, Components.results.NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED);
   1.122 +
   1.123 +      checkThrows(function() {
   1.124 +        pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_CACHE, 20);
   1.125 +      }, Components.results.NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED);
   1.126 +
   1.127 +      checkThrows(function() {
   1.128 +        pluginHost.clearSiteData(pluginTag, "baz.com", FLAG_CLEAR_ALL, 20);
   1.129 +      }, Components.results.NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED);
   1.130 +
   1.131 +      checkThrows(function() {
   1.132 +        pluginHost.clearSiteData(pluginTag, "baz.com", FLAG_CLEAR_CACHE, 20);
   1.133 +      }, Components.results.NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED);
   1.134 +
   1.135 +      // Clear cache for baz.com and globally for all ages.
   1.136 +      pluginHost.clearSiteData(pluginTag, "baz.com", FLAG_CLEAR_CACHE, -1);
   1.137 +      pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_CACHE, -1);
   1.138 +
   1.139 +      // Check that all of the above were no-ops.
   1.140 +      ok(stored(["baz.com"]), "Data stored for baz.com");
   1.141 +
   1.142 +      // Clear everything for baz.com.
   1.143 +      pluginHost.clearSiteData(pluginTag, "baz.com", FLAG_CLEAR_ALL, -1);
   1.144 +      ok(!stored(["baz.com"]), "Data cleared for baz.com");
   1.145 +      ok(!stored(null), "All data cleared");
   1.146 +
   1.147 +      // Set data to test subdomains, IP literals, and 'localhost'-like hosts.
   1.148 +      p.setSitesWithData(
   1.149 +        "foo.com:0:0," +
   1.150 +        "bar.foo.com:0:0," +
   1.151 +        "baz.foo.com:0:0," +
   1.152 +        "bar.com:0:0," +
   1.153 +        "[192.168.1.1]:0:0," +
   1.154 +        "localhost:0:0"
   1.155 +      );
   1.156 +
   1.157 +      ok(stored(["foo.com","nonexistent.foo.com","bar.com","192.168.1.1","localhost"]),
   1.158 +        "Data stored for sites");
   1.159 +
   1.160 +      // Clear data for "foo.com" and its subdomains.
   1.161 +      pluginHost.clearSiteData(pluginTag, "foo.com", FLAG_CLEAR_ALL, -1);
   1.162 +      ok(stored(["bar.com","192.168.1.1","localhost"]), "Data stored for sites");
   1.163 +      ok(!stored(["foo.com"]), "Data cleared for foo.com");
   1.164 +      ok(!stored(["bar.foo.com"]), "Data cleared for subdomains of foo.com");
   1.165 +
   1.166 +      // Clear data for "bar.com" using a subdomain.
   1.167 +      pluginHost.clearSiteData(pluginTag, "foo.bar.com", FLAG_CLEAR_ALL, -1);
   1.168 +      ok(!stored(["bar.com"]), "Data cleared for bar.com");
   1.169 +
   1.170 +      // Clear data for "192.168.1.1".
   1.171 +      pluginHost.clearSiteData(pluginTag, "192.168.1.1", FLAG_CLEAR_ALL, -1);
   1.172 +      ok(!stored(["192.168.1.1"]), "Data cleared for 192.168.1.1");
   1.173 +
   1.174 +      // Clear data for "localhost".
   1.175 +      pluginHost.clearSiteData(pluginTag, "localhost", FLAG_CLEAR_ALL, -1);
   1.176 +      ok(!stored(null), "All data cleared");
   1.177 +
   1.178 +      // Set data to test international domains.
   1.179 +      p.setSitesWithData(
   1.180 +        "b\u00FCcher.es:0:0," +
   1.181 +        "b\u00FCcher.uk:0:0," +
   1.182 +        "xn--bcher-kva.NZ:0:0"
   1.183 +      );
   1.184 +
   1.185 +      // Check that both the ACE and UTF-8 representations register.
   1.186 +      ok(stored(["b\u00FCcher.es","xn--bcher-kva.es","b\u00FCcher.uk","xn--bcher-kva.uk"]),
   1.187 +        "Data stored for sites");
   1.188 +
   1.189 +      // Clear data for the UTF-8 version.
   1.190 +      pluginHost.clearSiteData(pluginTag, "b\u00FCcher.es", FLAG_CLEAR_ALL, -1);
   1.191 +      ok(!stored(["b\u00FCcher.es"]), "Data cleared for UTF-8 representation");
   1.192 +      ok(!stored(["xn--bcher-kva.es"]), "Data cleared for ACE representation");
   1.193 +
   1.194 +      // Clear data for the ACE version.
   1.195 +      pluginHost.clearSiteData(pluginTag, "xn--bcher-kva.uk", FLAG_CLEAR_ALL, -1);
   1.196 +      ok(!stored(["b\u00FCcher.uk"]), "Data cleared for UTF-8 representation");
   1.197 +      ok(!stored(["xn--bcher-kva.uk"]), "Data cleared for ACE representation");
   1.198 +
   1.199 +      // The NPAPI spec requires that the plugin report sites in normalized
   1.200 +      // UTF-8. We do happen to normalize the result anyway, so while that's not
   1.201 +      // strictly required, we test it here.
   1.202 +      ok(stored(["b\u00FCcher.nz","xn--bcher-kva.nz"]),
   1.203 +        "Data stored for sites");
   1.204 +      pluginHost.clearSiteData(pluginTag, "b\u00FCcher.nz", FLAG_CLEAR_ALL, -1);
   1.205 +      ok(!stored(["b\u00FCcher.nz"]), "Data cleared for UTF-8 representation");
   1.206 +      ok(!stored(["xn--bcher-kva.nz"]), "Data cleared for ACE representation");
   1.207 +      ok(!stored(null), "All data cleared");
   1.208 +    }
   1.209 +  </script>
   1.210 +</body>
   1.211 +</html>

mercurial