summaryrefslogtreecommitdiff
path: root/test/parallel/test-dgram-setBroadcast.js
blob: 1232faa76c81fe6cb27a0cbda9b5a84c65f07221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'use strict';

const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');

const setup = () => {
  return dgram.createSocket({type: 'udp4', reuseAddr: true});
};

const teardown = (socket) => {
  if (socket.close)
    socket.close();
};

const runTest = (testCode, expectError) => {
  const socket = setup();
  const assertion = expectError ? assert.throws : assert.doesNotThrow;
  const wrapped = () => { testCode(socket); };
  assertion(wrapped, expectError);
  teardown(socket);
};

// Should throw EBADF if socket is never bound.
runTest((socket) => { socket.setBroadcast(true); }, /EBADF/);

// Should not throw if broadcast set to false after binding.
runTest((socket) => {
  socket.bind(0, common.localhostIPv4, () => {
    socket.setBroadcast(false);
  });
});

// Should not throw if broadcast set to true after binding.
runTest((socket) => {
  socket.bind(0, common.localhostIPv4, () => {
    socket.setBroadcast(true);
  });
});