// index.js
//
// This script first creates a server for use as a test fixture for uzmtp 
// library.  When test fixture sockets are ready, we than spawn uzmtp-client
// applications with various parameters to test the library. uzmtp-client 
// connects to our test fixture. We compare output from uzmtp-client to verify
// if test passes.  When test is finished we close test fixture.
//
// (Test fixture is capitalize echo server.)

// TODO - Add more logging feedback.  Use config script for ports.

var zmq = require('zmq'),
  TlsTerminate = require('./tls_terminate.js'),
  q = require('q'),
  spawn = require('child_process').spawn;

// Trivial module to manage ZMQ socket...
function Queue() {};
Queue.prototype.listen = function(port, cb) {
  var zsock = zmq.socket("router");
  return q.ninvoke(zsock, "bind", "tcp://*:" + port).then(function() {
    if ((cb) && (typeof(cb) == 'function')) cb(zsock);
    return zsock;
  }).catch(function(e) {
    console.log(e);
  });
}

// Proxy and ZMQ instances.
var queue = new Queue();
var proxy = new TlsTerminate();


// Begin test chain...
queue.listen(23401).then(function(zmtp) {
  // Create TLS and ZMQ sockets....
  return q.fcall(function() {
    // Backend is up, set up proxy...
    var server = proxy.listen({
      key: "./server_key.pem",
      cert: "./server_cert.pem",
      tcp: 23401,
      tcps: 23400
    });

    // Handle zmq.
    zmtp.on("message", function(rid, msg) {
      zmtp.send([rid, msg.toString().toUpperCase()]);
    });
    zmtp.on("error", function(e) {
      console.log(e);
    });
    return {
      zmtp: zmtp,
      server: server
    };
  });
}).then(function(servers) {
  // Test fixture is up... Spawn our client test application.
  var deferred = q.defer();
  var client = spawn('valgrind', ['../programs/uzmtp-client', 'tcp://127.0.0.1:23400', 'this is a test']);
  client.on('close', function() {
    deferred.resolve(servers);
  });
  client.stdout.on('data', function(data) {
    var result = data.toString();
    if (result == 'THIS IS A TEST\n') {
      console.log("test passed!\n");
    } else {
      console.log("test failed!\n");
      deferred.reject(servers);
    }
  });
  client.stderr.on('data', function(data) {
    console.log(data.toString());
  });
  return deferred.promise;
}).then(function(servers) {
  // Kill our processes.
  servers.server.close();
  servers.zmtp.close();
}).done();

//
//
//

@@ -1614,16 +1614,46 @@
 : server
+,%0A      echo: %22this is a test%22
 %0A    %7D;%0A
@@ -1667,39 +1667,35 @@
 ).then(function(
-servers
+ctx
 ) %7B%0A  // Test fi
@@ -1772,16 +1772,55 @@
 efer();%0A
+  ctx.response = %22%22;%0A  ctx.error = %22%22;%0A
   var cl
@@ -1902,24 +1902,16 @@
 0', 
-'this is a test'
+ctx.echo
 %5D);%0A
@@ -1965,23 +1965,19 @@
 resolve(
-servers
+ctx
 );%0A  %7D);
@@ -2029,19 +2029,22 @@
     
-var result 
+ctx.response +
 = da
@@ -2062,173 +2062,8 @@
 ();%0A
-    if (result == 'THIS IS A TEST%5Cn') %7B%0A      console.log(%22test passed!%5Cn%22);%0A    %7D else %7B%0A      console.log(%22test failed!%5Cn%22);%0A      deferred.reject(servers);%0A    %7D%0A
   %7D)
@@ -2113,27 +2113,28 @@
  %7B%0A    c
-onsole.log(
+tx.error += 
 data.toS
@@ -2140,17 +2140,16 @@
 String()
-)
 ;%0A  %7D);%0A
@@ -2196,18 +2196,214 @@
 ion(
-servers) %7B
+ctx) %7B%0A  console.log(%0A    %22%5Cn%22 +%0A    %22===============%5Cn%22 +%0A    %22VALGRIND REPORT:%5Cn%25s%5Cn%22 +%0A    %22ECHO STRING  : %25s%5Cn%22 +%0A    %22ECHO RESPONSE: %25s%5Cn%22 +%0A    %22=============%22,%0A    ctx.error, ctx.echo, ctx.response);
 %0A  /
@@ -2426,23 +2426,19 @@
 sses.%0A  
-servers
+ctx
 .server.
@@ -2452,15 +2452,11 @@
 ;%0A  
-servers
+ctx
 .zmt

format some outputs