caps/tests/mochitest/test_principal_jarprefix_origin_appid_appstatus.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/caps/tests/mochitest/test_principal_jarprefix_origin_appid_appstatus.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,444 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=758258
     1.8 +-->
     1.9 +<head>
    1.10 +  <meta charset="utf-8">
    1.11 +  <title>Test for nsIPrincipal jarPrefix, origin, appStatus and appId</title>
    1.12 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
    1.14 +</head>
    1.15 +<body>
    1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=758258">Mozilla Bug 758258</a>
    1.17 +<p id="display"></p>
    1.18 +<div id="content">
    1.19 +
    1.20 +</div>
    1.21 +<pre id="test">
    1.22 +<script type="application/javascript;version=1.7">
    1.23 +
    1.24 +/** Test for Bug 758258 **/
    1.25 +
    1.26 +var Ci = Components.interfaces;
    1.27 +SimpleTest.waitForExplicitFinish();
    1.28 +
    1.29 +/*
    1.30 + * gData is an array of objects. Each object represents a test case.
    1.31 + * - app: gives the app manifest URL, will set mozapp to it on the iframe;
    1.32 + * - origin: gives the origin of the iframe. This is the URL thas is going to
    1.33 + *           to be passed as iframe.src but also the expected principal's
    1.34 + *           origin.
    1.35 + * - isapp: tells if the iframe is really a mozapp. If the manifest url isn't
    1.36 + *          valid, the iframe will not be considered as a mozapp.
    1.37 + * - browser: say if the iframe should be a mozbrowser. This is implicit when
    1.38 + *            app is set.
    1.39 + * - test: an array of tests to run for this test case:
    1.40 + *   - eo-unique: the extendedOrigin of the prinicpal must be unique in the
    1.41 + *                current list.
    1.42 + *   - eo-as-last: the extendedOrigin of the principal must be the same as the
    1.43 + *                 last added to the list.
    1.44 + */
    1.45 +var gData = [
    1.46 +  {
    1.47 +    app: "http://example.org/manifest.webapp",
    1.48 +    src: "http://example.org/",
    1.49 +    isapp: true,
    1.50 +    test: [ "eo-unique" ],
    1.51 +  },
    1.52 +  {
    1.53 +    app: "https://example.com/manifest.webapp",
    1.54 +    src: "https://example.com/",
    1.55 +    isapp: true,
    1.56 +    test: [ "eo-unique" ],
    1.57 +  },
    1.58 +  {
    1.59 +    app: "http://test1.example.org/manifest.webapp",
    1.60 +    src: "http://test1.example.org/",
    1.61 +    isapp: true,
    1.62 +    test: [ "eo-unique" ],
    1.63 +  },
    1.64 +  {
    1.65 +    app: "http://test1.example.org:8000/manifest.webapp",
    1.66 +    src: "http://test1.example.org:8000/",
    1.67 +    isapp: true,
    1.68 +    test: [ "eo-unique" ],
    1.69 +  },
    1.70 +  {
    1.71 +    app: "http://sub1.test1.example.org/manifest.webapp",
    1.72 +    src: "http://sub1.test1.example.org/",
    1.73 +    isapp: true,
    1.74 +    test: [ "eo-unique" ],
    1.75 +  },
    1.76 +// WebApps implementation doesn't allow apps with the same origin. Sad...
    1.77 +//  {
    1.78 +//    app: "http://example.org/foo/manifest.webapp",
    1.79 +//    src: "http://example.org/",
    1.80 +//    isapp: true,
    1.81 +//    test: [ "eo-unique" ],
    1.82 +//  },
    1.83 +//  {
    1.84 +//    app: "http://example.org/bar/manifest.webapp",
    1.85 +//    src: "http://example.org/",
    1.86 +//    isapp: true,
    1.87 +//    test: [ "eo-unique" ],
    1.88 +//  },
    1.89 +  {
    1.90 +    src: "http://example.org/",
    1.91 +    isapp: false,
    1.92 +    test: [ "eo-unique" ],
    1.93 +  },
    1.94 +  {
    1.95 +    browser: true,
    1.96 +    src: "http://example.org/",
    1.97 +    isapp: false,
    1.98 +    test: [ "eo-unique" ],
    1.99 +  },
   1.100 +  {
   1.101 +    app: "http://example.org/wedonthaveanyappinthatdirectory/manifest.webapp",
   1.102 +    src: "http://example.org/",
   1.103 +    isapp: false,
   1.104 +    // TODO: this is a browser because we need apps to be browser and it's not
   1.105 +    // an app because the manifest is invalid. Ideally, it should not be a
   1.106 +    // browser.
   1.107 +    browser: true,
   1.108 +    test: [ "eo-as-last" ],
   1.109 +  },
   1.110 +//  {
   1.111 +//    app: "http://example.org/manifest.webapp",
   1.112 +//    src: "data:text/html,foobar",
   1.113 +//    test: [ "todo-src" ],
   1.114 +//  },
   1.115 +//  {
   1.116 +//    app: "http://example.org/manifest.webapp",
   1.117 +//    src: "data:text/html,foobar2",
   1.118 +//    test: [ "todo-src" ],
   1.119 +//  },
   1.120 +  {
   1.121 +    src: "file:///",
   1.122 +    isapp: false,
   1.123 +    test: [ "eo-unique" ],
   1.124 +  },
   1.125 +  {
   1.126 +    src: "file:///tmp/",
   1.127 +    isapp: false,
   1.128 +    test: [ "eo-unique" ],
   1.129 +  },
   1.130 +  {
   1.131 +    app: "http://example.org/manifest.webapp",
   1.132 +    src: "file:///",
   1.133 +    isapp: true,
   1.134 +    test: [ "eo-unique", "in-app-not-installed" ],
   1.135 +  },
   1.136 +  {
   1.137 +    app: "http://example.org/manifest.webapp",
   1.138 +    src: "file:///tmp/",
   1.139 +    isapp: true,
   1.140 +    test: [ "eo-unique", "in-app-not-installed" ],
   1.141 +  },
   1.142 +  // iframe inside an app is part of the app.
   1.143 +  {
   1.144 +    app: "http://example.org/manifest.webapp",
   1.145 +    src: "http://example.org/",
   1.146 +    isapp: true,
   1.147 +    child: {
   1.148 +      src: "http://example.org/chrome/",
   1.149 +      isapp: true
   1.150 +    },
   1.151 +    test: [ "child-has-same-eo" ],
   1.152 +  },
   1.153 +  // app A inside app B aren't the same app.
   1.154 +  {
   1.155 +    app: "http://example.org/manifest.webapp",
   1.156 +    src: "http://example.org/",
   1.157 +    isapp: true,
   1.158 +    child: {
   1.159 +      app: "https://example.com/manifest.webapp",
   1.160 +      src: "https://example.com/chrome/",
   1.161 +      isapp: true
   1.162 +    },
   1.163 +    test: [ "child-has-different-eo", "child-has-same-appstatus", "child-has-different-appid" ],
   1.164 +  },
   1.165 +  // app A inside app A are the same app.
   1.166 +  {
   1.167 +    app: "http://example.org/manifest.webapp",
   1.168 +    src: "http://example.org/",
   1.169 +    isapp: true,
   1.170 +    child: {
   1.171 +      app: "http://example.org/manifest.webapp",
   1.172 +      src: "http://example.org/chrome/",
   1.173 +      isapp: true
   1.174 +    },
   1.175 +    test: [ "child-has-same-eo" ],
   1.176 +  },
   1.177 +  // app inside a regular iframe is an app.
   1.178 +  {
   1.179 +    src: "http://example.org/",
   1.180 +    isapp: false,
   1.181 +    child: {
   1.182 +      app: "http://example.org/manifest.webapp",
   1.183 +      src: "http://example.org/chrome/",
   1.184 +      isapp: true
   1.185 +    },
   1.186 +    test: [ "child-has-different-eo", "child-has-different-appstatus", "child-has-different-appid" ],
   1.187 +  },
   1.188 +  // browser inside app is a browser, has appid but isn't installed.
   1.189 +  {
   1.190 +    src: "http://example.org/",
   1.191 +    app: "http://example.org/manifest.webapp",
   1.192 +    isapp: true,
   1.193 +    child: {
   1.194 +      src: "http://example.org/chrome/",
   1.195 +      isapp: false,
   1.196 +      browser: true,
   1.197 +    },
   1.198 +    test: [ "child-has-different-eo", "child-has-different-appstatus", "child-has-same-appid" ],
   1.199 +  },
   1.200 +  // app inside a browser is an app.
   1.201 +  {
   1.202 +    src: "http://example.org/",
   1.203 +    isapp: false,
   1.204 +    browser: true,
   1.205 +    child: {
   1.206 +      app: "http://example.org/manifest.webapp",
   1.207 +      src: "http://example.org/chrome/",
   1.208 +      isapp: true,
   1.209 +    },
   1.210 +    test: [ "child-has-different-eo", "child-has-different-appstatus", "child-has-different-appid" ],
   1.211 +  },
   1.212 +  // browser inside a browser are two browsers
   1.213 +  {
   1.214 +    src: "http://example.org/",
   1.215 +    isapp: false,
   1.216 +    browser: true,
   1.217 +    child: {
   1.218 +      src: "http://example.org/chrome/",
   1.219 +      isapp: false,
   1.220 +      browser: true,
   1.221 +    },
   1.222 +    test: [ "child-has-same-eo" ],
   1.223 +  },
   1.224 +  // browser inside a browser are two browsers
   1.225 +  {
   1.226 +    src: "http://example.org/",
   1.227 +    isapp: false,
   1.228 +    browser: true,
   1.229 +    child: {
   1.230 +      src: "https://example.com/chrome/",
   1.231 +      isapp: false,
   1.232 +      browser: true,
   1.233 +    },
   1.234 +    test: [ "child-has-different-eo", "child-has-same-appstatus", "child-has-same-appid" ],
   1.235 +  },
   1.236 +  // iframe containing a browser
   1.237 +  {
   1.238 +    src: "http://example.org/",
   1.239 +    isapp: false,
   1.240 +    browser: false,
   1.241 +    child: {
   1.242 +      src: "http://example.org/chrome/",
   1.243 +      isapp: false,
   1.244 +      browser: true,
   1.245 +    },
   1.246 +    test: [ "child-has-different-eo", "child-has-same-appstatus", "child-has-same-appid" ],
   1.247 +  },
   1.248 +  // browser containing an iframe is part of the browser
   1.249 +  {
   1.250 +    src: "http://example.org/",
   1.251 +    isapp: false,
   1.252 +    browser: true,
   1.253 +    child: {
   1.254 +      src: "http://example.org/chrome/",
   1.255 +      isapp: false,
   1.256 +      inBrowser: true,
   1.257 +    },
   1.258 +    test: [ "child-has-same-eo" ],
   1.259 +  },
   1.260 +];
   1.261 +
   1.262 +// The list of all data ids generated by this test.
   1.263 +var eoList = [];
   1.264 +
   1.265 +var content = document.getElementById('content');
   1.266 +var checkedCount = 0;
   1.267 +var checksTodo = gData.length;
   1.268 +
   1.269 +function checkIFrame(aFrame, data) {
   1.270 +  var principal = aFrame.contentDocument.nodePrincipal;
   1.271 +
   1.272 +  if (!data.test) {
   1.273 +    data.test = [];
   1.274 +  }
   1.275 +
   1.276 +  if (navigator.platform.indexOf("Mac") != -1) {
   1.277 +    is(principal.URI.spec,
   1.278 +       data.src.replace('file:///tmp/', 'file:///private/tmp/'),
   1.279 +       'the correct URL should have been loaded');
   1.280 +  } else {
   1.281 +    is(principal.URI.spec, data.src,
   1.282 +       'the correct URL should have been loaded');
   1.283 +  }
   1.284 +
   1.285 +  if (data.isapp) {
   1.286 +    if (data.test.indexOf("in-app-not-installed") != -1) {
   1.287 +      is(principal.appStatus, Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED, 'this should not be an installed app');
   1.288 +    } else {
   1.289 +      is(principal.appStatus, Ci.nsIPrincipal.APP_STATUS_INSTALLED, 'this should be an installed app');
   1.290 +    }
   1.291 +  } else {
   1.292 +    is(principal.appStatus, Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED,
   1.293 +       'this should not be an installed app');
   1.294 +    is(principal.appId, Ci.nsIScriptSecurityManager.NO_APP_ID,
   1.295 +       "principals from non-installed app should have NO_APP_ID");
   1.296 +  }
   1.297 +
   1.298 +  if (!data.isapp && !data.browser) {
   1.299 +    is(principal.jarPrefix, "",
   1.300 +       'jarPrefix should return an empty string for non-app and non-browsers principals');
   1.301 +  } else {
   1.302 +    isnot(principal.jarPrefix, "",
   1.303 +          'jarPrefix should not return an empty string for apps or mozbrowsers');
   1.304 +  }
   1.305 +
   1.306 +  if (data.test.indexOf("eo-unique") != -1) {
   1.307 +    is(eoList.indexOf(principal.jarPrefix + principal.origin), -1,
   1.308 +       "extended origin should be unique");
   1.309 +  }
   1.310 +  if (data.test.indexOf("eo-as-last") != -1) {
   1.311 +    is(principal.jarPrefix + principal.origin, eoList[eoList.length-1],
   1.312 +       "extended origin should be the same as the last inserted one");
   1.313 +  }
   1.314 +
   1.315 +  is(principal.isInBrowserElement, !!data.browser,
   1.316 +     "check principal.isInBrowserElement");
   1.317 +
   1.318 +  if (data.child) {
   1.319 +    let childPrincipal = aFrame.contentWindow.frames[0].document.nodePrincipal;
   1.320 +
   1.321 +    if (data.child.isapp) {
   1.322 +      is(childPrincipal.appStatus, Ci.nsIPrincipal.APP_STATUS_INSTALLED,
   1.323 +         "child should be an installed app");
   1.324 +    }
   1.325 +
   1.326 +    is(childPrincipal.isInBrowserElement, !!data.child.browser || !!data.child.inBrowser,
   1.327 +       "check childPrincipal.isInBrowserElement");
   1.328 +
   1.329 +    if (data.test.indexOf("child-has-same-eo") != -1) {
   1.330 +      is(childPrincipal.jarPrefix + childPrincipal.origin,
   1.331 +         principal.jarPrefix + principal.origin,
   1.332 +         "child should have the same extended origin as parent");
   1.333 +      is(childPrincipal.appStatus, principal.appStatus,
   1.334 +         "child should have the same appStatus if it has the same extended origin");
   1.335 +      is(childPrincipal.appId, principal.appId,
   1.336 +         "child should have the same appId if it has the same extended origin");
   1.337 +    }
   1.338 +
   1.339 +    if (data.test.indexOf("child-has-different-eo") != -1) {
   1.340 +      isnot(childPrincipal.jarPrefix + childPrincipal.origin,
   1.341 +            principal.jarPrefix + principal.origin,
   1.342 +            "child should not have the same extended origin as parent");
   1.343 +    }
   1.344 +
   1.345 +    if (data.test.indexOf("child-has-same-appstatus") != -1) {
   1.346 +      is(childPrincipal.appStatus, principal.appStatus,
   1.347 +         "childPrincipal and parent principal should have the same appStatus");
   1.348 +    }
   1.349 +
   1.350 +    if (data.test.indexOf("child-has-different-appstatus") != -1) {
   1.351 +      isnot(childPrincipal.appStatus, principal.appStatus,
   1.352 +            "childPrincipal and parent principal should not have the same appStatus");
   1.353 +    }
   1.354 +
   1.355 +    if (data.test.indexOf("child-has-same-appid") != -1) {
   1.356 +      is(childPrincipal.appId, principal.appId,
   1.357 +         "childPrincipal and parent principal should have the same appId");
   1.358 +    }
   1.359 +
   1.360 +    if (data.test.indexOf("child-has-different-appid") != -1) {
   1.361 +      isnot(childPrincipal.appId, principal.appId,
   1.362 +            "childPrincipal and parent principal should have different appId");
   1.363 +    }
   1.364 +  }
   1.365 +
   1.366 +  eoList.push(principal.jarPrefix + principal.origin);
   1.367 +
   1.368 +  checkedCount++;
   1.369 +  if (checkedCount == checksTodo) {
   1.370 +    SpecialPowers.removePermission("browser", "http://example.org");
   1.371 +    SpecialPowers.removePermission("embed-apps", "http://example.org");
   1.372 +    SimpleTest.finish();
   1.373 +  } else {
   1.374 +    gTestRunner.next();
   1.375 +  }
   1.376 +}
   1.377 +
   1.378 +is('appStatus' in document.nodePrincipal, true,
   1.379 +   'appStatus should be present in nsIPrincipal');
   1.380 +is('jarPrefix' in document.nodePrincipal, true,
   1.381 +   'jarPrefix should be present in nsIPrincipal');
   1.382 +is('origin' in document.nodePrincipal, true,
   1.383 +   'origin should be present in nsIPrincipal');
   1.384 +is('appId' in document.nodePrincipal, true,
   1.385 +   'appId should be present in nsIPrincipal');
   1.386 +
   1.387 +function runTest() {
   1.388 +  // We want to use a generator. Those only work in a one level stack so we
   1.389 +  // can't use .forEach() here.
   1.390 +  for (var i=0; i<gData.length; ++i) {
   1.391 +    let data = gData[i];
   1.392 +
   1.393 +    var iframe = document.createElement('iframe');
   1.394 +    iframe.check = function() {
   1.395 +      checkIFrame(this, data);
   1.396 +    };
   1.397 +    iframe.addChild = function() {
   1.398 +      SpecialPowers.addPermission("browser", true, iframe.contentDocument);
   1.399 +      SpecialPowers.addPermission("embed-apps", true, iframe.contentDocument);
   1.400 +
   1.401 +      var childFrame = document.createElement('iframe');
   1.402 +
   1.403 +      if (data.child.app) {
   1.404 +        childFrame.setAttribute('mozapp', data.child.app)
   1.405 +        childFrame.setAttribute('mozbrowser', '');
   1.406 +      } else if (data.child.browser) {
   1.407 +        childFrame.setAttribute('mozbrowser', '');
   1.408 +      }
   1.409 +
   1.410 +      childFrame.src = data.child.src;
   1.411 +
   1.412 +      this.removeEventListener('load', this.addChild.bind(this));
   1.413 +      childFrame.addEventListener('load', this.check.bind(this));
   1.414 +
   1.415 +      this.contentDocument.body.appendChild(childFrame);
   1.416 +    };
   1.417 +
   1.418 +    if (data.app) {
   1.419 +      iframe.setAttribute('mozapp', data.app);
   1.420 +      iframe.setAttribute('mozbrowser', '');
   1.421 +    } else if (data.browser) {
   1.422 +      iframe.setAttribute('mozbrowser', '');
   1.423 +    }
   1.424 +
   1.425 +    iframe.src = data.src;
   1.426 +
   1.427 +    if (data.child) {
   1.428 +      iframe.addEventListener('load', iframe.addChild.bind(iframe));
   1.429 +    } else {
   1.430 +      iframe.addEventListener('load', iframe.check.bind(iframe));
   1.431 +    }
   1.432 +
   1.433 +    content.appendChild(iframe);
   1.434 +
   1.435 +    yield;
   1.436 +  }
   1.437 +}
   1.438 +
   1.439 +var gTestRunner = runTest();
   1.440 +
   1.441 +SpecialPowers.pushPrefEnv({'set':[["dom.mozBrowserFramesEnabled", true]]},
   1.442 +                           function() { gTestRunner.next(); });
   1.443 +
   1.444 +</script>
   1.445 +</pre>
   1.446 +</body>
   1.447 +</html>

mercurial