2 ==================================================================================
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
20 /* Author : Ashwin Sridharan
24 #define CATCH_CONFIG_MAIN
25 #include <catch2/catch.hpp>
28 #include <xapp_utils.hpp>
30 #define MESSAGE_SIZE 512
32 int num_recv_pkts = 0;
34 int num_dropped_pkts = 0;
36 int num_ping_pkts = 0;
37 int num_pong_pkts = 0;
38 uint32_t NumPkts=1000;
39 unsigned char meid[32];
43 char payload[MESSAGE_SIZE];
46 bool rcvd_pkts(rmr_mbuf_t *rcv_msg){
48 rmr_get_meid(rcv_msg, meid);
53 bool echo_into_space_pkts(rmr_mbuf_t *rcv_msg){
54 rcv_msg->mtype = 100; // some invalid type
59 void dropped_pkts(rmr_mbuf_t *send_msg){
60 std::cout <<"Error handler triggered " << std::endl;
64 bool pong_a1(rmr_mbuf_t *rcv_msg){
65 rcv_msg->mtype = A1_POLICY_RESP;
71 bool pong_x(rmr_mbuf_t *rcv_msg){
72 rcv_msg->mtype = 102; //ping port
77 bool ping_recv(rmr_mbuf_t * rcv_msg){
83 TEST_CASE("Test xapp functionality", "[xapp]"){
86 char app_name[128] = "Test App";
87 char port[16] = "tcp:4999";
89 init_logger("UNIT_TEST_XAPP", MDCLOG_INFO);
91 SECTION("Illegal buffer size"){
92 REQUIRE_THROWS(XaPP(app_name, port, RMR_BUFFER_SIZE + 1));
96 REQUIRE_NOTHROW(XaPP(app_name, port, sizeof(Test_message)));
99 SECTION("Simple + memory"){
100 XaPP check_xapp = XaPP(app_name, port, sizeof(Test_message));
101 REQUIRE(check_xapp.get_name() == std::string(app_name));
104 SECTION("Configuration test"){
105 XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message));
106 REQUIRE(test_xapp.get_status() == true);
107 REQUIRE(test_xapp.get_rmr_context() != NULL);
112 SECTION("Transmission test with start"){
114 num_dropped_pkts = 0;
117 // Instantiate and configure xAPP
118 XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message));
120 // Start receiver for test
121 test_xapp.StartThread(rcvd_pkts);
124 // Test Send normal message
125 Test_message my_message;
127 for(i = 0; i < NumPkts; i++){
128 clock_gettime(CLOCK_REALTIME, &(my_message.ts));
129 snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
130 bool res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
138 REQUIRE(test_xapp.get_num_active_threads() == 1);
140 REQUIRE(test_xapp.get_num_active_threads() == 0);
142 std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
145 REQUIRE(num_recv_pkts > 0);
146 REQUIRE(num_dropped_pkts == 0);
147 REQUIRE(failed_tx == 0);
148 REQUIRE(num_recv_pkts == NumPkts);
151 SECTION("Transmission test error handler with start"){
154 num_dropped_pkts = 0;
157 // Instantiate and configure xAPP
158 XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
161 // Start receiver for test
162 test_xapp.StartThread(&echo_into_space_pkts, &dropped_pkts);
165 // Test Send normal message
166 Test_message my_message;
168 for(i = 0; i < NumPkts; i++){
169 clock_gettime(CLOCK_REALTIME, &(my_message.ts));
170 snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
171 bool res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
181 std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
184 REQUIRE(num_recv_pkts == NumPkts);
185 REQUIRE(num_dropped_pkts == NumPkts);
186 REQUIRE(failed_tx == 0);
190 SECTION("Transmission test error handler with start thread"){
193 num_dropped_pkts = 0;
196 // Instantiate and configure xAPP
197 XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
200 // Start receiver for test
201 test_xapp.StartThread(&echo_into_space_pkts, &dropped_pkts);
204 // Test Send normal message
205 Test_message my_message;
207 for(i = 0; i < NumPkts; i++){
208 clock_gettime(CLOCK_REALTIME, &(my_message.ts));
209 snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
210 bool res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
220 std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
223 REQUIRE(num_recv_pkts == NumPkts);
224 REQUIRE(num_dropped_pkts == NumPkts);
225 REQUIRE(failed_tx == 0);
230 SECTION("Test ping pong : two xapps send to each other. "){
232 char ping_name[] = "ping";
233 char pong_name[] = "pong";
234 char ping_port[] = "tcp:4999";
235 char pong_port[] = "tcp:4998";
237 // Instantiate ping xAPP
238 XaPP ping_xapp = XaPP(ping_name, ping_port, sizeof(Test_message) );
241 // Instantiate pong xapp
242 XaPP pong_xapp = XaPP(pong_name, pong_port, sizeof(Test_message) );
244 // Start receiver on ping
245 ping_xapp.StartThread(ping_recv);
248 // Start receiver on pong
249 pong_xapp.StartThread(pong_x);
251 // send messages from ping to pong
252 Test_message my_message;
254 for(i = 0; i < NumPkts; i++){
255 clock_gettime(CLOCK_REALTIME, &(my_message.ts));
256 snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
257 bool res = ping_xapp.Send(101, sizeof(Test_message), (void *) (&my_message));
266 REQUIRE(failed_tx == 0);
267 REQUIRE(num_ping_pkts == NumPkts);
268 REQUIRE(num_pong_pkts == NumPkts);
271 // Re-run experiment but now with A1 message type when
273 pong_xapp.StartThread(pong_a1);
281 for(i = 0; i < NumPkts; i++){
282 clock_gettime(CLOCK_REALTIME, &(my_message.ts));
283 snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
284 bool res = ping_xapp.Send(101, sizeof(Test_message), (void *) (&my_message));
294 std::cerr <<"Pong received ping pkts = " << num_ping_pkts << " Ping received a1 packets = " << num_pong_pkts << " failures = " << failed_tx << std::endl;
296 REQUIRE(num_ping_pkts >= NumPkts);
297 REQUIRE(num_pong_pkts >= NumPkts);
298 REQUIRE(failed_tx == 0);
307 TEST_CASE(" Test out various transmission methods ..", "1"){
310 char app_name[128] = "Test App";
311 char port[16] = "tcp:4999";
313 init_logger("UNIT_TEST_XAPP", MDCLOG_INFO);
316 unsigned char my_meid[32] = "ABC123";
317 Test_message my_message;
319 SECTION("Test if message larger than allowed"){
321 num_dropped_pkts = 0;
324 // Instantiate and configure xAPP
325 XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
327 // Start receiver for test
328 test_xapp.StartThread(&rcvd_pkts);
331 // Test sending a message of size larger than allowed
332 res = test_xapp.Send(102, RMR_BUFFER_SIZE + 100, (void *)(&my_message));
333 REQUIRE(res == false);
335 res = test_xapp.Send(102, RMR_BUFFER_SIZE + 100, (void *)(&my_message), "id");
336 REQUIRE(res == false);
342 SECTION("Test with tlv"){
344 num_dropped_pkts = 0;
347 // Instantiate and configure xAPP
348 XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
350 // Start receiver for test
351 test_xapp.StartThread(&rcvd_pkts);
354 // Test Send with tlv
355 clock_gettime(CLOCK_REALTIME, &(my_message.ts));
356 snprintf(my_message.payload, MESSAGE_SIZE, "hello world");
357 res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
360 // Test send with tlv and meid
361 res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message), my_meid);
366 REQUIRE(num_recv_pkts == 2);
367 REQUIRE(!strcmp((const char *)meid, (const char *)my_meid));