michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const nsIBLS = Components.interfaces.nsIBlocklistService; michael@0: Components.utils.import("resource://testing-common/httpd.js"); michael@0: michael@0: var gBlocklistService = null; michael@0: var gNotifier = null; michael@0: var gNextTest = null; michael@0: var gPluginHost = null; michael@0: michael@0: var gServer = new HttpServer(); michael@0: gServer.start(-1); michael@0: gPort = gServer.identity.primaryPort; michael@0: mapFile("/data/test_pluginBlocklistCtp.xml", gServer); michael@0: mapFile("/data/test_pluginBlocklistCtpUndo.xml", gServer); michael@0: michael@0: var PLUGINS = [{ michael@0: // severity=0, vulnerabilitystatus=0 -> outdated michael@0: name: "test_plugin_0", michael@0: version: "5", michael@0: disabled: false, michael@0: blocklisted: false michael@0: }, michael@0: { michael@0: // severity=0, vulnerabilitystatus=1 -> update available michael@0: name: "test_plugin_1", michael@0: version: "5", michael@0: disabled: false, michael@0: blocklisted: false michael@0: }, michael@0: { michael@0: // severity=0, vulnerabilitystatus=2 -> no update michael@0: name: "test_plugin_2", michael@0: version: "5", michael@0: disabled: false, michael@0: blocklisted: false michael@0: }, michael@0: { michael@0: // no severity field -> severity=3 by default -> hardblock michael@0: name: "test_plugin_3", michael@0: version: "5", michael@0: disabled: false, michael@0: blocklisted: false michael@0: }, michael@0: { michael@0: // severity=1, vulnerabilitystatus=2 -> softblock michael@0: name: "test_plugin_4", michael@0: version: "5", michael@0: disabled: false, michael@0: blocklisted: false michael@0: }, michael@0: { michael@0: // not in the blocklist -> not blocked michael@0: name: "test_plugin_5", michael@0: version: "5", michael@0: disabled: false, michael@0: blocklisted: false michael@0: }]; michael@0: michael@0: function test_basic() { michael@0: var blocklist = Components.classes["@mozilla.org/extensions/blocklist;1"].getService(nsIBLS); michael@0: michael@0: do_check_true(blocklist.getPluginBlocklistState(PLUGINS[0], "1", "1.9") == nsIBLS.STATE_OUTDATED); michael@0: michael@0: do_check_true(blocklist.getPluginBlocklistState(PLUGINS[1], "1", "1.9") == nsIBLS.STATE_VULNERABLE_UPDATE_AVAILABLE); michael@0: michael@0: do_check_true(blocklist.getPluginBlocklistState(PLUGINS[2], "1", "1.9") == nsIBLS.STATE_VULNERABLE_NO_UPDATE); michael@0: michael@0: do_check_true(blocklist.getPluginBlocklistState(PLUGINS[3], "1", "1.9") == nsIBLS.STATE_BLOCKED); michael@0: michael@0: do_check_true(blocklist.getPluginBlocklistState(PLUGINS[4], "1", "1.9") == nsIBLS.STATE_SOFTBLOCKED); michael@0: michael@0: do_check_true(blocklist.getPluginBlocklistState(PLUGINS[5], "1", "1.9") == nsIBLS.STATE_NOT_BLOCKED); michael@0: michael@0: gNextTest = test_is_not_clicktoplay; michael@0: do_execute_soon(gNextTest); michael@0: } michael@0: michael@0: function get_test_plugin() { michael@0: var pluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost); michael@0: for (var plugin of pluginHost.getPluginTags()) { michael@0: if (plugin.name == "Test Plug-in") michael@0: return plugin; michael@0: } michael@0: do_check_true(false); michael@0: return null; michael@0: } michael@0: michael@0: // At this time, the blocklist does not have an entry for the test plugin, michael@0: // so it shouldn't be click-to-play. michael@0: function test_is_not_clicktoplay() { michael@0: var plugin = get_test_plugin(); michael@0: var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9"); michael@0: do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_UPDATE_AVAILABLE); michael@0: do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE); michael@0: michael@0: Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtpUndo.xml"); michael@0: gNextTest = test_is_clicktoplay; michael@0: gNotifier.notify(null); michael@0: } michael@0: michael@0: // Here, we've updated the blocklist to have a block for the test plugin, michael@0: // so it should be click-to-play. michael@0: function test_is_clicktoplay() { michael@0: var plugin = get_test_plugin(); michael@0: var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9"); michael@0: do_check_eq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE); michael@0: michael@0: Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtp.xml"); michael@0: gNextTest = test_is_not_clicktoplay2; michael@0: gNotifier.notify(null); michael@0: } michael@0: michael@0: // But now we've removed that entry from the blocklist (really we've gone back michael@0: // to the old one), so the plugin shouldn't be click-to-play any more. michael@0: function test_is_not_clicktoplay2() { michael@0: var plugin = get_test_plugin(); michael@0: var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9"); michael@0: do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_UPDATE_AVAILABLE); michael@0: do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE); michael@0: michael@0: Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtpUndo.xml"); michael@0: gNextTest = test_disable_blocklist; michael@0: gNotifier.notify(null); michael@0: } michael@0: michael@0: // Test that disabling the blocklist when a plugin is ctp-blocklisted will michael@0: // result in the plugin not being click-to-play. michael@0: function test_disable_blocklist() { michael@0: var plugin = get_test_plugin(); michael@0: var blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9"); michael@0: do_check_eq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE); michael@0: michael@0: gNextTest = null; michael@0: Services.prefs.setBoolPref("extensions.blocklist.enabled", false); michael@0: blocklistState = gBlocklistService.getPluginBlocklistState(plugin, "1", "1.9"); michael@0: do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE); michael@0: do_check_neq(blocklistState, Components.interfaces.nsIBlocklistService.STATE_VULNERABLE_UPDATE_AVAILABLE); michael@0: michael@0: // it should still be possible to make a plugin click-to-play via the pref michael@0: // and setting that plugin's enabled state to click-to-play michael@0: Services.prefs.setBoolPref("plugins.click_to_play", true); michael@0: let previousEnabledState = plugin.enabledState; michael@0: plugin.enabledState = Components.interfaces.nsIPluginTag.STATE_CLICKTOPLAY; michael@0: do_check_eq(gPluginHost.getStateForType("application/x-test"), Components.interfaces.nsIPluginTag.STATE_CLICKTOPLAY); michael@0: // clean up plugin state michael@0: plugin.enabledState = previousEnabledState; michael@0: michael@0: gServer.stop(do_test_finished); michael@0: } michael@0: michael@0: // Observe "blocklist-updated" so we know when to advance to the next test michael@0: function observer() { michael@0: if (gNextTest) michael@0: do_execute_soon(gNextTest); michael@0: } michael@0: michael@0: function run_test() { michael@0: createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); michael@0: michael@0: Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/test_pluginBlocklistCtp.xml"); michael@0: startupManager(); michael@0: michael@0: gPluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost); michael@0: gBlocklistService = Components.classes["@mozilla.org/extensions/blocklist;1"].getService(Components.interfaces.nsIBlocklistService); michael@0: gNotifier = Components.classes["@mozilla.org/extensions/blocklist;1"].getService(Components.interfaces.nsITimerCallback); michael@0: Services.obs.addObserver(observer, "blocklist-updated", false); michael@0: michael@0: do_register_cleanup(function() { michael@0: Services.prefs.clearUserPref("extensions.blocklist.url"); michael@0: Services.prefs.clearUserPref("extensions.blocklist.enabled"); michael@0: Services.prefs.clearUserPref("plugins.click_to_play"); michael@0: Services.obs.removeObserver(observer, "blocklist-updated"); michael@0: }); michael@0: michael@0: gNextTest = test_basic; michael@0: do_test_pending(); michael@0: gNotifier.notify(null); michael@0: }