mock_server.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *      * Redistributions of source code must retain the above copyright
00011  *      notice, this list of conditions and the following disclaimer.
00012  *      * Redistributions in binary form must reproduce the above copyright
00013  *      notice, this list of conditions and the following disclaimer in the
00014  *      documentation and/or other materials provided with the distribution.
00015  *      * Neither the name of the Southwest Research Institute, nor the names
00016  *      of its contributors may be used to endorse or promote products derived
00017  *      from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #include <vector>
00033 #include <algorithm>
00034 
00035 // Headers from simple_message
00036 #include "simple_message.h"
00037 #include "shared_types.h"
00038 #include "message_manager.h"
00039 #include "socket/tcp_server.h"
00040 
00041 #include <gtest/gtest.h>
00042 #include <stdlib.h>
00043 #include <ctype.h>
00044 
00045 int g_argc;
00046 char** g_argv;
00047 const unsigned int g_num_vals = 4;
00048 
00049 TEST(MessageManagerSuite, tcp)
00050 {
00051   // Usage: mock_server <port> val1 .... valN
00052   ASSERT_GE(g_argc, 3);
00053   int port = atoi(g_argv[1]);
00054   std::vector<int> values;
00055   for(int i=2; i<g_argc; i++)
00056   {
00057     if(g_argv[i] && isdigit(g_argv[i][0]))
00058       values.push_back(atoi(g_argv[i]));
00059   }
00060   ASSERT_EQ(values.size(), g_num_vals);
00061   // The ByteArray::unload() calls will pull the values off in reverse
00062   // order, so we'll reverse the order in which we expect them.
00063   std::reverse(values.begin(), values.end());
00064 
00065   industrial::tcp_server::TcpServer tcpServer;
00066   industrial::message_manager::MessageManager tcpManager;
00067 
00068   // Construct server
00069   ASSERT_TRUE(tcpServer.init(port));
00070 
00071   // Listen for client connection, init manager and start thread
00072   ASSERT_TRUE(tcpServer.makeConnect());
00073   ASSERT_TRUE(tcpManager.init(&tcpServer));
00074 
00075   industrial::simple_message::SimpleMessage msg;
00076   ASSERT_TRUE(tcpServer.receiveMsg(msg));
00077   ASSERT_EQ(msg.getMessageType(), industrial::simple_message::StandardMsgTypes::IO);
00078   ASSERT_EQ(msg.getCommType(), industrial::simple_message::CommTypes::TOPIC);
00079   ASSERT_EQ(msg.getReplyCode(), industrial::simple_message::ReplyTypes::INVALID);
00080   ASSERT_EQ(msg.getDataLength(), (int)(g_num_vals*4));
00081   industrial::byte_array::ByteArray buf = msg.getData();
00082   for(unsigned int i=0; i<g_num_vals; i++)
00083   {
00084     int val;
00085     buf.unload(val);
00086     ASSERT_EQ(val, values[i]);
00087   }
00088 }
00089 
00090 int
00091 main(int argc, char** argv)
00092 {
00093   g_argc = argc;
00094   g_argv = argv;
00095 
00096   testing::InitGoogleTest(&argc, argv);
00097   return RUN_ALL_TESTS();
00098 }


industrial_io_client
Author(s): Brian Gerkey
autogenerated on Mon Oct 6 2014 01:03:41