blob: 4cb16dd798ec01ad2c7a39c7e34815b1558742e5 [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
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200233#define MDCX_TOO_LONG_CI \
234 "MDCX 18983222 1@mgw MGCP 1.0\r\n" \
235 "I: 123456789012345678901234567890123\n"
236
237#define MDCX_TOO_LONG_CI_RET "510 18983222 FAIL\r\n"
238
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200239#define SHORT2 "CRCX 1"
240#define SHORT2_RET "510 000000 FAIL\r\n"
241#define SHORT3 "CRCX 1 1@mgw"
242#define SHORT4 "CRCX 1 1@mgw MGCP"
243#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
244
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245#define CRCX \
246 "CRCX 2 1@mgw MGCP 1.0\r\n" \
247 "M: recvonly\r\n" \
248 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200249 "L: p:20\r\n" \
250 "\r\n" \
251 "v=0\r\n" \
252 "c=IN IP4 123.12.12.123\r\n" \
253 "m=audio 5904 RTP/AVP 97\r\n" \
254 "a=rtpmap:97 GSM-EFR/8000\r\n" \
255 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200256
Philipp Maier87bd9be2017-08-22 16:35:41 +0200257#define CRCX_RET \
258 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100259 "I: %s\r\n" \
260 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200261 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100262 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200263 "s=-\r\n" \
264 "c=IN IP4 0.0.0.0\r\n" \
265 "t=0 0\r\n" \
266 "m=audio 16002 RTP/AVP 97\r\n" \
267 "a=rtpmap:97 GSM-EFR/8000\r\n" \
268 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200269
Philipp Maier87bd9be2017-08-22 16:35:41 +0200270#define CRCX_RET_NO_RTPMAP \
271 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100272 "I: %s\r\n" \
273 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200274 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100275 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200276 "s=-\r\n" \
277 "c=IN IP4 0.0.0.0\r\n" \
278 "t=0 0\r\n" \
279 "m=audio 16002 RTP/AVP 97\r\n" \
280 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200281
Philipp Maier87bd9be2017-08-22 16:35:41 +0200282#define CRCX_FMTP_RET \
283 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100284 "I: %s\r\n" \
285 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200286 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100287 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200288 "s=-\r\n" \
289 "c=IN IP4 0.0.0.0\r\n" \
290 "t=0 0\r\n" \
291 "m=audio 16006 RTP/AVP 97\r\n" \
292 "a=rtpmap:97 GSM-EFR/8000\r\n" \
293 "a=fmtp:126 0/1/2\r\n" \
294 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200295
Philipp Maier87bd9be2017-08-22 16:35:41 +0200296#define CRCX_ZYN \
297 "CRCX 2 1@mgw MGCP 1.0\r" \
298 "M: recvonly\r" \
299 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200300 "\n" \
301 "v=0\r" \
302 "c=IN IP4 123.12.12.123\r" \
303 "m=audio 5904 RTP/AVP 97\r" \
304 "a=rtpmap:97 GSM-EFR/8000\r"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200305
Philipp Maier87bd9be2017-08-22 16:35:41 +0200306#define CRCX_ZYN_RET \
307 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100308 "I: %s\r\n" \
309 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200310 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100311 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200312 "s=-\r\n" \
313 "c=IN IP4 0.0.0.0\r\n" \
314 "t=0 0\r\n" \
315 "m=audio 16004 RTP/AVP 97\r\n" \
316 "a=rtpmap:97 GSM-EFR/8000\r\n" \
317 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200318
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200319#define CRCX_X_OSMO_IGN \
320 "CRCX 2 1@mgw MGCP 1.0\r\n" \
321 "M: recvonly\r\n" \
322 "C: 2\r\n" \
323 "L: p:20\r\n" \
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200324 "X-Osmo-IGN: C foo\r\n" \
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200325 "\r\n" \
326 "v=0\r\n" \
327 "c=IN IP4 123.12.12.123\r\n" \
328 "m=audio 5904 RTP/AVP 97\r\n" \
329 "a=rtpmap:97 GSM-EFR/8000\r\n" \
330 "a=ptime:40\r\n"
331
332#define CRCX_X_OSMO_IGN_RET \
333 "200 2 OK\r\n" \
334 "I: %s\r\n" \
335 "\r\n" \
336 "v=0\r\n" \
337 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
338 "s=-\r\n" \
339 "c=IN IP4 0.0.0.0\r\n" \
340 "t=0 0\r\n" \
341 "m=audio 16010 RTP/AVP 97\r\n" \
342 "a=rtpmap:97 GSM-EFR/8000\r\n" \
343 "a=ptime:40\r\n"
344
Philipp Maier87bd9be2017-08-22 16:35:41 +0200345#define DLCX \
346 "DLCX 7 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100347 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200348 "C: 2\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200349
Philipp Maier87bd9be2017-08-22 16:35:41 +0200350#define DLCX_RET \
351 "250 7 OK\r\n" \
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100352 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n"
353
354 #define DLCX_RET_OSMUX DLCX_RET \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200355 "X-Osmo-CP: EC TI=0, TO=0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200356
Philipp Maier87bd9be2017-08-22 16:35:41 +0200357#define RQNT \
358 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
359 "X: B244F267488\r\n" \
360 "S: D/9\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200361
Philipp Maier87bd9be2017-08-22 16:35:41 +0200362#define RQNT2 \
363 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
364 "X: ADD4F26746F\r\n" \
365 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366
367#define RQNT1_RET "200 186908780 OK\r\n"
368#define RQNT2_RET "200 186908781 OK\r\n"
369
Philipp Maier87bd9be2017-08-22 16:35:41 +0200370#define PTYPE_IGNORE 0 /* == default initializer */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200371#define PTYPE_NONE 128
372#define PTYPE_NYI PTYPE_NONE
373
Philipp Maier87bd9be2017-08-22 16:35:41 +0200374#define CRCX_MULT_1 \
375 "CRCX 2 1@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200376 "M: recvonly\r\n" \
377 "C: 2\r\n" \
378 "X\r\n" \
379 "L: p:20\r\n" \
380 "\r\n" \
381 "v=0\r\n" \
382 "c=IN IP4 123.12.12.123\r\n" \
383 "m=audio 5904 RTP/AVP 18 97\r\n" \
384 "a=rtpmap:18 G729/8000\r\n" \
385 "a=rtpmap:97 GSM-EFR/8000\r\n" \
386 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200387
Philipp Maier87bd9be2017-08-22 16:35:41 +0200388#define CRCX_MULT_2 \
389 "CRCX 2 2@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200390 "M: recvonly\r\n" \
391 "C: 2\r\n" \
392 "X\r\n" \
393 "L: p:20\r\n" \
394 "\r\n" \
395 "v=0\r\n" \
396 "c=IN IP4 123.12.12.123\r\n" \
397 "m=audio 5904 RTP/AVP 18 97 101\r\n" \
398 "a=rtpmap:18 G729/8000\r\n" \
399 "a=rtpmap:97 GSM-EFR/8000\r\n" \
400 "a=rtpmap:101 FOO/8000\r\n" \
401 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200402
Philipp Maier87bd9be2017-08-22 16:35:41 +0200403#define CRCX_MULT_3 \
404 "CRCX 2 3@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200405 "M: recvonly\r\n" \
406 "C: 2\r\n" \
407 "X\r\n" \
408 "L: p:20\r\n" \
409 "\r\n" \
410 "v=0\r\n" \
411 "c=IN IP4 123.12.12.123\r\n" \
412 "m=audio 5904 RTP/AVP\r\n" \
413 "a=rtpmap:18 G729/8000\r\n" \
414 "a=rtpmap:97 GSM-EFR/8000\r\n" \
415 "a=rtpmap:101 FOO/8000\r\n" \
416 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200417
Philipp Maier87bd9be2017-08-22 16:35:41 +0200418#define CRCX_MULT_4 \
419 "CRCX 2 4@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200420 "M: recvonly\r\n" \
421 "C: 2\r\n" \
422 "X\r\n" \
423 "L: p:20\r\n" \
424 "\r\n" \
425 "v=0\r\n" \
426 "c=IN IP4 123.12.12.123\r\n" \
427 "m=audio 5904 RTP/AVP 18\r\n" \
428 "a=rtpmap:18 G729/8000\r\n" \
429 "a=rtpmap:97 GSM-EFR/8000\r\n" \
430 "a=rtpmap:101 FOO/8000\r\n" \
431 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200432
433#define CRCX_MULT_GSM_EXACT \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200434 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
435 "C: 1355c6041e\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200436 "L: p:20, a:GSM, nt:IN\r\n" \
437 "M: recvonly\r\n" \
438 "\r\n" \
439 "v=0\r\n" \
440 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
441 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200442 "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 +0200443 "a=rtpmap:0 PCMU/8000\r\n" \
444 "a=rtpmap:8 PCMA/8000\r\n" \
445 "a=rtpmap:3 gsm/8000\r\n" \
446 "a=rtpmap:18 G729/8000\r\n" \
447 "a=fmtp:18 annexb=no\r\n" \
448 "a=rtpmap:4 G723/8000\r\n" \
449 "a=rtpmap:96 iLBC/8000\r\n" \
450 "a=fmtp:96 mode=20\r\n" \
451 "a=rtpmap:97 iLBC/8000\r\n" \
452 "a=fmtp:97 mode=30\r\n" \
453 "a=rtpmap:101 telephone-event/8000\r\n" \
454 "a=fmtp:101 0-15\r\n" \
455 "a=recvonly\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200456
Philipp Maier87bd9be2017-08-22 16:35:41 +0200457#define MDCX_NAT_DUMMY \
458 "MDCX 23 5@mgw MGCP 1.0\r\n" \
459 "C: 1355c6041e\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100460 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200461 "\r\n" \
462 "c=IN IP4 8.8.8.8\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200463 "m=audio 16434 RTP/AVP 3\r\n"
464
465#define CRCX_NO_LCO_NO_SDP \
466 "CRCX 2 6@mgw MGCP 1.0\r\n" \
467 "M: recvonly\r\n" \
468 "C: 2\r\n"
469
470#define CRCX_NO_LCO_NO_SDP_RET \
471 "200 2 OK\r\n" \
472 "I: %s\r\n" \
473 "\r\n" \
474 "v=0\r\n" \
475 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
476 "s=-\r\n" \
477 "c=IN IP4 0.0.0.0\r\n" \
478 "t=0 0\r\n" \
479 "m=audio 16008 RTP/AVP 0\r\n" \
480 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200481
482struct mgcp_test {
483 const char *name;
484 const char *req;
485 const char *exp_resp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200486 int ptype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200487 const char *extra_fmtp;
488};
489
490static const struct mgcp_test tests[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200491 {"AUEP1", AUEP1, AUEP1_RET},
492 {"AUEP2", AUEP2, AUEP2_RET},
493 {"MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET},
494 {"MDCX2", MDCX_UNALLOCATED, MDCX_RET},
495 {"CRCX", CRCX, CRCX_RET, 97},
496 {"MDCX3", MDCX3, MDCX3_RET, PTYPE_IGNORE},
497 {"MDCX4", MDCX4, MDCX4_RET("18983216"), 99},
498 {"MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99},
499 {"MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99},
500 {"MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99},
501 {"MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99},
502 {"MDCX4_RO", MDCX4_RO, MDCX4_RO_RET("18983221"), PTYPE_IGNORE},
503 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
504 {"CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97},
505 {"EMPTY", EMPTY, EMPTY_RET},
506 {"SHORT1", SHORT, SHORT_RET},
507 {"SHORT2", SHORT2, SHORT2_RET},
508 {"SHORT3", SHORT3, SHORT2_RET},
509 {"SHORT4", SHORT4, SHORT2_RET},
510 {"RQNT1", RQNT, RQNT1_RET},
511 {"RQNT2", RQNT2, RQNT2_RET},
512 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
513 {"CRCX", CRCX, CRCX_FMTP_RET, 97,.extra_fmtp = "a=fmtp:126 0/1/2"},
514 {"MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE,.extra_fmtp =
515 "a=fmtp:126 0/1/2"},
516 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE,.extra_fmtp = "a=fmtp:126 0/1/2"},
Philipp Maierbc0346e2018-06-07 09:52:16 +0200517 {"CRCX", CRCX_NO_LCO_NO_SDP, CRCX_NO_LCO_NO_SDP_RET, 97},
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200518 {"CRCX", CRCX_X_OSMO_IGN, CRCX_X_OSMO_IGN_RET, 97},
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200519 {"MDCX_TOO_LONG_CI", MDCX_TOO_LONG_CI, MDCX_TOO_LONG_CI_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200520};
521
522static const struct mgcp_test retransmit[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200523 {"CRCX", CRCX, CRCX_RET},
524 {"RQNT1", RQNT, RQNT1_RET},
525 {"RQNT2", RQNT2, RQNT2_RET},
526 {"MDCX3", MDCX3, MDCX3A_RET},
527 {"DLCX", DLCX, DLCX_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200528};
529
Philipp Maierffd75e42017-11-22 11:44:50 +0100530static struct msgb *create_msg(const char *str, const char *conn_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200531{
532 struct msgb *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100533 int len;
534
535 printf("creating message from statically defined input:\n");
536 printf("---------8<---------\n%s\n---------8<---------\n", str);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200537
538 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Philipp Maierffd75e42017-11-22 11:44:50 +0100539 if (conn_id && strlen(conn_id))
540 len = sprintf((char *)msg->data, str, conn_id, conn_id);
541 else
542 len = sprintf((char *)msg->data, "%s", str);
543
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200544 msg->l2h = msgb_put(msg, len);
545 return msg;
546}
547
548static int last_endpoint = -1;
549
550static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
551 int state, const char *transactio_id)
552{
553 fprintf(stderr, "Policy CB got state %d on endpoint %d\n",
554 state, endpoint);
555 last_endpoint = endpoint;
556 return MGCP_POLICY_CONT;
557}
558
559#define MGCP_DUMMY_LOAD 0x23
560static int dummy_packets = 0;
561/* override and forward */
562ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200563 const struct sockaddr *dest_addr, socklen_t addrlen)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200564{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200565 uint32_t dest_host =
566 htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
567 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200568
Philipp Maier87bd9be2017-08-22 16:35:41 +0200569 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
570 fprintf(stderr,
571 "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
572 dest_host, dest_port, len, osmo_hexdump(buf, len));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200573 dummy_packets += 1;
574 }
575
Philipp Maier3d9b6562017-10-13 18:33:44 +0200576 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200577}
578
579static int64_t force_monotonic_time_us = -1;
580/* override and forward */
581int clock_gettime(clockid_t clk_id, struct timespec *tp)
582{
583 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
584 static clock_gettime_t real_clock_gettime = NULL;
585
586 if (!real_clock_gettime)
587 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
588
589 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
590 tp->tv_sec = force_monotonic_time_us / 1000000;
591 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
592 return 0;
593 }
594
595 return real_clock_gettime(clk_id, tp);
596}
597
598#define CONN_UNMODIFIED (0x1000)
599
600static void test_values(void)
601{
602 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200603 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200604
605 /* Check that LOOPBACK enables all output */
606 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200607 MGCP_CONN_RECV_SEND);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200608}
609
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200610/* Extract a connection ID from a response and return in conn_id;
611 * if there is none, return -EINVAL and leave conn_id unchanged. */
Philipp Maierffd75e42017-11-22 11:44:50 +0100612static int get_conn_id_from_response(uint8_t *resp, char *conn_id,
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200613 size_t conn_id_buflen)
Philipp Maierffd75e42017-11-22 11:44:50 +0100614{
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200615 const char *conn_id_start;
616 const char *conn_id_end;
617 int conn_id_len;
Philipp Maierffd75e42017-11-22 11:44:50 +0100618
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200619 const char *header_I = "\r\nI: ";
620 const char *header_o = "\r\no=- ";
Philipp Maierffd75e42017-11-22 11:44:50 +0100621
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200622 /* Try to get the conn_id from the 'I:' or 'o=-' parameter */
623 if ((conn_id_start = strstr((char *)resp, header_I))) {
624 conn_id_start += strlen(header_I);
625 conn_id_end = strstr(conn_id_start, "\r\n");
626 } else if ((conn_id_start = strstr((char *)resp, header_o))) {
627 conn_id_start += strlen(header_o);
628 conn_id_end = strchr(conn_id_start, ' ');
629 } else
630 return -EINVAL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100631
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200632 if (conn_id_end)
633 conn_id_len = conn_id_end - conn_id_start;
634 else
635 conn_id_len = strlen(conn_id_start);
636 OSMO_ASSERT(conn_id_len <= conn_id_buflen - 1);
Philipp Maier55295f72018-01-15 14:00:28 +0100637
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200638 /* A valid conn_id must at least contain one digit, and must
639 * not exceed a length of 32 digits */
640 OSMO_ASSERT(conn_id_len <= 32);
641 OSMO_ASSERT(conn_id_len > 0);
642
643 strncpy(conn_id, conn_id_start, conn_id_len);
644 conn_id[conn_id_len] = '\0';
645 return 0;
Philipp Maierffd75e42017-11-22 11:44:50 +0100646}
647
648/* Check response, automatically patch connection ID if needed */
649static int check_response(uint8_t *resp, const char *exp_resp)
650{
651 char exp_resp_patched[4096];
652 const char *exp_resp_ptr;
653 char conn_id[256];
654
655 printf("checking response:\n");
656
657 /* If the expected response is intened to be patched
658 * (%s placeholder inside) we will patch it with the
659 * connection identifier we just received from the
660 * real response. This is necessary because the CI
661 * is generated by the mgcp code on CRCX and we can
662 * not know it in advance */
663 if (strstr(exp_resp, "%s")) {
664 if (get_conn_id_from_response(resp, conn_id, sizeof(conn_id)) ==
665 0) {
666 sprintf(exp_resp_patched, exp_resp, conn_id, conn_id);
667 exp_resp_ptr = exp_resp_patched;
668 printf
669 ("using message with patched conn_id for comparison\n");
670 } else {
671 printf
672 ("patching conn_id failed, using message as statically defined for comparison\n");
673 exp_resp_ptr = exp_resp;
674 }
675 } else {
676 printf("using message as statically defined for comparison\n");
677 exp_resp_ptr = exp_resp;
678 }
679
680 if (strcmp((char *)resp, exp_resp_ptr) != 0) {
681 printf("Unexpected response, please check!\n");
682 printf
683 ("Got:\n---------8<---------\n%s\n---------8<---------\n\n",
684 resp);
685 printf
686 ("Expected:\n---------8<---------\n%s\n---------8<---------\n",
687 exp_resp_ptr);
688 return -EINVAL;
689 }
690
691 printf("Response matches our expectations.\n");
692 return 0;
693}
694
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200695static void test_messages(void)
696{
697 struct mgcp_config *cfg;
698 struct mgcp_endpoint *endp;
699 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200700 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100701 char last_conn_id[256];
Philipp Maier7df419b2017-12-04 17:11:42 +0100702 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200703
704 cfg = mgcp_config_alloc();
705
Philipp Maierfcd06552017-11-10 17:32:22 +0100706 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200707 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200708 cfg->policy_cb = mgcp_test_policy_cb;
709
Philipp Maierffd75e42017-11-22 11:44:50 +0100710 memset(last_conn_id, 0, sizeof(last_conn_id));
711
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200712 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
713
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200714 for (i = 0; i < ARRAY_SIZE(tests); i++) {
715 const struct mgcp_test *t = &tests[i];
716 struct msgb *inp;
717 struct msgb *msg;
718
Philipp Maierffd75e42017-11-22 11:44:50 +0100719 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200720 printf("Testing %s\n", t->name);
721
722 last_endpoint = -1;
723 dummy_packets = 0;
724
Philipp Maier87bd9be2017-08-22 16:35:41 +0200725 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
726 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200727
Philipp Maierffd75e42017-11-22 11:44:50 +0100728 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200729 msg = mgcp_handle_message(cfg, inp);
730 msgb_free(inp);
731 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200732 if (msg) {
733 printf("%s failed '%s'\n", t->name,
734 (char *)msg->data);
735 OSMO_ASSERT(false);
736 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100737 } else if (check_response(msg->data, t->exp_resp) != 0) {
738 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200739 OSMO_ASSERT(false);
740 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100741
Philipp Maier7df419b2017-12-04 17:11:42 +0100742 if (msg) {
743 rc = get_conn_id_from_response(msg->data, last_conn_id,
744 sizeof(last_conn_id));
Neels Hofmeyr08e07042018-08-28 16:22:14 +0200745 if (rc == 0)
Philipp Maier7df419b2017-12-04 17:11:42 +0100746 printf("(response contains a connection id)\n");
747 else
748 printf("(response does not contain a connection id)\n");
749 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100750
Philipp Maiera330b862017-12-04 17:16:16 +0100751 if (msg)
752 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200753
754 if (dummy_packets)
755 printf("Dummy packets: %d\n", dummy_packets);
756
757 if (last_endpoint != -1) {
758 endp = &cfg->trunk.endpoints[last_endpoint];
759
Philipp Maier01d24a32017-11-21 17:26:09 +0100760 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200761 if (conn) {
762 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200763
Philipp Maier87bd9be2017-08-22 16:35:41 +0200764 if (conn->end.packet_duration_ms != -1)
765 printf("Detected packet duration: %d\n",
766 conn->end.packet_duration_ms);
767 else
768 printf("Packet duration not set\n");
769 if (endp->local_options.pkt_period_min ||
770 endp->local_options.pkt_period_max)
771 printf
772 ("Requested packetetization period: "
773 "%d-%d\n",
774 endp->local_options.pkt_period_min,
775 endp->
776 local_options.pkt_period_max);
777 else
778 printf
779 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200780
Philipp Maier87bd9be2017-08-22 16:35:41 +0200781 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
782 printf("Connection mode: %d:%s%s%s%s\n",
783 conn->conn->mode,
784 !conn->conn->mode ? " NONE" : "",
785 conn->conn->mode & MGCP_CONN_SEND_ONLY
786 ? " SEND" : "",
787 conn->conn->mode & MGCP_CONN_RECV_ONLY
788 ? " RECV" : "",
789 conn->conn->mode & MGCP_CONN_LOOPBACK
790 & ~MGCP_CONN_RECV_SEND
791 ? " LOOP" : "");
792 fprintf(stderr,
793 "RTP output %sabled, NET output %sabled\n",
794 conn->end.output_enabled
795 ? "en" : "dis",
796 conn->end.output_enabled
797 ? "en" : "dis");
798 } else
799 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200800
Philipp Maier87bd9be2017-08-22 16:35:41 +0200801 OSMO_ASSERT(conn->end.output_enabled
802 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
803
804 conn->conn->mode |= CONN_UNMODIFIED;
805
806 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200807 endp->local_options.pkt_period_min = 0;
808 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200809 }
810
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100812 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200813 OSMO_ASSERT(last_endpoint != -1);
814 endp = &cfg->trunk.endpoints[last_endpoint];
815
816 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200817 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200818 last_endpoint,
Philipp Maierbc0346e2018-06-07 09:52:16 +0200819 conn->end.codec->payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200820
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821 if (t->ptype != PTYPE_IGNORE)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200822 OSMO_ASSERT(conn->end.codec->payload_type ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200823 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200824
825 /* Reset them again for next test */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200826 conn->end.codec->payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200827 }
828 }
829
830 talloc_free(cfg);
831}
832
833static void test_retransmission(void)
834{
835 struct mgcp_config *cfg;
836 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100837 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100838 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200839
840 cfg = mgcp_config_alloc();
841
Philipp Maierfcd06552017-11-10 17:32:22 +0100842 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200843 mgcp_endpoints_allocate(&cfg->trunk);
844
Philipp Maierffd75e42017-11-22 11:44:50 +0100845 memset(last_conn_id, 0, sizeof(last_conn_id));
846
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200847 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
848
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200849 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
850 const struct mgcp_test *t = &retransmit[i];
851 struct msgb *inp;
852 struct msgb *msg;
853
Philipp Maierffd75e42017-11-22 11:44:50 +0100854 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200855 printf("Testing %s\n", t->name);
856
Philipp Maierffd75e42017-11-22 11:44:50 +0100857 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200859
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200860 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100861 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200862 printf("%s failed '%s'\n", t->name, (char *)msg->data);
863 OSMO_ASSERT(false);
864 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100865
Philipp Maier23b8e292017-12-04 16:48:45 +0100866 if (msg && strcmp(t->name, "CRCX") == 0) {
867 rc = get_conn_id_from_response(msg->data, last_conn_id,
868 sizeof(last_conn_id));
869 OSMO_ASSERT(rc == 0);
870 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100871
Philipp Maier7cedfd72017-12-04 16:49:12 +0100872 if (msg)
873 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200874
875 /* Retransmit... */
876 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100877 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200878 msg = mgcp_handle_message(cfg, inp);
879 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100880 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200881 printf("%s failed '%s'\n", t->name, (char *)msg->data);
882 OSMO_ASSERT(false);
883 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884 msgb_free(msg);
885 }
886
887 talloc_free(cfg);
888}
889
890static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
891{
892 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200893 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894 return 0;
895}
896
897static void test_rqnt_cb(void)
898{
899 struct mgcp_config *cfg;
900 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100901 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200902
903 cfg = mgcp_config_alloc();
904 cfg->rqnt_cb = rqnt_cb;
905
Philipp Maierfcd06552017-11-10 17:32:22 +0100906 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200907 mgcp_endpoints_allocate(&cfg->trunk);
908
909 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
910
Philipp Maierffd75e42017-11-22 11:44:50 +0100911 inp = create_msg(CRCX, NULL);
912 msg = mgcp_handle_message(cfg, inp);
913 OSMO_ASSERT(msg);
914 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
915 sizeof(conn_id)) == 0);
916 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200917 msgb_free(inp);
918
919 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100920 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200921 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200922 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200923 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
924 abort();
925 }
926
Philipp Maier87bd9be2017-08-22 16:35:41 +0200927 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200928 printf("FAILED: callback not called: %p\n", cfg->data);
929 abort();
930 }
931
932 msgb_free(msg);
933 msgb_free(inp);
934
Philipp Maierffd75e42017-11-22 11:44:50 +0100935 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200936 msgb_free(mgcp_handle_message(cfg, inp));
937 msgb_free(inp);
938 talloc_free(cfg);
939}
940
941struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200942 int cycles;
943 uint16_t base_seq;
944 uint16_t max_seq;
945 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200946
Philipp Maier87bd9be2017-08-22 16:35:41 +0200947 uint32_t expected;
948 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200949};
950
951static const struct pl_test pl_test_dat[] = {
952 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200953 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
954 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200955 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200956 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
957 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200958 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200959 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
960 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200961 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200962 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
963 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200964 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200965 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
966 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200967};
968
969static void test_packet_loss_calc(void)
970{
971 int i;
Philipp Maiercede2a42018-07-03 14:14:21 +0200972 struct mgcp_endpoint endp;
973 struct mgcp_trunk_config trunk;
974
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200975 printf("Testing packet loss calculation.\n");
976
Philipp Maiercede2a42018-07-03 14:14:21 +0200977 memset(&endp, 0, sizeof(endp));
978 memset(&trunk, 0, sizeof(trunk));
979
980 endp.type = &ep_typeset.rtp;
981 trunk.vty_number_endpoints = 1;
982 trunk.endpoints = &endp;
983 endp.tcfg = &trunk;
984 INIT_LLIST_HEAD(&endp.conns);
985
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200986 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
987 uint32_t expected;
988 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200989
Philipp Maiercede2a42018-07-03 14:14:21 +0200990 struct mgcp_conn_rtp *conn = NULL;
991 struct mgcp_conn *_conn = NULL;
992 struct mgcp_rtp_state *state;
993 struct rate_ctr *packets_rx;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200994
Philipp Maiercede2a42018-07-03 14:14:21 +0200995 _conn =
996 mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
997 "test-connection");
998 conn = mgcp_conn_get_rtp(&endp, _conn->id);
999 state = &conn->state;
1000 packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
1001
1002 state->stats.initialized = 1;
1003 state->stats.base_seq = pl_test_dat[i].base_seq;
1004 state->stats.max_seq = pl_test_dat[i].max_seq;
1005 state->stats.cycles = pl_test_dat[i].cycles;
1006
1007 packets_rx->current = pl_test_dat[i].packets;
1008 calc_loss(conn, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001009
Philipp Maier87bd9be2017-08-22 16:35:41 +02001010 if (loss != pl_test_dat[i].loss
1011 || expected != pl_test_dat[i].expected) {
1012 printf
1013 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
1014 i, loss, pl_test_dat[i].loss, expected,
1015 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001016 }
Philipp Maiercede2a42018-07-03 14:14:21 +02001017
1018 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001019 }
Philipp Maiercede2a42018-07-03 14:14:21 +02001020
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021}
1022
Philipp Maier87bd9be2017-08-22 16:35:41 +02001023int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
1024 uint32_t *pr, uint32_t *_or, int *loss,
1025 uint32_t *jitter)
1026{
1027 char *line, *save;
1028 int rc;
1029
1030 /* initialize with bad values */
1031 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
1032 *loss = INT_MAX;
1033
1034 line = strtok_r((char *)msg->l2h, "\r\n", &save);
1035 if (!line)
1036 return -1;
1037
1038 /* this can only parse the message that is created above... */
1039 for_each_non_empty_line(line, save) {
1040 switch (line[0]) {
1041 case 'P':
1042 rc = sscanf(line,
1043 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
1044 ps, os, pr, _or, loss, jitter);
1045 return rc == 6 ? 0 : -1;
1046 }
1047 }
1048
1049 return -1;
1050}
1051
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001052static void test_mgcp_stats(void)
1053{
1054 printf("Testing stat parsing\n");
1055
1056 uint32_t bps, bos, pr, _or, jitter;
1057 struct msgb *msg;
1058 int loss;
1059 int rc;
1060
Philipp Maierffd75e42017-11-22 11:44:50 +01001061 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001062 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1063 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001064 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
1065 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001066 printf("FAIL: Parsing failed1.\n");
1067 msgb_free(msg);
1068
Philipp Maier87bd9be2017-08-22 16:35:41 +02001069 msg =
1070 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +01001071 ("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 +02001072 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1073 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001074 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
1075 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001076 printf("FAIL: Parsing failed2.\n");
1077 msgb_free(msg);
1078}
1079
1080struct rtp_packet_info {
1081 float txtime;
1082 int len;
1083 char *data;
1084};
1085
1086struct rtp_packet_info test_rtp_packets1[] = {
1087 /* RTP: SeqNo=0, TS=0 */
1088 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001089 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001090 /* RTP: SeqNo=1, TS=160 */
1091 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001092 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001093 /* RTP: SeqNo=2, TS=320 */
1094 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001095 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001096 /* Repeat RTP timestamp: */
1097 /* RTP: SeqNo=3, TS=320 */
1098 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001099 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001100 /* RTP: SeqNo=4, TS=480 */
1101 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001102 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001103 /* RTP: SeqNo=5, TS=640 */
1104 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001105 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106 /* Double skip RTP timestamp (delta = 2*160): */
1107 /* RTP: SeqNo=6, TS=960 */
1108 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001109 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001110 /* RTP: SeqNo=7, TS=1120 */
1111 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001112 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001113 /* RTP: SeqNo=8, TS=1280 */
1114 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001115 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001116 /* Non 20ms RTP timestamp (delta = 120): */
1117 /* RTP: SeqNo=9, TS=1400 */
1118 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001119 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001120 /* RTP: SeqNo=10, TS=1560 */
1121 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001122 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001123 /* RTP: SeqNo=11, TS=1720 */
1124 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001125 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001126 /* SSRC changed to 0x10203040, RTP timestamp jump */
1127 /* RTP: SeqNo=12, TS=34688 */
1128 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001129 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001130 /* RTP: SeqNo=13, TS=34848 */
1131 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001132 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001133 /* RTP: SeqNo=14, TS=35008 */
1134 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001135 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001136 /* Non 20ms RTP timestamp (delta = 120): */
1137 /* RTP: SeqNo=15, TS=35128 */
1138 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001139 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001140 /* RTP: SeqNo=16, TS=35288 */
1141 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001142 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001143 /* RTP: SeqNo=17, TS=35448 */
1144 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001145 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001146 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1147 /* RTP: SeqNo=19, TS=35768 */
1148 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001149 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001150 /* RTP: SeqNo=20, TS=35928 */
1151 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001152 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001153 /* RTP: SeqNo=21, TS=36088 */
1154 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001155 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001156 /* Repeat last packet */
1157 /* RTP: SeqNo=21, TS=36088 */
1158 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001159 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001160 /* RTP: SeqNo=22, TS=36248 */
1161 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001162 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001163 /* RTP: SeqNo=23, TS=36408 */
1164 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001165 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001166 /* Don't increment SeqNo but increment timestamp by 160 */
1167 /* RTP: SeqNo=23, TS=36568 */
1168 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001169 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001170 /* RTP: SeqNo=24, TS=36728 */
1171 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001172 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001173 /* RTP: SeqNo=25, TS=36888 */
1174 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001175 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001176 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1177 * SeqNo jump */
1178 /* RTP: SeqNo=1000, TS=160000 */
1179 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001180 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001181 /* RTP: SeqNo=1001, TS=160160 */
1182 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001183 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001184 /* RTP: SeqNo=1002, TS=160320 */
1185 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001186 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001187};
1188
Philipp Maier87bd9be2017-08-22 16:35:41 +02001189void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1190 struct mgcp_rtp_state *state,
1191 struct mgcp_rtp_end *rtp_end,
1192 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001193
1194static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1195{
1196 int i;
1197
1198 struct mgcp_trunk_config trunk;
1199 struct mgcp_endpoint endp;
1200 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001201 struct mgcp_rtp_end *rtp;
1202 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001203 char buffer[4096];
1204 uint32_t last_ssrc = 0;
1205 uint32_t last_timestamp = 0;
1206 uint32_t last_seqno = 0;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001207 uint64_t last_in_ts_err_cnt = 0;
1208 uint64_t last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001209 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001210 struct mgcp_conn *_conn = NULL;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001211 struct rate_ctr test_ctr_in;
1212 struct rate_ctr test_ctr_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001213
1214 printf("Testing packet error detection%s%s.\n",
1215 patch_ssrc ? ", patch SSRC" : "",
1216 patch_ts ? ", patch timestamps" : "");
1217
1218 memset(&trunk, 0, sizeof(trunk));
1219 memset(&endp, 0, sizeof(endp));
1220 memset(&state, 0, sizeof(state));
1221
Philipp Maier9e1d1642018-05-09 16:26:34 +02001222 memset(&test_ctr_in, 0, sizeof(test_ctr_in));
1223 memset(&test_ctr_out, 0, sizeof(test_ctr_out));
1224 state.in_stream.err_ts_ctr = &test_ctr_in;
1225 state.out_stream.err_ts_ctr = &test_ctr_out;
1226
Philipp Maier87bd9be2017-08-22 16:35:41 +02001227 endp.type = &ep_typeset.rtp;
1228
Philipp Maierfcd06552017-11-10 17:32:22 +01001229 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001230 trunk.endpoints = &endp;
1231 trunk.force_constant_ssrc = patch_ssrc;
1232 trunk.force_aligned_timing = patch_ts;
1233
1234 endp.tcfg = &trunk;
1235
Philipp Maier87bd9be2017-08-22 16:35:41 +02001236 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001237 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1238 "test-connection");
1239 OSMO_ASSERT(_conn);
1240 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001241 OSMO_ASSERT(conn);
1242
1243 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001244
Philipp Maierbc0346e2018-06-07 09:52:16 +02001245 OSMO_ASSERT(mgcp_codec_add(conn, PTYPE_UNDEFINED, "AMR/8000/1") == 0);
1246 rtp->codec = &rtp->codecs[0];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001247
1248 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1249 struct rtp_packet_info *info = test_rtp_packets1 + i;
1250
1251 force_monotonic_time_us = round(1000000.0 * info->txtime);
1252
1253 OSMO_ASSERT(info->len <= sizeof(buffer));
1254 OSMO_ASSERT(info->len >= 0);
1255 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001256 mgcp_rtp_end_config(&endp, 1, rtp);
1257
1258 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1259 buffer, info->len);
1260
1261 if (state.out_stream.ssrc != last_ssrc) {
1262 printf("Output SSRC changed to %08x\n",
1263 state.out_stream.ssrc);
1264 last_ssrc = state.out_stream.ssrc;
1265 }
1266
1267 printf("In TS: %d, dTS: %d, Seq: %d\n",
1268 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001269 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001270
1271 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
Philipp Maier9e1d1642018-05-09 16:26:34 +02001272 "TS Err change: in +%u, out +%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001273 state.out_stream.last_timestamp - last_timestamp,
1274 state.out_stream.last_tsdelta,
1275 state.out_stream.last_seq - last_seqno,
Philipp Maier9e1d1642018-05-09 16:26:34 +02001276 (unsigned int) (state.in_stream.err_ts_ctr->current - last_in_ts_err_cnt),
1277 (unsigned int) (state.out_stream.err_ts_ctr->current - last_out_ts_err_cnt));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001278
1279 printf("Stats: Jitter = %u, Transit = %d\n",
Harald Welte49e3d5a2017-12-25 09:47:57 +01001280 calc_jitter(&state), state.stats.transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001281
Philipp Maier9e1d1642018-05-09 16:26:34 +02001282 last_in_ts_err_cnt = state.in_stream.err_ts_ctr->current;
1283 last_out_ts_err_cnt = state.out_stream.err_ts_ctr->current;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001284 last_timestamp = state.out_stream.last_timestamp;
1285 last_seqno = state.out_stream.last_seq;
1286 }
1287
1288 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001289 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001290}
1291
1292static void test_multilple_codec(void)
1293{
1294 struct mgcp_config *cfg;
1295 struct mgcp_endpoint *endp;
1296 struct msgb *inp, *resp;
1297 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001298 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001299 char conn_id[256];
Philipp Maiera74c0ea2018-08-02 11:59:09 +02001300 int i;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001301
1302 printf("Testing multiple payload types\n");
1303
1304 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001305 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001306 mgcp_endpoints_allocate(&cfg->trunk);
1307 cfg->policy_cb = mgcp_test_policy_cb;
1308 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1309
1310 /* Allocate endpoint 1@mgw with two codecs */
1311 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001312 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001313 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001314 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1315 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001316 msgb_free(inp);
1317 msgb_free(resp);
1318
1319 OSMO_ASSERT(last_endpoint == 1);
1320 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001321 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001322 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001323 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001324
1325 /* Allocate 2@mgw with three codecs, last one ignored */
1326 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001327 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001328 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001329 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1330 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001331 msgb_free(inp);
1332 msgb_free(resp);
1333
1334 OSMO_ASSERT(last_endpoint == 2);
1335 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001336 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001337 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001338 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001339
Philipp Maierbc0346e2018-06-07 09:52:16 +02001340 /* Allocate 3@mgw with no codecs, check for PT == 0 */
1341 /* Note: It usually makes no sense to leave the payload type list
1342 * out. However RFC 2327 does not clearly forbid this case and
1343 * it makes and since we already decided in OS#2658 that a missing
1344 * LCO should pick a sane default codec, it makes sense to expect
1345 * the same behaviour if SDP lacks proper payload type information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001346 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001347 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001348 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001349 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1350 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001351 msgb_free(inp);
1352 msgb_free(resp);
1353
1354 OSMO_ASSERT(last_endpoint == 3);
1355 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001356 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001357 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001358 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001359
1360 /* Allocate 4@mgw with a single codec */
1361 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001362 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001363 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001364 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1365 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001366 msgb_free(inp);
1367 msgb_free(resp);
1368
1369 OSMO_ASSERT(last_endpoint == 4);
1370 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001371 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001372 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001373 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001374
1375 /* Allocate 5@mgw at select GSM.. */
1376 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001377 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001378 talloc_free(cfg->trunk.audio_name);
1379 cfg->trunk.audio_name = "GSM/8000";
1380 cfg->trunk.no_audio_transcoding = 1;
1381 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001382 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1383 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001384 msgb_free(inp);
1385 msgb_free(resp);
1386
1387 OSMO_ASSERT(last_endpoint == 5);
1388 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001389 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001390 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001391 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001392
Philipp Maierffd75e42017-11-22 11:44:50 +01001393 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001394 last_endpoint = -1;
1395 resp = mgcp_handle_message(cfg, inp);
1396 msgb_free(inp);
1397 msgb_free(resp);
1398 OSMO_ASSERT(last_endpoint == 5);
1399 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001400 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001401 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001402 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001403 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001404 memset(&addr, 0, sizeof(addr));
1405 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001406 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001407
1408 /* Check what happens without that flag */
1409
Philipp Maier87bd9be2017-08-22 16:35:41 +02001410 /* Free the previous endpoint and the data and
1411 * check if the connection really vanished... */
Philipp Maier1355d7e2018-02-01 14:30:06 +01001412 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001413 talloc_free(endp->last_response);
1414 talloc_free(endp->last_trans);
1415 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001416 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001417 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001418
1419 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001420 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001421 cfg->trunk.no_audio_transcoding = 0;
1422 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001423 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1424 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001425 msgb_free(inp);
1426 msgb_free(resp);
1427
1428 OSMO_ASSERT(last_endpoint == 5);
1429 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001430 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001431 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001432 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001433
Philipp Maiera74c0ea2018-08-02 11:59:09 +02001434 for (i = 1; i < cfg->trunk.number_endpoints; i++)
1435 mgcp_endp_release(&cfg->trunk.endpoints[i]);
1436
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001437 talloc_free(cfg);
1438}
1439
1440static void test_no_cycle(void)
1441{
1442 struct mgcp_config *cfg;
1443 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001444 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001445 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001446
1447 printf("Testing no sequence flow on initial packet\n");
1448
1449 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001450 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001451 mgcp_endpoints_allocate(&cfg->trunk);
1452
1453 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001454
Philipp Maierffd75e42017-11-22 11:44:50 +01001455 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1456 "test-connection");
1457 OSMO_ASSERT(_conn);
1458 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001459 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001460
Harald Welte49e3d5a2017-12-25 09:47:57 +01001461 OSMO_ASSERT(conn->state.stats.initialized == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001462
1463 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001464 OSMO_ASSERT(conn->state.stats.initialized == 1);
1465 OSMO_ASSERT(conn->state.stats.cycles == 0);
1466 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001467
1468 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001469 OSMO_ASSERT(conn->state.stats.initialized == 1);
1470 OSMO_ASSERT(conn->state.stats.cycles == 0);
1471 OSMO_ASSERT(conn->state.stats.max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001472
1473 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001474 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001475 OSMO_ASSERT(conn->state.stats.initialized == 1);
1476 OSMO_ASSERT(conn->state.stats.cycles == 0);
1477 OSMO_ASSERT(conn->state.stats.max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001478
1479 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001480 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001481 OSMO_ASSERT(conn->state.stats.initialized == 1);
1482 OSMO_ASSERT(conn->state.stats.cycles == UINT16_MAX + 1);
1483 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001484
Philipp Maier1355d7e2018-02-01 14:30:06 +01001485 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001486 talloc_free(cfg);
1487}
1488
1489static void test_no_name(void)
1490{
1491 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001492 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001493
1494 printf("Testing no rtpmap name\n");
1495 cfg = mgcp_config_alloc();
1496
Philipp Maierfcd06552017-11-10 17:32:22 +01001497 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001498 cfg->trunk.audio_send_name = 0;
1499 mgcp_endpoints_allocate(&cfg->trunk);
1500
1501 cfg->policy_cb = mgcp_test_policy_cb;
1502
1503 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1504
Philipp Maierffd75e42017-11-22 11:44:50 +01001505 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001506 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001507
1508 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001509 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001510 (char *)msg->data);
1511 OSMO_ASSERT(false);
1512 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001513 msgb_free(inp);
1514 msgb_free(msg);
1515
Philipp Maier1355d7e2018-02-01 14:30:06 +01001516 mgcp_endp_release(&cfg->trunk.endpoints[1]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001517 talloc_free(cfg);
1518}
1519
1520static void test_osmux_cid(void)
1521{
1522 int id, i;
1523
1524 OSMO_ASSERT(osmux_used_cid() == 0);
1525 id = osmux_get_cid();
1526 OSMO_ASSERT(id == 0);
1527 OSMO_ASSERT(osmux_used_cid() == 1);
1528 osmux_put_cid(id);
1529 OSMO_ASSERT(osmux_used_cid() == 0);
1530
1531 for (i = 0; i < 256; ++i) {
1532 id = osmux_get_cid();
1533 OSMO_ASSERT(id == i);
1534 OSMO_ASSERT(osmux_used_cid() == i + 1);
1535 }
1536
1537 id = osmux_get_cid();
1538 OSMO_ASSERT(id == -1);
1539
1540 for (i = 0; i < 256; ++i)
1541 osmux_put_cid(i);
1542 OSMO_ASSERT(osmux_used_cid() == 0);
1543}
1544
1545static const struct log_info_cat log_categories[] = {
1546};
1547
1548const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001549 .cat = log_categories,
1550 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001551};
1552
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001553static void test_get_lco_identifier(void)
1554{
1555 char *test;
1556 printf("Testing get_lco_identifier()\n");
1557
1558 /* Normal case at the beginning */
1559 test = "p:10, a:PCMU";
1560 printf("%s -> %s\n", test, get_lco_identifier(test));
1561
1562 test = "p:10, a:PCMU";
1563 printf("%s -> %s\n", test, get_lco_identifier(test));
1564
1565 /* Begin parsing in the middle of the value part of
1566 * the previous LCO option value */
1567 test = "XXXX, p:10, a:PCMU";
1568 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1569
1570 test = "XXXX,p:10,a:PCMU";
1571 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1572
1573 test = "10,a:PCMU";
1574 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1575
1576 test = "10, a:PCMU";
1577 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1578
1579 test = "10,a: PCMU";
1580 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1581
1582 test = "10 ,a: PCMU";
1583 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1584
1585 /* Begin parsing right at the end of the previous LCO
1586 * option value */
1587 test = ", a:PCMU";
1588 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1589
1590 test = " a:PCMU";
1591 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1592
1593 /* Empty string, result should be (null) */
1594 test = "";
1595 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1596
1597 /* Missing colons, result should be (null) */
1598 test = "p10, aPCMU";
1599 printf("%s -> %s\n", test, get_lco_identifier(test));
1600
1601 /* Colon separated from the identifier, result should be (null) */
1602 test = "10,a :PCMU";
1603 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1604}
1605
1606static void test_check_local_cx_options(void *ctx)
1607{
1608 /* Legal cases */
1609 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1610 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU") == 0);
1611 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p:10, IN:10") == 0);
1612 OSMO_ASSERT(check_local_cx_options(ctx, "one:AAA, two:BB, tree:C") ==
1613 0);
1614 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1615 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:G726-32") == 0);
1616 OSMO_ASSERT(check_local_cx_options(ctx, "p:10-20, b:64") == 0);
1617 OSMO_ASSERT(check_local_cx_options(ctx, "b:32-64, e:off") == 0);
1618 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU;G726-32") == 0);
1619
1620 /* Illegal spaces before and after colon */
1621 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p :10") == -1);
1622 OSMO_ASSERT(check_local_cx_options(ctx, "a :PCMU, p:10") == -1);
1623 OSMO_ASSERT(check_local_cx_options(ctx, "p: 10, a:PCMU") == -1);
1624
1625 /* Check if multiple appearances of LCOs are rejected */
1626 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, p:10") == -1);
1627 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, a:PCMU, p:10") ==
1628 -1);
1629 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, p:10") == -1);
1630
1631 /* Check if empty LCO are rejected */
1632 OSMO_ASSERT(check_local_cx_options(ctx, "p: , 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: PCMU") == -1);
1635 OSMO_ASSERT(check_local_cx_options(ctx, "p:, a:PCMU") == -1);
1636 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:") == -1);
1637
1638 /* Garbeled beginning and ends */
1639 OSMO_ASSERT(check_local_cx_options(ctx, ":10, a:10") == -1);
1640 OSMO_ASSERT(check_local_cx_options(ctx, "10, 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 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU,") == -1);
1644 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, ") == -1);
1645
1646 /* Illegal strings */
1647 OSMO_ASSERT(check_local_cx_options(ctx, " ") == -1);
1648 OSMO_ASSERT(check_local_cx_options(ctx, "") == -1);
1649 OSMO_ASSERT(check_local_cx_options(ctx, "AAA") == -1);
1650 OSMO_ASSERT(check_local_cx_options(ctx, ":,") == -1);
1651 OSMO_ASSERT(check_local_cx_options(ctx, ",:") == -1);
1652 OSMO_ASSERT(check_local_cx_options(ctx, ",,,") == -1);
1653}
1654
Philipp Maier6931f9a2018-07-26 09:29:31 +02001655static void test_mgcp_codec_pt_translate_pars(struct mgcp_rtp_codec *c)
1656{
1657 c->rate = 8000;
1658 c->channels = 1;
1659 c->frame_duration_num = 23;
1660 c->frame_duration_den = 42;
1661}
1662
1663static void test_mgcp_codec_pt_translate(void)
1664{
1665 struct mgcp_conn_rtp conn_src;
1666 struct mgcp_conn_rtp conn_dst;
1667 int pt_dst;
1668
1669 /* Setup a realistic set of codec configurations on both
1670 * ends. AMR and HR will use different payload types. PCMU
1671 * must use 0 on both ends since this is not a dynamic payload
1672 * type */
1673 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[0]);
1674 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[0]);
1675 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[1]);
1676 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[1]);
1677 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[2]);
1678 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[2]);
1679 conn_src.end.codecs[0].payload_type = 112;
1680 conn_dst.end.codecs[0].payload_type = 96;
1681 conn_src.end.codecs[1].payload_type = 0;
1682 conn_dst.end.codecs[1].payload_type = 0;
1683 conn_src.end.codecs[2].payload_type = 111;
1684 conn_dst.end.codecs[2].payload_type = 97;
1685 conn_src.end.codecs[0].audio_name = "AMR/8000/1";
1686 conn_dst.end.codecs[0].audio_name = "AMR/8000/1";
1687 conn_src.end.codecs[1].audio_name = "PCMU/8000/1";
1688 conn_dst.end.codecs[1].audio_name = "PCMU/8000/1";
1689 conn_src.end.codecs[2].audio_name = "GSM-HR-08/8000/1";
1690 conn_dst.end.codecs[2].audio_name = "GSM-HR-08/8000/1";
1691 conn_src.end.codecs[0].subtype_name = "AMR";
1692 conn_dst.end.codecs[0].subtype_name = "AMR";
1693 conn_src.end.codecs[1].subtype_name = "PCMU";
1694 conn_dst.end.codecs[1].subtype_name = "PCMU";
1695 conn_src.end.codecs[2].subtype_name = "GSM-HR-08";
1696 conn_dst.end.codecs[2].subtype_name = "GSM-HR-08";
1697 conn_src.end.codecs_assigned = 3;
1698 conn_dst.end.codecs_assigned = 3;
1699
1700 /* We expect the function to find the PT we must use when we send the
1701 * packet out to the destination. All we know is the context for both
1702 * connections and the payload type from the source packet */
1703 pt_dst =
1704 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1705 conn_src.end.codecs[0].payload_type);
1706 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[0].payload_type);
1707 pt_dst =
1708 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1709 conn_src.end.codecs[1].payload_type);
1710 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[1].payload_type);
1711 pt_dst =
1712 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1713 conn_src.end.codecs[2].payload_type);
1714 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[2].payload_type);
1715
1716 /* Try some constellations that must fail */
1717 pt_dst = mgcp_codec_pt_translate(&conn_src, &conn_dst, 123);
1718 OSMO_ASSERT(pt_dst == -EINVAL);
1719 conn_src.end.codecs_assigned = 0;
1720 conn_dst.end.codecs_assigned = 3;
1721 pt_dst =
1722 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1723 conn_src.end.codecs[0].payload_type);
1724 OSMO_ASSERT(pt_dst == -EINVAL);
1725 pt_dst =
1726 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1727 conn_src.end.codecs[1].payload_type);
1728 OSMO_ASSERT(pt_dst == -EINVAL);
1729 pt_dst =
1730 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1731 conn_src.end.codecs[2].payload_type);
1732 OSMO_ASSERT(pt_dst == -EINVAL);
1733 conn_src.end.codecs_assigned = 3;
1734 conn_dst.end.codecs_assigned = 0;
1735 pt_dst =
1736 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1737 conn_src.end.codecs[0].payload_type);
1738 OSMO_ASSERT(pt_dst == -EINVAL);
1739 pt_dst =
1740 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1741 conn_src.end.codecs[1].payload_type);
1742 OSMO_ASSERT(pt_dst == -EINVAL);
1743 pt_dst =
1744 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1745 conn_src.end.codecs[2].payload_type);
1746 OSMO_ASSERT(pt_dst == -EINVAL);
1747}
1748
Neels Hofmeyr65317262018-09-03 22:11:05 +02001749void test_conn_id_matching()
1750{
1751 struct mgcp_endpoint endp = {};
1752 struct mgcp_conn *conn;
1753 struct mgcp_conn *conn_match;
1754 int i;
1755 const char *conn_id_generated = "000023AB";
1756 const char *conn_id_request[] = {
Neels Hofmeyra77eade2018-08-29 02:30:39 +02001757 "23AB",
1758 "0023AB",
Neels Hofmeyr65317262018-09-03 22:11:05 +02001759 "000023AB",
Neels Hofmeyra77eade2018-08-29 02:30:39 +02001760 "00000023AB",
1761 "23ab",
1762 "0023ab",
Neels Hofmeyr65317262018-09-03 22:11:05 +02001763 "000023ab",
Neels Hofmeyra77eade2018-08-29 02:30:39 +02001764 "00000023ab",
Neels Hofmeyr65317262018-09-03 22:11:05 +02001765 };
1766
1767 printf("\nTesting %s\n", __func__);
1768
1769 INIT_LLIST_HEAD(&endp.conns);
1770
1771 conn = talloc_zero(NULL, struct mgcp_conn);
1772 OSMO_ASSERT(conn);
1773 osmo_strlcpy(conn->id, conn_id_generated, sizeof(conn->id));
1774 llist_add(&conn->entry, &endp.conns);
1775
1776 for (i = 0; i < ARRAY_SIZE(conn_id_request); i++) {
1777 const char *needle = conn_id_request[i];
1778 printf("needle='%s' ", needle);
1779 conn_match = mgcp_conn_get(&endp, needle);
1780 OSMO_ASSERT(conn_match);
1781 printf("found '%s'\n", conn_match->id);
1782 OSMO_ASSERT(conn_match == conn);
1783 }
1784
1785 llist_del(&conn->entry);
1786 talloc_free(conn);
1787}
1788
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001789int main(int argc, char **argv)
1790{
Neels Hofmeyr60f8e312018-03-30 23:01:07 +02001791 void *ctx = talloc_named_const(NULL, 0, "mgcp_test");
1792 void *msgb_ctx = msgb_talloc_ctx_init(ctx, 0);
1793 osmo_init_logging2(ctx, &log_info);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001794
1795 test_strline();
1796 test_values();
1797 test_messages();
1798 test_retransmission();
1799 test_packet_loss_calc();
1800 test_rqnt_cb();
1801 test_mgcp_stats();
1802 test_packet_error_detection(1, 0);
1803 test_packet_error_detection(0, 0);
1804 test_packet_error_detection(0, 1);
1805 test_packet_error_detection(1, 1);
1806 test_multilple_codec();
1807 test_no_cycle();
1808 test_no_name();
1809 test_osmux_cid();
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001810 test_get_lco_identifier();
1811 test_check_local_cx_options(ctx);
Philipp Maier6931f9a2018-07-26 09:29:31 +02001812 test_mgcp_codec_pt_translate();
Neels Hofmeyr65317262018-09-03 22:11:05 +02001813 test_conn_id_matching();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001814
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001815 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1816 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1817 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001818 printf("Done\n");
1819 return EXIT_SUCCESS;
1820}