• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const net = require('net');
5const assert = require('assert');
6const fs = require('fs');
7
8const tmpdir = require('../common/tmpdir');
9tmpdir.refresh();
10
11function closeServer() {
12  return common.mustCall(function() {
13    this.close();
14  });
15}
16
17let counter = 0;
18
19// Avoid conflict with listen-handle
20function randomPipePath() {
21  return `${common.PIPE}-listen-path-${counter++}`;
22}
23
24// Test listen(path)
25{
26  const handlePath = randomPipePath();
27  net.createServer()
28    .listen(handlePath)
29    .on('listening', closeServer());
30}
31
32// Test listen({path})
33{
34  const handlePath = randomPipePath();
35  net.createServer()
36    .listen({ path: handlePath })
37    .on('listening', closeServer());
38}
39
40// Test listen(path, cb)
41{
42  const handlePath = randomPipePath();
43  net.createServer()
44    .listen(handlePath, closeServer());
45}
46
47// Test listen(path, cb)
48{
49  const handlePath = randomPipePath();
50  net.createServer()
51    .listen({ path: handlePath }, closeServer());
52}
53
54// Test pipe chmod
55{
56  const handlePath = randomPipePath();
57
58  const server = net.createServer()
59    .listen({
60      path: handlePath,
61      readableAll: true,
62      writableAll: true
63    }, common.mustCall(() => {
64      if (process.platform !== 'win32') {
65        const mode = fs.statSync(handlePath).mode;
66        assert.notStrictEqual(mode & fs.constants.S_IROTH, 0);
67        assert.notStrictEqual(mode & fs.constants.S_IWOTH, 0);
68      }
69      server.close();
70    }));
71}
72
73// Test should emit "error" events when listening fails.
74{
75  const handlePath = randomPipePath();
76  const server1 = net.createServer().listen({ path: handlePath }, () => {
77    // As the handlePath is in use, binding to the same address again should
78    // make the server emit an 'EADDRINUSE' error.
79    const server2 = net.createServer()
80      .listen({
81        path: handlePath,
82        writableAll: true,
83      }, common.mustNotCall());
84
85    server2.on('error', common.mustCall((err) => {
86      server1.close();
87      assert.strictEqual(err.code, 'EADDRINUSE');
88      assert.match(err.message, /^listen EADDRINUSE: address already in use/);
89    }));
90  });
91}
92