1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const dgram = require('dgram');
6const multicastAddress = '224.0.0.114';
7
8const setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true });
9
10// addMembership() on closed socket should throw
11{
12  const socket = setup();
13  socket.close(common.mustCall(() => {
14    assert.throws(() => {
15      socket.addMembership(multicastAddress);
16    }, {
17      code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
18      name: 'Error',
19      message: /^Not running$/
20    });
21  }));
22}
23
24// dropMembership() on closed socket should throw
25{
26  const socket = setup();
27  socket.close(common.mustCall(() => {
28    assert.throws(() => {
29      socket.dropMembership(multicastAddress);
30    }, {
31      code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
32      name: 'Error',
33      message: /^Not running$/
34    });
35  }));
36}
37
38// addMembership() with no argument should throw
39{
40  const socket = setup();
41  assert.throws(() => {
42    socket.addMembership();
43  }, {
44    code: 'ERR_MISSING_ARGS',
45    name: 'TypeError',
46    message: /^The "multicastAddress" argument must be specified$/
47  });
48  socket.close();
49}
50
51// dropMembership() with no argument should throw
52{
53  const socket = setup();
54  assert.throws(() => {
55    socket.dropMembership();
56  }, {
57    code: 'ERR_MISSING_ARGS',
58    name: 'TypeError',
59    message: /^The "multicastAddress" argument must be specified$/
60  });
61  socket.close();
62}
63
64// addMembership() with invalid multicast address should throw
65{
66  const socket = setup();
67  assert.throws(() => { socket.addMembership('256.256.256.256'); },
68                /^Error: addMembership EINVAL$/);
69  socket.close();
70}
71
72// dropMembership() with invalid multicast address should throw
73{
74  const socket = setup();
75  assert.throws(() => { socket.dropMembership('256.256.256.256'); },
76                /^Error: dropMembership EINVAL$/);
77  socket.close();
78}
79
80// addSourceSpecificMembership with invalid sourceAddress should throw
81{
82  const socket = setup();
83  assert.throws(() => {
84    socket.addSourceSpecificMembership(0, multicastAddress);
85  }, {
86    code: 'ERR_INVALID_ARG_TYPE',
87    message: 'The "sourceAddress" argument must be of type string. ' +
88    'Received type number (0)'
89  });
90  socket.close();
91}
92
93// addSourceSpecificMembership with invalid sourceAddress should throw
94{
95  const socket = setup();
96  assert.throws(() => {
97    socket.addSourceSpecificMembership(multicastAddress, 0);
98  }, {
99    code: 'ERR_INVALID_ARG_TYPE',
100    message: 'The "groupAddress" argument must be of type string. ' +
101    'Received type number (0)'
102  });
103  socket.close();
104}
105
106// addSourceSpecificMembership with invalid groupAddress should throw
107{
108  const socket = setup();
109  assert.throws(() => {
110    socket.addSourceSpecificMembership(multicastAddress, '0');
111  }, {
112    code: 'EINVAL',
113    message: 'addSourceSpecificMembership EINVAL'
114  });
115  socket.close();
116}
117
118// dropSourceSpecificMembership with invalid sourceAddress should throw
119{
120  const socket = setup();
121  assert.throws(() => {
122    socket.dropSourceSpecificMembership(0, multicastAddress);
123  }, {
124    code: 'ERR_INVALID_ARG_TYPE',
125    message: 'The "sourceAddress" argument must be of type string. ' +
126    'Received type number (0)'
127  });
128  socket.close();
129}
130
131// dropSourceSpecificMembership with invalid groupAddress should throw
132{
133  const socket = setup();
134  assert.throws(() => {
135    socket.dropSourceSpecificMembership(multicastAddress, 0);
136  }, {
137    code: 'ERR_INVALID_ARG_TYPE',
138    message: 'The "groupAddress" argument must be of type string. ' +
139    'Received type number (0)'
140  });
141  socket.close();
142}
143
144// dropSourceSpecificMembership with invalid UDP should throw
145{
146  const socket = setup();
147  assert.throws(() => {
148    socket.dropSourceSpecificMembership(multicastAddress, '0');
149  }, {
150    code: 'EINVAL',
151    message: 'dropSourceSpecificMembership EINVAL'
152  });
153  socket.close();
154}
155