• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3const common = require('../common');
4const assert = require('assert');
5const dgram = require('dgram');
6const { internalBinding } = require('internal/test/binding');
7const { UV_UNKNOWN } = internalBinding('uv');
8const { getSystemErrorName } = require('util');
9const { kStateSymbol } = require('internal/dgram');
10const mockError = new Error('mock DNS error');
11
12function getSocket(callback) {
13  const socket = dgram.createSocket('udp4');
14
15  socket.on('message', common.mustNotCall('Should not receive any messages.'));
16  socket.bind(common.mustCall(() => {
17    socket[kStateSymbol].handle.lookup = function(address, callback) {
18      process.nextTick(callback, mockError);
19    };
20
21    callback(socket);
22  }));
23  return socket;
24}
25
26getSocket((socket) => {
27  socket.on('error', common.mustCall((err) => {
28    socket.close();
29    assert.strictEqual(err, mockError);
30  }));
31
32  socket.send('foo', socket.address().port, 'localhost');
33});
34
35getSocket((socket) => {
36  const callback = common.mustCall((err) => {
37    socket.close();
38    assert.strictEqual(err, mockError);
39  });
40
41  socket.send('foo', socket.address().port, 'localhost', callback);
42});
43
44{
45  const socket = dgram.createSocket('udp4');
46
47  socket.on('message', common.mustNotCall('Should not receive any messages.'));
48
49  socket.bind(common.mustCall(() => {
50    const port = socket.address().port;
51    const callback = common.mustCall((err) => {
52      socket.close();
53      assert.strictEqual(err.code, 'UNKNOWN');
54      assert.strictEqual(getSystemErrorName(err.errno), 'UNKNOWN');
55      assert.strictEqual(err.syscall, 'send');
56      assert.strictEqual(err.address, common.localhostIPv4);
57      assert.strictEqual(err.port, port);
58      assert.strictEqual(
59        err.message,
60        `${err.syscall} ${err.code} ${err.address}:${err.port}`
61      );
62    });
63
64    socket[kStateSymbol].handle.send = function() {
65      return UV_UNKNOWN;
66    };
67
68    socket.send('foo', port, common.localhostIPv4, callback);
69  }));
70}
71