blob: 7d976da3b4aaabdd7f1ea7721c51e1caf0ec242e [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +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
Philipp Maier87bd9be2017-08-22 16:35:41 +020022#include <osmocom/mgcp/mgcp.h>
23#include <osmocom/mgcp/vty.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020024#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020025#include <osmocom/mgcp/mgcp_internal.h>
26#include <osmocom/mgcp/mgcp_stat.h>
27#include <osmocom/mgcp/mgcp_msg.h>
28#include <osmocom/mgcp/mgcp_ep.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020029
30#include <osmocom/core/application.h>
31#include <osmocom/core/talloc.h>
32#include <osmocom/core/utils.h>
33#include <string.h>
34#include <limits.h>
35#include <dlfcn.h>
36#include <time.h>
37#include <math.h>
38
39char *strline_r(char *str, char **saveptr);
40
41const char *strline_test_data =
42 "one CR\r"
43 "two CR\r"
44 "\r"
45 "one CRLF\r\n"
46 "two CRLF\r\n"
Philipp Maier87bd9be2017-08-22 16:35:41 +020047 "\r\n" "one LF\n" "two LF\n" "\n" "mixed (4 lines)\r\r\n\n\r\n";
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020048
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
Philipp Maier87bd9be2017-08-22 16:35:41 +020060 for (line = mgcp_strline(buf, &save); line;
61 line = mgcp_strline(NULL, &save)) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020062 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"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020082
Philipp Maier87bd9be2017-08-22 16:35:41 +020083#define MDCX3 \
84 "MDCX 18983215 1@mgw MGCP 1.0\r\n" \
85 "I: 1\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020086
Philipp Maier87bd9be2017-08-22 16:35:41 +020087#define MDCX3_RET \
88 "200 18983215 OK\r\n" \
89 "I: 1\n" \
90 "\n" \
91 "v=0\r\n" \
92 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
93 "s=-\r\n" \
94 "c=IN IP4 0.0.0.0\r\n" \
95 "t=0 0\r\n" \
96 "m=audio 16002 RTP/AVP 128\r\n" \
97 "a=rtpmap:128 GSM-EFR/8000\r\n" \
98 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020099
Philipp Maier87bd9be2017-08-22 16:35:41 +0200100#define MDCX3A_RET \
101 "200 18983215 OK\r\n" \
102 "I: 1\n" \
103 "\n" \
104 "v=0\r\n" \
105 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
106 "s=-\r\n" \
107 "c=IN IP4 0.0.0.0\r\n" \
108 "t=0 0\r\n" \
109 "m=audio 16002 RTP/AVP 97\r\n" \
110 "a=rtpmap:97 GSM-EFR/8000\r\n" \
111 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200112
Philipp Maier87bd9be2017-08-22 16:35:41 +0200113#define MDCX3_FMTP_RET \
114 "200 18983215 OK\r\n" \
115 "I: 1\n" \
116 "\n" \
117 "v=0\r\n" \
118 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
119 "s=-\r\n" \
120 "c=IN IP4 0.0.0.0\r\n" \
121 "t=0 0\r\n" \
122 "m=audio 16006 RTP/AVP 128\r\n" \
123 "a=rtpmap:128 GSM-EFR/8000\r\n" \
124 "a=fmtp:126 0/1/2\r\n" \
125 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127#define MDCX4 \
128 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \
129 "M: sendrecv\r" \
130 "C: 2\r\n" \
131 "I: 1\r\n" \
132 "L: p:20, a:AMR, nt:IN\r\n" \
133 "\n" \
134 "v=0\r\n" \
135 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
136 "c=IN IP4 0.0.0.0\r\n" \
137 "t=0 0\r\n" \
138 "m=audio 4441 RTP/AVP 99\r\n" \
139 "a=rtpmap:99 AMR/8000\r\n" \
140 "a=ptime:40\r\n"
141
142#define MDCX4_RET(Ident) \
143 "200 " Ident " OK\r\n" \
144 "I: 1\n" \
145 "\n" \
146 "v=0\r\n" \
147 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
148 "s=-\r\n" \
149 "c=IN IP4 0.0.0.0\r\n" \
150 "t=0 0\r\n" \
151 "m=audio 16002 RTP/AVP 99\r\n" \
152 "a=rtpmap:99 AMR/8000\r\n" \
153 "a=ptime:40\r\n"
154
155#define MDCX4_RO_RET(Ident) \
156 "200 " Ident " OK\r\n" \
157 "I: 1\n" \
158 "\n" \
159 "v=0\r\n" \
160 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
161 "s=-\r\n" \
162 "c=IN IP4 0.0.0.0\r\n" \
163 "t=0 0\r\n" \
164 "m=audio 16002 RTP/AVP 96\r\n" \
165 "a=rtpmap:96 AMR\r\n" \
166 "a=ptime:40\r\n"
167
168#define MDCX4_PT1 \
169 "MDCX 18983217 1@mgw MGCP 1.0\r\n" \
170 "M: sendrecv\r" \
171 "C: 2\r\n" \
172 "I: 1\r\n" \
173 "L: p:20-40, a:AMR, nt:IN\r\n" \
174 "\n" \
175 "v=0\r\n" \
176 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
177 "c=IN IP4 0.0.0.0\r\n" \
178 "t=0 0\r\n" \
179 "m=audio 4441 RTP/AVP 99\r\n" \
180 "a=rtpmap:99 AMR/8000\r\n" \
181 "a=ptime:40\r\n"
182
183#define MDCX4_PT2 \
184 "MDCX 18983218 1@mgw MGCP 1.0\r\n" \
185 "M: sendrecv\r" \
186 "C: 2\r\n" \
187 "I: 1\r\n" \
188 "L: p:20-20, a:AMR, nt:IN\r\n" \
189 "\n" \
190 "v=0\r\n" \
191 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
192 "c=IN IP4 0.0.0.0\r\n" \
193 "t=0 0\r\n" \
194 "m=audio 4441 RTP/AVP 99\r\n" \
195 "a=rtpmap:99 AMR/8000\r\n" \
196 "a=ptime:40\r\n"
197
198#define MDCX4_PT3 \
199 "MDCX 18983219 1@mgw MGCP 1.0\r\n" \
200 "M: sendrecv\r" \
201 "C: 2\r\n" \
202 "I: 1\r\n" \
203 "L: a:AMR, nt:IN\r\n" \
204 "\n" \
205 "v=0\r\n" \
206 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
207 "c=IN IP4 0.0.0.0\r\n" \
208 "t=0 0\r\n" \
209 "m=audio 4441 RTP/AVP 99\r\n" \
210 "a=rtpmap:99 AMR/8000\r\n" \
211 "a=ptime:40\r\n"
212
213#define MDCX4_SO \
214 "MDCX 18983220 1@mgw MGCP 1.0\r\n" \
215 "M: sendonly\r" \
216 "C: 2\r\n" \
217 "I: 1\r\n" \
218 "L: p:20, a:AMR, nt:IN\r\n" \
219 "\n" \
220 "v=0\r\n" \
221 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
222 "c=IN IP4 0.0.0.0\r\n" \
223 "t=0 0\r\n" \
224 "m=audio 4441 RTP/AVP 99\r\n" \
225 "a=rtpmap:99 AMR/8000\r\n" \
226 "a=ptime:40\r\n"
227
228#define MDCX4_RO \
229 "MDCX 18983221 1@mgw MGCP 1.0\r\n" \
230 "M: recvonly\r" \
231 "C: 2\r\n" \
232 "I: 1\r\n" \
233 "L: p:20, a:AMR, nt:IN\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234
235#define SHORT2 "CRCX 1"
236#define SHORT2_RET "510 000000 FAIL\r\n"
237#define SHORT3 "CRCX 1 1@mgw"
238#define SHORT4 "CRCX 1 1@mgw MGCP"
239#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
240
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241#define CRCX \
242 "CRCX 2 1@mgw MGCP 1.0\r\n" \
243 "M: recvonly\r\n" \
244 "C: 2\r\n" \
245 "I: 1\r\n" \
246 "L: p:20\r\n" \
247 "\r\n" \
248 "v=0\r\n" \
249 "c=IN IP4 123.12.12.123\r\n" \
250 "m=audio 5904 RTP/AVP 97\r\n" \
251 "a=rtpmap:97 GSM-EFR/8000\r\n" \
252 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200253
Philipp Maier87bd9be2017-08-22 16:35:41 +0200254#define CRCX_RET \
255 "200 2 OK\r\n" \
256 "I: 1\n" \
257 "\n" \
258 "v=0\r\n" \
259 "o=- 1 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 16002 RTP/AVP 97\r\n" \
264 "a=rtpmap:97 GSM-EFR/8000\r\n" \
265 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200266
Philipp Maier87bd9be2017-08-22 16:35:41 +0200267#define CRCX_RET_NO_RTPMAP \
268 "200 2 OK\r\n" \
269 "I: 1\n" \
270 "\n" \
271 "v=0\r\n" \
272 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
273 "s=-\r\n" \
274 "c=IN IP4 0.0.0.0\r\n" \
275 "t=0 0\r\n" \
276 "m=audio 16002 RTP/AVP 97\r\n" \
277 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200278
Philipp Maier87bd9be2017-08-22 16:35:41 +0200279#define CRCX_FMTP_RET \
280 "200 2 OK\r\n" \
281 "I: 1\n" \
282 "\n" \
283 "v=0\r\n" \
284 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
285 "s=-\r\n" \
286 "c=IN IP4 0.0.0.0\r\n" \
287 "t=0 0\r\n" \
288 "m=audio 16006 RTP/AVP 97\r\n" \
289 "a=rtpmap:97 GSM-EFR/8000\r\n" \
290 "a=fmtp:126 0/1/2\r\n" \
291 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200292
Philipp Maier87bd9be2017-08-22 16:35:41 +0200293#define CRCX_ZYN \
294 "CRCX 2 1@mgw MGCP 1.0\r" \
295 "M: recvonly\r" \
296 "C: 2\r\n" \
297 "I: 1\n" \
298 "\n" \
299 "v=0\r" \
300 "c=IN IP4 123.12.12.123\r" \
301 "m=audio 5904 RTP/AVP 97\r" \
302 "a=rtpmap:97 GSM-EFR/8000\r"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200303
Philipp Maier87bd9be2017-08-22 16:35:41 +0200304#define CRCX_ZYN_RET \
305 "200 2 OK\r\n" \
306 "I: 1\n" \
307 "\n" \
308 "v=0\r\n" \
309 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
310 "s=-\r\n" \
311 "c=IN IP4 0.0.0.0\r\n" \
312 "t=0 0\r\n" \
313 "m=audio 16004 RTP/AVP 97\r\n" \
314 "a=rtpmap:97 GSM-EFR/8000\r\n" \
315 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200316
Philipp Maier87bd9be2017-08-22 16:35:41 +0200317#define DLCX \
318 "DLCX 7 1@mgw MGCP 1.0\r\n" \
319 "I: 1\r\n" \
320 "C: 2\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200321
Philipp Maier87bd9be2017-08-22 16:35:41 +0200322#define DLCX_RET \
323 "250 7 OK\r\n" \
324 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n" \
325 "X-Osmo-CP: EC TI=0, TO=0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200326
Philipp Maier87bd9be2017-08-22 16:35:41 +0200327#define RQNT \
328 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
329 "X: B244F267488\r\n" \
330 "S: D/9\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200331
Philipp Maier87bd9be2017-08-22 16:35:41 +0200332#define RQNT2 \
333 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
334 "X: ADD4F26746F\r\n" \
335 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200336
337#define RQNT1_RET "200 186908780 OK\r\n"
338#define RQNT2_RET "200 186908781 OK\r\n"
339
Philipp Maier87bd9be2017-08-22 16:35:41 +0200340#define PTYPE_IGNORE 0 /* == default initializer */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200341#define PTYPE_NONE 128
342#define PTYPE_NYI PTYPE_NONE
343
Philipp Maier87bd9be2017-08-22 16:35:41 +0200344#define CRCX_MULT_1 \
345 "CRCX 2 1@mgw MGCP 1.0\r\n" \
346 "I: 4711\r\n" \
347 "M: recvonly\r\n" \
348 "C: 2\r\n" \
349 "X\r\n" \
350 "L: p:20\r\n" \
351 "\r\n" \
352 "v=0\r\n" \
353 "c=IN IP4 123.12.12.123\r\n" \
354 "m=audio 5904 RTP/AVP 18 97\r\n" \
355 "a=rtpmap:18 G729/8000\r\n" \
356 "a=rtpmap:97 GSM-EFR/8000\r\n" \
357 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358
Philipp Maier87bd9be2017-08-22 16:35:41 +0200359#define CRCX_MULT_2 \
360 "CRCX 2 2@mgw MGCP 1.0\r\n" \
361 "I: 90210\r\n" \
362 "M: recvonly\r\n" \
363 "C: 2\r\n" \
364 "X\r\n" \
365 "L: p:20\r\n" \
366 "\r\n" \
367 "v=0\r\n" \
368 "c=IN IP4 123.12.12.123\r\n" \
369 "m=audio 5904 RTP/AVP 18 97 101\r\n" \
370 "a=rtpmap:18 G729/8000\r\n" \
371 "a=rtpmap:97 GSM-EFR/8000\r\n" \
372 "a=rtpmap:101 FOO/8000\r\n" \
373 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200374
Philipp Maier87bd9be2017-08-22 16:35:41 +0200375#define CRCX_MULT_3 \
376 "CRCX 2 3@mgw MGCP 1.0\r\n" \
377 "I: 0815\r\n" \
378 "M: recvonly\r\n" \
379 "C: 2\r\n" \
380 "X\r\n" \
381 "L: p:20\r\n" \
382 "\r\n" \
383 "v=0\r\n" \
384 "c=IN IP4 123.12.12.123\r\n" \
385 "m=audio 5904 RTP/AVP\r\n" \
386 "a=rtpmap:18 G729/8000\r\n" \
387 "a=rtpmap:97 GSM-EFR/8000\r\n" \
388 "a=rtpmap:101 FOO/8000\r\n" \
389 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200390
Philipp Maier87bd9be2017-08-22 16:35:41 +0200391#define CRCX_MULT_4 \
392 "CRCX 2 4@mgw MGCP 1.0\r\n" \
393 "I: 32168\r\n" \
394 "M: recvonly\r\n" \
395 "C: 2\r\n" \
396 "X\r\n" \
397 "L: p:20\r\n" \
398 "\r\n" \
399 "v=0\r\n" \
400 "c=IN IP4 123.12.12.123\r\n" \
401 "m=audio 5904 RTP/AVP 18\r\n" \
402 "a=rtpmap:18 G729/8000\r\n" \
403 "a=rtpmap:97 GSM-EFR/8000\r\n" \
404 "a=rtpmap:101 FOO/8000\r\n" \
405 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200406
407#define CRCX_MULT_GSM_EXACT \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200408 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
409 "C: 1355c6041e\r\n" \
410 "I: 3\r\n" \
411 "L: p:20, a:GSM, nt:IN\r\n" \
412 "M: recvonly\r\n" \
413 "\r\n" \
414 "v=0\r\n" \
415 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
416 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
417 "t=0 0\r\nm=audio 29084 RTP/AVP 255 0 8 3 18 4 96 97 101\r\n" \
418 "a=rtpmap:0 PCMU/8000\r\n" \
419 "a=rtpmap:8 PCMA/8000\r\n" \
420 "a=rtpmap:3 gsm/8000\r\n" \
421 "a=rtpmap:18 G729/8000\r\n" \
422 "a=fmtp:18 annexb=no\r\n" \
423 "a=rtpmap:4 G723/8000\r\n" \
424 "a=rtpmap:96 iLBC/8000\r\n" \
425 "a=fmtp:96 mode=20\r\n" \
426 "a=rtpmap:97 iLBC/8000\r\n" \
427 "a=fmtp:97 mode=30\r\n" \
428 "a=rtpmap:101 telephone-event/8000\r\n" \
429 "a=fmtp:101 0-15\r\n" \
430 "a=recvonly\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200431
Philipp Maier87bd9be2017-08-22 16:35:41 +0200432#define MDCX_NAT_DUMMY \
433 "MDCX 23 5@mgw MGCP 1.0\r\n" \
434 "C: 1355c6041e\r\n" \
435 "I: 3\r\n" \
436 "\r\n" \
437 "c=IN IP4 8.8.8.8\r\n" \
438 "m=audio 16434 RTP/AVP 255\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200439
440struct mgcp_test {
441 const char *name;
442 const char *req;
443 const char *exp_resp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200444 int ptype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200445 const char *extra_fmtp;
446};
447
448static const struct mgcp_test tests[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200449 {"AUEP1", AUEP1, AUEP1_RET},
450 {"AUEP2", AUEP2, AUEP2_RET},
451 {"MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET},
452 {"MDCX2", MDCX_UNALLOCATED, MDCX_RET},
453 {"CRCX", CRCX, CRCX_RET, 97},
454 {"MDCX3", MDCX3, MDCX3_RET, PTYPE_IGNORE},
455 {"MDCX4", MDCX4, MDCX4_RET("18983216"), 99},
456 {"MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99},
457 {"MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99},
458 {"MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99},
459 {"MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99},
460 {"MDCX4_RO", MDCX4_RO, MDCX4_RO_RET("18983221"), PTYPE_IGNORE},
461 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
462 {"CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97},
463 {"EMPTY", EMPTY, EMPTY_RET},
464 {"SHORT1", SHORT, SHORT_RET},
465 {"SHORT2", SHORT2, SHORT2_RET},
466 {"SHORT3", SHORT3, SHORT2_RET},
467 {"SHORT4", SHORT4, SHORT2_RET},
468 {"RQNT1", RQNT, RQNT1_RET},
469 {"RQNT2", RQNT2, RQNT2_RET},
470 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
471 {"CRCX", CRCX, CRCX_FMTP_RET, 97,.extra_fmtp = "a=fmtp:126 0/1/2"},
472 {"MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE,.extra_fmtp =
473 "a=fmtp:126 0/1/2"},
474 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE,.extra_fmtp = "a=fmtp:126 0/1/2"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475};
476
477static const struct mgcp_test retransmit[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478 {"CRCX", CRCX, CRCX_RET},
479 {"RQNT1", RQNT, RQNT1_RET},
480 {"RQNT2", RQNT2, RQNT2_RET},
481 {"MDCX3", MDCX3, MDCX3A_RET},
482 {"DLCX", DLCX, DLCX_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200483};
484
485static struct msgb *create_msg(const char *str)
486{
487 struct msgb *msg;
488
489 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
490 int len = sprintf((char *)msg->data, "%s", str);
491 msg->l2h = msgb_put(msg, len);
492 return msg;
493}
494
495static int last_endpoint = -1;
496
497static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
498 int state, const char *transactio_id)
499{
500 fprintf(stderr, "Policy CB got state %d on endpoint %d\n",
501 state, endpoint);
502 last_endpoint = endpoint;
503 return MGCP_POLICY_CONT;
504}
505
506#define MGCP_DUMMY_LOAD 0x23
507static int dummy_packets = 0;
508/* override and forward */
509ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200510 const struct sockaddr *dest_addr, socklen_t addrlen)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200511{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200512 uint32_t dest_host =
513 htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
514 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200515
Philipp Maier87bd9be2017-08-22 16:35:41 +0200516 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
517 fprintf(stderr,
518 "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
519 dest_host, dest_port, len, osmo_hexdump(buf, len));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200520 dummy_packets += 1;
521 }
522
Philipp Maier3d9b6562017-10-13 18:33:44 +0200523 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200524}
525
526static int64_t force_monotonic_time_us = -1;
527/* override and forward */
528int clock_gettime(clockid_t clk_id, struct timespec *tp)
529{
530 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
531 static clock_gettime_t real_clock_gettime = NULL;
532
533 if (!real_clock_gettime)
534 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
535
536 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
537 tp->tv_sec = force_monotonic_time_us / 1000000;
538 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
539 return 0;
540 }
541
542 return real_clock_gettime(clk_id, tp);
543}
544
545#define CONN_UNMODIFIED (0x1000)
546
547static void test_values(void)
548{
549 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200550 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200551
552 /* Check that LOOPBACK enables all output */
553 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200554 MGCP_CONN_RECV_SEND);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555}
556
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200557static void test_messages(void)
558{
559 struct mgcp_config *cfg;
560 struct mgcp_endpoint *endp;
561 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200562 struct mgcp_conn_rtp *conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200563
564 cfg = mgcp_config_alloc();
565
Philipp Maierfcd06552017-11-10 17:32:22 +0100566 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200567 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200568 cfg->policy_cb = mgcp_test_policy_cb;
569
570 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
571
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200572 for (i = 0; i < ARRAY_SIZE(tests); i++) {
573 const struct mgcp_test *t = &tests[i];
574 struct msgb *inp;
575 struct msgb *msg;
576
577 printf("Testing %s\n", t->name);
578
579 last_endpoint = -1;
580 dummy_packets = 0;
581
Philipp Maier87bd9be2017-08-22 16:35:41 +0200582 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
583 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200584
585 inp = create_msg(t->req);
586 msg = mgcp_handle_message(cfg, inp);
587 msgb_free(inp);
588 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200589 if (msg) {
590 printf("%s failed '%s'\n", t->name,
591 (char *)msg->data);
592 OSMO_ASSERT(false);
593 }
594 } else if (strcmp((char *)msg->data, t->exp_resp) != 0) {
Neels Hofmeyr020e89b2017-09-19 14:28:14 +0200595 printf("%s failed.\nExpected:\n%s\nGot:\n%s\n",
596 t->name, t->exp_resp, (char *) msg->data);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200597 OSMO_ASSERT(false);
598 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200599 msgb_free(msg);
600
601 if (dummy_packets)
602 printf("Dummy packets: %d\n", dummy_packets);
603
604 if (last_endpoint != -1) {
605 endp = &cfg->trunk.endpoints[last_endpoint];
606
Philipp Maier01d24a32017-11-21 17:26:09 +0100607 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200608 if (conn) {
609 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200610
Philipp Maier87bd9be2017-08-22 16:35:41 +0200611 if (conn->end.packet_duration_ms != -1)
612 printf("Detected packet duration: %d\n",
613 conn->end.packet_duration_ms);
614 else
615 printf("Packet duration not set\n");
616 if (endp->local_options.pkt_period_min ||
617 endp->local_options.pkt_period_max)
618 printf
619 ("Requested packetetization period: "
620 "%d-%d\n",
621 endp->local_options.pkt_period_min,
622 endp->
623 local_options.pkt_period_max);
624 else
625 printf
626 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200627
Philipp Maier87bd9be2017-08-22 16:35:41 +0200628 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
629 printf("Connection mode: %d:%s%s%s%s\n",
630 conn->conn->mode,
631 !conn->conn->mode ? " NONE" : "",
632 conn->conn->mode & MGCP_CONN_SEND_ONLY
633 ? " SEND" : "",
634 conn->conn->mode & MGCP_CONN_RECV_ONLY
635 ? " RECV" : "",
636 conn->conn->mode & MGCP_CONN_LOOPBACK
637 & ~MGCP_CONN_RECV_SEND
638 ? " LOOP" : "");
639 fprintf(stderr,
640 "RTP output %sabled, NET output %sabled\n",
641 conn->end.output_enabled
642 ? "en" : "dis",
643 conn->end.output_enabled
644 ? "en" : "dis");
645 } else
646 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200647
Philipp Maier87bd9be2017-08-22 16:35:41 +0200648 OSMO_ASSERT(conn->end.output_enabled
649 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
650
651 conn->conn->mode |= CONN_UNMODIFIED;
652
653 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200654 endp->local_options.pkt_period_min = 0;
655 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200656 }
657
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200658 /* Check detected payload type */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200659 if (t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200660 OSMO_ASSERT(last_endpoint != -1);
661 endp = &cfg->trunk.endpoints[last_endpoint];
662
663 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200664 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200665 last_endpoint,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200666 conn->end.codec.payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200667
Philipp Maier87bd9be2017-08-22 16:35:41 +0200668 if (t->ptype != PTYPE_IGNORE)
669 OSMO_ASSERT(conn->end.codec.payload_type ==
670 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200671
672 /* Reset them again for next test */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200673 conn->end.codec.payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200674 }
675 }
676
677 talloc_free(cfg);
678}
679
680static void test_retransmission(void)
681{
682 struct mgcp_config *cfg;
683 int i;
684
685 cfg = mgcp_config_alloc();
686
Philipp Maierfcd06552017-11-10 17:32:22 +0100687 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688 mgcp_endpoints_allocate(&cfg->trunk);
689
690 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
691
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200692 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
693 const struct mgcp_test *t = &retransmit[i];
694 struct msgb *inp;
695 struct msgb *msg;
696
697 printf("Testing %s\n", t->name);
698
699 inp = create_msg(t->req);
700 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200701
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702 msgb_free(inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200703 if (strcmp((char *)msg->data, t->exp_resp) != 0) {
704 printf("%s failed '%s'\n", t->name, (char *)msg->data);
705 OSMO_ASSERT(false);
706 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200707 msgb_free(msg);
708
709 /* Retransmit... */
710 printf("Re-transmitting %s\n", t->name);
711 inp = create_msg(t->req);
712 msg = mgcp_handle_message(cfg, inp);
713 msgb_free(inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200714 if (strcmp((char *)msg->data, t->exp_resp) != 0) {
715 printf("%s failed '%s'\n", t->name, (char *)msg->data);
716 OSMO_ASSERT(false);
717 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200718 msgb_free(msg);
719 }
720
721 talloc_free(cfg);
722}
723
724static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
725{
726 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200727 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200728 return 0;
729}
730
731static void test_rqnt_cb(void)
732{
733 struct mgcp_config *cfg;
734 struct msgb *inp, *msg;
735
736 cfg = mgcp_config_alloc();
737 cfg->rqnt_cb = rqnt_cb;
738
Philipp Maierfcd06552017-11-10 17:32:22 +0100739 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200740 mgcp_endpoints_allocate(&cfg->trunk);
741
742 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
743
744 inp = create_msg(CRCX);
745 msgb_free(mgcp_handle_message(cfg, inp));
746 msgb_free(inp);
747
748 /* send the RQNT and check for the CB */
749 inp = create_msg(RQNT);
750 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200751 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200752 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
753 abort();
754 }
755
Philipp Maier87bd9be2017-08-22 16:35:41 +0200756 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 printf("FAILED: callback not called: %p\n", cfg->data);
758 abort();
759 }
760
761 msgb_free(msg);
762 msgb_free(inp);
763
764 inp = create_msg(DLCX);
765 msgb_free(mgcp_handle_message(cfg, inp));
766 msgb_free(inp);
767 talloc_free(cfg);
768}
769
770struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200771 int cycles;
772 uint16_t base_seq;
773 uint16_t max_seq;
774 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200775
Philipp Maier87bd9be2017-08-22 16:35:41 +0200776 uint32_t expected;
777 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200778};
779
780static const struct pl_test pl_test_dat[] = {
781 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200782 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
783 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200784 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200785 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
786 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200787 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200788 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
789 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200790 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200791 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
792 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200793 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200794 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
795 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200796};
797
798static void test_packet_loss_calc(void)
799{
800 int i;
801 printf("Testing packet loss calculation.\n");
802
803 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
804 uint32_t expected;
805 int loss;
806 struct mgcp_rtp_state state;
807 struct mgcp_rtp_end rtp;
808 memset(&state, 0, sizeof(state));
809 memset(&rtp, 0, sizeof(rtp));
810
811 state.stats_initialized = 1;
812 state.stats_base_seq = pl_test_dat[i].base_seq;
813 state.stats_max_seq = pl_test_dat[i].max_seq;
814 state.stats_cycles = pl_test_dat[i].cycles;
815
Philipp Maier87bd9be2017-08-22 16:35:41 +0200816 rtp.packets_rx = pl_test_dat[i].packets;
817 calc_loss(&state, &rtp, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200818
Philipp Maier87bd9be2017-08-22 16:35:41 +0200819 if (loss != pl_test_dat[i].loss
820 || expected != pl_test_dat[i].expected) {
821 printf
822 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
823 i, loss, pl_test_dat[i].loss, expected,
824 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825 }
826 }
827}
828
Philipp Maier87bd9be2017-08-22 16:35:41 +0200829int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
830 uint32_t *pr, uint32_t *_or, int *loss,
831 uint32_t *jitter)
832{
833 char *line, *save;
834 int rc;
835
836 /* initialize with bad values */
837 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
838 *loss = INT_MAX;
839
840 line = strtok_r((char *)msg->l2h, "\r\n", &save);
841 if (!line)
842 return -1;
843
844 /* this can only parse the message that is created above... */
845 for_each_non_empty_line(line, save) {
846 switch (line[0]) {
847 case 'P':
848 rc = sscanf(line,
849 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
850 ps, os, pr, _or, loss, jitter);
851 return rc == 6 ? 0 : -1;
852 }
853 }
854
855 return -1;
856}
857
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858static void test_mgcp_stats(void)
859{
860 printf("Testing stat parsing\n");
861
862 uint32_t bps, bos, pr, _or, jitter;
863 struct msgb *msg;
864 int loss;
865 int rc;
866
867 msg = create_msg(DLCX_RET);
868 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
869 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200870 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
871 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872 printf("FAIL: Parsing failed1.\n");
873 msgb_free(msg);
874
Philipp Maier87bd9be2017-08-22 16:35:41 +0200875 msg =
876 create_msg
877 ("250 7 OK\r\nP: PS=10, OS=20, PR=30, OR=40, PL=-3, JI=40\r\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200878 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
879 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200880 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
881 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200882 printf("FAIL: Parsing failed2.\n");
883 msgb_free(msg);
884}
885
886struct rtp_packet_info {
887 float txtime;
888 int len;
889 char *data;
890};
891
892struct rtp_packet_info test_rtp_packets1[] = {
893 /* RTP: SeqNo=0, TS=0 */
894 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200895 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200896 /* RTP: SeqNo=1, TS=160 */
897 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200898 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200899 /* RTP: SeqNo=2, TS=320 */
900 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200901 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200902 /* Repeat RTP timestamp: */
903 /* RTP: SeqNo=3, TS=320 */
904 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200905 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200906 /* RTP: SeqNo=4, TS=480 */
907 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200908 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200909 /* RTP: SeqNo=5, TS=640 */
910 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200911 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200912 /* Double skip RTP timestamp (delta = 2*160): */
913 /* RTP: SeqNo=6, TS=960 */
914 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200915 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200916 /* RTP: SeqNo=7, TS=1120 */
917 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200918 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200919 /* RTP: SeqNo=8, TS=1280 */
920 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200921 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200922 /* Non 20ms RTP timestamp (delta = 120): */
923 /* RTP: SeqNo=9, TS=1400 */
924 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200926 /* RTP: SeqNo=10, TS=1560 */
927 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200928 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200929 /* RTP: SeqNo=11, TS=1720 */
930 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200931 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200932 /* SSRC changed to 0x10203040, RTP timestamp jump */
933 /* RTP: SeqNo=12, TS=34688 */
934 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200935 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200936 /* RTP: SeqNo=13, TS=34848 */
937 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200938 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200939 /* RTP: SeqNo=14, TS=35008 */
940 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200941 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200942 /* Non 20ms RTP timestamp (delta = 120): */
943 /* RTP: SeqNo=15, TS=35128 */
944 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200945 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200946 /* RTP: SeqNo=16, TS=35288 */
947 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200948 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200949 /* RTP: SeqNo=17, TS=35448 */
950 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200951 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200952 /* SeqNo increment by 2, RTP timestamp delta = 320: */
953 /* RTP: SeqNo=19, TS=35768 */
954 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200955 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200956 /* RTP: SeqNo=20, TS=35928 */
957 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200958 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200959 /* RTP: SeqNo=21, TS=36088 */
960 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200961 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200962 /* Repeat last packet */
963 /* RTP: SeqNo=21, TS=36088 */
964 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200965 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200966 /* RTP: SeqNo=22, TS=36248 */
967 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200968 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200969 /* RTP: SeqNo=23, TS=36408 */
970 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200971 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200972 /* Don't increment SeqNo but increment timestamp by 160 */
973 /* RTP: SeqNo=23, TS=36568 */
974 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200975 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200976 /* RTP: SeqNo=24, TS=36728 */
977 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200979 /* RTP: SeqNo=25, TS=36888 */
980 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200981 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200982 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
983 * SeqNo jump */
984 /* RTP: SeqNo=1000, TS=160000 */
985 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200986 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200987 /* RTP: SeqNo=1001, TS=160160 */
988 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200989 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200990 /* RTP: SeqNo=1002, TS=160320 */
991 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200992 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200993};
994
Philipp Maier87bd9be2017-08-22 16:35:41 +0200995void mgcp_patch_and_count(struct mgcp_endpoint *endp,
996 struct mgcp_rtp_state *state,
997 struct mgcp_rtp_end *rtp_end,
998 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200999
1000static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1001{
1002 int i;
1003
1004 struct mgcp_trunk_config trunk;
1005 struct mgcp_endpoint endp;
1006 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001007 struct mgcp_rtp_end *rtp;
1008 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001009 char buffer[4096];
1010 uint32_t last_ssrc = 0;
1011 uint32_t last_timestamp = 0;
1012 uint32_t last_seqno = 0;
1013 int last_in_ts_err_cnt = 0;
1014 int last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001015 struct mgcp_conn_rtp *conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001016
1017 printf("Testing packet error detection%s%s.\n",
1018 patch_ssrc ? ", patch SSRC" : "",
1019 patch_ts ? ", patch timestamps" : "");
1020
1021 memset(&trunk, 0, sizeof(trunk));
1022 memset(&endp, 0, sizeof(endp));
1023 memset(&state, 0, sizeof(state));
1024
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025 endp.type = &ep_typeset.rtp;
1026
Philipp Maierfcd06552017-11-10 17:32:22 +01001027 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001028 trunk.endpoints = &endp;
1029 trunk.force_constant_ssrc = patch_ssrc;
1030 trunk.force_aligned_timing = patch_ts;
1031
1032 endp.tcfg = &trunk;
1033
Philipp Maier87bd9be2017-08-22 16:35:41 +02001034 INIT_LLIST_HEAD(&endp.conns);
Philipp Maier01d24a32017-11-21 17:26:09 +01001035 mgcp_conn_alloc(NULL, &endp, "4711", MGCP_CONN_TYPE_RTP,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001036 "test-connection");
Philipp Maier01d24a32017-11-21 17:26:09 +01001037 conn = mgcp_conn_get_rtp(&endp, "4711");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001038 OSMO_ASSERT(conn);
1039
1040 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001041
1042 rtp->codec.payload_type = 98;
1043
1044 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1045 struct rtp_packet_info *info = test_rtp_packets1 + i;
1046
1047 force_monotonic_time_us = round(1000000.0 * info->txtime);
1048
1049 OSMO_ASSERT(info->len <= sizeof(buffer));
1050 OSMO_ASSERT(info->len >= 0);
1051 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001052 mgcp_rtp_end_config(&endp, 1, rtp);
1053
1054 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1055 buffer, info->len);
1056
1057 if (state.out_stream.ssrc != last_ssrc) {
1058 printf("Output SSRC changed to %08x\n",
1059 state.out_stream.ssrc);
1060 last_ssrc = state.out_stream.ssrc;
1061 }
1062
1063 printf("In TS: %d, dTS: %d, Seq: %d\n",
1064 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001065 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001066
1067 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
1068 "TS Err change: in %+d, out %+d\n",
1069 state.out_stream.last_timestamp - last_timestamp,
1070 state.out_stream.last_tsdelta,
1071 state.out_stream.last_seq - last_seqno,
1072 state.in_stream.err_ts_counter - last_in_ts_err_cnt,
1073 state.out_stream.err_ts_counter - last_out_ts_err_cnt);
1074
1075 printf("Stats: Jitter = %u, Transit = %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001076 calc_jitter(&state), state.stats_transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001077
1078 last_in_ts_err_cnt = state.in_stream.err_ts_counter;
1079 last_out_ts_err_cnt = state.out_stream.err_ts_counter;
1080 last_timestamp = state.out_stream.last_timestamp;
1081 last_seqno = state.out_stream.last_seq;
1082 }
1083
1084 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001085 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001086}
1087
1088static void test_multilple_codec(void)
1089{
1090 struct mgcp_config *cfg;
1091 struct mgcp_endpoint *endp;
1092 struct msgb *inp, *resp;
1093 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001094 struct mgcp_conn_rtp *conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001095
1096 printf("Testing multiple payload types\n");
1097
1098 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001099 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001100 mgcp_endpoints_allocate(&cfg->trunk);
1101 cfg->policy_cb = mgcp_test_policy_cb;
1102 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1103
1104 /* Allocate endpoint 1@mgw with two codecs */
1105 last_endpoint = -1;
1106 inp = create_msg(CRCX_MULT_1);
1107 resp = mgcp_handle_message(cfg, inp);
1108 msgb_free(inp);
1109 msgb_free(resp);
1110
1111 OSMO_ASSERT(last_endpoint == 1);
1112 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maier01d24a32017-11-21 17:26:09 +01001113 conn = mgcp_conn_get_rtp(endp, "4711");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001114 OSMO_ASSERT(conn);
1115 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1116 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001117
1118 /* Allocate 2@mgw with three codecs, last one ignored */
1119 last_endpoint = -1;
1120 inp = create_msg(CRCX_MULT_2);
1121 resp = mgcp_handle_message(cfg, inp);
1122 msgb_free(inp);
1123 msgb_free(resp);
1124
1125 OSMO_ASSERT(last_endpoint == 2);
1126 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maier01d24a32017-11-21 17:26:09 +01001127 conn = mgcp_conn_get_rtp(endp, "90210");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001128 OSMO_ASSERT(conn);
1129 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1130 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001131
1132 /* Allocate 3@mgw with no codecs, check for PT == -1 */
1133 last_endpoint = -1;
1134 inp = create_msg(CRCX_MULT_3);
1135 resp = mgcp_handle_message(cfg, inp);
1136 msgb_free(inp);
1137 msgb_free(resp);
1138
1139 OSMO_ASSERT(last_endpoint == 3);
1140 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maier01d24a32017-11-21 17:26:09 +01001141 conn = mgcp_conn_get_rtp(endp, "0815");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001142 OSMO_ASSERT(conn);
1143 OSMO_ASSERT(conn->end.codec.payload_type == -1);
1144 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001145
1146 /* Allocate 4@mgw with a single codec */
1147 last_endpoint = -1;
1148 inp = create_msg(CRCX_MULT_4);
1149 resp = mgcp_handle_message(cfg, inp);
1150 msgb_free(inp);
1151 msgb_free(resp);
1152
1153 OSMO_ASSERT(last_endpoint == 4);
1154 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maier01d24a32017-11-21 17:26:09 +01001155 conn = mgcp_conn_get_rtp(endp, "32168");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001156 OSMO_ASSERT(conn);
1157 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1158 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001159
1160 /* Allocate 5@mgw at select GSM.. */
1161 last_endpoint = -1;
1162 inp = create_msg(CRCX_MULT_GSM_EXACT);
1163 talloc_free(cfg->trunk.audio_name);
1164 cfg->trunk.audio_name = "GSM/8000";
1165 cfg->trunk.no_audio_transcoding = 1;
1166 resp = mgcp_handle_message(cfg, inp);
1167 msgb_free(inp);
1168 msgb_free(resp);
1169
1170 OSMO_ASSERT(last_endpoint == 5);
1171 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maier01d24a32017-11-21 17:26:09 +01001172 conn = mgcp_conn_get_rtp(endp, "3");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001173 OSMO_ASSERT(conn);
1174 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1175 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001176
1177 inp = create_msg(MDCX_NAT_DUMMY);
1178 last_endpoint = -1;
1179 resp = mgcp_handle_message(cfg, inp);
1180 msgb_free(inp);
1181 msgb_free(resp);
1182 OSMO_ASSERT(last_endpoint == 5);
1183 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maier01d24a32017-11-21 17:26:09 +01001184 conn = mgcp_conn_get_rtp(endp, "3");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001185 OSMO_ASSERT(conn);
1186 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1187 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
1188 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001189 memset(&addr, 0, sizeof(addr));
1190 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001191 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001192
1193 /* Check what happens without that flag */
1194
Philipp Maier87bd9be2017-08-22 16:35:41 +02001195 /* Free the previous endpoint and the data and
1196 * check if the connection really vanished... */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001197 mgcp_release_endp(endp);
1198 talloc_free(endp->last_response);
1199 talloc_free(endp->last_trans);
1200 endp->last_response = endp->last_trans = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001201 conn = mgcp_conn_get_rtp(endp, "3");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001202 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001203
1204 last_endpoint = -1;
1205 inp = create_msg(CRCX_MULT_GSM_EXACT);
1206 cfg->trunk.no_audio_transcoding = 0;
1207 resp = mgcp_handle_message(cfg, inp);
1208 msgb_free(inp);
1209 msgb_free(resp);
1210
1211 OSMO_ASSERT(last_endpoint == 5);
1212 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maier01d24a32017-11-21 17:26:09 +01001213 conn = mgcp_conn_get_rtp(endp, "3");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001214 OSMO_ASSERT(conn);
1215 OSMO_ASSERT(conn->end.codec.payload_type == 255);
1216 OSMO_ASSERT(conn->end.alt_codec.payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001217
1218 talloc_free(cfg);
1219}
1220
1221static void test_no_cycle(void)
1222{
1223 struct mgcp_config *cfg;
1224 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225 struct mgcp_conn_rtp *conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001226
1227 printf("Testing no sequence flow on initial packet\n");
1228
1229 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001230 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001231 mgcp_endpoints_allocate(&cfg->trunk);
1232
1233 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001234
Philipp Maier01d24a32017-11-21 17:26:09 +01001235 mgcp_conn_alloc(NULL, endp, "4711", MGCP_CONN_TYPE_RTP,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001236 "test-connection");
Philipp Maier01d24a32017-11-21 17:26:09 +01001237 conn = mgcp_conn_get_rtp(endp, "4711");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001238 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001239
Philipp Maier87bd9be2017-08-22 16:35:41 +02001240 OSMO_ASSERT(conn->state.stats_initialized == 0);
1241
1242 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
1243 OSMO_ASSERT(conn->state.stats_initialized == 1);
1244 OSMO_ASSERT(conn->state.stats_cycles == 0);
1245 OSMO_ASSERT(conn->state.stats_max_seq == 0);
1246
1247 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
1248 OSMO_ASSERT(conn->state.stats_initialized == 1);
1249 OSMO_ASSERT(conn->state.stats_cycles == 0);
1250 OSMO_ASSERT(conn->state.stats_max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001251
1252 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001253 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
1254 OSMO_ASSERT(conn->state.stats_initialized == 1);
1255 OSMO_ASSERT(conn->state.stats_cycles == 0);
1256 OSMO_ASSERT(conn->state.stats_max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001257
1258 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001259 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
1260 OSMO_ASSERT(conn->state.stats_initialized == 1);
1261 OSMO_ASSERT(conn->state.stats_cycles == UINT16_MAX + 1);
1262 OSMO_ASSERT(conn->state.stats_max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001263
Neels Hofmeyrb597b4f2017-11-18 21:28:35 +01001264 mgcp_release_endp(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001265 talloc_free(cfg);
1266}
1267
1268static void test_no_name(void)
1269{
1270 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001271 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001272
1273 printf("Testing no rtpmap name\n");
1274 cfg = mgcp_config_alloc();
1275
Philipp Maierfcd06552017-11-10 17:32:22 +01001276 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001277 cfg->trunk.audio_send_name = 0;
1278 mgcp_endpoints_allocate(&cfg->trunk);
1279
1280 cfg->policy_cb = mgcp_test_policy_cb;
1281
1282 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1283
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001284 inp = create_msg(CRCX);
1285 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001286 if (strcmp((char *)msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001287 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001288 (char *)msg->data);
1289 OSMO_ASSERT(false);
1290 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001291 msgb_free(inp);
1292 msgb_free(msg);
1293
1294 mgcp_release_endp(&cfg->trunk.endpoints[1]);
1295 talloc_free(cfg);
1296}
1297
1298static void test_osmux_cid(void)
1299{
1300 int id, i;
1301
1302 OSMO_ASSERT(osmux_used_cid() == 0);
1303 id = osmux_get_cid();
1304 OSMO_ASSERT(id == 0);
1305 OSMO_ASSERT(osmux_used_cid() == 1);
1306 osmux_put_cid(id);
1307 OSMO_ASSERT(osmux_used_cid() == 0);
1308
1309 for (i = 0; i < 256; ++i) {
1310 id = osmux_get_cid();
1311 OSMO_ASSERT(id == i);
1312 OSMO_ASSERT(osmux_used_cid() == i + 1);
1313 }
1314
1315 id = osmux_get_cid();
1316 OSMO_ASSERT(id == -1);
1317
1318 for (i = 0; i < 256; ++i)
1319 osmux_put_cid(i);
1320 OSMO_ASSERT(osmux_used_cid() == 0);
1321}
1322
1323static const struct log_info_cat log_categories[] = {
1324};
1325
1326const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001327 .cat = log_categories,
1328 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001329};
1330
1331int main(int argc, char **argv)
1332{
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001333 void *msgb_ctx = msgb_talloc_ctx_init(NULL, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001334 osmo_init_logging(&log_info);
1335
1336 test_strline();
1337 test_values();
1338 test_messages();
1339 test_retransmission();
1340 test_packet_loss_calc();
1341 test_rqnt_cb();
1342 test_mgcp_stats();
1343 test_packet_error_detection(1, 0);
1344 test_packet_error_detection(0, 0);
1345 test_packet_error_detection(0, 1);
1346 test_packet_error_detection(1, 1);
1347 test_multilple_codec();
1348 test_no_cycle();
1349 test_no_name();
1350 test_osmux_cid();
1351
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001352 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1353 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1354 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001355 printf("Done\n");
1356 return EXIT_SUCCESS;
1357}