00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "simple_message/simple_message.h"
00033 #include "simple_message/byte_array.h"
00034 #include "simple_message/shared_types.h"
00035 #include "simple_message/smpl_msg_connection.h"
00036 #include "simple_message/socket/udp_client.h"
00037 #include "simple_message/socket/udp_server.h"
00038 #include "simple_message/socket/tcp_client.h"
00039 #include "simple_message/socket/tcp_server.h"
00040 #include "simple_message/ping_message.h"
00041 #include "simple_message/ping_handler.h"
00042 #include "simple_message/messages/joint_message.h"
00043 #include "simple_message/joint_data.h"
00044 #include "simple_message/message_manager.h"
00045 #include "simple_message/simple_comms_fault_handler.h"
00046 #include "simple_message/joint_traj_pt.h"
00047 #include "simple_message/messages/joint_traj_pt_message.h"
00048 #include "simple_message/typed_message.h"
00049 #include "simple_message/joint_traj.h"
00050
00051 #include <gtest/gtest.h>
00052
00053
00054
00055 #include <pthread.h>
00056
00057 using namespace industrial::simple_message;
00058 using namespace industrial::byte_array;
00059 using namespace industrial::shared_types;
00060 using namespace industrial::smpl_msg_connection;
00061 using namespace industrial::udp_socket;
00062 using namespace industrial::udp_client;
00063 using namespace industrial::udp_server;
00064 using namespace industrial::tcp_socket;
00065 using namespace industrial::tcp_client;
00066 using namespace industrial::tcp_server;
00067 using namespace industrial::ping_message;
00068 using namespace industrial::ping_handler;
00069 using namespace industrial::joint_data;
00070 using namespace industrial::joint_message;
00071 using namespace industrial::message_manager;
00072 using namespace industrial::simple_comms_fault_handler;
00073 using namespace industrial::joint_traj_pt;
00074 using namespace industrial::joint_traj_pt_message;
00075 using namespace industrial::typed_message;
00076 using namespace industrial::joint_traj;
00077
00078
00079
00080
00081
00082 TEST(ByteArraySuite, init)
00083 {
00084
00085 const shared_int SIZE = 100;
00086
00087 ByteArray bytes;
00088 shared_int TOO_BIG = bytes.getMaxBufferSize()+1;
00089
00090 char bigBuffer[TOO_BIG];
00091 char buffer[SIZE];
00092
00093
00094 EXPECT_TRUE(bytes.init(&buffer[0], SIZE));
00095 EXPECT_EQ((shared_int)bytes.getBufferSize(), SIZE);
00096
00097
00098
00099 EXPECT_FALSE(bytes.init(&bigBuffer[0], TOO_BIG));
00100 }
00101
00102 TEST(ByteArraySuite, loading)
00103 {
00104 const shared_int SIZE = 100;
00105 char buffer[SIZE];
00106
00107 ByteArray bytes;
00108 ByteArray empty;
00109
00110 ASSERT_TRUE(bytes.init(&buffer[0], SIZE));
00111
00112 shared_bool bIN = true, bOUT = false;
00113 shared_int iIN = 999, iOUT = 0;
00114 shared_real rIN = 9999.9999, rOUT = 0;
00115
00116
00117 EXPECT_TRUE(bytes.load(bIN));
00118 EXPECT_EQ(bytes.getBufferSize(), SIZE+sizeof(shared_bool));
00119 EXPECT_TRUE(bytes.unload(bOUT));
00120 EXPECT_EQ((shared_int)bytes.getBufferSize(), SIZE);
00121 EXPECT_EQ(bOUT, bIN);
00122
00123
00124 EXPECT_TRUE(bytes.load(iIN));
00125 EXPECT_EQ(bytes.getBufferSize(), SIZE+sizeof(shared_int));
00126 EXPECT_TRUE(bytes.unload(iOUT));
00127 EXPECT_EQ((shared_int)bytes.getBufferSize(), SIZE);
00128 EXPECT_EQ(iOUT, iIN);
00129
00130
00131 EXPECT_TRUE(bytes.load(rIN));
00132 EXPECT_EQ(bytes.getBufferSize(), SIZE+sizeof(shared_real));
00133 EXPECT_TRUE(bytes.unload(rOUT));
00134 EXPECT_EQ((shared_int)bytes.getBufferSize(), SIZE);
00135 EXPECT_EQ(rOUT, rIN);
00136
00137
00138 EXPECT_TRUE(empty.load(bIN));
00139 EXPECT_EQ(empty.getBufferSize(), sizeof(shared_bool));
00140 EXPECT_TRUE(empty.unload(bOUT));
00141 EXPECT_EQ((int)empty.getBufferSize(), 0);
00142 EXPECT_EQ(bOUT, bIN);
00143
00144
00145 rOUT = 0.0;
00146 iOUT = 0;
00147 EXPECT_TRUE(empty.load(rIN));
00148 EXPECT_EQ(empty.getBufferSize(), sizeof(shared_real));
00149 EXPECT_TRUE(empty.load(iIN));
00150 EXPECT_EQ(empty.getBufferSize(), sizeof(shared_real)+sizeof(shared_int));
00151 EXPECT_TRUE(empty.unloadFront(rOUT));
00152 EXPECT_EQ(rOUT, rIN);
00153 EXPECT_TRUE(empty.unload(iOUT));
00154 EXPECT_EQ((int)empty.getBufferSize(), 0);
00155 EXPECT_EQ(iOUT, iIN);
00156 }
00157
00158 TEST(ByteArraySuite, byteSwapping)
00159 {
00160 if(ByteArray::isByteSwapEnabled())
00161 {
00162 ASSERT_TRUE(ByteArray::isByteSwapEnabled());
00163
00164 ByteArray swapped;
00165 unsigned char buffer[] = {
00166 0x00, 0x00, 0x00, 0x38,
00167 0x00, 0x00, 0x00, 0x0a,
00168 0x00, 0x00, 0x00, 0x01,
00169
00170 0x3e, 0x81, 0x32, 0x64,
00171 0x3f, 0x30, 0x4b, 0x75,
00172 0x3f, 0xa8, 0x9d, 0xd2,
00173 0x3f, 0x85, 0x93, 0xdd,
00174 0xbf, 0xf4, 0x8c, 0xc5,
00175
00176 };
00177 const unsigned int bufferLength = 32;
00178 shared_int tempInt;
00179 shared_real tempReal;
00180
00181 swapped.init((const char*) buffer, bufferLength);
00182 ASSERT_EQ(swapped.getBufferSize(), bufferLength);
00183
00184 ASSERT_TRUE(swapped.unload(tempReal));
00185 EXPECT_FLOAT_EQ(tempReal, -1.9105459451675415);
00186
00187 ASSERT_TRUE(swapped.unload(tempReal));
00188 EXPECT_FLOAT_EQ(tempReal, 1.0435749292373657);
00189
00190 ASSERT_TRUE(swapped.unload(tempReal));
00191 EXPECT_FLOAT_EQ(tempReal, 1.3173162937164307);
00192
00193 ASSERT_TRUE(swapped.unload(tempReal));
00194 EXPECT_FLOAT_EQ(tempReal, 0.68865138292312622);
00195
00196 ASSERT_TRUE(swapped.unload(tempReal));
00197 EXPECT_FLOAT_EQ(tempReal, 0.25233757495880127);
00198
00199 ASSERT_TRUE(swapped.unload(tempInt));
00200 EXPECT_EQ(tempInt, 1);
00201
00202 ASSERT_TRUE(swapped.unload(tempInt));
00203 EXPECT_EQ(tempInt, 10);
00204
00205 ASSERT_TRUE(swapped.unload(tempInt));
00206 EXPECT_EQ(tempInt, 56);
00207
00208 ASSERT_EQ(swapped.getBufferSize(), 0);
00209 }
00210
00211 }
00212
00213 TEST(ByteArraySuite, copy)
00214 {
00215
00216 const shared_int SIZE = 100;
00217 char buffer[SIZE];
00218
00219
00220 ByteArray copyFrom;
00221 ByteArray copyTo;
00222 ByteArray tooBig;
00223
00224
00225 shared_int TOO_BIG = tooBig.getMaxBufferSize()-1;
00226 char bigBuffer[TOO_BIG];
00227
00228 EXPECT_TRUE(copyFrom.init(&buffer[0], SIZE));
00229 EXPECT_TRUE(copyTo.load(copyFrom));
00230 EXPECT_EQ((shared_int)copyTo.getBufferSize(), SIZE);
00231 EXPECT_TRUE(copyTo.load(copyFrom));
00232 EXPECT_EQ((shared_int)copyTo.getBufferSize(), 2*SIZE);
00233
00234
00235 EXPECT_TRUE(tooBig.init(&bigBuffer[0], TOO_BIG));
00236 EXPECT_FALSE(copyTo.load(tooBig));
00237
00238 EXPECT_EQ((shared_int)copyTo.getBufferSize(), 2*SIZE);
00239 }
00240
00241
00242 class TestTcpClient : public TcpClient
00243 {
00244 public:
00245 bool sendBytes(ByteArray & buffer)
00246 {
00247 return TcpClient::sendBytes(buffer);
00248 };
00249 };
00250 class TestTcpServer : public TcpServer
00251 {
00252 public:
00253 bool receiveBytes(ByteArray & buffer, shared_int num_bytes)
00254 {
00255 return TcpServer::receiveBytes(buffer, num_bytes);
00256 }
00257 };
00258 TEST(SocketSuite, read)
00259 {
00260 const int tcpPort = TEST_PORT_BASE;
00261 char ipAddr[] = "127.0.0.1";
00262
00263 TestTcpClient tcpClient;
00264 TestTcpServer tcpServer;
00265 ByteArray send, recv;
00266 shared_int DATA = 99;
00267 shared_int TWO_INTS = 2 * sizeof(shared_int);
00268 shared_int ONE_INTS = 1 * sizeof(shared_int);
00269
00270
00271 ASSERT_TRUE(tcpServer.init(tcpPort));
00272
00273
00274 ASSERT_TRUE(tcpClient.init(&ipAddr[0], tcpPort));
00275 ASSERT_TRUE(tcpClient.makeConnect());
00276
00277 ASSERT_TRUE(tcpServer.makeConnect());
00278
00279 ASSERT_TRUE(send.load(DATA));
00280
00281
00282 ASSERT_TRUE(tcpClient.sendBytes(send));
00283 ASSERT_TRUE(tcpClient.sendBytes(send));
00284 ASSERT_TRUE(tcpServer.receiveBytes(recv, TWO_INTS));
00285 ASSERT_EQ(TWO_INTS, recv.getBufferSize());
00286
00287
00288
00289 ASSERT_TRUE(tcpClient.sendBytes(send));
00290 ASSERT_TRUE(tcpClient.sendBytes(send));
00291 ASSERT_TRUE(tcpClient.sendBytes(send));
00292 ASSERT_TRUE(tcpServer.receiveBytes(recv, TWO_INTS));
00293 ASSERT_EQ(TWO_INTS, recv.getBufferSize());
00294 ASSERT_TRUE(tcpServer.receiveBytes(recv, ONE_INTS));
00295 ASSERT_EQ(ONE_INTS, recv.getBufferSize());
00296 }
00297
00298
00299
00300 void*
00301 spinSender(void* arg)
00302 {
00303 TestTcpClient* client = (TestTcpClient*)arg;
00304 ByteArray send;
00305 const int DATA = 256;
00306
00307 send.load(DATA);
00308
00309 while(true)
00310 {
00311 client->sendBytes(send);
00312 sleep(2);
00313 }
00314 }
00315
00316 TEST(SocketSuite, splitPackets)
00317 {
00318 const int tcpPort = TEST_PORT_BASE + 1;
00319 char ipAddr[] = "127.0.0.1";
00320 const int RECV_LENGTH = 64;
00321
00322 TestTcpClient tcpClient;
00323 TestTcpServer tcpServer;
00324 ByteArray recv;
00325
00326 ASSERT_TRUE(tcpServer.init(tcpPort));
00327
00328
00329 ASSERT_TRUE(tcpClient.init(&ipAddr[0], tcpPort));
00330 ASSERT_TRUE(tcpClient.makeConnect());
00331
00332 ASSERT_TRUE(tcpServer.makeConnect());
00333
00334 pthread_t senderThrd;
00335 pthread_create(&senderThrd, NULL, spinSender, &tcpClient);
00336
00337 ASSERT_TRUE(tcpServer.receiveBytes(recv, RECV_LENGTH));
00338 ASSERT_EQ(RECV_LENGTH, recv.getBufferSize());
00339
00340 pthread_cancel(senderThrd);
00341 pthread_join(senderThrd, NULL);
00342 }
00343
00344
00345 TEST(SimpleMessageSuite, init)
00346 {
00347 SimpleMessage msg;
00348 ByteArray bytes;
00349
00350
00351 EXPECT_TRUE(msg.init(StandardMsgTypes::PING, CommTypes::TOPIC, ReplyTypes::INVALID, bytes));
00352 EXPECT_TRUE(msg.init(StandardMsgTypes::PING, CommTypes::SERVICE_REQUEST,ReplyTypes::INVALID, bytes));
00353 EXPECT_TRUE(msg.init(StandardMsgTypes::PING, CommTypes::SERVICE_REPLY,ReplyTypes::SUCCESS, bytes));
00354 EXPECT_TRUE(msg.init(StandardMsgTypes::PING, CommTypes::SERVICE_REPLY,ReplyTypes::FAILURE, bytes));
00355
00356
00357 EXPECT_FALSE(msg.init(StandardMsgTypes::INVALID, CommTypes::INVALID,ReplyTypes::INVALID, bytes));
00358
00359
00360 EXPECT_FALSE(msg.init(StandardMsgTypes::PING, CommTypes::SERVICE_REQUEST,ReplyTypes::SUCCESS, bytes));
00361 EXPECT_FALSE(msg.init(StandardMsgTypes::PING, CommTypes::SERVICE_REQUEST,ReplyTypes::FAILURE, bytes));
00362 }
00363
00364 TEST(PingMessageSuite, init)
00365 {
00366 PingMessage ping;
00367 SimpleMessage msg;
00368
00369 EXPECT_FALSE(ping.init(msg));
00370 ping.init();
00371 EXPECT_EQ(StandardMsgTypes::PING, ping.getMessageType());
00372
00373 ping = PingMessage();
00374 ASSERT_TRUE(msg.init(StandardMsgTypes::PING, CommTypes::SERVICE_REQUEST, ReplyTypes::INVALID));
00375 EXPECT_TRUE(ping.init(msg));
00376 EXPECT_EQ(StandardMsgTypes::PING, ping.getMessageType());
00377 }
00378
00379 TEST(PingMessageSuite, toMessage)
00380 {
00381 PingMessage ping;
00382 SimpleMessage msg;
00383
00384 ping.init();
00385
00386 ASSERT_TRUE(ping.toReply(msg, ReplyTypes::SUCCESS));
00387 EXPECT_EQ(StandardMsgTypes::PING, msg.getMessageType());
00388 EXPECT_EQ(CommTypes::SERVICE_REPLY, msg.getCommType());
00389 EXPECT_EQ(ReplyTypes::SUCCESS, msg.getReplyCode());
00390
00391 ASSERT_TRUE(ping.toRequest(msg));
00392 EXPECT_EQ(StandardMsgTypes::PING, msg.getMessageType());
00393 EXPECT_EQ(CommTypes::SERVICE_REQUEST, msg.getCommType());
00394 EXPECT_EQ(ReplyTypes::INVALID, msg.getReplyCode());
00395
00396 EXPECT_FALSE(ping.toTopic(msg));
00397
00398 }
00399
00400 TEST(PingHandlerSuite, init)
00401 {
00402 PingHandler handler;
00403 UdpClient udp;
00404
00405 ASSERT_TRUE(handler.init(&udp));
00406 EXPECT_EQ(StandardMsgTypes::PING, handler.getMsgType());
00407
00408 EXPECT_FALSE(handler.init(NULL));
00409
00410 }
00411
00412 TEST(MessageManagerSuite, init)
00413 {
00414 MessageManager manager;
00415 UdpClient udp;
00416
00417 EXPECT_TRUE(manager.init(&udp));
00418 EXPECT_FALSE(manager.init(NULL));
00419
00420 }
00421
00422 TEST(MessageManagerSuite, addHandler)
00423 {
00424 MessageManager manager;
00425 UdpClient udp;
00426 PingHandler handler;
00427
00428 EXPECT_EQ(0, (int)manager.getNumHandlers());
00429
00430 ASSERT_TRUE(manager.init(&udp));
00431 EXPECT_EQ(1, (int)manager.getNumHandlers());
00432 EXPECT_FALSE(manager.add(NULL));
00433
00434 ASSERT_TRUE(handler.init(&udp));
00435 EXPECT_FALSE(manager.add(&handler));
00436 }
00437
00438
00439
00440 void*
00441 spinFunc(void* arg)
00442 {
00443 MessageManager* mgr = (MessageManager*)arg;
00444 mgr->spin();
00445 return NULL;
00446 }
00447
00448 TEST(DISABLED_MessageManagerSuite, udp)
00449 {
00450 const int udpPort = TEST_PORT_BASE + 100;
00451 char ipAddr[] = "127.0.0.1";
00452
00453 UdpClient* udpClient = new UdpClient();
00454 UdpServer udpServer;
00455 SimpleMessage pingRequest, pingReply;
00456 MessageManager udpManager;
00457
00458 ASSERT_TRUE(pingRequest.init(StandardMsgTypes::PING, CommTypes::SERVICE_REQUEST, ReplyTypes::INVALID));
00459
00460
00461
00462 ASSERT_TRUE(udpServer.init(udpPort));
00463 ASSERT_TRUE(udpManager.init(&udpServer));
00464
00465 pthread_t udpSrvThrd;
00466 pthread_create(&udpSrvThrd, NULL, spinFunc, &udpManager);
00467
00468
00469 ASSERT_TRUE(udpClient->init(&ipAddr[0], udpPort));
00470 ASSERT_TRUE(udpClient->makeConnect());
00471 ASSERT_TRUE(udpClient->sendMsg(pingRequest));
00472 ASSERT_TRUE(udpClient->receiveMsg(pingReply));
00473 ASSERT_TRUE(udpClient->sendAndReceiveMsg(pingRequest, pingReply));
00474
00475
00476 delete udpClient;
00477 udpClient = new UdpClient();
00478 ASSERT_TRUE(udpClient->init(&ipAddr[0], udpPort));
00479 ASSERT_TRUE(udpClient->makeConnect());
00480 ASSERT_TRUE(udpClient->sendAndReceiveMsg(pingRequest, pingReply));
00481
00482 pthread_cancel(udpSrvThrd);
00483 pthread_join(udpSrvThrd, NULL);
00484 }
00485
00486 TEST(DISABLED_MessageManagerSuite, tcp)
00487 {
00488 const int tcpPort = TEST_PORT_BASE + 101;
00489 char ipAddr[] = "127.0.0.1";
00490
00491 TcpClient* tcpClient = new TcpClient();
00492 TcpServer tcpServer;
00493 SimpleMessage pingRequest, pingReply;
00494 MessageManager tcpManager;
00495
00496
00497 ros::AsyncSpinner spinner(0);
00498 spinner.start();
00499
00500 ASSERT_TRUE(pingRequest.init(StandardMsgTypes::PING, CommTypes::SERVICE_REQUEST, ReplyTypes::INVALID));
00501
00502
00503
00504
00505 ASSERT_TRUE(tcpServer.init(tcpPort));
00506
00507
00508 ASSERT_TRUE(tcpClient->init(&ipAddr[0], tcpPort));
00509 ASSERT_TRUE(tcpClient->makeConnect());
00510
00511
00512 ASSERT_TRUE(tcpServer.makeConnect());
00513 ASSERT_TRUE(tcpManager.init(&tcpServer));
00514
00515
00516
00517
00518
00519 pthread_t tcpSrvThrd;
00520 pthread_create(&tcpSrvThrd, NULL, spinFunc, &tcpManager);
00521
00522
00523 ASSERT_TRUE(tcpClient->sendMsg(pingRequest));
00524 ASSERT_TRUE(tcpClient->receiveMsg(pingReply));
00525 ASSERT_TRUE(tcpClient->sendAndReceiveMsg(pingRequest, pingReply));
00526
00527
00528
00529 delete tcpClient;
00530 sleep(10);
00531 tcpClient = new TcpClient();
00532
00533 ASSERT_TRUE(tcpClient->init(&ipAddr[0], tcpPort));
00534 ASSERT_TRUE(tcpClient->makeConnect());
00535 ASSERT_TRUE(tcpClient->sendAndReceiveMsg(pingRequest, pingReply));
00536
00537 pthread_cancel(tcpSrvThrd);
00538 pthread_join(tcpSrvThrd, NULL);
00539
00540 delete tcpClient;
00541 }
00542
00543
00544 int main(int argc, char **argv)
00545 {
00546 ros::init(argc, argv, "test");
00547 testing::InitGoogleTest(&argc, argv);
00548 return RUN_ALL_TESTS();
00549 }
00550