11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst dgram = require('dgram');
71cb0ef41Sopenharmony_ciconst { inspect } = require('util');
81cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
91cb0ef41Sopenharmony_ciconst {
101cb0ef41Sopenharmony_ci  UV_EBADF,
111cb0ef41Sopenharmony_ci  UV_EINVAL,
121cb0ef41Sopenharmony_ci  UV_ENOTSOCK
131cb0ef41Sopenharmony_ci} = internalBinding('uv');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cifunction getExpectedError(type) {
161cb0ef41Sopenharmony_ci  const code = common.isWindows ? 'ENOTSOCK' : 'EBADF';
171cb0ef41Sopenharmony_ci  const message = common.isWindows ?
181cb0ef41Sopenharmony_ci    'socket operation on non-socket' : 'bad file descriptor';
191cb0ef41Sopenharmony_ci  const errno = common.isWindows ? UV_ENOTSOCK : UV_EBADF;
201cb0ef41Sopenharmony_ci  const syscall = `uv_${type}_buffer_size`;
211cb0ef41Sopenharmony_ci  const suffix = common.isWindows ?
221cb0ef41Sopenharmony_ci    'ENOTSOCK (socket operation on non-socket)' : 'EBADF (bad file descriptor)';
231cb0ef41Sopenharmony_ci  const error = {
241cb0ef41Sopenharmony_ci    code: 'ERR_SOCKET_BUFFER_SIZE',
251cb0ef41Sopenharmony_ci    name: 'SystemError',
261cb0ef41Sopenharmony_ci    message: `Could not get or set buffer size: ${syscall} returned ${suffix}`,
271cb0ef41Sopenharmony_ci    info: {
281cb0ef41Sopenharmony_ci      code,
291cb0ef41Sopenharmony_ci      message,
301cb0ef41Sopenharmony_ci      errno,
311cb0ef41Sopenharmony_ci      syscall
321cb0ef41Sopenharmony_ci    }
331cb0ef41Sopenharmony_ci  };
341cb0ef41Sopenharmony_ci  return error;
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci{
381cb0ef41Sopenharmony_ci  // Should throw error if the socket is never bound.
391cb0ef41Sopenharmony_ci  const errorObj = getExpectedError('send');
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  assert.throws(() => {
441cb0ef41Sopenharmony_ci    socket.setSendBufferSize(8192);
451cb0ef41Sopenharmony_ci  }, (err) => {
461cb0ef41Sopenharmony_ci    assert.strictEqual(
471cb0ef41Sopenharmony_ci      inspect(err).replace(/^ +at .*\n/gm, ''),
481cb0ef41Sopenharmony_ci      `SystemError [ERR_SOCKET_BUFFER_SIZE]: ${errorObj.message}\n` +
491cb0ef41Sopenharmony_ci        "  code: 'ERR_SOCKET_BUFFER_SIZE',\n" +
501cb0ef41Sopenharmony_ci        '  info: {\n' +
511cb0ef41Sopenharmony_ci        `    errno: ${errorObj.info.errno},\n` +
521cb0ef41Sopenharmony_ci        `    code: '${errorObj.info.code}',\n` +
531cb0ef41Sopenharmony_ci        `    message: '${errorObj.info.message}',\n` +
541cb0ef41Sopenharmony_ci        `    syscall: '${errorObj.info.syscall}'\n` +
551cb0ef41Sopenharmony_ci        '  },\n' +
561cb0ef41Sopenharmony_ci        `  errno: [Getter/Setter: ${errorObj.info.errno}],\n` +
571cb0ef41Sopenharmony_ci        `  syscall: [Getter/Setter: '${errorObj.info.syscall}']\n` +
581cb0ef41Sopenharmony_ci        '}'
591cb0ef41Sopenharmony_ci    );
601cb0ef41Sopenharmony_ci    return true;
611cb0ef41Sopenharmony_ci  });
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  assert.throws(() => {
641cb0ef41Sopenharmony_ci    socket.getSendBufferSize();
651cb0ef41Sopenharmony_ci  }, errorObj);
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci{
691cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  // Should throw error if the socket is never bound.
721cb0ef41Sopenharmony_ci  const errorObj = getExpectedError('recv');
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  assert.throws(() => {
751cb0ef41Sopenharmony_ci    socket.setRecvBufferSize(8192);
761cb0ef41Sopenharmony_ci  }, errorObj);
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  assert.throws(() => {
791cb0ef41Sopenharmony_ci    socket.getRecvBufferSize();
801cb0ef41Sopenharmony_ci  }, errorObj);
811cb0ef41Sopenharmony_ci}
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci{
841cb0ef41Sopenharmony_ci  // Should throw error if invalid buffer size is specified
851cb0ef41Sopenharmony_ci  const errorObj = {
861cb0ef41Sopenharmony_ci    code: 'ERR_SOCKET_BAD_BUFFER_SIZE',
871cb0ef41Sopenharmony_ci    name: 'TypeError',
881cb0ef41Sopenharmony_ci    message: /^Buffer size must be a positive integer$/
891cb0ef41Sopenharmony_ci  };
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci  const badBufferSizes = [-1, Infinity, 'Doh!'];
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  socket.bind(common.mustCall(() => {
961cb0ef41Sopenharmony_ci    badBufferSizes.forEach((badBufferSize) => {
971cb0ef41Sopenharmony_ci      assert.throws(() => {
981cb0ef41Sopenharmony_ci        socket.setRecvBufferSize(badBufferSize);
991cb0ef41Sopenharmony_ci      }, errorObj);
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci      assert.throws(() => {
1021cb0ef41Sopenharmony_ci        socket.setSendBufferSize(badBufferSize);
1031cb0ef41Sopenharmony_ci      }, errorObj);
1041cb0ef41Sopenharmony_ci    });
1051cb0ef41Sopenharmony_ci    socket.close();
1061cb0ef41Sopenharmony_ci  }));
1071cb0ef41Sopenharmony_ci}
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci{
1101cb0ef41Sopenharmony_ci  // Can set and get buffer sizes after binding the socket.
1111cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  socket.bind(common.mustCall(() => {
1141cb0ef41Sopenharmony_ci    socket.setRecvBufferSize(10000);
1151cb0ef41Sopenharmony_ci    socket.setSendBufferSize(10000);
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci    // note: linux will double the buffer size
1181cb0ef41Sopenharmony_ci    const expectedBufferSize = common.isLinux ? 20000 : 10000;
1191cb0ef41Sopenharmony_ci    assert.strictEqual(socket.getRecvBufferSize(), expectedBufferSize);
1201cb0ef41Sopenharmony_ci    assert.strictEqual(socket.getSendBufferSize(), expectedBufferSize);
1211cb0ef41Sopenharmony_ci    socket.close();
1221cb0ef41Sopenharmony_ci  }));
1231cb0ef41Sopenharmony_ci}
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci{
1261cb0ef41Sopenharmony_ci  const info = {
1271cb0ef41Sopenharmony_ci    code: 'EINVAL',
1281cb0ef41Sopenharmony_ci    message: 'invalid argument',
1291cb0ef41Sopenharmony_ci    errno: UV_EINVAL,
1301cb0ef41Sopenharmony_ci    syscall: 'uv_recv_buffer_size'
1311cb0ef41Sopenharmony_ci  };
1321cb0ef41Sopenharmony_ci  const errorObj = {
1331cb0ef41Sopenharmony_ci    code: 'ERR_SOCKET_BUFFER_SIZE',
1341cb0ef41Sopenharmony_ci    name: 'SystemError',
1351cb0ef41Sopenharmony_ci    message: 'Could not get or set buffer size: uv_recv_buffer_size ' +
1361cb0ef41Sopenharmony_ci             'returned EINVAL (invalid argument)',
1371cb0ef41Sopenharmony_ci    info
1381cb0ef41Sopenharmony_ci  };
1391cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
1401cb0ef41Sopenharmony_ci  socket.bind(common.mustCall(() => {
1411cb0ef41Sopenharmony_ci    assert.throws(() => {
1421cb0ef41Sopenharmony_ci      socket.setRecvBufferSize(2147483648);
1431cb0ef41Sopenharmony_ci    }, errorObj);
1441cb0ef41Sopenharmony_ci    socket.close();
1451cb0ef41Sopenharmony_ci  }));
1461cb0ef41Sopenharmony_ci}
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci{
1491cb0ef41Sopenharmony_ci  const info = {
1501cb0ef41Sopenharmony_ci    code: 'EINVAL',
1511cb0ef41Sopenharmony_ci    message: 'invalid argument',
1521cb0ef41Sopenharmony_ci    errno: UV_EINVAL,
1531cb0ef41Sopenharmony_ci    syscall: 'uv_send_buffer_size'
1541cb0ef41Sopenharmony_ci  };
1551cb0ef41Sopenharmony_ci  const errorObj = {
1561cb0ef41Sopenharmony_ci    code: 'ERR_SOCKET_BUFFER_SIZE',
1571cb0ef41Sopenharmony_ci    name: 'SystemError',
1581cb0ef41Sopenharmony_ci    message: 'Could not get or set buffer size: uv_send_buffer_size ' +
1591cb0ef41Sopenharmony_ci             'returned EINVAL (invalid argument)',
1601cb0ef41Sopenharmony_ci    info
1611cb0ef41Sopenharmony_ci  };
1621cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
1631cb0ef41Sopenharmony_ci  socket.bind(common.mustCall(() => {
1641cb0ef41Sopenharmony_ci    assert.throws(() => {
1651cb0ef41Sopenharmony_ci      socket.setSendBufferSize(2147483648);
1661cb0ef41Sopenharmony_ci    }, errorObj);
1671cb0ef41Sopenharmony_ci    socket.close();
1681cb0ef41Sopenharmony_ci  }));
1691cb0ef41Sopenharmony_ci}
170