blob: 99ddd7186970adc074e325d3abba594dffa3c114 [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
Philipp Maierffd75e42017-11-22 11:44:50 +0100610/* Extract a connection ID from a response (CRCX) */
611static int get_conn_id_from_response(uint8_t *resp, char *conn_id,
612 unsigned int conn_id_len)
613{
614 char *conn_id_ptr;
615 int i;
Philipp Maier55295f72018-01-15 14:00:28 +0100616 bool got_conn_id = false;
Philipp Maierffd75e42017-11-22 11:44:50 +0100617
Philipp Maier55295f72018-01-15 14:00:28 +0100618 /* First try to get the conn_id from the I: parameter */
Philipp Maierffd75e42017-11-22 11:44:50 +0100619 conn_id_ptr = strstr((char *)resp, "I: ");
Philipp Maier55295f72018-01-15 14:00:28 +0100620 if (conn_id_ptr) {
621 memset(conn_id, 0, conn_id_len);
622 memcpy(conn_id, conn_id_ptr + 3, 32);
623 got_conn_id = true;
624 } else {
625 /* Alternatively try to extract the conn_id from the o=- SDP
626 * parameter */
627 conn_id_ptr = strstr((char *)resp, "o=- ");
628 if(conn_id_ptr) {
629 memset(conn_id, 0, conn_id_len);
630 memcpy(conn_id, conn_id_ptr + 4, 32);
631 got_conn_id = true;
632 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100633 }
634
Philipp Maier55295f72018-01-15 14:00:28 +0100635 if (got_conn_id) {
636 for (i = 0; i < conn_id_len; i++) {
Neels Hofmeyrb861db92018-08-28 16:19:25 +0200637 if (!isxdigit(conn_id[i])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100638 conn_id[i] = '\0';
Neels Hofmeyrb861db92018-08-28 16:19:25 +0200639 break;
640 }
Philipp Maier55295f72018-01-15 14:00:28 +0100641 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100642
Philipp Maier55295f72018-01-15 14:00:28 +0100643 /* A valid conn_id must at least contain one digit, and must
644 * not exceed a length of 32 digits */
645 OSMO_ASSERT(strlen(conn_id) <= 32);
646 OSMO_ASSERT(strlen(conn_id) > 0);
647
648 return 0;
649 }
650 return -EINVAL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100651}
652
653/* Check response, automatically patch connection ID if needed */
654static int check_response(uint8_t *resp, const char *exp_resp)
655{
656 char exp_resp_patched[4096];
657 const char *exp_resp_ptr;
658 char conn_id[256];
659
660 printf("checking response:\n");
661
662 /* If the expected response is intened to be patched
663 * (%s placeholder inside) we will patch it with the
664 * connection identifier we just received from the
665 * real response. This is necessary because the CI
666 * is generated by the mgcp code on CRCX and we can
667 * not know it in advance */
668 if (strstr(exp_resp, "%s")) {
669 if (get_conn_id_from_response(resp, conn_id, sizeof(conn_id)) ==
670 0) {
671 sprintf(exp_resp_patched, exp_resp, conn_id, conn_id);
672 exp_resp_ptr = exp_resp_patched;
673 printf
674 ("using message with patched conn_id for comparison\n");
675 } else {
676 printf
677 ("patching conn_id failed, using message as statically defined for comparison\n");
678 exp_resp_ptr = exp_resp;
679 }
680 } else {
681 printf("using message as statically defined for comparison\n");
682 exp_resp_ptr = exp_resp;
683 }
684
685 if (strcmp((char *)resp, exp_resp_ptr) != 0) {
686 printf("Unexpected response, please check!\n");
687 printf
688 ("Got:\n---------8<---------\n%s\n---------8<---------\n\n",
689 resp);
690 printf
691 ("Expected:\n---------8<---------\n%s\n---------8<---------\n",
692 exp_resp_ptr);
693 return -EINVAL;
694 }
695
696 printf("Response matches our expectations.\n");
697 return 0;
698}
699
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200700static void test_messages(void)
701{
702 struct mgcp_config *cfg;
703 struct mgcp_endpoint *endp;
704 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200705 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100706 char last_conn_id[256];
Philipp Maier7df419b2017-12-04 17:11:42 +0100707 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200708
709 cfg = mgcp_config_alloc();
710
Philipp Maierfcd06552017-11-10 17:32:22 +0100711 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200712 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200713 cfg->policy_cb = mgcp_test_policy_cb;
714
Philipp Maierffd75e42017-11-22 11:44:50 +0100715 memset(last_conn_id, 0, sizeof(last_conn_id));
716
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200717 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
718
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200719 for (i = 0; i < ARRAY_SIZE(tests); i++) {
720 const struct mgcp_test *t = &tests[i];
721 struct msgb *inp;
722 struct msgb *msg;
723
Philipp Maierffd75e42017-11-22 11:44:50 +0100724 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200725 printf("Testing %s\n", t->name);
726
727 last_endpoint = -1;
728 dummy_packets = 0;
729
Philipp Maier87bd9be2017-08-22 16:35:41 +0200730 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
731 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200732
Philipp Maierffd75e42017-11-22 11:44:50 +0100733 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200734 msg = mgcp_handle_message(cfg, inp);
735 msgb_free(inp);
736 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200737 if (msg) {
738 printf("%s failed '%s'\n", t->name,
739 (char *)msg->data);
740 OSMO_ASSERT(false);
741 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100742 } else if (check_response(msg->data, t->exp_resp) != 0) {
743 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200744 OSMO_ASSERT(false);
745 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100746
Philipp Maier7df419b2017-12-04 17:11:42 +0100747 if (msg) {
748 rc = get_conn_id_from_response(msg->data, last_conn_id,
749 sizeof(last_conn_id));
Neels Hofmeyr08e07042018-08-28 16:22:14 +0200750 if (rc == 0)
Philipp Maier7df419b2017-12-04 17:11:42 +0100751 printf("(response contains a connection id)\n");
752 else
753 printf("(response does not contain a connection id)\n");
754 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100755
Philipp Maiera330b862017-12-04 17:16:16 +0100756 if (msg)
757 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200758
759 if (dummy_packets)
760 printf("Dummy packets: %d\n", dummy_packets);
761
762 if (last_endpoint != -1) {
763 endp = &cfg->trunk.endpoints[last_endpoint];
764
Philipp Maier01d24a32017-11-21 17:26:09 +0100765 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200766 if (conn) {
767 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200768
Philipp Maier87bd9be2017-08-22 16:35:41 +0200769 if (conn->end.packet_duration_ms != -1)
770 printf("Detected packet duration: %d\n",
771 conn->end.packet_duration_ms);
772 else
773 printf("Packet duration not set\n");
774 if (endp->local_options.pkt_period_min ||
775 endp->local_options.pkt_period_max)
776 printf
777 ("Requested packetetization period: "
778 "%d-%d\n",
779 endp->local_options.pkt_period_min,
780 endp->
781 local_options.pkt_period_max);
782 else
783 printf
784 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200785
Philipp Maier87bd9be2017-08-22 16:35:41 +0200786 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
787 printf("Connection mode: %d:%s%s%s%s\n",
788 conn->conn->mode,
789 !conn->conn->mode ? " NONE" : "",
790 conn->conn->mode & MGCP_CONN_SEND_ONLY
791 ? " SEND" : "",
792 conn->conn->mode & MGCP_CONN_RECV_ONLY
793 ? " RECV" : "",
794 conn->conn->mode & MGCP_CONN_LOOPBACK
795 & ~MGCP_CONN_RECV_SEND
796 ? " LOOP" : "");
797 fprintf(stderr,
798 "RTP output %sabled, NET output %sabled\n",
799 conn->end.output_enabled
800 ? "en" : "dis",
801 conn->end.output_enabled
802 ? "en" : "dis");
803 } else
804 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200805
Philipp Maier87bd9be2017-08-22 16:35:41 +0200806 OSMO_ASSERT(conn->end.output_enabled
807 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
808
809 conn->conn->mode |= CONN_UNMODIFIED;
810
811 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200812 endp->local_options.pkt_period_min = 0;
813 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200814 }
815
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200816 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100817 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200818 OSMO_ASSERT(last_endpoint != -1);
819 endp = &cfg->trunk.endpoints[last_endpoint];
820
821 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200822 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200823 last_endpoint,
Philipp Maierbc0346e2018-06-07 09:52:16 +0200824 conn->end.codec->payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825
Philipp Maier87bd9be2017-08-22 16:35:41 +0200826 if (t->ptype != PTYPE_IGNORE)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200827 OSMO_ASSERT(conn->end.codec->payload_type ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200829
830 /* Reset them again for next test */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200831 conn->end.codec->payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832 }
833 }
834
835 talloc_free(cfg);
836}
837
838static void test_retransmission(void)
839{
840 struct mgcp_config *cfg;
841 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100842 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100843 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200844
845 cfg = mgcp_config_alloc();
846
Philipp Maierfcd06552017-11-10 17:32:22 +0100847 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200848 mgcp_endpoints_allocate(&cfg->trunk);
849
Philipp Maierffd75e42017-11-22 11:44:50 +0100850 memset(last_conn_id, 0, sizeof(last_conn_id));
851
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200852 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
853
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200854 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
855 const struct mgcp_test *t = &retransmit[i];
856 struct msgb *inp;
857 struct msgb *msg;
858
Philipp Maierffd75e42017-11-22 11:44:50 +0100859 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200860 printf("Testing %s\n", t->name);
861
Philipp Maierffd75e42017-11-22 11:44:50 +0100862 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200863 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200864
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200865 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100866 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200867 printf("%s failed '%s'\n", t->name, (char *)msg->data);
868 OSMO_ASSERT(false);
869 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100870
Philipp Maier23b8e292017-12-04 16:48:45 +0100871 if (msg && strcmp(t->name, "CRCX") == 0) {
872 rc = get_conn_id_from_response(msg->data, last_conn_id,
873 sizeof(last_conn_id));
874 OSMO_ASSERT(rc == 0);
875 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100876
Philipp Maier7cedfd72017-12-04 16:49:12 +0100877 if (msg)
878 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200879
880 /* Retransmit... */
881 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100882 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200883 msg = mgcp_handle_message(cfg, inp);
884 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100885 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 printf("%s failed '%s'\n", t->name, (char *)msg->data);
887 OSMO_ASSERT(false);
888 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200889 msgb_free(msg);
890 }
891
892 talloc_free(cfg);
893}
894
895static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
896{
897 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200898 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200899 return 0;
900}
901
902static void test_rqnt_cb(void)
903{
904 struct mgcp_config *cfg;
905 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100906 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200907
908 cfg = mgcp_config_alloc();
909 cfg->rqnt_cb = rqnt_cb;
910
Philipp Maierfcd06552017-11-10 17:32:22 +0100911 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200912 mgcp_endpoints_allocate(&cfg->trunk);
913
914 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
915
Philipp Maierffd75e42017-11-22 11:44:50 +0100916 inp = create_msg(CRCX, NULL);
917 msg = mgcp_handle_message(cfg, inp);
918 OSMO_ASSERT(msg);
919 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
920 sizeof(conn_id)) == 0);
921 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200922 msgb_free(inp);
923
924 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100925 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200926 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200927 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200928 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
929 abort();
930 }
931
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200933 printf("FAILED: callback not called: %p\n", cfg->data);
934 abort();
935 }
936
937 msgb_free(msg);
938 msgb_free(inp);
939
Philipp Maierffd75e42017-11-22 11:44:50 +0100940 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200941 msgb_free(mgcp_handle_message(cfg, inp));
942 msgb_free(inp);
943 talloc_free(cfg);
944}
945
946struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200947 int cycles;
948 uint16_t base_seq;
949 uint16_t max_seq;
950 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200951
Philipp Maier87bd9be2017-08-22 16:35:41 +0200952 uint32_t expected;
953 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200954};
955
956static const struct pl_test pl_test_dat[] = {
957 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200958 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
959 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200960 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200961 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
962 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200963 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200964 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
965 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200966 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200967 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
968 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200969 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200970 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
971 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200972};
973
974static void test_packet_loss_calc(void)
975{
976 int i;
Philipp Maiercede2a42018-07-03 14:14:21 +0200977 struct mgcp_endpoint endp;
978 struct mgcp_trunk_config trunk;
979
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200980 printf("Testing packet loss calculation.\n");
981
Philipp Maiercede2a42018-07-03 14:14:21 +0200982 memset(&endp, 0, sizeof(endp));
983 memset(&trunk, 0, sizeof(trunk));
984
985 endp.type = &ep_typeset.rtp;
986 trunk.vty_number_endpoints = 1;
987 trunk.endpoints = &endp;
988 endp.tcfg = &trunk;
989 INIT_LLIST_HEAD(&endp.conns);
990
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200991 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
992 uint32_t expected;
993 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200994
Philipp Maiercede2a42018-07-03 14:14:21 +0200995 struct mgcp_conn_rtp *conn = NULL;
996 struct mgcp_conn *_conn = NULL;
997 struct mgcp_rtp_state *state;
998 struct rate_ctr *packets_rx;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200999
Philipp Maiercede2a42018-07-03 14:14:21 +02001000 _conn =
1001 mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1002 "test-connection");
1003 conn = mgcp_conn_get_rtp(&endp, _conn->id);
1004 state = &conn->state;
1005 packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
1006
1007 state->stats.initialized = 1;
1008 state->stats.base_seq = pl_test_dat[i].base_seq;
1009 state->stats.max_seq = pl_test_dat[i].max_seq;
1010 state->stats.cycles = pl_test_dat[i].cycles;
1011
1012 packets_rx->current = pl_test_dat[i].packets;
1013 calc_loss(conn, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001014
Philipp Maier87bd9be2017-08-22 16:35:41 +02001015 if (loss != pl_test_dat[i].loss
1016 || expected != pl_test_dat[i].expected) {
1017 printf
1018 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
1019 i, loss, pl_test_dat[i].loss, expected,
1020 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021 }
Philipp Maiercede2a42018-07-03 14:14:21 +02001022
1023 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001024 }
Philipp Maiercede2a42018-07-03 14:14:21 +02001025
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001026}
1027
Philipp Maier87bd9be2017-08-22 16:35:41 +02001028int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
1029 uint32_t *pr, uint32_t *_or, int *loss,
1030 uint32_t *jitter)
1031{
1032 char *line, *save;
1033 int rc;
1034
1035 /* initialize with bad values */
1036 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
1037 *loss = INT_MAX;
1038
1039 line = strtok_r((char *)msg->l2h, "\r\n", &save);
1040 if (!line)
1041 return -1;
1042
1043 /* this can only parse the message that is created above... */
1044 for_each_non_empty_line(line, save) {
1045 switch (line[0]) {
1046 case 'P':
1047 rc = sscanf(line,
1048 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
1049 ps, os, pr, _or, loss, jitter);
1050 return rc == 6 ? 0 : -1;
1051 }
1052 }
1053
1054 return -1;
1055}
1056
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001057static void test_mgcp_stats(void)
1058{
1059 printf("Testing stat parsing\n");
1060
1061 uint32_t bps, bos, pr, _or, jitter;
1062 struct msgb *msg;
1063 int loss;
1064 int rc;
1065
Philipp Maierffd75e42017-11-22 11:44:50 +01001066 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001067 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1068 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001069 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
1070 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001071 printf("FAIL: Parsing failed1.\n");
1072 msgb_free(msg);
1073
Philipp Maier87bd9be2017-08-22 16:35:41 +02001074 msg =
1075 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +01001076 ("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 +02001077 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1078 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001079 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
1080 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001081 printf("FAIL: Parsing failed2.\n");
1082 msgb_free(msg);
1083}
1084
1085struct rtp_packet_info {
1086 float txtime;
1087 int len;
1088 char *data;
1089};
1090
1091struct rtp_packet_info test_rtp_packets1[] = {
1092 /* RTP: SeqNo=0, TS=0 */
1093 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001094 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001095 /* RTP: SeqNo=1, TS=160 */
1096 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\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=2, TS=320 */
1099 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\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 /* Repeat RTP timestamp: */
1102 /* RTP: SeqNo=3, TS=320 */
1103 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001104 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001105 /* RTP: SeqNo=4, TS=480 */
1106 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\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=5, TS=640 */
1109 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\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 /* Double skip RTP timestamp (delta = 2*160): */
1112 /* RTP: SeqNo=6, TS=960 */
1113 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001114 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001115 /* RTP: SeqNo=7, TS=1120 */
1116 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\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=8, TS=1280 */
1119 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\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 /* Non 20ms RTP timestamp (delta = 120): */
1122 /* RTP: SeqNo=9, TS=1400 */
1123 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001124 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001125 /* RTP: SeqNo=10, TS=1560 */
1126 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
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=11, TS=1720 */
1129 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001130 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001131 /* SSRC changed to 0x10203040, RTP timestamp jump */
1132 /* RTP: SeqNo=12, TS=34688 */
1133 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001135 /* RTP: SeqNo=13, TS=34848 */
1136 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\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=14, TS=35008 */
1139 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\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 /* Non 20ms RTP timestamp (delta = 120): */
1142 /* RTP: SeqNo=15, TS=35128 */
1143 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001144 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001145 /* RTP: SeqNo=16, TS=35288 */
1146 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\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=17, TS=35448 */
1149 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001150 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001151 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1152 /* RTP: SeqNo=19, TS=35768 */
1153 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001154 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001155 /* RTP: SeqNo=20, TS=35928 */
1156 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\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=21, TS=36088 */
1159 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\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 /* Repeat last packet */
1162 /* RTP: SeqNo=21, TS=36088 */
1163 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001164 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001165 /* RTP: SeqNo=22, TS=36248 */
1166 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\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=23, TS=36408 */
1169 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\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 /* Don't increment SeqNo but increment timestamp by 160 */
1172 /* RTP: SeqNo=23, TS=36568 */
1173 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001174 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001175 /* RTP: SeqNo=24, TS=36728 */
1176 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001177 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001178 /* RTP: SeqNo=25, TS=36888 */
1179 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001180 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001181 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1182 * SeqNo jump */
1183 /* RTP: SeqNo=1000, TS=160000 */
1184 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001185 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001186 /* RTP: SeqNo=1001, TS=160160 */
1187 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001188 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001189 /* RTP: SeqNo=1002, TS=160320 */
1190 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001191 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001192};
1193
Philipp Maier87bd9be2017-08-22 16:35:41 +02001194void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1195 struct mgcp_rtp_state *state,
1196 struct mgcp_rtp_end *rtp_end,
1197 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001198
1199static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1200{
1201 int i;
1202
1203 struct mgcp_trunk_config trunk;
1204 struct mgcp_endpoint endp;
1205 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001206 struct mgcp_rtp_end *rtp;
1207 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001208 char buffer[4096];
1209 uint32_t last_ssrc = 0;
1210 uint32_t last_timestamp = 0;
1211 uint32_t last_seqno = 0;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001212 uint64_t last_in_ts_err_cnt = 0;
1213 uint64_t last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001214 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001215 struct mgcp_conn *_conn = NULL;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001216 struct rate_ctr test_ctr_in;
1217 struct rate_ctr test_ctr_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001218
1219 printf("Testing packet error detection%s%s.\n",
1220 patch_ssrc ? ", patch SSRC" : "",
1221 patch_ts ? ", patch timestamps" : "");
1222
1223 memset(&trunk, 0, sizeof(trunk));
1224 memset(&endp, 0, sizeof(endp));
1225 memset(&state, 0, sizeof(state));
1226
Philipp Maier9e1d1642018-05-09 16:26:34 +02001227 memset(&test_ctr_in, 0, sizeof(test_ctr_in));
1228 memset(&test_ctr_out, 0, sizeof(test_ctr_out));
1229 state.in_stream.err_ts_ctr = &test_ctr_in;
1230 state.out_stream.err_ts_ctr = &test_ctr_out;
1231
Philipp Maier87bd9be2017-08-22 16:35:41 +02001232 endp.type = &ep_typeset.rtp;
1233
Philipp Maierfcd06552017-11-10 17:32:22 +01001234 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001235 trunk.endpoints = &endp;
1236 trunk.force_constant_ssrc = patch_ssrc;
1237 trunk.force_aligned_timing = patch_ts;
1238
1239 endp.tcfg = &trunk;
1240
Philipp Maier87bd9be2017-08-22 16:35:41 +02001241 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001242 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1243 "test-connection");
1244 OSMO_ASSERT(_conn);
1245 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001246 OSMO_ASSERT(conn);
1247
1248 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001249
Philipp Maierbc0346e2018-06-07 09:52:16 +02001250 OSMO_ASSERT(mgcp_codec_add(conn, PTYPE_UNDEFINED, "AMR/8000/1") == 0);
1251 rtp->codec = &rtp->codecs[0];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001252
1253 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1254 struct rtp_packet_info *info = test_rtp_packets1 + i;
1255
1256 force_monotonic_time_us = round(1000000.0 * info->txtime);
1257
1258 OSMO_ASSERT(info->len <= sizeof(buffer));
1259 OSMO_ASSERT(info->len >= 0);
1260 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001261 mgcp_rtp_end_config(&endp, 1, rtp);
1262
1263 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1264 buffer, info->len);
1265
1266 if (state.out_stream.ssrc != last_ssrc) {
1267 printf("Output SSRC changed to %08x\n",
1268 state.out_stream.ssrc);
1269 last_ssrc = state.out_stream.ssrc;
1270 }
1271
1272 printf("In TS: %d, dTS: %d, Seq: %d\n",
1273 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001274 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001275
1276 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
Philipp Maier9e1d1642018-05-09 16:26:34 +02001277 "TS Err change: in +%u, out +%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001278 state.out_stream.last_timestamp - last_timestamp,
1279 state.out_stream.last_tsdelta,
1280 state.out_stream.last_seq - last_seqno,
Philipp Maier9e1d1642018-05-09 16:26:34 +02001281 (unsigned int) (state.in_stream.err_ts_ctr->current - last_in_ts_err_cnt),
1282 (unsigned int) (state.out_stream.err_ts_ctr->current - last_out_ts_err_cnt));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001283
1284 printf("Stats: Jitter = %u, Transit = %d\n",
Harald Welte49e3d5a2017-12-25 09:47:57 +01001285 calc_jitter(&state), state.stats.transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001286
Philipp Maier9e1d1642018-05-09 16:26:34 +02001287 last_in_ts_err_cnt = state.in_stream.err_ts_ctr->current;
1288 last_out_ts_err_cnt = state.out_stream.err_ts_ctr->current;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001289 last_timestamp = state.out_stream.last_timestamp;
1290 last_seqno = state.out_stream.last_seq;
1291 }
1292
1293 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001294 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001295}
1296
1297static void test_multilple_codec(void)
1298{
1299 struct mgcp_config *cfg;
1300 struct mgcp_endpoint *endp;
1301 struct msgb *inp, *resp;
1302 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001303 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001304 char conn_id[256];
Philipp Maiera74c0ea2018-08-02 11:59:09 +02001305 int i;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001306
1307 printf("Testing multiple payload types\n");
1308
1309 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001310 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001311 mgcp_endpoints_allocate(&cfg->trunk);
1312 cfg->policy_cb = mgcp_test_policy_cb;
1313 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1314
1315 /* Allocate endpoint 1@mgw with two codecs */
1316 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001317 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001318 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001319 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1320 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001321 msgb_free(inp);
1322 msgb_free(resp);
1323
1324 OSMO_ASSERT(last_endpoint == 1);
1325 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001326 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001327 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001328 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001329
1330 /* Allocate 2@mgw with three codecs, last one ignored */
1331 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001332 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001333 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001334 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1335 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001336 msgb_free(inp);
1337 msgb_free(resp);
1338
1339 OSMO_ASSERT(last_endpoint == 2);
1340 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001341 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001342 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001343 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001344
Philipp Maierbc0346e2018-06-07 09:52:16 +02001345 /* Allocate 3@mgw with no codecs, check for PT == 0 */
1346 /* Note: It usually makes no sense to leave the payload type list
1347 * out. However RFC 2327 does not clearly forbid this case and
1348 * it makes and since we already decided in OS#2658 that a missing
1349 * LCO should pick a sane default codec, it makes sense to expect
1350 * the same behaviour if SDP lacks proper payload type information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001351 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001352 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001353 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001354 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1355 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001356 msgb_free(inp);
1357 msgb_free(resp);
1358
1359 OSMO_ASSERT(last_endpoint == 3);
1360 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001361 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001362 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001363 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001364
1365 /* Allocate 4@mgw with a single codec */
1366 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001367 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001368 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001369 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1370 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001371 msgb_free(inp);
1372 msgb_free(resp);
1373
1374 OSMO_ASSERT(last_endpoint == 4);
1375 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001376 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001377 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001378 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001379
1380 /* Allocate 5@mgw at select GSM.. */
1381 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001382 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001383 talloc_free(cfg->trunk.audio_name);
1384 cfg->trunk.audio_name = "GSM/8000";
1385 cfg->trunk.no_audio_transcoding = 1;
1386 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001387 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1388 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001389 msgb_free(inp);
1390 msgb_free(resp);
1391
1392 OSMO_ASSERT(last_endpoint == 5);
1393 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001394 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001395 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001396 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001397
Philipp Maierffd75e42017-11-22 11:44:50 +01001398 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001399 last_endpoint = -1;
1400 resp = mgcp_handle_message(cfg, inp);
1401 msgb_free(inp);
1402 msgb_free(resp);
1403 OSMO_ASSERT(last_endpoint == 5);
1404 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001405 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001406 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001407 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001408 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001409 memset(&addr, 0, sizeof(addr));
1410 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001411 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001412
1413 /* Check what happens without that flag */
1414
Philipp Maier87bd9be2017-08-22 16:35:41 +02001415 /* Free the previous endpoint and the data and
1416 * check if the connection really vanished... */
Philipp Maier1355d7e2018-02-01 14:30:06 +01001417 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001418 talloc_free(endp->last_response);
1419 talloc_free(endp->last_trans);
1420 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001421 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001422 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001423
1424 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001425 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001426 cfg->trunk.no_audio_transcoding = 0;
1427 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001428 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1429 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001430 msgb_free(inp);
1431 msgb_free(resp);
1432
1433 OSMO_ASSERT(last_endpoint == 5);
1434 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001435 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001436 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001437 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001438
Philipp Maiera74c0ea2018-08-02 11:59:09 +02001439 for (i = 1; i < cfg->trunk.number_endpoints; i++)
1440 mgcp_endp_release(&cfg->trunk.endpoints[i]);
1441
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001442 talloc_free(cfg);
1443}
1444
1445static void test_no_cycle(void)
1446{
1447 struct mgcp_config *cfg;
1448 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001449 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001450 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001451
1452 printf("Testing no sequence flow on initial packet\n");
1453
1454 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001455 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001456 mgcp_endpoints_allocate(&cfg->trunk);
1457
1458 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001459
Philipp Maierffd75e42017-11-22 11:44:50 +01001460 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1461 "test-connection");
1462 OSMO_ASSERT(_conn);
1463 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001464 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001465
Harald Welte49e3d5a2017-12-25 09:47:57 +01001466 OSMO_ASSERT(conn->state.stats.initialized == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001467
1468 mgcp_rtp_annex_count(endp, &conn->state, 0, 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 == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001472
1473 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001474 OSMO_ASSERT(conn->state.stats.initialized == 1);
1475 OSMO_ASSERT(conn->state.stats.cycles == 0);
1476 OSMO_ASSERT(conn->state.stats.max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001477
1478 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001479 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001480 OSMO_ASSERT(conn->state.stats.initialized == 1);
1481 OSMO_ASSERT(conn->state.stats.cycles == 0);
1482 OSMO_ASSERT(conn->state.stats.max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001483
1484 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001485 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001486 OSMO_ASSERT(conn->state.stats.initialized == 1);
1487 OSMO_ASSERT(conn->state.stats.cycles == UINT16_MAX + 1);
1488 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001489
Philipp Maier1355d7e2018-02-01 14:30:06 +01001490 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001491 talloc_free(cfg);
1492}
1493
1494static void test_no_name(void)
1495{
1496 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001497 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001498
1499 printf("Testing no rtpmap name\n");
1500 cfg = mgcp_config_alloc();
1501
Philipp Maierfcd06552017-11-10 17:32:22 +01001502 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001503 cfg->trunk.audio_send_name = 0;
1504 mgcp_endpoints_allocate(&cfg->trunk);
1505
1506 cfg->policy_cb = mgcp_test_policy_cb;
1507
1508 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1509
Philipp Maierffd75e42017-11-22 11:44:50 +01001510 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001511 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001512
1513 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001514 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001515 (char *)msg->data);
1516 OSMO_ASSERT(false);
1517 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001518 msgb_free(inp);
1519 msgb_free(msg);
1520
Philipp Maier1355d7e2018-02-01 14:30:06 +01001521 mgcp_endp_release(&cfg->trunk.endpoints[1]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001522 talloc_free(cfg);
1523}
1524
1525static void test_osmux_cid(void)
1526{
1527 int id, i;
1528
1529 OSMO_ASSERT(osmux_used_cid() == 0);
1530 id = osmux_get_cid();
1531 OSMO_ASSERT(id == 0);
1532 OSMO_ASSERT(osmux_used_cid() == 1);
1533 osmux_put_cid(id);
1534 OSMO_ASSERT(osmux_used_cid() == 0);
1535
1536 for (i = 0; i < 256; ++i) {
1537 id = osmux_get_cid();
1538 OSMO_ASSERT(id == i);
1539 OSMO_ASSERT(osmux_used_cid() == i + 1);
1540 }
1541
1542 id = osmux_get_cid();
1543 OSMO_ASSERT(id == -1);
1544
1545 for (i = 0; i < 256; ++i)
1546 osmux_put_cid(i);
1547 OSMO_ASSERT(osmux_used_cid() == 0);
1548}
1549
1550static const struct log_info_cat log_categories[] = {
1551};
1552
1553const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001554 .cat = log_categories,
1555 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001556};
1557
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001558static void test_get_lco_identifier(void)
1559{
1560 char *test;
1561 printf("Testing get_lco_identifier()\n");
1562
1563 /* Normal case at the beginning */
1564 test = "p:10, a:PCMU";
1565 printf("%s -> %s\n", test, get_lco_identifier(test));
1566
1567 test = "p:10, a:PCMU";
1568 printf("%s -> %s\n", test, get_lco_identifier(test));
1569
1570 /* Begin parsing in the middle of the value part of
1571 * the previous LCO option value */
1572 test = "XXXX, p:10, a:PCMU";
1573 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1574
1575 test = "XXXX,p:10,a:PCMU";
1576 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1577
1578 test = "10,a:PCMU";
1579 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1580
1581 test = "10, a:PCMU";
1582 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1583
1584 test = "10,a: PCMU";
1585 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1586
1587 test = "10 ,a: PCMU";
1588 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1589
1590 /* Begin parsing right at the end of the previous LCO
1591 * option value */
1592 test = ", a:PCMU";
1593 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1594
1595 test = " a:PCMU";
1596 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1597
1598 /* Empty string, result should be (null) */
1599 test = "";
1600 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1601
1602 /* Missing colons, result should be (null) */
1603 test = "p10, aPCMU";
1604 printf("%s -> %s\n", test, get_lco_identifier(test));
1605
1606 /* Colon separated from the identifier, result should be (null) */
1607 test = "10,a :PCMU";
1608 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1609}
1610
1611static void test_check_local_cx_options(void *ctx)
1612{
1613 /* Legal cases */
1614 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1615 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU") == 0);
1616 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p:10, IN:10") == 0);
1617 OSMO_ASSERT(check_local_cx_options(ctx, "one:AAA, two:BB, tree:C") ==
1618 0);
1619 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1620 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:G726-32") == 0);
1621 OSMO_ASSERT(check_local_cx_options(ctx, "p:10-20, b:64") == 0);
1622 OSMO_ASSERT(check_local_cx_options(ctx, "b:32-64, e:off") == 0);
1623 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU;G726-32") == 0);
1624
1625 /* Illegal spaces before and after colon */
1626 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p :10") == -1);
1627 OSMO_ASSERT(check_local_cx_options(ctx, "a :PCMU, p:10") == -1);
1628 OSMO_ASSERT(check_local_cx_options(ctx, "p: 10, a:PCMU") == -1);
1629
1630 /* Check if multiple appearances of LCOs are rejected */
1631 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, p:10") == -1);
1632 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, a:PCMU, p:10") ==
1633 -1);
1634 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, p:10") == -1);
1635
1636 /* Check if empty LCO are rejected */
1637 OSMO_ASSERT(check_local_cx_options(ctx, "p: , a:PCMU") == -1);
1638 OSMO_ASSERT(check_local_cx_options(ctx, "p: , a: PCMU") == -1);
1639 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a: PCMU") == -1);
1640 OSMO_ASSERT(check_local_cx_options(ctx, "p:, a:PCMU") == -1);
1641 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:") == -1);
1642
1643 /* Garbeled beginning and ends */
1644 OSMO_ASSERT(check_local_cx_options(ctx, ":10, a:10") == -1);
1645 OSMO_ASSERT(check_local_cx_options(ctx, "10, a:PCMU") == -1);
1646 OSMO_ASSERT(check_local_cx_options(ctx, ", a:PCMU") == -1);
1647 OSMO_ASSERT(check_local_cx_options(ctx, " a:PCMU") == -1);
1648 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU,") == -1);
1649 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, ") == -1);
1650
1651 /* Illegal strings */
1652 OSMO_ASSERT(check_local_cx_options(ctx, " ") == -1);
1653 OSMO_ASSERT(check_local_cx_options(ctx, "") == -1);
1654 OSMO_ASSERT(check_local_cx_options(ctx, "AAA") == -1);
1655 OSMO_ASSERT(check_local_cx_options(ctx, ":,") == -1);
1656 OSMO_ASSERT(check_local_cx_options(ctx, ",:") == -1);
1657 OSMO_ASSERT(check_local_cx_options(ctx, ",,,") == -1);
1658}
1659
Philipp Maier6931f9a2018-07-26 09:29:31 +02001660static void test_mgcp_codec_pt_translate_pars(struct mgcp_rtp_codec *c)
1661{
1662 c->rate = 8000;
1663 c->channels = 1;
1664 c->frame_duration_num = 23;
1665 c->frame_duration_den = 42;
1666}
1667
1668static void test_mgcp_codec_pt_translate(void)
1669{
1670 struct mgcp_conn_rtp conn_src;
1671 struct mgcp_conn_rtp conn_dst;
1672 int pt_dst;
1673
1674 /* Setup a realistic set of codec configurations on both
1675 * ends. AMR and HR will use different payload types. PCMU
1676 * must use 0 on both ends since this is not a dynamic payload
1677 * type */
1678 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[0]);
1679 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[0]);
1680 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[1]);
1681 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[1]);
1682 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[2]);
1683 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[2]);
1684 conn_src.end.codecs[0].payload_type = 112;
1685 conn_dst.end.codecs[0].payload_type = 96;
1686 conn_src.end.codecs[1].payload_type = 0;
1687 conn_dst.end.codecs[1].payload_type = 0;
1688 conn_src.end.codecs[2].payload_type = 111;
1689 conn_dst.end.codecs[2].payload_type = 97;
1690 conn_src.end.codecs[0].audio_name = "AMR/8000/1";
1691 conn_dst.end.codecs[0].audio_name = "AMR/8000/1";
1692 conn_src.end.codecs[1].audio_name = "PCMU/8000/1";
1693 conn_dst.end.codecs[1].audio_name = "PCMU/8000/1";
1694 conn_src.end.codecs[2].audio_name = "GSM-HR-08/8000/1";
1695 conn_dst.end.codecs[2].audio_name = "GSM-HR-08/8000/1";
1696 conn_src.end.codecs[0].subtype_name = "AMR";
1697 conn_dst.end.codecs[0].subtype_name = "AMR";
1698 conn_src.end.codecs[1].subtype_name = "PCMU";
1699 conn_dst.end.codecs[1].subtype_name = "PCMU";
1700 conn_src.end.codecs[2].subtype_name = "GSM-HR-08";
1701 conn_dst.end.codecs[2].subtype_name = "GSM-HR-08";
1702 conn_src.end.codecs_assigned = 3;
1703 conn_dst.end.codecs_assigned = 3;
1704
1705 /* We expect the function to find the PT we must use when we send the
1706 * packet out to the destination. All we know is the context for both
1707 * connections and the payload type from the source packet */
1708 pt_dst =
1709 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1710 conn_src.end.codecs[0].payload_type);
1711 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[0].payload_type);
1712 pt_dst =
1713 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1714 conn_src.end.codecs[1].payload_type);
1715 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[1].payload_type);
1716 pt_dst =
1717 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1718 conn_src.end.codecs[2].payload_type);
1719 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[2].payload_type);
1720
1721 /* Try some constellations that must fail */
1722 pt_dst = mgcp_codec_pt_translate(&conn_src, &conn_dst, 123);
1723 OSMO_ASSERT(pt_dst == -EINVAL);
1724 conn_src.end.codecs_assigned = 0;
1725 conn_dst.end.codecs_assigned = 3;
1726 pt_dst =
1727 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1728 conn_src.end.codecs[0].payload_type);
1729 OSMO_ASSERT(pt_dst == -EINVAL);
1730 pt_dst =
1731 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1732 conn_src.end.codecs[1].payload_type);
1733 OSMO_ASSERT(pt_dst == -EINVAL);
1734 pt_dst =
1735 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1736 conn_src.end.codecs[2].payload_type);
1737 OSMO_ASSERT(pt_dst == -EINVAL);
1738 conn_src.end.codecs_assigned = 3;
1739 conn_dst.end.codecs_assigned = 0;
1740 pt_dst =
1741 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1742 conn_src.end.codecs[0].payload_type);
1743 OSMO_ASSERT(pt_dst == -EINVAL);
1744 pt_dst =
1745 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1746 conn_src.end.codecs[1].payload_type);
1747 OSMO_ASSERT(pt_dst == -EINVAL);
1748 pt_dst =
1749 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1750 conn_src.end.codecs[2].payload_type);
1751 OSMO_ASSERT(pt_dst == -EINVAL);
1752}
1753
Neels Hofmeyr65317262018-09-03 22:11:05 +02001754void test_conn_id_matching()
1755{
1756 struct mgcp_endpoint endp = {};
1757 struct mgcp_conn *conn;
1758 struct mgcp_conn *conn_match;
1759 int i;
1760 const char *conn_id_generated = "000023AB";
1761 const char *conn_id_request[] = {
1762 "000023AB",
1763 "000023ab",
1764 };
1765
1766 printf("\nTesting %s\n", __func__);
1767
1768 INIT_LLIST_HEAD(&endp.conns);
1769
1770 conn = talloc_zero(NULL, struct mgcp_conn);
1771 OSMO_ASSERT(conn);
1772 osmo_strlcpy(conn->id, conn_id_generated, sizeof(conn->id));
1773 llist_add(&conn->entry, &endp.conns);
1774
1775 for (i = 0; i < ARRAY_SIZE(conn_id_request); i++) {
1776 const char *needle = conn_id_request[i];
1777 printf("needle='%s' ", needle);
1778 conn_match = mgcp_conn_get(&endp, needle);
1779 OSMO_ASSERT(conn_match);
1780 printf("found '%s'\n", conn_match->id);
1781 OSMO_ASSERT(conn_match == conn);
1782 }
1783
1784 llist_del(&conn->entry);
1785 talloc_free(conn);
1786}
1787
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001788int main(int argc, char **argv)
1789{
Neels Hofmeyr60f8e312018-03-30 23:01:07 +02001790 void *ctx = talloc_named_const(NULL, 0, "mgcp_test");
1791 void *msgb_ctx = msgb_talloc_ctx_init(ctx, 0);
1792 osmo_init_logging2(ctx, &log_info);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001793
1794 test_strline();
1795 test_values();
1796 test_messages();
1797 test_retransmission();
1798 test_packet_loss_calc();
1799 test_rqnt_cb();
1800 test_mgcp_stats();
1801 test_packet_error_detection(1, 0);
1802 test_packet_error_detection(0, 0);
1803 test_packet_error_detection(0, 1);
1804 test_packet_error_detection(1, 1);
1805 test_multilple_codec();
1806 test_no_cycle();
1807 test_no_name();
1808 test_osmux_cid();
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001809 test_get_lco_identifier();
1810 test_check_local_cx_options(ctx);
Philipp Maier6931f9a2018-07-26 09:29:31 +02001811 test_mgcp_codec_pt_translate();
Neels Hofmeyr65317262018-09-03 22:11:05 +02001812 test_conn_id_matching();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001813
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001814 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1815 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1816 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001817 printf("Done\n");
1818 return EXIT_SUCCESS;
1819}