bug 1600330: remote: test nsIRemoteAgent r=remote-protocol-reviewers,maja_zf,whimboo
☠☠ backed out by 7a14672fc95d ☠ ☠
authorAndreas Tolfsen <ato@sny.no>
Tue, 03 Dec 2019 17:13:47 +0000
changeset 505137 eef79aaf5378bc898ae68633abc59330f4a8bd1e
parent 505136 695a3213e7fdb1b0c8fdb69a8d563b774149a905
child 505138 7cabf80092ab9fe28266b39670a851df0a302115
push id102166
push useratolfsen@mozilla.com
push dateTue, 03 Dec 2019 17:25:10 +0000
treeherderautoland@eef79aaf5378 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersremote-protocol-reviewers, maja_zf, whimboo
bugs1600330, 1590828
milestone73.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 1600330: remote: test nsIRemoteAgent r=remote-protocol-reviewers,maja_zf,whimboo Adds browser-chrome tests for the main entry point of the remote agent, nsIRemoteAgent. add_agent_task() wraps add_plain_task() (which again wraps the original add_task()) so we can ensure that the agent isn't listening before a test runs, and that any modified state such as preferences is reset afterwards. The tests rely heavily on the assumption that nsIRemoteAgent.close() behaves as a no-op when the server is not listening. This patch is a follow-up to bug 1590828. Differential Revision: https://phabricator.services.mozilla.com/D55456
remote/test/browser/browser.ini
remote/test/browser/browser_agent.js
--- a/remote/test/browser/browser.ini
+++ b/remote/test/browser/browser.ini
@@ -1,12 +1,13 @@
 [DEFAULT]
 tags = remote
 subsuite = remote
 prefs = remote.enabled=true
 support-files =
   chrome-remote-interface.js
   head.js
 
+[browser_agent.js]
 [browser_cdp.js]
 [browser_main_target.js]
 [browser_session.js]
 [browser_tabs.js]
new file mode 100644
--- /dev/null
+++ b/remote/test/browser/browser_agent.js
@@ -0,0 +1,109 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const { Preferences } = ChromeUtils.import(
+  "resource://gre/modules/Preferences.jsm"
+);
+
+const URL = Services.io.newURI("http://localhost:0");
+
+// set up test conditions and clean up
+function add_agent_task(taskFn) {
+  add_plain_task(async () => {
+    try {
+      await RemoteAgent.close();
+      await taskFn();
+    } finally {
+      Preferences.reset("remote.enabled");
+      Preferences.reset("remote.force-local");
+    }
+  });
+}
+
+add_agent_task(async function listening() {
+  is(RemoteAgent.listening, false, "Agent is not listening");
+  await RemoteAgent.listen(URL);
+  is(RemoteAgent.listening, true, "Agent is listening");
+});
+
+add_agent_task(async function listen() {
+  const port = getNonAtomicFreePort();
+
+  let boundURL;
+  function observer(subject, topic, data) {
+    Services.obs.removeObserver(observer, topic);
+    boundURL = Services.io.newURI(data);
+  }
+  Services.obs.addObserver(observer, "remote-listening");
+
+  await RemoteAgent.listen("http://localhost:" + port);
+  isnot(boundURL, undefined, "remote-listening observer not notified");
+  is(
+    boundURL.port,
+    port,
+    `expected default port ${port}, but got ${boundURL.port}`
+  );
+});
+
+add_agent_task(async function listenWhenDisabled() {
+  Preferences.set("remote.enabled", false);
+  try {
+    await RemoteAgent.listen(URL);
+    fail("listen() did not return exception");
+  } catch (e) {
+    is(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
+    is(e.message, "Disabled by preference");
+  }
+});
+
+// TODO(ato): https://bugzil.la/1590829
+add_agent_task(async function listenTakesString() {
+  await RemoteAgent.listen("http://localhost:0");
+  await RemoteAgent.close();
+});
+
+// TODO(ato): https://bugzil.la/1590829
+add_agent_task(async function listenNonURL() {
+  try {
+    await RemoteAgent.listen("foobar");
+    fail("listen() did not reject non-URL");
+  } catch (e) {
+    is(e.result, Cr.NS_ERROR_MALFORMED_URI);
+  }
+});
+
+add_agent_task(async function listenRestrictedToLoopbackDevice() {
+  try {
+    await RemoteAgent.listen("http://0.0.0.0:0");
+    fail("listen() did not reject non-loopback device");
+  } catch (e) {
+    is(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
+    is(e.message, "Restricted to loopback devices");
+  }
+});
+
+add_agent_task(async function listenNonLoopbackDevice() {
+  Preferences.set("remote.force-local", false);
+  await RemoteAgent.listen("http://0.0.0.0:0");
+});
+
+add_agent_task(async function test_close() {
+  await RemoteAgent.listen(URL);
+  await RemoteAgent.close();
+  // no-op when not listening
+  await RemoteAgent.close();
+});
+
+function getNonAtomicFreePort() {
+  const so = Cc["@mozilla.org/network/server-socket;1"].createInstance(
+    Ci.nsIServerSocket
+  );
+  try {
+    so.init(-1, true /* aLoopbackOnly */, -1 /* aBackLog */);
+    return so.port;
+  } finally {
+    so.close();
+  }
+}