e1d47f12d4660673e30d063596ddce67d9adabbd
[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 MESSAGE_SIZE 512
31
32 int num_recv_pkts = 0;
33 int num_tx_pkts = 0;
34 int num_dropped_pkts = 0;
35 int failed_tx = 0;
36 int num_ping_pkts = 0;
37 int num_pong_pkts = 0;
38 uint32_t NumPkts=1000;
39 unsigned char meid[32];
40
41 struct Test_message{
42   struct timespec ts;
43   char payload[MESSAGE_SIZE];
44 };
45
46 bool rcvd_pkts(rmr_mbuf_t *rcv_msg){
47   memset(meid, 0,  32);
48   rmr_get_meid(rcv_msg, meid);
49   num_recv_pkts++;
50   return false;
51 }
52
53 bool echo_into_space_pkts(rmr_mbuf_t *rcv_msg){
54   rcv_msg->mtype = 100; // some invalid type 
55   num_recv_pkts++;
56   return true;
57 }
58
59 void dropped_pkts(rmr_mbuf_t *send_msg){
60   std::cout <<"Error handler triggered " << std::endl;
61   num_dropped_pkts++;
62 }
63
64 bool pong_a1(rmr_mbuf_t *rcv_msg){
65   rcv_msg->mtype = A1_POLICY_RESP; 
66   num_ping_pkts++;
67   return true;
68 }
69
70
71 bool pong_x(rmr_mbuf_t *rcv_msg){
72   rcv_msg->mtype = 102; //ping port
73   num_ping_pkts++;
74   return true;
75 }
76
77 bool ping_recv(rmr_mbuf_t * rcv_msg){
78   num_pong_pkts++;
79   return false;
80 }
81
82
83 TEST_CASE("Test xapp functionality", "[xapp]"){
84
85   // Test parameters
86   char app_name[128] = "Test App";
87   char port[16] = "tcp:4999";
88
89   init_logger("UNIT_TEST_XAPP", MDCLOG_INFO);
90   
91   SECTION("Illegal buffer size"){
92     REQUIRE_THROWS(XaPP(app_name, port, RMR_BUFFER_SIZE + 1));
93   }
94   
95   SECTION("All good"){
96     REQUIRE_NOTHROW(XaPP(app_name, port, sizeof(Test_message)));
97   }
98    
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));
102   }
103   
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);
108     
109   }
110   
111
112   SECTION("Transmission test with start"){
113     num_recv_pkts = 0;
114     num_dropped_pkts = 0;
115     failed_tx = 0;
116     
117     // Instantiate and configure xAPP
118     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message));
119
120     // Start receiver for test
121     test_xapp.StartThread(rcvd_pkts);
122     sleep(1);
123   
124     // Test Send  normal message
125     Test_message my_message;
126     uint32_t i = 0;
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));
131       if (!res){
132         failed_tx ++;
133       }
134       usleep(10);
135     
136     }
137     sleep(1);
138     REQUIRE(test_xapp.get_num_active_threads() == 1);
139     test_xapp.Stop();
140     REQUIRE(test_xapp.get_num_active_threads() == 0);
141     
142     std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
143
144     
145     REQUIRE(num_recv_pkts > 0);
146     REQUIRE(num_dropped_pkts == 0);
147     REQUIRE(failed_tx == 0);
148     REQUIRE(num_recv_pkts == NumPkts);
149   }
150
151     SECTION("Transmission test error handler with start"){
152
153       num_recv_pkts = 0;
154       num_dropped_pkts = 0;
155       failed_tx = 0;
156       
157       // Instantiate and configure xAPP
158       XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
159
160       
161       // Start receiver for test
162       test_xapp.StartThread(&echo_into_space_pkts, &dropped_pkts);
163       sleep(1);
164       
165       // Test Send  normal message
166       Test_message my_message;
167       uint32_t i = 0;
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));
172         if (!res){
173           failed_tx ++;
174         }
175         usleep(10);
176         
177       }
178       sleep(1);
179       
180       test_xapp.Stop();  
181       std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
182
183       
184       REQUIRE(num_recv_pkts == NumPkts);
185       REQUIRE(num_dropped_pkts == NumPkts);
186       REQUIRE(failed_tx == 0);
187     }
188     
189     
190     SECTION("Transmission test error handler with start thread"){
191       
192       num_recv_pkts = 0;
193       num_dropped_pkts = 0;
194       failed_tx = 0;
195       
196       // Instantiate and configure xAPP
197       XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
198
199       
200       // Start receiver for test
201       test_xapp.StartThread(&echo_into_space_pkts, &dropped_pkts);
202       sleep(1);
203       
204       // Test Send  normal message
205       Test_message my_message;
206       uint32_t i = 0;
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));
211         if (!res){
212           failed_tx ++;
213         }
214         usleep(10);
215         
216       }
217       sleep(1);
218       
219       test_xapp.Stop();  
220       std::cout <<"Num Packets Sent = " << NumPkts << " Received packets = " << num_recv_pkts << " Dropped packets = " << num_dropped_pkts << " Failed sends = "<< failed_tx << std::endl;
221
222       
223       REQUIRE(num_recv_pkts == NumPkts);
224       REQUIRE(num_dropped_pkts == NumPkts);
225       REQUIRE(failed_tx == 0);
226
227
228     }
229
230     SECTION("Test ping pong : two xapps send to each other. "){
231
232       char ping_name[] = "ping";
233       char pong_name[] = "pong";
234       char ping_port[] = "tcp:4999";
235       char pong_port[] = "tcp:4998";
236       
237       // Instantiate  ping xAPP
238       XaPP ping_xapp = XaPP(ping_name, ping_port, sizeof(Test_message) );
239
240
241       // Instantiate pong xapp
242       XaPP pong_xapp = XaPP(pong_name, pong_port, sizeof(Test_message) );
243  
244       // Start receiver on ping
245       ping_xapp.StartThread(ping_recv);
246       sleep(1);
247
248       // Start receiver on pong
249       pong_xapp.StartThread(pong_x);
250       
251       // send messages from ping to pong
252       Test_message my_message;
253       uint32_t i = 0;
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));
258         if (!res){
259           failed_tx ++;
260         }
261       }
262
263       sleep(1);
264       pong_xapp.Stop();
265
266       REQUIRE(failed_tx == 0);
267       REQUIRE(num_ping_pkts == NumPkts);
268       REQUIRE(num_pong_pkts == NumPkts);
269
270
271       // Re-run experiment but now with A1 message type when
272       // ping responds
273       pong_xapp.StartThread(pong_a1);
274       sleep(1);
275
276       failed_tx = 0;
277       num_ping_pkts = 0;
278       num_pong_pkts = 0;
279
280       
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));
285         if (!res){
286           failed_tx ++;
287         }
288       }
289
290       sleep(1);
291       ping_xapp.Stop();
292       pong_xapp.Stop();
293
294       std::cerr <<"Pong received ping pkts = " << num_ping_pkts << " Ping received a1 packets = " << num_pong_pkts << " failures = " << failed_tx << std::endl;
295       
296       REQUIRE(num_ping_pkts >=  NumPkts);
297       REQUIRE(num_pong_pkts >=  NumPkts);
298       REQUIRE(failed_tx == 0);
299
300
301       
302      
303     }
304 }
305
306
307 TEST_CASE(" Test out various  transmission methods ..", "1"){
308
309   // Test parameters
310   char app_name[128] = "Test App";
311   char port[16] = "tcp:4999";
312   
313   init_logger("UNIT_TEST_XAPP", MDCLOG_INFO);
314   
315   bool res;
316   unsigned char my_meid[32] = "ABC123";
317   Test_message my_message;
318   
319   SECTION("Test if message larger than allowed"){
320     num_recv_pkts = 0;
321     num_dropped_pkts = 0;
322     failed_tx = 0;
323     
324     // Instantiate and configure xAPP
325     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
326     
327     // Start receiver for test
328     test_xapp.StartThread(&rcvd_pkts);
329     sleep(1);
330     
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);
334
335     res = test_xapp.Send(102, RMR_BUFFER_SIZE + 100, (void *)(&my_message), "id");
336     REQUIRE(res == false);
337     test_xapp.Stop();
338   
339
340   }
341   
342   SECTION("Test with tlv"){
343     num_recv_pkts = 0;
344     num_dropped_pkts = 0;
345     failed_tx = 0;
346     
347     // Instantiate and configure xAPP
348     XaPP test_xapp = XaPP(app_name, port, sizeof(Test_message) );
349
350     // Start receiver for test
351     test_xapp.StartThread(&rcvd_pkts);
352     sleep(1);
353     
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));
358     sleep(1);
359     
360     // Test send with tlv and meid
361     res = test_xapp.Send(102, sizeof(Test_message), (void *) (&my_message), my_meid);
362     sleep(1);
363     
364     test_xapp.Stop();
365     
366     REQUIRE(num_recv_pkts == 2);
367     REQUIRE(!strcmp((const char *)meid, (const char *)my_meid));
368   }  
369 }