THRIFT-2347 C# TLS Transport based on THRIFT-181
Client: C#
Patch: Beat Käslin

This closes #104

commit 21c33abd59a2333c48722933c6894d8ed145e638
 Author: Beat Kaeslin <beat.kaeslin@siemens.com>
 Date: 2014-04-16T14:07:58Z

Add TLS transport for C#

commit 60a0baa1797b0ef0ea6f8c21e5b81a78cdfcdf16
 Author: Beat Kaeslin <beat.kaeslin@siemens.com>
 Date: 2014-04-17T06:23:57Z

csharp tests moved to the end
diff --git a/lib/csharp/test/ThriftTest/TestClient.cs b/lib/csharp/test/ThriftTest/TestClient.cs
index ba2d4d0..593169f 100644
--- a/lib/csharp/test/ThriftTest/TestClient.cs
+++ b/lib/csharp/test/ThriftTest/TestClient.cs
@@ -30,6 +30,7 @@
 	public class TestClient
 	{
 		private static int numIterations = 1;
+		private static string protocol = "";
 
 		public static void Execute(string[] args)
 		{
@@ -39,7 +40,7 @@
 				int port = 9090;
 				string url = null, pipe = null;
 				int numThreads = 1;
-				bool buffered = false, framed = false;
+				bool buffered = false, framed = false, encrypted = false;
 
 				try
 				{
@@ -81,6 +82,21 @@
 						{
 							numThreads = Convert.ToInt32(args[++i]);
 						}
+						else if (args[i] == "-ssl")
+						{
+							encrypted = true;
+							Console.WriteLine("Using encrypted transport");
+						}
+						else if (args[i] == "-compact")
+						{
+							protocol = "compact";
+							Console.WriteLine("Using compact protocol");
+						}
+						else if (args[i] == "-json")
+						{
+							protocol = "json";
+							Console.WriteLine("Using JSON protocol");
+						}
 					}
 				}
 				catch (Exception e)
@@ -88,8 +104,6 @@
 					Console.WriteLine(e.StackTrace);
 				}
 
-
-
 				//issue tests on separate threads simultaneously
 				Thread[] threads = new Thread[numThreads];
 				DateTime start = DateTime.Now;
@@ -101,17 +115,22 @@
 					{
 						// endpoint transport
 						TTransport trans = null;
-						if( pipe != null)
+						if (pipe != null)
 							trans = new TNamedPipeClientTransport(pipe);
 						else
-							trans = new TSocket(host, port);
-						
+						{
+							if (encrypted)
+								trans = new TTLSSocket(host, port, "../../../../../keys/client.pem");
+							else
+								trans = new TSocket(host, port);
+						}
+
 						// layered transport
 						if (buffered)
 							trans = new TBufferedTransport(trans as TStreamTransport);
 						if (framed)
 							trans = new TFramedTransport(trans);
-						
+
 						//ensure proper open/close of transport
 						trans.Open();
 						trans.Close();
@@ -151,9 +170,15 @@
 
 		public static void ClientTest(TTransport transport)
 		{
-			TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport);
+			TProtocol proto;
+			if (protocol == "compact")
+				proto = new TCompactProtocol(transport);
+			else if (protocol == "json")
+				proto = new TJSONProtocol(transport);
+			else
+				proto = new TBinaryProtocol(transport);
 
-			ThriftTest.Client client = new ThriftTest.Client(binaryProtocol);
+			ThriftTest.Client client = new ThriftTest.Client(proto);
 			try
 			{
 				if (!transport.IsOpen)
@@ -430,7 +455,6 @@
 			}
 			Console.WriteLine("}");
 
-
 			sbyte arg0 = 1;
 			int arg1 = 2;
 			long arg2 = long.MaxValue;
diff --git a/lib/csharp/test/ThriftTest/TestServer.cs b/lib/csharp/test/ThriftTest/TestServer.cs
index 965a7de..f0e9abb 100644
--- a/lib/csharp/test/ThriftTest/TestServer.cs
+++ b/lib/csharp/test/ThriftTest/TestServer.cs
@@ -23,6 +23,7 @@
 // http://developers.facebook.com/thrift/
 using System;
 using System.Collections.Generic;
+using System.Security.Cryptography.X509Certificates;
 using Thrift.Collections;
 using Thrift.Test; //generated code
 using Thrift.Transport;
@@ -321,7 +322,7 @@
 		{
 			try
 			{
-				bool useBufferedSockets = false, useFramed = false;
+				bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false;
 				int port = 9090, i = 0;
 				string pipe = null;
 				if (args.Length > 0)
@@ -343,7 +344,7 @@
 						{
 							// as default
 						}
-						else if ( args[i] == "buffered" )
+						else if (args[i] == "buffered")
 						{
 							useBufferedSockets = true;
 						}
@@ -351,6 +352,18 @@
 						{
 							useFramed = true;
 						}
+						else if (args[i] == "ssl")
+						{
+							useEncryption = true;
+						}
+						else if (args[i] == "compact" )
+						{
+							compact = true;
+						}
+						else if (args[i] == "json" )
+						{
+							json = true;
+						}
 						else
 						{
 							// Fall back to the older boolean syntax
@@ -371,15 +384,30 @@
 				}
 				else
 				{
-					trans = new TServerSocket(port, 0, useBufferedSockets);
+					if (useEncryption)
+					{
+						trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2("../../../../../keys/server.pem"));
+					}
+					else
+					{
+						trans = new TServerSocket(port, 0, useBufferedSockets);
+					}
 				}
 
+				TProtocolFactory proto;
+				if ( compact )
+					proto = new TCompactProtocol.Factory();
+				else if ( json )
+					proto = new TJSONProtocol.Factory();
+				else
+					proto = new TBinaryProtocol.Factory();
+					
 				// Simple Server
 				TServer serverEngine;
 				if ( useFramed )
-					serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory());
+					serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto);
 				else
-					serverEngine = new TSimpleServer(testProcessor, trans);
+					serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto);
 
 				// ThreadPool Server
 				// serverEngine = new TThreadPoolServer(testProcessor, tServerSocket);
@@ -391,9 +419,11 @@
 
 				// Run it
 				string where = ( pipe != null ? "on pipe "+pipe : "on port " + port);
-				Console.WriteLine("Starting the server " +where+
+				Console.WriteLine("Starting the server " + where +
 					(useBufferedSockets ? " with buffered socket" : "") + 
 					(useFramed ? " with framed transport" : "") + 
+					(useEncryption ? " with encryption" : "") + 
+					(compact ? " with compact protocol" : "") + 
 					"...");
 				serverEngine.Serve();
 
diff --git a/lib/csharp/test/ThriftTest/maketest.sh b/lib/csharp/test/ThriftTest/maketest.sh
index 86c1a11..210a523 100644
--- a/lib/csharp/test/ThriftTest/maketest.sh
+++ b/lib/csharp/test/ThriftTest/maketest.sh
@@ -26,4 +26,5 @@
 export MONO_PATH=../../
 
 timeout 120 ./TestClientServer.exe server &
+sleep 1
 ./TestClientServer.exe client