11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst dgram = require('dgram');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst PORT = 12345;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp4');
101cb0ef41Sopenharmony_ciclient.connect(PORT, common.mustCall(() => {
111cb0ef41Sopenharmony_ci  const remoteAddr = client.remoteAddress();
121cb0ef41Sopenharmony_ci  assert.strictEqual(remoteAddr.port, PORT);
131cb0ef41Sopenharmony_ci  assert.throws(() => {
141cb0ef41Sopenharmony_ci    client.connect(PORT, common.mustNotCall());
151cb0ef41Sopenharmony_ci  }, {
161cb0ef41Sopenharmony_ci    name: 'Error',
171cb0ef41Sopenharmony_ci    message: 'Already connected',
181cb0ef41Sopenharmony_ci    code: 'ERR_SOCKET_DGRAM_IS_CONNECTED'
191cb0ef41Sopenharmony_ci  });
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  client.disconnect();
221cb0ef41Sopenharmony_ci  assert.throws(() => {
231cb0ef41Sopenharmony_ci    client.disconnect();
241cb0ef41Sopenharmony_ci  }, {
251cb0ef41Sopenharmony_ci    name: 'Error',
261cb0ef41Sopenharmony_ci    message: 'Not connected',
271cb0ef41Sopenharmony_ci    code: 'ERR_SOCKET_DGRAM_NOT_CONNECTED'
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  assert.throws(() => {
311cb0ef41Sopenharmony_ci    client.remoteAddress();
321cb0ef41Sopenharmony_ci  }, {
331cb0ef41Sopenharmony_ci    name: 'Error',
341cb0ef41Sopenharmony_ci    message: 'Not connected',
351cb0ef41Sopenharmony_ci    code: 'ERR_SOCKET_DGRAM_NOT_CONNECTED'
361cb0ef41Sopenharmony_ci  });
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  client.once('connect', common.mustCall(() => client.close()));
391cb0ef41Sopenharmony_ci  client.connect(PORT);
401cb0ef41Sopenharmony_ci}));
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciassert.throws(() => {
431cb0ef41Sopenharmony_ci  client.connect(PORT);
441cb0ef41Sopenharmony_ci}, {
451cb0ef41Sopenharmony_ci  name: 'Error',
461cb0ef41Sopenharmony_ci  message: 'Already connected',
471cb0ef41Sopenharmony_ci  code: 'ERR_SOCKET_DGRAM_IS_CONNECTED'
481cb0ef41Sopenharmony_ci});
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciassert.throws(() => {
511cb0ef41Sopenharmony_ci  client.disconnect();
521cb0ef41Sopenharmony_ci}, {
531cb0ef41Sopenharmony_ci  name: 'Error',
541cb0ef41Sopenharmony_ci  message: 'Not connected',
551cb0ef41Sopenharmony_ci  code: 'ERR_SOCKET_DGRAM_NOT_CONNECTED'
561cb0ef41Sopenharmony_ci});
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci[ 0, null, 78960, undefined ].forEach((port) => {
591cb0ef41Sopenharmony_ci  assert.throws(() => {
601cb0ef41Sopenharmony_ci    client.connect(port);
611cb0ef41Sopenharmony_ci  }, {
621cb0ef41Sopenharmony_ci    name: 'RangeError',
631cb0ef41Sopenharmony_ci    message: /^Port should be > 0 and < 65536/,
641cb0ef41Sopenharmony_ci    code: 'ERR_SOCKET_BAD_PORT'
651cb0ef41Sopenharmony_ci  });
661cb0ef41Sopenharmony_ci});
67