Skip to content

Commit

Permalink
fix(common): servers#listen() port number validation
Browse files Browse the repository at this point in the history
Fixes hyperledger#383

Ensures that the when specifying port 0 the liste()
method does not throw an exception since it is
perfectly valid to bind to port zero in general.

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed Dec 15, 2020
1 parent 4d93c72 commit ee28b50
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/cactus-common/src/main/typescript/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export class Servers {

Checks.truthy(options, `${fnTag} arg options`);
Checks.truthy(options.server, `${fnTag} arg options.server`);
Checks.truthy(options.port, `${fnTag} arg options.port`);
Checks.truthy(
options.port || options.port === 0,
`${fnTag} arg options.port`
);
Checks.truthy(options.hostname, `${fnTag} arg options.hostname`);
const { server, port, hostname } = options;

Expand Down
41 changes: 41 additions & 0 deletions packages/cactus-common/src/test/typescript/unit/servers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { createServer } from "http";

import test, { Test } from "tape-promise/tape";

import { Servers } from "../../../main/typescript/index";

test("Servers", async (tParent: Test) => {
test("Servers#listen()", async (t: Test) => {
{
const server = createServer();
await t.rejects(
Servers.listen({ hostname: "x", port: "" as any, server }),
/options\.port/,
"Rejects when port specified as empty string OK"
);
}

{
const server = createServer();
await t.rejects(
Servers.listen({ hostname: "localhost", port: false as any, server }),
/options\.port/,
"Rejects when port specified as literal false boolean OK"
);
// await Servers.shutdown(server);
}

{
const server = createServer();
await t.doesNotReject(
Servers.listen({ hostname: "localhost", port: 0, server }),
"Does not rejects when port specified as zero OK"
);
await Servers.shutdown(server);
}

t.end();
});

tParent.end();
});

0 comments on commit ee28b50

Please sign in to comment.