Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <html> |
michael@0 | 2 | <head> |
michael@0 | 3 | <title>NPAPI ClearSiteData/GetSitesWithData Functionality</title> |
michael@0 | 4 | <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 5 | <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"></script> |
michael@0 | 6 | <script type="application/javascript" src="utils.js"></script> |
michael@0 | 7 | </head> |
michael@0 | 8 | <body> |
michael@0 | 9 | <script type="application/javascript"> |
michael@0 | 10 | setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED); |
michael@0 | 11 | </script> |
michael@0 | 12 | |
michael@0 | 13 | <embed id="plugin1" type="application/x-test" width="200" height="200"></embed> |
michael@0 | 14 | |
michael@0 | 15 | <script class="testbody" type="application/javascript"> |
michael@0 | 16 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 17 | |
michael@0 | 18 | const pluginHostIface = Components.interfaces.nsIPluginHost; |
michael@0 | 19 | var pluginHost = Components.classes["@mozilla.org/plugin/host;1"]. |
michael@0 | 20 | getService(pluginHostIface); |
michael@0 | 21 | const FLAG_CLEAR_ALL = pluginHostIface.FLAG_CLEAR_ALL; |
michael@0 | 22 | const FLAG_CLEAR_CACHE = pluginHostIface.FLAG_CLEAR_CACHE; |
michael@0 | 23 | |
michael@0 | 24 | var p = document.getElementById("plugin1"); |
michael@0 | 25 | |
michael@0 | 26 | // Since we're running with chrome permissions, accessing the plugin wont |
michael@0 | 27 | // synchronously spawn it -- wait for the async spawning to finish. |
michael@0 | 28 | SimpleTest.executeSoon(function() { |
michael@0 | 29 | // Make sure clearing by timerange is supported. |
michael@0 | 30 | p.setSitesWithDataCapabilities(true); |
michael@0 | 31 | |
michael@0 | 32 | ok(PluginUtils.withTestPlugin(runTest), "Test plugin found"); |
michael@0 | 33 | SimpleTest.finish(); |
michael@0 | 34 | }); |
michael@0 | 35 | |
michael@0 | 36 | function stored(needles) { |
michael@0 | 37 | var something = pluginHost.siteHasData(this.pluginTag, null); |
michael@0 | 38 | if (!needles) |
michael@0 | 39 | return something; |
michael@0 | 40 | |
michael@0 | 41 | if (!something) |
michael@0 | 42 | return false; |
michael@0 | 43 | |
michael@0 | 44 | for (var i = 0; i < needles.length; ++i) { |
michael@0 | 45 | if (!pluginHost.siteHasData(this.pluginTag, needles[i])) |
michael@0 | 46 | return false; |
michael@0 | 47 | } |
michael@0 | 48 | return true; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | function checkThrows(fn, result) { |
michael@0 | 52 | try { |
michael@0 | 53 | fn(); |
michael@0 | 54 | throw new Error("bad exception"); |
michael@0 | 55 | } catch (e) { |
michael@0 | 56 | is(e.result, result, "Correct exception thrown"); |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | function runTest(pluginTag) { |
michael@0 | 61 | this.pluginTag = pluginTag; |
michael@0 | 62 | |
michael@0 | 63 | p.setSitesWithData( |
michael@0 | 64 | "foo.com:0:5," + |
michael@0 | 65 | "foo.com:0:7," + |
michael@0 | 66 | "bar.com:0:10," + |
michael@0 | 67 | "baz.com:0:10," + |
michael@0 | 68 | "foo.com:1:7," + |
michael@0 | 69 | "qux.com:1:5," + |
michael@0 | 70 | "quz.com:1:8" |
michael@0 | 71 | ); |
michael@0 | 72 | |
michael@0 | 73 | ok(stored(["foo.com","bar.com","baz.com","qux.com","quz.com"]), |
michael@0 | 74 | "Data stored for sites"); |
michael@0 | 75 | |
michael@0 | 76 | // Clear nothing. |
michael@0 | 77 | pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_ALL, 4); |
michael@0 | 78 | ok(stored(["foo.com","bar.com","baz.com","qux.com","quz.com"]), |
michael@0 | 79 | "Data stored for sites"); |
michael@0 | 80 | |
michael@0 | 81 | pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_CACHE, 4); |
michael@0 | 82 | ok(stored(["foo.com","bar.com","baz.com","qux.com","quz.com"]), |
michael@0 | 83 | "Data stored for sites"); |
michael@0 | 84 | |
michael@0 | 85 | // Clear cache data 5 seconds or older. |
michael@0 | 86 | pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_CACHE, 5); |
michael@0 | 87 | ok(stored(["foo.com","bar.com","baz.com","quz.com"]), |
michael@0 | 88 | "Data stored for sites"); |
michael@0 | 89 | ok(!stored(["qux.com"]), "Data cleared for qux.com"); |
michael@0 | 90 | |
michael@0 | 91 | // Clear cache data for foo.com, but leave non-cache data. |
michael@0 | 92 | pluginHost.clearSiteData(pluginTag, "foo.com", FLAG_CLEAR_CACHE, 20); |
michael@0 | 93 | ok(stored(["foo.com","bar.com","baz.com","quz.com"]), |
michael@0 | 94 | "Data stored for sites"); |
michael@0 | 95 | |
michael@0 | 96 | // Clear all data 7 seconds or older. |
michael@0 | 97 | pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_ALL, 7); |
michael@0 | 98 | ok(stored(["bar.com","baz.com","quz.com"]), "Data stored for sites"); |
michael@0 | 99 | ok(!stored(["foo.com"]), "Data cleared for foo.com"); |
michael@0 | 100 | ok(!stored(["qux.com"]), "Data cleared for qux.com"); |
michael@0 | 101 | |
michael@0 | 102 | // Clear all cache data. |
michael@0 | 103 | pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_CACHE, 20); |
michael@0 | 104 | ok(stored(["bar.com","baz.com"]), "Data stored for sites"); |
michael@0 | 105 | ok(!stored(["quz.com"]), "Data cleared for quz.com"); |
michael@0 | 106 | |
michael@0 | 107 | // Clear all data for bar.com. |
michael@0 | 108 | pluginHost.clearSiteData(pluginTag, "bar.com", FLAG_CLEAR_ALL, 20); |
michael@0 | 109 | ok(stored(["baz.com"]), "Data stored for baz.com"); |
michael@0 | 110 | ok(!stored(["bar.com"]), "Data cleared for bar.com"); |
michael@0 | 111 | |
michael@0 | 112 | // Disable clearing by age. |
michael@0 | 113 | p.setSitesWithDataCapabilities(false); |
michael@0 | 114 | |
michael@0 | 115 | // Attempt to clear data by age. |
michael@0 | 116 | checkThrows(function() { |
michael@0 | 117 | pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_ALL, 20); |
michael@0 | 118 | }, Components.results.NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED); |
michael@0 | 119 | |
michael@0 | 120 | checkThrows(function() { |
michael@0 | 121 | pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_CACHE, 20); |
michael@0 | 122 | }, Components.results.NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED); |
michael@0 | 123 | |
michael@0 | 124 | checkThrows(function() { |
michael@0 | 125 | pluginHost.clearSiteData(pluginTag, "baz.com", FLAG_CLEAR_ALL, 20); |
michael@0 | 126 | }, Components.results.NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED); |
michael@0 | 127 | |
michael@0 | 128 | checkThrows(function() { |
michael@0 | 129 | pluginHost.clearSiteData(pluginTag, "baz.com", FLAG_CLEAR_CACHE, 20); |
michael@0 | 130 | }, Components.results.NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED); |
michael@0 | 131 | |
michael@0 | 132 | // Clear cache for baz.com and globally for all ages. |
michael@0 | 133 | pluginHost.clearSiteData(pluginTag, "baz.com", FLAG_CLEAR_CACHE, -1); |
michael@0 | 134 | pluginHost.clearSiteData(pluginTag, null, FLAG_CLEAR_CACHE, -1); |
michael@0 | 135 | |
michael@0 | 136 | // Check that all of the above were no-ops. |
michael@0 | 137 | ok(stored(["baz.com"]), "Data stored for baz.com"); |
michael@0 | 138 | |
michael@0 | 139 | // Clear everything for baz.com. |
michael@0 | 140 | pluginHost.clearSiteData(pluginTag, "baz.com", FLAG_CLEAR_ALL, -1); |
michael@0 | 141 | ok(!stored(["baz.com"]), "Data cleared for baz.com"); |
michael@0 | 142 | ok(!stored(null), "All data cleared"); |
michael@0 | 143 | |
michael@0 | 144 | // Set data to test subdomains, IP literals, and 'localhost'-like hosts. |
michael@0 | 145 | p.setSitesWithData( |
michael@0 | 146 | "foo.com:0:0," + |
michael@0 | 147 | "bar.foo.com:0:0," + |
michael@0 | 148 | "baz.foo.com:0:0," + |
michael@0 | 149 | "bar.com:0:0," + |
michael@0 | 150 | "[192.168.1.1]:0:0," + |
michael@0 | 151 | "localhost:0:0" |
michael@0 | 152 | ); |
michael@0 | 153 | |
michael@0 | 154 | ok(stored(["foo.com","nonexistent.foo.com","bar.com","192.168.1.1","localhost"]), |
michael@0 | 155 | "Data stored for sites"); |
michael@0 | 156 | |
michael@0 | 157 | // Clear data for "foo.com" and its subdomains. |
michael@0 | 158 | pluginHost.clearSiteData(pluginTag, "foo.com", FLAG_CLEAR_ALL, -1); |
michael@0 | 159 | ok(stored(["bar.com","192.168.1.1","localhost"]), "Data stored for sites"); |
michael@0 | 160 | ok(!stored(["foo.com"]), "Data cleared for foo.com"); |
michael@0 | 161 | ok(!stored(["bar.foo.com"]), "Data cleared for subdomains of foo.com"); |
michael@0 | 162 | |
michael@0 | 163 | // Clear data for "bar.com" using a subdomain. |
michael@0 | 164 | pluginHost.clearSiteData(pluginTag, "foo.bar.com", FLAG_CLEAR_ALL, -1); |
michael@0 | 165 | ok(!stored(["bar.com"]), "Data cleared for bar.com"); |
michael@0 | 166 | |
michael@0 | 167 | // Clear data for "192.168.1.1". |
michael@0 | 168 | pluginHost.clearSiteData(pluginTag, "192.168.1.1", FLAG_CLEAR_ALL, -1); |
michael@0 | 169 | ok(!stored(["192.168.1.1"]), "Data cleared for 192.168.1.1"); |
michael@0 | 170 | |
michael@0 | 171 | // Clear data for "localhost". |
michael@0 | 172 | pluginHost.clearSiteData(pluginTag, "localhost", FLAG_CLEAR_ALL, -1); |
michael@0 | 173 | ok(!stored(null), "All data cleared"); |
michael@0 | 174 | |
michael@0 | 175 | // Set data to test international domains. |
michael@0 | 176 | p.setSitesWithData( |
michael@0 | 177 | "b\u00FCcher.es:0:0," + |
michael@0 | 178 | "b\u00FCcher.uk:0:0," + |
michael@0 | 179 | "xn--bcher-kva.NZ:0:0" |
michael@0 | 180 | ); |
michael@0 | 181 | |
michael@0 | 182 | // Check that both the ACE and UTF-8 representations register. |
michael@0 | 183 | ok(stored(["b\u00FCcher.es","xn--bcher-kva.es","b\u00FCcher.uk","xn--bcher-kva.uk"]), |
michael@0 | 184 | "Data stored for sites"); |
michael@0 | 185 | |
michael@0 | 186 | // Clear data for the UTF-8 version. |
michael@0 | 187 | pluginHost.clearSiteData(pluginTag, "b\u00FCcher.es", FLAG_CLEAR_ALL, -1); |
michael@0 | 188 | ok(!stored(["b\u00FCcher.es"]), "Data cleared for UTF-8 representation"); |
michael@0 | 189 | ok(!stored(["xn--bcher-kva.es"]), "Data cleared for ACE representation"); |
michael@0 | 190 | |
michael@0 | 191 | // Clear data for the ACE version. |
michael@0 | 192 | pluginHost.clearSiteData(pluginTag, "xn--bcher-kva.uk", FLAG_CLEAR_ALL, -1); |
michael@0 | 193 | ok(!stored(["b\u00FCcher.uk"]), "Data cleared for UTF-8 representation"); |
michael@0 | 194 | ok(!stored(["xn--bcher-kva.uk"]), "Data cleared for ACE representation"); |
michael@0 | 195 | |
michael@0 | 196 | // The NPAPI spec requires that the plugin report sites in normalized |
michael@0 | 197 | // UTF-8. We do happen to normalize the result anyway, so while that's not |
michael@0 | 198 | // strictly required, we test it here. |
michael@0 | 199 | ok(stored(["b\u00FCcher.nz","xn--bcher-kva.nz"]), |
michael@0 | 200 | "Data stored for sites"); |
michael@0 | 201 | pluginHost.clearSiteData(pluginTag, "b\u00FCcher.nz", FLAG_CLEAR_ALL, -1); |
michael@0 | 202 | ok(!stored(["b\u00FCcher.nz"]), "Data cleared for UTF-8 representation"); |
michael@0 | 203 | ok(!stored(["xn--bcher-kva.nz"]), "Data cleared for ACE representation"); |
michael@0 | 204 | ok(!stored(null), "All data cleared"); |
michael@0 | 205 | } |
michael@0 | 206 | </script> |
michael@0 | 207 | </body> |
michael@0 | 208 | </html> |