Skip to content

Commit 78520fa

Browse files
committed
test,dgram: add tests for setBroadcast()
The only tests for `setBroadcast()` (from the `dgram` module) were in `test/internet` which means they almost never get run. This adds a minimal test that can check JS-land functionality in `test/parallel`. I also expanded a comment and did some minor formatting on the existing `test/internet` test. If there were an easy and reliable way to check for the BROADCAST flag on an interface, it's possible that a version of the test could be moved to `test/sequential` or `test/parallel` once it was modified to only use internal networks. PR-URL: #6750 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5f31b7e commit 78520fa

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

test/internet/test-dgram-broadcast-multi-process.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ if (common.inFreeBSDJail) {
2020
return;
2121
}
2222

23-
// take the first non-internal interface as the address for binding
23+
// Take the first non-internal interface as the address for binding.
24+
// Ideally, this should check for whether or not an interface is set up for
25+
// BROADCAST and favor internal/private interfaces.
2426
get_bindAddress: for (var name in networkInterfaces) {
2527
var interfaces = networkInterfaces[name];
2628
for (var i = 0; i < interfaces.length; i++) {
@@ -209,7 +211,7 @@ if (process.argv[2] === 'child') {
209211

210212
receivedMessages.push(buf);
211213

212-
process.send({ message: buf.toString() });
214+
process.send({message: buf.toString()});
213215

214216
if (receivedMessages.length == messages.length) {
215217
process.nextTick(function() {
@@ -228,7 +230,7 @@ if (process.argv[2] === 'child') {
228230
});
229231

230232
listenSocket.on('listening', function() {
231-
process.send({ listening: true });
233+
process.send({listening: true});
232234
});
233235

234236
listenSocket.bind(common.PORT);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const dgram = require('dgram');
6+
7+
const setup = () => {
8+
return dgram.createSocket({type: 'udp4', reuseAddr: true});
9+
};
10+
11+
const teardown = (socket) => {
12+
if (socket.close)
13+
socket.close();
14+
};
15+
16+
const runTest = (testCode, expectError) => {
17+
const socket = setup();
18+
const assertion = expectError ? assert.throws : assert.doesNotThrow;
19+
const wrapped = () => { testCode(socket); };
20+
assertion(wrapped, expectError);
21+
teardown(socket);
22+
};
23+
24+
// Should throw EBADF if socket is never bound.
25+
runTest((socket) => { socket.setBroadcast(true); }, /EBADF/);
26+
27+
// Should not throw if broadcast set to false after binding.
28+
runTest((socket) => {
29+
socket.bind(common.PORT, common.localhostIPv4, () => {
30+
socket.setBroadcast(false);
31+
});
32+
});
33+
34+
// Should not throw if broadcast set to true after binding.
35+
runTest((socket) => {
36+
socket.bind(common.PORT, common.localhostIPv4, () => {
37+
socket.setBroadcast(true);
38+
});
39+
});

0 commit comments

Comments
 (0)