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