summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/unit/test_allowed_types.js
blob: 09b30b31af2fd6a1beb99ed456dcc45280725f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

Components.utils.import("resource://gre/modules/Services.jsm");

function run_test() {
  const pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
  const pluginDefaultState = Services.prefs.getIntPref("plugin.default.state");

  Services.prefs.setBoolPref("plugin.load_flash_only", false);

  function reload_plugins_with_allowed_types(allowed_types) {
    if (typeof allowed_types === "undefined") {
      // If we didn't get an allowed_types string, then unset the pref,
      // so the caller can test the behavior when the pref isn't set.
      Services.prefs.clearUserPref("plugin.allowed_types");
    } else {
      Services.prefs.setCharPref("plugin.allowed_types", allowed_types);
    }
    pluginHost.reloadPlugins();
  }

  function get_status_for_type(type) {
    try {
      return pluginHost.getStateForType(type);
    } catch(ex) {
      // If the type is not allowed, then nsIPluginHost.getStateForType throws
      // NS_ERROR_NOT_AVAILABLE, for which we return undefined to make it easier
      // to write assertions about the API.
      if (ex.result === Cr.NS_ERROR_NOT_AVAILABLE) {
        return undefined;
      }
      throw ex;
    }
  }

  // If allowed_types isn't set, then all plugin types are enabled.
  reload_plugins_with_allowed_types();
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  // If allowed_types is set to the empty string, then all plugin types are enabled.
  reload_plugins_with_allowed_types("");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  // If allowed_types is set to anything other than the empty string,
  // then only types that exactly match its comma-separated entries are enabled,
  // so a single space disables all types.
  reload_plugins_with_allowed_types(" ");
  do_check_eq(get_status_for_type("application/x-test"), undefined);
  do_check_eq(get_status_for_type("application/x-Second-Test"), undefined);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  // The rest of the assertions test various values of allowed_types to ensure
  // that the correct types are enabled.

  reload_plugins_with_allowed_types("application/x-test");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), undefined);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-Second-Test");
  do_check_eq(get_status_for_type("application/x-test"), undefined);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-nonexistent");
  do_check_eq(get_status_for_type("application/x-test"), undefined);
  do_check_eq(get_status_for_type("application/x-Second-Test"), undefined);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-test,application/x-Second-Test");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-Second-Test,application/x-test");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-test,application/x-nonexistent");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), undefined);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-nonexistent,application/x-test");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), undefined);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-test,application/x-Second-Test,application/x-nonexistent");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-test,application/x-nonexistent,application/x-Second-Test");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-Second-Test,application/x-test,application/x-nonexistent");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-Second-Test,application/x-nonexistent,application/x-test");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-nonexistent,application/x-test,application/x-Second-Test");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  reload_plugins_with_allowed_types("application/x-nonexistent,application/x-Second-Test,application/x-test");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-Second-Test"), pluginDefaultState);
  do_check_eq(get_status_for_type("application/x-nonexistent"), undefined);

  // Plugin types are case-insensitive, so matching should be too.
  reload_plugins_with_allowed_types("APPLICATION/X-TEST");
  do_check_eq(get_status_for_type("application/x-test"), pluginDefaultState);
  reload_plugins_with_allowed_types("application/x-test");
  do_check_eq(get_status_for_type("APPLICATION/X-TEST"), pluginDefaultState);

  // Types must match exactly, so supersets should not enable subset types.
  reload_plugins_with_allowed_types("application/x-test-superset");
  do_check_eq(get_status_for_type("application/x-test"), undefined);
  reload_plugins_with_allowed_types("superset-application/x-test");
  do_check_eq(get_status_for_type("application/x-test"), undefined);

  // Clean up.
  Services.prefs.clearUserPref("plugin.allowed_types");
  Services.prefs.clearUserPref("plugin.importedState");
}