11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// This is similar to simple/test-socket-write-after-fin, except that
61cb0ef41Sopenharmony_ci// we don't set allowHalfOpen.  Then we write after the client has sent
71cb0ef41Sopenharmony_ci// a FIN, and this is an error.  However, the standard "write after end"
81cb0ef41Sopenharmony_ci// message is too vague, and doesn't actually tell you what happens.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst net = require('net');
111cb0ef41Sopenharmony_cilet serverData = '';
121cb0ef41Sopenharmony_cilet gotServerEnd = false;
131cb0ef41Sopenharmony_cilet clientData = '';
141cb0ef41Sopenharmony_cilet gotClientEnd = false;
151cb0ef41Sopenharmony_cilet gotServerError = false;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst server = net.createServer(function(sock) {
181cb0ef41Sopenharmony_ci  sock.setEncoding('utf8');
191cb0ef41Sopenharmony_ci  sock.on('error', function() {});
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  sock.on('data', function(c) {
221cb0ef41Sopenharmony_ci    serverData += c;
231cb0ef41Sopenharmony_ci  });
241cb0ef41Sopenharmony_ci  sock.on('end', function() {
251cb0ef41Sopenharmony_ci    gotServerEnd = true;
261cb0ef41Sopenharmony_ci    setImmediate(() => {
271cb0ef41Sopenharmony_ci      sock.write(serverData, function(er) {
281cb0ef41Sopenharmony_ci        console.error(`${er.code}: ${er.message}`);
291cb0ef41Sopenharmony_ci        gotServerError = er;
301cb0ef41Sopenharmony_ci      });
311cb0ef41Sopenharmony_ci      sock.end();
321cb0ef41Sopenharmony_ci    });
331cb0ef41Sopenharmony_ci  });
341cb0ef41Sopenharmony_ci  server.close();
351cb0ef41Sopenharmony_ci});
361cb0ef41Sopenharmony_ciserver.listen(0, function() {
371cb0ef41Sopenharmony_ci  const sock = net.connect(this.address().port);
381cb0ef41Sopenharmony_ci  sock.setEncoding('utf8');
391cb0ef41Sopenharmony_ci  sock.on('data', function(c) {
401cb0ef41Sopenharmony_ci    clientData += c;
411cb0ef41Sopenharmony_ci  });
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  sock.on('end', function() {
441cb0ef41Sopenharmony_ci    gotClientEnd = true;
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  process.on('exit', function() {
481cb0ef41Sopenharmony_ci    assert.strictEqual(clientData, '');
491cb0ef41Sopenharmony_ci    assert.strictEqual(serverData, 'hello1hello2hello3\nTHUNDERMUSCLE!');
501cb0ef41Sopenharmony_ci    assert(gotClientEnd);
511cb0ef41Sopenharmony_ci    assert(gotServerEnd);
521cb0ef41Sopenharmony_ci    assert(gotServerError);
531cb0ef41Sopenharmony_ci    assert.strictEqual(gotServerError.code, 'EPIPE');
541cb0ef41Sopenharmony_ci    assert.notStrictEqual(gotServerError.message, 'write after end');
551cb0ef41Sopenharmony_ci    console.log('ok');
561cb0ef41Sopenharmony_ci  });
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  sock.write('hello1');
591cb0ef41Sopenharmony_ci  sock.write('hello2');
601cb0ef41Sopenharmony_ci  sock.write('hello3\n');
611cb0ef41Sopenharmony_ci  sock.end('THUNDERMUSCLE!');
621cb0ef41Sopenharmony_ci});
63