• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4const common = require('../common');
5const assert = require('assert');
6const dgram = require('dgram');
7const { inspect } = require('util');
8const { internalBinding } = require('internal/test/binding');
9const {
10  UV_EBADF,
11  UV_EINVAL,
12  UV_ENOTSOCK
13} = internalBinding('uv');
14
15function getExpectedError(type) {
16  const code = common.isWindows ? 'ENOTSOCK' : 'EBADF';
17  const message = common.isWindows ?
18    'socket operation on non-socket' : 'bad file descriptor';
19  const errno = common.isWindows ? UV_ENOTSOCK : UV_EBADF;
20  const syscall = `uv_${type}_buffer_size`;
21  const suffix = common.isWindows ?
22    'ENOTSOCK (socket operation on non-socket)' : 'EBADF (bad file descriptor)';
23  const error = {
24    code: 'ERR_SOCKET_BUFFER_SIZE',
25    name: 'SystemError',
26    message: `Could not get or set buffer size: ${syscall} returned ${suffix}`,
27    info: {
28      code,
29      message,
30      errno,
31      syscall
32    }
33  };
34  return error;
35}
36
37{
38  // Should throw error if the socket is never bound.
39  const errorObj = getExpectedError('send');
40
41  const socket = dgram.createSocket('udp4');
42
43  assert.throws(() => {
44    socket.setSendBufferSize(8192);
45  }, (err) => {
46    assert.strictEqual(
47      inspect(err).replace(/^ +at .*\n/gm, ''),
48      `SystemError [ERR_SOCKET_BUFFER_SIZE]: ${errorObj.message}\n` +
49        "  code: 'ERR_SOCKET_BUFFER_SIZE',\n" +
50        '  info: {\n' +
51        `    errno: ${errorObj.info.errno},\n` +
52        `    code: '${errorObj.info.code}',\n` +
53        `    message: '${errorObj.info.message}',\n` +
54        `    syscall: '${errorObj.info.syscall}'\n` +
55        '  },\n' +
56        `  errno: [Getter/Setter: ${errorObj.info.errno}],\n` +
57        `  syscall: [Getter/Setter: '${errorObj.info.syscall}']\n` +
58        '}'
59    );
60    return true;
61  });
62
63  assert.throws(() => {
64    socket.getSendBufferSize();
65  }, errorObj);
66}
67
68{
69  const socket = dgram.createSocket('udp4');
70
71  // Should throw error if the socket is never bound.
72  const errorObj = getExpectedError('recv');
73
74  assert.throws(() => {
75    socket.setRecvBufferSize(8192);
76  }, errorObj);
77
78  assert.throws(() => {
79    socket.getRecvBufferSize();
80  }, errorObj);
81}
82
83{
84  // Should throw error if invalid buffer size is specified
85  const errorObj = {
86    code: 'ERR_SOCKET_BAD_BUFFER_SIZE',
87    name: 'TypeError',
88    message: /^Buffer size must be a positive integer$/
89  };
90
91  const badBufferSizes = [-1, Infinity, 'Doh!'];
92
93  const socket = dgram.createSocket('udp4');
94
95  socket.bind(common.mustCall(() => {
96    badBufferSizes.forEach((badBufferSize) => {
97      assert.throws(() => {
98        socket.setRecvBufferSize(badBufferSize);
99      }, errorObj);
100
101      assert.throws(() => {
102        socket.setSendBufferSize(badBufferSize);
103      }, errorObj);
104    });
105    socket.close();
106  }));
107}
108
109{
110  // Can set and get buffer sizes after binding the socket.
111  const socket = dgram.createSocket('udp4');
112
113  socket.bind(common.mustCall(() => {
114    socket.setRecvBufferSize(10000);
115    socket.setSendBufferSize(10000);
116
117    // note: linux will double the buffer size
118    const expectedBufferSize = common.isLinux ? 20000 : 10000;
119    assert.strictEqual(socket.getRecvBufferSize(), expectedBufferSize);
120    assert.strictEqual(socket.getSendBufferSize(), expectedBufferSize);
121    socket.close();
122  }));
123}
124
125{
126  const info = {
127    code: 'EINVAL',
128    message: 'invalid argument',
129    errno: UV_EINVAL,
130    syscall: 'uv_recv_buffer_size'
131  };
132  const errorObj = {
133    code: 'ERR_SOCKET_BUFFER_SIZE',
134    name: 'SystemError',
135    message: 'Could not get or set buffer size: uv_recv_buffer_size ' +
136             'returned EINVAL (invalid argument)',
137    info
138  };
139  const socket = dgram.createSocket('udp4');
140  socket.bind(common.mustCall(() => {
141    assert.throws(() => {
142      socket.setRecvBufferSize(2147483648);
143    }, errorObj);
144    socket.close();
145  }));
146}
147
148{
149  const info = {
150    code: 'EINVAL',
151    message: 'invalid argument',
152    errno: UV_EINVAL,
153    syscall: 'uv_send_buffer_size'
154  };
155  const errorObj = {
156    code: 'ERR_SOCKET_BUFFER_SIZE',
157    name: 'SystemError',
158    message: 'Could not get or set buffer size: uv_send_buffer_size ' +
159             'returned EINVAL (invalid argument)',
160    info
161  };
162  const socket = dgram.createSocket('udp4');
163  socket.bind(common.mustCall(() => {
164    assert.throws(() => {
165      socket.setSendBufferSize(2147483648);
166    }, errorObj);
167    socket.close();
168  }));
169}
170