js_stream: fix buffer index in DoWrite
authorShigeki Ohtsu <ohtsu@iij.ad.jp>
Wed, 6 May 2015 04:45:46 +0000 (13:45 +0900)
committerFedor Indutny <fedor@indutny.com>
Wed, 6 May 2015 13:30:07 +0000 (16:30 +0300)
The index of buffer to write in JSStream was always 0 by mistake. This
fix was to use increment index of buffer arrays.
The test was originally made by Brian White in #1594.

Fix: https://github.com/iojs/io.js/issues/1595
Fix: https://github.com/iojs/io.js/pull/1594
PR-URL: https://github.com/iojs/io.js/pull/1635
Reviewed-By: Fedor Indutny <fedor@indutny.com>
src/js_stream.cc
test/parallel/test-tls-connect-stream-writes.js [new file with mode: 0644]

index 09c4f58..6b7c406 100644 (file)
@@ -89,7 +89,7 @@ int JSStream::DoWrite(WriteWrap* w,
 
   Local<Array> bufs_arr = Array::New(env()->isolate(), count);
   for (size_t i = 0; i < count; i++)
-    bufs_arr->Set(i, Buffer::New(env(), bufs[0].base, bufs[0].len));
+    bufs_arr->Set(i, Buffer::New(env(), bufs[i].base, bufs[i].len));
 
   Local<Value> argv[] = {
     w->object(),
diff --git a/test/parallel/test-tls-connect-stream-writes.js b/test/parallel/test-tls-connect-stream-writes.js
new file mode 100644 (file)
index 0000000..0bf1db1
--- /dev/null
@@ -0,0 +1,65 @@
+var assert = require('assert'),
+    fs = require('fs'),
+    path = require('path'),
+    tls = require('tls'),
+    stream = require('stream'),
+    net = require('net');
+
+var common = require('../common');
+
+var server;
+var cert_dir = path.resolve(__dirname, '../fixtures'),
+    options = { key: fs.readFileSync(cert_dir + '/test_key.pem'),
+                cert: fs.readFileSync(cert_dir + '/test_cert.pem'),
+                ca: [ fs.readFileSync(cert_dir + '/test_ca.pem') ],
+                ciphers: 'AES256-GCM-SHA384' };
+var content = 'hello world';
+var recv_bufs = [];
+var send_data = '';
+server = tls.createServer(options, function(s) {
+  s.on('data', function(c) {
+    recv_bufs.push(c);
+  });
+});
+server.listen(common.PORT, function() {
+  var raw = net.connect(common.PORT);
+
+  var pending = false;
+  raw.on('readable', function() {
+    if (pending)
+      p._read();
+  });
+
+  var p = new stream.Duplex({
+    read: function read() {
+      pending = false;
+
+      var chunk = raw.read();
+      if (chunk) {
+        this.push(chunk);
+      } else {
+        pending = true;
+      }
+    },
+    write: function write(data, enc, cb) {
+      raw.write(data, enc, cb);
+    }
+  });
+
+  var socket = tls.connect({
+    socket: p,
+    rejectUnauthorized: false
+  }, function() {
+    for (var i = 0; i < 50; ++i) {
+      socket.write(content);
+      send_data += content;
+    }
+    socket.end();
+    server.close();
+  });
+});
+
+process.on('exit', function() {
+  var recv_data = (Buffer.concat(recv_bufs)).toString();
+  assert.strictEqual(send_data, recv_data);
+});