1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_pluginBlocklistCtp.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,181 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const nsIBLS = Components.interfaces.nsIBlocklistService; 1.9 +Components.utils.import("resource://testing-common/httpd.js"); 1.10 + 1.11 +var gBlocklistService = null; 1.12 +var gNotifier = null; 1.13 +var gNextTest = null; 1.14 +var gPluginHost = null; 1.15 + 1.16 +var gServer = new HttpServer(); 1.17 +gServer.start(-1); 1.18 +gPort = gServer.identity.primaryPort; 1.19 +mapFile("/data/test_pluginBlocklistCtp.xml", gServer); 1.20 +mapFile("/data/test_pluginBlocklistCtpUndo.xml", gServer); 1.21 + 1.22 +var PLUGINS = [{ 1.23 + // severity=0, vulnerabilitystatus=0 -> outdated 1.24 + name: "test_plugin_0", 1.25 + version: "5", 1.26 + disabled: false, 1.27 + blocklisted: false 1.28 +}, 1.29 +{ 1.30 + // severity=0, vulnerabilitystatus=1 -> update available 1.31 + name: "test_plugin_1", 1.32 + version: "5", 1.33 + disabled: false, 1.34 + blocklisted: false 1.35 +}, 1.36 +{ 1.37 + // severity=0, vulnerabilitystatus=2 -> no update 1.38 + name: "test_plugin_2", 1.39 + version: "5", 1.40 + disabled: false, 1.41 + blocklisted: false 1.42 +}, 1.43 +{ 1.44 + // no severity field -> severity=3 by default -> hardblock 1.45 + name: "test_plugin_3", 1.46 + version: "5", 1.47 + disabled: false, 1.48 + blocklisted: false 1.49 +}, 1.50 +{ 1.51 + // severity=1, vulnerabilitystatus=2 -> softblock 1.52 + name: "test_plugin_4", 1.53 + version: "5", 1.54 + disabled: false, 1.55 + blocklisted: false 1.56 +}, 1.57 +{ 1.58 + // not in the blocklist -> not blocked 1.59 + name: "test_plugin_5", 1.60 + version: "5", 1.61 + disabled: false, 1.62 + blocklisted: false 1.63 +}]; 1.64 + 1.65 +function test_basic() { 1.66 + var blocklist = Components.classes["@mozilla.org/extensions/blocklist;1"].getService(nsIBLS); 1.67 + 1.68 + do_check_true(blocklist.getPluginBlocklistState(PLUGINS[0], "1", "1.9") == nsIBLS.STATE_OUTDATED); 1.69 + 1.70 + do_check_true(blocklist.getPluginBlocklistState(PLUGINS[1], "1", "1.9") == nsIBLS.STATE_VULNERABLE_UPDATE_AVAILABLE); 1.71 + 1.72 + do_check_true(blocklist.getPluginBlocklistState(PLUGINS[2], "1", "1.9") == nsIBLS.STATE_VULNERABLE_NO_UPDATE); 1.73 + 1.74 + do_check_true(blocklist.getPluginBlocklistState(PLUGINS[3], "1", "1.9") == nsIBLS.STATE_BLOCKED); 1.75 + 1.76 + do_check_true(blocklist.getPluginBlocklistState(PLUGINS[4], "1", "1.9") == nsIBLS.STATE_SOFTBLOCKED); 1.77 + 1.78 + do_check_true(blocklist.getPluginBlocklistState(PLUGINS[5], "1", "1.9") == nsIBLS.STATE_NOT_BLOCKED); 1.79 + 1.80 + gNextTest = test_is_not_clicktoplay; 1.81 + do_execute_soon(gNextTest); 1.82 +} 1.83 + 1.84 +function get_test_plugin() { 1.85 + var pluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost); 1.86 + for (var plugin of pluginHost.getPluginTags()) { 1.87 + if (plugin.name == "Test Plug-in") 1.88 + return plugin; 1.89 + } 1.90 + do_check_true(false); 1.91 + return null; 1.92 +} 1.93 + 1.94 +// At this time, the blocklist does not have an entry for the test plugin, 1.95 +// so it shouldn't be click-to-play. 1.96 +function test_is_not_clicktoplay() { 1.97 + var plugin = get_test_plugin(); 1.98 + var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9"); 1.99 + do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_UPDATE_AVAILABLE); 1.100 + do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE); 1.101 + 1.102 + Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtpUndo.xml"); 1.103 + gNextTest = test_is_clicktoplay; 1.104 + gNotifier.notify(null); 1.105 +} 1.106 + 1.107 +// Here, we've updated the blocklist to have a block for the test plugin, 1.108 +// so it should be click-to-play. 1.109 +function test_is_clicktoplay() { 1.110 + var plugin = get_test_plugin(); 1.111 + var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9"); 1.112 + do_check_eq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE); 1.113 + 1.114 + Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtp.xml"); 1.115 + gNextTest = test_is_not_clicktoplay2; 1.116 + gNotifier.notify(null); 1.117 +} 1.118 + 1.119 +// But now we've removed that entry from the blocklist (really we've gone back 1.120 +// to the old one), so the plugin shouldn't be click-to-play any more. 1.121 +function test_is_not_clicktoplay2() { 1.122 + var plugin = get_test_plugin(); 1.123 + var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9"); 1.124 + do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_UPDATE_AVAILABLE); 1.125 + do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE); 1.126 + 1.127 + Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtpUndo.xml"); 1.128 + gNextTest = test_disable_blocklist; 1.129 + gNotifier.notify(null); 1.130 +} 1.131 + 1.132 +// Test that disabling the blocklist when a plugin is ctp-blocklisted will 1.133 +// result in the plugin not being click-to-play. 1.134 +function test_disable_blocklist() { 1.135 + var plugin = get_test_plugin(); 1.136 + var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9"); 1.137 + do_check_eq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE); 1.138 + 1.139 + gNextTest = null; 1.140 + Services.prefs.setBoolPref("extensions.blocklist.enabled", false); 1.141 + blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9"); 1.142 + do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE); 1.143 + do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_UPDATE_AVAILABLE); 1.144 + 1.145 + // it should still be possible to make a plugin click-to-play via the pref 1.146 + // and setting that plugin's enabled state to click-to-play 1.147 + Services.prefs.setBoolPref("plugins.click_to_play", true); 1.148 + let previousEnabledState = plugin.enabledState; 1.149 + plugin.enabledState = Components.interfaces.nsIPluginTag.STATE_CLICKTOPLAY; 1.150 + do_check_eq(gPluginHost.getStateForType("application/x-test"), Components.interfaces.nsIPluginTag.STATE_CLICKTOPLAY); 1.151 + // clean up plugin state 1.152 + plugin.enabledState = previousEnabledState; 1.153 + 1.154 + gServer.stop(do_test_finished); 1.155 +} 1.156 + 1.157 +// Observe "blocklist-updated" so we know when to advance to the next test 1.158 +function observer() { 1.159 + if (gNextTest) 1.160 + do_execute_soon(gNextTest); 1.161 +} 1.162 + 1.163 +function run_test() { 1.164 + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); 1.165 + 1.166 + Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtp.xml"); 1.167 + startupManager(); 1.168 + 1.169 + gPluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost); 1.170 + gBlocklistService = Components.classes["@mozilla.org/extensions/blocklist;1"].getService(Components.interfaces.nsIBlocklistService); 1.171 + gNotifier = Components.classes["@mozilla.org/extensions/blocklist;1"].getService(Components.interfaces.nsITimerCallback); 1.172 + Services.obs.addObserver(observer, "blocklist-updated", false); 1.173 + 1.174 + do_register_cleanup(function() { 1.175 + Services.prefs.clearUserPref("extensions.blocklist.url"); 1.176 + Services.prefs.clearUserPref("extensions.blocklist.enabled"); 1.177 + Services.prefs.clearUserPref("plugins.click_to_play"); 1.178 + Services.obs.removeObserver(observer, "blocklist-updated"); 1.179 + }); 1.180 + 1.181 + gNextTest = test_basic; 1.182 + do_test_pending(); 1.183 + gNotifier.notify(null); 1.184 +}