-
-
Save pquerna/d5f76453282285362248 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('helper') | |
local fixture = require('./fixture-tls') | |
local tls = require('tls') | |
local timer = require('timer') | |
local Buffer = require('buffer').Buffer | |
local options = { | |
cert = fixture.certPem, | |
key = fixture.keyPem | |
} | |
local serverConnected = 0 | |
local clientConnected = 0 | |
local clientSent = 0 | |
local clientBuffer = "" | |
local serverSent = 0 | |
local serverBuffer = "" | |
local server | |
function fib(n) | |
return n<2 and n or fib(n-1)+fib(n-2) | |
end | |
function fill(buf, char) | |
local i = 1 | |
while i < buf.length do | |
buf[i] = char | |
i = i + 1 | |
end | |
end | |
function writeFibBuffer(isClient, conn, fibCount, callback) | |
timer.setTimeout(fibCount, function() | |
if (fibCount == 0) then | |
callback() | |
return | |
end | |
local buf = Buffer:new(fib(fibCount)) | |
p(buf.length) | |
fill(buf, 0xA) | |
conn:write(buf) | |
if isClient then | |
clientSent = clientSent + buf.length | |
else | |
serverSent = serverSent + buf.length | |
end | |
writeFibBuffer(isClient, conn, fibCount - 1, callback) | |
end) | |
end | |
server = tls.createServer(options, function(conn) | |
serverConnected = serverConnected + 1 | |
conn:on('data', function(chunk) | |
p('got server data callback') | |
serverBuffer = serverBuffer + chunk | |
end) | |
writeFibBuffer(false, conn, 20, function() | |
p('closing server') | |
server:close() | |
end) | |
end) | |
server:listen(fixture.commonPort, function() | |
local client | |
client = tls.connect({port = fixture.commonPort, host = '127.0.0.1'}, {}, function() | |
clientConnected = clientConnected + 1 | |
client:on('data', function(chunk) | |
p('got client data callback') | |
clientBuffer = clientBuffer + chunk | |
end) | |
writeFibBuffer(true, client, 22, function() | |
p('closing client') | |
client:destroy() | |
end) | |
end) | |
end) | |
process:on('exit', function() | |
assert(serverConnected == 1) | |
assert(clientConnected == 1) | |
p(clientBuffer) | |
p(serverSent) | |
assert(#clientBuffer == serverSent) | |
assert(#serverBuffer == clientSent) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment