node/test/parallel/test-dgram-blocklist.js
Sohyeon Kim dccc0f2971
test: refactor error checks to use assert.ifError/mustSucceed
Replace manual `if (err) assert.fail(err)` and `assert.ok(!err)` with
`assert.ifError()` or `common.mustSucceed()` in a few tests to clarify
intent and follow project conventions.

- test/parallel/test-child-process-send-returns-boolean.js
- test/parallel/test-dgram-blocklist.js
- test/parallel/test-fs-watchfile.js

PR-URL: https://github.com/nodejs/node/pull/59424
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2025-08-15 05:40:01 +00:00

53 lines
1.6 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const net = require('net');
{
const blockList = new net.BlockList();
blockList.addAddress(common.localhostIPv4);
const connectSocket = dgram.createSocket({ type: 'udp4', sendBlockList: blockList });
connectSocket.connect(9999, common.localhostIPv4, common.mustCall((err) => {
assert.ok(err.code === 'ERR_IP_BLOCKED', err);
connectSocket.close();
}));
}
{
const blockList = new net.BlockList();
blockList.addAddress(common.localhostIPv4);
const sendSocket = dgram.createSocket({ type: 'udp4', sendBlockList: blockList });
sendSocket.send('hello', 9999, common.localhostIPv4, common.mustCall((err) => {
assert.ok(err.code === 'ERR_IP_BLOCKED', err);
sendSocket.close();
}));
}
{
const blockList = new net.BlockList();
blockList.addAddress(common.localhostIPv4);
const receiveSocket = dgram.createSocket({ type: 'udp4', receiveBlockList: blockList });
// Hack to close the socket
const check = blockList.check;
blockList.check = function() {
process.nextTick(() => {
receiveSocket.close();
});
return check.apply(this, arguments);
};
receiveSocket.on('message', common.mustNotCall());
receiveSocket.bind(0, common.localhostIPv4, common.mustCall(() => {
const addressInfo = receiveSocket.address();
const client = dgram.createSocket('udp4');
client.send(
'hello',
addressInfo.port,
addressInfo.address,
common.mustSucceed(() => {
client.close();
})
);
}));
}