11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasIPv6)
51cb0ef41Sopenharmony_ci  common.skip('no IPv6 support');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst dgram = require('dgram');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp6');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst toSend = [Buffer.alloc(256, 'x'),
131cb0ef41Sopenharmony_ci                Buffer.alloc(256, 'y'),
141cb0ef41Sopenharmony_ci                Buffer.alloc(256, 'z'),
151cb0ef41Sopenharmony_ci                'hello'];
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst received = [];
181cb0ef41Sopenharmony_cilet totalBytesSent = 0;
191cb0ef41Sopenharmony_cilet totalBytesReceived = 0;
201cb0ef41Sopenharmony_ciconst arrayBufferViewLength = common.getArrayBufferViews(
211cb0ef41Sopenharmony_ci  Buffer.from('')
221cb0ef41Sopenharmony_ci).length;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciclient.on('listening', common.mustCall(() => {
251cb0ef41Sopenharmony_ci  const port = client.address().port;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  client.send(toSend[0], 0, toSend[0].length, port);
281cb0ef41Sopenharmony_ci  client.send(toSend[1], port);
291cb0ef41Sopenharmony_ci  client.send([toSend[2]], port);
301cb0ef41Sopenharmony_ci  client.send(toSend[3], 0, toSend[3].length, port);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  totalBytesSent += toSend.map((buf) => buf.length)
331cb0ef41Sopenharmony_ci                      .reduce((a, b) => a + b, 0);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  for (const msgBuf of common.getArrayBufferViews(toSend[0])) {
361cb0ef41Sopenharmony_ci    client.send(msgBuf, 0, msgBuf.byteLength, port);
371cb0ef41Sopenharmony_ci    totalBytesSent += msgBuf.byteLength;
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci  for (const msgBuf of common.getArrayBufferViews(toSend[1])) {
401cb0ef41Sopenharmony_ci    client.send(msgBuf, port);
411cb0ef41Sopenharmony_ci    totalBytesSent += msgBuf.byteLength;
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci  for (const msgBuf of common.getArrayBufferViews(toSend[2])) {
441cb0ef41Sopenharmony_ci    client.send([msgBuf], port);
451cb0ef41Sopenharmony_ci    totalBytesSent += msgBuf.byteLength;
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci}));
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciclient.on('message', common.mustCall((buf, info) => {
501cb0ef41Sopenharmony_ci  received.push(buf.toString());
511cb0ef41Sopenharmony_ci  totalBytesReceived += info.size;
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  if (totalBytesReceived === totalBytesSent) {
541cb0ef41Sopenharmony_ci    client.close();
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci  // For every buffer in `toSend`, we send the raw Buffer,
571cb0ef41Sopenharmony_ci  // as well as every TypedArray in getArrayBufferViews()
581cb0ef41Sopenharmony_ci}, toSend.length + (toSend.length - 1) * arrayBufferViewLength));
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ciclient.on('close', common.mustCall((buf, info) => {
611cb0ef41Sopenharmony_ci  // The replies may arrive out of order -> sort them before checking.
621cb0ef41Sopenharmony_ci  received.sort();
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  const repeated = [...toSend];
651cb0ef41Sopenharmony_ci  for (let i = 0; i < arrayBufferViewLength; i++) {
661cb0ef41Sopenharmony_ci    // We get arrayBufferViews only for toSend[0..2].
671cb0ef41Sopenharmony_ci    repeated.push(...toSend.slice(0, 3));
681cb0ef41Sopenharmony_ci  }
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  assert.strictEqual(totalBytesSent, totalBytesReceived);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  const expected = repeated.map(String).sort();
731cb0ef41Sopenharmony_ci  assert.deepStrictEqual(received, expected);
741cb0ef41Sopenharmony_ci}));
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ciclient.bind(0);
77