myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [3066] Added new tests for the `File', `Socket' and `S


From: Giuseppe Scrivano
Subject: [myserver-commit] [3066] Added new tests for the `File', `Socket' and `SslSocket' classes.
Date: Sat, 02 May 2009 15:03:40 +0000

Revision: 3066
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=3066
Author:   gscrivano
Date:     2009-05-02 15:03:39 +0000 (Sat, 02 May 2009)
Log Message:
-----------
Added new tests for the `File', `Socket' and `SslSocket' classes.  Thanks to 
Ivaylo Ilionov.

Modified Paths:
--------------
    trunk/myserver/src/base/socket/ssl_socket.cpp
    trunk/myserver/src/base/ssl/ssl.cpp
    trunk/myserver/tests/Makefile.am

Added Paths:
-----------
    trunk/myserver/tests/test_file.cpp
    trunk/myserver/tests/test_socket.cpp
    trunk/myserver/tests/test_ssl_socket.cpp

Modified: trunk/myserver/src/base/socket/ssl_socket.cpp
===================================================================
--- trunk/myserver/src/base/socket/ssl_socket.cpp       2009-05-02 10:54:25 UTC 
(rev 3065)
+++ trunk/myserver/src/base/socket/ssl_socket.cpp       2009-05-02 15:03:39 UTC 
(rev 3066)
@@ -95,7 +95,7 @@
   {
     err = SSL_write(sslConnection,buffer,len);
   }while((err <= 0) &&
-         (SSL_get_error(sslConnection,err) == SSL_ERROR_WANT_WRITE 
+         (SSL_get_error(sslConnection,err) == SSL_ERROR_WANT_WRITE
           || SSL_get_error(sslConnection,err) == SSL_ERROR_WANT_READ));
   if(err <= 0)
     return -1;
@@ -108,14 +108,14 @@
  */
 int SslSocket::connect(MYSERVER_SOCKADDR* sa, int na)
 {
-   if ( sa == NULL || (sa->ss_family != AF_INET && sa->ss_family != AF_INET6) )
-      return 1;//Andu: TODO our error code or what?
-  if ( (sa->ss_family == AF_INET && na != sizeof(sockaddr_in)) 
+  if ( sa == NULL || (sa->ss_family != AF_INET && sa->ss_family != AF_INET6) )
+    return 1;
+  if ( (sa->ss_family == AF_INET && na != sizeof(sockaddr_in))
 #if HAVE_IPV6
-   || (sa->ss_family == AF_INET6 && na != sizeof(sockaddr_in6)) 
+       || (sa->ss_family == AF_INET6 && na != sizeof(sockaddr_in6))
 #endif
-)
-     return 1;//Andu: TODO our error code or what?
+       )
+    return 1;
 
   sslMethod = SSLv23_client_method();
   /*! Create the local context. */
@@ -218,7 +218,8 @@
   do
   {
     sslAccept = SSL_accept (sslConnection);
-  }while (SSL_get_error (sslConnection, sslAccept) == SSL_ERROR_WANT_READ);
+  }while (sslAccept != 1 &&
+          SSL_get_error (sslConnection, sslAccept) == SSL_ERROR_WANT_READ);
 
   if(sslAccept != 1 )
   {

Modified: trunk/myserver/src/base/ssl/ssl.cpp
===================================================================
--- trunk/myserver/src/base/ssl/ssl.cpp 2009-05-02 10:54:25 UTC (rev 3065)
+++ trunk/myserver/src/base/ssl/ssl.cpp 2009-05-02 15:03:39 UTC (rev 3066)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2007, 2008 Free Software Foundation, Inc.
+Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
@@ -47,7 +47,7 @@
   if(FilesUtility::fileExists(certificateFile.c_str()) == 0)
     return -1;
   
-  if(!SSL_CTX_use_certificate_file (context, certificateFile.c_str 
(),SSL_FILETYPE_PEM))
+  if(SSL_CTX_use_certificate_file (context, certificateFile.c_str 
(),SSL_FILETYPE_PEM) != 1)
     return -1;
 
   /*
@@ -56,11 +56,10 @@
   if(FilesUtility::fileExists(privateKeyFile) == 0)
     return -1;
 
-  if(!(SSL_CTX_use_PrivateKey_file(context, privateKeyFile.c_str(), 
-                                   SSL_FILETYPE_PEM)))
+  if(SSL_CTX_use_PrivateKey_file(context, privateKeyFile.c_str(),
+                                   SSL_FILETYPE_PEM) != 1)
     return -1;
 
-
   return 1;
 }  
 

Modified: trunk/myserver/tests/Makefile.am
===================================================================
--- trunk/myserver/tests/Makefile.am    2009-05-02 10:54:25 UTC (rev 3065)
+++ trunk/myserver/tests/Makefile.am    2009-05-02 15:03:39 UTC (rev 3066)
@@ -2,5 +2,5 @@
 #
 
 bin_PROGRAMS = tests_suite
-tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp 
test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp 
test_security_domain.cpp test_validator.cpp test_auth_domain.cpp 
test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp 
test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp 
test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp 
test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp 
test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp 
test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp 
test_cached_file.cpp test_files_utility.cpp test_http_request.cpp 
test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp 
test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp 
test_http_response.cpp test_multicast.cpp test_security_cache.cpp 
test_security_token.cpp test_socket_pair.cpp test_unix_socket.cpp 
test_utility.cpp  test_xml_validator.cpp test_ip.cpp test_plugin_info.cpp 
test_fork_server.cpp
+tests_suite_SOURCES = main.cpp test_connection.cpp test_file.cpp test_ftp.cpp 
test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp 
test_security_domain.cpp test_validator.cpp test_auth_domain.cpp 
test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp 
test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp 
test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp 
test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp 
test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp 
test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp 
test_cached_file.cpp test_files_utility.cpp test_http_request.cpp 
test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp 
test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp 
test_http_response.cpp test_multicast.cpp test_security_cache.cpp 
test_security_token.cpp test_socket.cpp test_socket_pair.cpp 
test_ssl_socket.cpp test_unix_socket.cpp test_utility.cpp  
test_xml_validator.cpp test_ip.cpp test_plugin_info.cpp test_fork_server.cpp
 tests_suite_LDADD = ../src/libmyserver.a $(CPPUNIT_LDFLAGS) $(PTHREAD_LIB) 
$(IDN_LIB) $(XNET_LIB) $(EVENT_LIB) $(DL_LIB) $(SSL_LIB) $(ZLIB_LIB) 
$(XML_LIBS) $(LDFLAGS)

Added: trunk/myserver/tests/test_file.cpp
===================================================================
--- trunk/myserver/tests/test_file.cpp                          (rev 0)
+++ trunk/myserver/tests/test_file.cpp  2009-05-02 15:03:39 UTC (rev 3066)
@@ -0,0 +1,97 @@
+/*
+ MyServer
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+ 
+ This program is distributed in the hope that it will be useful, 
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+ 
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+ 
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+
+#include "../include/base/utility.h"
+#include "../include/base/file/file.h"
+#include "../include/base/file/files_utility.h"
+
+#include <string.h>
+#include <unistd.h>
+#include <string>
+
+using namespace std;
+
+class TestFile : public CppUnit::TestFixture
+{
+  File *tfile;
+  string fname;
+  
+  CPPUNIT_TEST_SUITE( TestFile );
+  
+  CPPUNIT_TEST( testCreateTemporaryFile );
+  CPPUNIT_TEST( testOnFile );
+  
+  CPPUNIT_TEST_SUITE_END();
+
+public:
+  void setUp ( )
+  {
+    setcwdBuffer ();/* Be sure the cwd buffer is initialized.  */
+    FilesUtility::temporaryFileName ( 0, fname );
+    tfile = new File;
+  }
+  
+  void tearDown ( )
+  {
+    delete tfile;
+  }
+  
+  void testCreateTemporaryFile ( )
+  {
+    CPPUNIT_ASSERT_EQUAL( tfile->createTemporaryFile ( fname.c_str() ), 0 );
+    
+    CPPUNIT_ASSERT_EQUAL( tfile->close ( ), 0 );
+  }
+  
+  void testOnFile ( )
+  {
+    char buf[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'm', 'y', 'W', 'o', 'r', 
'l', 'd', 0 };
+    const int bufLen = sizeof (buf) / sizeof (char);
+    u_long nbw;
+    u_long nbr;
+    
+    CPPUNIT_ASSERT_EQUAL( tfile->openFile( fname.c_str(), 
File::MYSERVER_OPEN_WRITE | File::MYSERVER_OPEN_READ | 
File::MYSERVER_CREATE_ALWAYS ), 0 );
+    
+    CPPUNIT_ASSERT_EQUAL( tfile->writeToFile ( buf, bufLen, &nbw ), 0 );
+    
+    memset ( buf, 0, bufLen );
+    
+    CPPUNIT_ASSERT_EQUAL( tfile->seek ( 0 ), 0 );
+    
+    CPPUNIT_ASSERT_EQUAL( tfile->read ( buf, bufLen, &nbr ), 0 );
+    
+    CPPUNIT_ASSERT( nbr > 0 );
+    
+    CPPUNIT_ASSERT( tfile->getCreationTime ( ) != -1 );
+    
+    CPPUNIT_ASSERT( tfile->getLastAccTime ( ) != -1 );
+    
+    CPPUNIT_ASSERT( tfile->getLastModTime ( ) != -1 );
+    
+    CPPUNIT_ASSERT( tfile->getFileSize ( ) != -1 );
+    
+    CPPUNIT_ASSERT_EQUAL( tfile->close ( ), 0 );
+  }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestFile );

Added: trunk/myserver/tests/test_socket.cpp
===================================================================
--- trunk/myserver/tests/test_socket.cpp                                (rev 0)
+++ trunk/myserver/tests/test_socket.cpp        2009-05-02 15:03:39 UTC (rev 
3066)
@@ -0,0 +1,191 @@
+/*
+ MyServer
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+ 
+ This program is distributed in the hope that it will be useful, 
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+ 
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include "../include/base/socket/socket.h"
+#include "../include/base/thread/thread.h"
+
+#include <string.h>
+#include <errno.h>
+#include <arpa/inet.h>
+#include <iostream>
+
+using namespace std;
+
+void* testRecvClient ( void* );
+
+class TestSocket : public CppUnit::TestFixture
+{
+  Socket *obj;
+  ThreadID tid;
+  
+  CPPUNIT_TEST_SUITE( TestSocket );
+  
+  CPPUNIT_TEST( testGethostname );
+  CPPUNIT_TEST( testRecv );
+  CPPUNIT_TEST( testGetLocalIPsList );
+  
+  CPPUNIT_TEST_SUITE_END( );
+  
+public:
+  
+  void setUp ( )
+  {
+    CPPUNIT_ASSERT_EQUAL( Socket::startupSocketLib ( ), 0 );
+    
+    obj = new Socket;
+  }
+  
+  void tearDown ( )
+  {
+    delete obj;
+  }
+  
+  void testGethostname ( )
+  { 
+    char host[] = "localhost";
+    int len = sizeof ( host ) / sizeof ( char );
+      
+    int status = obj->gethostname ( host, len );
+  
+    CPPUNIT_ASSERT_EQUAL( status, 0 );
+  }
+  
+  void testRecv ( )
+  {
+    ThreadID tid;
+    int optvalReuseAddr = 1;
+    char host[] = "localhost";
+    int port = 6543;
+    MYSERVER_SOCKADDRIN sockIn = { 0 };
+    int status;
+    
+    ((sockaddr_in*) (&sockIn))->sin_family = AF_INET;
+    ((sockaddr_in*) (&sockIn))->sin_addr.s_addr = inet_addr ( "127.0.0.1" );
+    ((sockaddr_in*) (&sockIn))->sin_port = htons ( port );
+    
+    int sockInLen = sizeof ( sockaddr_in );
+    
+    CPPUNIT_ASSERT( obj->socket ( AF_INET, SOCK_STREAM, 0 ) != -1 );
+    
+    CPPUNIT_ASSERT( obj->setsockopt ( SOL_SOCKET, SO_REUSEADDR, (const char*) 
&optvalReuseAddr, sizeof(optvalReuseAddr) ) != -1 );
+    
+    // If the port is used by another program, try a few others.
+      if ( ( status = obj->bind ( &sockIn, sockInLen ) ) != 0 )
+      while ( ++port < 28000 )
+      {
+        ((sockaddr_in*) (&sockIn))->sin_port = htons ( port );
+        
+        if ( ( status = obj->bind ( &sockIn, sockInLen ) ) == 0 )
+          break;
+      }
+    
+    CPPUNIT_ASSERT( status != -1 );
+    
+    CPPUNIT_ASSERT( obj->listen ( 1 ) != -1 );
+    
+    CPPUNIT_ASSERT_EQUAL( Thread::create ( &tid, testRecvClient, &port ), 0 );
+    
+    Socket s = obj->accept ( &sockIn, &sockInLen );
+    
+    status = int ( s.getHandle ( ) );
+    
+    if ( status < 0 )
+      CPPUNIT_ASSERT( status != -1 );
+    
+    CPPUNIT_ASSERT( s.bytesToRead ( ) >= 0 );
+    
+    int bufLen = 8;
+    char buf[bufLen];
+    memset ( buf, 0, bufLen );
+    
+    if ( ( status = s.recv ( buf, 1, 0 ) ) < 0 )
+      CPPUNIT_ASSERT( status >= 0 );
+      
+    CPPUNIT_ASSERT( s.bytesToRead ( ) > 0 );
+    
+    if ( ( status = s.recv ( buf, bufLen, 0, 2 ) ) < 0 && status != -2 )
+      CPPUNIT_ASSERT( status >= 0 || status == -2 );
+      
+    CPPUNIT_ASSERT( s.bytesToRead ( ) == 0 );
+    
+    if ( ( status = s.recv ( buf, bufLen, 0, 1 ) ) < 0 && status != -2 )
+      CPPUNIT_ASSERT( status >= 0 || status == -2 );
+    
+    // no other messages, must be -1
+    if ( ( status = s.recv ( buf, bufLen, 0 ) ) != -1)
+      CPPUNIT_ASSERT( status == -1 );
+    
+    Thread::join ( tid );
+    
+    CPPUNIT_ASSERT( obj->close ( ) != -1 );
+  }
+  
+  void testGetLocalIPsList ( )
+  {
+    int status = obj->socket ( AF_INET, SOCK_STREAM, 0 );
+
+    CPPUNIT_ASSERT( status != -1 );
+    
+    string out;
+    
+    status = obj->getLocalIPsList ( out );
+    
+    CPPUNIT_ASSERT( status != -1 );
+    
+    status = obj->close ( );
+
+    CPPUNIT_ASSERT( status != -1 );
+  }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestSocket );
+
+void*
+testRecvClient ( void *arg )
+{
+  Socket *obj2 = new Socket;
+  
+  int optvalReuseAddr = 1;
+  char host[] = "localhost";
+  int port = *((int*)arg);
+  int status;
+  
+  CPPUNIT_ASSERT( obj2->socket ( AF_INET, SOCK_STREAM, 0 ) != -1 );
+  
+  CPPUNIT_ASSERT( obj2->connect ( host, port ) != -1 );
+  
+  int bufLen = 8;
+  char buf[bufLen];
+  memset ( buf, 0, bufLen );
+  strcpy ( buf, "ehlo" );
+  
+  CPPUNIT_ASSERT( obj2->send ( buf, strlen ( buf ), 0 ) != -1 );
+  
+  sleep ( 2 );
+  
+  CPPUNIT_ASSERT( obj2->shutdown ( SD_BOTH ) != -1 );
+ 
+  CPPUNIT_ASSERT( obj2->close ( ) != -1 );
+  
+  delete obj2;
+  obj2 = NULL;
+}

Added: trunk/myserver/tests/test_ssl_socket.cpp
===================================================================
--- trunk/myserver/tests/test_ssl_socket.cpp                            (rev 0)
+++ trunk/myserver/tests/test_ssl_socket.cpp    2009-05-02 15:03:39 UTC (rev 
3066)
@@ -0,0 +1,236 @@
+/*
+ MyServer
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+ 
+ This program is distributed in the hope that it will be useful, 
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+ 
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define TESTSERVERPEM "server.pem"
+#define TESTSERVERKEY "server.key"
+
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include "../include/base/socket/ssl_socket.h"
+#include "../include/base/file/file.h"
+#include "../include/base/file/files_utility.h"
+#include "../include/base/thread/thread.h"
+
+#include <string.h>
+#include <errno.h>
+#include <arpa/inet.h>
+#include <iostream>
+
+using namespace std;
+
+
+static const char *serverKey = "-----BEGIN RSA PRIVATE KEY-----\n\
+MIIEowIBAAKCAQEA5EaaswqVe0pk+4nuhwp4S+P5Xw6f8Ba/g1kXFGP3CPhRKzEQ\n\
+q1JmkMl6t/AWVbr+wRz/JZ71JjHg+EDzaNNyHjtQQZL5A/DGXdgyRmSsgqJInAao\n\
+WB4EIbfZ+7/UalwRNyyto2sG5T2kIrFfyHDQ7oX/fuw0JjTbR8vLCPw4pQkuWm1o\n\
+JYwuh2HV2w+IEtXE1yBNDDZcy4DPRixqB8z9wN8iOHOrW9pgCaim+EB0QdrWlgae\n\
+jVsrwSZNpivN6hxXupPv+I/LBrf7xDirDRxBvgZjaQgaiMtZ2SHLV2m17x6rIKXw\n\
+wBV8xUAxTwcrCnvqXusZAx58Sow//9GCKpHsJQIDAQABAoIBAAJqIaEz56vfHKZ6\n\
+e87q3Vy9SDoyY56PAngC2OBK1q909hiME2LZGjXq2oTzs/VZMDld/Kbq3QTTVhjH\n\
+2xj6r5aLj6OwyKY7PEFYAZZq4p9ki58SThln8H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7r+XoohvxSk+oS6shNmF8\n\
+FX3J22wtVUi4Ve/XftYt6RJKd764o5WTdh/Z+RUbtusXnj3ygpI/G7fTzuPUj9uF\n\
+3K2VTrZpJnbMs7+i3w/ziC/cqWRVK6Rcq3bLzTXrig==\n\
+-----END CERTIFICATE-----\n";
+
+
+
+void* testSslRecvClient ( void* ); //
+
+class TestSslSocket : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( TestSslSocket );
+  
+  CPPUNIT_TEST( testRecv );
+  
+  CPPUNIT_TEST_SUITE_END( );
+  
+public:
+  
+  void setUp ( )
+  {
+    SSL_library_init ( );
+    u_long nbw;
+    File f;
+
+    f.openFile (TESTSERVERKEY,  File::MYSERVER_OPEN_WRITE | 
File::MYSERVER_CREATE_ALWAYS);
+    f.writeToFile (serverKey, strlen (serverKey), &nbw);
+    f.close ();
+
+    f.openFile (TESTSERVERPEM,  File::MYSERVER_OPEN_WRITE | 
File::MYSERVER_CREATE_ALWAYS);
+    f.writeToFile (serverPem, strlen (serverPem), &nbw);
+    f.close ();
+  }
+  
+  void tearDown ( )
+  {
+    FilesUtility::deleteFile (TESTSERVERPEM);
+    FilesUtility::deleteFile (TESTSERVERKEY);
+  }
+  
+  void testRecv ( )
+  {
+    Socket *obj = new Socket;
+    SslSocket *sslObj = NULL;
+    SSL_CTX *ctx = NULL;;
+    ThreadID tid;
+    
+    int optvalReuseAddr = 1;
+    char host[] = "localhost";
+    int port = 6543;
+    MYSERVER_SOCKADDRIN sockIn = { 0 };
+    int status;
+    
+    ((sockaddr_in*) (&sockIn))->sin_family = AF_INET;
+    ((sockaddr_in*) (&sockIn))->sin_addr.s_addr = inet_addr ( "127.0.0.1" );
+    ((sockaddr_in*) (&sockIn))->sin_port = htons ( port );
+    
+    int sockInLen = sizeof ( sockaddr_in );
+    
+    CPPUNIT_ASSERT( obj->socket ( AF_INET, SOCK_STREAM, 0 ) != -1 );
+    
+    CPPUNIT_ASSERT( obj->setsockopt ( SOL_SOCKET, SO_REUSEADDR,
+                                      (const char*) &optvalReuseAddr,
+                                      sizeof(optvalReuseAddr) ) != -1 );
+    
+    ctx = SSL_CTX_new ( SSLv23_server_method() );
+
+    if ( SSL_CTX_use_certificate_file ( ctx, TESTSERVERPEM, SSL_FILETYPE_PEM ) 
!= 1 )
+    {
+      SSL_CTX_free ( ctx );
+      CPPUNIT_ASSERT (false);
+    }
+
+    if ( SSL_CTX_use_PrivateKey_file ( ctx, TESTSERVERKEY, SSL_FILETYPE_PEM ) 
!= 1 )
+    {
+      SSL_CTX_free ( ctx );
+      CPPUNIT_ASSERT (false);
+    }
+
+    // If the port is used by another program, try a few others.
+    do
+    {
+      port++;
+      ((sockaddr_in*) (&sockIn))->sin_port = htons ( port);
+    }
+    while ((status = obj->bind (&sockIn, sockInLen)) != 0 && port < 28000);
+
+    CPPUNIT_ASSERT( status != -1 );
+
+    CPPUNIT_ASSERT( obj->listen ( 1 ) != -1 );
+
+    CPPUNIT_ASSERT_EQUAL( Thread::create ( &tid, testSslRecvClient, &port ), 0 
);
+    Socket s = obj->accept ( &sockIn, &sockInLen );
+    sslObj = new SslSocket (&s);
+
+    sslObj->setSSLContext ( ctx );
+
+    int ret = sslObj->sslAccept ( );
+
+    if ( ret < 0 )
+    {
+      delete obj;
+      delete sslObj;
+      SSL_CTX_free ( ctx );
+      CPPUNIT_ASSERT (false);
+    }
+    
+    char buf[32] = {0};
+    
+    CPPUNIT_ASSERT( sslObj->recv ( buf, sizeof(buf), 0 ) != -1 );
+
+    CPPUNIT_ASSERT( sslObj->close ( ) != -1 );
+
+    CPPUNIT_ASSERT( obj->close ( ) != -1 );
+
+    SSL_CTX_free ( ctx );
+
+    delete obj;
+    delete sslObj;
+  }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestSslSocket );
+
+void*
+testSslRecvClient ( void *arg )
+{
+  Socket *obj2 = new Socket;
+  SslSocket *sslObj2 = NULL;
+  int optvalReuseAddr = 1;
+  char host[] = "localhost";
+  int port = *((int*)arg);
+  int cstatus = 0;
+  MYSERVER_SOCKADDRIN sockIn = { 0 };
+
+  ((sockaddr_in*) (&sockIn))->sin_family = AF_INET;
+  ((sockaddr_in*) (&sockIn))->sin_addr.s_addr = inet_addr ( "127.0.0.1" );
+  ((sockaddr_in*) (&sockIn))->sin_port = htons ( port );
+
+  int sockInLen = sizeof ( struct sockaddr_in );
+
+  CPPUNIT_ASSERT( obj2->socket ( AF_INET, SOCK_STREAM, 0 ) != -1 );
+
+  sslObj2 = new SslSocket ( obj2 );
+
+  CPPUNIT_ASSERT( sslObj2->connect ( &sockIn, sockInLen ) != -1 );
+
+  char buf[] = "Works?\n";
+
+  CPPUNIT_ASSERT(sslObj2->rawSend ( buf, strlen(buf), 0 ) != -1 );
+
+  CPPUNIT_ASSERT( sslObj2->close ( ) != -1 );
+
+  delete obj2;
+  delete sslObj2;
+  return 0;
+}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]