11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst dgram = require('dgram');
61cb0ef41Sopenharmony_ciconst multicastAddress = '224.0.0.114';
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true });
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// addMembership() on closed socket should throw
111cb0ef41Sopenharmony_ci{
121cb0ef41Sopenharmony_ci  const socket = setup();
131cb0ef41Sopenharmony_ci  socket.close(common.mustCall(() => {
141cb0ef41Sopenharmony_ci    assert.throws(() => {
151cb0ef41Sopenharmony_ci      socket.addMembership(multicastAddress);
161cb0ef41Sopenharmony_ci    }, {
171cb0ef41Sopenharmony_ci      code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
181cb0ef41Sopenharmony_ci      name: 'Error',
191cb0ef41Sopenharmony_ci      message: /^Not running$/
201cb0ef41Sopenharmony_ci    });
211cb0ef41Sopenharmony_ci  }));
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// dropMembership() on closed socket should throw
251cb0ef41Sopenharmony_ci{
261cb0ef41Sopenharmony_ci  const socket = setup();
271cb0ef41Sopenharmony_ci  socket.close(common.mustCall(() => {
281cb0ef41Sopenharmony_ci    assert.throws(() => {
291cb0ef41Sopenharmony_ci      socket.dropMembership(multicastAddress);
301cb0ef41Sopenharmony_ci    }, {
311cb0ef41Sopenharmony_ci      code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
321cb0ef41Sopenharmony_ci      name: 'Error',
331cb0ef41Sopenharmony_ci      message: /^Not running$/
341cb0ef41Sopenharmony_ci    });
351cb0ef41Sopenharmony_ci  }));
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci// addMembership() with no argument should throw
391cb0ef41Sopenharmony_ci{
401cb0ef41Sopenharmony_ci  const socket = setup();
411cb0ef41Sopenharmony_ci  assert.throws(() => {
421cb0ef41Sopenharmony_ci    socket.addMembership();
431cb0ef41Sopenharmony_ci  }, {
441cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_ARGS',
451cb0ef41Sopenharmony_ci    name: 'TypeError',
461cb0ef41Sopenharmony_ci    message: /^The "multicastAddress" argument must be specified$/
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci  socket.close();
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci// dropMembership() with no argument should throw
521cb0ef41Sopenharmony_ci{
531cb0ef41Sopenharmony_ci  const socket = setup();
541cb0ef41Sopenharmony_ci  assert.throws(() => {
551cb0ef41Sopenharmony_ci    socket.dropMembership();
561cb0ef41Sopenharmony_ci  }, {
571cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_ARGS',
581cb0ef41Sopenharmony_ci    name: 'TypeError',
591cb0ef41Sopenharmony_ci    message: /^The "multicastAddress" argument must be specified$/
601cb0ef41Sopenharmony_ci  });
611cb0ef41Sopenharmony_ci  socket.close();
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci// addMembership() with invalid multicast address should throw
651cb0ef41Sopenharmony_ci{
661cb0ef41Sopenharmony_ci  const socket = setup();
671cb0ef41Sopenharmony_ci  assert.throws(() => { socket.addMembership('256.256.256.256'); },
681cb0ef41Sopenharmony_ci                /^Error: addMembership EINVAL$/);
691cb0ef41Sopenharmony_ci  socket.close();
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci// dropMembership() with invalid multicast address should throw
731cb0ef41Sopenharmony_ci{
741cb0ef41Sopenharmony_ci  const socket = setup();
751cb0ef41Sopenharmony_ci  assert.throws(() => { socket.dropMembership('256.256.256.256'); },
761cb0ef41Sopenharmony_ci                /^Error: dropMembership EINVAL$/);
771cb0ef41Sopenharmony_ci  socket.close();
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci// addSourceSpecificMembership with invalid sourceAddress should throw
811cb0ef41Sopenharmony_ci{
821cb0ef41Sopenharmony_ci  const socket = setup();
831cb0ef41Sopenharmony_ci  assert.throws(() => {
841cb0ef41Sopenharmony_ci    socket.addSourceSpecificMembership(0, multicastAddress);
851cb0ef41Sopenharmony_ci  }, {
861cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
871cb0ef41Sopenharmony_ci    message: 'The "sourceAddress" argument must be of type string. ' +
881cb0ef41Sopenharmony_ci    'Received type number (0)'
891cb0ef41Sopenharmony_ci  });
901cb0ef41Sopenharmony_ci  socket.close();
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci// addSourceSpecificMembership with invalid sourceAddress should throw
941cb0ef41Sopenharmony_ci{
951cb0ef41Sopenharmony_ci  const socket = setup();
961cb0ef41Sopenharmony_ci  assert.throws(() => {
971cb0ef41Sopenharmony_ci    socket.addSourceSpecificMembership(multicastAddress, 0);
981cb0ef41Sopenharmony_ci  }, {
991cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1001cb0ef41Sopenharmony_ci    message: 'The "groupAddress" argument must be of type string. ' +
1011cb0ef41Sopenharmony_ci    'Received type number (0)'
1021cb0ef41Sopenharmony_ci  });
1031cb0ef41Sopenharmony_ci  socket.close();
1041cb0ef41Sopenharmony_ci}
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci// addSourceSpecificMembership with invalid groupAddress should throw
1071cb0ef41Sopenharmony_ci{
1081cb0ef41Sopenharmony_ci  const socket = setup();
1091cb0ef41Sopenharmony_ci  assert.throws(() => {
1101cb0ef41Sopenharmony_ci    socket.addSourceSpecificMembership(multicastAddress, '0');
1111cb0ef41Sopenharmony_ci  }, {
1121cb0ef41Sopenharmony_ci    code: 'EINVAL',
1131cb0ef41Sopenharmony_ci    message: 'addSourceSpecificMembership EINVAL'
1141cb0ef41Sopenharmony_ci  });
1151cb0ef41Sopenharmony_ci  socket.close();
1161cb0ef41Sopenharmony_ci}
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci// dropSourceSpecificMembership with invalid sourceAddress should throw
1191cb0ef41Sopenharmony_ci{
1201cb0ef41Sopenharmony_ci  const socket = setup();
1211cb0ef41Sopenharmony_ci  assert.throws(() => {
1221cb0ef41Sopenharmony_ci    socket.dropSourceSpecificMembership(0, multicastAddress);
1231cb0ef41Sopenharmony_ci  }, {
1241cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1251cb0ef41Sopenharmony_ci    message: 'The "sourceAddress" argument must be of type string. ' +
1261cb0ef41Sopenharmony_ci    'Received type number (0)'
1271cb0ef41Sopenharmony_ci  });
1281cb0ef41Sopenharmony_ci  socket.close();
1291cb0ef41Sopenharmony_ci}
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci// dropSourceSpecificMembership with invalid groupAddress should throw
1321cb0ef41Sopenharmony_ci{
1331cb0ef41Sopenharmony_ci  const socket = setup();
1341cb0ef41Sopenharmony_ci  assert.throws(() => {
1351cb0ef41Sopenharmony_ci    socket.dropSourceSpecificMembership(multicastAddress, 0);
1361cb0ef41Sopenharmony_ci  }, {
1371cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1381cb0ef41Sopenharmony_ci    message: 'The "groupAddress" argument must be of type string. ' +
1391cb0ef41Sopenharmony_ci    'Received type number (0)'
1401cb0ef41Sopenharmony_ci  });
1411cb0ef41Sopenharmony_ci  socket.close();
1421cb0ef41Sopenharmony_ci}
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci// dropSourceSpecificMembership with invalid UDP should throw
1451cb0ef41Sopenharmony_ci{
1461cb0ef41Sopenharmony_ci  const socket = setup();
1471cb0ef41Sopenharmony_ci  assert.throws(() => {
1481cb0ef41Sopenharmony_ci    socket.dropSourceSpecificMembership(multicastAddress, '0');
1491cb0ef41Sopenharmony_ci  }, {
1501cb0ef41Sopenharmony_ci    code: 'EINVAL',
1511cb0ef41Sopenharmony_ci    message: 'dropSourceSpecificMembership EINVAL'
1521cb0ef41Sopenharmony_ci  });
1531cb0ef41Sopenharmony_ci  socket.close();
1541cb0ef41Sopenharmony_ci}
155