11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst dgram = require('dgram');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci{
71cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci  socket.bind(0);
101cb0ef41Sopenharmony_ci  socket.on('listening', common.mustCall(() => {
111cb0ef41Sopenharmony_ci    // Explicitly request default system selection
121cb0ef41Sopenharmony_ci    socket.setMulticastInterface('0.0.0.0');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci    socket.close();
151cb0ef41Sopenharmony_ci  }));
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci{
191cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  socket.bind(0);
221cb0ef41Sopenharmony_ci  socket.on('listening', common.mustCall(() => {
231cb0ef41Sopenharmony_ci    socket.close(common.mustCall(() => {
241cb0ef41Sopenharmony_ci      assert.throws(() => { socket.setMulticastInterface('0.0.0.0'); },
251cb0ef41Sopenharmony_ci                    /Not running/);
261cb0ef41Sopenharmony_ci    }));
271cb0ef41Sopenharmony_ci  }));
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci{
311cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  socket.bind(0);
341cb0ef41Sopenharmony_ci  socket.on('listening', common.mustCall(() => {
351cb0ef41Sopenharmony_ci    // Try to set with an invalid interfaceAddress (wrong address class)
361cb0ef41Sopenharmony_ci    //
371cb0ef41Sopenharmony_ci    // This operation succeeds on some platforms, throws `EINVAL` on some
381cb0ef41Sopenharmony_ci    // platforms, and throws `ENOPROTOOPT` on others. This is unpleasant, but
391cb0ef41Sopenharmony_ci    // we should at least test for it.
401cb0ef41Sopenharmony_ci    try {
411cb0ef41Sopenharmony_ci      socket.setMulticastInterface('::');
421cb0ef41Sopenharmony_ci    } catch (e) {
431cb0ef41Sopenharmony_ci      assert(e.code === 'EINVAL' || e.code === 'ENOPROTOOPT');
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    socket.close();
471cb0ef41Sopenharmony_ci  }));
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci{
511cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  socket.bind(0);
541cb0ef41Sopenharmony_ci  socket.on('listening', common.mustCall(() => {
551cb0ef41Sopenharmony_ci    // Try to set with an invalid interfaceAddress (wrong Type)
561cb0ef41Sopenharmony_ci    assert.throws(() => {
571cb0ef41Sopenharmony_ci      socket.setMulticastInterface(1);
581cb0ef41Sopenharmony_ci    }, /TypeError/);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci    socket.close();
611cb0ef41Sopenharmony_ci  }));
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci{
651cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  socket.bind(0);
681cb0ef41Sopenharmony_ci  socket.on('listening', common.mustCall(() => {
691cb0ef41Sopenharmony_ci    // Try to set with an invalid interfaceAddress (non-unicast)
701cb0ef41Sopenharmony_ci    assert.throws(() => {
711cb0ef41Sopenharmony_ci      socket.setMulticastInterface('224.0.0.2');
721cb0ef41Sopenharmony_ci    }, /Error/);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    socket.close();
751cb0ef41Sopenharmony_ci  }));
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci// If IPv6 is not supported, skip the rest of the test. However, don't call
791cb0ef41Sopenharmony_ci// common.skip(), which calls process.exit() while there is outstanding
801cb0ef41Sopenharmony_ci// common.mustCall() activity.
811cb0ef41Sopenharmony_ciif (!common.hasIPv6)
821cb0ef41Sopenharmony_ci  return;
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci{
851cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp6');
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  socket.bind(0);
881cb0ef41Sopenharmony_ci  socket.on('listening', common.mustCall(() => {
891cb0ef41Sopenharmony_ci    // Try to set with an invalid interfaceAddress ('undefined')
901cb0ef41Sopenharmony_ci    assert.throws(() => {
911cb0ef41Sopenharmony_ci      socket.setMulticastInterface(String(undefined));
921cb0ef41Sopenharmony_ci    }, /EINVAL/);
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci    socket.close();
951cb0ef41Sopenharmony_ci  }));
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci{
991cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp6');
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  socket.bind(0);
1021cb0ef41Sopenharmony_ci  socket.on('listening', common.mustCall(() => {
1031cb0ef41Sopenharmony_ci    // Try to set with an invalid interfaceAddress ('')
1041cb0ef41Sopenharmony_ci    assert.throws(() => {
1051cb0ef41Sopenharmony_ci      socket.setMulticastInterface('');
1061cb0ef41Sopenharmony_ci    }, /EINVAL/);
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci    socket.close();
1091cb0ef41Sopenharmony_ci  }));
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci{
1131cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp6');
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  socket.bind(0);
1161cb0ef41Sopenharmony_ci  socket.on('listening', common.mustCall(() => {
1171cb0ef41Sopenharmony_ci    // Using lo0 for OsX, on all other OSes, an invalid Scope gets
1181cb0ef41Sopenharmony_ci    // turned into #0 (default selection) which is also acceptable.
1191cb0ef41Sopenharmony_ci    socket.setMulticastInterface('::%lo0');
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci    socket.close();
1221cb0ef41Sopenharmony_ci  }));
1231cb0ef41Sopenharmony_ci}
124