blob: ee49eef4ffc1f877aef6a588cb49de3508c0eeb9 [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>
28#include <osmocom/mgcp/mgcp_ep.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020029
30#include <osmocom/core/application.h>
31#include <osmocom/core/talloc.h>
32#include <osmocom/core/utils.h>
33#include <string.h>
34#include <limits.h>
35#include <dlfcn.h>
36#include <time.h>
37#include <math.h>
38
39char *strline_r(char *str, char **saveptr);
40
41const char *strline_test_data =
42 "one CR\r"
43 "two CR\r"
44 "\r"
45 "one CRLF\r\n"
46 "two CRLF\r\n"
Philipp Maier87bd9be2017-08-22 16:35:41 +020047 "\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 +020048
49#define EXPECTED_NUMBER_OF_LINES 13
50
51static void test_strline(void)
52{
53 char *save = NULL;
54 char *line;
55 char buf[2048];
56 int counter = 0;
57
58 osmo_strlcpy(buf, strline_test_data, sizeof(buf));
59
Philipp Maier87bd9be2017-08-22 16:35:41 +020060 for (line = mgcp_strline(buf, &save); line;
61 line = mgcp_strline(NULL, &save)) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020062 printf("line: '%s'\n", line);
63 counter++;
64 }
65
66 OSMO_ASSERT(counter == EXPECTED_NUMBER_OF_LINES);
67}
68
69#define AUEP1 "AUEP 158663169 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n"
70#define AUEP1_RET "200 158663169 OK\r\n"
71#define AUEP2 "AUEP 18983213 ds/e1-2/1@172.16.6.66 MGCP 1.0\r\n"
72#define AUEP2_RET "500 18983213 FAIL\r\n"
73#define EMPTY "\r\n"
74#define EMPTY_RET NULL
75#define SHORT "CRCX \r\n"
76#define SHORT_RET "510 000000 FAIL\r\n"
77
78#define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@172.16.6.66 MGCP 1.0\r\n"
79#define MDCX_ERR_RET "510 18983213 FAIL\r\n"
80#define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n"
81#define MDCX_RET "400 18983214 FAIL\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020082
Philipp Maier87bd9be2017-08-22 16:35:41 +020083#define MDCX3 \
84 "MDCX 18983215 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010085 "I: %s\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020086
Philipp Maier87bd9be2017-08-22 16:35:41 +020087#define MDCX3_RET \
88 "200 18983215 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010089 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020090 "\n" \
91 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010092 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020093 "s=-\r\n" \
94 "c=IN IP4 0.0.0.0\r\n" \
95 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010096 "m=audio 16002 RTP/AVP 97\r\n" \
97 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020098 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020099
Philipp Maier87bd9be2017-08-22 16:35:41 +0200100#define MDCX3A_RET \
101 "200 18983215 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100102 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200103 "\n" \
104 "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 Maierffd75e42017-11-22 11:44:50 +0100115 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200116 "\n" \
117 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100118 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200119 "s=-\r\n" \
120 "c=IN IP4 0.0.0.0\r\n" \
121 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100122 "m=audio 16006 RTP/AVP 97\r\n" \
123 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200124 "a=fmtp:126 0/1/2\r\n" \
125 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127#define MDCX4 \
128 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \
129 "M: sendrecv\r" \
130 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100131 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200132 "L: p:20, a:AMR, nt:IN\r\n" \
133 "\n" \
134 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100135 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200136 "c=IN IP4 0.0.0.0\r\n" \
137 "t=0 0\r\n" \
138 "m=audio 4441 RTP/AVP 99\r\n" \
139 "a=rtpmap:99 AMR/8000\r\n" \
140 "a=ptime:40\r\n"
141
142#define MDCX4_RET(Ident) \
143 "200 " Ident " OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100144 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145 "\n" \
146 "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 Maierffd75e42017-11-22 11:44:50 +0100157 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158 "\n" \
159 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100160 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161 "s=-\r\n" \
162 "c=IN IP4 0.0.0.0\r\n" \
163 "t=0 0\r\n" \
164 "m=audio 16002 RTP/AVP 96\r\n" \
165 "a=rtpmap:96 AMR\r\n" \
166 "a=ptime:40\r\n"
167
168#define MDCX4_PT1 \
169 "MDCX 18983217 1@mgw MGCP 1.0\r\n" \
170 "M: sendrecv\r" \
171 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100172 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173 "L: p:20-40, a:AMR, nt:IN\r\n" \
174 "\n" \
175 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100176 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200177 "c=IN IP4 0.0.0.0\r\n" \
178 "t=0 0\r\n" \
179 "m=audio 4441 RTP/AVP 99\r\n" \
180 "a=rtpmap:99 AMR/8000\r\n" \
181 "a=ptime:40\r\n"
182
183#define MDCX4_PT2 \
184 "MDCX 18983218 1@mgw MGCP 1.0\r\n" \
185 "M: sendrecv\r" \
186 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100187 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200188 "L: p:20-20, a:AMR, nt:IN\r\n" \
189 "\n" \
190 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100191 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200192 "c=IN IP4 0.0.0.0\r\n" \
193 "t=0 0\r\n" \
194 "m=audio 4441 RTP/AVP 99\r\n" \
195 "a=rtpmap:99 AMR/8000\r\n" \
196 "a=ptime:40\r\n"
197
198#define MDCX4_PT3 \
199 "MDCX 18983219 1@mgw MGCP 1.0\r\n" \
200 "M: sendrecv\r" \
201 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100202 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200203 "L: a:AMR, nt:IN\r\n" \
204 "\n" \
205 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100206 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207 "c=IN IP4 0.0.0.0\r\n" \
208 "t=0 0\r\n" \
209 "m=audio 4441 RTP/AVP 99\r\n" \
210 "a=rtpmap:99 AMR/8000\r\n" \
211 "a=ptime:40\r\n"
212
213#define MDCX4_SO \
214 "MDCX 18983220 1@mgw MGCP 1.0\r\n" \
215 "M: sendonly\r" \
216 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100217 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200218 "L: p:20, a:AMR, nt:IN\r\n" \
219 "\n" \
220 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100221 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200222 "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_RO \
229 "MDCX 18983221 1@mgw MGCP 1.0\r\n" \
230 "M: recvonly\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"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234
235#define SHORT2 "CRCX 1"
236#define SHORT2_RET "510 000000 FAIL\r\n"
237#define SHORT3 "CRCX 1 1@mgw"
238#define SHORT4 "CRCX 1 1@mgw MGCP"
239#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
240
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241#define CRCX \
242 "CRCX 2 1@mgw MGCP 1.0\r\n" \
243 "M: recvonly\r\n" \
244 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245 "L: p:20\r\n" \
246 "\r\n" \
247 "v=0\r\n" \
248 "c=IN IP4 123.12.12.123\r\n" \
249 "m=audio 5904 RTP/AVP 97\r\n" \
250 "a=rtpmap:97 GSM-EFR/8000\r\n" \
251 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200252
Philipp Maier87bd9be2017-08-22 16:35:41 +0200253#define CRCX_RET \
254 "200 2 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100255 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200256 "\n" \
257 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100258 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200259 "s=-\r\n" \
260 "c=IN IP4 0.0.0.0\r\n" \
261 "t=0 0\r\n" \
262 "m=audio 16002 RTP/AVP 97\r\n" \
263 "a=rtpmap:97 GSM-EFR/8000\r\n" \
264 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265
Philipp Maier87bd9be2017-08-22 16:35:41 +0200266#define CRCX_RET_NO_RTPMAP \
267 "200 2 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100268 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200269 "\n" \
270 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100271 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200272 "s=-\r\n" \
273 "c=IN IP4 0.0.0.0\r\n" \
274 "t=0 0\r\n" \
275 "m=audio 16002 RTP/AVP 97\r\n" \
276 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200277
Philipp Maier87bd9be2017-08-22 16:35:41 +0200278#define CRCX_FMTP_RET \
279 "200 2 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100280 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281 "\n" \
282 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100283 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200284 "s=-\r\n" \
285 "c=IN IP4 0.0.0.0\r\n" \
286 "t=0 0\r\n" \
287 "m=audio 16006 RTP/AVP 97\r\n" \
288 "a=rtpmap:97 GSM-EFR/8000\r\n" \
289 "a=fmtp:126 0/1/2\r\n" \
290 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200291
Philipp Maier87bd9be2017-08-22 16:35:41 +0200292#define CRCX_ZYN \
293 "CRCX 2 1@mgw MGCP 1.0\r" \
294 "M: recvonly\r" \
295 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200296 "\n" \
297 "v=0\r" \
298 "c=IN IP4 123.12.12.123\r" \
299 "m=audio 5904 RTP/AVP 97\r" \
300 "a=rtpmap:97 GSM-EFR/8000\r"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200301
Philipp Maier87bd9be2017-08-22 16:35:41 +0200302#define CRCX_ZYN_RET \
303 "200 2 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100304 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200305 "\n" \
306 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100307 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200308 "s=-\r\n" \
309 "c=IN IP4 0.0.0.0\r\n" \
310 "t=0 0\r\n" \
311 "m=audio 16004 RTP/AVP 97\r\n" \
312 "a=rtpmap:97 GSM-EFR/8000\r\n" \
313 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200314
Philipp Maier87bd9be2017-08-22 16:35:41 +0200315#define DLCX \
316 "DLCX 7 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100317 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200318 "C: 2\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200319
Philipp Maier87bd9be2017-08-22 16:35:41 +0200320#define DLCX_RET \
321 "250 7 OK\r\n" \
322 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n" \
323 "X-Osmo-CP: EC TI=0, TO=0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200324
Philipp Maier87bd9be2017-08-22 16:35:41 +0200325#define RQNT \
326 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
327 "X: B244F267488\r\n" \
328 "S: D/9\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329
Philipp Maier87bd9be2017-08-22 16:35:41 +0200330#define RQNT2 \
331 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
332 "X: ADD4F26746F\r\n" \
333 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200334
335#define RQNT1_RET "200 186908780 OK\r\n"
336#define RQNT2_RET "200 186908781 OK\r\n"
337
Philipp Maier87bd9be2017-08-22 16:35:41 +0200338#define PTYPE_IGNORE 0 /* == default initializer */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200339#define PTYPE_NONE 128
340#define PTYPE_NYI PTYPE_NONE
341
Philipp Maier87bd9be2017-08-22 16:35:41 +0200342#define CRCX_MULT_1 \
343 "CRCX 2 1@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200344 "M: recvonly\r\n" \
345 "C: 2\r\n" \
346 "X\r\n" \
347 "L: p:20\r\n" \
348 "\r\n" \
349 "v=0\r\n" \
350 "c=IN IP4 123.12.12.123\r\n" \
351 "m=audio 5904 RTP/AVP 18 97\r\n" \
352 "a=rtpmap:18 G729/8000\r\n" \
353 "a=rtpmap:97 GSM-EFR/8000\r\n" \
354 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200355
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356#define CRCX_MULT_2 \
357 "CRCX 2 2@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200358 "M: recvonly\r\n" \
359 "C: 2\r\n" \
360 "X\r\n" \
361 "L: p:20\r\n" \
362 "\r\n" \
363 "v=0\r\n" \
364 "c=IN IP4 123.12.12.123\r\n" \
365 "m=audio 5904 RTP/AVP 18 97 101\r\n" \
366 "a=rtpmap:18 G729/8000\r\n" \
367 "a=rtpmap:97 GSM-EFR/8000\r\n" \
368 "a=rtpmap:101 FOO/8000\r\n" \
369 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200370
Philipp Maier87bd9be2017-08-22 16:35:41 +0200371#define CRCX_MULT_3 \
372 "CRCX 2 3@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200373 "M: recvonly\r\n" \
374 "C: 2\r\n" \
375 "X\r\n" \
376 "L: p:20\r\n" \
377 "\r\n" \
378 "v=0\r\n" \
379 "c=IN IP4 123.12.12.123\r\n" \
380 "m=audio 5904 RTP/AVP\r\n" \
381 "a=rtpmap:18 G729/8000\r\n" \
382 "a=rtpmap:97 GSM-EFR/8000\r\n" \
383 "a=rtpmap:101 FOO/8000\r\n" \
384 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200385
Philipp Maier87bd9be2017-08-22 16:35:41 +0200386#define CRCX_MULT_4 \
387 "CRCX 2 4@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200388 "M: recvonly\r\n" \
389 "C: 2\r\n" \
390 "X\r\n" \
391 "L: p:20\r\n" \
392 "\r\n" \
393 "v=0\r\n" \
394 "c=IN IP4 123.12.12.123\r\n" \
395 "m=audio 5904 RTP/AVP 18\r\n" \
396 "a=rtpmap:18 G729/8000\r\n" \
397 "a=rtpmap:97 GSM-EFR/8000\r\n" \
398 "a=rtpmap:101 FOO/8000\r\n" \
399 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400
401#define CRCX_MULT_GSM_EXACT \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200402 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
403 "C: 1355c6041e\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200404 "L: p:20, a:GSM, nt:IN\r\n" \
405 "M: recvonly\r\n" \
406 "\r\n" \
407 "v=0\r\n" \
408 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
409 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
410 "t=0 0\r\nm=audio 29084 RTP/AVP 255 0 8 3 18 4 96 97 101\r\n" \
411 "a=rtpmap:0 PCMU/8000\r\n" \
412 "a=rtpmap:8 PCMA/8000\r\n" \
413 "a=rtpmap:3 gsm/8000\r\n" \
414 "a=rtpmap:18 G729/8000\r\n" \
415 "a=fmtp:18 annexb=no\r\n" \
416 "a=rtpmap:4 G723/8000\r\n" \
417 "a=rtpmap:96 iLBC/8000\r\n" \
418 "a=fmtp:96 mode=20\r\n" \
419 "a=rtpmap:97 iLBC/8000\r\n" \
420 "a=fmtp:97 mode=30\r\n" \
421 "a=rtpmap:101 telephone-event/8000\r\n" \
422 "a=fmtp:101 0-15\r\n" \
423 "a=recvonly\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200424
Philipp Maier87bd9be2017-08-22 16:35:41 +0200425#define MDCX_NAT_DUMMY \
426 "MDCX 23 5@mgw MGCP 1.0\r\n" \
427 "C: 1355c6041e\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100428 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200429 "\r\n" \
430 "c=IN IP4 8.8.8.8\r\n" \
431 "m=audio 16434 RTP/AVP 255\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200432
433struct mgcp_test {
434 const char *name;
435 const char *req;
436 const char *exp_resp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437 int ptype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200438 const char *extra_fmtp;
439};
440
441static const struct mgcp_test tests[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200442 {"AUEP1", AUEP1, AUEP1_RET},
443 {"AUEP2", AUEP2, AUEP2_RET},
444 {"MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET},
445 {"MDCX2", MDCX_UNALLOCATED, MDCX_RET},
446 {"CRCX", CRCX, CRCX_RET, 97},
447 {"MDCX3", MDCX3, MDCX3_RET, PTYPE_IGNORE},
448 {"MDCX4", MDCX4, MDCX4_RET("18983216"), 99},
449 {"MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99},
450 {"MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99},
451 {"MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99},
452 {"MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99},
453 {"MDCX4_RO", MDCX4_RO, MDCX4_RO_RET("18983221"), PTYPE_IGNORE},
454 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
455 {"CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97},
456 {"EMPTY", EMPTY, EMPTY_RET},
457 {"SHORT1", SHORT, SHORT_RET},
458 {"SHORT2", SHORT2, SHORT2_RET},
459 {"SHORT3", SHORT3, SHORT2_RET},
460 {"SHORT4", SHORT4, SHORT2_RET},
461 {"RQNT1", RQNT, RQNT1_RET},
462 {"RQNT2", RQNT2, RQNT2_RET},
463 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
464 {"CRCX", CRCX, CRCX_FMTP_RET, 97,.extra_fmtp = "a=fmtp:126 0/1/2"},
465 {"MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE,.extra_fmtp =
466 "a=fmtp:126 0/1/2"},
467 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE,.extra_fmtp = "a=fmtp:126 0/1/2"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200468};
469
470static const struct mgcp_test retransmit[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200471 {"CRCX", CRCX, CRCX_RET},
472 {"RQNT1", RQNT, RQNT1_RET},
473 {"RQNT2", RQNT2, RQNT2_RET},
474 {"MDCX3", MDCX3, MDCX3A_RET},
475 {"DLCX", DLCX, DLCX_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200476};
477
Philipp Maierffd75e42017-11-22 11:44:50 +0100478static struct msgb *create_msg(const char *str, const char *conn_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200479{
480 struct msgb *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100481 int len;
482
483 printf("creating message from statically defined input:\n");
484 printf("---------8<---------\n%s\n---------8<---------\n", str);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200485
486 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Philipp Maierffd75e42017-11-22 11:44:50 +0100487 if (conn_id && strlen(conn_id))
488 len = sprintf((char *)msg->data, str, conn_id, conn_id);
489 else
490 len = sprintf((char *)msg->data, "%s", str);
491
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200492 msg->l2h = msgb_put(msg, len);
493 return msg;
494}
495
496static int last_endpoint = -1;
497
498static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
499 int state, const char *transactio_id)
500{
501 fprintf(stderr, "Policy CB got state %d on endpoint %d\n",
502 state, endpoint);
503 last_endpoint = endpoint;
504 return MGCP_POLICY_CONT;
505}
506
507#define MGCP_DUMMY_LOAD 0x23
508static int dummy_packets = 0;
509/* override and forward */
510ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200511 const struct sockaddr *dest_addr, socklen_t addrlen)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200512{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513 uint32_t dest_host =
514 htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
515 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200516
Philipp Maier87bd9be2017-08-22 16:35:41 +0200517 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
518 fprintf(stderr,
519 "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
520 dest_host, dest_port, len, osmo_hexdump(buf, len));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200521 dummy_packets += 1;
522 }
523
Philipp Maier3d9b6562017-10-13 18:33:44 +0200524 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525}
526
527static int64_t force_monotonic_time_us = -1;
528/* override and forward */
529int clock_gettime(clockid_t clk_id, struct timespec *tp)
530{
531 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
532 static clock_gettime_t real_clock_gettime = NULL;
533
534 if (!real_clock_gettime)
535 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
536
537 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
538 tp->tv_sec = force_monotonic_time_us / 1000000;
539 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
540 return 0;
541 }
542
543 return real_clock_gettime(clk_id, tp);
544}
545
546#define CONN_UNMODIFIED (0x1000)
547
548static void test_values(void)
549{
550 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200552
553 /* Check that LOOPBACK enables all output */
554 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200555 MGCP_CONN_RECV_SEND);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200556}
557
Philipp Maierffd75e42017-11-22 11:44:50 +0100558/* Extract a connection ID from a response (CRCX) */
559static int get_conn_id_from_response(uint8_t *resp, char *conn_id,
560 unsigned int conn_id_len)
561{
562 char *conn_id_ptr;
563 int i;
564
565 conn_id_ptr = strstr((char *)resp, "I: ");
566 if (!conn_id_ptr)
567 return -EINVAL;
568
569 memset(conn_id, 0, conn_id_len);
570 memcpy(conn_id, conn_id_ptr + 3, 32);
571
572 for (i = 0; i < conn_id_len; i++) {
573 if (conn_id[i] == '\n' || conn_id[i] == '\r')
574 conn_id[i] = '\0';
575 }
576
577 /* A valid conn_id must at least contain one digit, and must
578 * not exceed a length of 32 digits */
579 OSMO_ASSERT(strlen(conn_id) <= 32);
580 OSMO_ASSERT(strlen(conn_id) > 0);
581
582 return 0;
583}
584
585/* Check response, automatically patch connection ID if needed */
586static int check_response(uint8_t *resp, const char *exp_resp)
587{
588 char exp_resp_patched[4096];
589 const char *exp_resp_ptr;
590 char conn_id[256];
591
592 printf("checking response:\n");
593
594 /* If the expected response is intened to be patched
595 * (%s placeholder inside) we will patch it with the
596 * connection identifier we just received from the
597 * real response. This is necessary because the CI
598 * is generated by the mgcp code on CRCX and we can
599 * not know it in advance */
600 if (strstr(exp_resp, "%s")) {
601 if (get_conn_id_from_response(resp, conn_id, sizeof(conn_id)) ==
602 0) {
603 sprintf(exp_resp_patched, exp_resp, conn_id, conn_id);
604 exp_resp_ptr = exp_resp_patched;
605 printf
606 ("using message with patched conn_id for comparison\n");
607 } else {
608 printf
609 ("patching conn_id failed, using message as statically defined for comparison\n");
610 exp_resp_ptr = exp_resp;
611 }
612 } else {
613 printf("using message as statically defined for comparison\n");
614 exp_resp_ptr = exp_resp;
615 }
616
617 if (strcmp((char *)resp, exp_resp_ptr) != 0) {
618 printf("Unexpected response, please check!\n");
619 printf
620 ("Got:\n---------8<---------\n%s\n---------8<---------\n\n",
621 resp);
622 printf
623 ("Expected:\n---------8<---------\n%s\n---------8<---------\n",
624 exp_resp_ptr);
625 return -EINVAL;
626 }
627
628 printf("Response matches our expectations.\n");
629 return 0;
630}
631
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200632static void test_messages(void)
633{
634 struct mgcp_config *cfg;
635 struct mgcp_endpoint *endp;
636 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200637 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100638 char last_conn_id[256];
Philipp Maier7df419b2017-12-04 17:11:42 +0100639 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200640
641 cfg = mgcp_config_alloc();
642
Philipp Maierfcd06552017-11-10 17:32:22 +0100643 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200644 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200645 cfg->policy_cb = mgcp_test_policy_cb;
646
Philipp Maierffd75e42017-11-22 11:44:50 +0100647 memset(last_conn_id, 0, sizeof(last_conn_id));
648
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200649 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
650
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200651 for (i = 0; i < ARRAY_SIZE(tests); i++) {
652 const struct mgcp_test *t = &tests[i];
653 struct msgb *inp;
654 struct msgb *msg;
655
Philipp Maierffd75e42017-11-22 11:44:50 +0100656 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200657 printf("Testing %s\n", t->name);
658
659 last_endpoint = -1;
660 dummy_packets = 0;
661
Philipp Maier87bd9be2017-08-22 16:35:41 +0200662 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
663 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200664
Philipp Maierffd75e42017-11-22 11:44:50 +0100665 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200666 msg = mgcp_handle_message(cfg, inp);
667 msgb_free(inp);
668 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200669 if (msg) {
670 printf("%s failed '%s'\n", t->name,
671 (char *)msg->data);
672 OSMO_ASSERT(false);
673 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100674 } else if (check_response(msg->data, t->exp_resp) != 0) {
675 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200676 OSMO_ASSERT(false);
677 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100678
Philipp Maier7df419b2017-12-04 17:11:42 +0100679 if (msg) {
680 rc = get_conn_id_from_response(msg->data, last_conn_id,
681 sizeof(last_conn_id));
682 if (rc)
683 printf("(response contains a connection id)\n");
684 else
685 printf("(response does not contain a connection id)\n");
686 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100687
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688 msgb_free(msg);
689
690 if (dummy_packets)
691 printf("Dummy packets: %d\n", dummy_packets);
692
693 if (last_endpoint != -1) {
694 endp = &cfg->trunk.endpoints[last_endpoint];
695
Philipp Maier01d24a32017-11-21 17:26:09 +0100696 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200697 if (conn) {
698 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200699
Philipp Maier87bd9be2017-08-22 16:35:41 +0200700 if (conn->end.packet_duration_ms != -1)
701 printf("Detected packet duration: %d\n",
702 conn->end.packet_duration_ms);
703 else
704 printf("Packet duration not set\n");
705 if (endp->local_options.pkt_period_min ||
706 endp->local_options.pkt_period_max)
707 printf
708 ("Requested packetetization period: "
709 "%d-%d\n",
710 endp->local_options.pkt_period_min,
711 endp->
712 local_options.pkt_period_max);
713 else
714 printf
715 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200716
Philipp Maier87bd9be2017-08-22 16:35:41 +0200717 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
718 printf("Connection mode: %d:%s%s%s%s\n",
719 conn->conn->mode,
720 !conn->conn->mode ? " NONE" : "",
721 conn->conn->mode & MGCP_CONN_SEND_ONLY
722 ? " SEND" : "",
723 conn->conn->mode & MGCP_CONN_RECV_ONLY
724 ? " RECV" : "",
725 conn->conn->mode & MGCP_CONN_LOOPBACK
726 & ~MGCP_CONN_RECV_SEND
727 ? " LOOP" : "");
728 fprintf(stderr,
729 "RTP output %sabled, NET output %sabled\n",
730 conn->end.output_enabled
731 ? "en" : "dis",
732 conn->end.output_enabled
733 ? "en" : "dis");
734 } else
735 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200736
Philipp Maier87bd9be2017-08-22 16:35:41 +0200737 OSMO_ASSERT(conn->end.output_enabled
738 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
739
740 conn->conn->mode |= CONN_UNMODIFIED;
741
742 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200743 endp->local_options.pkt_period_min = 0;
744 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200745 }
746
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200747 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100748 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200749 OSMO_ASSERT(last_endpoint != -1);
750 endp = &cfg->trunk.endpoints[last_endpoint];
751
752 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200753 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200754 last_endpoint,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200755 conn->end.codec.payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200756
Philipp Maier87bd9be2017-08-22 16:35:41 +0200757 if (t->ptype != PTYPE_IGNORE)
758 OSMO_ASSERT(conn->end.codec.payload_type ==
759 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200760
761 /* Reset them again for next test */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200762 conn->end.codec.payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200763 }
764 }
765
766 talloc_free(cfg);
767}
768
769static void test_retransmission(void)
770{
771 struct mgcp_config *cfg;
772 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100773 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100774 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200775
776 cfg = mgcp_config_alloc();
777
Philipp Maierfcd06552017-11-10 17:32:22 +0100778 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200779 mgcp_endpoints_allocate(&cfg->trunk);
780
Philipp Maierffd75e42017-11-22 11:44:50 +0100781 memset(last_conn_id, 0, sizeof(last_conn_id));
782
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200783 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
784
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200785 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
786 const struct mgcp_test *t = &retransmit[i];
787 struct msgb *inp;
788 struct msgb *msg;
789
Philipp Maierffd75e42017-11-22 11:44:50 +0100790 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200791 printf("Testing %s\n", t->name);
792
Philipp Maierffd75e42017-11-22 11:44:50 +0100793 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200794 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200795
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200796 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100797 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200798 printf("%s failed '%s'\n", t->name, (char *)msg->data);
799 OSMO_ASSERT(false);
800 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100801
Philipp Maier23b8e292017-12-04 16:48:45 +0100802 if (msg && strcmp(t->name, "CRCX") == 0) {
803 rc = get_conn_id_from_response(msg->data, last_conn_id,
804 sizeof(last_conn_id));
805 OSMO_ASSERT(rc == 0);
806 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100807
Philipp Maier7cedfd72017-12-04 16:49:12 +0100808 if (msg)
809 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200810
811 /* Retransmit... */
812 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100813 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200814 msg = mgcp_handle_message(cfg, inp);
815 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100816 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200817 printf("%s failed '%s'\n", t->name, (char *)msg->data);
818 OSMO_ASSERT(false);
819 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200820 msgb_free(msg);
821 }
822
823 talloc_free(cfg);
824}
825
826static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
827{
828 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200829 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200830 return 0;
831}
832
833static void test_rqnt_cb(void)
834{
835 struct mgcp_config *cfg;
836 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100837 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838
839 cfg = mgcp_config_alloc();
840 cfg->rqnt_cb = rqnt_cb;
841
Philipp Maierfcd06552017-11-10 17:32:22 +0100842 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200843 mgcp_endpoints_allocate(&cfg->trunk);
844
845 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
846
Philipp Maierffd75e42017-11-22 11:44:50 +0100847 inp = create_msg(CRCX, NULL);
848 msg = mgcp_handle_message(cfg, inp);
849 OSMO_ASSERT(msg);
850 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
851 sizeof(conn_id)) == 0);
852 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853 msgb_free(inp);
854
855 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100856 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200857 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200858 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200859 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
860 abort();
861 }
862
Philipp Maier87bd9be2017-08-22 16:35:41 +0200863 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200864 printf("FAILED: callback not called: %p\n", cfg->data);
865 abort();
866 }
867
868 msgb_free(msg);
869 msgb_free(inp);
870
Philipp Maierffd75e42017-11-22 11:44:50 +0100871 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872 msgb_free(mgcp_handle_message(cfg, inp));
873 msgb_free(inp);
874 talloc_free(cfg);
875}
876
877struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200878 int cycles;
879 uint16_t base_seq;
880 uint16_t max_seq;
881 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200882
Philipp Maier87bd9be2017-08-22 16:35:41 +0200883 uint32_t expected;
884 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200885};
886
887static const struct pl_test pl_test_dat[] = {
888 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200889 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
890 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200892 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
893 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200895 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
896 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200897 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200898 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
899 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200900 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200901 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
902 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200903};
904
905static void test_packet_loss_calc(void)
906{
907 int i;
908 printf("Testing packet loss calculation.\n");
909
910 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
911 uint32_t expected;
912 int loss;
913 struct mgcp_rtp_state state;
914 struct mgcp_rtp_end rtp;
915 memset(&state, 0, sizeof(state));
916 memset(&rtp, 0, sizeof(rtp));
917
918 state.stats_initialized = 1;
919 state.stats_base_seq = pl_test_dat[i].base_seq;
920 state.stats_max_seq = pl_test_dat[i].max_seq;
921 state.stats_cycles = pl_test_dat[i].cycles;
922
Philipp Maier87bd9be2017-08-22 16:35:41 +0200923 rtp.packets_rx = pl_test_dat[i].packets;
924 calc_loss(&state, &rtp, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200925
Philipp Maier87bd9be2017-08-22 16:35:41 +0200926 if (loss != pl_test_dat[i].loss
927 || expected != pl_test_dat[i].expected) {
928 printf
929 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
930 i, loss, pl_test_dat[i].loss, expected,
931 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200932 }
933 }
934}
935
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
937 uint32_t *pr, uint32_t *_or, int *loss,
938 uint32_t *jitter)
939{
940 char *line, *save;
941 int rc;
942
943 /* initialize with bad values */
944 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
945 *loss = INT_MAX;
946
947 line = strtok_r((char *)msg->l2h, "\r\n", &save);
948 if (!line)
949 return -1;
950
951 /* this can only parse the message that is created above... */
952 for_each_non_empty_line(line, save) {
953 switch (line[0]) {
954 case 'P':
955 rc = sscanf(line,
956 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
957 ps, os, pr, _or, loss, jitter);
958 return rc == 6 ? 0 : -1;
959 }
960 }
961
962 return -1;
963}
964
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200965static void test_mgcp_stats(void)
966{
967 printf("Testing stat parsing\n");
968
969 uint32_t bps, bos, pr, _or, jitter;
970 struct msgb *msg;
971 int loss;
972 int rc;
973
Philipp Maierffd75e42017-11-22 11:44:50 +0100974 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200975 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
976 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200977 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
978 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200979 printf("FAIL: Parsing failed1.\n");
980 msgb_free(msg);
981
Philipp Maier87bd9be2017-08-22 16:35:41 +0200982 msg =
983 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +0100984 ("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 +0200985 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
986 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200987 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
988 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200989 printf("FAIL: Parsing failed2.\n");
990 msgb_free(msg);
991}
992
993struct rtp_packet_info {
994 float txtime;
995 int len;
996 char *data;
997};
998
999struct rtp_packet_info test_rtp_packets1[] = {
1000 /* RTP: SeqNo=0, TS=0 */
1001 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001002 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001003 /* RTP: SeqNo=1, TS=160 */
1004 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001005 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001006 /* RTP: SeqNo=2, TS=320 */
1007 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001008 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001009 /* Repeat RTP timestamp: */
1010 /* RTP: SeqNo=3, TS=320 */
1011 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001012 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001013 /* RTP: SeqNo=4, TS=480 */
1014 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001015 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001016 /* RTP: SeqNo=5, TS=640 */
1017 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001018 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001019 /* Double skip RTP timestamp (delta = 2*160): */
1020 /* RTP: SeqNo=6, TS=960 */
1021 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001022 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001023 /* RTP: SeqNo=7, TS=1120 */
1024 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001026 /* RTP: SeqNo=8, TS=1280 */
1027 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001028 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001029 /* Non 20ms RTP timestamp (delta = 120): */
1030 /* RTP: SeqNo=9, TS=1400 */
1031 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001032 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001033 /* RTP: SeqNo=10, TS=1560 */
1034 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001035 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001036 /* RTP: SeqNo=11, TS=1720 */
1037 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001038 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001039 /* SSRC changed to 0x10203040, RTP timestamp jump */
1040 /* RTP: SeqNo=12, TS=34688 */
1041 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001042 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001043 /* RTP: SeqNo=13, TS=34848 */
1044 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001045 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001046 /* RTP: SeqNo=14, TS=35008 */
1047 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001048 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001049 /* Non 20ms RTP timestamp (delta = 120): */
1050 /* RTP: SeqNo=15, TS=35128 */
1051 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001052 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001053 /* RTP: SeqNo=16, TS=35288 */
1054 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001055 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001056 /* RTP: SeqNo=17, TS=35448 */
1057 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001058 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001059 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1060 /* RTP: SeqNo=19, TS=35768 */
1061 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001062 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001063 /* RTP: SeqNo=20, TS=35928 */
1064 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001065 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001066 /* RTP: SeqNo=21, TS=36088 */
1067 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001068 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001069 /* Repeat last packet */
1070 /* RTP: SeqNo=21, TS=36088 */
1071 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001072 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001073 /* RTP: SeqNo=22, TS=36248 */
1074 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001075 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001076 /* RTP: SeqNo=23, TS=36408 */
1077 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001078 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001079 /* Don't increment SeqNo but increment timestamp by 160 */
1080 /* RTP: SeqNo=23, TS=36568 */
1081 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001082 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001083 /* RTP: SeqNo=24, TS=36728 */
1084 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001085 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001086 /* RTP: SeqNo=25, TS=36888 */
1087 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001088 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001089 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1090 * SeqNo jump */
1091 /* RTP: SeqNo=1000, TS=160000 */
1092 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001093 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001094 /* RTP: SeqNo=1001, TS=160160 */
1095 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001096 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001097 /* RTP: SeqNo=1002, TS=160320 */
1098 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001099 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001100};
1101
Philipp Maier87bd9be2017-08-22 16:35:41 +02001102void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1103 struct mgcp_rtp_state *state,
1104 struct mgcp_rtp_end *rtp_end,
1105 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106
1107static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1108{
1109 int i;
1110
1111 struct mgcp_trunk_config trunk;
1112 struct mgcp_endpoint endp;
1113 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001114 struct mgcp_rtp_end *rtp;
1115 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001116 char buffer[4096];
1117 uint32_t last_ssrc = 0;
1118 uint32_t last_timestamp = 0;
1119 uint32_t last_seqno = 0;
1120 int last_in_ts_err_cnt = 0;
1121 int last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001122 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001123 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001124
1125 printf("Testing packet error detection%s%s.\n",
1126 patch_ssrc ? ", patch SSRC" : "",
1127 patch_ts ? ", patch timestamps" : "");
1128
1129 memset(&trunk, 0, sizeof(trunk));
1130 memset(&endp, 0, sizeof(endp));
1131 memset(&state, 0, sizeof(state));
1132
Philipp Maier87bd9be2017-08-22 16:35:41 +02001133 endp.type = &ep_typeset.rtp;
1134
Philipp Maierfcd06552017-11-10 17:32:22 +01001135 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001136 trunk.endpoints = &endp;
1137 trunk.force_constant_ssrc = patch_ssrc;
1138 trunk.force_aligned_timing = patch_ts;
1139
1140 endp.tcfg = &trunk;
1141
Philipp Maier87bd9be2017-08-22 16:35:41 +02001142 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001143 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1144 "test-connection");
1145 OSMO_ASSERT(_conn);
1146 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001147 OSMO_ASSERT(conn);
1148
1149 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001150
1151 rtp->codec.payload_type = 98;
1152
1153 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1154 struct rtp_packet_info *info = test_rtp_packets1 + i;
1155
1156 force_monotonic_time_us = round(1000000.0 * info->txtime);
1157
1158 OSMO_ASSERT(info->len <= sizeof(buffer));
1159 OSMO_ASSERT(info->len >= 0);
1160 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001161 mgcp_rtp_end_config(&endp, 1, rtp);
1162
1163 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1164 buffer, info->len);
1165
1166 if (state.out_stream.ssrc != last_ssrc) {
1167 printf("Output SSRC changed to %08x\n",
1168 state.out_stream.ssrc);
1169 last_ssrc = state.out_stream.ssrc;
1170 }
1171
1172 printf("In TS: %d, dTS: %d, Seq: %d\n",
1173 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001174 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001175
1176 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
1177 "TS Err change: in %+d, out %+d\n",
1178 state.out_stream.last_timestamp - last_timestamp,
1179 state.out_stream.last_tsdelta,
1180 state.out_stream.last_seq - last_seqno,
1181 state.in_stream.err_ts_counter - last_in_ts_err_cnt,
1182 state.out_stream.err_ts_counter - last_out_ts_err_cnt);
1183
1184 printf("Stats: Jitter = %u, Transit = %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001185 calc_jitter(&state), state.stats_transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001186
1187 last_in_ts_err_cnt = state.in_stream.err_ts_counter;
1188 last_out_ts_err_cnt = state.out_stream.err_ts_counter;
1189 last_timestamp = state.out_stream.last_timestamp;
1190 last_seqno = state.out_stream.last_seq;
1191 }
1192
1193 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001194 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001195}
1196
1197static void test_multilple_codec(void)
1198{
1199 struct mgcp_config *cfg;
1200 struct mgcp_endpoint *endp;
1201 struct msgb *inp, *resp;
1202 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001203 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001204 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001205
1206 printf("Testing multiple payload types\n");
1207
1208 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001209 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001210 mgcp_endpoints_allocate(&cfg->trunk);
1211 cfg->policy_cb = mgcp_test_policy_cb;
1212 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1213
1214 /* Allocate endpoint 1@mgw with two codecs */
1215 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001216 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001217 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001218 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1219 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001220 msgb_free(inp);
1221 msgb_free(resp);
1222
1223 OSMO_ASSERT(last_endpoint == 1);
1224 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001225 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001226 OSMO_ASSERT(conn);
1227 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1228 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001229
1230 /* Allocate 2@mgw with three codecs, last one ignored */
1231 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001232 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001233 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001234 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1235 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001236 msgb_free(inp);
1237 msgb_free(resp);
1238
1239 OSMO_ASSERT(last_endpoint == 2);
1240 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001241 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001242 OSMO_ASSERT(conn);
1243 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1244 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001245
1246 /* Allocate 3@mgw with no codecs, check for PT == -1 */
1247 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001248 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001249 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001250 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1251 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001252 msgb_free(inp);
1253 msgb_free(resp);
1254
1255 OSMO_ASSERT(last_endpoint == 3);
1256 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001257 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001258 OSMO_ASSERT(conn);
1259 OSMO_ASSERT(conn->end.codec.payload_type == -1);
1260 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001261
1262 /* Allocate 4@mgw with a single codec */
1263 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001264 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001265 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001266 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1267 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001268 msgb_free(inp);
1269 msgb_free(resp);
1270
1271 OSMO_ASSERT(last_endpoint == 4);
1272 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001273 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001274 OSMO_ASSERT(conn);
1275 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1276 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001277
1278 /* Allocate 5@mgw at select GSM.. */
1279 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001280 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001281 talloc_free(cfg->trunk.audio_name);
1282 cfg->trunk.audio_name = "GSM/8000";
1283 cfg->trunk.no_audio_transcoding = 1;
1284 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001285 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1286 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001287 msgb_free(inp);
1288 msgb_free(resp);
1289
1290 OSMO_ASSERT(last_endpoint == 5);
1291 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001292 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001293 OSMO_ASSERT(conn);
1294 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1295 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001296
Philipp Maierffd75e42017-11-22 11:44:50 +01001297 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001298 last_endpoint = -1;
1299 resp = mgcp_handle_message(cfg, inp);
1300 msgb_free(inp);
1301 msgb_free(resp);
1302 OSMO_ASSERT(last_endpoint == 5);
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);
1306 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1307 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
1308 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001309 memset(&addr, 0, sizeof(addr));
1310 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001311 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001312
1313 /* Check what happens without that flag */
1314
Philipp Maier87bd9be2017-08-22 16:35:41 +02001315 /* Free the previous endpoint and the data and
1316 * check if the connection really vanished... */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001317 mgcp_release_endp(endp);
1318 talloc_free(endp->last_response);
1319 talloc_free(endp->last_trans);
1320 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001321 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001322 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001323
1324 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001325 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001326 cfg->trunk.no_audio_transcoding = 0;
1327 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001328 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1329 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001330 msgb_free(inp);
1331 msgb_free(resp);
1332
1333 OSMO_ASSERT(last_endpoint == 5);
1334 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001335 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001336 OSMO_ASSERT(conn);
1337 OSMO_ASSERT(conn->end.codec.payload_type == 255);
1338 OSMO_ASSERT(conn->end.alt_codec.payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001339
1340 talloc_free(cfg);
1341}
1342
1343static void test_no_cycle(void)
1344{
1345 struct mgcp_config *cfg;
1346 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001347 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001348 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001349
1350 printf("Testing no sequence flow on initial packet\n");
1351
1352 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001353 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001354 mgcp_endpoints_allocate(&cfg->trunk);
1355
1356 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001357
Philipp Maierffd75e42017-11-22 11:44:50 +01001358 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1359 "test-connection");
1360 OSMO_ASSERT(_conn);
1361 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001362 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001363
Philipp Maier87bd9be2017-08-22 16:35:41 +02001364 OSMO_ASSERT(conn->state.stats_initialized == 0);
1365
1366 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
1367 OSMO_ASSERT(conn->state.stats_initialized == 1);
1368 OSMO_ASSERT(conn->state.stats_cycles == 0);
1369 OSMO_ASSERT(conn->state.stats_max_seq == 0);
1370
1371 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
1372 OSMO_ASSERT(conn->state.stats_initialized == 1);
1373 OSMO_ASSERT(conn->state.stats_cycles == 0);
1374 OSMO_ASSERT(conn->state.stats_max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001375
1376 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001377 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
1378 OSMO_ASSERT(conn->state.stats_initialized == 1);
1379 OSMO_ASSERT(conn->state.stats_cycles == 0);
1380 OSMO_ASSERT(conn->state.stats_max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001381
1382 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001383 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
1384 OSMO_ASSERT(conn->state.stats_initialized == 1);
1385 OSMO_ASSERT(conn->state.stats_cycles == UINT16_MAX + 1);
1386 OSMO_ASSERT(conn->state.stats_max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001387
Neels Hofmeyrb597b4f2017-11-18 21:28:35 +01001388 mgcp_release_endp(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001389 talloc_free(cfg);
1390}
1391
1392static void test_no_name(void)
1393{
1394 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001395 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001396
1397 printf("Testing no rtpmap name\n");
1398 cfg = mgcp_config_alloc();
1399
Philipp Maierfcd06552017-11-10 17:32:22 +01001400 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001401 cfg->trunk.audio_send_name = 0;
1402 mgcp_endpoints_allocate(&cfg->trunk);
1403
1404 cfg->policy_cb = mgcp_test_policy_cb;
1405
1406 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1407
Philipp Maierffd75e42017-11-22 11:44:50 +01001408 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001409 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001410
1411 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001412 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001413 (char *)msg->data);
1414 OSMO_ASSERT(false);
1415 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001416 msgb_free(inp);
1417 msgb_free(msg);
1418
1419 mgcp_release_endp(&cfg->trunk.endpoints[1]);
1420 talloc_free(cfg);
1421}
1422
1423static void test_osmux_cid(void)
1424{
1425 int id, i;
1426
1427 OSMO_ASSERT(osmux_used_cid() == 0);
1428 id = osmux_get_cid();
1429 OSMO_ASSERT(id == 0);
1430 OSMO_ASSERT(osmux_used_cid() == 1);
1431 osmux_put_cid(id);
1432 OSMO_ASSERT(osmux_used_cid() == 0);
1433
1434 for (i = 0; i < 256; ++i) {
1435 id = osmux_get_cid();
1436 OSMO_ASSERT(id == i);
1437 OSMO_ASSERT(osmux_used_cid() == i + 1);
1438 }
1439
1440 id = osmux_get_cid();
1441 OSMO_ASSERT(id == -1);
1442
1443 for (i = 0; i < 256; ++i)
1444 osmux_put_cid(i);
1445 OSMO_ASSERT(osmux_used_cid() == 0);
1446}
1447
1448static const struct log_info_cat log_categories[] = {
1449};
1450
1451const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001452 .cat = log_categories,
1453 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001454};
1455
1456int main(int argc, char **argv)
1457{
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001458 void *msgb_ctx = msgb_talloc_ctx_init(NULL, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001459 osmo_init_logging(&log_info);
1460
1461 test_strline();
1462 test_values();
1463 test_messages();
1464 test_retransmission();
1465 test_packet_loss_calc();
1466 test_rqnt_cb();
1467 test_mgcp_stats();
1468 test_packet_error_detection(1, 0);
1469 test_packet_error_detection(0, 0);
1470 test_packet_error_detection(0, 1);
1471 test_packet_error_detection(1, 1);
1472 test_multilple_codec();
1473 test_no_cycle();
1474 test_no_name();
1475 test_osmux_cid();
1476
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001477 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1478 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1479 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001480 printf("Done\n");
1481 return EXIT_SUCCESS;
1482}