Repository: thrift
Updated Branches:
  refs/heads/master e544a8992 -> 808d14324


http://git-wip-us.apache.org/repos/asf/thrift/blob/808d1432/lib/cpp/test/TNonblockingSSLServerTest.cpp
----------------------------------------------------------------------
diff --git a/lib/cpp/test/TNonblockingSSLServerTest.cpp 
b/lib/cpp/test/TNonblockingSSLServerTest.cpp
new file mode 100644
index 0000000..f21dd18
--- /dev/null
+++ b/lib/cpp/test/TNonblockingSSLServerTest.cpp
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#define BOOST_TEST_MODULE TNonblockingSSLServerTest
+#include <boost/test/unit_test.hpp>
+#include <boost/smart_ptr.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/filesystem.hpp>
+#include <boost/format.hpp>
+
+#include "thrift/server/TNonblockingServer.h"
+#include "thrift/transport/TSSLSocket.h"
+#include "thrift/transport/TNonblockingSSLServerSocket.h"
+
+#include "gen-cpp/ParentService.h"
+
+#include <event.h>
+
+using namespace apache::thrift;
+using apache::thrift::concurrency::Guard;
+using apache::thrift::concurrency::Monitor;
+using apache::thrift::concurrency::Mutex;
+using apache::thrift::server::TServerEventHandler;
+using apache::thrift::transport::TSSLSocketFactory;
+using apache::thrift::transport::TSSLSocket;
+
+struct Handler : public test::ParentServiceIf {
+  void addString(const std::string& s) { strings_.push_back(s); }
+  void getStrings(std::vector<std::string>& _return) { _return = strings_; }
+  std::vector<std::string> strings_;
+
+  // dummy overrides not used in this test
+  int32_t incrementGeneration() { return 0; }
+  int32_t getGeneration() { return 0; }
+  void getDataWait(std::string&, const int32_t) {}
+  void onewayWait() {}
+  void exceptionWait(const std::string&) {}
+  void unexpectedExceptionWait(const std::string&) {}
+};
+
+boost::filesystem::path keyDir;
+boost::filesystem::path certFile(const std::string& filename)
+{
+  return keyDir / filename;
+}
+
+struct GlobalFixtureSSL
+{
+    GlobalFixtureSSL()
+    {
+      using namespace boost::unit_test::framework;
+      for (int i = 0; i < master_test_suite().argc; ++i)
+      {
+        BOOST_TEST_MESSAGE(boost::format("argv[%1%] = \"%2%\"") % i % 
master_test_suite().argv[i]);
+      }
+
+#ifdef __linux__
+      // OpenSSL calls send() without MSG_NOSIGPIPE so writing to a socket 
that has
+      // disconnected can cause a SIGPIPE signal...
+      signal(SIGPIPE, SIG_IGN);
+#endif
+
+      TSSLSocketFactory::setManualOpenSSLInitialization(true);
+      apache::thrift::transport::initializeOpenSSL();
+
+      keyDir = 
boost::filesystem::current_path().parent_path().parent_path().parent_path() / 
"test" / "keys";
+      if (!boost::filesystem::exists(certFile("server.crt")))
+      {
+        keyDir = 
boost::filesystem::path(master_test_suite().argv[master_test_suite().argc - 1]);
+        if (!boost::filesystem::exists(certFile("server.crt")))
+        {
+          throw std::invalid_argument("The last argument to this test must be 
the directory containing the test certificate(s).");
+        }
+      }
+    }
+
+    virtual ~GlobalFixtureSSL()
+    {
+      apache::thrift::transport::cleanupOpenSSL();
+#ifdef __linux__
+      signal(SIGPIPE, SIG_DFL);
+#endif
+    }
+};
+
+#if (BOOST_VERSION >= 105900)
+BOOST_GLOBAL_FIXTURE(GlobalFixtureSSL);
+#else
+BOOST_GLOBAL_FIXTURE(GlobalFixtureSSL)
+#endif
+
+boost::shared_ptr<TSSLSocketFactory> createServerSocketFactory() {
+  boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory;
+
+  pServerSocketFactory.reset(new TSSLSocketFactory());
+  pServerSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
+  
pServerSocketFactory->loadCertificate(certFile("server.crt").string().c_str());
+  
pServerSocketFactory->loadPrivateKey(certFile("server.key").string().c_str());
+  pServerSocketFactory->server(true);
+  return pServerSocketFactory;
+}
+
+boost::shared_ptr<TSSLSocketFactory> createClientSocketFactory() {
+  boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory;
+
+  pClientSocketFactory.reset(new TSSLSocketFactory());
+  pClientSocketFactory->authenticate(true);
+  
pClientSocketFactory->loadCertificate(certFile("client.crt").string().c_str());
+  
pClientSocketFactory->loadPrivateKey(certFile("client.key").string().c_str());
+  
pClientSocketFactory->loadTrustedCertificates(certFile("CA.pem").string().c_str());
+  return pClientSocketFactory;
+}
+
+class Fixture {
+private:
+  struct ListenEventHandler : public TServerEventHandler {
+    public:
+      ListenEventHandler(Mutex* mutex) : listenMonitor_(mutex), ready_(false) 
{}
+
+      void preServe() /* override */ {
+        Guard g(listenMonitor_.mutex());
+        ready_ = true;
+        listenMonitor_.notify();
+      }
+
+      Monitor listenMonitor_;
+      bool ready_;
+  };
+
+  struct Runner : public apache::thrift::concurrency::Runnable {
+    int port;
+    boost::shared_ptr<event_base> userEventBase;
+    boost::shared_ptr<TProcessor> processor;
+    boost::shared_ptr<server::TNonblockingServer> server;
+    boost::shared_ptr<ListenEventHandler> listenHandler;
+    boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory;
+    boost::shared_ptr<transport::TNonblockingSSLServerSocket> socket;
+    Mutex mutex_;
+
+    Runner() {
+      listenHandler.reset(new ListenEventHandler(&mutex_));
+    }
+
+    virtual void run() {
+      // When binding to explicit port, allow retrying to workaround bind 
failures on ports in use
+      int retryCount = port ? 10 : 0;
+      pServerSocketFactory = createServerSocketFactory();  
+      startServer(retryCount);
+    }
+
+    void readyBarrier() {
+      // block until server is listening and ready to accept connections
+      Guard g(mutex_);
+      while (!listenHandler->ready_) {
+        listenHandler->listenMonitor_.wait();
+      }
+    }
+  private:
+    void startServer(int retry_count) {
+      try {
+        socket.reset(new transport::TNonblockingSSLServerSocket(port, 
pServerSocketFactory));
+        server.reset(new server::TNonblockingServer(processor, socket));
+             server->setServerEventHandler(listenHandler);
+        server->setNumIOThreads(1);
+        if (userEventBase) {
+          server->registerEvents(userEventBase.get());
+        }
+        server->serve();
+      } catch (const transport::TTransportException&) {
+        if (retry_count > 0) {
+          ++port;
+          startServer(retry_count - 1);
+        } else {
+          throw;
+        }
+      }
+    }
+  };
+
+  struct EventDeleter {
+    void operator()(event_base* p) { event_base_free(p); }
+  };
+
+protected:
+  Fixture() : processor(new 
test::ParentServiceProcessor(boost::make_shared<Handler>())) {}
+
+  ~Fixture() {
+    if (server) {
+      server->stop();
+    }
+    if (thread) {
+      thread->join();
+    }
+  }
+
+  void setEventBase(event_base* user_event_base) {
+    userEventBase_.reset(user_event_base, EventDeleter());
+  }
+
+  int startServer(int port) {
+    boost::shared_ptr<Runner> runner(new Runner);
+    runner->port = port;
+    runner->processor = processor;
+    runner->userEventBase = userEventBase_;
+
+    boost::scoped_ptr<apache::thrift::concurrency::ThreadFactory> 
threadFactory(
+        new apache::thrift::concurrency::PlatformThreadFactory(
+#if !USE_BOOST_THREAD && !USE_STD_THREAD
+            concurrency::PlatformThreadFactory::OTHER, 
concurrency::PlatformThreadFactory::NORMAL,
+            1,
+#endif
+            false));
+    thread = threadFactory->newThread(runner);
+    thread->start();
+    runner->readyBarrier();
+
+    server = runner->server;
+    return runner->port;
+  }
+
+  bool canCommunicate(int serverPort) {
+    boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = 
createClientSocketFactory();
+    boost::shared_ptr<TSSLSocket> socket = 
pClientSocketFactory->createSocket("localhost", serverPort);
+    socket->open();
+    test::ParentServiceClient 
client(boost::make_shared<protocol::TBinaryProtocol>(
+        boost::make_shared<transport::TFramedTransport>(socket)));
+    client.addString("foo");
+    std::vector<std::string> strings;
+    client.getStrings(strings);
+    return strings.size() == 1 && !(strings[0].compare("foo"));
+  }
+
+private:
+  boost::shared_ptr<event_base> userEventBase_;
+  boost::shared_ptr<test::ParentServiceProcessor> processor;
+protected:
+  boost::shared_ptr<server::TNonblockingServer> server;
+private:
+  boost::shared_ptr<apache::thrift::concurrency::Thread> thread;
+
+};
+
+BOOST_AUTO_TEST_SUITE(TNonblockingSSLServerTest)
+
+BOOST_FIXTURE_TEST_CASE(get_specified_port, Fixture) {
+  int specified_port = startServer(12345);
+  BOOST_REQUIRE_GE(specified_port, 12345);
+  BOOST_REQUIRE_EQUAL(server->getListenPort(), specified_port);
+  BOOST_CHECK(canCommunicate(specified_port));
+
+  server->stop();
+}
+
+BOOST_FIXTURE_TEST_CASE(get_assigned_port, Fixture) {
+  int specified_port = startServer(0);
+  BOOST_REQUIRE_EQUAL(specified_port, 0);
+  int assigned_port = server->getListenPort();
+  BOOST_REQUIRE_NE(assigned_port, 0);
+  BOOST_CHECK(canCommunicate(assigned_port));
+
+  server->stop();
+}
+
+BOOST_FIXTURE_TEST_CASE(provide_event_base, Fixture) {
+  event_base* eb = event_base_new();
+  setEventBase(eb);
+  startServer(0);
+
+  // assert that the server works
+  BOOST_CHECK(canCommunicate(server->getListenPort()));
+#if LIBEVENT_VERSION_NUMBER > 0x02010400
+  // also assert that the event_base is actually used when it's easy
+  BOOST_CHECK_GT(event_base_get_num_events(eb, EVENT_BASE_COUNT_ADDED), 0);
+#endif
+}
+
+BOOST_AUTO_TEST_SUITE_END()

http://git-wip-us.apache.org/repos/asf/thrift/blob/808d1432/lib/cpp/test/TNonblockingServerTest.cpp
----------------------------------------------------------------------
diff --git a/lib/cpp/test/TNonblockingServerTest.cpp 
b/lib/cpp/test/TNonblockingServerTest.cpp
index e933d6b..36c64b1 100644
--- a/lib/cpp/test/TNonblockingServerTest.cpp
+++ b/lib/cpp/test/TNonblockingServerTest.cpp
@@ -24,6 +24,7 @@
 #include "thrift/concurrency/Monitor.h"
 #include "thrift/concurrency/Thread.h"
 #include "thrift/server/TNonblockingServer.h"
+#include "thrift/transport/TNonblockingServerSocket.h"
 
 #include "gen-cpp/ParentService.h"
 
@@ -71,6 +72,7 @@ private:
     boost::shared_ptr<TProcessor> processor;
     boost::shared_ptr<server::TNonblockingServer> server;
     boost::shared_ptr<ListenEventHandler> listenHandler;
+    boost::shared_ptr<transport::TNonblockingServerSocket> socket;
     Mutex mutex_;
 
     Runner() {
@@ -93,7 +95,8 @@ private:
   private:
     void startServer(int retry_count) {
       try {
-        server.reset(new server::TNonblockingServer(processor, port));
+        socket.reset(new transport::TNonblockingServerSocket(port));
+        server.reset(new server::TNonblockingServer(processor, socket));
         server->setServerEventHandler(listenHandler);
         if (userEventBase) {
           server->registerEvents(userEventBase.get());
@@ -181,7 +184,6 @@ BOOST_FIXTURE_TEST_CASE(get_specified_port, Fixture) {
   BOOST_CHECK(canCommunicate(specified_port));
 
   server->stop();
-  BOOST_CHECK_EQUAL(server->getListenPort(), specified_port);
 }
 
 BOOST_FIXTURE_TEST_CASE(get_assigned_port, Fixture) {
@@ -192,7 +194,6 @@ BOOST_FIXTURE_TEST_CASE(get_assigned_port, Fixture) {
   BOOST_CHECK(canCommunicate(assigned_port));
 
   server->stop();
-  BOOST_CHECK_EQUAL(server->getListenPort(), 0);
 }
 
 BOOST_FIXTURE_TEST_CASE(provide_event_base, Fixture) {

http://git-wip-us.apache.org/repos/asf/thrift/blob/808d1432/lib/cpp/test/processor/ProcessorTest.cpp
----------------------------------------------------------------------
diff --git a/lib/cpp/test/processor/ProcessorTest.cpp 
b/lib/cpp/test/processor/ProcessorTest.cpp
index a4e984c..486b8cf 100644
--- a/lib/cpp/test/processor/ProcessorTest.cpp
+++ b/lib/cpp/test/processor/ProcessorTest.cpp
@@ -33,6 +33,7 @@
 #include <thrift/server/TNonblockingServer.h>
 #include <thrift/server/TSimpleServer.h>
 #include <thrift/transport/TSocket.h>
+#include <thrift/transport/TNonblockingServerSocket.h>
 
 #include "EventLog.h"
 #include "ServerThread.h"
@@ -121,14 +122,14 @@ public:
     if (framedFactory == NULL) {
       throw TException("TNonblockingServer must use TFramedTransport");
     }
-
+    boost::shared_ptr<TNonblockingServerSocket> socket(new 
TNonblockingServerSocket(port));
     boost::shared_ptr<PlatformThreadFactory> threadFactory(new 
PlatformThreadFactory);
     boost::shared_ptr<ThreadManager> threadManager = 
ThreadManager::newSimpleThreadManager(8);
     threadManager->threadFactory(threadFactory);
     threadManager->start();
 
     return boost::shared_ptr<TNonblockingServer>(
-        new TNonblockingServer(processor, protocolFactory, port, 
threadManager));
+        new TNonblockingServer(processor, protocolFactory, socket, 
threadManager));
   }
 };
 
@@ -150,10 +151,11 @@ public:
       throw TException("TNonblockingServer must use TFramedTransport");
     }
 
+    boost::shared_ptr<TNonblockingServerSocket> socket(new 
TNonblockingServerSocket(port));
     // Use a NULL ThreadManager
     boost::shared_ptr<ThreadManager> threadManager;
     return boost::shared_ptr<TNonblockingServer>(
-        new TNonblockingServer(processor, protocolFactory, port, 
threadManager));
+        new TNonblockingServer(processor, protocolFactory, socket, 
threadManager));
   }
 };
 

http://git-wip-us.apache.org/repos/asf/thrift/blob/808d1432/test/cpp/src/StressTestNonBlocking.cpp
----------------------------------------------------------------------
diff --git a/test/cpp/src/StressTestNonBlocking.cpp 
b/test/cpp/src/StressTestNonBlocking.cpp
index 8f161c0..1ebb9e0 100644
--- a/test/cpp/src/StressTestNonBlocking.cpp
+++ b/test/cpp/src/StressTestNonBlocking.cpp
@@ -29,6 +29,7 @@
 #include <thrift/server/TNonblockingServer.h>
 #include <thrift/transport/TServerSocket.h>
 #include <thrift/transport/TSocket.h>
+#include <thrift/transport/TNonblockingServerSocket.h>
 #include <thrift/transport/TTransportUtils.h>
 #include <thrift/transport/TFileTransport.h>
 #include <thrift/TLogging.h>
@@ -391,13 +392,15 @@ int main(int argc, char** argv) {
 
     boost::shared_ptr<Thread> serverThread;
     boost::shared_ptr<Thread> serverThread2;
+    boost::shared_ptr<transport::TNonblockingServerSocket> nbSocket1, 
nbSocket2;
 
     if (serverType == "simple") {
-
+      nbSocket1.reset(new transport::TNonblockingServerSocket(port));
       serverThread = threadFactory->newThread(boost::shared_ptr<TServer>(
-          new TNonblockingServer(serviceProcessor, protocolFactory, port)));
+          new TNonblockingServer(serviceProcessor, protocolFactory, 
nbSocket1)));
+      nbSocket2.reset(new transport::TNonblockingServerSocket(port + 1));
       serverThread2 = threadFactory->newThread(boost::shared_ptr<TServer>(
-          new TNonblockingServer(serviceProcessor, protocolFactory, port + 
1)));
+          new TNonblockingServer(serviceProcessor, protocolFactory, 
nbSocket2)));
 
     } else if (serverType == "thread-pool") {
 
@@ -406,10 +409,12 @@ int main(int argc, char** argv) {
 
       threadManager->threadFactory(threadFactory);
       threadManager->start();
+      nbSocket1.reset(new transport::TNonblockingServerSocket(port));
       serverThread = threadFactory->newThread(boost::shared_ptr<TServer>(
-          new TNonblockingServer(serviceProcessor, protocolFactory, port, 
threadManager)));
+          new TNonblockingServer(serviceProcessor, protocolFactory, nbSocket1, 
threadManager)));
+      nbSocket2.reset(new transport::TNonblockingServerSocket(port + 1));
       serverThread2 = threadFactory->newThread(boost::shared_ptr<TServer>(
-          new TNonblockingServer(serviceProcessor, protocolFactory, port + 1, 
threadManager)));
+          new TNonblockingServer(serviceProcessor, protocolFactory, nbSocket2, 
threadManager)));
     }
 
     cerr << "Starting the server on port " << port << " and " << (port + 1) << 
endl;

http://git-wip-us.apache.org/repos/asf/thrift/blob/808d1432/test/cpp/src/TestServer.cpp
----------------------------------------------------------------------
diff --git a/test/cpp/src/TestServer.cpp b/test/cpp/src/TestServer.cpp
index b86b34c..2b58f8a 100644
--- a/test/cpp/src/TestServer.cpp
+++ b/test/cpp/src/TestServer.cpp
@@ -33,6 +33,7 @@
 #include <thrift/transport/TServerSocket.h>
 #include <thrift/transport/TSSLServerSocket.h>
 #include <thrift/transport/TSSLSocket.h>
+#include <thrift/transport/TNonblockingServerSocket.h>
 #include <thrift/transport/THttpServer.h>
 #include <thrift/transport/THttpTransport.h>
 #include <thrift/transport/TTransportUtils.h>
@@ -744,7 +745,9 @@ int main(int argc, char** argv) {
       TEvhttpServer nonblockingServer(testBufferProcessor, port);
       nonblockingServer.serve();
     } else {
-      server.reset(new TNonblockingServer(testProcessor, protocolFactory, 
port));
+      boost::shared_ptr<transport::TNonblockingServerSocket> nbSocket;
+      nbSocket.reset(new transport::TNonblockingServerSocket(port));
+      server.reset(new TNonblockingServer(testProcessor, protocolFactory, 
nbSocket));
     }
   }
 

Reply via email to