Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dgram,test: add tests for setBroadcast() #6750

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/api/dgram.md
Expand Up @@ -283,6 +283,9 @@ not work because the packet will get silently dropped without informing the
source that the data did not reach its intended recipient.

### socket.setBroadcast(flag)
<!-- YAML
added: v0.6.9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly sure it goes all the way back to v0.2.0. It was certainly in v0.4.x because I wrote the libuv replacement during the v0.5.x development cycle.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis I got v0.6.9 from f749338.

In v0.6.8, the function existed, but this was the implementation:

Socket.prototype.setBroadcast = function(arg) {
  throw new Error('not yet implemented');
};

My interpretation was that the function existed in earlier releases, but went away for a while, and then came back in 0.6.9. Am I mistaken? Was that function never really run and there was a version in dgram_legacy.js or something that was actually the implementation for 0.6.8?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I suppose it's possible I wrote the libuv code but forgot to hook it up in node... let's pretend this conversation never happened.

-->

* `flag` {Boolean}

Expand Down
8 changes: 5 additions & 3 deletions test/internet/test-dgram-broadcast-multi-process.js
Expand Up @@ -20,7 +20,9 @@ if (common.inFreeBSDJail) {
return;
}

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

receivedMessages.push(buf);

process.send({ message: buf.toString() });
process.send({message: buf.toString()});

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

listenSocket.on('listening', function() {
process.send({ listening: true });
process.send({listening: true});
});

listenSocket.bind(common.PORT);
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-dgram-setBroadcast.js
@@ -0,0 +1,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(common.PORT, common.localhostIPv4, () => {
socket.setBroadcast(false);
});
});

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