4e4b670b809fff01093719e8f3e42fe59fc4f9a7
[ric-app/admin.git] / test / unit_test_xapp.cc
1 /*
2     // basically just set size to be larger than allowed even if actual 
3     // message is small
4 ==================================================================================
5
6         Copyright (c) 2018-2019 AT&T Intellectual Property.
7
8    Licensed under the Apache License, Version 2.0 (the "License");
9    you may not use this file except in compliance with the License.
10    You may obtain a copy of the License at
11
12        http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19 ==================================================================================
20 */
21
22 /* Author : Ashwin Sridharan
23    Date   : Feb 2019
24 */
25
26 #define CATCH_CONFIG_MAIN
27 #include <catch2/catch.hpp>
28
29 #include <unistd.h>
30 #include <xapp_utils.hpp>
31
32 #define MESSAGE_SIZE 512
33
34 int num_recv_pkts = 0;
35 int num_tx_pkts = 0;
36 int num_dropped_pkts = 0;
37 int failed_tx = 0;
38 int num_ping_pkts = 0;
39 int num_pong_pkts = 0;
40 uint32_t NumPkts=1000;
41 unsigned char meid[32];
42
43 struct Test_message{
44   struct timespec ts;
45   char payload[MESSAGE_SIZE];
46 };
47
48 bool rcvd_pkts(rmr_mbuf_t *rcv_msg){
49   memset(meid, 0,  32);
50   rmr_get_meid(rcv_msg, meid);
51   num_recv_pkts++;
52   return false;
53 }
54
55 bool echo_into_space_pkts(rmr_mbuf_t *rcv_msg){
56   rcv_msg->mtype = 100; // some invalid type 
57   num_recv_pkts++;
58   return true;
59 }
60
61 void dropped_pkts(rmr_mbuf_t *send_msg){
62   std::cout <<"Error handler triggered " << std::endl;
63   num_dropped_pkts++;
64 }
65
66
67 bool pong_x(rmr_mbuf_t *rcv_msg){
68   rcv_msg->mtype = 102; //ping port
69   num_ping_pkts++;
70   return true;
71 }
72
73 bool ping_recv(rmr_mbuf_t * rcv_msg){
74   num_pong_pkts++;
75   return false;
76 }
77
78
79 TEST_CASE("Test xapp functionality", "[xapp]"){
80
81   // Test parameters
82   char app_name[128] = "Test App";
83   char port[16] = "tcp:4999";
84
85   init_logger("UNIT_TEST_XAPP", MDCLOG_INFO);
86   
87   SECTION("Illegal buffer size"){
88     REQUIRE_THROWS(XaPP(app_name, port, RMR_BUFFER_SIZE + 1));
89   }
90   
91   SECTION("All good"){
92     REQUIRE_NOTHROW(XaPP(app_name, port, sizeof(Test_message)));
93   }
94    
95   SECTION("Simple + memory"){
96     XaPP check_xapp = XaPP(app_name, port, sizeof(Test_message));
97     REQUIRE(check_xapp.get_name() == std::string(app_name));
98   }
99   
100   SECTION("Configuration test"){  
101     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message));
102     REQUIRE(test_xapp.get_status() == true);
103     REQUIRE(test_xapp.get_rmr_context() != NULL);
104     
105   }
106   
107
108   SECTION("Transmission test with start"){
109     num_recv_pkts = 0;
110     num_dropped_pkts = 0;
111     failed_tx = 0;
112     
113     // Instantiate and configure xAPP
114     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message));
115
116     // Start receiver for test
117     test_xapp.StartThread(rcvd_pkts);
118     sleep(1);
119   
120     // Test Send  normal message
121     Test_message my_message;
122     uint32_t i = 0;
123     for(i = 0; i < NumPkts; i++){
124       clock_gettime(CLOCK_REALTIME, &(my_message.ts));
125       snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
126       bool res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
127       if (!res){
128         failed_tx ++;
129       }
130       usleep(10);
131     
132     }
133     sleep(1);
134     REQUIRE(test_xapp.get_num_active_threads() == 1);
135     test_xapp.Stop();
136     REQUIRE(test_xapp.get_num_active_threads() == 0);
137     
138     std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
139
140     
141     REQUIRE(num_recv_pkts > 0);
142     REQUIRE(num_dropped_pkts == 0);
143     REQUIRE(failed_tx == 0);
144     REQUIRE(num_recv_pkts == NumPkts);
145   }
146
147     SECTION("Transmission test error handler with start"){
148
149       num_recv_pkts = 0;
150       num_dropped_pkts = 0;
151       failed_tx = 0;
152       
153       // Instantiate and configure xAPP
154       XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
155
156       
157       // Start receiver for test
158       test_xapp.StartThread(&echo_into_space_pkts, &dropped_pkts);
159       sleep(1);
160       
161       // Test Send  normal message
162       Test_message my_message;
163       uint32_t i = 0;
164       for(i = 0; i < NumPkts; i++){
165         clock_gettime(CLOCK_REALTIME, &(my_message.ts));
166         snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
167         bool res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
168         if (!res){
169           failed_tx ++;
170         }
171         usleep(10);
172         
173       }
174       sleep(1);
175       
176       test_xapp.Stop();  
177       std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
178
179       
180       REQUIRE(num_recv_pkts == NumPkts);
181       REQUIRE(num_dropped_pkts == NumPkts);
182       REQUIRE(failed_tx == 0);
183     }
184     
185     
186     SECTION("Transmission test error handler with start thread"){
187       
188       num_recv_pkts = 0;
189       num_dropped_pkts = 0;
190       failed_tx = 0;
191       
192       // Instantiate and configure xAPP
193       XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
194
195       
196       // Start receiver for test
197       test_xapp.StartThread(&echo_into_space_pkts, &dropped_pkts);
198       sleep(1);
199       
200       // Test Send  normal message
201       Test_message my_message;
202       uint32_t i = 0;
203       for(i = 0; i < NumPkts; i++){
204         clock_gettime(CLOCK_REALTIME, &(my_message.ts));
205         snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
206         bool res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
207         if (!res){
208           failed_tx ++;
209         }
210         usleep(10);
211         
212       }
213       sleep(1);
214       
215       test_xapp.Stop();  
216       std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
217
218       
219       REQUIRE(num_recv_pkts == NumPkts);
220       REQUIRE(num_dropped_pkts == NumPkts);
221       REQUIRE(failed_tx == 0);
222
223
224     }
225
226     SECTION("Test ping pong : two xapps send to each other. "){
227
228       char ping_name[] = "ping";
229       char pong_name[] = "pong";
230       char ping_port[] = "tcp:4999";
231       char pong_port[] = "tcp:4998";
232       
233       // Instantiate  ping xAPP
234       XaPP ping_xapp = XaPP(ping_name, ping_port, sizeof(Test_message) );
235
236
237       // Instantiate pong xapp
238       XaPP pong_xapp = XaPP(pong_name, pong_port, sizeof(Test_message) );
239  
240       // Start receiver on ping
241       ping_xapp.StartThread(ping_recv);
242       sleep(1);
243
244       // Start receiver on pong
245       pong_xapp.StartThread(pong_x);
246       
247       // send messages from ping to pong
248       Test_message my_message;
249       uint32_t i = 0;
250       for(i = 0; i < NumPkts; i++){
251         clock_gettime(CLOCK_REALTIME, &(my_message.ts));
252         snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
253         bool res = ping_xapp.Send(101, sizeof(Test_message), (void *) (&my_message));
254         if (!res){
255           failed_tx ++;
256         }
257       }
258
259       sleep(1);
260       pong_xapp.Stop();
261
262       REQUIRE(failed_tx == 0);
263       REQUIRE(num_ping_pkts == NumPkts);
264       REQUIRE(num_pong_pkts == NumPkts);
265
266
267       ping_xapp.Stop();
268
269     }
270 }
271
272
273 TEST_CASE(" Test out various  transmission methods ..", "1"){
274
275   // Test parameters
276   char app_name[128] = "Test App";
277   char port[16] = "tcp:4999";
278   
279   init_logger("UNIT_TEST_XAPP", MDCLOG_INFO);
280   
281   bool res;
282   unsigned char my_meid[32] = "ABC123";
283   Test_message my_message;
284   
285   SECTION("Test if message larger than allowed"){
286     num_recv_pkts = 0;
287     num_dropped_pkts = 0;
288     failed_tx = 0;
289     
290     // Instantiate and configure xAPP
291     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
292     
293     // Start receiver for test
294     test_xapp.StartThread(&rcvd_pkts);
295     sleep(1);
296     
297     // Test sending a message of size larger than allowed
298     // basically just set size to be larger than allowed even if actual 
299     // message is small
300     res = test_xapp.Send(102, RMR_BUFFER_SIZE + 100, (void *)(&my_message));
301     REQUIRE(res == false);
302
303     res = test_xapp.Send(102, RMR_BUFFER_SIZE + 100, (void *)(&my_message), my_meid);
304     REQUIRE(res == false);
305     test_xapp.Stop();
306   
307
308   }
309   
310   SECTION("Test with tlv"){
311     num_recv_pkts = 0;
312     num_dropped_pkts = 0;
313     failed_tx = 0;
314     
315     // Instantiate and configure xAPP
316     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
317
318     // Start receiver for test
319     test_xapp.StartThread(&rcvd_pkts);
320     sleep(1);
321     
322     // Test Send  with tlv
323     clock_gettime(CLOCK_REALTIME, &(my_message.ts));
324     snprintf(my_message.payload, MESSAGE_SIZE, "hello world");
325     res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message));
326     sleep(1);
327     
328     // Test send with tlv and meid
329     res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message), my_meid);
330     sleep(1);
331     
332     test_xapp.Stop();
333     
334     REQUIRE(num_recv_pkts == 2);
335     REQUIRE(!strcmp((const char *)meid, (const char *)my_meid));
336   }  
337 }