blob: 38a0a46265b6da591a6af196f3d2028107a7b58e [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>
Philipp Maier37d11c82018-02-01 14:38:12 +010028#include <osmocom/mgcp/mgcp_endp.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
Philipp Maier12943ea2018-01-17 15:40:25 +010069#define AUEP1 "AUEP 158663169 ds/e1-1/2@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020070#define AUEP1_RET "200 158663169 OK\r\n"
Philipp Maier12943ea2018-01-17 15:40:25 +010071#define AUEP2 "AUEP 18983213 ds/e1-2/1@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020072#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
Philipp Maier12943ea2018-01-17 15:40:25 +010078#define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@mgw MGCP 1.0\r\n"
Harald Welteabbb6b92017-12-28 13:13:50 +010079#define MDCX_ERR_RET "500 18983213 FAIL\r\n"
Philipp Maier12943ea2018-01-17 15:40:25 +010080#define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020081#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" \
Philipp Maierffd75e42017-11-22 11:44:50 +010085 "I: %s\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" \
Philipp Maierc3cfae22018-01-22 12:03:03 +010089 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020090 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010091 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020092 "s=-\r\n" \
93 "c=IN IP4 0.0.0.0\r\n" \
94 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010095 "m=audio 16002 RTP/AVP 97\r\n" \
96 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020097 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020098
Philipp Maier87bd9be2017-08-22 16:35:41 +020099#define MDCX3A_RET \
100 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100101 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200102 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100103 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200104 "s=-\r\n" \
105 "c=IN IP4 0.0.0.0\r\n" \
106 "t=0 0\r\n" \
107 "m=audio 16002 RTP/AVP 97\r\n" \
108 "a=rtpmap:97 GSM-EFR/8000\r\n" \
109 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200110
Philipp Maier87bd9be2017-08-22 16:35:41 +0200111#define MDCX3_FMTP_RET \
112 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100113 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200114 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100115 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200116 "s=-\r\n" \
117 "c=IN IP4 0.0.0.0\r\n" \
118 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100119 "m=audio 16006 RTP/AVP 97\r\n" \
120 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200121 "a=fmtp:126 0/1/2\r\n" \
122 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200123
Philipp Maier87bd9be2017-08-22 16:35:41 +0200124#define MDCX4 \
125 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \
126 "M: sendrecv\r" \
127 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100128 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129 "L: p:20, a:AMR, nt:IN\r\n" \
130 "\n" \
131 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100132 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200133 "c=IN IP4 0.0.0.0\r\n" \
134 "t=0 0\r\n" \
135 "m=audio 4441 RTP/AVP 99\r\n" \
136 "a=rtpmap:99 AMR/8000\r\n" \
137 "a=ptime:40\r\n"
138
139#define MDCX4_RET(Ident) \
140 "200 " Ident " OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100141 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200142 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100143 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200144 "s=-\r\n" \
145 "c=IN IP4 0.0.0.0\r\n" \
146 "t=0 0\r\n" \
147 "m=audio 16002 RTP/AVP 99\r\n" \
148 "a=rtpmap:99 AMR/8000\r\n" \
149 "a=ptime:40\r\n"
150
151#define MDCX4_RO_RET(Ident) \
152 "200 " Ident " OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100153 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200154 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100155 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200156 "s=-\r\n" \
157 "c=IN IP4 0.0.0.0\r\n" \
158 "t=0 0\r\n" \
159 "m=audio 16002 RTP/AVP 96\r\n" \
160 "a=rtpmap:96 AMR\r\n" \
161 "a=ptime:40\r\n"
162
163#define MDCX4_PT1 \
164 "MDCX 18983217 1@mgw MGCP 1.0\r\n" \
165 "M: sendrecv\r" \
166 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100167 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200168 "L: p:20-40, a:AMR, nt:IN\r\n" \
169 "\n" \
170 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100171 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172 "c=IN IP4 0.0.0.0\r\n" \
173 "t=0 0\r\n" \
174 "m=audio 4441 RTP/AVP 99\r\n" \
175 "a=rtpmap:99 AMR/8000\r\n" \
176 "a=ptime:40\r\n"
177
178#define MDCX4_PT2 \
179 "MDCX 18983218 1@mgw MGCP 1.0\r\n" \
180 "M: sendrecv\r" \
181 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100182 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200183 "L: p:20-20, a:AMR, nt:IN\r\n" \
184 "\n" \
185 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100186 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187 "c=IN IP4 0.0.0.0\r\n" \
188 "t=0 0\r\n" \
189 "m=audio 4441 RTP/AVP 99\r\n" \
190 "a=rtpmap:99 AMR/8000\r\n" \
191 "a=ptime:40\r\n"
192
193#define MDCX4_PT3 \
194 "MDCX 18983219 1@mgw MGCP 1.0\r\n" \
195 "M: sendrecv\r" \
196 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100197 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200198 "L: a:AMR, nt:IN\r\n" \
199 "\n" \
200 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100201 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200202 "c=IN IP4 0.0.0.0\r\n" \
203 "t=0 0\r\n" \
204 "m=audio 4441 RTP/AVP 99\r\n" \
205 "a=rtpmap:99 AMR/8000\r\n" \
206 "a=ptime:40\r\n"
207
208#define MDCX4_SO \
209 "MDCX 18983220 1@mgw MGCP 1.0\r\n" \
210 "M: sendonly\r" \
211 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100212 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200213 "L: p:20, a:AMR, nt:IN\r\n" \
214 "\n" \
215 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100216 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 "c=IN IP4 0.0.0.0\r\n" \
218 "t=0 0\r\n" \
219 "m=audio 4441 RTP/AVP 99\r\n" \
220 "a=rtpmap:99 AMR/8000\r\n" \
221 "a=ptime:40\r\n"
222
223#define MDCX4_RO \
224 "MDCX 18983221 1@mgw MGCP 1.0\r\n" \
225 "M: recvonly\r" \
226 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100227 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200228 "L: p:20, a:AMR, nt:IN\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229
230#define SHORT2 "CRCX 1"
231#define SHORT2_RET "510 000000 FAIL\r\n"
232#define SHORT3 "CRCX 1 1@mgw"
233#define SHORT4 "CRCX 1 1@mgw MGCP"
234#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
235
Philipp Maier87bd9be2017-08-22 16:35:41 +0200236#define CRCX \
237 "CRCX 2 1@mgw MGCP 1.0\r\n" \
238 "M: recvonly\r\n" \
239 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200240 "L: p:20\r\n" \
241 "\r\n" \
242 "v=0\r\n" \
243 "c=IN IP4 123.12.12.123\r\n" \
244 "m=audio 5904 RTP/AVP 97\r\n" \
245 "a=rtpmap:97 GSM-EFR/8000\r\n" \
246 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200247
Philipp Maier87bd9be2017-08-22 16:35:41 +0200248#define CRCX_RET \
249 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100250 "I: %s\r\n" \
251 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200252 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100253 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200254 "s=-\r\n" \
255 "c=IN IP4 0.0.0.0\r\n" \
256 "t=0 0\r\n" \
257 "m=audio 16002 RTP/AVP 97\r\n" \
258 "a=rtpmap:97 GSM-EFR/8000\r\n" \
259 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200260
Philipp Maier87bd9be2017-08-22 16:35:41 +0200261#define CRCX_RET_NO_RTPMAP \
262 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100263 "I: %s\r\n" \
264 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200265 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100266 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200267 "s=-\r\n" \
268 "c=IN IP4 0.0.0.0\r\n" \
269 "t=0 0\r\n" \
270 "m=audio 16002 RTP/AVP 97\r\n" \
271 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200272
Philipp Maier87bd9be2017-08-22 16:35:41 +0200273#define CRCX_FMTP_RET \
274 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100275 "I: %s\r\n" \
276 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200277 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100278 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200279 "s=-\r\n" \
280 "c=IN IP4 0.0.0.0\r\n" \
281 "t=0 0\r\n" \
282 "m=audio 16006 RTP/AVP 97\r\n" \
283 "a=rtpmap:97 GSM-EFR/8000\r\n" \
284 "a=fmtp:126 0/1/2\r\n" \
285 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200286
Philipp Maier87bd9be2017-08-22 16:35:41 +0200287#define CRCX_ZYN \
288 "CRCX 2 1@mgw MGCP 1.0\r" \
289 "M: recvonly\r" \
290 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200291 "\n" \
292 "v=0\r" \
293 "c=IN IP4 123.12.12.123\r" \
294 "m=audio 5904 RTP/AVP 97\r" \
295 "a=rtpmap:97 GSM-EFR/8000\r"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200296
Philipp Maier87bd9be2017-08-22 16:35:41 +0200297#define CRCX_ZYN_RET \
298 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100299 "I: %s\r\n" \
300 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200301 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100302 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200303 "s=-\r\n" \
304 "c=IN IP4 0.0.0.0\r\n" \
305 "t=0 0\r\n" \
306 "m=audio 16004 RTP/AVP 97\r\n" \
307 "a=rtpmap:97 GSM-EFR/8000\r\n" \
308 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200309
Philipp Maier87bd9be2017-08-22 16:35:41 +0200310#define DLCX \
311 "DLCX 7 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100312 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200313 "C: 2\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200314
Philipp Maier87bd9be2017-08-22 16:35:41 +0200315#define DLCX_RET \
316 "250 7 OK\r\n" \
317 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n" \
318 "X-Osmo-CP: EC TI=0, TO=0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200319
Philipp Maier87bd9be2017-08-22 16:35:41 +0200320#define RQNT \
321 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
322 "X: B244F267488\r\n" \
323 "S: D/9\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200324
Philipp Maier87bd9be2017-08-22 16:35:41 +0200325#define RQNT2 \
326 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
327 "X: ADD4F26746F\r\n" \
328 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329
330#define RQNT1_RET "200 186908780 OK\r\n"
331#define RQNT2_RET "200 186908781 OK\r\n"
332
Philipp Maier87bd9be2017-08-22 16:35:41 +0200333#define PTYPE_IGNORE 0 /* == default initializer */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200334#define PTYPE_NONE 128
335#define PTYPE_NYI PTYPE_NONE
336
Philipp Maier87bd9be2017-08-22 16:35:41 +0200337#define CRCX_MULT_1 \
338 "CRCX 2 1@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200339 "M: recvonly\r\n" \
340 "C: 2\r\n" \
341 "X\r\n" \
342 "L: p:20\r\n" \
343 "\r\n" \
344 "v=0\r\n" \
345 "c=IN IP4 123.12.12.123\r\n" \
346 "m=audio 5904 RTP/AVP 18 97\r\n" \
347 "a=rtpmap:18 G729/8000\r\n" \
348 "a=rtpmap:97 GSM-EFR/8000\r\n" \
349 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200350
Philipp Maier87bd9be2017-08-22 16:35:41 +0200351#define CRCX_MULT_2 \
352 "CRCX 2 2@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200353 "M: recvonly\r\n" \
354 "C: 2\r\n" \
355 "X\r\n" \
356 "L: p:20\r\n" \
357 "\r\n" \
358 "v=0\r\n" \
359 "c=IN IP4 123.12.12.123\r\n" \
360 "m=audio 5904 RTP/AVP 18 97 101\r\n" \
361 "a=rtpmap:18 G729/8000\r\n" \
362 "a=rtpmap:97 GSM-EFR/8000\r\n" \
363 "a=rtpmap:101 FOO/8000\r\n" \
364 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200365
Philipp Maier87bd9be2017-08-22 16:35:41 +0200366#define CRCX_MULT_3 \
367 "CRCX 2 3@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200368 "M: recvonly\r\n" \
369 "C: 2\r\n" \
370 "X\r\n" \
371 "L: p:20\r\n" \
372 "\r\n" \
373 "v=0\r\n" \
374 "c=IN IP4 123.12.12.123\r\n" \
375 "m=audio 5904 RTP/AVP\r\n" \
376 "a=rtpmap:18 G729/8000\r\n" \
377 "a=rtpmap:97 GSM-EFR/8000\r\n" \
378 "a=rtpmap:101 FOO/8000\r\n" \
379 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200380
Philipp Maier87bd9be2017-08-22 16:35:41 +0200381#define CRCX_MULT_4 \
382 "CRCX 2 4@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200383 "M: recvonly\r\n" \
384 "C: 2\r\n" \
385 "X\r\n" \
386 "L: p:20\r\n" \
387 "\r\n" \
388 "v=0\r\n" \
389 "c=IN IP4 123.12.12.123\r\n" \
390 "m=audio 5904 RTP/AVP 18\r\n" \
391 "a=rtpmap:18 G729/8000\r\n" \
392 "a=rtpmap:97 GSM-EFR/8000\r\n" \
393 "a=rtpmap:101 FOO/8000\r\n" \
394 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200395
396#define CRCX_MULT_GSM_EXACT \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200397 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
398 "C: 1355c6041e\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399 "L: p:20, a:GSM, nt:IN\r\n" \
400 "M: recvonly\r\n" \
401 "\r\n" \
402 "v=0\r\n" \
403 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
404 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
405 "t=0 0\r\nm=audio 29084 RTP/AVP 255 0 8 3 18 4 96 97 101\r\n" \
406 "a=rtpmap:0 PCMU/8000\r\n" \
407 "a=rtpmap:8 PCMA/8000\r\n" \
408 "a=rtpmap:3 gsm/8000\r\n" \
409 "a=rtpmap:18 G729/8000\r\n" \
410 "a=fmtp:18 annexb=no\r\n" \
411 "a=rtpmap:4 G723/8000\r\n" \
412 "a=rtpmap:96 iLBC/8000\r\n" \
413 "a=fmtp:96 mode=20\r\n" \
414 "a=rtpmap:97 iLBC/8000\r\n" \
415 "a=fmtp:97 mode=30\r\n" \
416 "a=rtpmap:101 telephone-event/8000\r\n" \
417 "a=fmtp:101 0-15\r\n" \
418 "a=recvonly\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200419
Philipp Maier87bd9be2017-08-22 16:35:41 +0200420#define MDCX_NAT_DUMMY \
421 "MDCX 23 5@mgw MGCP 1.0\r\n" \
422 "C: 1355c6041e\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100423 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200424 "\r\n" \
425 "c=IN IP4 8.8.8.8\r\n" \
426 "m=audio 16434 RTP/AVP 255\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200427
428struct mgcp_test {
429 const char *name;
430 const char *req;
431 const char *exp_resp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200432 int ptype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200433 const char *extra_fmtp;
434};
435
436static const struct mgcp_test tests[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437 {"AUEP1", AUEP1, AUEP1_RET},
438 {"AUEP2", AUEP2, AUEP2_RET},
439 {"MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET},
440 {"MDCX2", MDCX_UNALLOCATED, MDCX_RET},
441 {"CRCX", CRCX, CRCX_RET, 97},
442 {"MDCX3", MDCX3, MDCX3_RET, PTYPE_IGNORE},
443 {"MDCX4", MDCX4, MDCX4_RET("18983216"), 99},
444 {"MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99},
445 {"MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99},
446 {"MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99},
447 {"MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99},
448 {"MDCX4_RO", MDCX4_RO, MDCX4_RO_RET("18983221"), PTYPE_IGNORE},
449 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
450 {"CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97},
451 {"EMPTY", EMPTY, EMPTY_RET},
452 {"SHORT1", SHORT, SHORT_RET},
453 {"SHORT2", SHORT2, SHORT2_RET},
454 {"SHORT3", SHORT3, SHORT2_RET},
455 {"SHORT4", SHORT4, SHORT2_RET},
456 {"RQNT1", RQNT, RQNT1_RET},
457 {"RQNT2", RQNT2, RQNT2_RET},
458 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
459 {"CRCX", CRCX, CRCX_FMTP_RET, 97,.extra_fmtp = "a=fmtp:126 0/1/2"},
460 {"MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE,.extra_fmtp =
461 "a=fmtp:126 0/1/2"},
462 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE,.extra_fmtp = "a=fmtp:126 0/1/2"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200463};
464
465static const struct mgcp_test retransmit[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200466 {"CRCX", CRCX, CRCX_RET},
467 {"RQNT1", RQNT, RQNT1_RET},
468 {"RQNT2", RQNT2, RQNT2_RET},
469 {"MDCX3", MDCX3, MDCX3A_RET},
470 {"DLCX", DLCX, DLCX_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200471};
472
Philipp Maierffd75e42017-11-22 11:44:50 +0100473static struct msgb *create_msg(const char *str, const char *conn_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200474{
475 struct msgb *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100476 int len;
477
478 printf("creating message from statically defined input:\n");
479 printf("---------8<---------\n%s\n---------8<---------\n", str);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200480
481 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Philipp Maierffd75e42017-11-22 11:44:50 +0100482 if (conn_id && strlen(conn_id))
483 len = sprintf((char *)msg->data, str, conn_id, conn_id);
484 else
485 len = sprintf((char *)msg->data, "%s", str);
486
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200487 msg->l2h = msgb_put(msg, len);
488 return msg;
489}
490
491static int last_endpoint = -1;
492
493static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
494 int state, const char *transactio_id)
495{
496 fprintf(stderr, "Policy CB got state %d on endpoint %d\n",
497 state, endpoint);
498 last_endpoint = endpoint;
499 return MGCP_POLICY_CONT;
500}
501
502#define MGCP_DUMMY_LOAD 0x23
503static int dummy_packets = 0;
504/* override and forward */
505ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200506 const struct sockaddr *dest_addr, socklen_t addrlen)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200507{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200508 uint32_t dest_host =
509 htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
510 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200511
Philipp Maier87bd9be2017-08-22 16:35:41 +0200512 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
513 fprintf(stderr,
514 "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
515 dest_host, dest_port, len, osmo_hexdump(buf, len));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200516 dummy_packets += 1;
517 }
518
Philipp Maier3d9b6562017-10-13 18:33:44 +0200519 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200520}
521
522static int64_t force_monotonic_time_us = -1;
523/* override and forward */
524int clock_gettime(clockid_t clk_id, struct timespec *tp)
525{
526 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
527 static clock_gettime_t real_clock_gettime = NULL;
528
529 if (!real_clock_gettime)
530 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
531
532 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
533 tp->tv_sec = force_monotonic_time_us / 1000000;
534 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
535 return 0;
536 }
537
538 return real_clock_gettime(clk_id, tp);
539}
540
541#define CONN_UNMODIFIED (0x1000)
542
543static void test_values(void)
544{
545 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200546 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200547
548 /* Check that LOOPBACK enables all output */
549 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200550 MGCP_CONN_RECV_SEND);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200551}
552
Philipp Maierffd75e42017-11-22 11:44:50 +0100553/* Extract a connection ID from a response (CRCX) */
554static int get_conn_id_from_response(uint8_t *resp, char *conn_id,
555 unsigned int conn_id_len)
556{
557 char *conn_id_ptr;
558 int i;
Philipp Maier55295f72018-01-15 14:00:28 +0100559 bool got_conn_id = false;
Philipp Maierffd75e42017-11-22 11:44:50 +0100560
Philipp Maier55295f72018-01-15 14:00:28 +0100561 /* First try to get the conn_id from the I: parameter */
Philipp Maierffd75e42017-11-22 11:44:50 +0100562 conn_id_ptr = strstr((char *)resp, "I: ");
Philipp Maier55295f72018-01-15 14:00:28 +0100563 if (conn_id_ptr) {
564 memset(conn_id, 0, conn_id_len);
565 memcpy(conn_id, conn_id_ptr + 3, 32);
566 got_conn_id = true;
567 } else {
568 /* Alternatively try to extract the conn_id from the o=- SDP
569 * parameter */
570 conn_id_ptr = strstr((char *)resp, "o=- ");
571 if(conn_id_ptr) {
572 memset(conn_id, 0, conn_id_len);
573 memcpy(conn_id, conn_id_ptr + 4, 32);
574 got_conn_id = true;
575 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100576 }
577
Philipp Maier55295f72018-01-15 14:00:28 +0100578 if (got_conn_id) {
579 for (i = 0; i < conn_id_len; i++) {
580 if (conn_id[i] == '\n' || conn_id[i] == '\r')
581 conn_id[i] = '\0';
582 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100583
Philipp Maier55295f72018-01-15 14:00:28 +0100584 /* A valid conn_id must at least contain one digit, and must
585 * not exceed a length of 32 digits */
586 OSMO_ASSERT(strlen(conn_id) <= 32);
587 OSMO_ASSERT(strlen(conn_id) > 0);
588
589 return 0;
590 }
591 return -EINVAL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100592}
593
594/* Check response, automatically patch connection ID if needed */
595static int check_response(uint8_t *resp, const char *exp_resp)
596{
597 char exp_resp_patched[4096];
598 const char *exp_resp_ptr;
599 char conn_id[256];
600
601 printf("checking response:\n");
602
603 /* If the expected response is intened to be patched
604 * (%s placeholder inside) we will patch it with the
605 * connection identifier we just received from the
606 * real response. This is necessary because the CI
607 * is generated by the mgcp code on CRCX and we can
608 * not know it in advance */
609 if (strstr(exp_resp, "%s")) {
610 if (get_conn_id_from_response(resp, conn_id, sizeof(conn_id)) ==
611 0) {
612 sprintf(exp_resp_patched, exp_resp, conn_id, conn_id);
613 exp_resp_ptr = exp_resp_patched;
614 printf
615 ("using message with patched conn_id for comparison\n");
616 } else {
617 printf
618 ("patching conn_id failed, using message as statically defined for comparison\n");
619 exp_resp_ptr = exp_resp;
620 }
621 } else {
622 printf("using message as statically defined for comparison\n");
623 exp_resp_ptr = exp_resp;
624 }
625
626 if (strcmp((char *)resp, exp_resp_ptr) != 0) {
627 printf("Unexpected response, please check!\n");
628 printf
629 ("Got:\n---------8<---------\n%s\n---------8<---------\n\n",
630 resp);
631 printf
632 ("Expected:\n---------8<---------\n%s\n---------8<---------\n",
633 exp_resp_ptr);
634 return -EINVAL;
635 }
636
637 printf("Response matches our expectations.\n");
638 return 0;
639}
640
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200641static void test_messages(void)
642{
643 struct mgcp_config *cfg;
644 struct mgcp_endpoint *endp;
645 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200646 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100647 char last_conn_id[256];
Philipp Maier7df419b2017-12-04 17:11:42 +0100648 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200649
650 cfg = mgcp_config_alloc();
651
Philipp Maierfcd06552017-11-10 17:32:22 +0100652 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200653 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200654 cfg->policy_cb = mgcp_test_policy_cb;
655
Philipp Maierffd75e42017-11-22 11:44:50 +0100656 memset(last_conn_id, 0, sizeof(last_conn_id));
657
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200658 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
659
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200660 for (i = 0; i < ARRAY_SIZE(tests); i++) {
661 const struct mgcp_test *t = &tests[i];
662 struct msgb *inp;
663 struct msgb *msg;
664
Philipp Maierffd75e42017-11-22 11:44:50 +0100665 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200666 printf("Testing %s\n", t->name);
667
668 last_endpoint = -1;
669 dummy_packets = 0;
670
Philipp Maier87bd9be2017-08-22 16:35:41 +0200671 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
672 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200673
Philipp Maierffd75e42017-11-22 11:44:50 +0100674 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200675 msg = mgcp_handle_message(cfg, inp);
676 msgb_free(inp);
677 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200678 if (msg) {
679 printf("%s failed '%s'\n", t->name,
680 (char *)msg->data);
681 OSMO_ASSERT(false);
682 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100683 } else if (check_response(msg->data, t->exp_resp) != 0) {
684 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200685 OSMO_ASSERT(false);
686 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100687
Philipp Maier7df419b2017-12-04 17:11:42 +0100688 if (msg) {
689 rc = get_conn_id_from_response(msg->data, last_conn_id,
690 sizeof(last_conn_id));
691 if (rc)
692 printf("(response contains a connection id)\n");
693 else
694 printf("(response does not contain a connection id)\n");
695 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100696
Philipp Maiera330b862017-12-04 17:16:16 +0100697 if (msg)
698 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200699
700 if (dummy_packets)
701 printf("Dummy packets: %d\n", dummy_packets);
702
703 if (last_endpoint != -1) {
704 endp = &cfg->trunk.endpoints[last_endpoint];
705
Philipp Maier01d24a32017-11-21 17:26:09 +0100706 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200707 if (conn) {
708 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200709
Philipp Maier87bd9be2017-08-22 16:35:41 +0200710 if (conn->end.packet_duration_ms != -1)
711 printf("Detected packet duration: %d\n",
712 conn->end.packet_duration_ms);
713 else
714 printf("Packet duration not set\n");
715 if (endp->local_options.pkt_period_min ||
716 endp->local_options.pkt_period_max)
717 printf
718 ("Requested packetetization period: "
719 "%d-%d\n",
720 endp->local_options.pkt_period_min,
721 endp->
722 local_options.pkt_period_max);
723 else
724 printf
725 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200726
Philipp Maier87bd9be2017-08-22 16:35:41 +0200727 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
728 printf("Connection mode: %d:%s%s%s%s\n",
729 conn->conn->mode,
730 !conn->conn->mode ? " NONE" : "",
731 conn->conn->mode & MGCP_CONN_SEND_ONLY
732 ? " SEND" : "",
733 conn->conn->mode & MGCP_CONN_RECV_ONLY
734 ? " RECV" : "",
735 conn->conn->mode & MGCP_CONN_LOOPBACK
736 & ~MGCP_CONN_RECV_SEND
737 ? " LOOP" : "");
738 fprintf(stderr,
739 "RTP output %sabled, NET output %sabled\n",
740 conn->end.output_enabled
741 ? "en" : "dis",
742 conn->end.output_enabled
743 ? "en" : "dis");
744 } else
745 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746
Philipp Maier87bd9be2017-08-22 16:35:41 +0200747 OSMO_ASSERT(conn->end.output_enabled
748 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
749
750 conn->conn->mode |= CONN_UNMODIFIED;
751
752 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200753 endp->local_options.pkt_period_min = 0;
754 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200755 }
756
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100758 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200759 OSMO_ASSERT(last_endpoint != -1);
760 endp = &cfg->trunk.endpoints[last_endpoint];
761
762 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200763 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200764 last_endpoint,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200765 conn->end.codec.payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200766
Philipp Maier87bd9be2017-08-22 16:35:41 +0200767 if (t->ptype != PTYPE_IGNORE)
768 OSMO_ASSERT(conn->end.codec.payload_type ==
769 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200770
771 /* Reset them again for next test */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200772 conn->end.codec.payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200773 }
774 }
775
776 talloc_free(cfg);
777}
778
779static void test_retransmission(void)
780{
781 struct mgcp_config *cfg;
782 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100783 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100784 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200785
786 cfg = mgcp_config_alloc();
787
Philipp Maierfcd06552017-11-10 17:32:22 +0100788 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200789 mgcp_endpoints_allocate(&cfg->trunk);
790
Philipp Maierffd75e42017-11-22 11:44:50 +0100791 memset(last_conn_id, 0, sizeof(last_conn_id));
792
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200793 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
794
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200795 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
796 const struct mgcp_test *t = &retransmit[i];
797 struct msgb *inp;
798 struct msgb *msg;
799
Philipp Maierffd75e42017-11-22 11:44:50 +0100800 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200801 printf("Testing %s\n", t->name);
802
Philipp Maierffd75e42017-11-22 11:44:50 +0100803 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200804 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200805
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200806 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100807 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200808 printf("%s failed '%s'\n", t->name, (char *)msg->data);
809 OSMO_ASSERT(false);
810 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100811
Philipp Maier23b8e292017-12-04 16:48:45 +0100812 if (msg && strcmp(t->name, "CRCX") == 0) {
813 rc = get_conn_id_from_response(msg->data, last_conn_id,
814 sizeof(last_conn_id));
815 OSMO_ASSERT(rc == 0);
816 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100817
Philipp Maier7cedfd72017-12-04 16:49:12 +0100818 if (msg)
819 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200820
821 /* Retransmit... */
822 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100823 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200824 msg = mgcp_handle_message(cfg, inp);
825 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100826 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827 printf("%s failed '%s'\n", t->name, (char *)msg->data);
828 OSMO_ASSERT(false);
829 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200830 msgb_free(msg);
831 }
832
833 talloc_free(cfg);
834}
835
836static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
837{
838 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200839 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200840 return 0;
841}
842
843static void test_rqnt_cb(void)
844{
845 struct mgcp_config *cfg;
846 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100847 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200848
849 cfg = mgcp_config_alloc();
850 cfg->rqnt_cb = rqnt_cb;
851
Philipp Maierfcd06552017-11-10 17:32:22 +0100852 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853 mgcp_endpoints_allocate(&cfg->trunk);
854
855 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
856
Philipp Maierffd75e42017-11-22 11:44:50 +0100857 inp = create_msg(CRCX, NULL);
858 msg = mgcp_handle_message(cfg, inp);
859 OSMO_ASSERT(msg);
860 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
861 sizeof(conn_id)) == 0);
862 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200863 msgb_free(inp);
864
865 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100866 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200867 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200868 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200869 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
870 abort();
871 }
872
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200874 printf("FAILED: callback not called: %p\n", cfg->data);
875 abort();
876 }
877
878 msgb_free(msg);
879 msgb_free(inp);
880
Philipp Maierffd75e42017-11-22 11:44:50 +0100881 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200882 msgb_free(mgcp_handle_message(cfg, inp));
883 msgb_free(inp);
884 talloc_free(cfg);
885}
886
887struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200888 int cycles;
889 uint16_t base_seq;
890 uint16_t max_seq;
891 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200892
Philipp Maier87bd9be2017-08-22 16:35:41 +0200893 uint32_t expected;
894 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200895};
896
897static const struct pl_test pl_test_dat[] = {
898 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200899 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
900 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200902 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
903 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200905 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
906 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200907 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200908 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
909 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200910 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200911 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
912 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200913};
914
915static void test_packet_loss_calc(void)
916{
917 int i;
918 printf("Testing packet loss calculation.\n");
919
920 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
921 uint32_t expected;
922 int loss;
923 struct mgcp_rtp_state state;
924 struct mgcp_rtp_end rtp;
925 memset(&state, 0, sizeof(state));
926 memset(&rtp, 0, sizeof(rtp));
927
Harald Welte49e3d5a2017-12-25 09:47:57 +0100928 state.stats.initialized = 1;
929 state.stats.base_seq = pl_test_dat[i].base_seq;
930 state.stats.max_seq = pl_test_dat[i].max_seq;
931 state.stats.cycles = pl_test_dat[i].cycles;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200932
Harald Weltea0ac30f2017-12-25 09:52:30 +0100933 rtp.stats.packets_rx = pl_test_dat[i].packets;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200934 calc_loss(&state, &rtp, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200935
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936 if (loss != pl_test_dat[i].loss
937 || expected != pl_test_dat[i].expected) {
938 printf
939 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
940 i, loss, pl_test_dat[i].loss, expected,
941 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200942 }
943 }
944}
945
Philipp Maier87bd9be2017-08-22 16:35:41 +0200946int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
947 uint32_t *pr, uint32_t *_or, int *loss,
948 uint32_t *jitter)
949{
950 char *line, *save;
951 int rc;
952
953 /* initialize with bad values */
954 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
955 *loss = INT_MAX;
956
957 line = strtok_r((char *)msg->l2h, "\r\n", &save);
958 if (!line)
959 return -1;
960
961 /* this can only parse the message that is created above... */
962 for_each_non_empty_line(line, save) {
963 switch (line[0]) {
964 case 'P':
965 rc = sscanf(line,
966 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
967 ps, os, pr, _or, loss, jitter);
968 return rc == 6 ? 0 : -1;
969 }
970 }
971
972 return -1;
973}
974
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200975static void test_mgcp_stats(void)
976{
977 printf("Testing stat parsing\n");
978
979 uint32_t bps, bos, pr, _or, jitter;
980 struct msgb *msg;
981 int loss;
982 int rc;
983
Philipp Maierffd75e42017-11-22 11:44:50 +0100984 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200985 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
986 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200987 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
988 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200989 printf("FAIL: Parsing failed1.\n");
990 msgb_free(msg);
991
Philipp Maier87bd9be2017-08-22 16:35:41 +0200992 msg =
993 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +0100994 ("250 7 OK\r\nP: PS=10, OS=20, PR=30, OR=40, PL=-3, JI=40\r\n", NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200995 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
996 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200997 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
998 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200999 printf("FAIL: Parsing failed2.\n");
1000 msgb_free(msg);
1001}
1002
1003struct rtp_packet_info {
1004 float txtime;
1005 int len;
1006 char *data;
1007};
1008
1009struct rtp_packet_info test_rtp_packets1[] = {
1010 /* RTP: SeqNo=0, TS=0 */
1011 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001012 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001013 /* RTP: SeqNo=1, TS=160 */
1014 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001015 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001016 /* RTP: SeqNo=2, TS=320 */
1017 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001018 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001019 /* Repeat RTP timestamp: */
1020 /* RTP: SeqNo=3, TS=320 */
1021 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001022 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001023 /* RTP: SeqNo=4, TS=480 */
1024 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001026 /* RTP: SeqNo=5, TS=640 */
1027 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001028 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001029 /* Double skip RTP timestamp (delta = 2*160): */
1030 /* RTP: SeqNo=6, TS=960 */
1031 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001032 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001033 /* RTP: SeqNo=7, TS=1120 */
1034 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001035 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001036 /* RTP: SeqNo=8, TS=1280 */
1037 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001038 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001039 /* Non 20ms RTP timestamp (delta = 120): */
1040 /* RTP: SeqNo=9, TS=1400 */
1041 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001042 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001043 /* RTP: SeqNo=10, TS=1560 */
1044 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001045 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001046 /* RTP: SeqNo=11, TS=1720 */
1047 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001048 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001049 /* SSRC changed to 0x10203040, RTP timestamp jump */
1050 /* RTP: SeqNo=12, TS=34688 */
1051 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001052 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001053 /* RTP: SeqNo=13, TS=34848 */
1054 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001055 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001056 /* RTP: SeqNo=14, TS=35008 */
1057 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001058 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001059 /* Non 20ms RTP timestamp (delta = 120): */
1060 /* RTP: SeqNo=15, TS=35128 */
1061 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001062 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001063 /* RTP: SeqNo=16, TS=35288 */
1064 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001065 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001066 /* RTP: SeqNo=17, TS=35448 */
1067 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001068 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001069 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1070 /* RTP: SeqNo=19, TS=35768 */
1071 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001072 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001073 /* RTP: SeqNo=20, TS=35928 */
1074 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001075 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001076 /* RTP: SeqNo=21, TS=36088 */
1077 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001078 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001079 /* Repeat last packet */
1080 /* RTP: SeqNo=21, TS=36088 */
1081 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001082 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001083 /* RTP: SeqNo=22, TS=36248 */
1084 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001085 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001086 /* RTP: SeqNo=23, TS=36408 */
1087 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001088 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001089 /* Don't increment SeqNo but increment timestamp by 160 */
1090 /* RTP: SeqNo=23, TS=36568 */
1091 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001092 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001093 /* RTP: SeqNo=24, TS=36728 */
1094 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001095 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001096 /* RTP: SeqNo=25, TS=36888 */
1097 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001098 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001099 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1100 * SeqNo jump */
1101 /* RTP: SeqNo=1000, TS=160000 */
1102 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001103 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104 /* RTP: SeqNo=1001, TS=160160 */
1105 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001106 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001107 /* RTP: SeqNo=1002, TS=160320 */
1108 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001109 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001110};
1111
Philipp Maier87bd9be2017-08-22 16:35:41 +02001112void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1113 struct mgcp_rtp_state *state,
1114 struct mgcp_rtp_end *rtp_end,
1115 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001116
1117static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1118{
1119 int i;
1120
1121 struct mgcp_trunk_config trunk;
1122 struct mgcp_endpoint endp;
1123 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001124 struct mgcp_rtp_end *rtp;
1125 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001126 char buffer[4096];
1127 uint32_t last_ssrc = 0;
1128 uint32_t last_timestamp = 0;
1129 uint32_t last_seqno = 0;
1130 int last_in_ts_err_cnt = 0;
1131 int last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001132 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001133 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001134
1135 printf("Testing packet error detection%s%s.\n",
1136 patch_ssrc ? ", patch SSRC" : "",
1137 patch_ts ? ", patch timestamps" : "");
1138
1139 memset(&trunk, 0, sizeof(trunk));
1140 memset(&endp, 0, sizeof(endp));
1141 memset(&state, 0, sizeof(state));
1142
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143 endp.type = &ep_typeset.rtp;
1144
Philipp Maierfcd06552017-11-10 17:32:22 +01001145 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001146 trunk.endpoints = &endp;
1147 trunk.force_constant_ssrc = patch_ssrc;
1148 trunk.force_aligned_timing = patch_ts;
1149
1150 endp.tcfg = &trunk;
1151
Philipp Maier87bd9be2017-08-22 16:35:41 +02001152 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001153 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1154 "test-connection");
1155 OSMO_ASSERT(_conn);
1156 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001157 OSMO_ASSERT(conn);
1158
1159 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001160
1161 rtp->codec.payload_type = 98;
1162
1163 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1164 struct rtp_packet_info *info = test_rtp_packets1 + i;
1165
1166 force_monotonic_time_us = round(1000000.0 * info->txtime);
1167
1168 OSMO_ASSERT(info->len <= sizeof(buffer));
1169 OSMO_ASSERT(info->len >= 0);
1170 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001171 mgcp_rtp_end_config(&endp, 1, rtp);
1172
1173 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1174 buffer, info->len);
1175
1176 if (state.out_stream.ssrc != last_ssrc) {
1177 printf("Output SSRC changed to %08x\n",
1178 state.out_stream.ssrc);
1179 last_ssrc = state.out_stream.ssrc;
1180 }
1181
1182 printf("In TS: %d, dTS: %d, Seq: %d\n",
1183 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001184 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185
1186 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
1187 "TS Err change: in %+d, out %+d\n",
1188 state.out_stream.last_timestamp - last_timestamp,
1189 state.out_stream.last_tsdelta,
1190 state.out_stream.last_seq - last_seqno,
1191 state.in_stream.err_ts_counter - last_in_ts_err_cnt,
1192 state.out_stream.err_ts_counter - last_out_ts_err_cnt);
1193
1194 printf("Stats: Jitter = %u, Transit = %d\n",
Harald Welte49e3d5a2017-12-25 09:47:57 +01001195 calc_jitter(&state), state.stats.transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001196
1197 last_in_ts_err_cnt = state.in_stream.err_ts_counter;
1198 last_out_ts_err_cnt = state.out_stream.err_ts_counter;
1199 last_timestamp = state.out_stream.last_timestamp;
1200 last_seqno = state.out_stream.last_seq;
1201 }
1202
1203 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001204 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001205}
1206
1207static void test_multilple_codec(void)
1208{
1209 struct mgcp_config *cfg;
1210 struct mgcp_endpoint *endp;
1211 struct msgb *inp, *resp;
1212 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001213 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001214 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001215
1216 printf("Testing multiple payload types\n");
1217
1218 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001219 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001220 mgcp_endpoints_allocate(&cfg->trunk);
1221 cfg->policy_cb = mgcp_test_policy_cb;
1222 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1223
1224 /* Allocate endpoint 1@mgw with two codecs */
1225 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001226 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001227 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001228 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1229 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001230 msgb_free(inp);
1231 msgb_free(resp);
1232
1233 OSMO_ASSERT(last_endpoint == 1);
1234 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001235 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001236 OSMO_ASSERT(conn);
1237 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1238 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001239
1240 /* Allocate 2@mgw with three codecs, last one ignored */
1241 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001242 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001243 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001244 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1245 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001246 msgb_free(inp);
1247 msgb_free(resp);
1248
1249 OSMO_ASSERT(last_endpoint == 2);
1250 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001251 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252 OSMO_ASSERT(conn);
1253 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1254 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001255
1256 /* Allocate 3@mgw with no codecs, check for PT == -1 */
1257 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001258 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001259 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001260 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1261 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001262 msgb_free(inp);
1263 msgb_free(resp);
1264
1265 OSMO_ASSERT(last_endpoint == 3);
1266 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001267 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001268 OSMO_ASSERT(conn);
1269 OSMO_ASSERT(conn->end.codec.payload_type == -1);
1270 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001271
1272 /* Allocate 4@mgw with a single codec */
1273 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001274 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001275 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001276 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1277 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001278 msgb_free(inp);
1279 msgb_free(resp);
1280
1281 OSMO_ASSERT(last_endpoint == 4);
1282 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001283 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001284 OSMO_ASSERT(conn);
1285 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1286 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001287
1288 /* Allocate 5@mgw at select GSM.. */
1289 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001290 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001291 talloc_free(cfg->trunk.audio_name);
1292 cfg->trunk.audio_name = "GSM/8000";
1293 cfg->trunk.no_audio_transcoding = 1;
1294 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001295 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1296 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001297 msgb_free(inp);
1298 msgb_free(resp);
1299
1300 OSMO_ASSERT(last_endpoint == 5);
1301 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001302 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001303 OSMO_ASSERT(conn);
1304 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1305 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001306
Philipp Maierffd75e42017-11-22 11:44:50 +01001307 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001308 last_endpoint = -1;
1309 resp = mgcp_handle_message(cfg, inp);
1310 msgb_free(inp);
1311 msgb_free(resp);
1312 OSMO_ASSERT(last_endpoint == 5);
1313 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001314 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001315 OSMO_ASSERT(conn);
1316 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1317 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
1318 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001319 memset(&addr, 0, sizeof(addr));
1320 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001321 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001322
1323 /* Check what happens without that flag */
1324
Philipp Maier87bd9be2017-08-22 16:35:41 +02001325 /* Free the previous endpoint and the data and
1326 * check if the connection really vanished... */
Philipp Maier1355d7e2018-02-01 14:30:06 +01001327 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001328 talloc_free(endp->last_response);
1329 talloc_free(endp->last_trans);
1330 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001331 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001332 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001333
1334 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001335 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001336 cfg->trunk.no_audio_transcoding = 0;
1337 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001338 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1339 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001340 msgb_free(inp);
1341 msgb_free(resp);
1342
1343 OSMO_ASSERT(last_endpoint == 5);
1344 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001345 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001346 OSMO_ASSERT(conn);
1347 OSMO_ASSERT(conn->end.codec.payload_type == 255);
1348 OSMO_ASSERT(conn->end.alt_codec.payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001349
1350 talloc_free(cfg);
1351}
1352
1353static void test_no_cycle(void)
1354{
1355 struct mgcp_config *cfg;
1356 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001357 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001358 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001359
1360 printf("Testing no sequence flow on initial packet\n");
1361
1362 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001363 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001364 mgcp_endpoints_allocate(&cfg->trunk);
1365
1366 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001367
Philipp Maierffd75e42017-11-22 11:44:50 +01001368 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1369 "test-connection");
1370 OSMO_ASSERT(_conn);
1371 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001372 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001373
Harald Welte49e3d5a2017-12-25 09:47:57 +01001374 OSMO_ASSERT(conn->state.stats.initialized == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001375
1376 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001377 OSMO_ASSERT(conn->state.stats.initialized == 1);
1378 OSMO_ASSERT(conn->state.stats.cycles == 0);
1379 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001380
1381 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001382 OSMO_ASSERT(conn->state.stats.initialized == 1);
1383 OSMO_ASSERT(conn->state.stats.cycles == 0);
1384 OSMO_ASSERT(conn->state.stats.max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001385
1386 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001387 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001388 OSMO_ASSERT(conn->state.stats.initialized == 1);
1389 OSMO_ASSERT(conn->state.stats.cycles == 0);
1390 OSMO_ASSERT(conn->state.stats.max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001391
1392 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001393 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001394 OSMO_ASSERT(conn->state.stats.initialized == 1);
1395 OSMO_ASSERT(conn->state.stats.cycles == UINT16_MAX + 1);
1396 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001397
Philipp Maier1355d7e2018-02-01 14:30:06 +01001398 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001399 talloc_free(cfg);
1400}
1401
1402static void test_no_name(void)
1403{
1404 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001405 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001406
1407 printf("Testing no rtpmap name\n");
1408 cfg = mgcp_config_alloc();
1409
Philipp Maierfcd06552017-11-10 17:32:22 +01001410 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001411 cfg->trunk.audio_send_name = 0;
1412 mgcp_endpoints_allocate(&cfg->trunk);
1413
1414 cfg->policy_cb = mgcp_test_policy_cb;
1415
1416 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1417
Philipp Maierffd75e42017-11-22 11:44:50 +01001418 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001419 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001420
1421 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001422 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001423 (char *)msg->data);
1424 OSMO_ASSERT(false);
1425 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001426 msgb_free(inp);
1427 msgb_free(msg);
1428
Philipp Maier1355d7e2018-02-01 14:30:06 +01001429 mgcp_endp_release(&cfg->trunk.endpoints[1]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001430 talloc_free(cfg);
1431}
1432
1433static void test_osmux_cid(void)
1434{
1435 int id, i;
1436
1437 OSMO_ASSERT(osmux_used_cid() == 0);
1438 id = osmux_get_cid();
1439 OSMO_ASSERT(id == 0);
1440 OSMO_ASSERT(osmux_used_cid() == 1);
1441 osmux_put_cid(id);
1442 OSMO_ASSERT(osmux_used_cid() == 0);
1443
1444 for (i = 0; i < 256; ++i) {
1445 id = osmux_get_cid();
1446 OSMO_ASSERT(id == i);
1447 OSMO_ASSERT(osmux_used_cid() == i + 1);
1448 }
1449
1450 id = osmux_get_cid();
1451 OSMO_ASSERT(id == -1);
1452
1453 for (i = 0; i < 256; ++i)
1454 osmux_put_cid(i);
1455 OSMO_ASSERT(osmux_used_cid() == 0);
1456}
1457
1458static const struct log_info_cat log_categories[] = {
1459};
1460
1461const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001462 .cat = log_categories,
1463 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001464};
1465
1466int main(int argc, char **argv)
1467{
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001468 void *msgb_ctx = msgb_talloc_ctx_init(NULL, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001469 osmo_init_logging(&log_info);
1470
1471 test_strline();
1472 test_values();
1473 test_messages();
1474 test_retransmission();
1475 test_packet_loss_calc();
1476 test_rqnt_cb();
1477 test_mgcp_stats();
1478 test_packet_error_detection(1, 0);
1479 test_packet_error_detection(0, 0);
1480 test_packet_error_detection(0, 1);
1481 test_packet_error_detection(1, 1);
1482 test_multilple_codec();
1483 test_no_cycle();
1484 test_no_name();
1485 test_osmux_cid();
1486
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001487 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1488 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1489 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001490 printf("Done\n");
1491 return EXIT_SUCCESS;
1492}