summaryrefslogtreecommitdiffstats
path: root/devtools/shared/security/tests/xpcshell/test_encryption.js
blob: 1721418a5bf5c58902883e8099ba3e1964308878 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test basic functionality of DevTools client and server TLS encryption mode
function run_test() {
  // Need profile dir to store the key / cert
  do_get_profile();
  // Ensure PSM is initialized
  Cc["@mozilla.org/psm;1"].getService(Ci.nsISupports);
  run_next_test();
}

function connectClient(client) {
  return client.connect();
}

add_task(async function() {
  initTestDevToolsServer();
});

// Client w/ encryption connects successfully to server w/ encryption
add_task(async function() {
  equal(DevToolsServer.listeningSockets, 0, "0 listening sockets");

  const AuthenticatorType = DevToolsServer.Authenticators.get("PROMPT");
  const authenticator = new AuthenticatorType.Server();
  authenticator.allowConnection = () => {
    return DevToolsServer.AuthenticationResult.ALLOW;
  };

  const socketOptions = {
    authenticator,
    encryption: true,
    portOrPath: -1,
  };
  const listener = new SocketListener(DevToolsServer, socketOptions);
  ok(listener, "Socket listener created");
  await listener.open();
  equal(DevToolsServer.listeningSockets, 1, "1 listening socket");

  const transport = await DevToolsClient.socketConnect({
    host: "127.0.0.1",
    port: listener.port,
    encryption: true,
  });
  ok(transport, "Client transport created");

  const client = new DevToolsClient(transport);
  const onUnexpectedClose = () => {
    do_throw("Closed unexpectedly");
  };
  client.on("closed", onUnexpectedClose);
  await connectClient(client);

  // Send a message the server will echo back
  const message = "secrets";
  const reply = await client.mainRoot.echo({ message });
  equal(reply.message, message, "Encrypted echo matches");

  client.off("closed", onUnexpectedClose);
  transport.close();
  listener.close();
  equal(DevToolsServer.listeningSockets, 0, "0 listening sockets");
});

// Client w/o encryption fails to connect to server w/ encryption
add_task(async function() {
  equal(DevToolsServer.listeningSockets, 0, "0 listening sockets");

  const AuthenticatorType = DevToolsServer.Authenticators.get("PROMPT");
  const authenticator = new AuthenticatorType.Server();
  authenticator.allowConnection = () => {
    return DevToolsServer.AuthenticationResult.ALLOW;
  };

  const socketOptions = {
    authenticator,
    encryption: true,
    portOrPath: -1,
  };
  const listener = new SocketListener(DevToolsServer, socketOptions);
  ok(listener, "Socket listener created");
  await listener.open();
  equal(DevToolsServer.listeningSockets, 1, "1 listening socket");

  try {
    await DevToolsClient.socketConnect({
      host: "127.0.0.1",
      port: listener.port,
      // encryption: false is the default
    });
  } catch (e) {
    ok(true, "Client failed to connect as expected");
    listener.close();
    equal(DevToolsServer.listeningSockets, 0, "0 listening sockets");
    return;
  }

  do_throw("Connection unexpectedly succeeded");
});

add_task(async function() {
  DevToolsServer.destroy();
});