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