blob: e1e62902374d49c3cd9e7c45bcbefe3a0b9c9076 [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" \
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100317 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n"
318
319 #define DLCX_RET_OSMUX DLCX_RET \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200320 "X-Osmo-CP: EC TI=0, TO=0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200321
Philipp Maier87bd9be2017-08-22 16:35:41 +0200322#define RQNT \
323 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
324 "X: B244F267488\r\n" \
325 "S: D/9\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200326
Philipp Maier87bd9be2017-08-22 16:35:41 +0200327#define RQNT2 \
328 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
329 "X: ADD4F26746F\r\n" \
330 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200331
332#define RQNT1_RET "200 186908780 OK\r\n"
333#define RQNT2_RET "200 186908781 OK\r\n"
334
Philipp Maier87bd9be2017-08-22 16:35:41 +0200335#define PTYPE_IGNORE 0 /* == default initializer */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200336#define PTYPE_NONE 128
337#define PTYPE_NYI PTYPE_NONE
338
Philipp Maier87bd9be2017-08-22 16:35:41 +0200339#define CRCX_MULT_1 \
340 "CRCX 2 1@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200341 "M: recvonly\r\n" \
342 "C: 2\r\n" \
343 "X\r\n" \
344 "L: p:20\r\n" \
345 "\r\n" \
346 "v=0\r\n" \
347 "c=IN IP4 123.12.12.123\r\n" \
348 "m=audio 5904 RTP/AVP 18 97\r\n" \
349 "a=rtpmap:18 G729/8000\r\n" \
350 "a=rtpmap:97 GSM-EFR/8000\r\n" \
351 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200352
Philipp Maier87bd9be2017-08-22 16:35:41 +0200353#define CRCX_MULT_2 \
354 "CRCX 2 2@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200355 "M: recvonly\r\n" \
356 "C: 2\r\n" \
357 "X\r\n" \
358 "L: p:20\r\n" \
359 "\r\n" \
360 "v=0\r\n" \
361 "c=IN IP4 123.12.12.123\r\n" \
362 "m=audio 5904 RTP/AVP 18 97 101\r\n" \
363 "a=rtpmap:18 G729/8000\r\n" \
364 "a=rtpmap:97 GSM-EFR/8000\r\n" \
365 "a=rtpmap:101 FOO/8000\r\n" \
366 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367
Philipp Maier87bd9be2017-08-22 16:35:41 +0200368#define CRCX_MULT_3 \
369 "CRCX 2 3@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200370 "M: recvonly\r\n" \
371 "C: 2\r\n" \
372 "X\r\n" \
373 "L: p:20\r\n" \
374 "\r\n" \
375 "v=0\r\n" \
376 "c=IN IP4 123.12.12.123\r\n" \
377 "m=audio 5904 RTP/AVP\r\n" \
378 "a=rtpmap:18 G729/8000\r\n" \
379 "a=rtpmap:97 GSM-EFR/8000\r\n" \
380 "a=rtpmap:101 FOO/8000\r\n" \
381 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200382
Philipp Maier87bd9be2017-08-22 16:35:41 +0200383#define CRCX_MULT_4 \
384 "CRCX 2 4@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200385 "M: recvonly\r\n" \
386 "C: 2\r\n" \
387 "X\r\n" \
388 "L: p:20\r\n" \
389 "\r\n" \
390 "v=0\r\n" \
391 "c=IN IP4 123.12.12.123\r\n" \
392 "m=audio 5904 RTP/AVP 18\r\n" \
393 "a=rtpmap:18 G729/8000\r\n" \
394 "a=rtpmap:97 GSM-EFR/8000\r\n" \
395 "a=rtpmap:101 FOO/8000\r\n" \
396 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200397
398#define CRCX_MULT_GSM_EXACT \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
400 "C: 1355c6041e\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200401 "L: p:20, a:GSM, nt:IN\r\n" \
402 "M: recvonly\r\n" \
403 "\r\n" \
404 "v=0\r\n" \
405 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
406 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
407 "t=0 0\r\nm=audio 29084 RTP/AVP 255 0 8 3 18 4 96 97 101\r\n" \
408 "a=rtpmap:0 PCMU/8000\r\n" \
409 "a=rtpmap:8 PCMA/8000\r\n" \
410 "a=rtpmap:3 gsm/8000\r\n" \
411 "a=rtpmap:18 G729/8000\r\n" \
412 "a=fmtp:18 annexb=no\r\n" \
413 "a=rtpmap:4 G723/8000\r\n" \
414 "a=rtpmap:96 iLBC/8000\r\n" \
415 "a=fmtp:96 mode=20\r\n" \
416 "a=rtpmap:97 iLBC/8000\r\n" \
417 "a=fmtp:97 mode=30\r\n" \
418 "a=rtpmap:101 telephone-event/8000\r\n" \
419 "a=fmtp:101 0-15\r\n" \
420 "a=recvonly\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200421
Philipp Maier87bd9be2017-08-22 16:35:41 +0200422#define MDCX_NAT_DUMMY \
423 "MDCX 23 5@mgw MGCP 1.0\r\n" \
424 "C: 1355c6041e\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100425 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200426 "\r\n" \
427 "c=IN IP4 8.8.8.8\r\n" \
428 "m=audio 16434 RTP/AVP 255\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200429
430struct mgcp_test {
431 const char *name;
432 const char *req;
433 const char *exp_resp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200434 int ptype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200435 const char *extra_fmtp;
436};
437
438static const struct mgcp_test tests[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200439 {"AUEP1", AUEP1, AUEP1_RET},
440 {"AUEP2", AUEP2, AUEP2_RET},
441 {"MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET},
442 {"MDCX2", MDCX_UNALLOCATED, MDCX_RET},
443 {"CRCX", CRCX, CRCX_RET, 97},
444 {"MDCX3", MDCX3, MDCX3_RET, PTYPE_IGNORE},
445 {"MDCX4", MDCX4, MDCX4_RET("18983216"), 99},
446 {"MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99},
447 {"MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99},
448 {"MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99},
449 {"MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99},
450 {"MDCX4_RO", MDCX4_RO, MDCX4_RO_RET("18983221"), PTYPE_IGNORE},
451 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
452 {"CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97},
453 {"EMPTY", EMPTY, EMPTY_RET},
454 {"SHORT1", SHORT, SHORT_RET},
455 {"SHORT2", SHORT2, SHORT2_RET},
456 {"SHORT3", SHORT3, SHORT2_RET},
457 {"SHORT4", SHORT4, SHORT2_RET},
458 {"RQNT1", RQNT, RQNT1_RET},
459 {"RQNT2", RQNT2, RQNT2_RET},
460 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
461 {"CRCX", CRCX, CRCX_FMTP_RET, 97,.extra_fmtp = "a=fmtp:126 0/1/2"},
462 {"MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE,.extra_fmtp =
463 "a=fmtp:126 0/1/2"},
464 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE,.extra_fmtp = "a=fmtp:126 0/1/2"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200465};
466
467static const struct mgcp_test retransmit[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200468 {"CRCX", CRCX, CRCX_RET},
469 {"RQNT1", RQNT, RQNT1_RET},
470 {"RQNT2", RQNT2, RQNT2_RET},
471 {"MDCX3", MDCX3, MDCX3A_RET},
472 {"DLCX", DLCX, DLCX_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200473};
474
Philipp Maierffd75e42017-11-22 11:44:50 +0100475static struct msgb *create_msg(const char *str, const char *conn_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200476{
477 struct msgb *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100478 int len;
479
480 printf("creating message from statically defined input:\n");
481 printf("---------8<---------\n%s\n---------8<---------\n", str);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200482
483 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Philipp Maierffd75e42017-11-22 11:44:50 +0100484 if (conn_id && strlen(conn_id))
485 len = sprintf((char *)msg->data, str, conn_id, conn_id);
486 else
487 len = sprintf((char *)msg->data, "%s", str);
488
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200489 msg->l2h = msgb_put(msg, len);
490 return msg;
491}
492
493static int last_endpoint = -1;
494
495static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
496 int state, const char *transactio_id)
497{
498 fprintf(stderr, "Policy CB got state %d on endpoint %d\n",
499 state, endpoint);
500 last_endpoint = endpoint;
501 return MGCP_POLICY_CONT;
502}
503
504#define MGCP_DUMMY_LOAD 0x23
505static int dummy_packets = 0;
506/* override and forward */
507ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200508 const struct sockaddr *dest_addr, socklen_t addrlen)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200509{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200510 uint32_t dest_host =
511 htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
512 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200513
Philipp Maier87bd9be2017-08-22 16:35:41 +0200514 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
515 fprintf(stderr,
516 "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
517 dest_host, dest_port, len, osmo_hexdump(buf, len));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200518 dummy_packets += 1;
519 }
520
Philipp Maier3d9b6562017-10-13 18:33:44 +0200521 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200522}
523
524static int64_t force_monotonic_time_us = -1;
525/* override and forward */
526int clock_gettime(clockid_t clk_id, struct timespec *tp)
527{
528 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
529 static clock_gettime_t real_clock_gettime = NULL;
530
531 if (!real_clock_gettime)
532 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
533
534 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
535 tp->tv_sec = force_monotonic_time_us / 1000000;
536 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
537 return 0;
538 }
539
540 return real_clock_gettime(clk_id, tp);
541}
542
543#define CONN_UNMODIFIED (0x1000)
544
545static void test_values(void)
546{
547 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200548 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549
550 /* Check that LOOPBACK enables all output */
551 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200552 MGCP_CONN_RECV_SEND);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553}
554
Philipp Maierffd75e42017-11-22 11:44:50 +0100555/* Extract a connection ID from a response (CRCX) */
556static int get_conn_id_from_response(uint8_t *resp, char *conn_id,
557 unsigned int conn_id_len)
558{
559 char *conn_id_ptr;
560 int i;
Philipp Maier55295f72018-01-15 14:00:28 +0100561 bool got_conn_id = false;
Philipp Maierffd75e42017-11-22 11:44:50 +0100562
Philipp Maier55295f72018-01-15 14:00:28 +0100563 /* First try to get the conn_id from the I: parameter */
Philipp Maierffd75e42017-11-22 11:44:50 +0100564 conn_id_ptr = strstr((char *)resp, "I: ");
Philipp Maier55295f72018-01-15 14:00:28 +0100565 if (conn_id_ptr) {
566 memset(conn_id, 0, conn_id_len);
567 memcpy(conn_id, conn_id_ptr + 3, 32);
568 got_conn_id = true;
569 } else {
570 /* Alternatively try to extract the conn_id from the o=- SDP
571 * parameter */
572 conn_id_ptr = strstr((char *)resp, "o=- ");
573 if(conn_id_ptr) {
574 memset(conn_id, 0, conn_id_len);
575 memcpy(conn_id, conn_id_ptr + 4, 32);
576 got_conn_id = true;
577 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100578 }
579
Philipp Maier55295f72018-01-15 14:00:28 +0100580 if (got_conn_id) {
581 for (i = 0; i < conn_id_len; i++) {
582 if (conn_id[i] == '\n' || conn_id[i] == '\r')
583 conn_id[i] = '\0';
584 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100585
Philipp Maier55295f72018-01-15 14:00:28 +0100586 /* A valid conn_id must at least contain one digit, and must
587 * not exceed a length of 32 digits */
588 OSMO_ASSERT(strlen(conn_id) <= 32);
589 OSMO_ASSERT(strlen(conn_id) > 0);
590
591 return 0;
592 }
593 return -EINVAL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100594}
595
596/* Check response, automatically patch connection ID if needed */
597static int check_response(uint8_t *resp, const char *exp_resp)
598{
599 char exp_resp_patched[4096];
600 const char *exp_resp_ptr;
601 char conn_id[256];
602
603 printf("checking response:\n");
604
605 /* If the expected response is intened to be patched
606 * (%s placeholder inside) we will patch it with the
607 * connection identifier we just received from the
608 * real response. This is necessary because the CI
609 * is generated by the mgcp code on CRCX and we can
610 * not know it in advance */
611 if (strstr(exp_resp, "%s")) {
612 if (get_conn_id_from_response(resp, conn_id, sizeof(conn_id)) ==
613 0) {
614 sprintf(exp_resp_patched, exp_resp, conn_id, conn_id);
615 exp_resp_ptr = exp_resp_patched;
616 printf
617 ("using message with patched conn_id for comparison\n");
618 } else {
619 printf
620 ("patching conn_id failed, using message as statically defined for comparison\n");
621 exp_resp_ptr = exp_resp;
622 }
623 } else {
624 printf("using message as statically defined for comparison\n");
625 exp_resp_ptr = exp_resp;
626 }
627
628 if (strcmp((char *)resp, exp_resp_ptr) != 0) {
629 printf("Unexpected response, please check!\n");
630 printf
631 ("Got:\n---------8<---------\n%s\n---------8<---------\n\n",
632 resp);
633 printf
634 ("Expected:\n---------8<---------\n%s\n---------8<---------\n",
635 exp_resp_ptr);
636 return -EINVAL;
637 }
638
639 printf("Response matches our expectations.\n");
640 return 0;
641}
642
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200643static void test_messages(void)
644{
645 struct mgcp_config *cfg;
646 struct mgcp_endpoint *endp;
647 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200648 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100649 char last_conn_id[256];
Philipp Maier7df419b2017-12-04 17:11:42 +0100650 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200651
652 cfg = mgcp_config_alloc();
653
Philipp Maierfcd06552017-11-10 17:32:22 +0100654 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200655 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200656 cfg->policy_cb = mgcp_test_policy_cb;
657
Philipp Maierffd75e42017-11-22 11:44:50 +0100658 memset(last_conn_id, 0, sizeof(last_conn_id));
659
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200660 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
661
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200662 for (i = 0; i < ARRAY_SIZE(tests); i++) {
663 const struct mgcp_test *t = &tests[i];
664 struct msgb *inp;
665 struct msgb *msg;
666
Philipp Maierffd75e42017-11-22 11:44:50 +0100667 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200668 printf("Testing %s\n", t->name);
669
670 last_endpoint = -1;
671 dummy_packets = 0;
672
Philipp Maier87bd9be2017-08-22 16:35:41 +0200673 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
674 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200675
Philipp Maierffd75e42017-11-22 11:44:50 +0100676 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200677 msg = mgcp_handle_message(cfg, inp);
678 msgb_free(inp);
679 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200680 if (msg) {
681 printf("%s failed '%s'\n", t->name,
682 (char *)msg->data);
683 OSMO_ASSERT(false);
684 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100685 } else if (check_response(msg->data, t->exp_resp) != 0) {
686 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200687 OSMO_ASSERT(false);
688 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100689
Philipp Maier7df419b2017-12-04 17:11:42 +0100690 if (msg) {
691 rc = get_conn_id_from_response(msg->data, last_conn_id,
692 sizeof(last_conn_id));
693 if (rc)
694 printf("(response contains a connection id)\n");
695 else
696 printf("(response does not contain a connection id)\n");
697 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100698
Philipp Maiera330b862017-12-04 17:16:16 +0100699 if (msg)
700 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200701
702 if (dummy_packets)
703 printf("Dummy packets: %d\n", dummy_packets);
704
705 if (last_endpoint != -1) {
706 endp = &cfg->trunk.endpoints[last_endpoint];
707
Philipp Maier01d24a32017-11-21 17:26:09 +0100708 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200709 if (conn) {
710 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200711
Philipp Maier87bd9be2017-08-22 16:35:41 +0200712 if (conn->end.packet_duration_ms != -1)
713 printf("Detected packet duration: %d\n",
714 conn->end.packet_duration_ms);
715 else
716 printf("Packet duration not set\n");
717 if (endp->local_options.pkt_period_min ||
718 endp->local_options.pkt_period_max)
719 printf
720 ("Requested packetetization period: "
721 "%d-%d\n",
722 endp->local_options.pkt_period_min,
723 endp->
724 local_options.pkt_period_max);
725 else
726 printf
727 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200728
Philipp Maier87bd9be2017-08-22 16:35:41 +0200729 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
730 printf("Connection mode: %d:%s%s%s%s\n",
731 conn->conn->mode,
732 !conn->conn->mode ? " NONE" : "",
733 conn->conn->mode & MGCP_CONN_SEND_ONLY
734 ? " SEND" : "",
735 conn->conn->mode & MGCP_CONN_RECV_ONLY
736 ? " RECV" : "",
737 conn->conn->mode & MGCP_CONN_LOOPBACK
738 & ~MGCP_CONN_RECV_SEND
739 ? " LOOP" : "");
740 fprintf(stderr,
741 "RTP output %sabled, NET output %sabled\n",
742 conn->end.output_enabled
743 ? "en" : "dis",
744 conn->end.output_enabled
745 ? "en" : "dis");
746 } else
747 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200748
Philipp Maier87bd9be2017-08-22 16:35:41 +0200749 OSMO_ASSERT(conn->end.output_enabled
750 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
751
752 conn->conn->mode |= CONN_UNMODIFIED;
753
754 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200755 endp->local_options.pkt_period_min = 0;
756 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 }
758
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200759 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100760 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200761 OSMO_ASSERT(last_endpoint != -1);
762 endp = &cfg->trunk.endpoints[last_endpoint];
763
764 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200765 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200766 last_endpoint,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200767 conn->end.codec.payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200768
Philipp Maier87bd9be2017-08-22 16:35:41 +0200769 if (t->ptype != PTYPE_IGNORE)
770 OSMO_ASSERT(conn->end.codec.payload_type ==
771 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200772
773 /* Reset them again for next test */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200774 conn->end.codec.payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200775 }
776 }
777
778 talloc_free(cfg);
779}
780
781static void test_retransmission(void)
782{
783 struct mgcp_config *cfg;
784 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100785 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100786 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200787
788 cfg = mgcp_config_alloc();
789
Philipp Maierfcd06552017-11-10 17:32:22 +0100790 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200791 mgcp_endpoints_allocate(&cfg->trunk);
792
Philipp Maierffd75e42017-11-22 11:44:50 +0100793 memset(last_conn_id, 0, sizeof(last_conn_id));
794
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200795 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
796
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200797 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
798 const struct mgcp_test *t = &retransmit[i];
799 struct msgb *inp;
800 struct msgb *msg;
801
Philipp Maierffd75e42017-11-22 11:44:50 +0100802 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200803 printf("Testing %s\n", t->name);
804
Philipp Maierffd75e42017-11-22 11:44:50 +0100805 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200806 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200807
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200808 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100809 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200810 printf("%s failed '%s'\n", t->name, (char *)msg->data);
811 OSMO_ASSERT(false);
812 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100813
Philipp Maier23b8e292017-12-04 16:48:45 +0100814 if (msg && strcmp(t->name, "CRCX") == 0) {
815 rc = get_conn_id_from_response(msg->data, last_conn_id,
816 sizeof(last_conn_id));
817 OSMO_ASSERT(rc == 0);
818 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100819
Philipp Maier7cedfd72017-12-04 16:49:12 +0100820 if (msg)
821 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200822
823 /* Retransmit... */
824 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100825 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200826 msg = mgcp_handle_message(cfg, inp);
827 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100828 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200829 printf("%s failed '%s'\n", t->name, (char *)msg->data);
830 OSMO_ASSERT(false);
831 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832 msgb_free(msg);
833 }
834
835 talloc_free(cfg);
836}
837
838static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
839{
840 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200841 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200842 return 0;
843}
844
845static void test_rqnt_cb(void)
846{
847 struct mgcp_config *cfg;
848 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100849 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200850
851 cfg = mgcp_config_alloc();
852 cfg->rqnt_cb = rqnt_cb;
853
Philipp Maierfcd06552017-11-10 17:32:22 +0100854 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200855 mgcp_endpoints_allocate(&cfg->trunk);
856
857 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
858
Philipp Maierffd75e42017-11-22 11:44:50 +0100859 inp = create_msg(CRCX, NULL);
860 msg = mgcp_handle_message(cfg, inp);
861 OSMO_ASSERT(msg);
862 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
863 sizeof(conn_id)) == 0);
864 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200865 msgb_free(inp);
866
867 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100868 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200869 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200870 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200871 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
872 abort();
873 }
874
Philipp Maier87bd9be2017-08-22 16:35:41 +0200875 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200876 printf("FAILED: callback not called: %p\n", cfg->data);
877 abort();
878 }
879
880 msgb_free(msg);
881 msgb_free(inp);
882
Philipp Maierffd75e42017-11-22 11:44:50 +0100883 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884 msgb_free(mgcp_handle_message(cfg, inp));
885 msgb_free(inp);
886 talloc_free(cfg);
887}
888
889struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200890 int cycles;
891 uint16_t base_seq;
892 uint16_t max_seq;
893 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894
Philipp Maier87bd9be2017-08-22 16:35:41 +0200895 uint32_t expected;
896 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200897};
898
899static const struct pl_test pl_test_dat[] = {
900 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200901 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
902 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200903 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200904 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
905 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200906 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200907 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
908 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200909 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200910 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
911 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200912 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200913 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
914 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200915};
916
917static void test_packet_loss_calc(void)
918{
919 int i;
920 printf("Testing packet loss calculation.\n");
921
922 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
923 uint32_t expected;
924 int loss;
925 struct mgcp_rtp_state state;
926 struct mgcp_rtp_end rtp;
927 memset(&state, 0, sizeof(state));
928 memset(&rtp, 0, sizeof(rtp));
929
Harald Welte49e3d5a2017-12-25 09:47:57 +0100930 state.stats.initialized = 1;
931 state.stats.base_seq = pl_test_dat[i].base_seq;
932 state.stats.max_seq = pl_test_dat[i].max_seq;
933 state.stats.cycles = pl_test_dat[i].cycles;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200934
Harald Weltea0ac30f2017-12-25 09:52:30 +0100935 rtp.stats.packets_rx = pl_test_dat[i].packets;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936 calc_loss(&state, &rtp, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200937
Philipp Maier87bd9be2017-08-22 16:35:41 +0200938 if (loss != pl_test_dat[i].loss
939 || expected != pl_test_dat[i].expected) {
940 printf
941 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
942 i, loss, pl_test_dat[i].loss, expected,
943 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200944 }
945 }
946}
947
Philipp Maier87bd9be2017-08-22 16:35:41 +0200948int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
949 uint32_t *pr, uint32_t *_or, int *loss,
950 uint32_t *jitter)
951{
952 char *line, *save;
953 int rc;
954
955 /* initialize with bad values */
956 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
957 *loss = INT_MAX;
958
959 line = strtok_r((char *)msg->l2h, "\r\n", &save);
960 if (!line)
961 return -1;
962
963 /* this can only parse the message that is created above... */
964 for_each_non_empty_line(line, save) {
965 switch (line[0]) {
966 case 'P':
967 rc = sscanf(line,
968 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
969 ps, os, pr, _or, loss, jitter);
970 return rc == 6 ? 0 : -1;
971 }
972 }
973
974 return -1;
975}
976
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200977static void test_mgcp_stats(void)
978{
979 printf("Testing stat parsing\n");
980
981 uint32_t bps, bos, pr, _or, jitter;
982 struct msgb *msg;
983 int loss;
984 int rc;
985
Philipp Maierffd75e42017-11-22 11:44:50 +0100986 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200987 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
988 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200989 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
990 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200991 printf("FAIL: Parsing failed1.\n");
992 msgb_free(msg);
993
Philipp Maier87bd9be2017-08-22 16:35:41 +0200994 msg =
995 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +0100996 ("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 +0200997 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
998 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200999 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
1000 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001001 printf("FAIL: Parsing failed2.\n");
1002 msgb_free(msg);
1003}
1004
1005struct rtp_packet_info {
1006 float txtime;
1007 int len;
1008 char *data;
1009};
1010
1011struct rtp_packet_info test_rtp_packets1[] = {
1012 /* RTP: SeqNo=0, TS=0 */
1013 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001014 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001015 /* RTP: SeqNo=1, TS=160 */
1016 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001017 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001018 /* RTP: SeqNo=2, TS=320 */
1019 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001020 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021 /* Repeat RTP timestamp: */
1022 /* RTP: SeqNo=3, TS=320 */
1023 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001024 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001025 /* RTP: SeqNo=4, TS=480 */
1026 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001027 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001028 /* RTP: SeqNo=5, TS=640 */
1029 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001030 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001031 /* Double skip RTP timestamp (delta = 2*160): */
1032 /* RTP: SeqNo=6, TS=960 */
1033 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001034 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001035 /* RTP: SeqNo=7, TS=1120 */
1036 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001038 /* RTP: SeqNo=8, TS=1280 */
1039 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001040 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001041 /* Non 20ms RTP timestamp (delta = 120): */
1042 /* RTP: SeqNo=9, TS=1400 */
1043 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001044 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001045 /* RTP: SeqNo=10, TS=1560 */
1046 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001047 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001048 /* RTP: SeqNo=11, TS=1720 */
1049 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001050 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001051 /* SSRC changed to 0x10203040, RTP timestamp jump */
1052 /* RTP: SeqNo=12, TS=34688 */
1053 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001054 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001055 /* RTP: SeqNo=13, TS=34848 */
1056 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001057 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001058 /* RTP: SeqNo=14, TS=35008 */
1059 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001060 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001061 /* Non 20ms RTP timestamp (delta = 120): */
1062 /* RTP: SeqNo=15, TS=35128 */
1063 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001064 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001065 /* RTP: SeqNo=16, TS=35288 */
1066 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001067 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001068 /* RTP: SeqNo=17, TS=35448 */
1069 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001070 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001071 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1072 /* RTP: SeqNo=19, TS=35768 */
1073 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001074 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001075 /* RTP: SeqNo=20, TS=35928 */
1076 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001077 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001078 /* RTP: SeqNo=21, TS=36088 */
1079 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001080 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001081 /* Repeat last packet */
1082 /* RTP: SeqNo=21, TS=36088 */
1083 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001084 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001085 /* RTP: SeqNo=22, TS=36248 */
1086 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001087 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001088 /* RTP: SeqNo=23, TS=36408 */
1089 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001090 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001091 /* Don't increment SeqNo but increment timestamp by 160 */
1092 /* RTP: SeqNo=23, TS=36568 */
1093 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001094 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001095 /* RTP: SeqNo=24, TS=36728 */
1096 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001097 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001098 /* RTP: SeqNo=25, TS=36888 */
1099 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001101 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1102 * SeqNo jump */
1103 /* RTP: SeqNo=1000, TS=160000 */
1104 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001105 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106 /* RTP: SeqNo=1001, TS=160160 */
1107 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001108 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001109 /* RTP: SeqNo=1002, TS=160320 */
1110 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001111 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001112};
1113
Philipp Maier87bd9be2017-08-22 16:35:41 +02001114void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1115 struct mgcp_rtp_state *state,
1116 struct mgcp_rtp_end *rtp_end,
1117 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001118
1119static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1120{
1121 int i;
1122
1123 struct mgcp_trunk_config trunk;
1124 struct mgcp_endpoint endp;
1125 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001126 struct mgcp_rtp_end *rtp;
1127 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001128 char buffer[4096];
1129 uint32_t last_ssrc = 0;
1130 uint32_t last_timestamp = 0;
1131 uint32_t last_seqno = 0;
1132 int last_in_ts_err_cnt = 0;
1133 int last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001135 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001136
1137 printf("Testing packet error detection%s%s.\n",
1138 patch_ssrc ? ", patch SSRC" : "",
1139 patch_ts ? ", patch timestamps" : "");
1140
1141 memset(&trunk, 0, sizeof(trunk));
1142 memset(&endp, 0, sizeof(endp));
1143 memset(&state, 0, sizeof(state));
1144
Philipp Maier87bd9be2017-08-22 16:35:41 +02001145 endp.type = &ep_typeset.rtp;
1146
Philipp Maierfcd06552017-11-10 17:32:22 +01001147 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001148 trunk.endpoints = &endp;
1149 trunk.force_constant_ssrc = patch_ssrc;
1150 trunk.force_aligned_timing = patch_ts;
1151
1152 endp.tcfg = &trunk;
1153
Philipp Maier87bd9be2017-08-22 16:35:41 +02001154 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001155 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1156 "test-connection");
1157 OSMO_ASSERT(_conn);
1158 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001159 OSMO_ASSERT(conn);
1160
1161 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001162
1163 rtp->codec.payload_type = 98;
1164
1165 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1166 struct rtp_packet_info *info = test_rtp_packets1 + i;
1167
1168 force_monotonic_time_us = round(1000000.0 * info->txtime);
1169
1170 OSMO_ASSERT(info->len <= sizeof(buffer));
1171 OSMO_ASSERT(info->len >= 0);
1172 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001173 mgcp_rtp_end_config(&endp, 1, rtp);
1174
1175 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1176 buffer, info->len);
1177
1178 if (state.out_stream.ssrc != last_ssrc) {
1179 printf("Output SSRC changed to %08x\n",
1180 state.out_stream.ssrc);
1181 last_ssrc = state.out_stream.ssrc;
1182 }
1183
1184 printf("In TS: %d, dTS: %d, Seq: %d\n",
1185 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001186 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001187
1188 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
1189 "TS Err change: in %+d, out %+d\n",
1190 state.out_stream.last_timestamp - last_timestamp,
1191 state.out_stream.last_tsdelta,
1192 state.out_stream.last_seq - last_seqno,
1193 state.in_stream.err_ts_counter - last_in_ts_err_cnt,
1194 state.out_stream.err_ts_counter - last_out_ts_err_cnt);
1195
1196 printf("Stats: Jitter = %u, Transit = %d\n",
Harald Welte49e3d5a2017-12-25 09:47:57 +01001197 calc_jitter(&state), state.stats.transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001198
1199 last_in_ts_err_cnt = state.in_stream.err_ts_counter;
1200 last_out_ts_err_cnt = state.out_stream.err_ts_counter;
1201 last_timestamp = state.out_stream.last_timestamp;
1202 last_seqno = state.out_stream.last_seq;
1203 }
1204
1205 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001206 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001207}
1208
1209static void test_multilple_codec(void)
1210{
1211 struct mgcp_config *cfg;
1212 struct mgcp_endpoint *endp;
1213 struct msgb *inp, *resp;
1214 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001215 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001216 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001217
1218 printf("Testing multiple payload types\n");
1219
1220 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001221 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001222 mgcp_endpoints_allocate(&cfg->trunk);
1223 cfg->policy_cb = mgcp_test_policy_cb;
1224 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1225
1226 /* Allocate endpoint 1@mgw with two codecs */
1227 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001228 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001229 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001230 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1231 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001232 msgb_free(inp);
1233 msgb_free(resp);
1234
1235 OSMO_ASSERT(last_endpoint == 1);
1236 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001237 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001238 OSMO_ASSERT(conn);
1239 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1240 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001241
1242 /* Allocate 2@mgw with three codecs, last one ignored */
1243 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001244 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001245 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001246 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1247 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001248 msgb_free(inp);
1249 msgb_free(resp);
1250
1251 OSMO_ASSERT(last_endpoint == 2);
1252 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001253 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001254 OSMO_ASSERT(conn);
1255 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1256 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001257
1258 /* Allocate 3@mgw with no codecs, check for PT == -1 */
1259 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001260 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001261 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001262 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1263 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001264 msgb_free(inp);
1265 msgb_free(resp);
1266
1267 OSMO_ASSERT(last_endpoint == 3);
1268 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001269 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001270 OSMO_ASSERT(conn);
1271 OSMO_ASSERT(conn->end.codec.payload_type == -1);
1272 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001273
1274 /* Allocate 4@mgw with a single codec */
1275 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001276 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001277 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001278 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1279 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001280 msgb_free(inp);
1281 msgb_free(resp);
1282
1283 OSMO_ASSERT(last_endpoint == 4);
1284 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001285 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001286 OSMO_ASSERT(conn);
1287 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1288 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001289
1290 /* Allocate 5@mgw at select GSM.. */
1291 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001292 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001293 talloc_free(cfg->trunk.audio_name);
1294 cfg->trunk.audio_name = "GSM/8000";
1295 cfg->trunk.no_audio_transcoding = 1;
1296 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001297 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1298 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001299 msgb_free(inp);
1300 msgb_free(resp);
1301
1302 OSMO_ASSERT(last_endpoint == 5);
1303 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001304 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001305 OSMO_ASSERT(conn);
1306 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1307 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001308
Philipp Maierffd75e42017-11-22 11:44:50 +01001309 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001310 last_endpoint = -1;
1311 resp = mgcp_handle_message(cfg, inp);
1312 msgb_free(inp);
1313 msgb_free(resp);
1314 OSMO_ASSERT(last_endpoint == 5);
1315 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001316 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001317 OSMO_ASSERT(conn);
1318 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1319 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
1320 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001321 memset(&addr, 0, sizeof(addr));
1322 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001323 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001324
1325 /* Check what happens without that flag */
1326
Philipp Maier87bd9be2017-08-22 16:35:41 +02001327 /* Free the previous endpoint and the data and
1328 * check if the connection really vanished... */
Philipp Maier1355d7e2018-02-01 14:30:06 +01001329 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001330 talloc_free(endp->last_response);
1331 talloc_free(endp->last_trans);
1332 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001333 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001334 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001335
1336 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001337 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001338 cfg->trunk.no_audio_transcoding = 0;
1339 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001340 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1341 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001342 msgb_free(inp);
1343 msgb_free(resp);
1344
1345 OSMO_ASSERT(last_endpoint == 5);
1346 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001347 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001348 OSMO_ASSERT(conn);
1349 OSMO_ASSERT(conn->end.codec.payload_type == 255);
1350 OSMO_ASSERT(conn->end.alt_codec.payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001351
1352 talloc_free(cfg);
1353}
1354
1355static void test_no_cycle(void)
1356{
1357 struct mgcp_config *cfg;
1358 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001359 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001360 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001361
1362 printf("Testing no sequence flow on initial packet\n");
1363
1364 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001365 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001366 mgcp_endpoints_allocate(&cfg->trunk);
1367
1368 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001369
Philipp Maierffd75e42017-11-22 11:44:50 +01001370 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1371 "test-connection");
1372 OSMO_ASSERT(_conn);
1373 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001374 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001375
Harald Welte49e3d5a2017-12-25 09:47:57 +01001376 OSMO_ASSERT(conn->state.stats.initialized == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001377
1378 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001379 OSMO_ASSERT(conn->state.stats.initialized == 1);
1380 OSMO_ASSERT(conn->state.stats.cycles == 0);
1381 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001382
1383 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001384 OSMO_ASSERT(conn->state.stats.initialized == 1);
1385 OSMO_ASSERT(conn->state.stats.cycles == 0);
1386 OSMO_ASSERT(conn->state.stats.max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001387
1388 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001389 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001390 OSMO_ASSERT(conn->state.stats.initialized == 1);
1391 OSMO_ASSERT(conn->state.stats.cycles == 0);
1392 OSMO_ASSERT(conn->state.stats.max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001393
1394 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001395 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001396 OSMO_ASSERT(conn->state.stats.initialized == 1);
1397 OSMO_ASSERT(conn->state.stats.cycles == UINT16_MAX + 1);
1398 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001399
Philipp Maier1355d7e2018-02-01 14:30:06 +01001400 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001401 talloc_free(cfg);
1402}
1403
1404static void test_no_name(void)
1405{
1406 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001407 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001408
1409 printf("Testing no rtpmap name\n");
1410 cfg = mgcp_config_alloc();
1411
Philipp Maierfcd06552017-11-10 17:32:22 +01001412 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001413 cfg->trunk.audio_send_name = 0;
1414 mgcp_endpoints_allocate(&cfg->trunk);
1415
1416 cfg->policy_cb = mgcp_test_policy_cb;
1417
1418 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1419
Philipp Maierffd75e42017-11-22 11:44:50 +01001420 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001421 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001422
1423 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001424 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001425 (char *)msg->data);
1426 OSMO_ASSERT(false);
1427 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001428 msgb_free(inp);
1429 msgb_free(msg);
1430
Philipp Maier1355d7e2018-02-01 14:30:06 +01001431 mgcp_endp_release(&cfg->trunk.endpoints[1]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001432 talloc_free(cfg);
1433}
1434
1435static void test_osmux_cid(void)
1436{
1437 int id, i;
1438
1439 OSMO_ASSERT(osmux_used_cid() == 0);
1440 id = osmux_get_cid();
1441 OSMO_ASSERT(id == 0);
1442 OSMO_ASSERT(osmux_used_cid() == 1);
1443 osmux_put_cid(id);
1444 OSMO_ASSERT(osmux_used_cid() == 0);
1445
1446 for (i = 0; i < 256; ++i) {
1447 id = osmux_get_cid();
1448 OSMO_ASSERT(id == i);
1449 OSMO_ASSERT(osmux_used_cid() == i + 1);
1450 }
1451
1452 id = osmux_get_cid();
1453 OSMO_ASSERT(id == -1);
1454
1455 for (i = 0; i < 256; ++i)
1456 osmux_put_cid(i);
1457 OSMO_ASSERT(osmux_used_cid() == 0);
1458}
1459
1460static const struct log_info_cat log_categories[] = {
1461};
1462
1463const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001464 .cat = log_categories,
1465 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001466};
1467
1468int main(int argc, char **argv)
1469{
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001470 void *msgb_ctx = msgb_talloc_ctx_init(NULL, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001471 osmo_init_logging(&log_info);
1472
1473 test_strline();
1474 test_values();
1475 test_messages();
1476 test_retransmission();
1477 test_packet_loss_calc();
1478 test_rqnt_cb();
1479 test_mgcp_stats();
1480 test_packet_error_detection(1, 0);
1481 test_packet_error_detection(0, 0);
1482 test_packet_error_detection(0, 1);
1483 test_packet_error_detection(1, 1);
1484 test_multilple_codec();
1485 test_no_cycle();
1486 test_no_name();
1487 test_osmux_cid();
1488
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001489 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1490 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1491 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001492 printf("Done\n");
1493 return EXIT_SUCCESS;
1494}