blob: ea7948513c1095adb442096b4641565e168a5609 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/*
2 * (C) 2011-2012,2014 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2011-2012,2014 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#undef _GNU_SOURCE
20#define _GNU_SOURCE
21
Philipp Maier87bd9be2017-08-22 16:35:41 +020022#include <osmocom/mgcp/mgcp.h>
23#include <osmocom/mgcp/vty.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020024#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020025#include <osmocom/mgcp/mgcp_internal.h>
26#include <osmocom/mgcp/mgcp_stat.h>
27#include <osmocom/mgcp/mgcp_msg.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010028#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020029#include <osmocom/mgcp/mgcp_sdp.h>
30#include <osmocom/mgcp/mgcp_codec.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020031
32#include <osmocom/core/application.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/utils.h>
35#include <string.h>
36#include <limits.h>
37#include <dlfcn.h>
38#include <time.h>
39#include <math.h>
40
41char *strline_r(char *str, char **saveptr);
42
43const char *strline_test_data =
44 "one CR\r"
45 "two CR\r"
46 "\r"
47 "one CRLF\r\n"
48 "two CRLF\r\n"
Philipp Maier87bd9be2017-08-22 16:35:41 +020049 "\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 +020050
51#define EXPECTED_NUMBER_OF_LINES 13
52
53static void test_strline(void)
54{
55 char *save = NULL;
56 char *line;
57 char buf[2048];
58 int counter = 0;
59
60 osmo_strlcpy(buf, strline_test_data, sizeof(buf));
61
Philipp Maier87bd9be2017-08-22 16:35:41 +020062 for (line = mgcp_strline(buf, &save); line;
63 line = mgcp_strline(NULL, &save)) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020064 printf("line: '%s'\n", line);
65 counter++;
66 }
67
68 OSMO_ASSERT(counter == EXPECTED_NUMBER_OF_LINES);
69}
70
Philipp Maier12943ea2018-01-17 15:40:25 +010071#define AUEP1 "AUEP 158663169 ds/e1-1/2@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020072#define AUEP1_RET "200 158663169 OK\r\n"
Philipp Maier12943ea2018-01-17 15:40:25 +010073#define AUEP2 "AUEP 18983213 ds/e1-2/1@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020074#define AUEP2_RET "500 18983213 FAIL\r\n"
75#define EMPTY "\r\n"
76#define EMPTY_RET NULL
77#define SHORT "CRCX \r\n"
78#define SHORT_RET "510 000000 FAIL\r\n"
79
Philipp Maier12943ea2018-01-17 15:40:25 +010080#define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@mgw MGCP 1.0\r\n"
Harald Welteabbb6b92017-12-28 13:13:50 +010081#define MDCX_ERR_RET "500 18983213 FAIL\r\n"
Philipp Maier12943ea2018-01-17 15:40:25 +010082#define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020083#define MDCX_RET "400 18983214 FAIL\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020084
Philipp Maier87bd9be2017-08-22 16:35:41 +020085#define MDCX3 \
86 "MDCX 18983215 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010087 "I: %s\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020088
Philipp Maier87bd9be2017-08-22 16:35:41 +020089#define MDCX3_RET \
90 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +010091 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020092 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010093 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020094 "s=-\r\n" \
95 "c=IN IP4 0.0.0.0\r\n" \
96 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010097 "m=audio 16002 RTP/AVP 97\r\n" \
98 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020099 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200100
Philipp Maier87bd9be2017-08-22 16:35:41 +0200101#define MDCX3A_RET \
102 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100103 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200104 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100105 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200106 "s=-\r\n" \
107 "c=IN IP4 0.0.0.0\r\n" \
108 "t=0 0\r\n" \
109 "m=audio 16002 RTP/AVP 97\r\n" \
110 "a=rtpmap:97 GSM-EFR/8000\r\n" \
111 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200112
Philipp Maier87bd9be2017-08-22 16:35:41 +0200113#define MDCX3_FMTP_RET \
114 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100115 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200116 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100117 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200118 "s=-\r\n" \
119 "c=IN IP4 0.0.0.0\r\n" \
120 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100121 "m=audio 16006 RTP/AVP 97\r\n" \
122 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200123 "a=fmtp:126 0/1/2\r\n" \
124 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200125
Philipp Maier87bd9be2017-08-22 16:35:41 +0200126#define MDCX4 \
127 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \
128 "M: sendrecv\r" \
129 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100130 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200131 "L: p:20, a:AMR, nt:IN\r\n" \
132 "\n" \
133 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100134 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200135 "c=IN IP4 0.0.0.0\r\n" \
136 "t=0 0\r\n" \
137 "m=audio 4441 RTP/AVP 99\r\n" \
138 "a=rtpmap:99 AMR/8000\r\n" \
139 "a=ptime:40\r\n"
140
141#define MDCX4_RET(Ident) \
142 "200 " Ident " OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100143 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200144 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100145 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200146 "s=-\r\n" \
147 "c=IN IP4 0.0.0.0\r\n" \
148 "t=0 0\r\n" \
149 "m=audio 16002 RTP/AVP 99\r\n" \
150 "a=rtpmap:99 AMR/8000\r\n" \
151 "a=ptime:40\r\n"
152
153#define MDCX4_RO_RET(Ident) \
154 "200 " Ident " OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100155 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200156 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100157 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158 "s=-\r\n" \
159 "c=IN IP4 0.0.0.0\r\n" \
160 "t=0 0\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200161 "m=audio 16002 RTP/AVP 112\r\n" \
162 "a=rtpmap:112 AMR\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200163 "a=ptime:40\r\n"
164
165#define MDCX4_PT1 \
166 "MDCX 18983217 1@mgw MGCP 1.0\r\n" \
167 "M: sendrecv\r" \
168 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100169 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200170 "L: p:20-40, a:AMR, nt:IN\r\n" \
171 "\n" \
172 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100173 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174 "c=IN IP4 0.0.0.0\r\n" \
175 "t=0 0\r\n" \
176 "m=audio 4441 RTP/AVP 99\r\n" \
177 "a=rtpmap:99 AMR/8000\r\n" \
178 "a=ptime:40\r\n"
179
180#define MDCX4_PT2 \
181 "MDCX 18983218 1@mgw MGCP 1.0\r\n" \
182 "M: sendrecv\r" \
183 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100184 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200185 "L: p:20-20, a:AMR, nt:IN\r\n" \
186 "\n" \
187 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100188 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189 "c=IN IP4 0.0.0.0\r\n" \
190 "t=0 0\r\n" \
191 "m=audio 4441 RTP/AVP 99\r\n" \
192 "a=rtpmap:99 AMR/8000\r\n" \
193 "a=ptime:40\r\n"
194
195#define MDCX4_PT3 \
196 "MDCX 18983219 1@mgw MGCP 1.0\r\n" \
197 "M: sendrecv\r" \
198 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100199 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200200 "L: a:AMR, nt:IN\r\n" \
201 "\n" \
202 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100203 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200204 "c=IN IP4 0.0.0.0\r\n" \
205 "t=0 0\r\n" \
206 "m=audio 4441 RTP/AVP 99\r\n" \
207 "a=rtpmap:99 AMR/8000\r\n" \
208 "a=ptime:40\r\n"
209
210#define MDCX4_SO \
211 "MDCX 18983220 1@mgw MGCP 1.0\r\n" \
212 "M: sendonly\r" \
213 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100214 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200215 "L: p:20, a:AMR, nt:IN\r\n" \
216 "\n" \
217 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100218 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200219 "c=IN IP4 0.0.0.0\r\n" \
220 "t=0 0\r\n" \
221 "m=audio 4441 RTP/AVP 99\r\n" \
222 "a=rtpmap:99 AMR/8000\r\n" \
223 "a=ptime:40\r\n"
224
225#define MDCX4_RO \
226 "MDCX 18983221 1@mgw MGCP 1.0\r\n" \
227 "M: recvonly\r" \
228 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100229 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200230 "L: p:20, a:AMR, nt:IN\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200231
232#define SHORT2 "CRCX 1"
233#define SHORT2_RET "510 000000 FAIL\r\n"
234#define SHORT3 "CRCX 1 1@mgw"
235#define SHORT4 "CRCX 1 1@mgw MGCP"
236#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
237
Philipp Maier87bd9be2017-08-22 16:35:41 +0200238#define CRCX \
239 "CRCX 2 1@mgw MGCP 1.0\r\n" \
240 "M: recvonly\r\n" \
241 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200242 "L: p:20\r\n" \
243 "\r\n" \
244 "v=0\r\n" \
245 "c=IN IP4 123.12.12.123\r\n" \
246 "m=audio 5904 RTP/AVP 97\r\n" \
247 "a=rtpmap:97 GSM-EFR/8000\r\n" \
248 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200249
Philipp Maier87bd9be2017-08-22 16:35:41 +0200250#define CRCX_RET \
251 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100252 "I: %s\r\n" \
253 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200254 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100255 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200256 "s=-\r\n" \
257 "c=IN IP4 0.0.0.0\r\n" \
258 "t=0 0\r\n" \
259 "m=audio 16002 RTP/AVP 97\r\n" \
260 "a=rtpmap:97 GSM-EFR/8000\r\n" \
261 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200262
Philipp Maier87bd9be2017-08-22 16:35:41 +0200263#define CRCX_RET_NO_RTPMAP \
264 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100265 "I: %s\r\n" \
266 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200267 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100268 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200269 "s=-\r\n" \
270 "c=IN IP4 0.0.0.0\r\n" \
271 "t=0 0\r\n" \
272 "m=audio 16002 RTP/AVP 97\r\n" \
273 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200274
Philipp Maier87bd9be2017-08-22 16:35:41 +0200275#define CRCX_FMTP_RET \
276 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100277 "I: %s\r\n" \
278 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200279 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100280 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281 "s=-\r\n" \
282 "c=IN IP4 0.0.0.0\r\n" \
283 "t=0 0\r\n" \
284 "m=audio 16006 RTP/AVP 97\r\n" \
285 "a=rtpmap:97 GSM-EFR/8000\r\n" \
286 "a=fmtp:126 0/1/2\r\n" \
287 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200288
Philipp Maier87bd9be2017-08-22 16:35:41 +0200289#define CRCX_ZYN \
290 "CRCX 2 1@mgw MGCP 1.0\r" \
291 "M: recvonly\r" \
292 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200293 "\n" \
294 "v=0\r" \
295 "c=IN IP4 123.12.12.123\r" \
296 "m=audio 5904 RTP/AVP 97\r" \
297 "a=rtpmap:97 GSM-EFR/8000\r"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200298
Philipp Maier87bd9be2017-08-22 16:35:41 +0200299#define CRCX_ZYN_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 16004 RTP/AVP 97\r\n" \
309 "a=rtpmap:97 GSM-EFR/8000\r\n" \
310 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200311
Philipp Maier87bd9be2017-08-22 16:35:41 +0200312#define DLCX \
313 "DLCX 7 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100314 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200315 "C: 2\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200316
Philipp Maier87bd9be2017-08-22 16:35:41 +0200317#define DLCX_RET \
318 "250 7 OK\r\n" \
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100319 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n"
320
321 #define DLCX_RET_OSMUX DLCX_RET \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200322 "X-Osmo-CP: EC TI=0, TO=0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200323
Philipp Maier87bd9be2017-08-22 16:35:41 +0200324#define RQNT \
325 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
326 "X: B244F267488\r\n" \
327 "S: D/9\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200328
Philipp Maier87bd9be2017-08-22 16:35:41 +0200329#define RQNT2 \
330 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
331 "X: ADD4F26746F\r\n" \
332 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200333
334#define RQNT1_RET "200 186908780 OK\r\n"
335#define RQNT2_RET "200 186908781 OK\r\n"
336
Philipp Maier87bd9be2017-08-22 16:35:41 +0200337#define PTYPE_IGNORE 0 /* == default initializer */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200338#define PTYPE_NONE 128
339#define PTYPE_NYI PTYPE_NONE
340
Philipp Maier87bd9be2017-08-22 16:35:41 +0200341#define CRCX_MULT_1 \
342 "CRCX 2 1@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200343 "M: recvonly\r\n" \
344 "C: 2\r\n" \
345 "X\r\n" \
346 "L: p:20\r\n" \
347 "\r\n" \
348 "v=0\r\n" \
349 "c=IN IP4 123.12.12.123\r\n" \
350 "m=audio 5904 RTP/AVP 18 97\r\n" \
351 "a=rtpmap:18 G729/8000\r\n" \
352 "a=rtpmap:97 GSM-EFR/8000\r\n" \
353 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200354
Philipp Maier87bd9be2017-08-22 16:35:41 +0200355#define CRCX_MULT_2 \
356 "CRCX 2 2@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200357 "M: recvonly\r\n" \
358 "C: 2\r\n" \
359 "X\r\n" \
360 "L: p:20\r\n" \
361 "\r\n" \
362 "v=0\r\n" \
363 "c=IN IP4 123.12.12.123\r\n" \
364 "m=audio 5904 RTP/AVP 18 97 101\r\n" \
365 "a=rtpmap:18 G729/8000\r\n" \
366 "a=rtpmap:97 GSM-EFR/8000\r\n" \
367 "a=rtpmap:101 FOO/8000\r\n" \
368 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200369
Philipp Maier87bd9be2017-08-22 16:35:41 +0200370#define CRCX_MULT_3 \
371 "CRCX 2 3@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200372 "M: recvonly\r\n" \
373 "C: 2\r\n" \
374 "X\r\n" \
375 "L: p:20\r\n" \
376 "\r\n" \
377 "v=0\r\n" \
378 "c=IN IP4 123.12.12.123\r\n" \
379 "m=audio 5904 RTP/AVP\r\n" \
380 "a=rtpmap:18 G729/8000\r\n" \
381 "a=rtpmap:97 GSM-EFR/8000\r\n" \
382 "a=rtpmap:101 FOO/8000\r\n" \
383 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200384
Philipp Maier87bd9be2017-08-22 16:35:41 +0200385#define CRCX_MULT_4 \
386 "CRCX 2 4@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200387 "M: recvonly\r\n" \
388 "C: 2\r\n" \
389 "X\r\n" \
390 "L: p:20\r\n" \
391 "\r\n" \
392 "v=0\r\n" \
393 "c=IN IP4 123.12.12.123\r\n" \
394 "m=audio 5904 RTP/AVP 18\r\n" \
395 "a=rtpmap:18 G729/8000\r\n" \
396 "a=rtpmap:97 GSM-EFR/8000\r\n" \
397 "a=rtpmap:101 FOO/8000\r\n" \
398 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200399
400#define CRCX_MULT_GSM_EXACT \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200401 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
402 "C: 1355c6041e\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200403 "L: p:20, a:GSM, nt:IN\r\n" \
404 "M: recvonly\r\n" \
405 "\r\n" \
406 "v=0\r\n" \
407 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
408 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200409 "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 +0200410 "a=rtpmap:0 PCMU/8000\r\n" \
411 "a=rtpmap:8 PCMA/8000\r\n" \
412 "a=rtpmap:3 gsm/8000\r\n" \
413 "a=rtpmap:18 G729/8000\r\n" \
414 "a=fmtp:18 annexb=no\r\n" \
415 "a=rtpmap:4 G723/8000\r\n" \
416 "a=rtpmap:96 iLBC/8000\r\n" \
417 "a=fmtp:96 mode=20\r\n" \
418 "a=rtpmap:97 iLBC/8000\r\n" \
419 "a=fmtp:97 mode=30\r\n" \
420 "a=rtpmap:101 telephone-event/8000\r\n" \
421 "a=fmtp:101 0-15\r\n" \
422 "a=recvonly\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200423
Philipp Maier87bd9be2017-08-22 16:35:41 +0200424#define MDCX_NAT_DUMMY \
425 "MDCX 23 5@mgw MGCP 1.0\r\n" \
426 "C: 1355c6041e\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100427 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200428 "\r\n" \
429 "c=IN IP4 8.8.8.8\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200430 "m=audio 16434 RTP/AVP 3\r\n"
431
432#define CRCX_NO_LCO_NO_SDP \
433 "CRCX 2 6@mgw MGCP 1.0\r\n" \
434 "M: recvonly\r\n" \
435 "C: 2\r\n"
436
437#define CRCX_NO_LCO_NO_SDP_RET \
438 "200 2 OK\r\n" \
439 "I: %s\r\n" \
440 "\r\n" \
441 "v=0\r\n" \
442 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
443 "s=-\r\n" \
444 "c=IN IP4 0.0.0.0\r\n" \
445 "t=0 0\r\n" \
446 "m=audio 16008 RTP/AVP 0\r\n" \
447 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200448
449struct mgcp_test {
450 const char *name;
451 const char *req;
452 const char *exp_resp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200453 int ptype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454 const char *extra_fmtp;
455};
456
457static const struct mgcp_test tests[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200458 {"AUEP1", AUEP1, AUEP1_RET},
459 {"AUEP2", AUEP2, AUEP2_RET},
460 {"MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET},
461 {"MDCX2", MDCX_UNALLOCATED, MDCX_RET},
462 {"CRCX", CRCX, CRCX_RET, 97},
463 {"MDCX3", MDCX3, MDCX3_RET, PTYPE_IGNORE},
464 {"MDCX4", MDCX4, MDCX4_RET("18983216"), 99},
465 {"MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99},
466 {"MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99},
467 {"MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99},
468 {"MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99},
469 {"MDCX4_RO", MDCX4_RO, MDCX4_RO_RET("18983221"), PTYPE_IGNORE},
470 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
471 {"CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97},
472 {"EMPTY", EMPTY, EMPTY_RET},
473 {"SHORT1", SHORT, SHORT_RET},
474 {"SHORT2", SHORT2, SHORT2_RET},
475 {"SHORT3", SHORT3, SHORT2_RET},
476 {"SHORT4", SHORT4, SHORT2_RET},
477 {"RQNT1", RQNT, RQNT1_RET},
478 {"RQNT2", RQNT2, RQNT2_RET},
479 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
480 {"CRCX", CRCX, CRCX_FMTP_RET, 97,.extra_fmtp = "a=fmtp:126 0/1/2"},
481 {"MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE,.extra_fmtp =
482 "a=fmtp:126 0/1/2"},
483 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE,.extra_fmtp = "a=fmtp:126 0/1/2"},
Philipp Maierbc0346e2018-06-07 09:52:16 +0200484 {"CRCX", CRCX_NO_LCO_NO_SDP, CRCX_NO_LCO_NO_SDP_RET, 97},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200485};
486
487static const struct mgcp_test retransmit[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200488 {"CRCX", CRCX, CRCX_RET},
489 {"RQNT1", RQNT, RQNT1_RET},
490 {"RQNT2", RQNT2, RQNT2_RET},
491 {"MDCX3", MDCX3, MDCX3A_RET},
492 {"DLCX", DLCX, DLCX_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200493};
494
Philipp Maierffd75e42017-11-22 11:44:50 +0100495static struct msgb *create_msg(const char *str, const char *conn_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200496{
497 struct msgb *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100498 int len;
499
500 printf("creating message from statically defined input:\n");
501 printf("---------8<---------\n%s\n---------8<---------\n", str);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200502
503 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Philipp Maierffd75e42017-11-22 11:44:50 +0100504 if (conn_id && strlen(conn_id))
505 len = sprintf((char *)msg->data, str, conn_id, conn_id);
506 else
507 len = sprintf((char *)msg->data, "%s", str);
508
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200509 msg->l2h = msgb_put(msg, len);
510 return msg;
511}
512
513static int last_endpoint = -1;
514
515static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
516 int state, const char *transactio_id)
517{
518 fprintf(stderr, "Policy CB got state %d on endpoint %d\n",
519 state, endpoint);
520 last_endpoint = endpoint;
521 return MGCP_POLICY_CONT;
522}
523
524#define MGCP_DUMMY_LOAD 0x23
525static int dummy_packets = 0;
526/* override and forward */
527ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200528 const struct sockaddr *dest_addr, socklen_t addrlen)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200529{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200530 uint32_t dest_host =
531 htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
532 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200533
Philipp Maier87bd9be2017-08-22 16:35:41 +0200534 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
535 fprintf(stderr,
536 "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
537 dest_host, dest_port, len, osmo_hexdump(buf, len));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200538 dummy_packets += 1;
539 }
540
Philipp Maier3d9b6562017-10-13 18:33:44 +0200541 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200542}
543
544static int64_t force_monotonic_time_us = -1;
545/* override and forward */
546int clock_gettime(clockid_t clk_id, struct timespec *tp)
547{
548 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
549 static clock_gettime_t real_clock_gettime = NULL;
550
551 if (!real_clock_gettime)
552 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
553
554 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
555 tp->tv_sec = force_monotonic_time_us / 1000000;
556 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
557 return 0;
558 }
559
560 return real_clock_gettime(clk_id, tp);
561}
562
563#define CONN_UNMODIFIED (0x1000)
564
565static void test_values(void)
566{
567 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200568 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200569
570 /* Check that LOOPBACK enables all output */
571 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200572 MGCP_CONN_RECV_SEND);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200573}
574
Philipp Maierffd75e42017-11-22 11:44:50 +0100575/* Extract a connection ID from a response (CRCX) */
576static int get_conn_id_from_response(uint8_t *resp, char *conn_id,
577 unsigned int conn_id_len)
578{
579 char *conn_id_ptr;
580 int i;
Philipp Maier55295f72018-01-15 14:00:28 +0100581 bool got_conn_id = false;
Philipp Maierffd75e42017-11-22 11:44:50 +0100582
Philipp Maier55295f72018-01-15 14:00:28 +0100583 /* First try to get the conn_id from the I: parameter */
Philipp Maierffd75e42017-11-22 11:44:50 +0100584 conn_id_ptr = strstr((char *)resp, "I: ");
Philipp Maier55295f72018-01-15 14:00:28 +0100585 if (conn_id_ptr) {
586 memset(conn_id, 0, conn_id_len);
587 memcpy(conn_id, conn_id_ptr + 3, 32);
588 got_conn_id = true;
589 } else {
590 /* Alternatively try to extract the conn_id from the o=- SDP
591 * parameter */
592 conn_id_ptr = strstr((char *)resp, "o=- ");
593 if(conn_id_ptr) {
594 memset(conn_id, 0, conn_id_len);
595 memcpy(conn_id, conn_id_ptr + 4, 32);
596 got_conn_id = true;
597 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100598 }
599
Philipp Maier55295f72018-01-15 14:00:28 +0100600 if (got_conn_id) {
601 for (i = 0; i < conn_id_len; i++) {
602 if (conn_id[i] == '\n' || conn_id[i] == '\r')
603 conn_id[i] = '\0';
604 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100605
Philipp Maier55295f72018-01-15 14:00:28 +0100606 /* A valid conn_id must at least contain one digit, and must
607 * not exceed a length of 32 digits */
608 OSMO_ASSERT(strlen(conn_id) <= 32);
609 OSMO_ASSERT(strlen(conn_id) > 0);
610
611 return 0;
612 }
613 return -EINVAL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100614}
615
616/* Check response, automatically patch connection ID if needed */
617static int check_response(uint8_t *resp, const char *exp_resp)
618{
619 char exp_resp_patched[4096];
620 const char *exp_resp_ptr;
621 char conn_id[256];
622
623 printf("checking response:\n");
624
625 /* If the expected response is intened to be patched
626 * (%s placeholder inside) we will patch it with the
627 * connection identifier we just received from the
628 * real response. This is necessary because the CI
629 * is generated by the mgcp code on CRCX and we can
630 * not know it in advance */
631 if (strstr(exp_resp, "%s")) {
632 if (get_conn_id_from_response(resp, conn_id, sizeof(conn_id)) ==
633 0) {
634 sprintf(exp_resp_patched, exp_resp, conn_id, conn_id);
635 exp_resp_ptr = exp_resp_patched;
636 printf
637 ("using message with patched conn_id for comparison\n");
638 } else {
639 printf
640 ("patching conn_id failed, using message as statically defined for comparison\n");
641 exp_resp_ptr = exp_resp;
642 }
643 } else {
644 printf("using message as statically defined for comparison\n");
645 exp_resp_ptr = exp_resp;
646 }
647
648 if (strcmp((char *)resp, exp_resp_ptr) != 0) {
649 printf("Unexpected response, please check!\n");
650 printf
651 ("Got:\n---------8<---------\n%s\n---------8<---------\n\n",
652 resp);
653 printf
654 ("Expected:\n---------8<---------\n%s\n---------8<---------\n",
655 exp_resp_ptr);
656 return -EINVAL;
657 }
658
659 printf("Response matches our expectations.\n");
660 return 0;
661}
662
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200663static void test_messages(void)
664{
665 struct mgcp_config *cfg;
666 struct mgcp_endpoint *endp;
667 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200668 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100669 char last_conn_id[256];
Philipp Maier7df419b2017-12-04 17:11:42 +0100670 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200671
672 cfg = mgcp_config_alloc();
673
Philipp Maierfcd06552017-11-10 17:32:22 +0100674 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200675 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200676 cfg->policy_cb = mgcp_test_policy_cb;
677
Philipp Maierffd75e42017-11-22 11:44:50 +0100678 memset(last_conn_id, 0, sizeof(last_conn_id));
679
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200680 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
681
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200682 for (i = 0; i < ARRAY_SIZE(tests); i++) {
683 const struct mgcp_test *t = &tests[i];
684 struct msgb *inp;
685 struct msgb *msg;
686
Philipp Maierffd75e42017-11-22 11:44:50 +0100687 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688 printf("Testing %s\n", t->name);
689
690 last_endpoint = -1;
691 dummy_packets = 0;
692
Philipp Maier87bd9be2017-08-22 16:35:41 +0200693 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
694 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200695
Philipp Maierffd75e42017-11-22 11:44:50 +0100696 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200697 msg = mgcp_handle_message(cfg, inp);
698 msgb_free(inp);
699 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200700 if (msg) {
701 printf("%s failed '%s'\n", t->name,
702 (char *)msg->data);
703 OSMO_ASSERT(false);
704 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100705 } else if (check_response(msg->data, t->exp_resp) != 0) {
706 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200707 OSMO_ASSERT(false);
708 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100709
Philipp Maier7df419b2017-12-04 17:11:42 +0100710 if (msg) {
711 rc = get_conn_id_from_response(msg->data, last_conn_id,
712 sizeof(last_conn_id));
713 if (rc)
714 printf("(response contains a connection id)\n");
715 else
716 printf("(response does not contain a connection id)\n");
717 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100718
Philipp Maiera330b862017-12-04 17:16:16 +0100719 if (msg)
720 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200721
722 if (dummy_packets)
723 printf("Dummy packets: %d\n", dummy_packets);
724
725 if (last_endpoint != -1) {
726 endp = &cfg->trunk.endpoints[last_endpoint];
727
Philipp Maier01d24a32017-11-21 17:26:09 +0100728 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200729 if (conn) {
730 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200731
Philipp Maier87bd9be2017-08-22 16:35:41 +0200732 if (conn->end.packet_duration_ms != -1)
733 printf("Detected packet duration: %d\n",
734 conn->end.packet_duration_ms);
735 else
736 printf("Packet duration not set\n");
737 if (endp->local_options.pkt_period_min ||
738 endp->local_options.pkt_period_max)
739 printf
740 ("Requested packetetization period: "
741 "%d-%d\n",
742 endp->local_options.pkt_period_min,
743 endp->
744 local_options.pkt_period_max);
745 else
746 printf
747 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200748
Philipp Maier87bd9be2017-08-22 16:35:41 +0200749 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
750 printf("Connection mode: %d:%s%s%s%s\n",
751 conn->conn->mode,
752 !conn->conn->mode ? " NONE" : "",
753 conn->conn->mode & MGCP_CONN_SEND_ONLY
754 ? " SEND" : "",
755 conn->conn->mode & MGCP_CONN_RECV_ONLY
756 ? " RECV" : "",
757 conn->conn->mode & MGCP_CONN_LOOPBACK
758 & ~MGCP_CONN_RECV_SEND
759 ? " LOOP" : "");
760 fprintf(stderr,
761 "RTP output %sabled, NET output %sabled\n",
762 conn->end.output_enabled
763 ? "en" : "dis",
764 conn->end.output_enabled
765 ? "en" : "dis");
766 } else
767 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200768
Philipp Maier87bd9be2017-08-22 16:35:41 +0200769 OSMO_ASSERT(conn->end.output_enabled
770 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
771
772 conn->conn->mode |= CONN_UNMODIFIED;
773
774 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200775 endp->local_options.pkt_period_min = 0;
776 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200777 }
778
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200779 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100780 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200781 OSMO_ASSERT(last_endpoint != -1);
782 endp = &cfg->trunk.endpoints[last_endpoint];
783
784 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200785 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200786 last_endpoint,
Philipp Maierbc0346e2018-06-07 09:52:16 +0200787 conn->end.codec->payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200788
Philipp Maier87bd9be2017-08-22 16:35:41 +0200789 if (t->ptype != PTYPE_IGNORE)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200790 OSMO_ASSERT(conn->end.codec->payload_type ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200791 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200792
793 /* Reset them again for next test */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200794 conn->end.codec->payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200795 }
796 }
797
798 talloc_free(cfg);
799}
800
801static void test_retransmission(void)
802{
803 struct mgcp_config *cfg;
804 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100805 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100806 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200807
808 cfg = mgcp_config_alloc();
809
Philipp Maierfcd06552017-11-10 17:32:22 +0100810 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811 mgcp_endpoints_allocate(&cfg->trunk);
812
Philipp Maierffd75e42017-11-22 11:44:50 +0100813 memset(last_conn_id, 0, sizeof(last_conn_id));
814
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200815 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
816
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200817 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
818 const struct mgcp_test *t = &retransmit[i];
819 struct msgb *inp;
820 struct msgb *msg;
821
Philipp Maierffd75e42017-11-22 11:44:50 +0100822 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200823 printf("Testing %s\n", t->name);
824
Philipp Maierffd75e42017-11-22 11:44:50 +0100825 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200826 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200828 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100829 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200830 printf("%s failed '%s'\n", t->name, (char *)msg->data);
831 OSMO_ASSERT(false);
832 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100833
Philipp Maier23b8e292017-12-04 16:48:45 +0100834 if (msg && strcmp(t->name, "CRCX") == 0) {
835 rc = get_conn_id_from_response(msg->data, last_conn_id,
836 sizeof(last_conn_id));
837 OSMO_ASSERT(rc == 0);
838 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100839
Philipp Maier7cedfd72017-12-04 16:49:12 +0100840 if (msg)
841 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200842
843 /* Retransmit... */
844 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100845 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200846 msg = mgcp_handle_message(cfg, inp);
847 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100848 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200849 printf("%s failed '%s'\n", t->name, (char *)msg->data);
850 OSMO_ASSERT(false);
851 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200852 msgb_free(msg);
853 }
854
855 talloc_free(cfg);
856}
857
858static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
859{
860 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200861 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200862 return 0;
863}
864
865static void test_rqnt_cb(void)
866{
867 struct mgcp_config *cfg;
868 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100869 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200870
871 cfg = mgcp_config_alloc();
872 cfg->rqnt_cb = rqnt_cb;
873
Philipp Maierfcd06552017-11-10 17:32:22 +0100874 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200875 mgcp_endpoints_allocate(&cfg->trunk);
876
877 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
878
Philipp Maierffd75e42017-11-22 11:44:50 +0100879 inp = create_msg(CRCX, NULL);
880 msg = mgcp_handle_message(cfg, inp);
881 OSMO_ASSERT(msg);
882 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
883 sizeof(conn_id)) == 0);
884 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200885 msgb_free(inp);
886
887 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100888 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200889 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200890 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
892 abort();
893 }
894
Philipp Maier87bd9be2017-08-22 16:35:41 +0200895 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200896 printf("FAILED: callback not called: %p\n", cfg->data);
897 abort();
898 }
899
900 msgb_free(msg);
901 msgb_free(inp);
902
Philipp Maierffd75e42017-11-22 11:44:50 +0100903 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904 msgb_free(mgcp_handle_message(cfg, inp));
905 msgb_free(inp);
906 talloc_free(cfg);
907}
908
909struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200910 int cycles;
911 uint16_t base_seq;
912 uint16_t max_seq;
913 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200914
Philipp Maier87bd9be2017-08-22 16:35:41 +0200915 uint32_t expected;
916 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200917};
918
919static const struct pl_test pl_test_dat[] = {
920 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200921 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
922 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200923 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200924 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
925 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200926 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200927 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
928 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200929 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200930 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
931 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200932 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200933 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
934 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200935};
936
937static void test_packet_loss_calc(void)
938{
939 int i;
Philipp Maiercede2a42018-07-03 14:14:21 +0200940 struct mgcp_endpoint endp;
941 struct mgcp_trunk_config trunk;
942
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200943 printf("Testing packet loss calculation.\n");
944
Philipp Maiercede2a42018-07-03 14:14:21 +0200945 memset(&endp, 0, sizeof(endp));
946 memset(&trunk, 0, sizeof(trunk));
947
948 endp.type = &ep_typeset.rtp;
949 trunk.vty_number_endpoints = 1;
950 trunk.endpoints = &endp;
951 endp.tcfg = &trunk;
952 INIT_LLIST_HEAD(&endp.conns);
953
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200954 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
955 uint32_t expected;
956 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200957
Philipp Maiercede2a42018-07-03 14:14:21 +0200958 struct mgcp_conn_rtp *conn = NULL;
959 struct mgcp_conn *_conn = NULL;
960 struct mgcp_rtp_state *state;
961 struct rate_ctr *packets_rx;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200962
Philipp Maiercede2a42018-07-03 14:14:21 +0200963 _conn =
964 mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
965 "test-connection");
966 conn = mgcp_conn_get_rtp(&endp, _conn->id);
967 state = &conn->state;
968 packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
969
970 state->stats.initialized = 1;
971 state->stats.base_seq = pl_test_dat[i].base_seq;
972 state->stats.max_seq = pl_test_dat[i].max_seq;
973 state->stats.cycles = pl_test_dat[i].cycles;
974
975 packets_rx->current = pl_test_dat[i].packets;
976 calc_loss(conn, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200977
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978 if (loss != pl_test_dat[i].loss
979 || expected != pl_test_dat[i].expected) {
980 printf
981 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
982 i, loss, pl_test_dat[i].loss, expected,
983 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200984 }
Philipp Maiercede2a42018-07-03 14:14:21 +0200985
986 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200987 }
Philipp Maiercede2a42018-07-03 14:14:21 +0200988
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200989}
990
Philipp Maier87bd9be2017-08-22 16:35:41 +0200991int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
992 uint32_t *pr, uint32_t *_or, int *loss,
993 uint32_t *jitter)
994{
995 char *line, *save;
996 int rc;
997
998 /* initialize with bad values */
999 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
1000 *loss = INT_MAX;
1001
1002 line = strtok_r((char *)msg->l2h, "\r\n", &save);
1003 if (!line)
1004 return -1;
1005
1006 /* this can only parse the message that is created above... */
1007 for_each_non_empty_line(line, save) {
1008 switch (line[0]) {
1009 case 'P':
1010 rc = sscanf(line,
1011 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
1012 ps, os, pr, _or, loss, jitter);
1013 return rc == 6 ? 0 : -1;
1014 }
1015 }
1016
1017 return -1;
1018}
1019
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001020static void test_mgcp_stats(void)
1021{
1022 printf("Testing stat parsing\n");
1023
1024 uint32_t bps, bos, pr, _or, jitter;
1025 struct msgb *msg;
1026 int loss;
1027 int rc;
1028
Philipp Maierffd75e42017-11-22 11:44:50 +01001029 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001030 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1031 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001032 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
1033 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001034 printf("FAIL: Parsing failed1.\n");
1035 msgb_free(msg);
1036
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037 msg =
1038 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +01001039 ("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 +02001040 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1041 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001042 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
1043 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001044 printf("FAIL: Parsing failed2.\n");
1045 msgb_free(msg);
1046}
1047
1048struct rtp_packet_info {
1049 float txtime;
1050 int len;
1051 char *data;
1052};
1053
1054struct rtp_packet_info test_rtp_packets1[] = {
1055 /* RTP: SeqNo=0, TS=0 */
1056 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001057 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001058 /* RTP: SeqNo=1, TS=160 */
1059 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001060 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001061 /* RTP: SeqNo=2, TS=320 */
1062 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001063 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001064 /* Repeat RTP timestamp: */
1065 /* RTP: SeqNo=3, TS=320 */
1066 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001067 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001068 /* RTP: SeqNo=4, TS=480 */
1069 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001070 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001071 /* RTP: SeqNo=5, TS=640 */
1072 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001073 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001074 /* Double skip RTP timestamp (delta = 2*160): */
1075 /* RTP: SeqNo=6, TS=960 */
1076 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001077 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001078 /* RTP: SeqNo=7, TS=1120 */
1079 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001080 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001081 /* RTP: SeqNo=8, TS=1280 */
1082 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001083 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001084 /* Non 20ms RTP timestamp (delta = 120): */
1085 /* RTP: SeqNo=9, TS=1400 */
1086 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001087 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001088 /* RTP: SeqNo=10, TS=1560 */
1089 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001090 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001091 /* RTP: SeqNo=11, TS=1720 */
1092 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001093 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001094 /* SSRC changed to 0x10203040, RTP timestamp jump */
1095 /* RTP: SeqNo=12, TS=34688 */
1096 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001097 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001098 /* RTP: SeqNo=13, TS=34848 */
1099 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001101 /* RTP: SeqNo=14, TS=35008 */
1102 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001103 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104 /* Non 20ms RTP timestamp (delta = 120): */
1105 /* RTP: SeqNo=15, TS=35128 */
1106 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001107 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001108 /* RTP: SeqNo=16, TS=35288 */
1109 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001110 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001111 /* RTP: SeqNo=17, TS=35448 */
1112 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001113 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001114 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1115 /* RTP: SeqNo=19, TS=35768 */
1116 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001117 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001118 /* RTP: SeqNo=20, TS=35928 */
1119 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001120 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001121 /* RTP: SeqNo=21, TS=36088 */
1122 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001123 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001124 /* Repeat last packet */
1125 /* RTP: SeqNo=21, TS=36088 */
1126 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001127 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001128 /* RTP: SeqNo=22, TS=36248 */
1129 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001130 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001131 /* RTP: SeqNo=23, TS=36408 */
1132 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001133 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001134 /* Don't increment SeqNo but increment timestamp by 160 */
1135 /* RTP: SeqNo=23, TS=36568 */
1136 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001137 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001138 /* RTP: SeqNo=24, TS=36728 */
1139 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001140 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001141 /* RTP: SeqNo=25, TS=36888 */
1142 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001144 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1145 * SeqNo jump */
1146 /* RTP: SeqNo=1000, TS=160000 */
1147 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001148 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001149 /* RTP: SeqNo=1001, TS=160160 */
1150 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001151 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001152 /* RTP: SeqNo=1002, TS=160320 */
1153 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001154 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001155};
1156
Philipp Maier87bd9be2017-08-22 16:35:41 +02001157void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1158 struct mgcp_rtp_state *state,
1159 struct mgcp_rtp_end *rtp_end,
1160 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001161
1162static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1163{
1164 int i;
1165
1166 struct mgcp_trunk_config trunk;
1167 struct mgcp_endpoint endp;
1168 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001169 struct mgcp_rtp_end *rtp;
1170 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001171 char buffer[4096];
1172 uint32_t last_ssrc = 0;
1173 uint32_t last_timestamp = 0;
1174 uint32_t last_seqno = 0;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001175 uint64_t last_in_ts_err_cnt = 0;
1176 uint64_t last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001177 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001178 struct mgcp_conn *_conn = NULL;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001179 struct rate_ctr test_ctr_in;
1180 struct rate_ctr test_ctr_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001181
1182 printf("Testing packet error detection%s%s.\n",
1183 patch_ssrc ? ", patch SSRC" : "",
1184 patch_ts ? ", patch timestamps" : "");
1185
1186 memset(&trunk, 0, sizeof(trunk));
1187 memset(&endp, 0, sizeof(endp));
1188 memset(&state, 0, sizeof(state));
1189
Philipp Maier9e1d1642018-05-09 16:26:34 +02001190 memset(&test_ctr_in, 0, sizeof(test_ctr_in));
1191 memset(&test_ctr_out, 0, sizeof(test_ctr_out));
1192 state.in_stream.err_ts_ctr = &test_ctr_in;
1193 state.out_stream.err_ts_ctr = &test_ctr_out;
1194
Philipp Maier87bd9be2017-08-22 16:35:41 +02001195 endp.type = &ep_typeset.rtp;
1196
Philipp Maierfcd06552017-11-10 17:32:22 +01001197 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001198 trunk.endpoints = &endp;
1199 trunk.force_constant_ssrc = patch_ssrc;
1200 trunk.force_aligned_timing = patch_ts;
1201
1202 endp.tcfg = &trunk;
1203
Philipp Maier87bd9be2017-08-22 16:35:41 +02001204 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001205 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1206 "test-connection");
1207 OSMO_ASSERT(_conn);
1208 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001209 OSMO_ASSERT(conn);
1210
1211 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001212
Philipp Maierbc0346e2018-06-07 09:52:16 +02001213 OSMO_ASSERT(mgcp_codec_add(conn, PTYPE_UNDEFINED, "AMR/8000/1") == 0);
1214 rtp->codec = &rtp->codecs[0];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001215
1216 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1217 struct rtp_packet_info *info = test_rtp_packets1 + i;
1218
1219 force_monotonic_time_us = round(1000000.0 * info->txtime);
1220
1221 OSMO_ASSERT(info->len <= sizeof(buffer));
1222 OSMO_ASSERT(info->len >= 0);
1223 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001224 mgcp_rtp_end_config(&endp, 1, rtp);
1225
1226 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1227 buffer, info->len);
1228
1229 if (state.out_stream.ssrc != last_ssrc) {
1230 printf("Output SSRC changed to %08x\n",
1231 state.out_stream.ssrc);
1232 last_ssrc = state.out_stream.ssrc;
1233 }
1234
1235 printf("In TS: %d, dTS: %d, Seq: %d\n",
1236 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001237 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001238
1239 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
Philipp Maier9e1d1642018-05-09 16:26:34 +02001240 "TS Err change: in +%u, out +%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001241 state.out_stream.last_timestamp - last_timestamp,
1242 state.out_stream.last_tsdelta,
1243 state.out_stream.last_seq - last_seqno,
Philipp Maier9e1d1642018-05-09 16:26:34 +02001244 (unsigned int) (state.in_stream.err_ts_ctr->current - last_in_ts_err_cnt),
1245 (unsigned int) (state.out_stream.err_ts_ctr->current - last_out_ts_err_cnt));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001246
1247 printf("Stats: Jitter = %u, Transit = %d\n",
Harald Welte49e3d5a2017-12-25 09:47:57 +01001248 calc_jitter(&state), state.stats.transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001249
Philipp Maier9e1d1642018-05-09 16:26:34 +02001250 last_in_ts_err_cnt = state.in_stream.err_ts_ctr->current;
1251 last_out_ts_err_cnt = state.out_stream.err_ts_ctr->current;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001252 last_timestamp = state.out_stream.last_timestamp;
1253 last_seqno = state.out_stream.last_seq;
1254 }
1255
1256 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001257 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001258}
1259
1260static void test_multilple_codec(void)
1261{
1262 struct mgcp_config *cfg;
1263 struct mgcp_endpoint *endp;
1264 struct msgb *inp, *resp;
1265 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001266 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001267 char conn_id[256];
Philipp Maiera74c0ea2018-08-02 11:59:09 +02001268 int i;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001269
1270 printf("Testing multiple payload types\n");
1271
1272 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001273 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001274 mgcp_endpoints_allocate(&cfg->trunk);
1275 cfg->policy_cb = mgcp_test_policy_cb;
1276 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1277
1278 /* Allocate endpoint 1@mgw with two codecs */
1279 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001280 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001281 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001282 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1283 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001284 msgb_free(inp);
1285 msgb_free(resp);
1286
1287 OSMO_ASSERT(last_endpoint == 1);
1288 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001289 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001290 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001291 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001292
1293 /* Allocate 2@mgw with three codecs, last one ignored */
1294 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001295 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001296 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001297 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1298 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001299 msgb_free(inp);
1300 msgb_free(resp);
1301
1302 OSMO_ASSERT(last_endpoint == 2);
1303 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001304 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001305 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001306 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001307
Philipp Maierbc0346e2018-06-07 09:52:16 +02001308 /* Allocate 3@mgw with no codecs, check for PT == 0 */
1309 /* Note: It usually makes no sense to leave the payload type list
1310 * out. However RFC 2327 does not clearly forbid this case and
1311 * it makes and since we already decided in OS#2658 that a missing
1312 * LCO should pick a sane default codec, it makes sense to expect
1313 * the same behaviour if SDP lacks proper payload type information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001314 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001315 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001316 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001317 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1318 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001319 msgb_free(inp);
1320 msgb_free(resp);
1321
1322 OSMO_ASSERT(last_endpoint == 3);
1323 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001324 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001325 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001326 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001327
1328 /* Allocate 4@mgw with a single codec */
1329 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001330 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001331 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001332 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1333 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001334 msgb_free(inp);
1335 msgb_free(resp);
1336
1337 OSMO_ASSERT(last_endpoint == 4);
1338 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001339 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001340 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001341 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001342
1343 /* Allocate 5@mgw at select GSM.. */
1344 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001345 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001346 talloc_free(cfg->trunk.audio_name);
1347 cfg->trunk.audio_name = "GSM/8000";
1348 cfg->trunk.no_audio_transcoding = 1;
1349 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001350 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1351 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001352 msgb_free(inp);
1353 msgb_free(resp);
1354
1355 OSMO_ASSERT(last_endpoint == 5);
1356 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001357 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001358 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001359 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001360
Philipp Maierffd75e42017-11-22 11:44:50 +01001361 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001362 last_endpoint = -1;
1363 resp = mgcp_handle_message(cfg, inp);
1364 msgb_free(inp);
1365 msgb_free(resp);
1366 OSMO_ASSERT(last_endpoint == 5);
1367 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001368 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001369 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001370 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001371 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001372 memset(&addr, 0, sizeof(addr));
1373 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001374 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001375
1376 /* Check what happens without that flag */
1377
Philipp Maier87bd9be2017-08-22 16:35:41 +02001378 /* Free the previous endpoint and the data and
1379 * check if the connection really vanished... */
Philipp Maier1355d7e2018-02-01 14:30:06 +01001380 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001381 talloc_free(endp->last_response);
1382 talloc_free(endp->last_trans);
1383 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001384 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001385 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001386
1387 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001388 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001389 cfg->trunk.no_audio_transcoding = 0;
1390 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001391 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1392 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001393 msgb_free(inp);
1394 msgb_free(resp);
1395
1396 OSMO_ASSERT(last_endpoint == 5);
1397 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001398 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001399 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001400 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001401
Philipp Maiera74c0ea2018-08-02 11:59:09 +02001402 for (i = 1; i < cfg->trunk.number_endpoints; i++)
1403 mgcp_endp_release(&cfg->trunk.endpoints[i]);
1404
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001405 talloc_free(cfg);
1406}
1407
1408static void test_no_cycle(void)
1409{
1410 struct mgcp_config *cfg;
1411 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001412 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001413 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001414
1415 printf("Testing no sequence flow on initial packet\n");
1416
1417 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001418 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001419 mgcp_endpoints_allocate(&cfg->trunk);
1420
1421 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001422
Philipp Maierffd75e42017-11-22 11:44:50 +01001423 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1424 "test-connection");
1425 OSMO_ASSERT(_conn);
1426 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001427 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001428
Harald Welte49e3d5a2017-12-25 09:47:57 +01001429 OSMO_ASSERT(conn->state.stats.initialized == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001430
1431 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001432 OSMO_ASSERT(conn->state.stats.initialized == 1);
1433 OSMO_ASSERT(conn->state.stats.cycles == 0);
1434 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001435
1436 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001437 OSMO_ASSERT(conn->state.stats.initialized == 1);
1438 OSMO_ASSERT(conn->state.stats.cycles == 0);
1439 OSMO_ASSERT(conn->state.stats.max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001440
1441 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001442 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001443 OSMO_ASSERT(conn->state.stats.initialized == 1);
1444 OSMO_ASSERT(conn->state.stats.cycles == 0);
1445 OSMO_ASSERT(conn->state.stats.max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001446
1447 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001448 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001449 OSMO_ASSERT(conn->state.stats.initialized == 1);
1450 OSMO_ASSERT(conn->state.stats.cycles == UINT16_MAX + 1);
1451 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001452
Philipp Maier1355d7e2018-02-01 14:30:06 +01001453 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001454 talloc_free(cfg);
1455}
1456
1457static void test_no_name(void)
1458{
1459 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001460 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001461
1462 printf("Testing no rtpmap name\n");
1463 cfg = mgcp_config_alloc();
1464
Philipp Maierfcd06552017-11-10 17:32:22 +01001465 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001466 cfg->trunk.audio_send_name = 0;
1467 mgcp_endpoints_allocate(&cfg->trunk);
1468
1469 cfg->policy_cb = mgcp_test_policy_cb;
1470
1471 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1472
Philipp Maierffd75e42017-11-22 11:44:50 +01001473 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001474 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001475
1476 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001477 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001478 (char *)msg->data);
1479 OSMO_ASSERT(false);
1480 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001481 msgb_free(inp);
1482 msgb_free(msg);
1483
Philipp Maier1355d7e2018-02-01 14:30:06 +01001484 mgcp_endp_release(&cfg->trunk.endpoints[1]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001485 talloc_free(cfg);
1486}
1487
1488static void test_osmux_cid(void)
1489{
1490 int id, i;
1491
1492 OSMO_ASSERT(osmux_used_cid() == 0);
1493 id = osmux_get_cid();
1494 OSMO_ASSERT(id == 0);
1495 OSMO_ASSERT(osmux_used_cid() == 1);
1496 osmux_put_cid(id);
1497 OSMO_ASSERT(osmux_used_cid() == 0);
1498
1499 for (i = 0; i < 256; ++i) {
1500 id = osmux_get_cid();
1501 OSMO_ASSERT(id == i);
1502 OSMO_ASSERT(osmux_used_cid() == i + 1);
1503 }
1504
1505 id = osmux_get_cid();
1506 OSMO_ASSERT(id == -1);
1507
1508 for (i = 0; i < 256; ++i)
1509 osmux_put_cid(i);
1510 OSMO_ASSERT(osmux_used_cid() == 0);
1511}
1512
1513static const struct log_info_cat log_categories[] = {
1514};
1515
1516const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001517 .cat = log_categories,
1518 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001519};
1520
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001521static void test_get_lco_identifier(void)
1522{
1523 char *test;
1524 printf("Testing get_lco_identifier()\n");
1525
1526 /* Normal case at the beginning */
1527 test = "p:10, a:PCMU";
1528 printf("%s -> %s\n", test, get_lco_identifier(test));
1529
1530 test = "p:10, a:PCMU";
1531 printf("%s -> %s\n", test, get_lco_identifier(test));
1532
1533 /* Begin parsing in the middle of the value part of
1534 * the previous LCO option value */
1535 test = "XXXX, p:10, a:PCMU";
1536 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1537
1538 test = "XXXX,p:10,a:PCMU";
1539 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1540
1541 test = "10,a:PCMU";
1542 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1543
1544 test = "10, a:PCMU";
1545 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1546
1547 test = "10,a: PCMU";
1548 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1549
1550 test = "10 ,a: PCMU";
1551 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1552
1553 /* Begin parsing right at the end of the previous LCO
1554 * option value */
1555 test = ", a:PCMU";
1556 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1557
1558 test = " a:PCMU";
1559 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1560
1561 /* Empty string, result should be (null) */
1562 test = "";
1563 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1564
1565 /* Missing colons, result should be (null) */
1566 test = "p10, aPCMU";
1567 printf("%s -> %s\n", test, get_lco_identifier(test));
1568
1569 /* Colon separated from the identifier, result should be (null) */
1570 test = "10,a :PCMU";
1571 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1572}
1573
1574static void test_check_local_cx_options(void *ctx)
1575{
1576 /* Legal cases */
1577 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1578 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU") == 0);
1579 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p:10, IN:10") == 0);
1580 OSMO_ASSERT(check_local_cx_options(ctx, "one:AAA, two:BB, tree:C") ==
1581 0);
1582 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1583 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:G726-32") == 0);
1584 OSMO_ASSERT(check_local_cx_options(ctx, "p:10-20, b:64") == 0);
1585 OSMO_ASSERT(check_local_cx_options(ctx, "b:32-64, e:off") == 0);
1586 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU;G726-32") == 0);
1587
1588 /* Illegal spaces before and after colon */
1589 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p :10") == -1);
1590 OSMO_ASSERT(check_local_cx_options(ctx, "a :PCMU, p:10") == -1);
1591 OSMO_ASSERT(check_local_cx_options(ctx, "p: 10, a:PCMU") == -1);
1592
1593 /* Check if multiple appearances of LCOs are rejected */
1594 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, p:10") == -1);
1595 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, a:PCMU, p:10") ==
1596 -1);
1597 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, p:10") == -1);
1598
1599 /* Check if empty LCO are rejected */
1600 OSMO_ASSERT(check_local_cx_options(ctx, "p: , a:PCMU") == -1);
1601 OSMO_ASSERT(check_local_cx_options(ctx, "p: , a: PCMU") == -1);
1602 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a: PCMU") == -1);
1603 OSMO_ASSERT(check_local_cx_options(ctx, "p:, a:PCMU") == -1);
1604 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:") == -1);
1605
1606 /* Garbeled beginning and ends */
1607 OSMO_ASSERT(check_local_cx_options(ctx, ":10, a:10") == -1);
1608 OSMO_ASSERT(check_local_cx_options(ctx, "10, a:PCMU") == -1);
1609 OSMO_ASSERT(check_local_cx_options(ctx, ", a:PCMU") == -1);
1610 OSMO_ASSERT(check_local_cx_options(ctx, " a:PCMU") == -1);
1611 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU,") == -1);
1612 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, ") == -1);
1613
1614 /* Illegal strings */
1615 OSMO_ASSERT(check_local_cx_options(ctx, " ") == -1);
1616 OSMO_ASSERT(check_local_cx_options(ctx, "") == -1);
1617 OSMO_ASSERT(check_local_cx_options(ctx, "AAA") == -1);
1618 OSMO_ASSERT(check_local_cx_options(ctx, ":,") == -1);
1619 OSMO_ASSERT(check_local_cx_options(ctx, ",:") == -1);
1620 OSMO_ASSERT(check_local_cx_options(ctx, ",,,") == -1);
1621}
1622
Philipp Maier6931f9a2018-07-26 09:29:31 +02001623static void test_mgcp_codec_pt_translate_pars(struct mgcp_rtp_codec *c)
1624{
1625 c->rate = 8000;
1626 c->channels = 1;
1627 c->frame_duration_num = 23;
1628 c->frame_duration_den = 42;
1629}
1630
1631static void test_mgcp_codec_pt_translate(void)
1632{
1633 struct mgcp_conn_rtp conn_src;
1634 struct mgcp_conn_rtp conn_dst;
1635 int pt_dst;
1636
1637 /* Setup a realistic set of codec configurations on both
1638 * ends. AMR and HR will use different payload types. PCMU
1639 * must use 0 on both ends since this is not a dynamic payload
1640 * type */
1641 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[0]);
1642 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[0]);
1643 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[1]);
1644 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[1]);
1645 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[2]);
1646 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[2]);
1647 conn_src.end.codecs[0].payload_type = 112;
1648 conn_dst.end.codecs[0].payload_type = 96;
1649 conn_src.end.codecs[1].payload_type = 0;
1650 conn_dst.end.codecs[1].payload_type = 0;
1651 conn_src.end.codecs[2].payload_type = 111;
1652 conn_dst.end.codecs[2].payload_type = 97;
1653 conn_src.end.codecs[0].audio_name = "AMR/8000/1";
1654 conn_dst.end.codecs[0].audio_name = "AMR/8000/1";
1655 conn_src.end.codecs[1].audio_name = "PCMU/8000/1";
1656 conn_dst.end.codecs[1].audio_name = "PCMU/8000/1";
1657 conn_src.end.codecs[2].audio_name = "GSM-HR-08/8000/1";
1658 conn_dst.end.codecs[2].audio_name = "GSM-HR-08/8000/1";
1659 conn_src.end.codecs[0].subtype_name = "AMR";
1660 conn_dst.end.codecs[0].subtype_name = "AMR";
1661 conn_src.end.codecs[1].subtype_name = "PCMU";
1662 conn_dst.end.codecs[1].subtype_name = "PCMU";
1663 conn_src.end.codecs[2].subtype_name = "GSM-HR-08";
1664 conn_dst.end.codecs[2].subtype_name = "GSM-HR-08";
1665 conn_src.end.codecs_assigned = 3;
1666 conn_dst.end.codecs_assigned = 3;
1667
1668 /* We expect the function to find the PT we must use when we send the
1669 * packet out to the destination. All we know is the context for both
1670 * connections and the payload type from the source packet */
1671 pt_dst =
1672 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1673 conn_src.end.codecs[0].payload_type);
1674 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[0].payload_type);
1675 pt_dst =
1676 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1677 conn_src.end.codecs[1].payload_type);
1678 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[1].payload_type);
1679 pt_dst =
1680 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1681 conn_src.end.codecs[2].payload_type);
1682 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[2].payload_type);
1683
1684 /* Try some constellations that must fail */
1685 pt_dst = mgcp_codec_pt_translate(&conn_src, &conn_dst, 123);
1686 OSMO_ASSERT(pt_dst == -EINVAL);
1687 conn_src.end.codecs_assigned = 0;
1688 conn_dst.end.codecs_assigned = 3;
1689 pt_dst =
1690 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1691 conn_src.end.codecs[0].payload_type);
1692 OSMO_ASSERT(pt_dst == -EINVAL);
1693 pt_dst =
1694 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1695 conn_src.end.codecs[1].payload_type);
1696 OSMO_ASSERT(pt_dst == -EINVAL);
1697 pt_dst =
1698 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1699 conn_src.end.codecs[2].payload_type);
1700 OSMO_ASSERT(pt_dst == -EINVAL);
1701 conn_src.end.codecs_assigned = 3;
1702 conn_dst.end.codecs_assigned = 0;
1703 pt_dst =
1704 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1705 conn_src.end.codecs[0].payload_type);
1706 OSMO_ASSERT(pt_dst == -EINVAL);
1707 pt_dst =
1708 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1709 conn_src.end.codecs[1].payload_type);
1710 OSMO_ASSERT(pt_dst == -EINVAL);
1711 pt_dst =
1712 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1713 conn_src.end.codecs[2].payload_type);
1714 OSMO_ASSERT(pt_dst == -EINVAL);
1715}
1716
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001717int main(int argc, char **argv)
1718{
Neels Hofmeyr60f8e312018-03-30 23:01:07 +02001719 void *ctx = talloc_named_const(NULL, 0, "mgcp_test");
1720 void *msgb_ctx = msgb_talloc_ctx_init(ctx, 0);
1721 osmo_init_logging2(ctx, &log_info);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001722
1723 test_strline();
1724 test_values();
1725 test_messages();
1726 test_retransmission();
1727 test_packet_loss_calc();
1728 test_rqnt_cb();
1729 test_mgcp_stats();
1730 test_packet_error_detection(1, 0);
1731 test_packet_error_detection(0, 0);
1732 test_packet_error_detection(0, 1);
1733 test_packet_error_detection(1, 1);
1734 test_multilple_codec();
1735 test_no_cycle();
1736 test_no_name();
1737 test_osmux_cid();
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001738 test_get_lco_identifier();
1739 test_check_local_cx_options(ctx);
Philipp Maier6931f9a2018-07-26 09:29:31 +02001740 test_mgcp_codec_pt_translate();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001741
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001742 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1743 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1744 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001745 printf("Done\n");
1746 return EXIT_SUCCESS;
1747}