1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const dgram = require('dgram'); 5 6{ 7 const socket = dgram.createSocket('udp4'); 8 9 socket.bind(0); 10 socket.on('listening', common.mustCall(() => { 11 // Explicitly request default system selection 12 socket.setMulticastInterface('0.0.0.0'); 13 14 socket.close(); 15 })); 16} 17 18{ 19 const socket = dgram.createSocket('udp4'); 20 21 socket.bind(0); 22 socket.on('listening', common.mustCall(() => { 23 socket.close(common.mustCall(() => { 24 assert.throws(() => { socket.setMulticastInterface('0.0.0.0'); }, 25 /Not running/); 26 })); 27 })); 28} 29 30{ 31 const socket = dgram.createSocket('udp4'); 32 33 socket.bind(0); 34 socket.on('listening', common.mustCall(() => { 35 // Try to set with an invalid interfaceAddress (wrong address class) 36 // 37 // This operation succeeds on some platforms, throws `EINVAL` on some 38 // platforms, and throws `ENOPROTOOPT` on others. This is unpleasant, but 39 // we should at least test for it. 40 try { 41 socket.setMulticastInterface('::'); 42 } catch (e) { 43 assert(e.code === 'EINVAL' || e.code === 'ENOPROTOOPT'); 44 } 45 46 socket.close(); 47 })); 48} 49 50{ 51 const socket = dgram.createSocket('udp4'); 52 53 socket.bind(0); 54 socket.on('listening', common.mustCall(() => { 55 // Try to set with an invalid interfaceAddress (wrong Type) 56 assert.throws(() => { 57 socket.setMulticastInterface(1); 58 }, /TypeError/); 59 60 socket.close(); 61 })); 62} 63 64{ 65 const socket = dgram.createSocket('udp4'); 66 67 socket.bind(0); 68 socket.on('listening', common.mustCall(() => { 69 // Try to set with an invalid interfaceAddress (non-unicast) 70 assert.throws(() => { 71 socket.setMulticastInterface('224.0.0.2'); 72 }, /Error/); 73 74 socket.close(); 75 })); 76} 77 78// If IPv6 is not supported, skip the rest of the test. However, don't call 79// common.skip(), which calls process.exit() while there is outstanding 80// common.mustCall() activity. 81if (!common.hasIPv6) 82 return; 83 84{ 85 const socket = dgram.createSocket('udp6'); 86 87 socket.bind(0); 88 socket.on('listening', common.mustCall(() => { 89 // Try to set with an invalid interfaceAddress ('undefined') 90 assert.throws(() => { 91 socket.setMulticastInterface(String(undefined)); 92 }, /EINVAL/); 93 94 socket.close(); 95 })); 96} 97 98{ 99 const socket = dgram.createSocket('udp6'); 100 101 socket.bind(0); 102 socket.on('listening', common.mustCall(() => { 103 // Try to set with an invalid interfaceAddress ('') 104 assert.throws(() => { 105 socket.setMulticastInterface(''); 106 }, /EINVAL/); 107 108 socket.close(); 109 })); 110} 111 112{ 113 const socket = dgram.createSocket('udp6'); 114 115 socket.bind(0); 116 socket.on('listening', common.mustCall(() => { 117 // Using lo0 for OsX, on all other OSes, an invalid Scope gets 118 // turned into #0 (default selection) which is also acceptable. 119 socket.setMulticastInterface('::%lo0'); 120 121 socket.close(); 122 })); 123} 124