dom/identity/tests/mochitest/test_syntheticEvents.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <!--
     4   https://bugzilla.mozilla.org/show_bug.cgi?id=971379
     5 -->
     6 <head>
     7   <meta charset="utf-8">
     8   <title>Certified/packaged apps may use synthetic events with FXA -- Bug 971379</title>
     9   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    10   <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
    11 </head>
    12 <body>
    13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=971379">Mozilla Bug 971379</a>
    14 <p id="display"></p>
    15 <div id="content">
    17 </div>
    18 <pre id="test">
    19 <script type="application/javascript;version=1.8">
    21 SimpleTest.waitForExplicitFinish();
    23 Components.utils.import("resource://gre/modules/Promise.jsm");
    24 Components.utils.import("resource://gre/modules/Services.jsm");
    25 Components.utils.import("resource://gre/modules/DOMIdentity.jsm");
    26 Components.utils.import("resource://gre/modules/identity/jwcrypto.jsm");
    27 Components.utils.import("resource://gre/modules/identity/FirefoxAccounts.jsm");
    29 // Mock the Firefox Accounts manager to give a dummy assertion, just to confirm
    30 // that we're making the trip through the dom/identity and toolkit/identity
    31 // plumbing.
    32 function MockFXAManager() {}
    33 MockFXAManager.prototype = {
    34   getAssertion: function(audience, options) {
    35     if (options.silent) {
    36       return Promise.resolve(null);
    37     }
    38     return Promise.resolve("here~you.go.dude");
    39   }
    40 };
    42 let originalManager = FirefoxAccounts.fxAccountsManager;
    43 FirefoxAccounts.fxAccountsManager = new MockFXAManager();
    45 // Mock IdentityService (Persona) so we can test request() while not handling
    46 // user input on an installed app.  Since in this test suite, we have only this
    47 // one test for Persona, we additionally cause request() to throw if invoked, as
    48 // added security that nsDOMIdentity did not emit a request message.
    49 let MockIdentityService = function() {
    50   this.RP = this;
    51   this.contexts = {};
    52 }
    53 MockIdentityService.prototype = {
    54   watch: function(context) {
    55     this.contexts[context.id] = context;
    56     context.doReady();
    57   },
    59   request: function(message) {
    60     ok(false, "nsDOMIdentity should block Persona request() in this test suite");
    61   },
    62 };
    63 DOMIdentity._mockIdentityService = new MockIdentityService();
    65 // The manifests for these apps are all declared in
    66 // /testing/profiles/webapps_mochitest.json.  They are injected into the profile
    67 // by /testing/mochitest/runtests.py with the appropriate appStatus.  So we don't
    68 // have to manually install any apps.
    69 let apps = [
    70   {
    71     title: "an installed app, which must request() in a native event",
    72     manifest: "https://example.com/manifest.webapp",
    73     origin: "https://example.com",
    74     uri: "https://example.com/chrome/dom/identity/tests/mochitest/file_syntheticEvents.html",
    75     wantIssuer: "",  // default to persona
    76     expected: {
    77       success: false,
    78       errors: [
    79         "ERROR_REQUEST_WHILE_NOT_HANDLING_USER_INPUT",
    80       ],
    81     },
    82   },
    83   {
    84     title: "an installed app, which must may not use firefox accounts",
    85     manifest: "https://example.com/manifest.webapp",
    86     origin: "https://example.com",
    87     uri: "https://example.com/chrome/dom/identity/tests/mochitest/file_syntheticEvents.html",
    88     wantIssuer: "firefox-accounts",
    89     expected: {
    90       success: false,
    91       errors: [
    92         "ERROR_NOT_AUTHORIZED_FOR_FIREFOX_ACCOUNTS",
    93       ],
    94     },
    95   },
    96   {
    97     title: "a privileged app, which may use synthetic events",
    98     manifest: "https://example.com/manifest_priv.webapp",
    99     origin: "https://example.com",
   100     uri: "https://example.com/chrome/dom/identity/tests/mochitest/file_syntheticEvents.html",
   101     wantIssuer: "firefox-accounts",
   102     expected: {
   103       success: true,
   104     },
   105   },
   106   {
   107     title: "a certified app, which may use synthetic events",
   108     manifest: "https://example.com/manifest_cert.webapp",
   109     origin: "https://example.com",
   110     uri: "https://example.com/chrome/dom/identity/tests/mochitest/file_syntheticEvents.html",
   111     wantIssuer: "firefox-accounts",
   112     expected: {
   113       success: true,
   114     },
   115   },
   116 ];
   118 let appIndex = 0;
   119 let testRunner = runTest();
   120 let receivedErrors = [];
   122 function receiveMessage(event) {
   123   dump("** Received response: " + event.data + "\n");
   124   let result = JSON.parse(event.data);
   125   let app = apps[appIndex];
   126   let expected = app.expected;
   128   is(result.success, expected.success,
   129     "Assertion request " + (expected.success ? "succeeds" : "fails"));
   131   if (result.error) {
   132     receivedErrors.push(result.error);
   133   }
   135   if (receivedErrors.length === (expected.errors || []).length) {
   136     receivedErrors.forEach((error) => {
   137       ok(expected.errors.indexOf(error) > -1,
   138          "Received " + error + ".  " +
   139          "Expected errors are: " + JSON.stringify(expected.errors));
   140     });
   142     appIndex += 1;
   144     if (appIndex === apps.length) {
   145       window.removeEventListener("message", receiveMessage);
   147       // Remove mock from DOMIdentity
   148       DOMIdentity._mockIdentityService = null;
   150       // Restore original fxa manager
   151       FirefoxAccounts.fxAccountsManager = originalManager;
   153       SimpleTest.finish();
   154       return;
   155     }
   157     testRunner.next();
   158   }
   159 }
   161 window.addEventListener("message", receiveMessage, false, true);
   163 function runTest() {
   164   for (let app of apps) {
   165     dump("** Testing " + app.title + "\n");
   167     receivedErrors = [];
   169     let iframe = document.createElement("iframe");
   171     iframe.setAttribute("mozapp", app.manifest);
   172     iframe.setAttribute("mozbrowser", "true");
   173     iframe.src = app.uri;
   175     document.getElementById("content").appendChild(iframe);
   177     iframe.addEventListener("load", function onLoad() {
   178       iframe.removeEventListener("load", onLoad);
   180       // Because the <iframe mozapp> can't parent its way back to us, we
   181       // provide this handle to our window so it can postMessage to us.
   182       iframe.contentWindow.wrappedJSObject.realParent = window;
   183       iframe.contentWindow.postMessage({wantIssuer: app.wantIssuer}, "*");
   184     }, false);
   186     yield undefined;
   187   }
   188 }
   190 SpecialPowers.pushPrefEnv({"set":
   191   [
   192     ["dom.mozBrowserFramesEnabled", true],
   193     ["dom.identity.enabled", true],
   194     ["identity.fxaccounts.enabled", true],
   195     ["toolkit.identity.debug", true],
   197     ["security.apps.privileged.CSP.default", ""],
   198     ["security.apps.certified.CSP.default", ""],
   199   ]},
   200   function() {
   201     testRunner.next();
   202   }
   203 );
   206 </script>
   207 </pre>
   208 </body>
   209 </html>

mercurial