diff options
author | Frank Herchet <frank.fh.herchet@bmw.de> | 2012-02-21 14:21:21 +0100 |
---|---|---|
committer | Frank Herchet <frank.fh.herchet@bmw.de> | 2012-02-21 14:21:21 +0100 |
commit | 9c209bd92d9400012765d86fc5bd51ddfe266071 (patch) | |
tree | 751d6bc53b309f6ef1bfc536977d5d637783214d /AudioManagerDaemon/test | |
parent | b03052149ce2505367ab8177ca497d669ca07b1d (diff) | |
download | audiomanager-9c209bd92d9400012765d86fc5bd51ddfe266071.tar.gz |
* [GAM-21] 3 telnetserverTests
Diffstat (limited to 'AudioManagerDaemon/test')
-rw-r--r-- | AudioManagerDaemon/test/telnetserver/CAmTelnetServerTest.cpp | 196 | ||||
-rw-r--r-- | AudioManagerDaemon/test/telnetserver/CAmTelnetServerTest.h | 57 |
2 files changed, 150 insertions, 103 deletions
diff --git a/AudioManagerDaemon/test/telnetserver/CAmTelnetServerTest.cpp b/AudioManagerDaemon/test/telnetserver/CAmTelnetServerTest.cpp index da7ec2b..d3d621e 100644 --- a/AudioManagerDaemon/test/telnetserver/CAmTelnetServerTest.cpp +++ b/AudioManagerDaemon/test/telnetserver/CAmTelnetServerTest.cpp @@ -17,128 +17,125 @@ using namespace testing; using namespace am; +using namespace std; + + +static std::string controllerPlugin = std::string(CONTROLLER_PLUGIN); +static unsigned short servPort = 6060; +static int staticSocket = -1; +static SocketHandler* mpSocketHandler = NULL; void* startSocketHandler(void* data) { - CAmTelnetServerTest* Test = static_cast<CAmTelnetServerTest*>(data); + MyEnvironment* Env = static_cast<MyEnvironment*>(data); SocketHandler mySocketHandler; - Test->setSocketHandler(&mySocketHandler); - std::cout << "pThread: startSocketHandler" << std::endl; + Env->setSocketHandler(&mySocketHandler); mySocketHandler.start_listenting(); - Test->setSocketHandler(NULL); - std::cout << "pThread: return" << std::endl; + Env->setSocketHandler(NULL); return (NULL); } - -CAmTelnetServerTest::CAmTelnetServerTest() +MyEnvironment::MyEnvironment() : mlistRoutingPluginDirs() , mlistCommandPluginDirs() -, mpSocketHandler(NULL) +//, mpSocketHandler(NULL) , mDatabasehandler(std::string(":memory:")) , mRoutingSender(mlistRoutingPluginDirs) , mCommandSender(mlistRoutingPluginDirs) -, mControlSender(std::string("")) +, mControlSender(controllerPlugin) , mRouter(&mDatabasehandler,&mControlSender) , mpCommandReceiver(NULL) , mpRoutingReceiver(NULL) , mpControlReceiver(NULL) , mpTelnetServer(NULL) -, mSocketHandlerThread() +, mSocketHandlerThread(0) { - // TODO Auto-generated constructor stub +} +MyEnvironment::~MyEnvironment() +{ + usleep(500); + if(NULL != mpTelnetServer) + delete(mpTelnetServer); + if(NULL != mpControlReceiver) + delete(mpControlReceiver); + if(NULL != mpRoutingReceiver) + delete(mpRoutingReceiver); + if(NULL != mpCommandReceiver) + delete(mpCommandReceiver); } -CAmTelnetServerTest::~CAmTelnetServerTest() +void MyEnvironment::SetUp() { - if(NULL != mpTelnetServer) - delete(mpTelnetServer); - if(NULL != mpControlReceiver) - delete(mpControlReceiver); - if(NULL != mpRoutingReceiver) - delete(mpRoutingReceiver); - if(NULL != mpCommandReceiver) - delete(mpCommandReceiver); + pthread_create(&mSocketHandlerThread, NULL, startSocketHandler, this); + sleep(1); } -void CAmTelnetServerTest::SetUp() +void MyEnvironment::TearDown() +{ + +} + +void MyEnvironment::setSocketHandler(SocketHandler* pSocketHandler) { - std::cout << "CAmTelnetServerTest::SetUp" << std::endl; + mpSocketHandler = pSocketHandler; + if(NULL != pSocketHandler) + { + mpCommandReceiver = new CommandReceiver(&mDatabasehandler,&mControlSender,mpSocketHandler); + mpRoutingReceiver = new RoutingReceiver(&mDatabasehandler,&mRoutingSender,&mControlSender,mpSocketHandler); + mpControlReceiver = new ControlReceiver(&mDatabasehandler,&mRoutingSender,&mCommandSender,mpSocketHandler,&mRouter); + //startup all the Plugins and Interfaces + mControlSender.startupController(mpControlReceiver); + mCommandSender.startupInterface(mpCommandReceiver); + mRoutingSender.startupRoutingInterface(mpRoutingReceiver); - // Create a Thread for the SocketHandler loop - std::cout << "creating mSocketHandlerThread" << std::endl; - pthread_create(&mSocketHandlerThread, NULL, startSocketHandler, this); + //when the routingInterface is done, all plugins are loaded: + mControlSender.hookAllPluginsLoaded(); + + // Starting TelnetServer + mpTelnetServer = new TelnetServer(mpSocketHandler,&mCommandSender,mpCommandReceiver,&mRoutingSender,mpRoutingReceiver,&mControlSender,mpControlReceiver,&mDatabasehandler,&mRouter,servPort,3); + } } -void CAmTelnetServerTest::TearDown() +void MyEnvironment::stopSocketHandler() { - pthread_join(mSocketHandlerThread, NULL); + mpSocketHandler->stop_listening(); } -int main(int argc, char **argv) { - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); +CAmTelnetServerTest::CAmTelnetServerTest() +{ + } -void CAmTelnetServerTest::setSocketHandler(SocketHandler* pSocketHandler) +CAmTelnetServerTest::~CAmTelnetServerTest() { - mpSocketHandler = pSocketHandler; - - if(NULL != pSocketHandler) - { - std::cout << "CommandReceiver" << std::endl; - mpCommandReceiver = new CommandReceiver(&mDatabasehandler,&mControlSender,mpSocketHandler); - std::cout << "RoutingReceiver" << std::endl; - mpRoutingReceiver = new RoutingReceiver(&mDatabasehandler,&mRoutingSender,&mControlSender,mpSocketHandler); - std::cout << "ControlReceiver" << std::endl; - mpControlReceiver = new ControlReceiver(&mDatabasehandler,&mRoutingSender,&mCommandSender,mpSocketHandler,&mRouter); - - - std::cout << "startup all the Plugins and Interfaces" << std::endl; - //startup all the Plugins and Interfaces - mControlSender.startupController(mpControlReceiver); - mCommandSender.startupInterface(mpCommandReceiver); - mRoutingSender.startupRoutingInterface(mpRoutingReceiver); - - //when the routingInterface is done, all plugins are loaded: - mControlSender.hookAllPluginsLoaded(); - - // Starting TelnetServer - std::cout << "Starting TelnetServer" << std::endl; - mpTelnetServer = new TelnetServer(mpSocketHandler,&mCommandSender,mpCommandReceiver,&mRoutingSender,mpRoutingReceiver,&mControlSender,mpControlReceiver,&mDatabasehandler,&mRouter,6060,3); - } + } -TEST_F(CAmTelnetServerTest,playWithSockets) +void CAmTelnetServerTest::SetUp() { - //pthread_t serverThread; - std::cout << "start playWithSockets" << std::endl; - struct sockaddr_in servAddr; - unsigned short servPort = 6060; - struct hostent *host; - int socket_; - //creates a thread that handles the serverpart - //pthread_create(&serverThread, NULL, playWithSocketServer, NULL); +} + +void CAmTelnetServerTest::TearDown() +{ - sleep(1); //we need that here because the port needs to be opened - if ((socket_ = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) - { - std::cout << "socket problem" << std::endl; +} - } - else - { - std::cout << "socket open" << std::endl; - } +TEST_F(CAmTelnetServerTest,connectTelnetServer) +{ + struct sockaddr_in servAddr; - if ((host = (struct hostent*) gethostbyname("localhost")) == 0) + staticSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + ASSERT_GE(staticSocket,0); + + struct hostent *host = (struct hostent*) gethostbyname("localhost"); + if (host == 0) { std::cout << " ERROR: gethostbyname() failed\n" << std::endl; - exit(1); + return; } memset(&servAddr, 0, sizeof(servAddr)); @@ -146,15 +143,48 @@ TEST_F(CAmTelnetServerTest,playWithSockets) servAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*) (host->h_addr_list[0]))); servAddr.sin_port = htons(servPort); - if (connect(socket_, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) - { - std::cout << "ERROR: connect() failed\n" << std::endl; - } + int return_connect = connect(staticSocket, (struct sockaddr *) &servAddr, sizeof(servAddr)); + ASSERT_GE(return_connect,0); + char buffer[1000]; + int read=recv(staticSocket,buffer,sizeof(buffer),0); + ASSERT_GT(read,1); +} + +TEST_F(CAmTelnetServerTest,sendCmdTelnetServer) +{ + std::string string("help"); + + ssize_t sizesent = send(staticSocket, string.c_str(), string.size(), 0); + ASSERT_EQ(sizesent,string.size()); + + char buffer[1000]; + memset(buffer,0,sizeof(buffer)); + int read=recv(staticSocket,buffer,sizeof(buffer),0); + ASSERT_GT(read,1); +} - std::string string("finish!"); - send(socket_, string.c_str(), string.size(), 0); +TEST_F(CAmTelnetServerTest,closeTelnetServerConnection) +{ + std::string string ("exit"); + + mpSocketHandler->stop_listening(); + + ssize_t sizesent = send(staticSocket, string.c_str(), string.size(), 0); + ASSERT_EQ(sizesent,string.size()); - //pthread_join(serverThread, NULL); + char buffer[1000]; + memset(buffer,0,sizeof(buffer)); + int read=recv(staticSocket,buffer,sizeof(buffer),0); + ASSERT_GT(read,1); + + close(staticSocket); + staticSocket = -1; } +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + ::testing::Environment* const env = ::testing::AddGlobalTestEnvironment(new MyEnvironment); + (void) env; + return RUN_ALL_TESTS(); +} diff --git a/AudioManagerDaemon/test/telnetserver/CAmTelnetServerTest.h b/AudioManagerDaemon/test/telnetserver/CAmTelnetServerTest.h index fae79d4..182dfa1 100644 --- a/AudioManagerDaemon/test/telnetserver/CAmTelnetServerTest.h +++ b/AudioManagerDaemon/test/telnetserver/CAmTelnetServerTest.h @@ -31,35 +31,52 @@ class ControlReceiver; class TelnetServer; -class CAmTelnetServerTest : public ::testing::Test{ - public: - CAmTelnetServerTest(); - ~CAmTelnetServerTest(); +class MyEnvironment : public ::testing::Environment +{ + public: + MyEnvironment(); + ~MyEnvironment(); + // Override this to define how to set up the environment. + void SetUp(); + // Override this to define how to tear down the environment. + void TearDown(); - void SetUp() ; + void setSocketHandler(SocketHandler* pSocketHandler); - void TearDown() ; + void stopSocketHandler(); + + std::vector<std::string> mlistRoutingPluginDirs; + std::vector<std::string> mlistCommandPluginDirs; - void setSocketHandler(SocketHandler* pSocketHandler); + //SocketHandler* mpSocketHandler; + DatabaseHandler mDatabasehandler; + RoutingSender mRoutingSender; + CommandSender mCommandSender; + ControlSender mControlSender; + Router mRouter; - std::vector<std::string> mlistRoutingPluginDirs; - std::vector<std::string> mlistCommandPluginDirs; + CommandReceiver* mpCommandReceiver; + RoutingReceiver* mpRoutingReceiver; + ControlReceiver* mpControlReceiver; - SocketHandler* mpSocketHandler; - DatabaseHandler mDatabasehandler; - RoutingSender mRoutingSender; - CommandSender mCommandSender; - ControlSender mControlSender; - Router mRouter; + TelnetServer* mpTelnetServer; - CommandReceiver* mpCommandReceiver; - RoutingReceiver* mpRoutingReceiver; - ControlReceiver* mpControlReceiver; + pthread_t mSocketHandlerThread; +}; - TelnetServer* mpTelnetServer; +class CAmTelnetServerTest : public ::testing::Test +{ + public: + CAmTelnetServerTest(); + ~CAmTelnetServerTest(); + + + void SetUp() ; + + void TearDown() ; - pthread_t mSocketHandlerThread; + //int mSocket; }; } |