blob: 39400d4adde4c51bdca168546ec25c0b3786aa1b [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001/*
2 * (C) 2011-2012,2014 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2011-2012,2014 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#undef _GNU_SOURCE
20#define _GNU_SOURCE
21
22#include <osmocom/legacy_mgcp/mgcp.h>
23#include <osmocom/legacy_mgcp/vty.h>
24#include <osmocom/legacy_mgcp/mgcp_internal.h>
25
26#include <osmocom/core/application.h>
27#include <osmocom/core/talloc.h>
28#include <osmocom/core/utils.h>
29#include <string.h>
30#include <limits.h>
31#include <dlfcn.h>
32#include <time.h>
33#include <math.h>
34
35char *strline_r(char *str, char **saveptr);
36
37const char *strline_test_data =
38 "one CR\r"
39 "two CR\r"
40 "\r"
41 "one CRLF\r\n"
42 "two CRLF\r\n"
43 "\r\n"
44 "one LF\n"
45 "two LF\n"
46 "\n"
47 "mixed (4 lines)\r\r\n\n\r\n";
48
49#define EXPECTED_NUMBER_OF_LINES 13
50
51static void test_strline(void)
52{
53 char *save = NULL;
54 char *line;
55 char buf[2048];
56 int counter = 0;
57
58 osmo_strlcpy(buf, strline_test_data, sizeof(buf));
59
60 for (line = strline_r(buf, &save); line;
61 line = strline_r(NULL, &save)) {
62 printf("line: '%s'\n", line);
63 counter++;
64 }
65
66 OSMO_ASSERT(counter == EXPECTED_NUMBER_OF_LINES);
67}
68
69#define AUEP1 "AUEP 158663169 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n"
70#define AUEP1_RET "200 158663169 OK\r\n"
71#define AUEP2 "AUEP 18983213 ds/e1-2/1@172.16.6.66 MGCP 1.0\r\n"
72#define AUEP2_RET "500 18983213 FAIL\r\n"
73#define EMPTY "\r\n"
74#define EMPTY_RET NULL
75#define SHORT "CRCX \r\n"
76#define SHORT_RET "510 000000 FAIL\r\n"
77
78#define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@172.16.6.66 MGCP 1.0\r\n"
79#define MDCX_ERR_RET "510 18983213 FAIL\r\n"
80#define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n"
81#define MDCX_RET "400 18983214 FAIL\r\n"
82#define MDCX3 "MDCX 18983215 1@mgw MGCP 1.0\r\n"
83#define MDCX3_RET "200 18983215 OK\r\n" \
84 "I: 1\n" \
85 "\n" \
86 "v=0\r\n" \
87 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
88 "s=-\r\n" \
89 "c=IN IP4 0.0.0.0\r\n" \
90 "t=0 0\r\n" \
91 "m=audio 0 RTP/AVP 126\r\n" \
92 "a=rtpmap:126 AMR/8000\r\n" \
93 "a=ptime:20\r\n"
94#define MDCX3_FMTP_RET "200 18983215 OK\r\n" \
95 "I: 3\n" \
96 "\n" \
97 "v=0\r\n" \
98 "o=- 3 23 IN IP4 0.0.0.0\r\n" \
99 "s=-\r\n" \
100 "c=IN IP4 0.0.0.0\r\n" \
101 "t=0 0\r\n" \
102 "m=audio 0 RTP/AVP 126\r\n" \
103 "a=rtpmap:126 AMR/8000\r\n" \
104 "a=fmtp:126 0/1/2\r\n" \
105 "a=ptime:20\r\n"
106#define MDCX4 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \
107 "M: sendrecv\r" \
108 "C: 2\r\n" \
109 "I: 1\r\n" \
110 "L: p:20, a:AMR, nt:IN\r\n" \
111 "\n" \
112 "v=0\r\n" \
113 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
114 "c=IN IP4 0.0.0.0\r\n" \
115 "t=0 0\r\n" \
116 "m=audio 4441 RTP/AVP 99\r\n" \
117 "a=rtpmap:99 AMR/8000\r\n" \
118 "a=ptime:40\r\n"
119#define MDCX4_RET(Ident) "200 " Ident " OK\r\n" \
120 "I: 1\n" \
121 "\n" \
122 "v=0\r\n" \
123 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
124 "s=-\r\n" \
125 "c=IN IP4 0.0.0.0\r\n" \
126 "t=0 0\r\n" \
127 "m=audio 0 RTP/AVP 126\r\n" \
128 "a=rtpmap:126 AMR/8000\r\n" \
129 "a=ptime:20\r\n"
130
131#define MDCX4_PT1 "MDCX 18983217 1@mgw MGCP 1.0\r\n" \
132 "M: sendrecv\r" \
133 "C: 2\r\n" \
134 "I: 1\r\n" \
135 "L: p:20-40, a:AMR, nt:IN\r\n" \
136 "\n" \
137 "v=0\r\n" \
138 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
139 "c=IN IP4 0.0.0.0\r\n" \
140 "t=0 0\r\n" \
141 "m=audio 4441 RTP/AVP 99\r\n" \
142 "a=rtpmap:99 AMR/8000\r\n" \
143 "a=ptime:40\r\n"
144
145#define MDCX4_PT2 "MDCX 18983218 1@mgw MGCP 1.0\r\n" \
146 "M: sendrecv\r" \
147 "C: 2\r\n" \
148 "I: 1\r\n" \
149 "L: p:20-20, a:AMR, nt:IN\r\n" \
150 "\n" \
151 "v=0\r\n" \
152 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
153 "c=IN IP4 0.0.0.0\r\n" \
154 "t=0 0\r\n" \
155 "m=audio 4441 RTP/AVP 99\r\n" \
156 "a=rtpmap:99 AMR/8000\r\n" \
157 "a=ptime:40\r\n"
158
159#define MDCX4_PT3 "MDCX 18983219 1@mgw MGCP 1.0\r\n" \
160 "M: sendrecv\r" \
161 "C: 2\r\n" \
162 "I: 1\r\n" \
163 "L: a:AMR, nt:IN\r\n" \
164 "\n" \
165 "v=0\r\n" \
166 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
167 "c=IN IP4 0.0.0.0\r\n" \
168 "t=0 0\r\n" \
169 "m=audio 4441 RTP/AVP 99\r\n" \
170 "a=rtpmap:99 AMR/8000\r\n" \
171 "a=ptime:40\r\n"
172
173#define MDCX4_SO "MDCX 18983220 1@mgw MGCP 1.0\r\n" \
174 "M: sendonly\r" \
175 "C: 2\r\n" \
176 "I: 1\r\n" \
177 "L: p:20, a:AMR, nt:IN\r\n" \
178 "\n" \
179 "v=0\r\n" \
180 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
181 "c=IN IP4 0.0.0.0\r\n" \
182 "t=0 0\r\n" \
183 "m=audio 4441 RTP/AVP 99\r\n" \
184 "a=rtpmap:99 AMR/8000\r\n" \
185 "a=ptime:40\r\n"
186
187#define MDCX4_RO "MDCX 18983221 1@mgw MGCP 1.0\r\n" \
188 "M: recvonly\r" \
189 "C: 2\r\n" \
190 "I: 1\r\n" \
191 "L: p:20, a:AMR, nt:IN\r\n"
192
193#define SHORT2 "CRCX 1"
194#define SHORT2_RET "510 000000 FAIL\r\n"
195#define SHORT3 "CRCX 1 1@mgw"
196#define SHORT4 "CRCX 1 1@mgw MGCP"
197#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
198
199#define CRCX "CRCX 2 1@mgw MGCP 1.0\r\n" \
200 "M: recvonly\r\n" \
201 "C: 2\r\n" \
202 "X\r\n" \
203 "L: p:20\r\n" \
204 "\r\n" \
205 "v=0\r\n" \
206 "c=IN IP4 123.12.12.123\r\n" \
207 "m=audio 5904 RTP/AVP 97\r\n" \
208 "a=rtpmap:97 GSM-EFR/8000\r\n" \
209 "a=ptime:40\r\n"
210
211#define CRCX_RET "200 2 OK\r\n" \
212 "I: 1\n" \
213 "\n" \
214 "v=0\r\n" \
215 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
216 "s=-\r\n" \
217 "c=IN IP4 0.0.0.0\r\n" \
218 "t=0 0\r\n" \
219 "m=audio 0 RTP/AVP 126\r\n" \
220 "a=rtpmap:126 AMR/8000\r\n" \
221 "a=ptime:20\r\n"
222
223#define CRCX_RET_NO_RTPMAP "200 2 OK\r\n" \
224 "I: 1\n" \
225 "\n" \
226 "v=0\r\n" \
227 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
228 "s=-\r\n" \
229 "c=IN IP4 0.0.0.0\r\n" \
230 "t=0 0\r\n" \
231 "m=audio 0 RTP/AVP 126\r\n" \
232 "a=ptime:20\r\n"
233
234#define CRCX_FMTP_RET "200 2 OK\r\n" \
235 "I: 3\n" \
236 "\n" \
237 "v=0\r\n" \
238 "o=- 3 23 IN IP4 0.0.0.0\r\n" \
239 "s=-\r\n" \
240 "c=IN IP4 0.0.0.0\r\n" \
241 "t=0 0\r\n" \
242 "m=audio 0 RTP/AVP 126\r\n" \
243 "a=rtpmap:126 AMR/8000\r\n" \
244 "a=fmtp:126 0/1/2\r\n" \
245 "a=ptime:20\r\n"
246
247#define CRCX_ZYN "CRCX 2 1@mgw MGCP 1.0\r" \
248 "M: recvonly\r" \
249 "C: 2\r\r" \
250 "v=0\r" \
251 "c=IN IP4 123.12.12.123\r" \
252 "m=audio 5904 RTP/AVP 97\r" \
253 "a=rtpmap:97 GSM-EFR/8000\r"
254
255#define CRCX_ZYN_RET "200 2 OK\r\n" \
256 "I: 2\n" \
257 "\n" \
258 "v=0\r\n" \
259 "o=- 2 23 IN IP4 0.0.0.0\r\n" \
260 "s=-\r\n" \
261 "c=IN IP4 0.0.0.0\r\n" \
262 "t=0 0\r\n" \
263 "m=audio 0 RTP/AVP 126\r\n" \
264 "a=rtpmap:126 AMR/8000\r\n" \
265 "a=ptime:20\r\n"
266
267#define DLCX "DLCX 7 1@mgw MGCP 1.0\r\n" \
268 "C: 2\r\n"
269
270#define DLCX_RET "250 7 OK\r\n" \
Pau Espin Pedrolc3eed402018-02-20 12:04:28 +0100271 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n"
272
273 #define DLCX_RET_OSMUX DLCX_RET \
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200274 "X-Osmo-CP: EC TIS=0, TOS=0, TIR=0, TOR=0\r\n"
275
276#define RQNT "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
277 "X: B244F267488\r\n" \
278 "S: D/9\r\n"
279
280#define RQNT2 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
281 "X: ADD4F26746F\r\n" \
282 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
283
284#define RQNT1_RET "200 186908780 OK\r\n"
285#define RQNT2_RET "200 186908781 OK\r\n"
286
287#define PTYPE_IGNORE 0 /* == default initializer */
288#define PTYPE_NONE 128
289#define PTYPE_NYI PTYPE_NONE
290
291#define CRCX_MULT_1 "CRCX 2 1@mgw MGCP 1.0\r\n" \
292 "M: recvonly\r\n" \
293 "C: 2\r\n" \
294 "X\r\n" \
295 "L: p:20\r\n" \
296 "\r\n" \
297 "v=0\r\n" \
298 "c=IN IP4 123.12.12.123\r\n" \
299 "m=audio 5904 RTP/AVP 18 97\r\n"\
300 "a=rtpmap:18 G729/8000\r\n" \
301 "a=rtpmap:97 GSM-EFR/8000\r\n" \
302 "a=ptime:40\r\n"
303
304#define CRCX_MULT_2 "CRCX 2 2@mgw MGCP 1.0\r\n" \
305 "M: recvonly\r\n" \
306 "C: 2\r\n" \
307 "X\r\n" \
308 "L: p:20\r\n" \
309 "\r\n" \
310 "v=0\r\n" \
311 "c=IN IP4 123.12.12.123\r\n" \
312 "m=audio 5904 RTP/AVP 18 97 101\r\n"\
313 "a=rtpmap:18 G729/8000\r\n" \
314 "a=rtpmap:97 GSM-EFR/8000\r\n" \
315 "a=rtpmap:101 FOO/8000\r\n" \
316 "a=ptime:40\r\n"
317
318#define CRCX_MULT_3 "CRCX 2 3@mgw MGCP 1.0\r\n" \
319 "M: recvonly\r\n" \
320 "C: 2\r\n" \
321 "X\r\n" \
322 "L: p:20\r\n" \
323 "\r\n" \
324 "v=0\r\n" \
325 "c=IN IP4 123.12.12.123\r\n" \
326 "m=audio 5904 RTP/AVP\r\n" \
327 "a=rtpmap:18 G729/8000\r\n" \
328 "a=rtpmap:97 GSM-EFR/8000\r\n" \
329 "a=rtpmap:101 FOO/8000\r\n" \
330 "a=ptime:40\r\n"
331
332#define CRCX_MULT_4 "CRCX 2 4@mgw MGCP 1.0\r\n" \
333 "M: recvonly\r\n" \
334 "C: 2\r\n" \
335 "X\r\n" \
336 "L: p:20\r\n" \
337 "\r\n" \
338 "v=0\r\n" \
339 "c=IN IP4 123.12.12.123\r\n" \
340 "m=audio 5904 RTP/AVP 18\r\n" \
341 "a=rtpmap:18 G729/8000\r\n" \
342 "a=rtpmap:97 GSM-EFR/8000\r\n" \
343 "a=rtpmap:101 FOO/8000\r\n" \
344 "a=ptime:40\r\n"
345
346#define CRCX_MULT_GSM_EXACT \
347 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
348 "C: 1355c6041e\r\n" \
349 "I: 3\r\n" \
350 "L: p:20, a:GSM, nt:IN\r\n" \
351 "M: recvonly\r\n" \
352 "\r\n" \
353 "v=0\r\n" \
354 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
355 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
356 "t=0 0\r\nm=audio 29084 RTP/AVP 255 0 8 3 18 4 96 97 101\r\n" \
357 "a=rtpmap:0 PCMU/8000\r\n" \
358 "a=rtpmap:8 PCMA/8000\r\n" \
359 "a=rtpmap:3 gsm/8000\r\n" \
360 "a=rtpmap:18 G729/8000\r\n" \
361 "a=fmtp:18 annexb=no\r\n" \
362 "a=rtpmap:4 G723/8000\r\n" \
363 "a=rtpmap:96 iLBC/8000\r\n" \
364 "a=fmtp:96 mode=20\r\n" \
365 "a=rtpmap:97 iLBC/8000\r\n" \
366 "a=fmtp:97 mode=30\r\n" \
367 "a=rtpmap:101 telephone-event/8000\r\n" \
368 "a=fmtp:101 0-15\r\n" \
369 "a=recvonly\r\n"
370#define MDCX_NAT_DUMMY \
371 "MDCX 23 5@mgw MGCP 1.0\r\n" \
372 "C: 1355c6041e\r\n" \
373 "\r\n" \
374 "c=IN IP4 8.8.8.8\r\n" \
375 "m=audio 16434 RTP/AVP 255\r\n"
376
377
378struct mgcp_test {
379 const char *name;
380 const char *req;
381 const char *exp_resp;
382 int exp_net_ptype;
383 int exp_bts_ptype;
384
385 const char *extra_fmtp;
386};
387
388static const struct mgcp_test tests[] = {
389 { "AUEP1", AUEP1, AUEP1_RET },
390 { "AUEP2", AUEP2, AUEP2_RET },
391 { "MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET },
392 { "MDCX2", MDCX_UNALLOCATED, MDCX_RET },
393 { "CRCX", CRCX, CRCX_RET, 97, 126 },
394 { "MDCX3", MDCX3, MDCX3_RET, PTYPE_NONE, 126 },
395 { "MDCX4", MDCX4, MDCX4_RET("18983216"), 99, 126 },
396 { "MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99, 126 },
397 { "MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99, 126 },
398 { "MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99, 126 },
399 { "MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99, 126 },
400 { "MDCX4_RO", MDCX4_RO, MDCX4_RET("18983221"), PTYPE_IGNORE, 126 },
401 { "DLCX", DLCX, DLCX_RET, -1, -1 },
402 { "CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97, 126 },
403 { "EMPTY", EMPTY, EMPTY_RET },
404 { "SHORT1", SHORT, SHORT_RET },
405 { "SHORT2", SHORT2, SHORT2_RET },
406 { "SHORT3", SHORT3, SHORT2_RET },
407 { "SHORT4", SHORT4, SHORT2_RET },
408 { "RQNT1", RQNT, RQNT1_RET },
409 { "RQNT2", RQNT2, RQNT2_RET },
410 { "DLCX", DLCX, DLCX_RET, -1, -1 },
411 { "CRCX", CRCX, CRCX_FMTP_RET, 97, 126, .extra_fmtp = "a=fmtp:126 0/1/2" },
412 { "MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE, 126 , .extra_fmtp = "a=fmtp:126 0/1/2" },
413 { "DLCX", DLCX, DLCX_RET, -1, -1 , .extra_fmtp = "a=fmtp:126 0/1/2" },
414};
415
416static const struct mgcp_test retransmit[] = {
417 { "CRCX", CRCX, CRCX_RET },
418 { "RQNT1", RQNT, RQNT1_RET },
419 { "RQNT2", RQNT2, RQNT2_RET },
420 { "MDCX3", MDCX3, MDCX3_RET },
421 { "DLCX", DLCX, DLCX_RET },
422};
423
424static struct msgb *create_msg(const char *str)
425{
426 struct msgb *msg;
427
428 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
429 int len = sprintf((char *)msg->data, "%s", str);
430 msg->l2h = msgb_put(msg, len);
431 return msg;
432}
433
434static int last_endpoint = -1;
435
436static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
437 int state, const char *transactio_id)
438{
439 fprintf(stderr, "Policy CB got state %d on endpoint %d\n",
440 state, endpoint);
441 last_endpoint = endpoint;
442 return MGCP_POLICY_CONT;
443}
444
445#define MGCP_DUMMY_LOAD 0x23
446static int dummy_packets = 0;
447/* override and forward */
448ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
449 const struct sockaddr *dest_addr, socklen_t addrlen)
450{
451 typedef ssize_t (*sendto_t)(int, const void *, size_t, int,
452 const struct sockaddr *, socklen_t);
453 static sendto_t real_sendto = NULL;
454 uint32_t dest_host = htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
455 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
456
457 if (!real_sendto)
458 real_sendto = dlsym(RTLD_NEXT, "sendto");
459
460 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD ) {
461 fprintf(stderr, "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
462 dest_host, dest_port,
463 len, osmo_hexdump(buf, len));
464 dummy_packets += 1;
465 }
466
467 return real_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
468}
469
470static int64_t force_monotonic_time_us = -1;
471/* override and forward */
472int clock_gettime(clockid_t clk_id, struct timespec *tp)
473{
474 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
475 static clock_gettime_t real_clock_gettime = NULL;
476
477 if (!real_clock_gettime)
478 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
479
480 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
481 tp->tv_sec = force_monotonic_time_us / 1000000;
482 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
483 return 0;
484 }
485
486 return real_clock_gettime(clk_id, tp);
487}
488
489#define CONN_UNMODIFIED (0x1000)
490
491static void test_values(void)
492{
493 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200494 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200495
496 /* Check that LOOPBACK enables all output */
497 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200498 MGCP_CONN_RECV_SEND);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200499}
500
501
502static void test_messages(void)
503{
504 struct mgcp_config *cfg;
505 struct mgcp_endpoint *endp;
506 int i;
507
508 cfg = mgcp_config_alloc();
509
510 cfg->trunk.number_endpoints = 64;
511 mgcp_endpoints_allocate(&cfg->trunk);
512
513 cfg->policy_cb = mgcp_test_policy_cb;
514
515 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
516
517 /* reset endpoints */
518 for (i = 0; i < cfg->trunk.number_endpoints; i++) {
519 endp = &cfg->trunk.endpoints[i];
520 endp->net_end.codec.payload_type = PTYPE_NONE;
521 endp->net_end.packet_duration_ms = -1;
522
523 OSMO_ASSERT(endp->conn_mode == MGCP_CONN_NONE);
524 endp->conn_mode |= CONN_UNMODIFIED;
525 }
526
527 for (i = 0; i < ARRAY_SIZE(tests); i++) {
528 const struct mgcp_test *t = &tests[i];
529 struct msgb *inp;
530 struct msgb *msg;
531
532 printf("Testing %s\n", t->name);
533
534 last_endpoint = -1;
535 dummy_packets = 0;
536
537 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra, t->extra_fmtp);
538
539 inp = create_msg(t->req);
540 msg = mgcp_handle_message(cfg, inp);
541 msgb_free(inp);
542 if (!t->exp_resp) {
543 if (msg)
544 printf("%s failed '%s'\n", t->name, (char *) msg->data);
545 } else if (strcmp((char *) msg->data, t->exp_resp) != 0)
Neels Hofmeyr020e89b2017-09-19 14:28:14 +0200546 printf("%s failed.\nExpected:\n%s\nGot:\n%s\n",
547 t->name, t->exp_resp, (char *) msg->data);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200548 msgb_free(msg);
549
550 if (dummy_packets)
551 printf("Dummy packets: %d\n", dummy_packets);
552
553 if (last_endpoint != -1) {
554 endp = &cfg->trunk.endpoints[last_endpoint];
555
556 if (endp->net_end.packet_duration_ms != -1)
557 printf("Detected packet duration: %d\n",
558 endp->net_end.packet_duration_ms);
559 else
560 printf("Packet duration not set\n");
561 if (endp->local_options.pkt_period_min ||
562 endp->local_options.pkt_period_max)
563 printf("Requested packetetization period: "
564 "%d-%d\n",
565 endp->local_options.pkt_period_min,
566 endp->local_options.pkt_period_max);
567 else
568 printf("Requested packetization period not set\n");
569
570 if ((endp->conn_mode & CONN_UNMODIFIED) == 0) {
571 printf("Connection mode: %d:%s%s%s%s\n",
572 endp->conn_mode,
573 !endp->conn_mode ? " NONE" : "",
574 endp->conn_mode & MGCP_CONN_SEND_ONLY ?
575 " SEND" : "",
576 endp->conn_mode & MGCP_CONN_RECV_ONLY ?
577 " RECV" : "",
578 endp->conn_mode & MGCP_CONN_LOOPBACK &
579 ~MGCP_CONN_RECV_SEND ?
580 " LOOP" : "");
581 fprintf(stderr,
582 "BTS output %sabled, NET output %sabled\n",
583 endp->bts_end.output_enabled ? "en" : "dis",
584 endp->net_end.output_enabled ? "en" : "dis");
585 } else
586 printf("Connection mode not set\n");
587
588 OSMO_ASSERT(endp->net_end.output_enabled ==
589 (endp->conn_mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
590 OSMO_ASSERT(endp->bts_end.output_enabled ==
591 (endp->conn_mode & MGCP_CONN_RECV_ONLY ? 1 : 0));
592
593 endp->net_end.packet_duration_ms = -1;
594 endp->local_options.pkt_period_min = 0;
595 endp->local_options.pkt_period_max = 0;
596 endp->conn_mode |= CONN_UNMODIFIED;
597 }
598
599
600 /* Check detected payload type */
601 if (t->exp_net_ptype != PTYPE_IGNORE ||
602 t->exp_bts_ptype != PTYPE_IGNORE) {
603 OSMO_ASSERT(last_endpoint != -1);
604 endp = &cfg->trunk.endpoints[last_endpoint];
605
606 fprintf(stderr, "endpoint %d: "
607 "payload type BTS %d (exp %d), NET %d (exp %d)\n",
608 last_endpoint,
609 endp->bts_end.codec.payload_type, t->exp_bts_ptype,
610 endp->net_end.codec.payload_type, t->exp_net_ptype);
611
612 if (t->exp_bts_ptype != PTYPE_IGNORE)
613 OSMO_ASSERT(endp->bts_end.codec.payload_type ==
614 t->exp_bts_ptype);
615 if (t->exp_net_ptype != PTYPE_IGNORE)
616 OSMO_ASSERT(endp->net_end.codec.payload_type ==
617 t->exp_net_ptype);
618
619 /* Reset them again for next test */
620 endp->net_end.codec.payload_type = PTYPE_NONE;
621 }
622 }
623
624 talloc_free(cfg);
625}
626
627static void test_retransmission(void)
628{
629 struct mgcp_config *cfg;
630 int i;
631
632 cfg = mgcp_config_alloc();
633
634 cfg->trunk.number_endpoints = 64;
635 mgcp_endpoints_allocate(&cfg->trunk);
636
637 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
638
639 /* reset endpoints */
640 for (i = 0; i < cfg->trunk.number_endpoints; i++) {
641 struct mgcp_endpoint *endp;
642 endp = &cfg->trunk.endpoints[i];
643 endp->bts_end.packet_duration_ms = 20;
644 }
645
646 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
647 const struct mgcp_test *t = &retransmit[i];
648 struct msgb *inp;
649 struct msgb *msg;
650
651 printf("Testing %s\n", t->name);
652
653 inp = create_msg(t->req);
654 msg = mgcp_handle_message(cfg, inp);
655 msgb_free(inp);
656 if (strcmp((char *) msg->data, t->exp_resp) != 0)
657 printf("%s failed '%s'\n", t->name, (char *) msg->data);
658 msgb_free(msg);
659
660 /* Retransmit... */
661 printf("Re-transmitting %s\n", t->name);
662 inp = create_msg(t->req);
663 msg = mgcp_handle_message(cfg, inp);
664 msgb_free(inp);
665 if (strcmp((char *) msg->data, t->exp_resp) != 0)
666 printf("%s failed '%s'\n", t->name, (char *) msg->data);
667 msgb_free(msg);
668 }
669
670 talloc_free(cfg);
671}
672
673static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
674{
675 ptrdiff_t tone = _tone;
676 endp->cfg->data = (void *) tone;
677 return 0;
678}
679
680static void test_rqnt_cb(void)
681{
682 struct mgcp_config *cfg;
683 struct msgb *inp, *msg;
684
685 cfg = mgcp_config_alloc();
686 cfg->rqnt_cb = rqnt_cb;
687
688 cfg->trunk.number_endpoints = 64;
689 mgcp_endpoints_allocate(&cfg->trunk);
690
691 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
692
693 inp = create_msg(CRCX);
694 msgb_free(mgcp_handle_message(cfg, inp));
695 msgb_free(inp);
696
697 /* send the RQNT and check for the CB */
698 inp = create_msg(RQNT);
699 msg = mgcp_handle_message(cfg, inp);
700 if (strncmp((const char *) msg->l2h, "200", 3) != 0) {
701 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
702 abort();
703 }
704
705 if (cfg->data != (void *) '9') {
706 printf("FAILED: callback not called: %p\n", cfg->data);
707 abort();
708 }
709
710 msgb_free(msg);
711 msgb_free(inp);
712
713 inp = create_msg(DLCX);
714 msgb_free(mgcp_handle_message(cfg, inp));
715 msgb_free(inp);
716 talloc_free(cfg);
717}
718
719struct pl_test {
720 int cycles;
721 uint16_t base_seq;
722 uint16_t max_seq;
723 uint32_t packets;
724
725 uint32_t expected;
726 int loss;
727};
728
729static const struct pl_test pl_test_dat[] = {
730 /* basic.. just one package */
731 { .cycles = 0, .base_seq = 0, .max_seq = 0, .packets = 1, .expected = 1, .loss = 0},
732 /* some packages and a bit of loss */
733 { .cycles = 0, .base_seq = 0, .max_seq = 100, .packets = 100, .expected = 101, .loss = 1},
734 /* wrap around */
735 { .cycles = 1<<16, .base_seq = 0xffff, .max_seq = 2, .packets = 4, .expected = 4, .loss = 0},
736 /* min loss */
737 { .cycles = 0, .base_seq = 0, .max_seq = 0, .packets = UINT_MAX, .expected = 1, .loss = INT_MIN },
738 /* max loss, with wrap around on expected max */
739 { .cycles = INT_MAX, .base_seq = 0, .max_seq = UINT16_MAX, .packets = 0, .expected = ((uint32_t)(INT_MAX) + UINT16_MAX + 1), .loss = INT_MAX },
740};
741
742static void test_packet_loss_calc(void)
743{
744 int i;
745 printf("Testing packet loss calculation.\n");
746
747 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
748 uint32_t expected;
749 int loss;
750 struct mgcp_rtp_state state;
751 struct mgcp_rtp_end rtp;
752 memset(&state, 0, sizeof(state));
753 memset(&rtp, 0, sizeof(rtp));
754
755 state.stats_initialized = 1;
756 state.stats_base_seq = pl_test_dat[i].base_seq;
757 state.stats_max_seq = pl_test_dat[i].max_seq;
758 state.stats_cycles = pl_test_dat[i].cycles;
759
760 rtp.packets = pl_test_dat[i].packets;
761 mgcp_state_calc_loss(&state, &rtp, &expected, &loss);
762
763 if (loss != pl_test_dat[i].loss || expected != pl_test_dat[i].expected) {
764 printf("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
765 i, loss, pl_test_dat[i].loss,
766 expected, pl_test_dat[i].expected);
767 }
768 }
769}
770
771static void test_mgcp_stats(void)
772{
773 printf("Testing stat parsing\n");
774
775 uint32_t bps, bos, pr, _or, jitter;
776 struct msgb *msg;
777 int loss;
778 int rc;
779
780 msg = create_msg(DLCX_RET);
781 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
782 printf("Parsing result: %d\n", rc);
783 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0 || jitter != 0)
784 printf("FAIL: Parsing failed1.\n");
785 msgb_free(msg);
786
787 msg = create_msg("250 7 OK\r\nP: PS=10, OS=20, PR=30, OR=40, PL=-3, JI=40\r\n");
788 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
789 printf("Parsing result: %d\n", rc);
790 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3 || jitter != 40)
791 printf("FAIL: Parsing failed2.\n");
792 msgb_free(msg);
793}
794
795struct rtp_packet_info {
796 float txtime;
797 int len;
798 char *data;
799};
800
801struct rtp_packet_info test_rtp_packets1[] = {
802 /* RTP: SeqNo=0, TS=0 */
803 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
804 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
805 /* RTP: SeqNo=1, TS=160 */
806 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
807 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
808 /* RTP: SeqNo=2, TS=320 */
809 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
810 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
811 /* Repeat RTP timestamp: */
812 /* RTP: SeqNo=3, TS=320 */
813 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
814 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
815 /* RTP: SeqNo=4, TS=480 */
816 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
817 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
818 /* RTP: SeqNo=5, TS=640 */
819 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
820 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
821 /* Double skip RTP timestamp (delta = 2*160): */
822 /* RTP: SeqNo=6, TS=960 */
823 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
824 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
825 /* RTP: SeqNo=7, TS=1120 */
826 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
827 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
828 /* RTP: SeqNo=8, TS=1280 */
829 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
830 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
831 /* Non 20ms RTP timestamp (delta = 120): */
832 /* RTP: SeqNo=9, TS=1400 */
833 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
834 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
835 /* RTP: SeqNo=10, TS=1560 */
836 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
837 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
838 /* RTP: SeqNo=11, TS=1720 */
839 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
840 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
841 /* SSRC changed to 0x10203040, RTP timestamp jump */
842 /* RTP: SeqNo=12, TS=34688 */
843 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
844 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
845 /* RTP: SeqNo=13, TS=34848 */
846 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
847 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
848 /* RTP: SeqNo=14, TS=35008 */
849 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
850 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
851 /* Non 20ms RTP timestamp (delta = 120): */
852 /* RTP: SeqNo=15, TS=35128 */
853 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
854 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
855 /* RTP: SeqNo=16, TS=35288 */
856 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
857 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
858 /* RTP: SeqNo=17, TS=35448 */
859 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
860 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
861 /* SeqNo increment by 2, RTP timestamp delta = 320: */
862 /* RTP: SeqNo=19, TS=35768 */
863 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
864 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
865 /* RTP: SeqNo=20, TS=35928 */
866 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
867 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
868 /* RTP: SeqNo=21, TS=36088 */
869 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
870 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
871 /* Repeat last packet */
872 /* RTP: SeqNo=21, TS=36088 */
873 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
874 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
875 /* RTP: SeqNo=22, TS=36248 */
876 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
877 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
878 /* RTP: SeqNo=23, TS=36408 */
879 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
880 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
881 /* Don't increment SeqNo but increment timestamp by 160 */
882 /* RTP: SeqNo=23, TS=36568 */
883 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
884 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
885 /* RTP: SeqNo=24, TS=36728 */
886 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
887 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
888 /* RTP: SeqNo=25, TS=36888 */
889 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
890 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
891 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
892 * SeqNo jump */
893 /* RTP: SeqNo=1000, TS=160000 */
894 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
895 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
896 /* RTP: SeqNo=1001, TS=160160 */
897 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
898 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
899 /* RTP: SeqNo=1002, TS=160320 */
900 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
901 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
902};
903
904void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
905 struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr,
906 char *data, int len);
907
908static void test_packet_error_detection(int patch_ssrc, int patch_ts)
909{
910 int i;
911
912 struct mgcp_trunk_config trunk;
913 struct mgcp_endpoint endp;
914 struct mgcp_rtp_state state;
915 struct mgcp_rtp_end *rtp = &endp.net_end;
916 struct sockaddr_in addr = {0};
917 char buffer[4096];
918 uint32_t last_ssrc = 0;
919 uint32_t last_timestamp = 0;
920 uint32_t last_seqno = 0;
921 int last_in_ts_err_cnt = 0;
922 int last_out_ts_err_cnt = 0;
923
924 printf("Testing packet error detection%s%s.\n",
925 patch_ssrc ? ", patch SSRC" : "",
926 patch_ts ? ", patch timestamps" : "");
927
928 memset(&trunk, 0, sizeof(trunk));
929 memset(&endp, 0, sizeof(endp));
930 memset(&state, 0, sizeof(state));
931
932 trunk.number_endpoints = 1;
933 trunk.endpoints = &endp;
934 trunk.force_constant_ssrc = patch_ssrc;
935 trunk.force_aligned_timing = patch_ts;
936
937 endp.tcfg = &trunk;
938
939 mgcp_initialize_endp(&endp);
940
941 rtp->codec.payload_type = 98;
942
943 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
944 struct rtp_packet_info *info = test_rtp_packets1 + i;
945
946 force_monotonic_time_us = round(1000000.0 * info->txtime);
947
948 OSMO_ASSERT(info->len <= sizeof(buffer));
949 OSMO_ASSERT(info->len >= 0);
950 memmove(buffer, info->data, info->len);
951
952 mgcp_rtp_end_config(&endp, 1, rtp);
953
954 mgcp_patch_and_count(&endp, &state, rtp, &addr,
955 buffer, info->len);
956
957 if (state.out_stream.ssrc != last_ssrc) {
958 printf("Output SSRC changed to %08x\n",
959 state.out_stream.ssrc);
960 last_ssrc = state.out_stream.ssrc;
961 }
962
963 printf("In TS: %d, dTS: %d, Seq: %d\n",
964 state.in_stream.last_timestamp,
965 state.in_stream.last_tsdelta,
966 state.in_stream.last_seq);
967
968 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
969 "TS Err change: in %+d, out %+d\n",
970 state.out_stream.last_timestamp - last_timestamp,
971 state.out_stream.last_tsdelta,
972 state.out_stream.last_seq - last_seqno,
973 state.in_stream.err_ts_counter - last_in_ts_err_cnt,
974 state.out_stream.err_ts_counter - last_out_ts_err_cnt);
975
976 printf("Stats: Jitter = %u, Transit = %d\n",
977 mgcp_state_calc_jitter(&state), state.stats_transit);
978
979 last_in_ts_err_cnt = state.in_stream.err_ts_counter;
980 last_out_ts_err_cnt = state.out_stream.err_ts_counter;
981 last_timestamp = state.out_stream.last_timestamp;
982 last_seqno = state.out_stream.last_seq;
983 }
984
985 force_monotonic_time_us = -1;
986}
987
988static void test_multilple_codec(void)
989{
990 struct mgcp_config *cfg;
991 struct mgcp_endpoint *endp;
992 struct msgb *inp, *resp;
993 struct in_addr addr;
994
995 printf("Testing multiple payload types\n");
996
997 cfg = mgcp_config_alloc();
998 cfg->trunk.number_endpoints = 64;
999 mgcp_endpoints_allocate(&cfg->trunk);
1000 cfg->policy_cb = mgcp_test_policy_cb;
1001 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1002
1003 /* Allocate endpoint 1@mgw with two codecs */
1004 last_endpoint = -1;
1005 inp = create_msg(CRCX_MULT_1);
1006 resp = mgcp_handle_message(cfg, inp);
1007 msgb_free(inp);
1008 msgb_free(resp);
1009
1010 OSMO_ASSERT(last_endpoint == 1);
1011 endp = &cfg->trunk.endpoints[last_endpoint];
1012 OSMO_ASSERT(endp->net_end.codec.payload_type == 18);
1013 OSMO_ASSERT(endp->net_end.alt_codec.payload_type == 97);
1014
1015 /* Allocate 2@mgw with three codecs, last one ignored */
1016 last_endpoint = -1;
1017 inp = create_msg(CRCX_MULT_2);
1018 resp = mgcp_handle_message(cfg, inp);
1019 msgb_free(inp);
1020 msgb_free(resp);
1021
1022 OSMO_ASSERT(last_endpoint == 2);
1023 endp = &cfg->trunk.endpoints[last_endpoint];
1024 OSMO_ASSERT(endp->net_end.codec.payload_type == 18);
1025 OSMO_ASSERT(endp->net_end.alt_codec.payload_type == 97);
1026
1027 /* Allocate 3@mgw with no codecs, check for PT == -1 */
1028 last_endpoint = -1;
1029 inp = create_msg(CRCX_MULT_3);
1030 resp = mgcp_handle_message(cfg, inp);
1031 msgb_free(inp);
1032 msgb_free(resp);
1033
1034 OSMO_ASSERT(last_endpoint == 3);
1035 endp = &cfg->trunk.endpoints[last_endpoint];
1036 OSMO_ASSERT(endp->net_end.codec.payload_type == -1);
1037 OSMO_ASSERT(endp->net_end.alt_codec.payload_type == -1);
1038
1039 /* Allocate 4@mgw with a single codec */
1040 last_endpoint = -1;
1041 inp = create_msg(CRCX_MULT_4);
1042 resp = mgcp_handle_message(cfg, inp);
1043 msgb_free(inp);
1044 msgb_free(resp);
1045
1046 OSMO_ASSERT(last_endpoint == 4);
1047 endp = &cfg->trunk.endpoints[last_endpoint];
1048 OSMO_ASSERT(endp->net_end.codec.payload_type == 18);
1049 OSMO_ASSERT(endp->net_end.alt_codec.payload_type == -1);
1050
1051 /* Allocate 5@mgw at select GSM.. */
1052 last_endpoint = -1;
1053 inp = create_msg(CRCX_MULT_GSM_EXACT);
1054 talloc_free(cfg->trunk.audio_name);
1055 cfg->trunk.audio_name = "GSM/8000";
1056 cfg->trunk.no_audio_transcoding = 1;
1057 resp = mgcp_handle_message(cfg, inp);
1058 msgb_free(inp);
1059 msgb_free(resp);
1060
1061 OSMO_ASSERT(last_endpoint == 5);
1062 endp = &cfg->trunk.endpoints[last_endpoint];
1063 OSMO_ASSERT(endp->net_end.codec.payload_type == 3);
1064 OSMO_ASSERT(endp->net_end.alt_codec.payload_type == -1);
1065
1066 inp = create_msg(MDCX_NAT_DUMMY);
1067 last_endpoint = -1;
1068 resp = mgcp_handle_message(cfg, inp);
1069 msgb_free(inp);
1070 msgb_free(resp);
1071 OSMO_ASSERT(last_endpoint == 5);
1072 endp = &cfg->trunk.endpoints[last_endpoint];
1073 OSMO_ASSERT(endp->net_end.codec.payload_type == 3);
1074 OSMO_ASSERT(endp->net_end.alt_codec.payload_type == -1);
1075 OSMO_ASSERT(endp->net_end.rtp_port == htons(16434));
1076 memset(&addr, 0, sizeof(addr));
1077 inet_aton("8.8.8.8", &addr);
1078 OSMO_ASSERT(endp->net_end.addr.s_addr == addr.s_addr);
1079
1080 /* Check what happens without that flag */
1081
1082 /* Free the previous endpoint and the data ... */
1083 mgcp_release_endp(endp);
1084 talloc_free(endp->last_response);
1085 talloc_free(endp->last_trans);
1086 endp->last_response = endp->last_trans = NULL;
1087
1088 last_endpoint = -1;
1089 inp = create_msg(CRCX_MULT_GSM_EXACT);
1090 cfg->trunk.no_audio_transcoding = 0;
1091 resp = mgcp_handle_message(cfg, inp);
1092 msgb_free(inp);
1093 msgb_free(resp);
1094
1095 OSMO_ASSERT(last_endpoint == 5);
1096 endp = &cfg->trunk.endpoints[last_endpoint];
1097 OSMO_ASSERT(endp->net_end.codec.payload_type == 255);
1098 OSMO_ASSERT(endp->net_end.alt_codec.payload_type == 0);
1099
1100 talloc_free(cfg);
1101}
1102
1103static void test_no_cycle(void)
1104{
1105 struct mgcp_config *cfg;
1106 struct mgcp_endpoint *endp;
1107
1108 printf("Testing no sequence flow on initial packet\n");
1109
1110 cfg = mgcp_config_alloc();
1111 cfg->trunk.number_endpoints = 64;
1112 mgcp_endpoints_allocate(&cfg->trunk);
1113
1114 endp = &cfg->trunk.endpoints[1];
1115 OSMO_ASSERT(endp->net_state.stats_initialized == 0);
1116
1117 mgcp_rtp_annex_count(endp, &endp->net_state, 0, 0, 2342);
1118 OSMO_ASSERT(endp->net_state.stats_initialized == 1);
1119 OSMO_ASSERT(endp->net_state.stats_cycles == 0);
1120 OSMO_ASSERT(endp->net_state.stats_max_seq == 0);
1121
1122 mgcp_rtp_annex_count(endp, &endp->net_state, 1, 0, 2342);
1123 OSMO_ASSERT(endp->net_state.stats_initialized == 1);
1124 OSMO_ASSERT(endp->net_state.stats_cycles == 0);
1125 OSMO_ASSERT(endp->net_state.stats_max_seq == 1);
1126
1127 /* now jump.. */
1128 mgcp_rtp_annex_count(endp, &endp->net_state, UINT16_MAX, 0, 2342);
1129 OSMO_ASSERT(endp->net_state.stats_initialized == 1);
1130 OSMO_ASSERT(endp->net_state.stats_cycles == 0);
1131 OSMO_ASSERT(endp->net_state.stats_max_seq == UINT16_MAX);
1132
1133 /* and wrap */
1134 mgcp_rtp_annex_count(endp, &endp->net_state, 0, 0, 2342);
1135 OSMO_ASSERT(endp->net_state.stats_initialized == 1);
1136 OSMO_ASSERT(endp->net_state.stats_cycles == UINT16_MAX + 1);
1137 OSMO_ASSERT(endp->net_state.stats_max_seq == 0);
1138
1139 talloc_free(cfg);
1140}
1141
1142static void test_no_name(void)
1143{
1144 struct mgcp_config *cfg;
1145 struct mgcp_endpoint *endp;
1146 struct msgb *inp, *msg;
1147 int i;
1148
1149 printf("Testing no rtpmap name\n");
1150 cfg = mgcp_config_alloc();
1151
1152 cfg->trunk.number_endpoints = 64;
1153 cfg->trunk.audio_send_name = 0;
1154 mgcp_endpoints_allocate(&cfg->trunk);
1155
1156 cfg->policy_cb = mgcp_test_policy_cb;
1157
1158 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1159
1160 /* reset endpoints */
1161 for (i = 0; i < cfg->trunk.number_endpoints; i++) {
1162 endp = &cfg->trunk.endpoints[i];
1163 endp->net_end.codec.payload_type = PTYPE_NONE;
1164 endp->net_end.packet_duration_ms = -1;
1165
1166 OSMO_ASSERT(endp->conn_mode == MGCP_CONN_NONE);
1167 endp->conn_mode |= CONN_UNMODIFIED;
1168 }
1169
1170 inp = create_msg(CRCX);
1171 msg = mgcp_handle_message(cfg, inp);
1172 if (strcmp((char *) msg->data, CRCX_RET_NO_RTPMAP) != 0)
1173 printf("FAILED: there should not be a RTPMAP: %s\n",
1174 (char *) msg->data);
1175 msgb_free(inp);
1176 msgb_free(msg);
1177
1178 mgcp_release_endp(&cfg->trunk.endpoints[1]);
1179 talloc_free(cfg);
1180}
1181
1182static void test_osmux_cid(void)
1183{
1184 int id, i;
1185
1186 OSMO_ASSERT(osmux_used_cid() == 0);
1187 id = osmux_get_cid();
1188 OSMO_ASSERT(id == 0);
1189 OSMO_ASSERT(osmux_used_cid() == 1);
1190 osmux_put_cid(id);
1191 OSMO_ASSERT(osmux_used_cid() == 0);
1192
1193 for (i = 0; i < 256; ++i) {
1194 id = osmux_get_cid();
1195 OSMO_ASSERT(id == i);
1196 OSMO_ASSERT(osmux_used_cid() == i + 1);
1197 }
1198
1199 id = osmux_get_cid();
1200 OSMO_ASSERT(id == -1);
1201
1202 for (i = 0; i < 256; ++i)
1203 osmux_put_cid(i);
1204 OSMO_ASSERT(osmux_used_cid() == 0);
1205}
1206
1207static const struct log_info_cat log_categories[] = {
1208};
1209
1210const struct log_info log_info = {
1211 .cat = log_categories,
1212 .num_cat = ARRAY_SIZE(log_categories),
1213};
1214
1215int main(int argc, char **argv)
1216{
Neels Hofmeyr60f8e312018-03-30 23:01:07 +02001217 void *ctx = talloc_named_const(NULL, 0, "mgcp_test");
1218 void *msgb_ctx = msgb_talloc_ctx_init(ctx, 0);
1219 osmo_init_logging2(ctx, &log_info);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001220
1221 test_strline();
1222 test_values();
1223 test_messages();
1224 test_retransmission();
1225 test_packet_loss_calc();
1226 test_rqnt_cb();
1227 test_mgcp_stats();
1228 test_packet_error_detection(1, 0);
1229 test_packet_error_detection(0, 0);
1230 test_packet_error_detection(0, 1);
1231 test_packet_error_detection(1, 1);
1232 test_multilple_codec();
1233 test_no_cycle();
1234 test_no_name();
1235 test_osmux_cid();
1236
Neels Hofmeyr677f4ad2017-11-18 21:26:26 +01001237 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1238 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1239 talloc_free(msgb_ctx);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001240 printf("Done\n");
1241 return EXIT_SUCCESS;
1242}