Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const nsIBLS = Components.interfaces.nsIBlocklistService;
6 Components.utils.import("resource://testing-common/httpd.js");
8 var gBlocklistService = null;
9 var gNotifier = null;
10 var gNextTest = null;
11 var gPluginHost = null;
13 var gServer = new HttpServer();
14 gServer.start(-1);
15 gPort = gServer.identity.primaryPort;
16 mapFile("/data/test_pluginBlocklistCtp.xml", gServer);
17 mapFile("/data/test_pluginBlocklistCtpUndo.xml", gServer);
19 var PLUGINS = [{
20 // severity=0, vulnerabilitystatus=0 -> outdated
21 name: "test_plugin_0",
22 version: "5",
23 disabled: false,
24 blocklisted: false
25 },
26 {
27 // severity=0, vulnerabilitystatus=1 -> update available
28 name: "test_plugin_1",
29 version: "5",
30 disabled: false,
31 blocklisted: false
32 },
33 {
34 // severity=0, vulnerabilitystatus=2 -> no update
35 name: "test_plugin_2",
36 version: "5",
37 disabled: false,
38 blocklisted: false
39 },
40 {
41 // no severity field -> severity=3 by default -> hardblock
42 name: "test_plugin_3",
43 version: "5",
44 disabled: false,
45 blocklisted: false
46 },
47 {
48 // severity=1, vulnerabilitystatus=2 -> softblock
49 name: "test_plugin_4",
50 version: "5",
51 disabled: false,
52 blocklisted: false
53 },
54 {
55 // not in the blocklist -> not blocked
56 name: "test_plugin_5",
57 version: "5",
58 disabled: false,
59 blocklisted: false
60 }];
62 function test_basic() {
63 var blocklist = Components.classes["@mozilla.org/extensions/blocklist;1"].getService(nsIBLS);
65 do_check_true(blocklist.getPluginBlocklistState(PLUGINS[0], "1", "1.9") == nsIBLS.STATE_OUTDATED);
67 do_check_true(blocklist.getPluginBlocklistState(PLUGINS[1], "1", "1.9") == nsIBLS.STATE_VULNERABLE_UPDATE_AVAILABLE);
69 do_check_true(blocklist.getPluginBlocklistState(PLUGINS[2], "1", "1.9") == nsIBLS.STATE_VULNERABLE_NO_UPDATE);
71 do_check_true(blocklist.getPluginBlocklistState(PLUGINS[3], "1", "1.9") == nsIBLS.STATE_BLOCKED);
73 do_check_true(blocklist.getPluginBlocklistState(PLUGINS[4], "1", "1.9") == nsIBLS.STATE_SOFTBLOCKED);
75 do_check_true(blocklist.getPluginBlocklistState(PLUGINS[5], "1", "1.9") == nsIBLS.STATE_NOT_BLOCKED);
77 gNextTest = test_is_not_clicktoplay;
78 do_execute_soon(gNextTest);
79 }
81 function get_test_plugin() {
82 var pluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost);
83 for (var plugin of pluginHost.getPluginTags()) {
84 if (plugin.name == "Test Plug-in")
85 return plugin;
86 }
87 do_check_true(false);
88 return null;
89 }
91 // At this time, the blocklist does not have an entry for the test plugin,
92 // so it shouldn't be click-to-play.
93 function test_is_not_clicktoplay() {
94 var plugin = get_test_plugin();
95 var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9");
96 do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_UPDATE_AVAILABLE);
97 do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE);
99 Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtpUndo.xml");
100 gNextTest = test_is_clicktoplay;
101 gNotifier.notify(null);
102 }
104 // Here, we've updated the blocklist to have a block for the test plugin,
105 // so it should be click-to-play.
106 function test_is_clicktoplay() {
107 var plugin = get_test_plugin();
108 var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9");
109 do_check_eq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE);
111 Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtp.xml");
112 gNextTest = test_is_not_clicktoplay2;
113 gNotifier.notify(null);
114 }
116 // But now we've removed that entry from the blocklist (really we've gone back
117 // to the old one), so the plugin shouldn't be click-to-play any more.
118 function test_is_not_clicktoplay2() {
119 var plugin = get_test_plugin();
120 var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9");
121 do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_UPDATE_AVAILABLE);
122 do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE);
124 Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtpUndo.xml");
125 gNextTest = test_disable_blocklist;
126 gNotifier.notify(null);
127 }
129 // Test that disabling the blocklist when a plugin is ctp-blocklisted will
130 // result in the plugin not being click-to-play.
131 function test_disable_blocklist() {
132 var plugin = get_test_plugin();
133 var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9");
134 do_check_eq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE);
136 gNextTest = null;
137 Services.prefs.setBoolPref("extensions.blocklist.enabled", false);
138 blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9");
139 do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE);
140 do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_UPDATE_AVAILABLE);
142 // it should still be possible to make a plugin click-to-play via the pref
143 // and setting that plugin's enabled state to click-to-play
144 Services.prefs.setBoolPref("plugins.click_to_play", true);
145 let previousEnabledState = plugin.enabledState;
146 plugin.enabledState = Components.interfaces.nsIPluginTag.STATE_CLICKTOPLAY;
147 do_check_eq(gPluginHost.getStateForType("application/x-test"), Components.interfaces.nsIPluginTag.STATE_CLICKTOPLAY);
148 // clean up plugin state
149 plugin.enabledState = previousEnabledState;
151 gServer.stop(do_test_finished);
152 }
154 // Observe "blocklist-updated" so we know when to advance to the next test
155 function observer() {
156 if (gNextTest)
157 do_execute_soon(gNextTest);
158 }
160 function run_test() {
161 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
163 Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtp.xml");
164 startupManager();
166 gPluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost);
167 gBlocklistService = Components.classes["@mozilla.org/extensions/blocklist;1"].getService(Components.interfaces.nsIBlocklistService);
168 gNotifier = Components.classes["@mozilla.org/extensions/blocklist;1"].getService(Components.interfaces.nsITimerCallback);
169 Services.obs.addObserver(observer, "blocklist-updated", false);
171 do_register_cleanup(function() {
172 Services.prefs.clearUserPref("extensions.blocklist.url");
173 Services.prefs.clearUserPref("extensions.blocklist.enabled");
174 Services.prefs.clearUserPref("plugins.click_to_play");
175 Services.obs.removeObserver(observer, "blocklist-updated");
176 });
178 gNextTest = test_basic;
179 do_test_pending();
180 gNotifier.notify(null);
181 }