• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25const net = require('net');
26
27const tcp = net.Server(common.mustCall((s) => {
28  tcp.close();
29
30  let buf = '';
31  s.setEncoding('utf8');
32  s.on('data', function(d) {
33    buf += d;
34  });
35
36  s.on('end', common.mustCall(function() {
37    console.error('SERVER: end', buf);
38    assert.strictEqual(buf, "L'État, c'est moi");
39    s.end();
40  }));
41}));
42
43tcp.listen(0, common.mustCall(function() {
44  const socket = net.Stream({ highWaterMark: 0 });
45
46  let connected = false;
47  assert.strictEqual(socket.pending, true);
48  socket.connect(this.address().port, common.mustCall(() => connected = true));
49
50  assert.strictEqual(socket.pending, true);
51  assert.strictEqual(socket.connecting, true);
52  assert.strictEqual(socket.readyState, 'opening');
53
54  // Make sure that anything besides a buffer or a string throws.
55  assert.throws(() => socket.write(null),
56                {
57                  code: 'ERR_STREAM_NULL_VALUES',
58                  name: 'TypeError',
59                  message: 'May not write null values to stream'
60                });
61  [
62    true,
63    false,
64    1,
65    1.0,
66    +Infinity,
67    -Infinity
68  ].forEach((value) => {
69    assert.throws(() => socket.write(value), {
70      code: 'ERR_INVALID_ARG_TYPE',
71      name: 'TypeError',
72      message: 'The "chunk" argument must be of type string or an instance ' +
73               `of Buffer. Received type ${typeof value} (${value})`
74    });
75  });
76
77  // Write a string that contains a multi-byte character sequence to test that
78  // `bytesWritten` is incremented with the # of bytes, not # of characters.
79  const a = "L'État, c'est ";
80  const b = 'moi';
81
82  // We're still connecting at this point so the datagram is first pushed onto
83  // the connect queue. Make sure that it's not added to `bytesWritten` again
84  // when the actual write happens.
85  const r = socket.write(a, common.mustCall((er) => {
86    console.error('write cb');
87    assert.ok(connected);
88    assert.strictEqual(socket.bytesWritten, Buffer.from(a + b).length);
89    assert.strictEqual(socket.pending, false);
90  }));
91  socket.on('close', common.mustCall(() => {
92    assert.strictEqual(socket.pending, true);
93  }));
94
95  assert.strictEqual(socket.bytesWritten, Buffer.from(a).length);
96  assert.strictEqual(r, false);
97  socket.end(b);
98
99  assert.strictEqual(socket.readyState, 'opening');
100}));
101