blob: 2f3a8a7a3b2bb108a8b7bdbaad4cea77828ee3fe [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>
Philipp Maierbc0346e2018-06-07 09:52:16 +020029#include <osmocom/mgcp/mgcp_sdp.h>
30#include <osmocom/mgcp/mgcp_codec.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020031
32#include <osmocom/core/application.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/utils.h>
35#include <string.h>
36#include <limits.h>
37#include <dlfcn.h>
38#include <time.h>
39#include <math.h>
Neels Hofmeyrb861db92018-08-28 16:19:25 +020040#include <ctype.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020041
42char *strline_r(char *str, char **saveptr);
43
44const char *strline_test_data =
45 "one CR\r"
46 "two CR\r"
47 "\r"
48 "one CRLF\r\n"
49 "two CRLF\r\n"
Philipp Maier87bd9be2017-08-22 16:35:41 +020050 "\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 +020051
52#define EXPECTED_NUMBER_OF_LINES 13
53
54static void test_strline(void)
55{
56 char *save = NULL;
57 char *line;
58 char buf[2048];
59 int counter = 0;
60
61 osmo_strlcpy(buf, strline_test_data, sizeof(buf));
62
Philipp Maier87bd9be2017-08-22 16:35:41 +020063 for (line = mgcp_strline(buf, &save); line;
64 line = mgcp_strline(NULL, &save)) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020065 printf("line: '%s'\n", line);
66 counter++;
67 }
68
69 OSMO_ASSERT(counter == EXPECTED_NUMBER_OF_LINES);
70}
71
Philipp Maier12943ea2018-01-17 15:40:25 +010072#define AUEP1 "AUEP 158663169 ds/e1-1/2@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020073#define AUEP1_RET "200 158663169 OK\r\n"
Philipp Maier12943ea2018-01-17 15:40:25 +010074#define AUEP2 "AUEP 18983213 ds/e1-2/1@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020075#define AUEP2_RET "500 18983213 FAIL\r\n"
76#define EMPTY "\r\n"
77#define EMPTY_RET NULL
78#define SHORT "CRCX \r\n"
79#define SHORT_RET "510 000000 FAIL\r\n"
80
Philipp Maier12943ea2018-01-17 15:40:25 +010081#define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@mgw MGCP 1.0\r\n"
Harald Welteabbb6b92017-12-28 13:13:50 +010082#define MDCX_ERR_RET "500 18983213 FAIL\r\n"
Philipp Maier12943ea2018-01-17 15:40:25 +010083#define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020084#define MDCX_RET "400 18983214 FAIL\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020085
Philipp Maier87bd9be2017-08-22 16:35:41 +020086#define MDCX3 \
87 "MDCX 18983215 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010088 "I: %s\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020089
Philipp Maier87bd9be2017-08-22 16:35:41 +020090#define MDCX3_RET \
91 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +010092 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020093 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010094 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020095 "s=-\r\n" \
96 "c=IN IP4 0.0.0.0\r\n" \
97 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010098 "m=audio 16002 RTP/AVP 97\r\n" \
99 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200100 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200101
Philipp Maier87bd9be2017-08-22 16:35:41 +0200102#define MDCX3A_RET \
103 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100104 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200105 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100106 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200107 "s=-\r\n" \
108 "c=IN IP4 0.0.0.0\r\n" \
109 "t=0 0\r\n" \
110 "m=audio 16002 RTP/AVP 97\r\n" \
111 "a=rtpmap:97 GSM-EFR/8000\r\n" \
112 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200113
Philipp Maier87bd9be2017-08-22 16:35:41 +0200114#define MDCX3_FMTP_RET \
115 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100116 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200117 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100118 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200119 "s=-\r\n" \
120 "c=IN IP4 0.0.0.0\r\n" \
121 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100122 "m=audio 16006 RTP/AVP 97\r\n" \
123 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200124 "a=fmtp:126 0/1/2\r\n" \
125 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127#define MDCX4 \
128 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \
129 "M: sendrecv\r" \
130 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100131 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200132 "L: p:20, a:AMR, nt:IN\r\n" \
133 "\n" \
134 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100135 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200136 "c=IN IP4 0.0.0.0\r\n" \
137 "t=0 0\r\n" \
138 "m=audio 4441 RTP/AVP 99\r\n" \
139 "a=rtpmap:99 AMR/8000\r\n" \
140 "a=ptime:40\r\n"
141
142#define MDCX4_RET(Ident) \
143 "200 " Ident " OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100144 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100146 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200147 "s=-\r\n" \
148 "c=IN IP4 0.0.0.0\r\n" \
149 "t=0 0\r\n" \
150 "m=audio 16002 RTP/AVP 99\r\n" \
151 "a=rtpmap:99 AMR/8000\r\n" \
152 "a=ptime:40\r\n"
153
154#define MDCX4_RO_RET(Ident) \
155 "200 " Ident " OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100156 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200157 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100158 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200159 "s=-\r\n" \
160 "c=IN IP4 0.0.0.0\r\n" \
161 "t=0 0\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200162 "m=audio 16002 RTP/AVP 112\r\n" \
163 "a=rtpmap:112 AMR\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200164 "a=ptime:40\r\n"
165
166#define MDCX4_PT1 \
167 "MDCX 18983217 1@mgw MGCP 1.0\r\n" \
168 "M: sendrecv\r" \
169 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100170 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200171 "L: p:20-40, a:AMR, nt:IN\r\n" \
172 "\n" \
173 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100174 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200175 "c=IN IP4 0.0.0.0\r\n" \
176 "t=0 0\r\n" \
177 "m=audio 4441 RTP/AVP 99\r\n" \
178 "a=rtpmap:99 AMR/8000\r\n" \
179 "a=ptime:40\r\n"
180
181#define MDCX4_PT2 \
182 "MDCX 18983218 1@mgw MGCP 1.0\r\n" \
183 "M: sendrecv\r" \
184 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100185 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200186 "L: p:20-20, a:AMR, nt:IN\r\n" \
187 "\n" \
188 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100189 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190 "c=IN IP4 0.0.0.0\r\n" \
191 "t=0 0\r\n" \
192 "m=audio 4441 RTP/AVP 99\r\n" \
193 "a=rtpmap:99 AMR/8000\r\n" \
194 "a=ptime:40\r\n"
195
196#define MDCX4_PT3 \
197 "MDCX 18983219 1@mgw MGCP 1.0\r\n" \
198 "M: sendrecv\r" \
199 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100200 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201 "L: a:AMR, nt:IN\r\n" \
202 "\n" \
203 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100204 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200205 "c=IN IP4 0.0.0.0\r\n" \
206 "t=0 0\r\n" \
207 "m=audio 4441 RTP/AVP 99\r\n" \
208 "a=rtpmap:99 AMR/8000\r\n" \
209 "a=ptime:40\r\n"
210
211#define MDCX4_SO \
212 "MDCX 18983220 1@mgw MGCP 1.0\r\n" \
213 "M: sendonly\r" \
214 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100215 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216 "L: p:20, a:AMR, nt:IN\r\n" \
217 "\n" \
218 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100219 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200220 "c=IN IP4 0.0.0.0\r\n" \
221 "t=0 0\r\n" \
222 "m=audio 4441 RTP/AVP 99\r\n" \
223 "a=rtpmap:99 AMR/8000\r\n" \
224 "a=ptime:40\r\n"
225
226#define MDCX4_RO \
227 "MDCX 18983221 1@mgw MGCP 1.0\r\n" \
228 "M: recvonly\r" \
229 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100230 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200231 "L: p:20, a:AMR, nt:IN\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200232
233#define SHORT2 "CRCX 1"
234#define SHORT2_RET "510 000000 FAIL\r\n"
235#define SHORT3 "CRCX 1 1@mgw"
236#define SHORT4 "CRCX 1 1@mgw MGCP"
237#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
238
Philipp Maier87bd9be2017-08-22 16:35:41 +0200239#define CRCX \
240 "CRCX 2 1@mgw MGCP 1.0\r\n" \
241 "M: recvonly\r\n" \
242 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200243 "L: p:20\r\n" \
244 "\r\n" \
245 "v=0\r\n" \
246 "c=IN IP4 123.12.12.123\r\n" \
247 "m=audio 5904 RTP/AVP 97\r\n" \
248 "a=rtpmap:97 GSM-EFR/8000\r\n" \
249 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250
Philipp Maier87bd9be2017-08-22 16:35:41 +0200251#define CRCX_RET \
252 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100253 "I: %s\r\n" \
254 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200255 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100256 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200257 "s=-\r\n" \
258 "c=IN IP4 0.0.0.0\r\n" \
259 "t=0 0\r\n" \
260 "m=audio 16002 RTP/AVP 97\r\n" \
261 "a=rtpmap:97 GSM-EFR/8000\r\n" \
262 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264#define CRCX_RET_NO_RTPMAP \
265 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100266 "I: %s\r\n" \
267 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200268 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100269 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200270 "s=-\r\n" \
271 "c=IN IP4 0.0.0.0\r\n" \
272 "t=0 0\r\n" \
273 "m=audio 16002 RTP/AVP 97\r\n" \
274 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200275
Philipp Maier87bd9be2017-08-22 16:35:41 +0200276#define CRCX_FMTP_RET \
277 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100278 "I: %s\r\n" \
279 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200280 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100281 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200282 "s=-\r\n" \
283 "c=IN IP4 0.0.0.0\r\n" \
284 "t=0 0\r\n" \
285 "m=audio 16006 RTP/AVP 97\r\n" \
286 "a=rtpmap:97 GSM-EFR/8000\r\n" \
287 "a=fmtp:126 0/1/2\r\n" \
288 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200289
Philipp Maier87bd9be2017-08-22 16:35:41 +0200290#define CRCX_ZYN \
291 "CRCX 2 1@mgw MGCP 1.0\r" \
292 "M: recvonly\r" \
293 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200294 "\n" \
295 "v=0\r" \
296 "c=IN IP4 123.12.12.123\r" \
297 "m=audio 5904 RTP/AVP 97\r" \
298 "a=rtpmap:97 GSM-EFR/8000\r"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200299
Philipp Maier87bd9be2017-08-22 16:35:41 +0200300#define CRCX_ZYN_RET \
301 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100302 "I: %s\r\n" \
303 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200304 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100305 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200306 "s=-\r\n" \
307 "c=IN IP4 0.0.0.0\r\n" \
308 "t=0 0\r\n" \
309 "m=audio 16004 RTP/AVP 97\r\n" \
310 "a=rtpmap:97 GSM-EFR/8000\r\n" \
311 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200313#define CRCX_X_OSMO_IGN \
314 "CRCX 2 1@mgw MGCP 1.0\r\n" \
315 "M: recvonly\r\n" \
316 "C: 2\r\n" \
317 "L: p:20\r\n" \
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200318 "X-Osmo-IGN: C foo\r\n" \
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200319 "\r\n" \
320 "v=0\r\n" \
321 "c=IN IP4 123.12.12.123\r\n" \
322 "m=audio 5904 RTP/AVP 97\r\n" \
323 "a=rtpmap:97 GSM-EFR/8000\r\n" \
324 "a=ptime:40\r\n"
325
326#define CRCX_X_OSMO_IGN_RET \
327 "200 2 OK\r\n" \
328 "I: %s\r\n" \
329 "\r\n" \
330 "v=0\r\n" \
331 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
332 "s=-\r\n" \
333 "c=IN IP4 0.0.0.0\r\n" \
334 "t=0 0\r\n" \
335 "m=audio 16010 RTP/AVP 97\r\n" \
336 "a=rtpmap:97 GSM-EFR/8000\r\n" \
337 "a=ptime:40\r\n"
338
Philipp Maier87bd9be2017-08-22 16:35:41 +0200339#define DLCX \
340 "DLCX 7 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100341 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200342 "C: 2\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200343
Philipp Maier87bd9be2017-08-22 16:35:41 +0200344#define DLCX_RET \
345 "250 7 OK\r\n" \
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100346 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n"
347
348 #define DLCX_RET_OSMUX DLCX_RET \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200349 "X-Osmo-CP: EC TI=0, TO=0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200350
Philipp Maier87bd9be2017-08-22 16:35:41 +0200351#define RQNT \
352 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
353 "X: B244F267488\r\n" \
354 "S: D/9\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200355
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356#define RQNT2 \
357 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
358 "X: ADD4F26746F\r\n" \
359 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200360
361#define RQNT1_RET "200 186908780 OK\r\n"
362#define RQNT2_RET "200 186908781 OK\r\n"
363
Philipp Maier87bd9be2017-08-22 16:35:41 +0200364#define PTYPE_IGNORE 0 /* == default initializer */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200365#define PTYPE_NONE 128
366#define PTYPE_NYI PTYPE_NONE
367
Philipp Maier87bd9be2017-08-22 16:35:41 +0200368#define CRCX_MULT_1 \
369 "CRCX 2 1@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 18 97\r\n" \
378 "a=rtpmap:18 G729/8000\r\n" \
379 "a=rtpmap:97 GSM-EFR/8000\r\n" \
380 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200381
Philipp Maier87bd9be2017-08-22 16:35:41 +0200382#define CRCX_MULT_2 \
383 "CRCX 2 2@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200384 "M: recvonly\r\n" \
385 "C: 2\r\n" \
386 "X\r\n" \
387 "L: p:20\r\n" \
388 "\r\n" \
389 "v=0\r\n" \
390 "c=IN IP4 123.12.12.123\r\n" \
391 "m=audio 5904 RTP/AVP 18 97 101\r\n" \
392 "a=rtpmap:18 G729/8000\r\n" \
393 "a=rtpmap:97 GSM-EFR/8000\r\n" \
394 "a=rtpmap:101 FOO/8000\r\n" \
395 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200396
Philipp Maier87bd9be2017-08-22 16:35:41 +0200397#define CRCX_MULT_3 \
398 "CRCX 2 3@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399 "M: recvonly\r\n" \
400 "C: 2\r\n" \
401 "X\r\n" \
402 "L: p:20\r\n" \
403 "\r\n" \
404 "v=0\r\n" \
405 "c=IN IP4 123.12.12.123\r\n" \
406 "m=audio 5904 RTP/AVP\r\n" \
407 "a=rtpmap:18 G729/8000\r\n" \
408 "a=rtpmap:97 GSM-EFR/8000\r\n" \
409 "a=rtpmap:101 FOO/8000\r\n" \
410 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200411
Philipp Maier87bd9be2017-08-22 16:35:41 +0200412#define CRCX_MULT_4 \
413 "CRCX 2 4@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200414 "M: recvonly\r\n" \
415 "C: 2\r\n" \
416 "X\r\n" \
417 "L: p:20\r\n" \
418 "\r\n" \
419 "v=0\r\n" \
420 "c=IN IP4 123.12.12.123\r\n" \
421 "m=audio 5904 RTP/AVP 18\r\n" \
422 "a=rtpmap:18 G729/8000\r\n" \
423 "a=rtpmap:97 GSM-EFR/8000\r\n" \
424 "a=rtpmap:101 FOO/8000\r\n" \
425 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200426
427#define CRCX_MULT_GSM_EXACT \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200428 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
429 "C: 1355c6041e\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200430 "L: p:20, a:GSM, nt:IN\r\n" \
431 "M: recvonly\r\n" \
432 "\r\n" \
433 "v=0\r\n" \
434 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
435 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200436 "t=0 0\r\nm=audio 29084 RTP/AVP 0 8 3 18 4 96 97 101\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437 "a=rtpmap:0 PCMU/8000\r\n" \
438 "a=rtpmap:8 PCMA/8000\r\n" \
439 "a=rtpmap:3 gsm/8000\r\n" \
440 "a=rtpmap:18 G729/8000\r\n" \
441 "a=fmtp:18 annexb=no\r\n" \
442 "a=rtpmap:4 G723/8000\r\n" \
443 "a=rtpmap:96 iLBC/8000\r\n" \
444 "a=fmtp:96 mode=20\r\n" \
445 "a=rtpmap:97 iLBC/8000\r\n" \
446 "a=fmtp:97 mode=30\r\n" \
447 "a=rtpmap:101 telephone-event/8000\r\n" \
448 "a=fmtp:101 0-15\r\n" \
449 "a=recvonly\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200450
Philipp Maier87bd9be2017-08-22 16:35:41 +0200451#define MDCX_NAT_DUMMY \
452 "MDCX 23 5@mgw MGCP 1.0\r\n" \
453 "C: 1355c6041e\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100454 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200455 "\r\n" \
456 "c=IN IP4 8.8.8.8\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200457 "m=audio 16434 RTP/AVP 3\r\n"
458
459#define CRCX_NO_LCO_NO_SDP \
460 "CRCX 2 6@mgw MGCP 1.0\r\n" \
461 "M: recvonly\r\n" \
462 "C: 2\r\n"
463
464#define CRCX_NO_LCO_NO_SDP_RET \
465 "200 2 OK\r\n" \
466 "I: %s\r\n" \
467 "\r\n" \
468 "v=0\r\n" \
469 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
470 "s=-\r\n" \
471 "c=IN IP4 0.0.0.0\r\n" \
472 "t=0 0\r\n" \
473 "m=audio 16008 RTP/AVP 0\r\n" \
474 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475
476struct mgcp_test {
477 const char *name;
478 const char *req;
479 const char *exp_resp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200480 int ptype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200481 const char *extra_fmtp;
482};
483
484static const struct mgcp_test tests[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200485 {"AUEP1", AUEP1, AUEP1_RET},
486 {"AUEP2", AUEP2, AUEP2_RET},
487 {"MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET},
488 {"MDCX2", MDCX_UNALLOCATED, MDCX_RET},
489 {"CRCX", CRCX, CRCX_RET, 97},
490 {"MDCX3", MDCX3, MDCX3_RET, PTYPE_IGNORE},
491 {"MDCX4", MDCX4, MDCX4_RET("18983216"), 99},
492 {"MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99},
493 {"MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99},
494 {"MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99},
495 {"MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99},
496 {"MDCX4_RO", MDCX4_RO, MDCX4_RO_RET("18983221"), PTYPE_IGNORE},
497 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
498 {"CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97},
499 {"EMPTY", EMPTY, EMPTY_RET},
500 {"SHORT1", SHORT, SHORT_RET},
501 {"SHORT2", SHORT2, SHORT2_RET},
502 {"SHORT3", SHORT3, SHORT2_RET},
503 {"SHORT4", SHORT4, SHORT2_RET},
504 {"RQNT1", RQNT, RQNT1_RET},
505 {"RQNT2", RQNT2, RQNT2_RET},
506 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
507 {"CRCX", CRCX, CRCX_FMTP_RET, 97,.extra_fmtp = "a=fmtp:126 0/1/2"},
508 {"MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE,.extra_fmtp =
509 "a=fmtp:126 0/1/2"},
510 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE,.extra_fmtp = "a=fmtp:126 0/1/2"},
Philipp Maierbc0346e2018-06-07 09:52:16 +0200511 {"CRCX", CRCX_NO_LCO_NO_SDP, CRCX_NO_LCO_NO_SDP_RET, 97},
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200512 {"CRCX", CRCX_X_OSMO_IGN, CRCX_X_OSMO_IGN_RET, 97},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200513};
514
515static const struct mgcp_test retransmit[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200516 {"CRCX", CRCX, CRCX_RET},
517 {"RQNT1", RQNT, RQNT1_RET},
518 {"RQNT2", RQNT2, RQNT2_RET},
519 {"MDCX3", MDCX3, MDCX3A_RET},
520 {"DLCX", DLCX, DLCX_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200521};
522
Philipp Maierffd75e42017-11-22 11:44:50 +0100523static struct msgb *create_msg(const char *str, const char *conn_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200524{
525 struct msgb *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100526 int len;
527
528 printf("creating message from statically defined input:\n");
529 printf("---------8<---------\n%s\n---------8<---------\n", str);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200530
531 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Philipp Maierffd75e42017-11-22 11:44:50 +0100532 if (conn_id && strlen(conn_id))
533 len = sprintf((char *)msg->data, str, conn_id, conn_id);
534 else
535 len = sprintf((char *)msg->data, "%s", str);
536
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200537 msg->l2h = msgb_put(msg, len);
538 return msg;
539}
540
541static int last_endpoint = -1;
542
543static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
544 int state, const char *transactio_id)
545{
546 fprintf(stderr, "Policy CB got state %d on endpoint %d\n",
547 state, endpoint);
548 last_endpoint = endpoint;
549 return MGCP_POLICY_CONT;
550}
551
552#define MGCP_DUMMY_LOAD 0x23
553static int dummy_packets = 0;
554/* override and forward */
555ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200556 const struct sockaddr *dest_addr, socklen_t addrlen)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200557{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200558 uint32_t dest_host =
559 htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
560 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561
Philipp Maier87bd9be2017-08-22 16:35:41 +0200562 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
563 fprintf(stderr,
564 "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
565 dest_host, dest_port, len, osmo_hexdump(buf, len));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200566 dummy_packets += 1;
567 }
568
Philipp Maier3d9b6562017-10-13 18:33:44 +0200569 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200570}
571
572static int64_t force_monotonic_time_us = -1;
573/* override and forward */
574int clock_gettime(clockid_t clk_id, struct timespec *tp)
575{
576 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
577 static clock_gettime_t real_clock_gettime = NULL;
578
579 if (!real_clock_gettime)
580 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
581
582 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
583 tp->tv_sec = force_monotonic_time_us / 1000000;
584 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
585 return 0;
586 }
587
588 return real_clock_gettime(clk_id, tp);
589}
590
591#define CONN_UNMODIFIED (0x1000)
592
593static void test_values(void)
594{
595 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200596 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200597
598 /* Check that LOOPBACK enables all output */
599 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200600 MGCP_CONN_RECV_SEND);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200601}
602
Philipp Maierffd75e42017-11-22 11:44:50 +0100603/* Extract a connection ID from a response (CRCX) */
604static int get_conn_id_from_response(uint8_t *resp, char *conn_id,
605 unsigned int conn_id_len)
606{
607 char *conn_id_ptr;
608 int i;
Philipp Maier55295f72018-01-15 14:00:28 +0100609 bool got_conn_id = false;
Philipp Maierffd75e42017-11-22 11:44:50 +0100610
Philipp Maier55295f72018-01-15 14:00:28 +0100611 /* First try to get the conn_id from the I: parameter */
Philipp Maierffd75e42017-11-22 11:44:50 +0100612 conn_id_ptr = strstr((char *)resp, "I: ");
Philipp Maier55295f72018-01-15 14:00:28 +0100613 if (conn_id_ptr) {
614 memset(conn_id, 0, conn_id_len);
615 memcpy(conn_id, conn_id_ptr + 3, 32);
616 got_conn_id = true;
617 } else {
618 /* Alternatively try to extract the conn_id from the o=- SDP
619 * parameter */
620 conn_id_ptr = strstr((char *)resp, "o=- ");
621 if(conn_id_ptr) {
622 memset(conn_id, 0, conn_id_len);
623 memcpy(conn_id, conn_id_ptr + 4, 32);
624 got_conn_id = true;
625 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100626 }
627
Philipp Maier55295f72018-01-15 14:00:28 +0100628 if (got_conn_id) {
629 for (i = 0; i < conn_id_len; i++) {
Neels Hofmeyrb861db92018-08-28 16:19:25 +0200630 if (!isxdigit(conn_id[i])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100631 conn_id[i] = '\0';
Neels Hofmeyrb861db92018-08-28 16:19:25 +0200632 break;
633 }
Philipp Maier55295f72018-01-15 14:00:28 +0100634 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100635
Philipp Maier55295f72018-01-15 14:00:28 +0100636 /* A valid conn_id must at least contain one digit, and must
637 * not exceed a length of 32 digits */
638 OSMO_ASSERT(strlen(conn_id) <= 32);
639 OSMO_ASSERT(strlen(conn_id) > 0);
640
641 return 0;
642 }
643 return -EINVAL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100644}
645
646/* Check response, automatically patch connection ID if needed */
647static int check_response(uint8_t *resp, const char *exp_resp)
648{
649 char exp_resp_patched[4096];
650 const char *exp_resp_ptr;
651 char conn_id[256];
652
653 printf("checking response:\n");
654
655 /* If the expected response is intened to be patched
656 * (%s placeholder inside) we will patch it with the
657 * connection identifier we just received from the
658 * real response. This is necessary because the CI
659 * is generated by the mgcp code on CRCX and we can
660 * not know it in advance */
661 if (strstr(exp_resp, "%s")) {
662 if (get_conn_id_from_response(resp, conn_id, sizeof(conn_id)) ==
663 0) {
664 sprintf(exp_resp_patched, exp_resp, conn_id, conn_id);
665 exp_resp_ptr = exp_resp_patched;
666 printf
667 ("using message with patched conn_id for comparison\n");
668 } else {
669 printf
670 ("patching conn_id failed, using message as statically defined for comparison\n");
671 exp_resp_ptr = exp_resp;
672 }
673 } else {
674 printf("using message as statically defined for comparison\n");
675 exp_resp_ptr = exp_resp;
676 }
677
678 if (strcmp((char *)resp, exp_resp_ptr) != 0) {
679 printf("Unexpected response, please check!\n");
680 printf
681 ("Got:\n---------8<---------\n%s\n---------8<---------\n\n",
682 resp);
683 printf
684 ("Expected:\n---------8<---------\n%s\n---------8<---------\n",
685 exp_resp_ptr);
686 return -EINVAL;
687 }
688
689 printf("Response matches our expectations.\n");
690 return 0;
691}
692
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200693static void test_messages(void)
694{
695 struct mgcp_config *cfg;
696 struct mgcp_endpoint *endp;
697 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200698 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100699 char last_conn_id[256];
Philipp Maier7df419b2017-12-04 17:11:42 +0100700 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200701
702 cfg = mgcp_config_alloc();
703
Philipp Maierfcd06552017-11-10 17:32:22 +0100704 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200705 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200706 cfg->policy_cb = mgcp_test_policy_cb;
707
Philipp Maierffd75e42017-11-22 11:44:50 +0100708 memset(last_conn_id, 0, sizeof(last_conn_id));
709
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200710 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
711
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200712 for (i = 0; i < ARRAY_SIZE(tests); i++) {
713 const struct mgcp_test *t = &tests[i];
714 struct msgb *inp;
715 struct msgb *msg;
716
Philipp Maierffd75e42017-11-22 11:44:50 +0100717 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200718 printf("Testing %s\n", t->name);
719
720 last_endpoint = -1;
721 dummy_packets = 0;
722
Philipp Maier87bd9be2017-08-22 16:35:41 +0200723 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
724 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200725
Philipp Maierffd75e42017-11-22 11:44:50 +0100726 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200727 msg = mgcp_handle_message(cfg, inp);
728 msgb_free(inp);
729 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200730 if (msg) {
731 printf("%s failed '%s'\n", t->name,
732 (char *)msg->data);
733 OSMO_ASSERT(false);
734 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100735 } else if (check_response(msg->data, t->exp_resp) != 0) {
736 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200737 OSMO_ASSERT(false);
738 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100739
Philipp Maier7df419b2017-12-04 17:11:42 +0100740 if (msg) {
741 rc = get_conn_id_from_response(msg->data, last_conn_id,
742 sizeof(last_conn_id));
Neels Hofmeyr08e07042018-08-28 16:22:14 +0200743 if (rc == 0)
Philipp Maier7df419b2017-12-04 17:11:42 +0100744 printf("(response contains a connection id)\n");
745 else
746 printf("(response does not contain a connection id)\n");
747 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100748
Philipp Maiera330b862017-12-04 17:16:16 +0100749 if (msg)
750 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200751
752 if (dummy_packets)
753 printf("Dummy packets: %d\n", dummy_packets);
754
755 if (last_endpoint != -1) {
756 endp = &cfg->trunk.endpoints[last_endpoint];
757
Philipp Maier01d24a32017-11-21 17:26:09 +0100758 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200759 if (conn) {
760 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200761
Philipp Maier87bd9be2017-08-22 16:35:41 +0200762 if (conn->end.packet_duration_ms != -1)
763 printf("Detected packet duration: %d\n",
764 conn->end.packet_duration_ms);
765 else
766 printf("Packet duration not set\n");
767 if (endp->local_options.pkt_period_min ||
768 endp->local_options.pkt_period_max)
769 printf
770 ("Requested packetetization period: "
771 "%d-%d\n",
772 endp->local_options.pkt_period_min,
773 endp->
774 local_options.pkt_period_max);
775 else
776 printf
777 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200778
Philipp Maier87bd9be2017-08-22 16:35:41 +0200779 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
780 printf("Connection mode: %d:%s%s%s%s\n",
781 conn->conn->mode,
782 !conn->conn->mode ? " NONE" : "",
783 conn->conn->mode & MGCP_CONN_SEND_ONLY
784 ? " SEND" : "",
785 conn->conn->mode & MGCP_CONN_RECV_ONLY
786 ? " RECV" : "",
787 conn->conn->mode & MGCP_CONN_LOOPBACK
788 & ~MGCP_CONN_RECV_SEND
789 ? " LOOP" : "");
790 fprintf(stderr,
791 "RTP output %sabled, NET output %sabled\n",
792 conn->end.output_enabled
793 ? "en" : "dis",
794 conn->end.output_enabled
795 ? "en" : "dis");
796 } else
797 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200798
Philipp Maier87bd9be2017-08-22 16:35:41 +0200799 OSMO_ASSERT(conn->end.output_enabled
800 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
801
802 conn->conn->mode |= CONN_UNMODIFIED;
803
804 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200805 endp->local_options.pkt_period_min = 0;
806 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200807 }
808
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200809 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100810 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811 OSMO_ASSERT(last_endpoint != -1);
812 endp = &cfg->trunk.endpoints[last_endpoint];
813
814 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200815 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200816 last_endpoint,
Philipp Maierbc0346e2018-06-07 09:52:16 +0200817 conn->end.codec->payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200818
Philipp Maier87bd9be2017-08-22 16:35:41 +0200819 if (t->ptype != PTYPE_IGNORE)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200820 OSMO_ASSERT(conn->end.codec->payload_type ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200822
823 /* Reset them again for next test */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200824 conn->end.codec->payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825 }
826 }
827
828 talloc_free(cfg);
829}
830
831static void test_retransmission(void)
832{
833 struct mgcp_config *cfg;
834 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100835 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100836 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200837
838 cfg = mgcp_config_alloc();
839
Philipp Maierfcd06552017-11-10 17:32:22 +0100840 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200841 mgcp_endpoints_allocate(&cfg->trunk);
842
Philipp Maierffd75e42017-11-22 11:44:50 +0100843 memset(last_conn_id, 0, sizeof(last_conn_id));
844
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200845 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
846
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200847 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
848 const struct mgcp_test *t = &retransmit[i];
849 struct msgb *inp;
850 struct msgb *msg;
851
Philipp Maierffd75e42017-11-22 11:44:50 +0100852 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853 printf("Testing %s\n", t->name);
854
Philipp Maierffd75e42017-11-22 11:44:50 +0100855 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200856 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200857
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100859 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200860 printf("%s failed '%s'\n", t->name, (char *)msg->data);
861 OSMO_ASSERT(false);
862 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100863
Philipp Maier23b8e292017-12-04 16:48:45 +0100864 if (msg && strcmp(t->name, "CRCX") == 0) {
865 rc = get_conn_id_from_response(msg->data, last_conn_id,
866 sizeof(last_conn_id));
867 OSMO_ASSERT(rc == 0);
868 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100869
Philipp Maier7cedfd72017-12-04 16:49:12 +0100870 if (msg)
871 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872
873 /* Retransmit... */
874 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100875 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200876 msg = mgcp_handle_message(cfg, inp);
877 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100878 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200879 printf("%s failed '%s'\n", t->name, (char *)msg->data);
880 OSMO_ASSERT(false);
881 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200882 msgb_free(msg);
883 }
884
885 talloc_free(cfg);
886}
887
888static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
889{
890 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200891 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200892 return 0;
893}
894
895static void test_rqnt_cb(void)
896{
897 struct mgcp_config *cfg;
898 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100899 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200900
901 cfg = mgcp_config_alloc();
902 cfg->rqnt_cb = rqnt_cb;
903
Philipp Maierfcd06552017-11-10 17:32:22 +0100904 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200905 mgcp_endpoints_allocate(&cfg->trunk);
906
907 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
908
Philipp Maierffd75e42017-11-22 11:44:50 +0100909 inp = create_msg(CRCX, NULL);
910 msg = mgcp_handle_message(cfg, inp);
911 OSMO_ASSERT(msg);
912 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
913 sizeof(conn_id)) == 0);
914 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200915 msgb_free(inp);
916
917 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100918 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200919 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200920 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200921 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
922 abort();
923 }
924
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200926 printf("FAILED: callback not called: %p\n", cfg->data);
927 abort();
928 }
929
930 msgb_free(msg);
931 msgb_free(inp);
932
Philipp Maierffd75e42017-11-22 11:44:50 +0100933 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200934 msgb_free(mgcp_handle_message(cfg, inp));
935 msgb_free(inp);
936 talloc_free(cfg);
937}
938
939struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200940 int cycles;
941 uint16_t base_seq;
942 uint16_t max_seq;
943 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200944
Philipp Maier87bd9be2017-08-22 16:35:41 +0200945 uint32_t expected;
946 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200947};
948
949static const struct pl_test pl_test_dat[] = {
950 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200951 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
952 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200953 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200954 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
955 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200956 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200957 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
958 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200959 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200960 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
961 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200962 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200963 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
964 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200965};
966
967static void test_packet_loss_calc(void)
968{
969 int i;
Philipp Maiercede2a42018-07-03 14:14:21 +0200970 struct mgcp_endpoint endp;
971 struct mgcp_trunk_config trunk;
972
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200973 printf("Testing packet loss calculation.\n");
974
Philipp Maiercede2a42018-07-03 14:14:21 +0200975 memset(&endp, 0, sizeof(endp));
976 memset(&trunk, 0, sizeof(trunk));
977
978 endp.type = &ep_typeset.rtp;
979 trunk.vty_number_endpoints = 1;
980 trunk.endpoints = &endp;
981 endp.tcfg = &trunk;
982 INIT_LLIST_HEAD(&endp.conns);
983
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200984 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
985 uint32_t expected;
986 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200987
Philipp Maiercede2a42018-07-03 14:14:21 +0200988 struct mgcp_conn_rtp *conn = NULL;
989 struct mgcp_conn *_conn = NULL;
990 struct mgcp_rtp_state *state;
991 struct rate_ctr *packets_rx;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200992
Philipp Maiercede2a42018-07-03 14:14:21 +0200993 _conn =
994 mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
995 "test-connection");
996 conn = mgcp_conn_get_rtp(&endp, _conn->id);
997 state = &conn->state;
998 packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
999
1000 state->stats.initialized = 1;
1001 state->stats.base_seq = pl_test_dat[i].base_seq;
1002 state->stats.max_seq = pl_test_dat[i].max_seq;
1003 state->stats.cycles = pl_test_dat[i].cycles;
1004
1005 packets_rx->current = pl_test_dat[i].packets;
1006 calc_loss(conn, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001007
Philipp Maier87bd9be2017-08-22 16:35:41 +02001008 if (loss != pl_test_dat[i].loss
1009 || expected != pl_test_dat[i].expected) {
1010 printf
1011 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
1012 i, loss, pl_test_dat[i].loss, expected,
1013 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001014 }
Philipp Maiercede2a42018-07-03 14:14:21 +02001015
1016 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001017 }
Philipp Maiercede2a42018-07-03 14:14:21 +02001018
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001019}
1020
Philipp Maier87bd9be2017-08-22 16:35:41 +02001021int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
1022 uint32_t *pr, uint32_t *_or, int *loss,
1023 uint32_t *jitter)
1024{
1025 char *line, *save;
1026 int rc;
1027
1028 /* initialize with bad values */
1029 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
1030 *loss = INT_MAX;
1031
1032 line = strtok_r((char *)msg->l2h, "\r\n", &save);
1033 if (!line)
1034 return -1;
1035
1036 /* this can only parse the message that is created above... */
1037 for_each_non_empty_line(line, save) {
1038 switch (line[0]) {
1039 case 'P':
1040 rc = sscanf(line,
1041 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
1042 ps, os, pr, _or, loss, jitter);
1043 return rc == 6 ? 0 : -1;
1044 }
1045 }
1046
1047 return -1;
1048}
1049
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001050static void test_mgcp_stats(void)
1051{
1052 printf("Testing stat parsing\n");
1053
1054 uint32_t bps, bos, pr, _or, jitter;
1055 struct msgb *msg;
1056 int loss;
1057 int rc;
1058
Philipp Maierffd75e42017-11-22 11:44:50 +01001059 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001060 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1061 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001062 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
1063 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001064 printf("FAIL: Parsing failed1.\n");
1065 msgb_free(msg);
1066
Philipp Maier87bd9be2017-08-22 16:35:41 +02001067 msg =
1068 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +01001069 ("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 +02001070 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1071 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001072 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
1073 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001074 printf("FAIL: Parsing failed2.\n");
1075 msgb_free(msg);
1076}
1077
1078struct rtp_packet_info {
1079 float txtime;
1080 int len;
1081 char *data;
1082};
1083
1084struct rtp_packet_info test_rtp_packets1[] = {
1085 /* RTP: SeqNo=0, TS=0 */
1086 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
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=1, TS=160 */
1089 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001090 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001091 /* RTP: SeqNo=2, TS=320 */
1092 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001093 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001094 /* Repeat RTP timestamp: */
1095 /* RTP: SeqNo=3, TS=320 */
1096 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
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=4, TS=480 */
1099 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001101 /* RTP: SeqNo=5, TS=640 */
1102 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001103 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104 /* Double skip RTP timestamp (delta = 2*160): */
1105 /* RTP: SeqNo=6, TS=960 */
1106 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001107 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001108 /* RTP: SeqNo=7, TS=1120 */
1109 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001110 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001111 /* RTP: SeqNo=8, TS=1280 */
1112 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001113 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001114 /* Non 20ms RTP timestamp (delta = 120): */
1115 /* RTP: SeqNo=9, TS=1400 */
1116 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001117 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001118 /* RTP: SeqNo=10, TS=1560 */
1119 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001120 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001121 /* RTP: SeqNo=11, TS=1720 */
1122 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001123 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001124 /* SSRC changed to 0x10203040, RTP timestamp jump */
1125 /* RTP: SeqNo=12, TS=34688 */
1126 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001127 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001128 /* RTP: SeqNo=13, TS=34848 */
1129 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001130 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001131 /* RTP: SeqNo=14, TS=35008 */
1132 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001133 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001134 /* Non 20ms RTP timestamp (delta = 120): */
1135 /* RTP: SeqNo=15, TS=35128 */
1136 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001137 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001138 /* RTP: SeqNo=16, TS=35288 */
1139 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001140 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001141 /* RTP: SeqNo=17, TS=35448 */
1142 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001144 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1145 /* RTP: SeqNo=19, TS=35768 */
1146 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001147 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001148 /* RTP: SeqNo=20, TS=35928 */
1149 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001150 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001151 /* RTP: SeqNo=21, TS=36088 */
1152 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001153 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001154 /* Repeat last packet */
1155 /* RTP: SeqNo=21, TS=36088 */
1156 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001157 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001158 /* RTP: SeqNo=22, TS=36248 */
1159 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001160 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001161 /* RTP: SeqNo=23, TS=36408 */
1162 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001163 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001164 /* Don't increment SeqNo but increment timestamp by 160 */
1165 /* RTP: SeqNo=23, TS=36568 */
1166 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001167 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001168 /* RTP: SeqNo=24, TS=36728 */
1169 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001170 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001171 /* RTP: SeqNo=25, TS=36888 */
1172 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001173 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001174 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1175 * SeqNo jump */
1176 /* RTP: SeqNo=1000, TS=160000 */
1177 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001178 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001179 /* RTP: SeqNo=1001, TS=160160 */
1180 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001181 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001182 /* RTP: SeqNo=1002, TS=160320 */
1183 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001184 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185};
1186
Philipp Maier87bd9be2017-08-22 16:35:41 +02001187void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1188 struct mgcp_rtp_state *state,
1189 struct mgcp_rtp_end *rtp_end,
1190 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001191
1192static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1193{
1194 int i;
1195
1196 struct mgcp_trunk_config trunk;
1197 struct mgcp_endpoint endp;
1198 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001199 struct mgcp_rtp_end *rtp;
1200 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001201 char buffer[4096];
1202 uint32_t last_ssrc = 0;
1203 uint32_t last_timestamp = 0;
1204 uint32_t last_seqno = 0;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001205 uint64_t last_in_ts_err_cnt = 0;
1206 uint64_t last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001207 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001208 struct mgcp_conn *_conn = NULL;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001209 struct rate_ctr test_ctr_in;
1210 struct rate_ctr test_ctr_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001211
1212 printf("Testing packet error detection%s%s.\n",
1213 patch_ssrc ? ", patch SSRC" : "",
1214 patch_ts ? ", patch timestamps" : "");
1215
1216 memset(&trunk, 0, sizeof(trunk));
1217 memset(&endp, 0, sizeof(endp));
1218 memset(&state, 0, sizeof(state));
1219
Philipp Maier9e1d1642018-05-09 16:26:34 +02001220 memset(&test_ctr_in, 0, sizeof(test_ctr_in));
1221 memset(&test_ctr_out, 0, sizeof(test_ctr_out));
1222 state.in_stream.err_ts_ctr = &test_ctr_in;
1223 state.out_stream.err_ts_ctr = &test_ctr_out;
1224
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225 endp.type = &ep_typeset.rtp;
1226
Philipp Maierfcd06552017-11-10 17:32:22 +01001227 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001228 trunk.endpoints = &endp;
1229 trunk.force_constant_ssrc = patch_ssrc;
1230 trunk.force_aligned_timing = patch_ts;
1231
1232 endp.tcfg = &trunk;
1233
Philipp Maier87bd9be2017-08-22 16:35:41 +02001234 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001235 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1236 "test-connection");
1237 OSMO_ASSERT(_conn);
1238 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001239 OSMO_ASSERT(conn);
1240
1241 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001242
Philipp Maierbc0346e2018-06-07 09:52:16 +02001243 OSMO_ASSERT(mgcp_codec_add(conn, PTYPE_UNDEFINED, "AMR/8000/1") == 0);
1244 rtp->codec = &rtp->codecs[0];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001245
1246 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1247 struct rtp_packet_info *info = test_rtp_packets1 + i;
1248
1249 force_monotonic_time_us = round(1000000.0 * info->txtime);
1250
1251 OSMO_ASSERT(info->len <= sizeof(buffer));
1252 OSMO_ASSERT(info->len >= 0);
1253 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001254 mgcp_rtp_end_config(&endp, 1, rtp);
1255
1256 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1257 buffer, info->len);
1258
1259 if (state.out_stream.ssrc != last_ssrc) {
1260 printf("Output SSRC changed to %08x\n",
1261 state.out_stream.ssrc);
1262 last_ssrc = state.out_stream.ssrc;
1263 }
1264
1265 printf("In TS: %d, dTS: %d, Seq: %d\n",
1266 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001267 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001268
1269 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
Philipp Maier9e1d1642018-05-09 16:26:34 +02001270 "TS Err change: in +%u, out +%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001271 state.out_stream.last_timestamp - last_timestamp,
1272 state.out_stream.last_tsdelta,
1273 state.out_stream.last_seq - last_seqno,
Philipp Maier9e1d1642018-05-09 16:26:34 +02001274 (unsigned int) (state.in_stream.err_ts_ctr->current - last_in_ts_err_cnt),
1275 (unsigned int) (state.out_stream.err_ts_ctr->current - last_out_ts_err_cnt));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001276
1277 printf("Stats: Jitter = %u, Transit = %d\n",
Harald Welte49e3d5a2017-12-25 09:47:57 +01001278 calc_jitter(&state), state.stats.transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001279
Philipp Maier9e1d1642018-05-09 16:26:34 +02001280 last_in_ts_err_cnt = state.in_stream.err_ts_ctr->current;
1281 last_out_ts_err_cnt = state.out_stream.err_ts_ctr->current;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001282 last_timestamp = state.out_stream.last_timestamp;
1283 last_seqno = state.out_stream.last_seq;
1284 }
1285
1286 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001287 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001288}
1289
1290static void test_multilple_codec(void)
1291{
1292 struct mgcp_config *cfg;
1293 struct mgcp_endpoint *endp;
1294 struct msgb *inp, *resp;
1295 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001296 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001297 char conn_id[256];
Philipp Maiera74c0ea2018-08-02 11:59:09 +02001298 int i;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001299
1300 printf("Testing multiple payload types\n");
1301
1302 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001303 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001304 mgcp_endpoints_allocate(&cfg->trunk);
1305 cfg->policy_cb = mgcp_test_policy_cb;
1306 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1307
1308 /* Allocate endpoint 1@mgw with two codecs */
1309 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001310 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001311 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001312 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1313 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001314 msgb_free(inp);
1315 msgb_free(resp);
1316
1317 OSMO_ASSERT(last_endpoint == 1);
1318 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001319 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001320 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001321 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001322
1323 /* Allocate 2@mgw with three codecs, last one ignored */
1324 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001325 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001326 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001327 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1328 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001329 msgb_free(inp);
1330 msgb_free(resp);
1331
1332 OSMO_ASSERT(last_endpoint == 2);
1333 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001334 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001335 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001336 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001337
Philipp Maierbc0346e2018-06-07 09:52:16 +02001338 /* Allocate 3@mgw with no codecs, check for PT == 0 */
1339 /* Note: It usually makes no sense to leave the payload type list
1340 * out. However RFC 2327 does not clearly forbid this case and
1341 * it makes and since we already decided in OS#2658 that a missing
1342 * LCO should pick a sane default codec, it makes sense to expect
1343 * the same behaviour if SDP lacks proper payload type information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001344 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001345 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001346 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001347 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1348 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001349 msgb_free(inp);
1350 msgb_free(resp);
1351
1352 OSMO_ASSERT(last_endpoint == 3);
1353 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001354 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001355 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001356 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001357
1358 /* Allocate 4@mgw with a single codec */
1359 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001360 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001361 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001362 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1363 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001364 msgb_free(inp);
1365 msgb_free(resp);
1366
1367 OSMO_ASSERT(last_endpoint == 4);
1368 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001369 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001370 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001371 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001372
1373 /* Allocate 5@mgw at select GSM.. */
1374 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001375 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001376 talloc_free(cfg->trunk.audio_name);
1377 cfg->trunk.audio_name = "GSM/8000";
1378 cfg->trunk.no_audio_transcoding = 1;
1379 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001380 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1381 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001382 msgb_free(inp);
1383 msgb_free(resp);
1384
1385 OSMO_ASSERT(last_endpoint == 5);
1386 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001387 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001388 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001389 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001390
Philipp Maierffd75e42017-11-22 11:44:50 +01001391 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001392 last_endpoint = -1;
1393 resp = mgcp_handle_message(cfg, inp);
1394 msgb_free(inp);
1395 msgb_free(resp);
1396 OSMO_ASSERT(last_endpoint == 5);
1397 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001398 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001399 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001400 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001401 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001402 memset(&addr, 0, sizeof(addr));
1403 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001404 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001405
1406 /* Check what happens without that flag */
1407
Philipp Maier87bd9be2017-08-22 16:35:41 +02001408 /* Free the previous endpoint and the data and
1409 * check if the connection really vanished... */
Philipp Maier1355d7e2018-02-01 14:30:06 +01001410 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001411 talloc_free(endp->last_response);
1412 talloc_free(endp->last_trans);
1413 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001414 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001415 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001416
1417 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001418 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001419 cfg->trunk.no_audio_transcoding = 0;
1420 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001421 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1422 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001423 msgb_free(inp);
1424 msgb_free(resp);
1425
1426 OSMO_ASSERT(last_endpoint == 5);
1427 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001428 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001429 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001430 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001431
Philipp Maiera74c0ea2018-08-02 11:59:09 +02001432 for (i = 1; i < cfg->trunk.number_endpoints; i++)
1433 mgcp_endp_release(&cfg->trunk.endpoints[i]);
1434
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001435 talloc_free(cfg);
1436}
1437
1438static void test_no_cycle(void)
1439{
1440 struct mgcp_config *cfg;
1441 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001442 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001443 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001444
1445 printf("Testing no sequence flow on initial packet\n");
1446
1447 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001448 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001449 mgcp_endpoints_allocate(&cfg->trunk);
1450
1451 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001452
Philipp Maierffd75e42017-11-22 11:44:50 +01001453 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1454 "test-connection");
1455 OSMO_ASSERT(_conn);
1456 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001457 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001458
Harald Welte49e3d5a2017-12-25 09:47:57 +01001459 OSMO_ASSERT(conn->state.stats.initialized == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001460
1461 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001462 OSMO_ASSERT(conn->state.stats.initialized == 1);
1463 OSMO_ASSERT(conn->state.stats.cycles == 0);
1464 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001465
1466 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001467 OSMO_ASSERT(conn->state.stats.initialized == 1);
1468 OSMO_ASSERT(conn->state.stats.cycles == 0);
1469 OSMO_ASSERT(conn->state.stats.max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001470
1471 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001472 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001473 OSMO_ASSERT(conn->state.stats.initialized == 1);
1474 OSMO_ASSERT(conn->state.stats.cycles == 0);
1475 OSMO_ASSERT(conn->state.stats.max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001476
1477 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001478 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001479 OSMO_ASSERT(conn->state.stats.initialized == 1);
1480 OSMO_ASSERT(conn->state.stats.cycles == UINT16_MAX + 1);
1481 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001482
Philipp Maier1355d7e2018-02-01 14:30:06 +01001483 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001484 talloc_free(cfg);
1485}
1486
1487static void test_no_name(void)
1488{
1489 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001490 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001491
1492 printf("Testing no rtpmap name\n");
1493 cfg = mgcp_config_alloc();
1494
Philipp Maierfcd06552017-11-10 17:32:22 +01001495 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001496 cfg->trunk.audio_send_name = 0;
1497 mgcp_endpoints_allocate(&cfg->trunk);
1498
1499 cfg->policy_cb = mgcp_test_policy_cb;
1500
1501 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1502
Philipp Maierffd75e42017-11-22 11:44:50 +01001503 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001504 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001505
1506 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001507 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001508 (char *)msg->data);
1509 OSMO_ASSERT(false);
1510 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001511 msgb_free(inp);
1512 msgb_free(msg);
1513
Philipp Maier1355d7e2018-02-01 14:30:06 +01001514 mgcp_endp_release(&cfg->trunk.endpoints[1]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001515 talloc_free(cfg);
1516}
1517
1518static void test_osmux_cid(void)
1519{
1520 int id, i;
1521
1522 OSMO_ASSERT(osmux_used_cid() == 0);
1523 id = osmux_get_cid();
1524 OSMO_ASSERT(id == 0);
1525 OSMO_ASSERT(osmux_used_cid() == 1);
1526 osmux_put_cid(id);
1527 OSMO_ASSERT(osmux_used_cid() == 0);
1528
1529 for (i = 0; i < 256; ++i) {
1530 id = osmux_get_cid();
1531 OSMO_ASSERT(id == i);
1532 OSMO_ASSERT(osmux_used_cid() == i + 1);
1533 }
1534
1535 id = osmux_get_cid();
1536 OSMO_ASSERT(id == -1);
1537
1538 for (i = 0; i < 256; ++i)
1539 osmux_put_cid(i);
1540 OSMO_ASSERT(osmux_used_cid() == 0);
1541}
1542
1543static const struct log_info_cat log_categories[] = {
1544};
1545
1546const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001547 .cat = log_categories,
1548 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001549};
1550
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001551static void test_get_lco_identifier(void)
1552{
1553 char *test;
1554 printf("Testing get_lco_identifier()\n");
1555
1556 /* Normal case at the beginning */
1557 test = "p:10, a:PCMU";
1558 printf("%s -> %s\n", test, get_lco_identifier(test));
1559
1560 test = "p:10, a:PCMU";
1561 printf("%s -> %s\n", test, get_lco_identifier(test));
1562
1563 /* Begin parsing in the middle of the value part of
1564 * the previous LCO option value */
1565 test = "XXXX, p:10, a:PCMU";
1566 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1567
1568 test = "XXXX,p:10,a:PCMU";
1569 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1570
1571 test = "10,a:PCMU";
1572 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1573
1574 test = "10, a:PCMU";
1575 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1576
1577 test = "10,a: PCMU";
1578 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1579
1580 test = "10 ,a: PCMU";
1581 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1582
1583 /* Begin parsing right at the end of the previous LCO
1584 * option value */
1585 test = ", a:PCMU";
1586 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1587
1588 test = " a:PCMU";
1589 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1590
1591 /* Empty string, result should be (null) */
1592 test = "";
1593 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1594
1595 /* Missing colons, result should be (null) */
1596 test = "p10, aPCMU";
1597 printf("%s -> %s\n", test, get_lco_identifier(test));
1598
1599 /* Colon separated from the identifier, result should be (null) */
1600 test = "10,a :PCMU";
1601 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1602}
1603
1604static void test_check_local_cx_options(void *ctx)
1605{
1606 /* Legal cases */
1607 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1608 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU") == 0);
1609 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p:10, IN:10") == 0);
1610 OSMO_ASSERT(check_local_cx_options(ctx, "one:AAA, two:BB, tree:C") ==
1611 0);
1612 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1613 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:G726-32") == 0);
1614 OSMO_ASSERT(check_local_cx_options(ctx, "p:10-20, b:64") == 0);
1615 OSMO_ASSERT(check_local_cx_options(ctx, "b:32-64, e:off") == 0);
1616 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU;G726-32") == 0);
1617
1618 /* Illegal spaces before and after colon */
1619 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p :10") == -1);
1620 OSMO_ASSERT(check_local_cx_options(ctx, "a :PCMU, p:10") == -1);
1621 OSMO_ASSERT(check_local_cx_options(ctx, "p: 10, a:PCMU") == -1);
1622
1623 /* Check if multiple appearances of LCOs are rejected */
1624 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, p:10") == -1);
1625 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, a:PCMU, p:10") ==
1626 -1);
1627 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, p:10") == -1);
1628
1629 /* Check if empty LCO are rejected */
1630 OSMO_ASSERT(check_local_cx_options(ctx, "p: , a:PCMU") == -1);
1631 OSMO_ASSERT(check_local_cx_options(ctx, "p: , a: PCMU") == -1);
1632 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a: PCMU") == -1);
1633 OSMO_ASSERT(check_local_cx_options(ctx, "p:, a:PCMU") == -1);
1634 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:") == -1);
1635
1636 /* Garbeled beginning and ends */
1637 OSMO_ASSERT(check_local_cx_options(ctx, ":10, a:10") == -1);
1638 OSMO_ASSERT(check_local_cx_options(ctx, "10, a:PCMU") == -1);
1639 OSMO_ASSERT(check_local_cx_options(ctx, ", a:PCMU") == -1);
1640 OSMO_ASSERT(check_local_cx_options(ctx, " a:PCMU") == -1);
1641 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU,") == -1);
1642 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, ") == -1);
1643
1644 /* Illegal strings */
1645 OSMO_ASSERT(check_local_cx_options(ctx, " ") == -1);
1646 OSMO_ASSERT(check_local_cx_options(ctx, "") == -1);
1647 OSMO_ASSERT(check_local_cx_options(ctx, "AAA") == -1);
1648 OSMO_ASSERT(check_local_cx_options(ctx, ":,") == -1);
1649 OSMO_ASSERT(check_local_cx_options(ctx, ",:") == -1);
1650 OSMO_ASSERT(check_local_cx_options(ctx, ",,,") == -1);
1651}
1652
Philipp Maier6931f9a2018-07-26 09:29:31 +02001653static void test_mgcp_codec_pt_translate_pars(struct mgcp_rtp_codec *c)
1654{
1655 c->rate = 8000;
1656 c->channels = 1;
1657 c->frame_duration_num = 23;
1658 c->frame_duration_den = 42;
1659}
1660
1661static void test_mgcp_codec_pt_translate(void)
1662{
1663 struct mgcp_conn_rtp conn_src;
1664 struct mgcp_conn_rtp conn_dst;
1665 int pt_dst;
1666
1667 /* Setup a realistic set of codec configurations on both
1668 * ends. AMR and HR will use different payload types. PCMU
1669 * must use 0 on both ends since this is not a dynamic payload
1670 * type */
1671 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[0]);
1672 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[0]);
1673 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[1]);
1674 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[1]);
1675 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[2]);
1676 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[2]);
1677 conn_src.end.codecs[0].payload_type = 112;
1678 conn_dst.end.codecs[0].payload_type = 96;
1679 conn_src.end.codecs[1].payload_type = 0;
1680 conn_dst.end.codecs[1].payload_type = 0;
1681 conn_src.end.codecs[2].payload_type = 111;
1682 conn_dst.end.codecs[2].payload_type = 97;
1683 conn_src.end.codecs[0].audio_name = "AMR/8000/1";
1684 conn_dst.end.codecs[0].audio_name = "AMR/8000/1";
1685 conn_src.end.codecs[1].audio_name = "PCMU/8000/1";
1686 conn_dst.end.codecs[1].audio_name = "PCMU/8000/1";
1687 conn_src.end.codecs[2].audio_name = "GSM-HR-08/8000/1";
1688 conn_dst.end.codecs[2].audio_name = "GSM-HR-08/8000/1";
1689 conn_src.end.codecs[0].subtype_name = "AMR";
1690 conn_dst.end.codecs[0].subtype_name = "AMR";
1691 conn_src.end.codecs[1].subtype_name = "PCMU";
1692 conn_dst.end.codecs[1].subtype_name = "PCMU";
1693 conn_src.end.codecs[2].subtype_name = "GSM-HR-08";
1694 conn_dst.end.codecs[2].subtype_name = "GSM-HR-08";
1695 conn_src.end.codecs_assigned = 3;
1696 conn_dst.end.codecs_assigned = 3;
1697
1698 /* We expect the function to find the PT we must use when we send the
1699 * packet out to the destination. All we know is the context for both
1700 * connections and the payload type from the source packet */
1701 pt_dst =
1702 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1703 conn_src.end.codecs[0].payload_type);
1704 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[0].payload_type);
1705 pt_dst =
1706 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1707 conn_src.end.codecs[1].payload_type);
1708 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[1].payload_type);
1709 pt_dst =
1710 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1711 conn_src.end.codecs[2].payload_type);
1712 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[2].payload_type);
1713
1714 /* Try some constellations that must fail */
1715 pt_dst = mgcp_codec_pt_translate(&conn_src, &conn_dst, 123);
1716 OSMO_ASSERT(pt_dst == -EINVAL);
1717 conn_src.end.codecs_assigned = 0;
1718 conn_dst.end.codecs_assigned = 3;
1719 pt_dst =
1720 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1721 conn_src.end.codecs[0].payload_type);
1722 OSMO_ASSERT(pt_dst == -EINVAL);
1723 pt_dst =
1724 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1725 conn_src.end.codecs[1].payload_type);
1726 OSMO_ASSERT(pt_dst == -EINVAL);
1727 pt_dst =
1728 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1729 conn_src.end.codecs[2].payload_type);
1730 OSMO_ASSERT(pt_dst == -EINVAL);
1731 conn_src.end.codecs_assigned = 3;
1732 conn_dst.end.codecs_assigned = 0;
1733 pt_dst =
1734 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1735 conn_src.end.codecs[0].payload_type);
1736 OSMO_ASSERT(pt_dst == -EINVAL);
1737 pt_dst =
1738 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1739 conn_src.end.codecs[1].payload_type);
1740 OSMO_ASSERT(pt_dst == -EINVAL);
1741 pt_dst =
1742 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1743 conn_src.end.codecs[2].payload_type);
1744 OSMO_ASSERT(pt_dst == -EINVAL);
1745}
1746
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001747int main(int argc, char **argv)
1748{
Neels Hofmeyr60f8e312018-03-30 23:01:07 +02001749 void *ctx = talloc_named_const(NULL, 0, "mgcp_test");
1750 void *msgb_ctx = msgb_talloc_ctx_init(ctx, 0);
1751 osmo_init_logging2(ctx, &log_info);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001752
1753 test_strline();
1754 test_values();
1755 test_messages();
1756 test_retransmission();
1757 test_packet_loss_calc();
1758 test_rqnt_cb();
1759 test_mgcp_stats();
1760 test_packet_error_detection(1, 0);
1761 test_packet_error_detection(0, 0);
1762 test_packet_error_detection(0, 1);
1763 test_packet_error_detection(1, 1);
1764 test_multilple_codec();
1765 test_no_cycle();
1766 test_no_name();
1767 test_osmux_cid();
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001768 test_get_lco_identifier();
1769 test_check_local_cx_options(ctx);
Philipp Maier6931f9a2018-07-26 09:29:31 +02001770 test_mgcp_codec_pt_translate();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001771
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001772 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1773 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1774 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001775 printf("Done\n");
1776 return EXIT_SUCCESS;
1777}