blob: ed0fda0335b1dc2fe977abd6b5369195cbd29aff [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 Maierc66ab2c2020-06-02 20:55:34 +020029#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020030#include <osmocom/mgcp/mgcp_sdp.h>
31#include <osmocom/mgcp/mgcp_codec.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020032
33#include <osmocom/core/application.h>
34#include <osmocom/core/talloc.h>
35#include <osmocom/core/utils.h>
36#include <string.h>
37#include <limits.h>
38#include <dlfcn.h>
39#include <time.h>
40#include <math.h>
Neels Hofmeyrb861db92018-08-28 16:19:25 +020041#include <ctype.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020042
43char *strline_r(char *str, char **saveptr);
44
45const char *strline_test_data =
46 "one CR\r"
47 "two CR\r"
48 "\r"
49 "one CRLF\r\n"
50 "two CRLF\r\n"
Philipp Maier87bd9be2017-08-22 16:35:41 +020051 "\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 +020052
53#define EXPECTED_NUMBER_OF_LINES 13
54
55static void test_strline(void)
56{
57 char *save = NULL;
58 char *line;
59 char buf[2048];
60 int counter = 0;
61
62 osmo_strlcpy(buf, strline_test_data, sizeof(buf));
63
Philipp Maier87bd9be2017-08-22 16:35:41 +020064 for (line = mgcp_strline(buf, &save); line;
65 line = mgcp_strline(NULL, &save)) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020066 printf("line: '%s'\n", line);
67 counter++;
68 }
69
70 OSMO_ASSERT(counter == EXPECTED_NUMBER_OF_LINES);
71}
72
Philipp Maier12943ea2018-01-17 15:40:25 +010073#define AUEP1 "AUEP 158663169 ds/e1-1/2@mgw MGCP 1.0\r\n"
Philipp Maierc66ab2c2020-06-02 20:55:34 +020074#define AUEP1_RET "500 158663169 FAIL\r\n"
Philipp Maier12943ea2018-01-17 15:40:25 +010075#define AUEP2 "AUEP 18983213 ds/e1-2/1@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020076#define AUEP2_RET "500 18983213 FAIL\r\n"
77#define EMPTY "\r\n"
78#define EMPTY_RET NULL
79#define SHORT "CRCX \r\n"
80#define SHORT_RET "510 000000 FAIL\r\n"
81
Philipp Maier12943ea2018-01-17 15:40:25 +010082#define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@mgw MGCP 1.0\r\n"
Harald Welteabbb6b92017-12-28 13:13:50 +010083#define MDCX_ERR_RET "500 18983213 FAIL\r\n"
Philipp Maier12943ea2018-01-17 15:40:25 +010084#define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@mgw MGCP 1.0\r\n"
Philipp Maierc66ab2c2020-06-02 20:55:34 +020085#define MDCX_RET "500 18983214 FAIL\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020086
Philipp Maier87bd9be2017-08-22 16:35:41 +020087#define MDCX3 \
88 "MDCX 18983215 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010089 "I: %s\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020090
Philipp Maier87bd9be2017-08-22 16:35:41 +020091#define MDCX3_RET \
92 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +010093 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020094 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010095 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020096 "s=-\r\n" \
97 "c=IN IP4 0.0.0.0\r\n" \
98 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010099 "m=audio 16002 RTP/AVP 97\r\n" \
100 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200101 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200102
Philipp Maier87bd9be2017-08-22 16:35:41 +0200103#define MDCX3A_RET \
104 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100105 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200106 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100107 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200108 "s=-\r\n" \
109 "c=IN IP4 0.0.0.0\r\n" \
110 "t=0 0\r\n" \
111 "m=audio 16002 RTP/AVP 97\r\n" \
112 "a=rtpmap:97 GSM-EFR/8000\r\n" \
113 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200114
Philipp Maier87bd9be2017-08-22 16:35:41 +0200115#define MDCX3_FMTP_RET \
116 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100117 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200118 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100119 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200120 "s=-\r\n" \
121 "c=IN IP4 0.0.0.0\r\n" \
122 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100123 "m=audio 16006 RTP/AVP 97\r\n" \
124 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200125 "a=fmtp:126 0/1/2\r\n" \
126 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200127
Philipp Maier87bd9be2017-08-22 16:35:41 +0200128#define MDCX4 \
129 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \
130 "M: sendrecv\r" \
131 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100132 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200133 "L: p:20, a:AMR, nt:IN\r\n" \
134 "\n" \
135 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100136 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200137 "c=IN IP4 0.0.0.0\r\n" \
138 "t=0 0\r\n" \
139 "m=audio 4441 RTP/AVP 99\r\n" \
140 "a=rtpmap:99 AMR/8000\r\n" \
141 "a=ptime:40\r\n"
142
143#define MDCX4_RET(Ident) \
144 "200 " Ident " OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100145 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200146 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100147 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200148 "s=-\r\n" \
149 "c=IN IP4 0.0.0.0\r\n" \
150 "t=0 0\r\n" \
151 "m=audio 16002 RTP/AVP 99\r\n" \
152 "a=rtpmap:99 AMR/8000\r\n" \
153 "a=ptime:40\r\n"
154
155#define MDCX4_RO_RET(Ident) \
156 "200 " Ident " OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100157 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100159 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160 "s=-\r\n" \
161 "c=IN IP4 0.0.0.0\r\n" \
162 "t=0 0\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200163 "m=audio 16002 RTP/AVP 112\r\n" \
164 "a=rtpmap:112 AMR\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200165 "a=ptime:40\r\n"
166
167#define MDCX4_PT1 \
168 "MDCX 18983217 1@mgw MGCP 1.0\r\n" \
Pau Espin Pedrol17058482019-06-26 12:23:02 +0200169 "M: SENDRECV\r" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200170 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100171 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172 "L: p:20-40, a:AMR, nt:IN\r\n" \
173 "\n" \
174 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100175 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176 "c=IN IP4 0.0.0.0\r\n" \
177 "t=0 0\r\n" \
178 "m=audio 4441 RTP/AVP 99\r\n" \
179 "a=rtpmap:99 AMR/8000\r\n" \
180 "a=ptime:40\r\n"
181
182#define MDCX4_PT2 \
183 "MDCX 18983218 1@mgw MGCP 1.0\r\n" \
184 "M: sendrecv\r" \
185 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100186 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187 "L: p:20-20, a:AMR, nt:IN\r\n" \
188 "\n" \
189 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100190 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191 "c=IN IP4 0.0.0.0\r\n" \
192 "t=0 0\r\n" \
193 "m=audio 4441 RTP/AVP 99\r\n" \
194 "a=rtpmap:99 AMR/8000\r\n" \
195 "a=ptime:40\r\n"
196
197#define MDCX4_PT3 \
198 "MDCX 18983219 1@mgw MGCP 1.0\r\n" \
199 "M: sendrecv\r" \
200 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100201 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200202 "L: a:AMR, nt:IN\r\n" \
203 "\n" \
204 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100205 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200206 "c=IN IP4 0.0.0.0\r\n" \
207 "t=0 0\r\n" \
208 "m=audio 4441 RTP/AVP 99\r\n" \
209 "a=rtpmap:99 AMR/8000\r\n" \
210 "a=ptime:40\r\n"
211
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200212/* Test different upper/lower case in options */
213#define MDCX4_PT4 \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200214 "MDCX 18983220 1@mgw MGCP 1.0\r\n" \
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +0200215 "m: sendrecv\r" \
216 "c: 2\r\n" \
217 "i: %s\r\n" \
Pau Espin Pedrol83fd8a52019-06-26 12:55:26 +0200218 "l: A:amr, NT:IN\r\n" \
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200219 "\n" \
220 "v=0\r\n" \
221 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
222 "c=IN IP4 0.0.0.0\r\n" \
223 "t=0 0\r\n" \
224 "m=audio 4441 RTP/AVP 99\r\n" \
225 "a=rtpmap:99 AMR/8000\r\n" \
226 "a=ptime:40\r\n"
227
228#define MDCX4_SO \
229 "MDCX 18983221 1@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200230 "M: sendonly\r" \
231 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100232 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233 "L: p:20, a:AMR, nt:IN\r\n" \
234 "\n" \
235 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100236 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200237 "c=IN IP4 0.0.0.0\r\n" \
238 "t=0 0\r\n" \
239 "m=audio 4441 RTP/AVP 99\r\n" \
240 "a=rtpmap:99 AMR/8000\r\n" \
241 "a=ptime:40\r\n"
242
243#define MDCX4_RO \
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200244 "MDCX 18983222 1@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245 "M: recvonly\r" \
246 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100247 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200248 "L: p:20, a:AMR, nt:IN\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200249
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200250#define MDCX_TOO_LONG_CI \
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200251 "MDCX 18983223 1@mgw MGCP 1.0\r\n" \
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200252 "I: 123456789012345678901234567890123\n"
253
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200254#define MDCX_TOO_LONG_CI_RET "510 18983223 FAIL\r\n"
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200255
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200256#define SHORT2 "CRCX 1"
257#define SHORT2_RET "510 000000 FAIL\r\n"
258#define SHORT3 "CRCX 1 1@mgw"
259#define SHORT4 "CRCX 1 1@mgw MGCP"
260#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
261
Philipp Maier87bd9be2017-08-22 16:35:41 +0200262#define CRCX \
263 "CRCX 2 1@mgw MGCP 1.0\r\n" \
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +0200264 "m: recvonly\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200265 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200266 "L: p:20\r\n" \
267 "\r\n" \
268 "v=0\r\n" \
269 "c=IN IP4 123.12.12.123\r\n" \
270 "m=audio 5904 RTP/AVP 97\r\n" \
271 "a=rtpmap:97 GSM-EFR/8000\r\n" \
272 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200273
Philipp Maier87bd9be2017-08-22 16:35:41 +0200274#define CRCX_RET \
275 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100276 "I: %s\r\n" \
277 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200278 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100279 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200280 "s=-\r\n" \
281 "c=IN IP4 0.0.0.0\r\n" \
282 "t=0 0\r\n" \
283 "m=audio 16002 RTP/AVP 97\r\n" \
284 "a=rtpmap:97 GSM-EFR/8000\r\n" \
285 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200286
Philipp Maier87bd9be2017-08-22 16:35:41 +0200287#define CRCX_RET_NO_RTPMAP \
288 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100289 "I: %s\r\n" \
290 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200291 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100292 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200293 "s=-\r\n" \
294 "c=IN IP4 0.0.0.0\r\n" \
295 "t=0 0\r\n" \
296 "m=audio 16002 RTP/AVP 97\r\n" \
297 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200298
Philipp Maier87bd9be2017-08-22 16:35:41 +0200299#define CRCX_FMTP_RET \
300 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100301 "I: %s\r\n" \
302 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200303 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100304 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200305 "s=-\r\n" \
306 "c=IN IP4 0.0.0.0\r\n" \
307 "t=0 0\r\n" \
308 "m=audio 16006 RTP/AVP 97\r\n" \
309 "a=rtpmap:97 GSM-EFR/8000\r\n" \
310 "a=fmtp:126 0/1/2\r\n" \
311 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312
Philipp Maier87bd9be2017-08-22 16:35:41 +0200313#define CRCX_ZYN \
314 "CRCX 2 1@mgw MGCP 1.0\r" \
315 "M: recvonly\r" \
316 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200317 "\n" \
318 "v=0\r" \
319 "c=IN IP4 123.12.12.123\r" \
320 "m=audio 5904 RTP/AVP 97\r" \
321 "a=rtpmap:97 GSM-EFR/8000\r"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200322
Philipp Maier87bd9be2017-08-22 16:35:41 +0200323#define CRCX_ZYN_RET \
324 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100325 "I: %s\r\n" \
326 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200327 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100328 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200329 "s=-\r\n" \
330 "c=IN IP4 0.0.0.0\r\n" \
331 "t=0 0\r\n" \
332 "m=audio 16004 RTP/AVP 97\r\n" \
333 "a=rtpmap:97 GSM-EFR/8000\r\n" \
334 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200335
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200336#define CRCX_X_OSMO_IGN \
337 "CRCX 2 1@mgw MGCP 1.0\r\n" \
338 "M: recvonly\r\n" \
339 "C: 2\r\n" \
340 "L: p:20\r\n" \
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200341 "X-Osmo-IGN: C foo\r\n" \
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200342 "\r\n" \
343 "v=0\r\n" \
344 "c=IN IP4 123.12.12.123\r\n" \
345 "m=audio 5904 RTP/AVP 97\r\n" \
346 "a=rtpmap:97 GSM-EFR/8000\r\n" \
347 "a=ptime:40\r\n"
348
349#define CRCX_X_OSMO_IGN_RET \
350 "200 2 OK\r\n" \
351 "I: %s\r\n" \
352 "\r\n" \
353 "v=0\r\n" \
354 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
355 "s=-\r\n" \
356 "c=IN IP4 0.0.0.0\r\n" \
357 "t=0 0\r\n" \
358 "m=audio 16010 RTP/AVP 97\r\n" \
359 "a=rtpmap:97 GSM-EFR/8000\r\n" \
360 "a=ptime:40\r\n"
361
Philipp Maier87bd9be2017-08-22 16:35:41 +0200362#define DLCX \
363 "DLCX 7 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100364 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200365 "C: 2\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366
Philipp Maier87bd9be2017-08-22 16:35:41 +0200367#define DLCX_RET \
368 "250 7 OK\r\n" \
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100369 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n"
370
371 #define DLCX_RET_OSMUX DLCX_RET \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200372 "X-Osmo-CP: EC TI=0, TO=0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200373
Philipp Maier87bd9be2017-08-22 16:35:41 +0200374#define RQNT \
375 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
376 "X: B244F267488\r\n" \
377 "S: D/9\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378
Philipp Maier87bd9be2017-08-22 16:35:41 +0200379#define RQNT2 \
380 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
381 "X: ADD4F26746F\r\n" \
382 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200383
384#define RQNT1_RET "200 186908780 OK\r\n"
385#define RQNT2_RET "200 186908781 OK\r\n"
386
Philipp Maier87bd9be2017-08-22 16:35:41 +0200387#define PTYPE_IGNORE 0 /* == default initializer */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200388#define PTYPE_NONE 128
389#define PTYPE_NYI PTYPE_NONE
390
Philipp Maier87bd9be2017-08-22 16:35:41 +0200391#define CRCX_MULT_1 \
392 "CRCX 2 1@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200393 "M: recvonly\r\n" \
394 "C: 2\r\n" \
395 "X\r\n" \
396 "L: p:20\r\n" \
397 "\r\n" \
398 "v=0\r\n" \
399 "c=IN IP4 123.12.12.123\r\n" \
400 "m=audio 5904 RTP/AVP 18 97\r\n" \
401 "a=rtpmap:18 G729/8000\r\n" \
402 "a=rtpmap:97 GSM-EFR/8000\r\n" \
403 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200404
Philipp Maier87bd9be2017-08-22 16:35:41 +0200405#define CRCX_MULT_2 \
406 "CRCX 2 2@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200407 "M: recvonly\r\n" \
408 "C: 2\r\n" \
409 "X\r\n" \
410 "L: p:20\r\n" \
411 "\r\n" \
412 "v=0\r\n" \
413 "c=IN IP4 123.12.12.123\r\n" \
414 "m=audio 5904 RTP/AVP 18 97 101\r\n" \
415 "a=rtpmap:18 G729/8000\r\n" \
416 "a=rtpmap:97 GSM-EFR/8000\r\n" \
417 "a=rtpmap:101 FOO/8000\r\n" \
418 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200419
Philipp Maier87bd9be2017-08-22 16:35:41 +0200420#define CRCX_MULT_3 \
421 "CRCX 2 3@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200422 "M: recvonly\r\n" \
423 "C: 2\r\n" \
424 "X\r\n" \
425 "L: p:20\r\n" \
426 "\r\n" \
427 "v=0\r\n" \
428 "c=IN IP4 123.12.12.123\r\n" \
429 "m=audio 5904 RTP/AVP\r\n" \
430 "a=rtpmap:18 G729/8000\r\n" \
431 "a=rtpmap:97 GSM-EFR/8000\r\n" \
432 "a=rtpmap:101 FOO/8000\r\n" \
433 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200434
Philipp Maier87bd9be2017-08-22 16:35:41 +0200435#define CRCX_MULT_4 \
436 "CRCX 2 4@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437 "M: recvonly\r\n" \
438 "C: 2\r\n" \
439 "X\r\n" \
440 "L: p:20\r\n" \
441 "\r\n" \
442 "v=0\r\n" \
443 "c=IN IP4 123.12.12.123\r\n" \
444 "m=audio 5904 RTP/AVP 18\r\n" \
445 "a=rtpmap:18 G729/8000\r\n" \
446 "a=rtpmap:97 GSM-EFR/8000\r\n" \
447 "a=rtpmap:101 FOO/8000\r\n" \
448 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200449
450#define CRCX_MULT_GSM_EXACT \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200451 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
452 "C: 1355c6041e\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200453 "L: p:20, a:GSM, nt:IN\r\n" \
454 "M: recvonly\r\n" \
455 "\r\n" \
456 "v=0\r\n" \
457 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
458 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200459 "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 +0200460 "a=rtpmap:0 PCMU/8000\r\n" \
461 "a=rtpmap:8 PCMA/8000\r\n" \
462 "a=rtpmap:3 gsm/8000\r\n" \
463 "a=rtpmap:18 G729/8000\r\n" \
464 "a=fmtp:18 annexb=no\r\n" \
465 "a=rtpmap:4 G723/8000\r\n" \
466 "a=rtpmap:96 iLBC/8000\r\n" \
467 "a=fmtp:96 mode=20\r\n" \
468 "a=rtpmap:97 iLBC/8000\r\n" \
469 "a=fmtp:97 mode=30\r\n" \
470 "a=rtpmap:101 telephone-event/8000\r\n" \
471 "a=fmtp:101 0-15\r\n" \
472 "a=recvonly\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200473
Philipp Maier87bd9be2017-08-22 16:35:41 +0200474#define MDCX_NAT_DUMMY \
475 "MDCX 23 5@mgw MGCP 1.0\r\n" \
476 "C: 1355c6041e\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100477 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478 "\r\n" \
479 "c=IN IP4 8.8.8.8\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200480 "m=audio 16434 RTP/AVP 3\r\n"
481
482#define CRCX_NO_LCO_NO_SDP \
483 "CRCX 2 6@mgw MGCP 1.0\r\n" \
484 "M: recvonly\r\n" \
485 "C: 2\r\n"
486
Philipp Maier228e5912019-03-05 13:56:59 +0100487#define CRCX_AMR_WITH_FMTP \
488 "CRCX 2 7@mgw MGCP 1.0\r\n" \
489 "M: recvonly\r\n" \
490 "C: 2\r\n" \
491 "X\r\n" \
492 "L: p:20\r\n" \
493 "\r\n" \
494 "v=0\r\n" \
495 "c=IN IP4 123.12.12.123\r\n" \
496 "m=audio 5904 RTP/AVP 111\r\n" \
497 "a=rtpmap:111 AMR/8000/1\r\n" \
498 "a=ptime:20\r\n" \
499 "a=fmtp:111 mode-change-capability=2; octet-align=1\r\n" \
500
501#define CRCX_AMR_WITH_FMTP_RET \
502 "200 2 OK\r\n" \
503 "I: %s\r\n" \
504 "\r\n" \
505 "v=0\r\n" \
506 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
507 "s=-\r\n" \
508 "c=IN IP4 0.0.0.0\r\n" \
509 "t=0 0\r\n" \
510 "m=audio 16012 RTP/AVP 111\r\n" \
511 "a=rtpmap:111 AMR/8000/1\r\n" \
512 "a=fmtp:111 octet-align=1\r\n" \
513 "a=ptime:20\r\n"
514
Philipp Maierbc0346e2018-06-07 09:52:16 +0200515#define CRCX_NO_LCO_NO_SDP_RET \
516 "200 2 OK\r\n" \
517 "I: %s\r\n" \
518 "\r\n" \
519 "v=0\r\n" \
520 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
521 "s=-\r\n" \
522 "c=IN IP4 0.0.0.0\r\n" \
523 "t=0 0\r\n" \
524 "m=audio 16008 RTP/AVP 0\r\n" \
525 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200526
527struct mgcp_test {
528 const char *name;
529 const char *req;
530 const char *exp_resp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200531 int ptype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 const char *extra_fmtp;
533};
534
535static const struct mgcp_test tests[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200536 {"AUEP1", AUEP1, AUEP1_RET},
537 {"AUEP2", AUEP2, AUEP2_RET},
538 {"MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET},
539 {"MDCX2", MDCX_UNALLOCATED, MDCX_RET},
540 {"CRCX", CRCX, CRCX_RET, 97},
541 {"MDCX3", MDCX3, MDCX3_RET, PTYPE_IGNORE},
542 {"MDCX4", MDCX4, MDCX4_RET("18983216"), 99},
543 {"MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99},
544 {"MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99},
545 {"MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99},
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200546 {"MDCX4_PT4", MDCX4_PT4, MDCX4_RET("18983220"), 99},
547 {"MDCX4_SO", MDCX4_SO, MDCX4_RET("18983221"), 99},
548 {"MDCX4_RO", MDCX4_RO, MDCX4_RO_RET("18983222"), PTYPE_IGNORE},
Philipp Maier87bd9be2017-08-22 16:35:41 +0200549 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
550 {"CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97},
551 {"EMPTY", EMPTY, EMPTY_RET},
552 {"SHORT1", SHORT, SHORT_RET},
553 {"SHORT2", SHORT2, SHORT2_RET},
554 {"SHORT3", SHORT3, SHORT2_RET},
555 {"SHORT4", SHORT4, SHORT2_RET},
556 {"RQNT1", RQNT, RQNT1_RET},
557 {"RQNT2", RQNT2, RQNT2_RET},
558 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
559 {"CRCX", CRCX, CRCX_FMTP_RET, 97,.extra_fmtp = "a=fmtp:126 0/1/2"},
560 {"MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE,.extra_fmtp =
561 "a=fmtp:126 0/1/2"},
562 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE,.extra_fmtp = "a=fmtp:126 0/1/2"},
Philipp Maierbc0346e2018-06-07 09:52:16 +0200563 {"CRCX", CRCX_NO_LCO_NO_SDP, CRCX_NO_LCO_NO_SDP_RET, 97},
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200564 {"CRCX", CRCX_X_OSMO_IGN, CRCX_X_OSMO_IGN_RET, 97},
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200565 {"MDCX_TOO_LONG_CI", MDCX_TOO_LONG_CI, MDCX_TOO_LONG_CI_RET},
Philipp Maier228e5912019-03-05 13:56:59 +0100566 {"CRCX", CRCX_AMR_WITH_FMTP, CRCX_AMR_WITH_FMTP_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200567};
568
569static const struct mgcp_test retransmit[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200570 {"CRCX", CRCX, CRCX_RET},
571 {"RQNT1", RQNT, RQNT1_RET},
572 {"RQNT2", RQNT2, RQNT2_RET},
573 {"MDCX3", MDCX3, MDCX3A_RET},
574 {"DLCX", DLCX, DLCX_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200575};
576
Philipp Maierffd75e42017-11-22 11:44:50 +0100577static struct msgb *create_msg(const char *str, const char *conn_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200578{
579 struct msgb *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100580 int len;
581
582 printf("creating message from statically defined input:\n");
583 printf("---------8<---------\n%s\n---------8<---------\n", str);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200584
585 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Philipp Maierffd75e42017-11-22 11:44:50 +0100586 if (conn_id && strlen(conn_id))
587 len = sprintf((char *)msg->data, str, conn_id, conn_id);
588 else
589 len = sprintf((char *)msg->data, "%s", str);
590
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591 msg->l2h = msgb_put(msg, len);
592 return msg;
593}
594
595static int last_endpoint = -1;
596
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200597static int mgcp_test_policy_cb(struct mgcp_endpoint *endp,
598 int state, const char *transaction_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200599{
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200600 unsigned int i;
601 struct mgcp_trunk *trunk;
602
603 fprintf(stderr, "Policy CB got state %d on endpoint %s\n",
604 state, endp->name);
605
606 trunk = endp->trunk;
607 last_endpoint = -1;
608 for (i = 0; i < trunk->vty_number_endpoints; i++) {
609 if (strcmp(endp->name, trunk->endpoints[i]->name) == 0)
610 last_endpoint = i;
611 }
612
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200613 return MGCP_POLICY_CONT;
614}
615
616#define MGCP_DUMMY_LOAD 0x23
617static int dummy_packets = 0;
618/* override and forward */
619ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200620 const struct sockaddr *dest_addr, socklen_t addrlen)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200621{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200622 uint32_t dest_host =
623 htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
624 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200625
Philipp Maier87bd9be2017-08-22 16:35:41 +0200626 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
627 fprintf(stderr,
628 "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
629 dest_host, dest_port, len, osmo_hexdump(buf, len));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200630 dummy_packets += 1;
631 }
632
Philipp Maier3d9b6562017-10-13 18:33:44 +0200633 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634}
635
636static int64_t force_monotonic_time_us = -1;
637/* override and forward */
638int clock_gettime(clockid_t clk_id, struct timespec *tp)
639{
640 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
641 static clock_gettime_t real_clock_gettime = NULL;
642
643 if (!real_clock_gettime)
644 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
645
646 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
647 tp->tv_sec = force_monotonic_time_us / 1000000;
648 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
649 return 0;
650 }
651
652 return real_clock_gettime(clk_id, tp);
653}
654
Philipp Maier14b27a82020-06-02 20:15:30 +0200655static void mgcp_endpoints_release(struct mgcp_trunk *trunk)
Pau Espin Pedrold071a302019-09-19 17:39:31 +0200656{
657 int i;
658 for (i = 1; i < trunk->number_endpoints; i++)
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200659 mgcp_endp_release(trunk->endpoints[i]);
Pau Espin Pedrold071a302019-09-19 17:39:31 +0200660}
661
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200662#define CONN_UNMODIFIED (0x1000)
663
664static void test_values(void)
665{
666 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200667 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200668
669 /* Check that LOOPBACK enables all output */
670 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200671 MGCP_CONN_RECV_SEND);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200672}
673
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200674/* Extract a connection ID from a response and return in conn_id;
675 * if there is none, return -EINVAL and leave conn_id unchanged. */
Philipp Maierffd75e42017-11-22 11:44:50 +0100676static int get_conn_id_from_response(uint8_t *resp, char *conn_id,
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200677 size_t conn_id_buflen)
Philipp Maierffd75e42017-11-22 11:44:50 +0100678{
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200679 const char *conn_id_start;
680 const char *conn_id_end;
681 int conn_id_len;
Philipp Maierffd75e42017-11-22 11:44:50 +0100682
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200683 const char *header_I = "\r\nI: ";
684 const char *header_o = "\r\no=- ";
Philipp Maierffd75e42017-11-22 11:44:50 +0100685
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200686 /* Try to get the conn_id from the 'I:' or 'o=-' parameter */
687 if ((conn_id_start = strstr((char *)resp, header_I))) {
688 conn_id_start += strlen(header_I);
689 conn_id_end = strstr(conn_id_start, "\r\n");
690 } else if ((conn_id_start = strstr((char *)resp, header_o))) {
691 conn_id_start += strlen(header_o);
692 conn_id_end = strchr(conn_id_start, ' ');
693 } else
694 return -EINVAL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100695
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200696 if (conn_id_end)
697 conn_id_len = conn_id_end - conn_id_start;
698 else
699 conn_id_len = strlen(conn_id_start);
700 OSMO_ASSERT(conn_id_len <= conn_id_buflen - 1);
Philipp Maier55295f72018-01-15 14:00:28 +0100701
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200702 /* A valid conn_id must at least contain one digit, and must
703 * not exceed a length of 32 digits */
704 OSMO_ASSERT(conn_id_len <= 32);
705 OSMO_ASSERT(conn_id_len > 0);
706
707 strncpy(conn_id, conn_id_start, conn_id_len);
708 conn_id[conn_id_len] = '\0';
709 return 0;
Philipp Maierffd75e42017-11-22 11:44:50 +0100710}
711
712/* Check response, automatically patch connection ID if needed */
713static int check_response(uint8_t *resp, const char *exp_resp)
714{
715 char exp_resp_patched[4096];
716 const char *exp_resp_ptr;
717 char conn_id[256];
718
719 printf("checking response:\n");
720
721 /* If the expected response is intened to be patched
722 * (%s placeholder inside) we will patch it with the
723 * connection identifier we just received from the
724 * real response. This is necessary because the CI
725 * is generated by the mgcp code on CRCX and we can
726 * not know it in advance */
727 if (strstr(exp_resp, "%s")) {
728 if (get_conn_id_from_response(resp, conn_id, sizeof(conn_id)) ==
729 0) {
730 sprintf(exp_resp_patched, exp_resp, conn_id, conn_id);
731 exp_resp_ptr = exp_resp_patched;
732 printf
733 ("using message with patched conn_id for comparison\n");
734 } else {
735 printf
736 ("patching conn_id failed, using message as statically defined for comparison\n");
737 exp_resp_ptr = exp_resp;
738 }
739 } else {
740 printf("using message as statically defined for comparison\n");
741 exp_resp_ptr = exp_resp;
742 }
743
744 if (strcmp((char *)resp, exp_resp_ptr) != 0) {
745 printf("Unexpected response, please check!\n");
746 printf
747 ("Got:\n---------8<---------\n%s\n---------8<---------\n\n",
748 resp);
749 printf
750 ("Expected:\n---------8<---------\n%s\n---------8<---------\n",
751 exp_resp_ptr);
752 return -EINVAL;
753 }
754
755 printf("Response matches our expectations.\n");
756 return 0;
757}
758
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200759static void test_messages(void)
760{
761 struct mgcp_config *cfg;
762 struct mgcp_endpoint *endp;
Philipp Maier14b27a82020-06-02 20:15:30 +0200763 struct mgcp_trunk *trunk2;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200764 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200765 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100766 char last_conn_id[256];
Philipp Maier7df419b2017-12-04 17:11:42 +0100767 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200768
769 cfg = mgcp_config_alloc();
770
Harald Weltec39b1bf2020-03-08 11:29:39 +0100771 cfg->virt_trunk->vty_number_endpoints = 64;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200772 mgcp_trunk_alloc_endpts(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200773 cfg->policy_cb = mgcp_test_policy_cb;
774
Philipp Maierffd75e42017-11-22 11:44:50 +0100775 memset(last_conn_id, 0, sizeof(last_conn_id));
776
Harald Weltec39b1bf2020-03-08 11:29:39 +0100777 trunk2 = mgcp_trunk_alloc(cfg, MGCP_TRUNK_E1, 1);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200778 mgcp_trunk_alloc_endpts(trunk2);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200779
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200780 for (i = 0; i < ARRAY_SIZE(tests); i++) {
781 const struct mgcp_test *t = &tests[i];
782 struct msgb *inp;
783 struct msgb *msg;
784
Philipp Maierffd75e42017-11-22 11:44:50 +0100785 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200786 printf("Testing %s\n", t->name);
787
788 last_endpoint = -1;
789 dummy_packets = 0;
790
Harald Weltec39b1bf2020-03-08 11:29:39 +0100791 osmo_talloc_replace_string(cfg, &cfg->virt_trunk->audio_fmtp_extra,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200792 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200793
Philipp Maierffd75e42017-11-22 11:44:50 +0100794 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200795 msg = mgcp_handle_message(cfg, inp);
796 msgb_free(inp);
797 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200798 if (msg) {
799 printf("%s failed '%s'\n", t->name,
800 (char *)msg->data);
801 OSMO_ASSERT(false);
802 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100803 } else if (check_response(msg->data, t->exp_resp) != 0) {
804 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200805 OSMO_ASSERT(false);
806 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100807
Philipp Maier7df419b2017-12-04 17:11:42 +0100808 if (msg) {
809 rc = get_conn_id_from_response(msg->data, last_conn_id,
810 sizeof(last_conn_id));
Neels Hofmeyr08e07042018-08-28 16:22:14 +0200811 if (rc == 0)
Philipp Maier7df419b2017-12-04 17:11:42 +0100812 printf("(response contains a connection id)\n");
813 else
814 printf("(response does not contain a connection id)\n");
815 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100816
Philipp Maiera330b862017-12-04 17:16:16 +0100817 if (msg)
818 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200819
820 if (dummy_packets)
821 printf("Dummy packets: %d\n", dummy_packets);
822
823 if (last_endpoint != -1) {
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200824 endp = cfg->virt_trunk->endpoints[last_endpoint];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825
Philipp Maier01d24a32017-11-21 17:26:09 +0100826 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827 if (conn) {
828 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200829
Philipp Maier87bd9be2017-08-22 16:35:41 +0200830 if (conn->end.packet_duration_ms != -1)
831 printf("Detected packet duration: %d\n",
832 conn->end.packet_duration_ms);
833 else
834 printf("Packet duration not set\n");
835 if (endp->local_options.pkt_period_min ||
836 endp->local_options.pkt_period_max)
837 printf
838 ("Requested packetetization period: "
839 "%d-%d\n",
840 endp->local_options.pkt_period_min,
841 endp->
842 local_options.pkt_period_max);
843 else
844 printf
845 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200846
Philipp Maier87bd9be2017-08-22 16:35:41 +0200847 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
848 printf("Connection mode: %d:%s%s%s%s\n",
849 conn->conn->mode,
850 !conn->conn->mode ? " NONE" : "",
851 conn->conn->mode & MGCP_CONN_SEND_ONLY
852 ? " SEND" : "",
853 conn->conn->mode & MGCP_CONN_RECV_ONLY
854 ? " RECV" : "",
855 conn->conn->mode & MGCP_CONN_LOOPBACK
856 & ~MGCP_CONN_RECV_SEND
857 ? " LOOP" : "");
858 fprintf(stderr,
859 "RTP output %sabled, NET output %sabled\n",
860 conn->end.output_enabled
861 ? "en" : "dis",
862 conn->end.output_enabled
863 ? "en" : "dis");
864 } else
865 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200866
Philipp Maier87bd9be2017-08-22 16:35:41 +0200867 OSMO_ASSERT(conn->end.output_enabled
868 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
869
870 conn->conn->mode |= CONN_UNMODIFIED;
871
872 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200873 endp->local_options.pkt_period_min = 0;
874 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200875 }
876
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200877 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100878 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200879 OSMO_ASSERT(last_endpoint != -1);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200880 endp = cfg->virt_trunk->endpoints[last_endpoint];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200881
Pau Espin Pedrol9ecceb62018-10-16 15:35:58 +0200882 fprintf(stderr, "endpoint 0x%x: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200883 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884 last_endpoint,
Philipp Maierbc0346e2018-06-07 09:52:16 +0200885 conn->end.codec->payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200886
Philipp Maier87bd9be2017-08-22 16:35:41 +0200887 if (t->ptype != PTYPE_IGNORE)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200888 OSMO_ASSERT(conn->end.codec->payload_type ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200889 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200890
891 /* Reset them again for next test */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200892 conn->end.codec->payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200893 }
894 }
895
Pau Espin Pedrold071a302019-09-19 17:39:31 +0200896 mgcp_endpoints_release(trunk2);
Harald Weltec39b1bf2020-03-08 11:29:39 +0100897 mgcp_endpoints_release(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200898 talloc_free(cfg);
899}
900
901static void test_retransmission(void)
902{
903 struct mgcp_config *cfg;
Philipp Maier14b27a82020-06-02 20:15:30 +0200904 struct mgcp_trunk *trunk2;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200905 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100906 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100907 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200908
909 cfg = mgcp_config_alloc();
910
Harald Weltec39b1bf2020-03-08 11:29:39 +0100911 cfg->virt_trunk->vty_number_endpoints = 64;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200912 mgcp_trunk_alloc_endpts(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200913
Philipp Maierffd75e42017-11-22 11:44:50 +0100914 memset(last_conn_id, 0, sizeof(last_conn_id));
915
Harald Weltec39b1bf2020-03-08 11:29:39 +0100916 trunk2 = mgcp_trunk_alloc(cfg, MGCP_TRUNK_E1, 1);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200917 mgcp_trunk_alloc_endpts(trunk2);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200918
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200919 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
920 const struct mgcp_test *t = &retransmit[i];
921 struct msgb *inp;
922 struct msgb *msg;
923
Philipp Maierffd75e42017-11-22 11:44:50 +0100924 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200925 printf("Testing %s\n", t->name);
926
Philipp Maierffd75e42017-11-22 11:44:50 +0100927 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200928 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200929
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200930 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100931 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932 printf("%s failed '%s'\n", t->name, (char *)msg->data);
933 OSMO_ASSERT(false);
934 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100935
Philipp Maier23b8e292017-12-04 16:48:45 +0100936 if (msg && strcmp(t->name, "CRCX") == 0) {
937 rc = get_conn_id_from_response(msg->data, last_conn_id,
938 sizeof(last_conn_id));
939 OSMO_ASSERT(rc == 0);
940 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100941
Philipp Maier7cedfd72017-12-04 16:49:12 +0100942 if (msg)
943 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200944
945 /* Retransmit... */
946 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100947 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200948 msg = mgcp_handle_message(cfg, inp);
949 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100950 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200951 printf("%s failed '%s'\n", t->name, (char *)msg->data);
952 OSMO_ASSERT(false);
953 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200954 msgb_free(msg);
955 }
956
Pau Espin Pedrold071a302019-09-19 17:39:31 +0200957 mgcp_endpoints_release(trunk2);
Harald Weltec39b1bf2020-03-08 11:29:39 +0100958 mgcp_endpoints_release(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200959 talloc_free(cfg);
960}
961
962static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
963{
964 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200965 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200966 return 0;
967}
968
969static void test_rqnt_cb(void)
970{
971 struct mgcp_config *cfg;
Philipp Maier14b27a82020-06-02 20:15:30 +0200972 struct mgcp_trunk *trunk2;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200973 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100974 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200975
976 cfg = mgcp_config_alloc();
977 cfg->rqnt_cb = rqnt_cb;
978
Harald Weltec39b1bf2020-03-08 11:29:39 +0100979 cfg->virt_trunk->vty_number_endpoints = 64;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200980 mgcp_trunk_alloc_endpts(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200981
Harald Weltec39b1bf2020-03-08 11:29:39 +0100982 trunk2 = mgcp_trunk_alloc(cfg, MGCP_TRUNK_E1, 1);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200983 mgcp_trunk_alloc_endpts(trunk2);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200984
Philipp Maierffd75e42017-11-22 11:44:50 +0100985 inp = create_msg(CRCX, NULL);
986 msg = mgcp_handle_message(cfg, inp);
987 OSMO_ASSERT(msg);
988 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
989 sizeof(conn_id)) == 0);
990 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200991 msgb_free(inp);
992
993 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100994 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200995 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200996 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200997 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
998 abort();
999 }
1000
Philipp Maier87bd9be2017-08-22 16:35:41 +02001001 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001002 printf("FAILED: callback not called: %p\n", cfg->data);
1003 abort();
1004 }
1005
1006 msgb_free(msg);
1007 msgb_free(inp);
1008
Philipp Maierffd75e42017-11-22 11:44:50 +01001009 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001010 msgb_free(mgcp_handle_message(cfg, inp));
1011 msgb_free(inp);
Pau Espin Pedrold071a302019-09-19 17:39:31 +02001012 mgcp_endpoints_release(trunk2);
Harald Weltec39b1bf2020-03-08 11:29:39 +01001013 mgcp_endpoints_release(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001014 talloc_free(cfg);
1015}
1016
1017struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001018 int cycles;
1019 uint16_t base_seq;
1020 uint16_t max_seq;
1021 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001022
Philipp Maier87bd9be2017-08-22 16:35:41 +02001023 uint32_t expected;
1024 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001025};
1026
1027static const struct pl_test pl_test_dat[] = {
1028 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001029 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
1030 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001031 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001032 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
1033 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001034 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001035 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
1036 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001037 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001038 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
1039 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001040 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001041 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
1042 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001043};
1044
1045static void test_packet_loss_calc(void)
1046{
1047 int i;
Philipp Maiercede2a42018-07-03 14:14:21 +02001048 struct mgcp_endpoint endp;
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001049 struct mgcp_endpoint *endpoints[1];
Oliver Smithe36b7752019-01-22 16:31:36 +01001050 struct mgcp_config cfg = {0};
Philipp Maier14b27a82020-06-02 20:15:30 +02001051 struct mgcp_trunk trunk;
Philipp Maiercede2a42018-07-03 14:14:21 +02001052
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001053 printf("Testing packet loss calculation.\n");
1054
Philipp Maiercede2a42018-07-03 14:14:21 +02001055 memset(&endp, 0, sizeof(endp));
1056 memset(&trunk, 0, sizeof(trunk));
1057
Oliver Smithe36b7752019-01-22 16:31:36 +01001058 endp.cfg = &cfg;
Philipp Maiercede2a42018-07-03 14:14:21 +02001059 endp.type = &ep_typeset.rtp;
1060 trunk.vty_number_endpoints = 1;
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001061 trunk.endpoints = endpoints;
1062 trunk.endpoints[0] = &endp;
Philipp Maier14b27a82020-06-02 20:15:30 +02001063 endp.trunk = &trunk;
Philipp Maiercede2a42018-07-03 14:14:21 +02001064 INIT_LLIST_HEAD(&endp.conns);
1065
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001066 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
1067 uint32_t expected;
1068 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001069
Philipp Maiercede2a42018-07-03 14:14:21 +02001070 struct mgcp_conn_rtp *conn = NULL;
1071 struct mgcp_conn *_conn = NULL;
1072 struct mgcp_rtp_state *state;
1073 struct rate_ctr *packets_rx;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001074
Philipp Maiercede2a42018-07-03 14:14:21 +02001075 _conn =
1076 mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1077 "test-connection");
1078 conn = mgcp_conn_get_rtp(&endp, _conn->id);
1079 state = &conn->state;
1080 packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
1081
1082 state->stats.initialized = 1;
1083 state->stats.base_seq = pl_test_dat[i].base_seq;
1084 state->stats.max_seq = pl_test_dat[i].max_seq;
1085 state->stats.cycles = pl_test_dat[i].cycles;
1086
1087 packets_rx->current = pl_test_dat[i].packets;
1088 calc_loss(conn, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001089
Philipp Maier87bd9be2017-08-22 16:35:41 +02001090 if (loss != pl_test_dat[i].loss
1091 || expected != pl_test_dat[i].expected) {
1092 printf
1093 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
1094 i, loss, pl_test_dat[i].loss, expected,
1095 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001096 }
Philipp Maiercede2a42018-07-03 14:14:21 +02001097
1098 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001099 }
Philipp Maiercede2a42018-07-03 14:14:21 +02001100
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001101}
1102
Philipp Maier87bd9be2017-08-22 16:35:41 +02001103int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
1104 uint32_t *pr, uint32_t *_or, int *loss,
1105 uint32_t *jitter)
1106{
1107 char *line, *save;
1108 int rc;
1109
1110 /* initialize with bad values */
1111 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
1112 *loss = INT_MAX;
1113
1114 line = strtok_r((char *)msg->l2h, "\r\n", &save);
1115 if (!line)
1116 return -1;
1117
1118 /* this can only parse the message that is created above... */
1119 for_each_non_empty_line(line, save) {
1120 switch (line[0]) {
1121 case 'P':
1122 rc = sscanf(line,
1123 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
1124 ps, os, pr, _or, loss, jitter);
1125 return rc == 6 ? 0 : -1;
1126 }
1127 }
1128
1129 return -1;
1130}
1131
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001132static void test_mgcp_stats(void)
1133{
1134 printf("Testing stat parsing\n");
1135
1136 uint32_t bps, bos, pr, _or, jitter;
1137 struct msgb *msg;
1138 int loss;
1139 int rc;
1140
Philipp Maierffd75e42017-11-22 11:44:50 +01001141 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001142 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1143 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001144 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
1145 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001146 printf("FAIL: Parsing failed1.\n");
1147 msgb_free(msg);
1148
Philipp Maier87bd9be2017-08-22 16:35:41 +02001149 msg =
1150 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +01001151 ("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 +02001152 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1153 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001154 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
1155 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001156 printf("FAIL: Parsing failed2.\n");
1157 msgb_free(msg);
1158}
1159
1160struct rtp_packet_info {
1161 float txtime;
1162 int len;
1163 char *data;
1164};
1165
1166struct rtp_packet_info test_rtp_packets1[] = {
1167 /* RTP: SeqNo=0, TS=0 */
1168 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
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=1, TS=160 */
1171 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
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=2, TS=320 */
1174 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001175 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001176 /* Repeat RTP timestamp: */
1177 /* RTP: SeqNo=3, TS=320 */
1178 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001179 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001180 /* RTP: SeqNo=4, TS=480 */
1181 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001182 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001183 /* RTP: SeqNo=5, TS=640 */
1184 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001185 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001186 /* Double skip RTP timestamp (delta = 2*160): */
1187 /* RTP: SeqNo=6, TS=960 */
1188 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001189 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001190 /* RTP: SeqNo=7, TS=1120 */
1191 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001192 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001193 /* RTP: SeqNo=8, TS=1280 */
1194 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001195 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001196 /* Non 20ms RTP timestamp (delta = 120): */
1197 /* RTP: SeqNo=9, TS=1400 */
1198 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001199 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001200 /* RTP: SeqNo=10, TS=1560 */
1201 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001202 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001203 /* RTP: SeqNo=11, TS=1720 */
1204 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001205 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001206 /* SSRC changed to 0x10203040, RTP timestamp jump */
1207 /* RTP: SeqNo=12, TS=34688 */
1208 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001209 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001210 /* RTP: SeqNo=13, TS=34848 */
1211 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001212 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001213 /* RTP: SeqNo=14, TS=35008 */
1214 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001215 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001216 /* Non 20ms RTP timestamp (delta = 120): */
1217 /* RTP: SeqNo=15, TS=35128 */
1218 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001219 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001220 /* RTP: SeqNo=16, TS=35288 */
1221 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001222 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001223 /* RTP: SeqNo=17, TS=35448 */
1224 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001226 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1227 /* RTP: SeqNo=19, TS=35768 */
1228 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001229 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001230 /* RTP: SeqNo=20, TS=35928 */
1231 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001232 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001233 /* RTP: SeqNo=21, TS=36088 */
1234 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001235 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001236 /* Repeat last packet */
1237 /* RTP: SeqNo=21, TS=36088 */
1238 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001239 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001240 /* RTP: SeqNo=22, TS=36248 */
1241 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001242 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001243 /* RTP: SeqNo=23, TS=36408 */
1244 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001245 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001246 /* Don't increment SeqNo but increment timestamp by 160 */
1247 /* RTP: SeqNo=23, TS=36568 */
1248 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001249 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001250 /* RTP: SeqNo=24, TS=36728 */
1251 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001253 /* RTP: SeqNo=25, TS=36888 */
1254 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001255 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001256 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1257 * SeqNo jump */
1258 /* RTP: SeqNo=1000, TS=160000 */
1259 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001260 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001261 /* RTP: SeqNo=1001, TS=160160 */
1262 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001263 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001264 /* RTP: SeqNo=1002, TS=160320 */
1265 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001266 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001267};
1268
Philipp Maier87bd9be2017-08-22 16:35:41 +02001269void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1270 struct mgcp_rtp_state *state,
1271 struct mgcp_rtp_end *rtp_end,
1272 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001273
1274static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1275{
1276 int i;
1277
Philipp Maier14b27a82020-06-02 20:15:30 +02001278 struct mgcp_trunk trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001279 struct mgcp_endpoint endp;
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001280 struct mgcp_endpoint *endpoints[1];
Oliver Smithe36b7752019-01-22 16:31:36 +01001281 struct mgcp_config cfg = {0};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001282 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001283 struct mgcp_rtp_end *rtp;
1284 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001285 char buffer[4096];
1286 uint32_t last_ssrc = 0;
1287 uint32_t last_timestamp = 0;
1288 uint32_t last_seqno = 0;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001289 uint64_t last_in_ts_err_cnt = 0;
1290 uint64_t last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001291 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001292 struct mgcp_conn *_conn = NULL;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001293 struct rate_ctr test_ctr_in;
1294 struct rate_ctr test_ctr_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001295
1296 printf("Testing packet error detection%s%s.\n",
1297 patch_ssrc ? ", patch SSRC" : "",
1298 patch_ts ? ", patch timestamps" : "");
1299
1300 memset(&trunk, 0, sizeof(trunk));
1301 memset(&endp, 0, sizeof(endp));
1302 memset(&state, 0, sizeof(state));
1303
Philipp Maier9e1d1642018-05-09 16:26:34 +02001304 memset(&test_ctr_in, 0, sizeof(test_ctr_in));
1305 memset(&test_ctr_out, 0, sizeof(test_ctr_out));
1306 state.in_stream.err_ts_ctr = &test_ctr_in;
1307 state.out_stream.err_ts_ctr = &test_ctr_out;
1308
Oliver Smithe36b7752019-01-22 16:31:36 +01001309 endp.cfg = &cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001310 endp.type = &ep_typeset.rtp;
1311
Philipp Maierfcd06552017-11-10 17:32:22 +01001312 trunk.vty_number_endpoints = 1;
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001313 trunk.endpoints = endpoints;
1314 trunk.endpoints[0] = &endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001315 trunk.force_constant_ssrc = patch_ssrc;
1316 trunk.force_aligned_timing = patch_ts;
1317
Philipp Maier14b27a82020-06-02 20:15:30 +02001318 endp.trunk = &trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001319
Philipp Maier87bd9be2017-08-22 16:35:41 +02001320 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001321 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1322 "test-connection");
1323 OSMO_ASSERT(_conn);
1324 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001325 OSMO_ASSERT(conn);
1326
1327 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001328
Philipp Maier228e5912019-03-05 13:56:59 +01001329 OSMO_ASSERT(mgcp_codec_add(conn, PTYPE_UNDEFINED, "AMR/8000/1", NULL) == 0);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001330 rtp->codec = &rtp->codecs[0];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001331
1332 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1333 struct rtp_packet_info *info = test_rtp_packets1 + i;
1334
1335 force_monotonic_time_us = round(1000000.0 * info->txtime);
1336
1337 OSMO_ASSERT(info->len <= sizeof(buffer));
1338 OSMO_ASSERT(info->len >= 0);
1339 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001340 mgcp_rtp_end_config(&endp, 1, rtp);
1341
1342 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1343 buffer, info->len);
1344
1345 if (state.out_stream.ssrc != last_ssrc) {
1346 printf("Output SSRC changed to %08x\n",
1347 state.out_stream.ssrc);
1348 last_ssrc = state.out_stream.ssrc;
1349 }
1350
1351 printf("In TS: %d, dTS: %d, Seq: %d\n",
1352 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001353 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001354
1355 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
Philipp Maier9e1d1642018-05-09 16:26:34 +02001356 "TS Err change: in +%u, out +%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001357 state.out_stream.last_timestamp - last_timestamp,
1358 state.out_stream.last_tsdelta,
1359 state.out_stream.last_seq - last_seqno,
Philipp Maier9e1d1642018-05-09 16:26:34 +02001360 (unsigned int) (state.in_stream.err_ts_ctr->current - last_in_ts_err_cnt),
1361 (unsigned int) (state.out_stream.err_ts_ctr->current - last_out_ts_err_cnt));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001362
1363 printf("Stats: Jitter = %u, Transit = %d\n",
Harald Welte49e3d5a2017-12-25 09:47:57 +01001364 calc_jitter(&state), state.stats.transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001365
Philipp Maier9e1d1642018-05-09 16:26:34 +02001366 last_in_ts_err_cnt = state.in_stream.err_ts_ctr->current;
1367 last_out_ts_err_cnt = state.out_stream.err_ts_ctr->current;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001368 last_timestamp = state.out_stream.last_timestamp;
1369 last_seqno = state.out_stream.last_seq;
1370 }
1371
1372 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001373 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001374}
1375
1376static void test_multilple_codec(void)
1377{
1378 struct mgcp_config *cfg;
Philipp Maier14b27a82020-06-02 20:15:30 +02001379 struct mgcp_trunk *trunk2;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001380 struct mgcp_endpoint *endp;
1381 struct msgb *inp, *resp;
1382 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001383 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001384 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001385
1386 printf("Testing multiple payload types\n");
1387
1388 cfg = mgcp_config_alloc();
Harald Weltec39b1bf2020-03-08 11:29:39 +01001389 cfg->virt_trunk->vty_number_endpoints = 64;
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001390 mgcp_trunk_alloc_endpts(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001391 cfg->policy_cb = mgcp_test_policy_cb;
Pau Espin Pedrold071a302019-09-19 17:39:31 +02001392
Harald Weltec39b1bf2020-03-08 11:29:39 +01001393 trunk2 = mgcp_trunk_alloc(cfg, MGCP_TRUNK_E1, 1);
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001394 mgcp_trunk_alloc_endpts(trunk2);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001395
1396 /* Allocate endpoint 1@mgw with two codecs */
1397 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001398 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001399 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001400 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1401 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001402 msgb_free(inp);
1403 msgb_free(resp);
1404
1405 OSMO_ASSERT(last_endpoint == 1);
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001406 endp = cfg->virt_trunk->endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001407 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001408 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001409 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001410
1411 /* Allocate 2@mgw with three codecs, last one ignored */
1412 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001413 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001414 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001415 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1416 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001417 msgb_free(inp);
1418 msgb_free(resp);
1419
1420 OSMO_ASSERT(last_endpoint == 2);
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001421 endp = cfg->virt_trunk->endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001422 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001423 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001424 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001425
Philipp Maierbc0346e2018-06-07 09:52:16 +02001426 /* Allocate 3@mgw with no codecs, check for PT == 0 */
1427 /* Note: It usually makes no sense to leave the payload type list
1428 * out. However RFC 2327 does not clearly forbid this case and
1429 * it makes and since we already decided in OS#2658 that a missing
1430 * LCO should pick a sane default codec, it makes sense to expect
1431 * the same behaviour if SDP lacks proper payload type information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001432 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001433 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001434 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001435 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1436 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001437 msgb_free(inp);
1438 msgb_free(resp);
1439
1440 OSMO_ASSERT(last_endpoint == 3);
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001441 endp = cfg->virt_trunk->endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001442 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001443 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001444 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001445
1446 /* Allocate 4@mgw with a single codec */
1447 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001448 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001449 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001450 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1451 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001452 msgb_free(inp);
1453 msgb_free(resp);
1454
1455 OSMO_ASSERT(last_endpoint == 4);
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001456 endp = cfg->virt_trunk->endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001457 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001458 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001459 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001460
1461 /* Allocate 5@mgw at select GSM.. */
1462 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001463 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Harald Weltec39b1bf2020-03-08 11:29:39 +01001464 talloc_free(cfg->virt_trunk->audio_name);
1465 cfg->virt_trunk->audio_name = "GSM/8000";
1466 cfg->virt_trunk->no_audio_transcoding = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001467 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001468 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1469 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001470 msgb_free(inp);
1471 msgb_free(resp);
1472
1473 OSMO_ASSERT(last_endpoint == 5);
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001474 endp = cfg->virt_trunk->endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001475 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001476 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001477 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001478
Philipp Maierffd75e42017-11-22 11:44:50 +01001479 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001480 last_endpoint = -1;
1481 resp = mgcp_handle_message(cfg, inp);
1482 msgb_free(inp);
1483 msgb_free(resp);
1484 OSMO_ASSERT(last_endpoint == 5);
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001485 endp = cfg->virt_trunk->endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001486 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001487 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001488 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001489 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001490 memset(&addr, 0, sizeof(addr));
1491 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001492 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001493
1494 /* Check what happens without that flag */
1495
Philipp Maier87bd9be2017-08-22 16:35:41 +02001496 /* Free the previous endpoint and the data and
1497 * check if the connection really vanished... */
Philipp Maier1355d7e2018-02-01 14:30:06 +01001498 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001499 talloc_free(endp->last_response);
1500 talloc_free(endp->last_trans);
1501 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001502 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001503 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001504
1505 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001506 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Harald Weltec39b1bf2020-03-08 11:29:39 +01001507 cfg->virt_trunk->no_audio_transcoding = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001508 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001509 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1510 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001511 msgb_free(inp);
1512 msgb_free(resp);
1513
1514 OSMO_ASSERT(last_endpoint == 5);
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001515 endp = cfg->virt_trunk->endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001516 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001517 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001518 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001519
Pau Espin Pedrold071a302019-09-19 17:39:31 +02001520 mgcp_endpoints_release(trunk2);
Harald Weltec39b1bf2020-03-08 11:29:39 +01001521 mgcp_endpoints_release(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001522 talloc_free(cfg);
1523}
1524
1525static void test_no_cycle(void)
1526{
1527 struct mgcp_config *cfg;
1528 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001529 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001530 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001531
1532 printf("Testing no sequence flow on initial packet\n");
1533
1534 cfg = mgcp_config_alloc();
Harald Weltec39b1bf2020-03-08 11:29:39 +01001535 cfg->virt_trunk->vty_number_endpoints = 64;
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001536 mgcp_trunk_alloc_endpts(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001537
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001538 endp = cfg->virt_trunk->endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001539
Philipp Maierffd75e42017-11-22 11:44:50 +01001540 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1541 "test-connection");
1542 OSMO_ASSERT(_conn);
1543 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001544 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001545
Harald Welte49e3d5a2017-12-25 09:47:57 +01001546 OSMO_ASSERT(conn->state.stats.initialized == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001547
1548 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001549 OSMO_ASSERT(conn->state.stats.initialized == 1);
1550 OSMO_ASSERT(conn->state.stats.cycles == 0);
1551 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001552
1553 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001554 OSMO_ASSERT(conn->state.stats.initialized == 1);
1555 OSMO_ASSERT(conn->state.stats.cycles == 0);
1556 OSMO_ASSERT(conn->state.stats.max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001557
1558 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001559 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001560 OSMO_ASSERT(conn->state.stats.initialized == 1);
1561 OSMO_ASSERT(conn->state.stats.cycles == 0);
1562 OSMO_ASSERT(conn->state.stats.max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001563
1564 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001565 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001566 OSMO_ASSERT(conn->state.stats.initialized == 1);
1567 OSMO_ASSERT(conn->state.stats.cycles == UINT16_MAX + 1);
1568 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001569
Harald Weltec39b1bf2020-03-08 11:29:39 +01001570 mgcp_endpoints_release(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001571 talloc_free(cfg);
1572}
1573
1574static void test_no_name(void)
1575{
Philipp Maier14b27a82020-06-02 20:15:30 +02001576 struct mgcp_trunk *trunk2;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001577 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001578 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001579
1580 printf("Testing no rtpmap name\n");
1581 cfg = mgcp_config_alloc();
1582
Harald Weltec39b1bf2020-03-08 11:29:39 +01001583 cfg->virt_trunk->vty_number_endpoints = 64;
1584 cfg->virt_trunk->audio_send_name = 0;
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001585 mgcp_trunk_alloc_endpts(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001586
1587 cfg->policy_cb = mgcp_test_policy_cb;
1588
Harald Weltec39b1bf2020-03-08 11:29:39 +01001589 trunk2 = mgcp_trunk_alloc(cfg, MGCP_TRUNK_E1, 1);
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001590 mgcp_trunk_alloc_endpts(trunk2);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001591
Philipp Maierffd75e42017-11-22 11:44:50 +01001592 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001593 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001594
1595 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001596 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001597 (char *)msg->data);
1598 OSMO_ASSERT(false);
1599 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001600 msgb_free(inp);
1601 msgb_free(msg);
1602
Pau Espin Pedrold071a302019-09-19 17:39:31 +02001603 mgcp_endpoints_release(trunk2);
Harald Weltec39b1bf2020-03-08 11:29:39 +01001604 mgcp_endpoints_release(cfg->virt_trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001605 talloc_free(cfg);
1606}
1607
1608static void test_osmux_cid(void)
1609{
1610 int id, i;
1611
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +02001612 OSMO_ASSERT(osmux_cid_pool_count_used() == 0);
1613
1614 id = osmux_cid_pool_get_next();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001615 OSMO_ASSERT(id == 0);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +02001616 OSMO_ASSERT(osmux_cid_pool_count_used() == 1);
1617
1618 osmux_cid_pool_get(30);
1619 OSMO_ASSERT(osmux_cid_pool_count_used() == 2);
1620 osmux_cid_pool_get(30);
1621 OSMO_ASSERT(osmux_cid_pool_count_used() == 2);
1622
1623 osmux_cid_pool_put(id);
1624 OSMO_ASSERT(osmux_cid_pool_count_used() == 1);
1625 osmux_cid_pool_put(30);
1626 OSMO_ASSERT(osmux_cid_pool_count_used() == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001627
1628 for (i = 0; i < 256; ++i) {
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +02001629 id = osmux_cid_pool_get_next();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001630 OSMO_ASSERT(id == i);
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +02001631 OSMO_ASSERT(osmux_cid_pool_count_used() == i + 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001632 }
1633
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +02001634 id = osmux_cid_pool_get_next();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001635 OSMO_ASSERT(id == -1);
1636
1637 for (i = 0; i < 256; ++i)
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +02001638 osmux_cid_pool_put(i);
1639 OSMO_ASSERT(osmux_cid_pool_count_used() == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001640}
1641
1642static const struct log_info_cat log_categories[] = {
1643};
1644
1645const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001646 .cat = log_categories,
1647 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001648};
1649
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001650static void test_get_lco_identifier(void)
1651{
1652 char *test;
1653 printf("Testing get_lco_identifier()\n");
1654
1655 /* Normal case at the beginning */
1656 test = "p:10, a:PCMU";
1657 printf("%s -> %s\n", test, get_lco_identifier(test));
1658
1659 test = "p:10, a:PCMU";
1660 printf("%s -> %s\n", test, get_lco_identifier(test));
1661
1662 /* Begin parsing in the middle of the value part of
1663 * the previous LCO option value */
1664 test = "XXXX, p:10, a:PCMU";
1665 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1666
1667 test = "XXXX,p:10,a:PCMU";
1668 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1669
1670 test = "10,a:PCMU";
1671 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1672
1673 test = "10, a:PCMU";
1674 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1675
1676 test = "10,a: PCMU";
1677 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1678
1679 test = "10 ,a: PCMU";
1680 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1681
1682 /* Begin parsing right at the end of the previous LCO
1683 * option value */
1684 test = ", a:PCMU";
1685 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1686
1687 test = " a:PCMU";
1688 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1689
1690 /* Empty string, result should be (null) */
1691 test = "";
1692 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1693
1694 /* Missing colons, result should be (null) */
1695 test = "p10, aPCMU";
1696 printf("%s -> %s\n", test, get_lco_identifier(test));
1697
1698 /* Colon separated from the identifier, result should be (null) */
1699 test = "10,a :PCMU";
1700 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1701}
1702
1703static void test_check_local_cx_options(void *ctx)
1704{
1705 /* Legal cases */
1706 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1707 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU") == 0);
1708 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p:10, IN:10") == 0);
1709 OSMO_ASSERT(check_local_cx_options(ctx, "one:AAA, two:BB, tree:C") ==
1710 0);
1711 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1712 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:G726-32") == 0);
1713 OSMO_ASSERT(check_local_cx_options(ctx, "p:10-20, b:64") == 0);
1714 OSMO_ASSERT(check_local_cx_options(ctx, "b:32-64, e:off") == 0);
1715 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU;G726-32") == 0);
1716
1717 /* Illegal spaces before and after colon */
1718 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p :10") == -1);
1719 OSMO_ASSERT(check_local_cx_options(ctx, "a :PCMU, p:10") == -1);
1720 OSMO_ASSERT(check_local_cx_options(ctx, "p: 10, a:PCMU") == -1);
1721
1722 /* Check if multiple appearances of LCOs are rejected */
1723 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, p:10") == -1);
1724 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, a:PCMU, p:10") ==
1725 -1);
1726 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, p:10") == -1);
1727
1728 /* Check if empty LCO are rejected */
1729 OSMO_ASSERT(check_local_cx_options(ctx, "p: , a:PCMU") == -1);
1730 OSMO_ASSERT(check_local_cx_options(ctx, "p: , a: PCMU") == -1);
1731 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a: PCMU") == -1);
1732 OSMO_ASSERT(check_local_cx_options(ctx, "p:, a:PCMU") == -1);
1733 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:") == -1);
1734
1735 /* Garbeled beginning and ends */
1736 OSMO_ASSERT(check_local_cx_options(ctx, ":10, a:10") == -1);
1737 OSMO_ASSERT(check_local_cx_options(ctx, "10, a:PCMU") == -1);
1738 OSMO_ASSERT(check_local_cx_options(ctx, ", a:PCMU") == -1);
1739 OSMO_ASSERT(check_local_cx_options(ctx, " a:PCMU") == -1);
1740 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU,") == -1);
1741 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, ") == -1);
1742
1743 /* Illegal strings */
1744 OSMO_ASSERT(check_local_cx_options(ctx, " ") == -1);
1745 OSMO_ASSERT(check_local_cx_options(ctx, "") == -1);
1746 OSMO_ASSERT(check_local_cx_options(ctx, "AAA") == -1);
1747 OSMO_ASSERT(check_local_cx_options(ctx, ":,") == -1);
1748 OSMO_ASSERT(check_local_cx_options(ctx, ",:") == -1);
1749 OSMO_ASSERT(check_local_cx_options(ctx, ",,,") == -1);
1750}
1751
Neels Hofmeyrd2f5e692019-08-08 21:59:26 +02001752static const struct mgcp_codec_param amr_param_octet_aligned_true = {
1753 .amr_octet_aligned_present = true,
1754 .amr_octet_aligned = true,
1755};
1756
Neels Hofmeyrd2f5e692019-08-08 21:59:26 +02001757static const struct mgcp_codec_param amr_param_octet_aligned_false = {
1758 .amr_octet_aligned_present = true,
1759 .amr_octet_aligned = false,
1760};
1761
1762static const struct mgcp_codec_param amr_param_octet_aligned_unset = {
1763 .amr_octet_aligned_present = false,
1764};
Neels Hofmeyrd2f5e692019-08-08 21:59:26 +02001765
1766struct testcase_mgcp_codec_pt_translate_codec {
1767 int payload_type;
1768 const char *audio_name;
1769 const struct mgcp_codec_param *param;
1770 int expect_rc;
1771};
1772
1773struct testcase_mgcp_codec_pt_translate_expect {
1774 bool end;
1775 int payload_type_map[2];
1776};
1777
1778struct testcase_mgcp_codec_pt_translate {
1779 const char *descr;
1780 /* two conns on an endpoint, each with N configured codecs */
1781 struct testcase_mgcp_codec_pt_translate_codec codecs[2][10];
1782 struct testcase_mgcp_codec_pt_translate_expect expect[32];
1783};
1784
1785static const struct testcase_mgcp_codec_pt_translate test_mgcp_codec_pt_translate_cases[] = {
1786 {
1787 .descr = "same order, but differing payload type numbers",
1788 .codecs = {
1789 {
1790 { 112, "AMR/8000/1", &amr_param_octet_aligned_true, },
1791 { 0, "PCMU/8000/1", NULL, },
1792 { 111, "GSM-HR-08/8000/1", NULL, },
1793 },
1794 {
1795 { 96, "AMR/8000/1", &amr_param_octet_aligned_true, },
1796 { 0, "PCMU/8000/1", NULL, },
1797 { 97, "GSM-HR-08/8000/1", NULL, },
1798 },
1799 },
1800 .expect = {
1801 { .payload_type_map = {112, 96}, },
1802 { .payload_type_map = {0, 0}, },
1803 { .payload_type_map = {111, 97} },
1804 { .payload_type_map = {123, -EINVAL} },
1805 { .end = true },
1806 },
1807 },
1808 {
Neels Hofmeyr26985402019-08-08 22:39:55 +02001809 .descr = "different order and different payload type numbers",
1810 .codecs = {
1811 {
1812 { 0, "PCMU/8000/1", NULL, },
1813 { 111, "GSM-HR-08/8000/1", NULL, },
1814 { 112, "AMR/8000/1", &amr_param_octet_aligned_true, },
1815 },
1816 {
1817 { 97, "GSM-HR-08/8000/1", NULL, },
1818 { 0, "PCMU/8000/1", NULL, },
1819 { 96, "AMR/8000/1", &amr_param_octet_aligned_true, },
1820 },
1821 },
1822 .expect = {
1823 { .payload_type_map = {112, 96}, },
1824 { .payload_type_map = {0, 0}, },
1825 { .payload_type_map = {111, 97} },
1826 { .payload_type_map = {123, -EINVAL} },
1827 { .end = true },
1828 },
1829 },
1830 {
1831 .descr = "both sides have the same payload_type numbers assigned to differing codecs",
1832 .codecs = {
1833 {
1834 { 0, "PCMU/8000/1", NULL, },
1835 { 96, "GSM-HR-08/8000/1", NULL, },
1836 { 97, "AMR/8000/1", &amr_param_octet_aligned_true, },
1837 },
1838 {
1839 { 97, "GSM-HR-08/8000/1", NULL, },
1840 { 0, "PCMU/8000/1", NULL, },
1841 { 96, "AMR/8000/1", &amr_param_octet_aligned_true, },
1842 },
1843 },
1844 .expect = {
1845 { .payload_type_map = {96, 97}, },
1846 { .payload_type_map = {97, 96}, },
1847 { .payload_type_map = {0, 0}, },
1848 { .end = true },
1849 },
1850 },
1851 {
Neels Hofmeyrd2f5e692019-08-08 21:59:26 +02001852 .descr = "conn0 has no codecs",
1853 .codecs = {
1854 {
1855 /* no codecs */
1856 },
1857 {
1858 { 96, "AMR/8000/1", &amr_param_octet_aligned_true, },
1859 { 0, "PCMU/8000/1", NULL, },
1860 { 97, "GSM-HR-08/8000/1", NULL, },
1861 },
1862 },
1863 .expect = {
1864 { .payload_type_map = {112, -EINVAL}, },
1865 { .payload_type_map = {0, -EINVAL}, },
1866 { .payload_type_map = {111, -EINVAL} },
1867 { .end = true },
1868 },
1869 },
1870 {
1871 .descr = "conn1 has no codecs",
1872 .codecs = {
1873 {
1874 { 112, "AMR/8000/1", &amr_param_octet_aligned_true, },
1875 { 0, "PCMU/8000/1", NULL, },
1876 { 111, "GSM-HR-08/8000/1", NULL, },
1877 },
1878 {
1879 /* no codecs */
1880 },
1881 },
1882 .expect = {
1883 { .payload_type_map = {112, -EINVAL}, },
1884 { .payload_type_map = {0, -EINVAL}, },
1885 { .payload_type_map = {111, -EINVAL} },
1886 { .end = true },
1887 },
1888 },
Neels Hofmeyr16b637b2019-08-08 22:47:10 +02001889 {
1890 .descr = "test AMR with differing octet-aligned settings",
1891 .codecs = {
1892 {
1893 { 111, "AMR/8000", &amr_param_octet_aligned_true, },
1894 { 112, "AMR/8000", &amr_param_octet_aligned_false, },
1895 },
1896 {
1897 { 122, "AMR/8000", &amr_param_octet_aligned_false, },
1898 { 121, "AMR/8000", &amr_param_octet_aligned_true, },
1899 },
1900 },
1901 .expect = {
1902 { .payload_type_map = {111, 121}, },
1903 { .payload_type_map = {112, 122} },
1904 { .end = true },
1905 },
1906 },
1907 {
1908 .descr = "test AMR with missing octet-aligned settings (defaults to 0)",
1909 .codecs = {
1910 {
1911 { 111, "AMR/8000", &amr_param_octet_aligned_true, },
1912 { 112, "AMR/8000", &amr_param_octet_aligned_false, },
1913 },
1914 {
1915 { 122, "AMR/8000", &amr_param_octet_aligned_unset, },
1916 },
1917 },
1918 .expect = {
1919 { .payload_type_map = {111, -EINVAL}, },
1920 { .payload_type_map = {112, 122} },
1921 { .end = true },
1922 },
1923 },
1924 {
1925 .descr = "test AMR with NULL param (defaults to 0)",
1926 .codecs = {
1927 {
1928 { 111, "AMR/8000", &amr_param_octet_aligned_true, },
1929 { 112, "AMR/8000", &amr_param_octet_aligned_false, },
1930 },
1931 {
1932 { 122, "AMR/8000", NULL, },
1933 },
1934 },
1935 .expect = {
1936 { .payload_type_map = {111, -EINVAL}, },
1937 { .payload_type_map = {112, 122} },
1938 { .end = true },
1939 },
1940 },
Neels Hofmeyr683e05f2019-08-08 22:48:18 +02001941 {
1942 .descr = "match FOO/8000/1 and FOO/8000 as identical, single channel is implicit",
1943 .codecs = {
1944 {
1945 { 0, "PCMU/8000/1", NULL, },
1946 { 111, "GSM-HR-08/8000/1", NULL, },
1947 { 112, "AMR/8000/1", &amr_param_octet_aligned_true, },
1948 },
1949 {
1950 { 97, "GSM-HR-08/8000", NULL, },
1951 { 0, "PCMU/8000", NULL, },
1952 { 96, "AMR/8000", &amr_param_octet_aligned_true, },
1953 },
1954 },
1955 .expect = {
1956 { .payload_type_map = {112, 96}, },
1957 { .payload_type_map = {0, 0}, },
1958 { .payload_type_map = {111, 97} },
1959 { .payload_type_map = {123, -EINVAL} },
1960 { .end = true },
1961 },
1962 },
1963 {
1964 .descr = "match FOO/8000/1 and FOO as identical, 8k and single channel are implicit",
1965 .codecs = {
1966 {
1967 { 0, "PCMU/8000/1", NULL, },
1968 { 111, "GSM-HR-08/8000/1", NULL, },
1969 { 112, "AMR/8000/1", &amr_param_octet_aligned_true, },
1970 },
1971 {
1972 { 97, "GSM-HR-08", NULL, },
1973 { 0, "PCMU", NULL, },
1974 { 96, "AMR", &amr_param_octet_aligned_true, },
1975 },
1976 },
1977 .expect = {
1978 { .payload_type_map = {112, 96}, },
1979 { .payload_type_map = {0, 0}, },
1980 { .payload_type_map = {111, 97} },
1981 { .payload_type_map = {123, -EINVAL} },
1982 { .end = true },
1983 },
1984 },
1985 {
1986 .descr = "test whether channel number matching is waterproof",
1987 .codecs = {
1988 {
1989 { 111, "GSM-HR-08/8000", },
1990 { 112, "GSM-HR-08/8000/2", .expect_rc = -22},
1991 { 113, "GSM-HR-08/8000/3", .expect_rc = -22},
1992 },
1993 {
1994 { 122, "GSM-HR-08/8000/2", .expect_rc = -22},
1995 { 121, "GSM-HR-08/8000/1", },
1996 },
1997 },
1998 .expect = {
1999 { .payload_type_map = {111, 121}, },
2000 { .payload_type_map = {112, -EINVAL} },
2001 { .payload_type_map = {113, -EINVAL} },
2002 { .end = true },
2003 },
2004 },
Neels Hofmeyrd2f5e692019-08-08 21:59:26 +02002005};
Philipp Maier6931f9a2018-07-26 09:29:31 +02002006
2007static void test_mgcp_codec_pt_translate(void)
2008{
Neels Hofmeyrd2f5e692019-08-08 21:59:26 +02002009 int i;
2010 bool ok = true;
2011 printf("\nTesting mgcp_codec_pt_translate()\n");
Philipp Maier6931f9a2018-07-26 09:29:31 +02002012
Neels Hofmeyrd2f5e692019-08-08 21:59:26 +02002013 for (i = 0; i < ARRAY_SIZE(test_mgcp_codec_pt_translate_cases); i++) {
2014 const struct testcase_mgcp_codec_pt_translate *t = &test_mgcp_codec_pt_translate_cases[i];
2015 struct mgcp_conn_rtp conn[2] = {};
2016 int rc;
2017 int conn_i;
2018 int c;
Philipp Maier6931f9a2018-07-26 09:29:31 +02002019
Neels Hofmeyrd2f5e692019-08-08 21:59:26 +02002020 printf("#%d: %s\n", i, t->descr);
Philipp Maier6931f9a2018-07-26 09:29:31 +02002021
Neels Hofmeyrd2f5e692019-08-08 21:59:26 +02002022 for (conn_i = 0; conn_i < 2; conn_i++) {
2023 printf(" - add codecs on conn%d:\n", conn_i);
2024 for (c = 0; c < ARRAY_SIZE(t->codecs[conn_i]); c++) {
2025 const struct testcase_mgcp_codec_pt_translate_codec *codec = &t->codecs[conn_i][c];
2026 if (!codec->audio_name)
2027 break;
2028
2029 rc = mgcp_codec_add(&conn[conn_i], codec->payload_type, codec->audio_name, codec->param);
2030
2031 printf(" %2d: %3d %s%s -> rc=%d\n", c, codec->payload_type, codec->audio_name,
2032 codec->param ?
2033 (codec->param->amr_octet_aligned_present?
2034 (codec->param->amr_octet_aligned ?
2035 " octet-aligned=1" : " octet-aligned=0")
2036 : " octet-aligned=unset")
2037 : "",
2038 rc);
2039 if (rc != codec->expect_rc) {
2040 printf(" ERROR: expected rc=%d\n", codec->expect_rc);
2041 ok = false;
2042 }
2043 }
2044 if (!c)
2045 printf(" (none)\n");
2046 }
2047
2048 for (c = 0; c < ARRAY_SIZE(t->expect); c++) {
2049 const struct testcase_mgcp_codec_pt_translate_expect *expect = &t->expect[c];
2050 int result;
2051
2052 if (expect->end)
2053 break;
2054
2055 result = mgcp_codec_pt_translate(&conn[0], &conn[1], expect->payload_type_map[0]);
2056 printf(" - mgcp_codec_pt_translate(conn0, conn1, %d) -> %d\n",
2057 expect->payload_type_map[0], result);
2058 if (result != expect->payload_type_map[1]) {
2059 printf(" ERROR: expected -> %d\n", expect->payload_type_map[1]);
2060 ok = false;
2061 }
2062
2063 /* If the expected result is an error, don't do reverse map test */
2064 if (expect->payload_type_map[1] < 0)
2065 continue;
2066
2067 result = mgcp_codec_pt_translate(&conn[1], &conn[0], expect->payload_type_map[1]);
2068 printf(" - mgcp_codec_pt_translate(conn1, conn0, %d) -> %d\n",
2069 expect->payload_type_map[1], result);
2070 if (result != expect->payload_type_map[0]) {
2071 printf(" ERROR: expected -> %d\n", expect->payload_type_map[0]);
2072 ok = false;
2073 }
2074 }
2075
2076 for (conn_i = 0; conn_i < 2; conn_i++)
2077 mgcp_codec_reset_all(&conn[conn_i]);
2078 }
2079
2080 OSMO_ASSERT(ok);
Philipp Maier6931f9a2018-07-26 09:29:31 +02002081}
2082
Neels Hofmeyr65317262018-09-03 22:11:05 +02002083void test_conn_id_matching()
2084{
2085 struct mgcp_endpoint endp = {};
2086 struct mgcp_conn *conn;
2087 struct mgcp_conn *conn_match;
2088 int i;
2089 const char *conn_id_generated = "000023AB";
2090 const char *conn_id_request[] = {
Neels Hofmeyra77eade2018-08-29 02:30:39 +02002091 "23AB",
2092 "0023AB",
Neels Hofmeyr65317262018-09-03 22:11:05 +02002093 "000023AB",
Neels Hofmeyra77eade2018-08-29 02:30:39 +02002094 "00000023AB",
2095 "23ab",
2096 "0023ab",
Neels Hofmeyr65317262018-09-03 22:11:05 +02002097 "000023ab",
Neels Hofmeyra77eade2018-08-29 02:30:39 +02002098 "00000023ab",
Neels Hofmeyr65317262018-09-03 22:11:05 +02002099 };
2100
2101 printf("\nTesting %s\n", __func__);
2102
2103 INIT_LLIST_HEAD(&endp.conns);
2104
2105 conn = talloc_zero(NULL, struct mgcp_conn);
2106 OSMO_ASSERT(conn);
2107 osmo_strlcpy(conn->id, conn_id_generated, sizeof(conn->id));
2108 llist_add(&conn->entry, &endp.conns);
2109
2110 for (i = 0; i < ARRAY_SIZE(conn_id_request); i++) {
2111 const char *needle = conn_id_request[i];
2112 printf("needle='%s' ", needle);
2113 conn_match = mgcp_conn_get(&endp, needle);
2114 OSMO_ASSERT(conn_match);
2115 printf("found '%s'\n", conn_match->id);
2116 OSMO_ASSERT(conn_match == conn);
2117 }
2118
2119 llist_del(&conn->entry);
2120 talloc_free(conn);
2121}
2122
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02002123int main(int argc, char **argv)
2124{
Neels Hofmeyr60f8e312018-03-30 23:01:07 +02002125 void *ctx = talloc_named_const(NULL, 0, "mgcp_test");
2126 void *msgb_ctx = msgb_talloc_ctx_init(ctx, 0);
2127 osmo_init_logging2(ctx, &log_info);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02002128
2129 test_strline();
2130 test_values();
2131 test_messages();
2132 test_retransmission();
2133 test_packet_loss_calc();
2134 test_rqnt_cb();
2135 test_mgcp_stats();
2136 test_packet_error_detection(1, 0);
2137 test_packet_error_detection(0, 0);
2138 test_packet_error_detection(0, 1);
2139 test_packet_error_detection(1, 1);
2140 test_multilple_codec();
2141 test_no_cycle();
2142 test_no_name();
2143 test_osmux_cid();
Philipp Maier3d7b58d2018-06-06 09:35:31 +02002144 test_get_lco_identifier();
2145 test_check_local_cx_options(ctx);
Philipp Maier6931f9a2018-07-26 09:29:31 +02002146 test_mgcp_codec_pt_translate();
Neels Hofmeyr65317262018-09-03 22:11:05 +02002147 test_conn_id_matching();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02002148
Neels Hofmeyr465446b2017-11-18 21:26:26 +01002149 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
2150 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
2151 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02002152 printf("Done\n");
2153 return EXIT_SUCCESS;
2154}