6801ec2593bf959f4515c429f7c0aef4d1260816
[ric-app/admin.git] / test / unit_test_xapp.cc
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 /* Author : Ashwin Sridharan
21    Date   : Feb 2019
22 */
23
24 #define CATCH_CONFIG_MAIN
25 #include <catch2/catch.hpp>
26
27 #include <unistd.h>
28 #include <xapp_utils.hpp>
29
30 #define RMR_RT_FILE "/home/asridharan/projects/ric-xapp-dev/ric-app/adm-ctrl-xapp/test/uta_rtg.rt"
31 #define MESSAGE_SIZE 512
32
33 int num_recv_pkts = 0;
34 int num_tx_pkts = 0;
35 int num_dropped_pkts = 0;
36 int failed_tx = 0;
37 int num_ping_pkts = 0;
38 int num_pong_pkts = 0;
39 uint32_t NumPkts=1000;
40 unsigned char meid[32];
41
42 struct Test_message{
43   struct timespec ts;
44   char payload[MESSAGE_SIZE];
45 };
46
47 bool rcvd_pkts(rmr_mbuf_t *rcv_msg){
48   memset(meid, 0,  32);
49   rmr_get_meid(rcv_msg, meid);
50   num_recv_pkts++;
51   return false;
52 }
53
54 bool echo_into_space_pkts(rmr_mbuf_t *rcv_msg){
55   rcv_msg->mtype = 100; // some invalid type 
56   num_recv_pkts++;
57   return true;
58 }
59
60 void dropped_pkts(rmr_mbuf_t *send_msg){
61   std::cout <<"Error handler triggered " << std::endl;
62   num_dropped_pkts++;
63 }
64
65 bool ping_x(rmr_mbuf_t *rcv_msg){
66   rcv_msg->mtype = 101; //pong
67   num_ping_pkts++;
68   return true;
69 }
70
71 bool pong_x(rmr_mbuf_t *rcv_msg){
72   num_pong_pkts++;
73   return false;
74 }
75
76
77 TEST_CASE("Test xapp functionality", "[xapp]"){
78
79   // Test parameters
80   char app_name[128] = "Test App";
81   char port[16] = "tcp:4999";
82   int num_retries = 4;
83
84   
85   mdclog_attr_t *attr;
86   mdclog_attr_init(&attr);
87   mdclog_attr_set_ident(attr, "UNIT TEST XAPP FRAMEWORK");
88   mdclog_init(attr);
89   mdclog_level_set(MDCLOG_INFO);
90   mdclog_attr_destroy(attr);
91
92   SECTION("RMR illegal options"){
93     char illegal_port [] = "udp:-1";
94     REQUIRE_THROWS(XaPP(app_name, illegal_port, sizeof(Test_message), 1));
95     REQUIRE_THROWS(XaPP(app_name, port, RMR_BUFFER_SIZE + 1, 1));
96   }
97
98   SECTION("All good"){
99     REQUIRE_NOTHROW(XaPP(app_name, port, sizeof(Test_message), 1));
100   }
101    
102   SECTION("Simple + memory"){
103     XaPP check_xapp = XaPP(app_name, port, sizeof(Test_message), 1);
104     REQUIRE(check_xapp.getName() == std::string(app_name));
105   }
106   
107   SECTION("Configuration test"){  
108     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
109     test_xapp.set_num_retries(num_retries);
110     
111     REQUIRE(test_xapp.get_num_retries() == num_retries); 
112     REQUIRE(test_xapp.getStatus() == 1);
113     REQUIRE(test_xapp.get_num_retries() != 2);
114     REQUIRE(test_xapp.getStatus() == true);
115     REQUIRE(test_xapp.get_rmr_context() != NULL);
116     
117   }
118   
119
120   SECTION("Transmission test with start"){
121     num_recv_pkts = 0;
122     num_dropped_pkts = 0;
123     failed_tx = 0;
124     
125     // Instantiate and configure xAPP
126     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
127     test_xapp.set_num_retries(num_retries);
128
129     // Start receiver for test
130     test_xapp.Start(&rcvd_pkts);
131     sleep(1);
132   
133     // Test Send  normal message
134     Test_message my_message;
135     uint32_t i = 0;
136     for(i = 0; i < NumPkts; i++){
137       clock_gettime(CLOCK_REALTIME, &(my_message.ts));
138       snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
139       bool res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
140       if (!res){
141         failed_tx ++;
142       }
143       usleep(10);
144     
145     }
146     sleep(1);
147
148     test_xapp.Stop();  
149     std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
150     std::cout <<"Num attempts = " << test_xapp.get_Send_attempts() <<  " Num failed = " << test_xapp.get_Send_fails() << std::endl;
151     
152     REQUIRE(num_recv_pkts > 0);
153     REQUIRE(num_dropped_pkts == 0);
154     REQUIRE(failed_tx == 0);
155     REQUIRE(num_recv_pkts == NumPkts);
156   }
157
158 //   SECTION("Transmission test with start specific thread"){
159 //     num_recv_pkts = 0;
160 //     num_dropped_pkts = 0;
161 //     failed_tx = 0;
162     
163 //     // Instantiate and configure xAPP
164 //     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
165 //     test_xapp.set_num_retries(num_retries);
166     
167 //     // Start receiver for test
168 //     test_xapp.StartThread(&rcvd_pkts);
169 //     sleep(1);
170   
171 //     // Test Send  normal message
172 //     Test_message my_message;
173 //     uint32_t i = 0;
174 //     for(i = 0; i < NumPkts; i++){
175 //       clock_gettime(CLOCK_REALTIME, &(my_message.ts));
176 //       snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
177 //       bool res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
178 //       if (!res){
179 //      failed_tx ++;
180 //       }
181 //       usleep(10);
182     
183 //     }
184 //     sleep(1);
185
186 //     test_xapp.Stop();  
187 //     std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
188 //     std::cout <<"Num attempts = " << test_xapp.get_Send_attempts() <<  " Num failed = " << test_xapp.get_Send_fails() << std::endl;
189     
190 //     REQUIRE(num_recv_pkts > 0);
191 //     REQUIRE(num_dropped_pkts == 0);
192 //     REQUIRE(failed_tx == 0);
193 //     REQUIRE(num_recv_pkts == NumPkts);
194 //   }
195
196 //     SECTION("Transmission test error handler with start"){
197
198 //       num_recv_pkts = 0;
199 //       num_dropped_pkts = 0;
200 //       failed_tx = 0;
201       
202 //       // Instantiate and configure xAPP
203 //       XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
204 //       test_xapp.set_num_retries(num_retries);
205       
206 //       // Start receiver for test
207 //       test_xapp.Start(&echo_into_space_pkts, &dropped_pkts);
208 //       sleep(1);
209       
210 //       // Test Send  normal message
211 //       Test_message my_message;
212 //       uint32_t i = 0;
213 //       for(i = 0; i < NumPkts; i++){
214 //      clock_gettime(CLOCK_REALTIME, &(my_message.ts));
215 //      snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
216 //      bool res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
217 //      if (!res){
218 //        failed_tx ++;
219 //      }
220 //      usleep(10);
221         
222 //       }
223 //       sleep(1);
224       
225 //       test_xapp.Stop();  
226 //       std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
227 //       std::cout <<"Num attempts = " << test_xapp.get_Send_attempts() <<  " Num failed = " << test_xapp.get_Send_fails() << std::endl;
228       
229 //       REQUIRE(num_recv_pkts == NumPkts);
230 //       REQUIRE(num_dropped_pkts == NumPkts);
231 //       REQUIRE(failed_tx == 0);
232 //     }
233     
234     
235 //     SECTION("Transmission test error handler with start thread"){
236       
237 //       num_recv_pkts = 0;
238 //       num_dropped_pkts = 0;
239 //       failed_tx = 0;
240       
241 //       // Instantiate and configure xAPP
242 //       XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
243 //       test_xapp.set_num_retries(num_retries);
244       
245 //       // Start receiver for test
246 //       test_xapp.StartThread(&echo_into_space_pkts, &dropped_pkts);
247 //       sleep(1);
248       
249 //       // Test Send  normal message
250 //       Test_message my_message;
251 //       uint32_t i = 0;
252 //       for(i = 0; i < NumPkts; i++){
253 //      clock_gettime(CLOCK_REALTIME, &(my_message.ts));
254 //      snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
255 //      bool res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
256 //      if (!res){
257 //        failed_tx ++;
258 //      }
259 //      usleep(10);
260         
261 //       }
262 //       sleep(1);
263       
264 //       test_xapp.Stop();  
265 //       std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
266 //       std::cout <<"Num attempts = " << test_xapp.get_Send_attempts() <<  " Num failed = " << test_xapp.get_Send_fails() << std::endl;
267       
268 //       REQUIRE(num_recv_pkts == NumPkts);
269 //       REQUIRE(num_dropped_pkts == NumPkts);
270 //       REQUIRE(failed_tx == 0);
271
272
273 //     }
274
275 //     SECTION("Test ping pong : two xapps send to each other. "){
276
277 //       char ping_name[] = "ping";
278 //       char pong_name[] = "pong";
279 //       char ping_port[] = "tcp:4999";
280 //       char pong_port[] = "tcp:4998";
281       
282 //       // Instantiate  ping xAPP
283 //       XaPP ping_xapp = XaPP(ping_name, ping_port, sizeof(Test_message) , 1);
284
285
286 //       // Instantiate pong xapp
287 //       XaPP pong_xapp = XaPP(pong_name, pong_port, sizeof(Test_message) , 1);
288  
289 //       // Start receiver on ping
290 //       ping_xapp.StartThread(ping_x);
291 //       sleep(1);
292
293 //       // Start receiver on pong
294 //       pong_xapp.StartThread(&pong_x);
295       
296 //       // send messages to ping
297 //       Test_message my_message;
298 //       uint32_t i = 0;
299 //       for(i = 0; i < NumPkts; i++){
300 //      clock_gettime(CLOCK_REALTIME, &(my_message.ts));
301 //      snprintf(my_message.payload, MESSAGE_SIZE, "hello world %d", i);
302 //      bool res = ping_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
303 //      if (!res){
304 //        failed_tx ++;
305 //      }
306 //       }
307
308 //       sleep(1);
309 //       ping_xapp.Stop();
310 //       pong_xapp.Stop();
311
312 //       REQUIRE(failed_tx == 0);
313 //       REQUIRE(num_ping_pkts == NumPkts);
314 //       REQUIRE(num_pong_pkts == NumPkts);
315
316 //     }
317 // }
318
319
320 // TEST_CASE(" Test out various  transmission methods ..", "1"){
321
322 //   // Test parameters
323 //   char app_name[128] = "Test App";
324 //   char port[16] = "tcp:4999";
325   
326   
327 //   mdclog_attr_t *attr;
328 //   mdclog_attr_init(&attr);
329 //   mdclog_attr_set_ident(attr, "UNIT TEST XAPP FRAMEWORK");
330 //   mdclog_init(attr);
331 //   mdclog_level_set(MDCLOG_INFO);
332 //   mdclog_attr_destroy(attr);
333 //   bool res;
334 //   unsigned char my_meid[32] = "ABC123";
335 //   Test_message my_message;
336   
337 //   SECTION("Test if message larger than allowed"){
338 //     num_recv_pkts = 0;
339 //     num_dropped_pkts = 0;
340 //     failed_tx = 0;
341     
342 //     // Instantiate and configure xAPP
343 //     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
344     
345 //     // Start receiver for test
346 //     test_xapp.StartThread(&rcvd_pkts);
347 //     sleep(1);
348     
349 //     // Test sending a message of size larger than allowed
350 //     res = test_xapp.Send(0, RMR_BUFFER_SIZE + 100, (void *)(&my_message));
351 //     test_xapp.Stop();
352   
353 //     REQUIRE(res == false);
354 //   }
355   
356 //   SECTION("Test with tlv"){
357 //     num_recv_pkts = 0;
358 //     num_dropped_pkts = 0;
359 //     failed_tx = 0;
360     
361 //     // Instantiate and configure xAPP
362 //     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) , 1);
363
364 //     // Start receiver for test
365 //     test_xapp.StartThread(&rcvd_pkts);
366 //     sleep(1);
367     
368 //     // Test Send  with tlv
369 //     clock_gettime(CLOCK_REALTIME, &(my_message.ts));
370 //     snprintf(my_message.payload, MESSAGE_SIZE, "hello world");
371 //     res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message));
372 //     sleep(1);
373     
374 //     // Test send with tlv and meid
375 //     res = test_xapp.Send(0, sizeof(Test_message), (void *) (&my_message), my_meid);
376 //     sleep(1);
377     
378 //     test_xapp.Stop();
379     
380 //     REQUIRE(num_recv_pkts == 2);
381 //     REQUIRE(!strcmp((const char *)meid, (const char *)my_meid));
382 //   }  
383 }