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