11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst net = require('net');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// With only a callback, server should get a port assigned by the OS
81cb0ef41Sopenharmony_ci{
91cb0ef41Sopenharmony_ci  const server = net.createServer(common.mustNotCall());
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci  server.listen(common.mustCall(function() {
121cb0ef41Sopenharmony_ci    assert.ok(server.address().port > 100);
131cb0ef41Sopenharmony_ci    server.close();
141cb0ef41Sopenharmony_ci  }));
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci// No callback to listen(), assume we can bind in 100 ms
181cb0ef41Sopenharmony_ci{
191cb0ef41Sopenharmony_ci  const server = net.createServer(common.mustNotCall());
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  server.listen(common.PORT);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  setTimeout(function() {
241cb0ef41Sopenharmony_ci    const address = server.address();
251cb0ef41Sopenharmony_ci    assert.strictEqual(address.port, common.PORT);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    if (address.family === 'IPv6')
281cb0ef41Sopenharmony_ci      assert.strictEqual(server._connectionKey, `6::::${address.port}`);
291cb0ef41Sopenharmony_ci    else
301cb0ef41Sopenharmony_ci      assert.strictEqual(server._connectionKey, `4:0.0.0.0:${address.port}`);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    server.close();
331cb0ef41Sopenharmony_ci  }, 100);
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci// Callback to listen()
371cb0ef41Sopenharmony_ci{
381cb0ef41Sopenharmony_ci  const server = net.createServer(common.mustNotCall());
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  server.listen(common.PORT + 1, common.mustCall(function() {
411cb0ef41Sopenharmony_ci    assert.strictEqual(server.address().port, common.PORT + 1);
421cb0ef41Sopenharmony_ci    server.close();
431cb0ef41Sopenharmony_ci  }));
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci// Backlog argument
471cb0ef41Sopenharmony_ci{
481cb0ef41Sopenharmony_ci  const server = net.createServer(common.mustNotCall());
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  server.listen(common.PORT + 2, '0.0.0.0', 127, common.mustCall(function() {
511cb0ef41Sopenharmony_ci    assert.strictEqual(server.address().port, common.PORT + 2);
521cb0ef41Sopenharmony_ci    server.close();
531cb0ef41Sopenharmony_ci  }));
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci// Backlog argument without host argument
571cb0ef41Sopenharmony_ci{
581cb0ef41Sopenharmony_ci  const server = net.createServer(common.mustNotCall());
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  server.listen(common.PORT + 3, 127, common.mustCall(function() {
611cb0ef41Sopenharmony_ci    assert.strictEqual(server.address().port, common.PORT + 3);
621cb0ef41Sopenharmony_ci    server.close();
631cb0ef41Sopenharmony_ci  }));
641cb0ef41Sopenharmony_ci}
65