summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/unit/test_nsIMsgContentPolicy.js
blob: de49d5b7eb319316d7b0cb341d07f6eeb124690a (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
/* -*- mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * Test suite for nsIMsgContentPolicy to check we could add/remove customized protocol to
 * nsMsgContentPolicy.
 */
const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");

function makeURI(aURL) {
  return Services.io.newURI(aURL);
}

function run_test() {
  var content_policy = Cc["@mozilla.org/messenger/content-policy;1"].getService(
    Ci.nsIContentPolicy
  );

  Assert.ok(content_policy);

  var msg_content_policy = content_policy.QueryInterface(
    Ci.nsIMsgContentPolicy
  );

  Assert.ok(msg_content_policy);

  var req_uri = makeURI("custom-scheme://custom_url/1.emal");
  Assert.ok(req_uri);

  var content_uri = makeURI("custom-scheme://custom_content_url/1.jsp");
  Assert.ok(content_uri);

  let tmpChannel = NetUtil.newChannel({
    uri: content_uri,
    // Needs one of 'loadingNode', 'loadingPrincipal' or 'loadUsingSystemPrincipal' which we don't have.
    // Even with `loadUsingSystemPrincipal: true` this fails with "unknown protocol". See bug 1446587.
    securityFlags: Ci.nsILoadInfo.SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK,
    contentPolicyType: Ci.nsIContentPolicy.TYPE_IMAGE,
  });
  let tmpLoadInfo = tmpChannel.loadInfo;

  var decision = content_policy.shouldLoad(
    content_uri,
    tmpLoadInfo,
    "img/jpeg"
  );
  Assert.notEqual(
    decision,
    Ci.nsIContentPolicy.ACCEPT,
    "customized protocol should not load"
  );

  msg_content_policy.addExposedProtocol("custom-scheme");

  decision = content_policy.shouldLoad(content_uri, tmpLoadInfo, "img/jpeg");
  Assert.equal(
    decision,
    Ci.nsIContentPolicy.ACCEPT,
    "customized protocol should load"
  );

  msg_content_policy.removeExposedProtocol("custom-scheme");

  decision = content_policy.shouldLoad(content_uri, tmpLoadInfo, "img/jpeg");
  Assert.notEqual(
    decision,
    Ci.nsIContentPolicy.ACCEPT,
    "customized protocol should not load"
  );
}