summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_bug375314.html
blob: c7c69a8576c0ead7b0f26239c122f94ae226f253 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=375314
-->
<head>
  <title>Test for Bug 375314</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>        
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=375314">Mozilla Bug 375314</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Bug 375314 **/

var lastContentType = -1;
const testURL = window.location.href + "/this/is/the/test/url";
const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;
var { AppConstants } = SpecialPowers.ChromeUtils.import(
  "resource://gre/modules/AppConstants.jsm"
);

// Content policy / factory implementation for the test
var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}");
var policyName = "@mozilla.org/testpolicy;1";
var policy = {
  // nsISupports implementation
  QueryInterface(iid) {

    iid = SpecialPowers.wrap(iid);
    if (iid.equals(Ci.nsISupports) ||
        iid.equals(Ci.nsIFactory) ||
        iid.equals(Ci.nsIContentPolicy))
      return this;

    throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
  },

  // nsIFactory implementation
  createInstance(iid) {
    return this.QueryInterface(iid);
  },

  // nsIContentPolicy implementation
  shouldLoad(contentLocation, loadInfo, mimeTypeGuess) {
    let contentType = loadInfo.externalContentPolicyType;
    // Remember last content type seen for the test url
    if (SpecialPowers.wrap(contentLocation).spec == testURL) {
      lastContentType = contentType;
      return Ci.nsIContentPolicy.REJECT_REQUEST;
    }

    return Ci.nsIContentPolicy.ACCEPT;
  },

  shouldProcess(contentLocation, loadInfo, mimeTypeGuess) {
    return Ci.nsIContentPolicy.ACCEPT;
  }
}
policy = SpecialPowers.wrapCallbackObject(policy);

// Register content policy
var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager
                                                     .QueryInterface(Ci.nsIComponentRegistrar);

componentManager.registerFactory(policyID, "Test content policy", policyName, policy);

var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true);

// Try creating different request types
var tests = ["SCRIPT", "IMAGE", "STYLESHEET", "XMLHTTPREQUEST"];
var curTest = -1;
var div;

SimpleTest.waitForExplicitFinish();
setTimeout(runNextTest, 0);

function runNextTest() {

  if (curTest >= 0) {
    var type = "TYPE_" + tests[curTest];
    is(lastContentType, Ci.nsIContentPolicy[type], "Content policies triggered for " + type);
  }

  curTest++;
  if (curTest < tests.length) {
    var method = "request_" + tests[curTest].toLowerCase();
    try {
      window[method]();
    } catch(e) {}
    setTimeout(runNextTest, 0);
  }
  else {
    // Unregister content policy
    categoryManager.deleteCategoryEntry("content-policy", policyName, false);

    setTimeout(function() {
      // Component must be unregistered delayed, otherwise other content
      // policy will not be removed from the category correctly
      componentManager.unregisterFactory(policyID, policy);
    }, 0);

    SimpleTest.finish();
  }
}

// Request creating functions

function request_script() {
  var content = $("content");

  var script = document.createElement("script");
  script.setAttribute("type", "text/javascript")
  script.setAttribute("src", testURL)
  content.appendChild(script);
}

function request_image() {
  var content = $("content");

  var image = new Image();
  image.src = testURL;
}

function request_stylesheet() {
  var content = $("content");

  var stylesheet = document.createElement("link");
  stylesheet.setAttribute("rel", "stylesheet");
  stylesheet.setAttribute("type", "text/css");
  stylesheet.setAttribute("href", testURL);
  content.appendChild(stylesheet);
}

function request_object() {
  var content = $("content");

  var object = document.createElement("embed");
  object.setAttribute("src", testURL);
  content.appendChild(object);
}

function request_xmlhttprequest() {
  var request = new XMLHttpRequest();
  request.open("GET", testURL, false);
  request.send(null);
}

</script>
</pre>
</body>
</html>