summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contentanalysis/tests/browser/browser_content_analysis_policies.js
blob: e7122508e182ded206aa3b376fcadf5f5a5238c4 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* 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/. */

// Check that CA is active if and only if:
// 1. browser.contentanalysis.enabled is true and
// 2. Either browser.contentanalysis.enabled was set by an enteprise
//    policy or the "-allow-content-analysis" command line arg was present
// We can't really test command line arguments so we instead use a test-only
// method to set the value the command-line is supposed to update.

"use strict";

const { EnterprisePolicyTesting, PoliciesPrefTracker } =
  ChromeUtils.importESModule(
    "resource://testing-common/EnterprisePolicyTesting.sys.mjs"
  );

const kEnabledPref = "enabled";
const kPipeNamePref = "pipe_path_name";
const kTimeoutPref = "agent_timeout";
const kAllowUrlPref = "allow_url_regex_list";
const kDenyUrlPref = "deny_url_regex_list";
const kAgentNamePref = "agent_name";
const kClientSignaturePref = "client_signature";
const kPerUserPref = "is_per_user";
const kShowBlockedPref = "show_blocked_result";
const kDefaultResultPref = "default_result";
const kBypassForSameTabOperationsPref = "bypass_for_same_tab_operations";

const ca = Cc["@mozilla.org/contentanalysis;1"].getService(
  Ci.nsIContentAnalysis
);

add_task(async function test_ca_active() {
  PoliciesPrefTracker.start();
  ok(!ca.isActive, "CA is inactive when pref and cmd line arg are missing");

  // Set the pref without enterprise policy.  CA should not be active.
  Services.prefs.setBoolPref("browser.contentanalysis." + kEnabledPref, true);
  ok(
    !ca.isActive,
    "CA is inactive when pref is set but cmd line arg is missing"
  );

  // Set the pref without enterprise policy but also set command line arg
  // property.  CA should be active.
  ca.testOnlySetCACmdLineArg(true);
  ok(ca.isActive, "CA is active when pref is set and cmd line arg is present");

  // Undo test-only value before later tests.
  ca.testOnlySetCACmdLineArg(false);
  ok(!ca.isActive, "properly unset cmd line arg value");

  // Disabled the pref with enterprise policy.  CA should not be active.
  await EnterprisePolicyTesting.setupPolicyEngineWithJson({
    policies: {
      ContentAnalysis: { Enabled: false },
    },
  });
  ok(!ca.isActive, "CA is inactive when disabled by enterprise policy pref");

  // Enabled the pref with enterprise policy.  CA should be active.
  await EnterprisePolicyTesting.setupPolicyEngineWithJson({
    policies: {
      ContentAnalysis: { Enabled: true },
    },
  });
  ok(ca.isActive, "CA is active when enabled by enterprise policy pref");
  PoliciesPrefTracker.stop();
});

add_task(async function test_ca_enterprise_config() {
  PoliciesPrefTracker.start();
  const string1 = "this is a string";
  const string2 = "this is another string";
  const string3 = "an agent name";
  const string4 = "a client signature";

  await EnterprisePolicyTesting.setupPolicyEngineWithJson({
    policies: {
      ContentAnalysis: {
        PipePathName: "abc",
        AgentTimeout: 99,
        AllowUrlRegexList: string1,
        DenyUrlRegexList: string2,
        AgentName: string3,
        ClientSignature: string4,
        IsPerUser: true,
        ShowBlockedResult: false,
        DefaultResult: 1,
        BypassForSameTabOperations: true,
      },
    },
  });

  is(
    Services.prefs.getStringPref("browser.contentanalysis." + kPipeNamePref),
    "abc",
    "pipe name match"
  );
  is(
    Services.prefs.getIntPref("browser.contentanalysis." + kTimeoutPref),
    99,
    "timeout match"
  );
  is(
    Services.prefs.getStringPref("browser.contentanalysis." + kAllowUrlPref),
    string1,
    "allow urls match"
  );
  is(
    Services.prefs.getStringPref("browser.contentanalysis." + kDenyUrlPref),
    string2,
    "deny urls match"
  );
  is(
    Services.prefs.getStringPref("browser.contentanalysis." + kAgentNamePref),
    string3,
    "agent names match"
  );
  is(
    Services.prefs.getStringPref(
      "browser.contentanalysis." + kClientSignaturePref
    ),
    string4,
    "client signatures match"
  );
  is(
    Services.prefs.getBoolPref("browser.contentanalysis." + kPerUserPref),
    true,
    "per user match"
  );
  is(
    Services.prefs.getBoolPref("browser.contentanalysis." + kShowBlockedPref),
    false,
    "show blocked match"
  );
  is(
    Services.prefs.getIntPref("browser.contentanalysis." + kDefaultResultPref),
    1,
    "default result match"
  );
  is(
    Services.prefs.getBoolPref(
      "browser.contentanalysis." + kBypassForSameTabOperationsPref
    ),
    true,
    "bypass for same tab operations match"
  );
  PoliciesPrefTracker.stop();
});

add_task(async function test_cleanup() {
  ca.testOnlySetCACmdLineArg(false);
  await EnterprisePolicyTesting.setupPolicyEngineWithJson({
    policies: {},
  });
  // These may have gotten set when ContentAnalysis was enabled through
  // the policy and do not get cleared if there is no ContentAnalysis
  // element - reset them manually here.
  ca.isSetByEnterprisePolicy = false;
  Services.prefs.setBoolPref("browser.contentanalysis." + kEnabledPref, false);
});