caps/tests/mochitest/test_principal_jarprefix_origin_appid_appstatus.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=758258
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <meta charset="utf-8">
michael@0 8 <title>Test for nsIPrincipal jarPrefix, origin, appStatus and appId</title>
michael@0 9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 10 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
michael@0 11 </head>
michael@0 12 <body>
michael@0 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=758258">Mozilla Bug 758258</a>
michael@0 14 <p id="display"></p>
michael@0 15 <div id="content">
michael@0 16
michael@0 17 </div>
michael@0 18 <pre id="test">
michael@0 19 <script type="application/javascript;version=1.7">
michael@0 20
michael@0 21 /** Test for Bug 758258 **/
michael@0 22
michael@0 23 var Ci = Components.interfaces;
michael@0 24 SimpleTest.waitForExplicitFinish();
michael@0 25
michael@0 26 /*
michael@0 27 * gData is an array of objects. Each object represents a test case.
michael@0 28 * - app: gives the app manifest URL, will set mozapp to it on the iframe;
michael@0 29 * - origin: gives the origin of the iframe. This is the URL thas is going to
michael@0 30 * to be passed as iframe.src but also the expected principal's
michael@0 31 * origin.
michael@0 32 * - isapp: tells if the iframe is really a mozapp. If the manifest url isn't
michael@0 33 * valid, the iframe will not be considered as a mozapp.
michael@0 34 * - browser: say if the iframe should be a mozbrowser. This is implicit when
michael@0 35 * app is set.
michael@0 36 * - test: an array of tests to run for this test case:
michael@0 37 * - eo-unique: the extendedOrigin of the prinicpal must be unique in the
michael@0 38 * current list.
michael@0 39 * - eo-as-last: the extendedOrigin of the principal must be the same as the
michael@0 40 * last added to the list.
michael@0 41 */
michael@0 42 var gData = [
michael@0 43 {
michael@0 44 app: "http://example.org/manifest.webapp",
michael@0 45 src: "http://example.org/",
michael@0 46 isapp: true,
michael@0 47 test: [ "eo-unique" ],
michael@0 48 },
michael@0 49 {
michael@0 50 app: "https://example.com/manifest.webapp",
michael@0 51 src: "https://example.com/",
michael@0 52 isapp: true,
michael@0 53 test: [ "eo-unique" ],
michael@0 54 },
michael@0 55 {
michael@0 56 app: "http://test1.example.org/manifest.webapp",
michael@0 57 src: "http://test1.example.org/",
michael@0 58 isapp: true,
michael@0 59 test: [ "eo-unique" ],
michael@0 60 },
michael@0 61 {
michael@0 62 app: "http://test1.example.org:8000/manifest.webapp",
michael@0 63 src: "http://test1.example.org:8000/",
michael@0 64 isapp: true,
michael@0 65 test: [ "eo-unique" ],
michael@0 66 },
michael@0 67 {
michael@0 68 app: "http://sub1.test1.example.org/manifest.webapp",
michael@0 69 src: "http://sub1.test1.example.org/",
michael@0 70 isapp: true,
michael@0 71 test: [ "eo-unique" ],
michael@0 72 },
michael@0 73 // WebApps implementation doesn't allow apps with the same origin. Sad...
michael@0 74 // {
michael@0 75 // app: "http://example.org/foo/manifest.webapp",
michael@0 76 // src: "http://example.org/",
michael@0 77 // isapp: true,
michael@0 78 // test: [ "eo-unique" ],
michael@0 79 // },
michael@0 80 // {
michael@0 81 // app: "http://example.org/bar/manifest.webapp",
michael@0 82 // src: "http://example.org/",
michael@0 83 // isapp: true,
michael@0 84 // test: [ "eo-unique" ],
michael@0 85 // },
michael@0 86 {
michael@0 87 src: "http://example.org/",
michael@0 88 isapp: false,
michael@0 89 test: [ "eo-unique" ],
michael@0 90 },
michael@0 91 {
michael@0 92 browser: true,
michael@0 93 src: "http://example.org/",
michael@0 94 isapp: false,
michael@0 95 test: [ "eo-unique" ],
michael@0 96 },
michael@0 97 {
michael@0 98 app: "http://example.org/wedonthaveanyappinthatdirectory/manifest.webapp",
michael@0 99 src: "http://example.org/",
michael@0 100 isapp: false,
michael@0 101 // TODO: this is a browser because we need apps to be browser and it's not
michael@0 102 // an app because the manifest is invalid. Ideally, it should not be a
michael@0 103 // browser.
michael@0 104 browser: true,
michael@0 105 test: [ "eo-as-last" ],
michael@0 106 },
michael@0 107 // {
michael@0 108 // app: "http://example.org/manifest.webapp",
michael@0 109 // src: "data:text/html,foobar",
michael@0 110 // test: [ "todo-src" ],
michael@0 111 // },
michael@0 112 // {
michael@0 113 // app: "http://example.org/manifest.webapp",
michael@0 114 // src: "data:text/html,foobar2",
michael@0 115 // test: [ "todo-src" ],
michael@0 116 // },
michael@0 117 {
michael@0 118 src: "file:///",
michael@0 119 isapp: false,
michael@0 120 test: [ "eo-unique" ],
michael@0 121 },
michael@0 122 {
michael@0 123 src: "file:///tmp/",
michael@0 124 isapp: false,
michael@0 125 test: [ "eo-unique" ],
michael@0 126 },
michael@0 127 {
michael@0 128 app: "http://example.org/manifest.webapp",
michael@0 129 src: "file:///",
michael@0 130 isapp: true,
michael@0 131 test: [ "eo-unique", "in-app-not-installed" ],
michael@0 132 },
michael@0 133 {
michael@0 134 app: "http://example.org/manifest.webapp",
michael@0 135 src: "file:///tmp/",
michael@0 136 isapp: true,
michael@0 137 test: [ "eo-unique", "in-app-not-installed" ],
michael@0 138 },
michael@0 139 // iframe inside an app is part of the app.
michael@0 140 {
michael@0 141 app: "http://example.org/manifest.webapp",
michael@0 142 src: "http://example.org/",
michael@0 143 isapp: true,
michael@0 144 child: {
michael@0 145 src: "http://example.org/chrome/",
michael@0 146 isapp: true
michael@0 147 },
michael@0 148 test: [ "child-has-same-eo" ],
michael@0 149 },
michael@0 150 // app A inside app B aren't the same app.
michael@0 151 {
michael@0 152 app: "http://example.org/manifest.webapp",
michael@0 153 src: "http://example.org/",
michael@0 154 isapp: true,
michael@0 155 child: {
michael@0 156 app: "https://example.com/manifest.webapp",
michael@0 157 src: "https://example.com/chrome/",
michael@0 158 isapp: true
michael@0 159 },
michael@0 160 test: [ "child-has-different-eo", "child-has-same-appstatus", "child-has-different-appid" ],
michael@0 161 },
michael@0 162 // app A inside app A are the same app.
michael@0 163 {
michael@0 164 app: "http://example.org/manifest.webapp",
michael@0 165 src: "http://example.org/",
michael@0 166 isapp: true,
michael@0 167 child: {
michael@0 168 app: "http://example.org/manifest.webapp",
michael@0 169 src: "http://example.org/chrome/",
michael@0 170 isapp: true
michael@0 171 },
michael@0 172 test: [ "child-has-same-eo" ],
michael@0 173 },
michael@0 174 // app inside a regular iframe is an app.
michael@0 175 {
michael@0 176 src: "http://example.org/",
michael@0 177 isapp: false,
michael@0 178 child: {
michael@0 179 app: "http://example.org/manifest.webapp",
michael@0 180 src: "http://example.org/chrome/",
michael@0 181 isapp: true
michael@0 182 },
michael@0 183 test: [ "child-has-different-eo", "child-has-different-appstatus", "child-has-different-appid" ],
michael@0 184 },
michael@0 185 // browser inside app is a browser, has appid but isn't installed.
michael@0 186 {
michael@0 187 src: "http://example.org/",
michael@0 188 app: "http://example.org/manifest.webapp",
michael@0 189 isapp: true,
michael@0 190 child: {
michael@0 191 src: "http://example.org/chrome/",
michael@0 192 isapp: false,
michael@0 193 browser: true,
michael@0 194 },
michael@0 195 test: [ "child-has-different-eo", "child-has-different-appstatus", "child-has-same-appid" ],
michael@0 196 },
michael@0 197 // app inside a browser is an app.
michael@0 198 {
michael@0 199 src: "http://example.org/",
michael@0 200 isapp: false,
michael@0 201 browser: true,
michael@0 202 child: {
michael@0 203 app: "http://example.org/manifest.webapp",
michael@0 204 src: "http://example.org/chrome/",
michael@0 205 isapp: true,
michael@0 206 },
michael@0 207 test: [ "child-has-different-eo", "child-has-different-appstatus", "child-has-different-appid" ],
michael@0 208 },
michael@0 209 // browser inside a browser are two browsers
michael@0 210 {
michael@0 211 src: "http://example.org/",
michael@0 212 isapp: false,
michael@0 213 browser: true,
michael@0 214 child: {
michael@0 215 src: "http://example.org/chrome/",
michael@0 216 isapp: false,
michael@0 217 browser: true,
michael@0 218 },
michael@0 219 test: [ "child-has-same-eo" ],
michael@0 220 },
michael@0 221 // browser inside a browser are two browsers
michael@0 222 {
michael@0 223 src: "http://example.org/",
michael@0 224 isapp: false,
michael@0 225 browser: true,
michael@0 226 child: {
michael@0 227 src: "https://example.com/chrome/",
michael@0 228 isapp: false,
michael@0 229 browser: true,
michael@0 230 },
michael@0 231 test: [ "child-has-different-eo", "child-has-same-appstatus", "child-has-same-appid" ],
michael@0 232 },
michael@0 233 // iframe containing a browser
michael@0 234 {
michael@0 235 src: "http://example.org/",
michael@0 236 isapp: false,
michael@0 237 browser: false,
michael@0 238 child: {
michael@0 239 src: "http://example.org/chrome/",
michael@0 240 isapp: false,
michael@0 241 browser: true,
michael@0 242 },
michael@0 243 test: [ "child-has-different-eo", "child-has-same-appstatus", "child-has-same-appid" ],
michael@0 244 },
michael@0 245 // browser containing an iframe is part of the browser
michael@0 246 {
michael@0 247 src: "http://example.org/",
michael@0 248 isapp: false,
michael@0 249 browser: true,
michael@0 250 child: {
michael@0 251 src: "http://example.org/chrome/",
michael@0 252 isapp: false,
michael@0 253 inBrowser: true,
michael@0 254 },
michael@0 255 test: [ "child-has-same-eo" ],
michael@0 256 },
michael@0 257 ];
michael@0 258
michael@0 259 // The list of all data ids generated by this test.
michael@0 260 var eoList = [];
michael@0 261
michael@0 262 var content = document.getElementById('content');
michael@0 263 var checkedCount = 0;
michael@0 264 var checksTodo = gData.length;
michael@0 265
michael@0 266 function checkIFrame(aFrame, data) {
michael@0 267 var principal = aFrame.contentDocument.nodePrincipal;
michael@0 268
michael@0 269 if (!data.test) {
michael@0 270 data.test = [];
michael@0 271 }
michael@0 272
michael@0 273 if (navigator.platform.indexOf("Mac") != -1) {
michael@0 274 is(principal.URI.spec,
michael@0 275 data.src.replace('file:///tmp/', 'file:///private/tmp/'),
michael@0 276 'the correct URL should have been loaded');
michael@0 277 } else {
michael@0 278 is(principal.URI.spec, data.src,
michael@0 279 'the correct URL should have been loaded');
michael@0 280 }
michael@0 281
michael@0 282 if (data.isapp) {
michael@0 283 if (data.test.indexOf("in-app-not-installed") != -1) {
michael@0 284 is(principal.appStatus, Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED, 'this should not be an installed app');
michael@0 285 } else {
michael@0 286 is(principal.appStatus, Ci.nsIPrincipal.APP_STATUS_INSTALLED, 'this should be an installed app');
michael@0 287 }
michael@0 288 } else {
michael@0 289 is(principal.appStatus, Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED,
michael@0 290 'this should not be an installed app');
michael@0 291 is(principal.appId, Ci.nsIScriptSecurityManager.NO_APP_ID,
michael@0 292 "principals from non-installed app should have NO_APP_ID");
michael@0 293 }
michael@0 294
michael@0 295 if (!data.isapp && !data.browser) {
michael@0 296 is(principal.jarPrefix, "",
michael@0 297 'jarPrefix should return an empty string for non-app and non-browsers principals');
michael@0 298 } else {
michael@0 299 isnot(principal.jarPrefix, "",
michael@0 300 'jarPrefix should not return an empty string for apps or mozbrowsers');
michael@0 301 }
michael@0 302
michael@0 303 if (data.test.indexOf("eo-unique") != -1) {
michael@0 304 is(eoList.indexOf(principal.jarPrefix + principal.origin), -1,
michael@0 305 "extended origin should be unique");
michael@0 306 }
michael@0 307 if (data.test.indexOf("eo-as-last") != -1) {
michael@0 308 is(principal.jarPrefix + principal.origin, eoList[eoList.length-1],
michael@0 309 "extended origin should be the same as the last inserted one");
michael@0 310 }
michael@0 311
michael@0 312 is(principal.isInBrowserElement, !!data.browser,
michael@0 313 "check principal.isInBrowserElement");
michael@0 314
michael@0 315 if (data.child) {
michael@0 316 let childPrincipal = aFrame.contentWindow.frames[0].document.nodePrincipal;
michael@0 317
michael@0 318 if (data.child.isapp) {
michael@0 319 is(childPrincipal.appStatus, Ci.nsIPrincipal.APP_STATUS_INSTALLED,
michael@0 320 "child should be an installed app");
michael@0 321 }
michael@0 322
michael@0 323 is(childPrincipal.isInBrowserElement, !!data.child.browser || !!data.child.inBrowser,
michael@0 324 "check childPrincipal.isInBrowserElement");
michael@0 325
michael@0 326 if (data.test.indexOf("child-has-same-eo") != -1) {
michael@0 327 is(childPrincipal.jarPrefix + childPrincipal.origin,
michael@0 328 principal.jarPrefix + principal.origin,
michael@0 329 "child should have the same extended origin as parent");
michael@0 330 is(childPrincipal.appStatus, principal.appStatus,
michael@0 331 "child should have the same appStatus if it has the same extended origin");
michael@0 332 is(childPrincipal.appId, principal.appId,
michael@0 333 "child should have the same appId if it has the same extended origin");
michael@0 334 }
michael@0 335
michael@0 336 if (data.test.indexOf("child-has-different-eo") != -1) {
michael@0 337 isnot(childPrincipal.jarPrefix + childPrincipal.origin,
michael@0 338 principal.jarPrefix + principal.origin,
michael@0 339 "child should not have the same extended origin as parent");
michael@0 340 }
michael@0 341
michael@0 342 if (data.test.indexOf("child-has-same-appstatus") != -1) {
michael@0 343 is(childPrincipal.appStatus, principal.appStatus,
michael@0 344 "childPrincipal and parent principal should have the same appStatus");
michael@0 345 }
michael@0 346
michael@0 347 if (data.test.indexOf("child-has-different-appstatus") != -1) {
michael@0 348 isnot(childPrincipal.appStatus, principal.appStatus,
michael@0 349 "childPrincipal and parent principal should not have the same appStatus");
michael@0 350 }
michael@0 351
michael@0 352 if (data.test.indexOf("child-has-same-appid") != -1) {
michael@0 353 is(childPrincipal.appId, principal.appId,
michael@0 354 "childPrincipal and parent principal should have the same appId");
michael@0 355 }
michael@0 356
michael@0 357 if (data.test.indexOf("child-has-different-appid") != -1) {
michael@0 358 isnot(childPrincipal.appId, principal.appId,
michael@0 359 "childPrincipal and parent principal should have different appId");
michael@0 360 }
michael@0 361 }
michael@0 362
michael@0 363 eoList.push(principal.jarPrefix + principal.origin);
michael@0 364
michael@0 365 checkedCount++;
michael@0 366 if (checkedCount == checksTodo) {
michael@0 367 SpecialPowers.removePermission("browser", "http://example.org");
michael@0 368 SpecialPowers.removePermission("embed-apps", "http://example.org");
michael@0 369 SimpleTest.finish();
michael@0 370 } else {
michael@0 371 gTestRunner.next();
michael@0 372 }
michael@0 373 }
michael@0 374
michael@0 375 is('appStatus' in document.nodePrincipal, true,
michael@0 376 'appStatus should be present in nsIPrincipal');
michael@0 377 is('jarPrefix' in document.nodePrincipal, true,
michael@0 378 'jarPrefix should be present in nsIPrincipal');
michael@0 379 is('origin' in document.nodePrincipal, true,
michael@0 380 'origin should be present in nsIPrincipal');
michael@0 381 is('appId' in document.nodePrincipal, true,
michael@0 382 'appId should be present in nsIPrincipal');
michael@0 383
michael@0 384 function runTest() {
michael@0 385 // We want to use a generator. Those only work in a one level stack so we
michael@0 386 // can't use .forEach() here.
michael@0 387 for (var i=0; i<gData.length; ++i) {
michael@0 388 let data = gData[i];
michael@0 389
michael@0 390 var iframe = document.createElement('iframe');
michael@0 391 iframe.check = function() {
michael@0 392 checkIFrame(this, data);
michael@0 393 };
michael@0 394 iframe.addChild = function() {
michael@0 395 SpecialPowers.addPermission("browser", true, iframe.contentDocument);
michael@0 396 SpecialPowers.addPermission("embed-apps", true, iframe.contentDocument);
michael@0 397
michael@0 398 var childFrame = document.createElement('iframe');
michael@0 399
michael@0 400 if (data.child.app) {
michael@0 401 childFrame.setAttribute('mozapp', data.child.app)
michael@0 402 childFrame.setAttribute('mozbrowser', '');
michael@0 403 } else if (data.child.browser) {
michael@0 404 childFrame.setAttribute('mozbrowser', '');
michael@0 405 }
michael@0 406
michael@0 407 childFrame.src = data.child.src;
michael@0 408
michael@0 409 this.removeEventListener('load', this.addChild.bind(this));
michael@0 410 childFrame.addEventListener('load', this.check.bind(this));
michael@0 411
michael@0 412 this.contentDocument.body.appendChild(childFrame);
michael@0 413 };
michael@0 414
michael@0 415 if (data.app) {
michael@0 416 iframe.setAttribute('mozapp', data.app);
michael@0 417 iframe.setAttribute('mozbrowser', '');
michael@0 418 } else if (data.browser) {
michael@0 419 iframe.setAttribute('mozbrowser', '');
michael@0 420 }
michael@0 421
michael@0 422 iframe.src = data.src;
michael@0 423
michael@0 424 if (data.child) {
michael@0 425 iframe.addEventListener('load', iframe.addChild.bind(iframe));
michael@0 426 } else {
michael@0 427 iframe.addEventListener('load', iframe.check.bind(iframe));
michael@0 428 }
michael@0 429
michael@0 430 content.appendChild(iframe);
michael@0 431
michael@0 432 yield;
michael@0 433 }
michael@0 434 }
michael@0 435
michael@0 436 var gTestRunner = runTest();
michael@0 437
michael@0 438 SpecialPowers.pushPrefEnv({'set':[["dom.mozBrowserFramesEnabled", true]]},
michael@0 439 function() { gTestRunner.next(); });
michael@0 440
michael@0 441 </script>
michael@0 442 </pre>
michael@0 443 </body>
michael@0 444 </html>

mercurial