blob: 46fd69b1a07c36be4a0908e34280fc95a76cd473 [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"
Harald Welteabbb6b92017-12-28 13:13:50 +010079#define MDCX_ERR_RET "500 18983213 FAIL\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020080#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
Philipp Maiera330b862017-12-04 17:16:16 +0100688 if (msg)
689 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200690
691 if (dummy_packets)
692 printf("Dummy packets: %d\n", dummy_packets);
693
694 if (last_endpoint != -1) {
695 endp = &cfg->trunk.endpoints[last_endpoint];
696
Philipp Maier01d24a32017-11-21 17:26:09 +0100697 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200698 if (conn) {
699 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200700
Philipp Maier87bd9be2017-08-22 16:35:41 +0200701 if (conn->end.packet_duration_ms != -1)
702 printf("Detected packet duration: %d\n",
703 conn->end.packet_duration_ms);
704 else
705 printf("Packet duration not set\n");
706 if (endp->local_options.pkt_period_min ||
707 endp->local_options.pkt_period_max)
708 printf
709 ("Requested packetetization period: "
710 "%d-%d\n",
711 endp->local_options.pkt_period_min,
712 endp->
713 local_options.pkt_period_max);
714 else
715 printf
716 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200717
Philipp Maier87bd9be2017-08-22 16:35:41 +0200718 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
719 printf("Connection mode: %d:%s%s%s%s\n",
720 conn->conn->mode,
721 !conn->conn->mode ? " NONE" : "",
722 conn->conn->mode & MGCP_CONN_SEND_ONLY
723 ? " SEND" : "",
724 conn->conn->mode & MGCP_CONN_RECV_ONLY
725 ? " RECV" : "",
726 conn->conn->mode & MGCP_CONN_LOOPBACK
727 & ~MGCP_CONN_RECV_SEND
728 ? " LOOP" : "");
729 fprintf(stderr,
730 "RTP output %sabled, NET output %sabled\n",
731 conn->end.output_enabled
732 ? "en" : "dis",
733 conn->end.output_enabled
734 ? "en" : "dis");
735 } else
736 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200737
Philipp Maier87bd9be2017-08-22 16:35:41 +0200738 OSMO_ASSERT(conn->end.output_enabled
739 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
740
741 conn->conn->mode |= CONN_UNMODIFIED;
742
743 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200744 endp->local_options.pkt_period_min = 0;
745 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746 }
747
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200748 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100749 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200750 OSMO_ASSERT(last_endpoint != -1);
751 endp = &cfg->trunk.endpoints[last_endpoint];
752
753 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200754 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200755 last_endpoint,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200756 conn->end.codec.payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757
Philipp Maier87bd9be2017-08-22 16:35:41 +0200758 if (t->ptype != PTYPE_IGNORE)
759 OSMO_ASSERT(conn->end.codec.payload_type ==
760 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200761
762 /* Reset them again for next test */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200763 conn->end.codec.payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200764 }
765 }
766
767 talloc_free(cfg);
768}
769
770static void test_retransmission(void)
771{
772 struct mgcp_config *cfg;
773 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100774 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100775 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200776
777 cfg = mgcp_config_alloc();
778
Philipp Maierfcd06552017-11-10 17:32:22 +0100779 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200780 mgcp_endpoints_allocate(&cfg->trunk);
781
Philipp Maierffd75e42017-11-22 11:44:50 +0100782 memset(last_conn_id, 0, sizeof(last_conn_id));
783
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200784 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
785
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200786 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
787 const struct mgcp_test *t = &retransmit[i];
788 struct msgb *inp;
789 struct msgb *msg;
790
Philipp Maierffd75e42017-11-22 11:44:50 +0100791 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200792 printf("Testing %s\n", t->name);
793
Philipp Maierffd75e42017-11-22 11:44:50 +0100794 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200795 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200796
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200797 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100798 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200799 printf("%s failed '%s'\n", t->name, (char *)msg->data);
800 OSMO_ASSERT(false);
801 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100802
Philipp Maier23b8e292017-12-04 16:48:45 +0100803 if (msg && strcmp(t->name, "CRCX") == 0) {
804 rc = get_conn_id_from_response(msg->data, last_conn_id,
805 sizeof(last_conn_id));
806 OSMO_ASSERT(rc == 0);
807 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100808
Philipp Maier7cedfd72017-12-04 16:49:12 +0100809 if (msg)
810 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811
812 /* Retransmit... */
813 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100814 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200815 msg = mgcp_handle_message(cfg, inp);
816 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100817 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200818 printf("%s failed '%s'\n", t->name, (char *)msg->data);
819 OSMO_ASSERT(false);
820 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200821 msgb_free(msg);
822 }
823
824 talloc_free(cfg);
825}
826
827static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
828{
829 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200830 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200831 return 0;
832}
833
834static void test_rqnt_cb(void)
835{
836 struct mgcp_config *cfg;
837 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100838 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200839
840 cfg = mgcp_config_alloc();
841 cfg->rqnt_cb = rqnt_cb;
842
Philipp Maierfcd06552017-11-10 17:32:22 +0100843 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200844 mgcp_endpoints_allocate(&cfg->trunk);
845
846 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
847
Philipp Maierffd75e42017-11-22 11:44:50 +0100848 inp = create_msg(CRCX, NULL);
849 msg = mgcp_handle_message(cfg, inp);
850 OSMO_ASSERT(msg);
851 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
852 sizeof(conn_id)) == 0);
853 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200854 msgb_free(inp);
855
856 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100857 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200859 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200860 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
861 abort();
862 }
863
Philipp Maier87bd9be2017-08-22 16:35:41 +0200864 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200865 printf("FAILED: callback not called: %p\n", cfg->data);
866 abort();
867 }
868
869 msgb_free(msg);
870 msgb_free(inp);
871
Philipp Maierffd75e42017-11-22 11:44:50 +0100872 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200873 msgb_free(mgcp_handle_message(cfg, inp));
874 msgb_free(inp);
875 talloc_free(cfg);
876}
877
878struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200879 int cycles;
880 uint16_t base_seq;
881 uint16_t max_seq;
882 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200883
Philipp Maier87bd9be2017-08-22 16:35:41 +0200884 uint32_t expected;
885 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200886};
887
888static const struct pl_test pl_test_dat[] = {
889 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200890 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
891 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200892 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200893 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
894 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200895 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200896 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
897 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200898 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200899 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
900 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200902 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
903 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904};
905
906static void test_packet_loss_calc(void)
907{
908 int i;
909 printf("Testing packet loss calculation.\n");
910
911 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
912 uint32_t expected;
913 int loss;
914 struct mgcp_rtp_state state;
915 struct mgcp_rtp_end rtp;
916 memset(&state, 0, sizeof(state));
917 memset(&rtp, 0, sizeof(rtp));
918
Harald Welte49e3d5a2017-12-25 09:47:57 +0100919 state.stats.initialized = 1;
920 state.stats.base_seq = pl_test_dat[i].base_seq;
921 state.stats.max_seq = pl_test_dat[i].max_seq;
922 state.stats.cycles = pl_test_dat[i].cycles;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200923
Harald Weltea0ac30f2017-12-25 09:52:30 +0100924 rtp.stats.packets_rx = pl_test_dat[i].packets;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925 calc_loss(&state, &rtp, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200926
Philipp Maier87bd9be2017-08-22 16:35:41 +0200927 if (loss != pl_test_dat[i].loss
928 || expected != pl_test_dat[i].expected) {
929 printf
930 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
931 i, loss, pl_test_dat[i].loss, expected,
932 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200933 }
934 }
935}
936
Philipp Maier87bd9be2017-08-22 16:35:41 +0200937int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
938 uint32_t *pr, uint32_t *_or, int *loss,
939 uint32_t *jitter)
940{
941 char *line, *save;
942 int rc;
943
944 /* initialize with bad values */
945 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
946 *loss = INT_MAX;
947
948 line = strtok_r((char *)msg->l2h, "\r\n", &save);
949 if (!line)
950 return -1;
951
952 /* this can only parse the message that is created above... */
953 for_each_non_empty_line(line, save) {
954 switch (line[0]) {
955 case 'P':
956 rc = sscanf(line,
957 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
958 ps, os, pr, _or, loss, jitter);
959 return rc == 6 ? 0 : -1;
960 }
961 }
962
963 return -1;
964}
965
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200966static void test_mgcp_stats(void)
967{
968 printf("Testing stat parsing\n");
969
970 uint32_t bps, bos, pr, _or, jitter;
971 struct msgb *msg;
972 int loss;
973 int rc;
974
Philipp Maierffd75e42017-11-22 11:44:50 +0100975 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200976 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
977 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
979 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200980 printf("FAIL: Parsing failed1.\n");
981 msgb_free(msg);
982
Philipp Maier87bd9be2017-08-22 16:35:41 +0200983 msg =
984 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +0100985 ("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 +0200986 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
987 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200988 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
989 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200990 printf("FAIL: Parsing failed2.\n");
991 msgb_free(msg);
992}
993
994struct rtp_packet_info {
995 float txtime;
996 int len;
997 char *data;
998};
999
1000struct rtp_packet_info test_rtp_packets1[] = {
1001 /* RTP: SeqNo=0, TS=0 */
1002 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001003 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001004 /* RTP: SeqNo=1, TS=160 */
1005 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001006 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001007 /* RTP: SeqNo=2, TS=320 */
1008 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001009 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001010 /* Repeat RTP timestamp: */
1011 /* RTP: SeqNo=3, TS=320 */
1012 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001013 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001014 /* RTP: SeqNo=4, TS=480 */
1015 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001016 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001017 /* RTP: SeqNo=5, TS=640 */
1018 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001019 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001020 /* Double skip RTP timestamp (delta = 2*160): */
1021 /* RTP: SeqNo=6, TS=960 */
1022 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001023 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001024 /* RTP: SeqNo=7, TS=1120 */
1025 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001026 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001027 /* RTP: SeqNo=8, TS=1280 */
1028 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001029 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001030 /* Non 20ms RTP timestamp (delta = 120): */
1031 /* RTP: SeqNo=9, TS=1400 */
1032 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001033 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001034 /* RTP: SeqNo=10, TS=1560 */
1035 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001036 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001037 /* RTP: SeqNo=11, TS=1720 */
1038 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001039 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001040 /* SSRC changed to 0x10203040, RTP timestamp jump */
1041 /* RTP: SeqNo=12, TS=34688 */
1042 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001043 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001044 /* RTP: SeqNo=13, TS=34848 */
1045 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001046 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001047 /* RTP: SeqNo=14, TS=35008 */
1048 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001049 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001050 /* Non 20ms RTP timestamp (delta = 120): */
1051 /* RTP: SeqNo=15, TS=35128 */
1052 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001053 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001054 /* RTP: SeqNo=16, TS=35288 */
1055 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001056 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001057 /* RTP: SeqNo=17, TS=35448 */
1058 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001059 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001060 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1061 /* RTP: SeqNo=19, TS=35768 */
1062 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001063 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001064 /* RTP: SeqNo=20, TS=35928 */
1065 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001066 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001067 /* RTP: SeqNo=21, TS=36088 */
1068 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001069 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001070 /* Repeat last packet */
1071 /* RTP: SeqNo=21, TS=36088 */
1072 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001073 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001074 /* RTP: SeqNo=22, TS=36248 */
1075 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001076 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001077 /* RTP: SeqNo=23, TS=36408 */
1078 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001079 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001080 /* Don't increment SeqNo but increment timestamp by 160 */
1081 /* RTP: SeqNo=23, TS=36568 */
1082 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001083 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001084 /* RTP: SeqNo=24, TS=36728 */
1085 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001086 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001087 /* RTP: SeqNo=25, TS=36888 */
1088 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001089 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001090 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1091 * SeqNo jump */
1092 /* RTP: SeqNo=1000, TS=160000 */
1093 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001094 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001095 /* RTP: SeqNo=1001, TS=160160 */
1096 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
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=1002, TS=160320 */
1099 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001101};
1102
Philipp Maier87bd9be2017-08-22 16:35:41 +02001103void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1104 struct mgcp_rtp_state *state,
1105 struct mgcp_rtp_end *rtp_end,
1106 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001107
1108static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1109{
1110 int i;
1111
1112 struct mgcp_trunk_config trunk;
1113 struct mgcp_endpoint endp;
1114 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001115 struct mgcp_rtp_end *rtp;
1116 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001117 char buffer[4096];
1118 uint32_t last_ssrc = 0;
1119 uint32_t last_timestamp = 0;
1120 uint32_t last_seqno = 0;
1121 int last_in_ts_err_cnt = 0;
1122 int last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001123 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001124 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001125
1126 printf("Testing packet error detection%s%s.\n",
1127 patch_ssrc ? ", patch SSRC" : "",
1128 patch_ts ? ", patch timestamps" : "");
1129
1130 memset(&trunk, 0, sizeof(trunk));
1131 memset(&endp, 0, sizeof(endp));
1132 memset(&state, 0, sizeof(state));
1133
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134 endp.type = &ep_typeset.rtp;
1135
Philipp Maierfcd06552017-11-10 17:32:22 +01001136 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001137 trunk.endpoints = &endp;
1138 trunk.force_constant_ssrc = patch_ssrc;
1139 trunk.force_aligned_timing = patch_ts;
1140
1141 endp.tcfg = &trunk;
1142
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001144 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1145 "test-connection");
1146 OSMO_ASSERT(_conn);
1147 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001148 OSMO_ASSERT(conn);
1149
1150 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001151
1152 rtp->codec.payload_type = 98;
1153
1154 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1155 struct rtp_packet_info *info = test_rtp_packets1 + i;
1156
1157 force_monotonic_time_us = round(1000000.0 * info->txtime);
1158
1159 OSMO_ASSERT(info->len <= sizeof(buffer));
1160 OSMO_ASSERT(info->len >= 0);
1161 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001162 mgcp_rtp_end_config(&endp, 1, rtp);
1163
1164 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1165 buffer, info->len);
1166
1167 if (state.out_stream.ssrc != last_ssrc) {
1168 printf("Output SSRC changed to %08x\n",
1169 state.out_stream.ssrc);
1170 last_ssrc = state.out_stream.ssrc;
1171 }
1172
1173 printf("In TS: %d, dTS: %d, Seq: %d\n",
1174 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001175 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001176
1177 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
1178 "TS Err change: in %+d, out %+d\n",
1179 state.out_stream.last_timestamp - last_timestamp,
1180 state.out_stream.last_tsdelta,
1181 state.out_stream.last_seq - last_seqno,
1182 state.in_stream.err_ts_counter - last_in_ts_err_cnt,
1183 state.out_stream.err_ts_counter - last_out_ts_err_cnt);
1184
1185 printf("Stats: Jitter = %u, Transit = %d\n",
Harald Welte49e3d5a2017-12-25 09:47:57 +01001186 calc_jitter(&state), state.stats.transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001187
1188 last_in_ts_err_cnt = state.in_stream.err_ts_counter;
1189 last_out_ts_err_cnt = state.out_stream.err_ts_counter;
1190 last_timestamp = state.out_stream.last_timestamp;
1191 last_seqno = state.out_stream.last_seq;
1192 }
1193
1194 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001195 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001196}
1197
1198static void test_multilple_codec(void)
1199{
1200 struct mgcp_config *cfg;
1201 struct mgcp_endpoint *endp;
1202 struct msgb *inp, *resp;
1203 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001204 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001205 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001206
1207 printf("Testing multiple payload types\n");
1208
1209 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001210 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001211 mgcp_endpoints_allocate(&cfg->trunk);
1212 cfg->policy_cb = mgcp_test_policy_cb;
1213 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1214
1215 /* Allocate endpoint 1@mgw with two codecs */
1216 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001217 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001218 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001219 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1220 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001221 msgb_free(inp);
1222 msgb_free(resp);
1223
1224 OSMO_ASSERT(last_endpoint == 1);
1225 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001226 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001227 OSMO_ASSERT(conn);
1228 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1229 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001230
1231 /* Allocate 2@mgw with three codecs, last one ignored */
1232 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001233 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001234 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001235 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1236 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001237 msgb_free(inp);
1238 msgb_free(resp);
1239
1240 OSMO_ASSERT(last_endpoint == 2);
1241 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001242 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001243 OSMO_ASSERT(conn);
1244 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1245 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001246
1247 /* Allocate 3@mgw with no codecs, check for PT == -1 */
1248 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001249 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001250 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001251 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1252 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001253 msgb_free(inp);
1254 msgb_free(resp);
1255
1256 OSMO_ASSERT(last_endpoint == 3);
1257 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001258 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001259 OSMO_ASSERT(conn);
1260 OSMO_ASSERT(conn->end.codec.payload_type == -1);
1261 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001262
1263 /* Allocate 4@mgw with a single codec */
1264 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001265 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001266 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001267 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1268 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001269 msgb_free(inp);
1270 msgb_free(resp);
1271
1272 OSMO_ASSERT(last_endpoint == 4);
1273 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001274 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001275 OSMO_ASSERT(conn);
1276 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1277 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001278
1279 /* Allocate 5@mgw at select GSM.. */
1280 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001281 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001282 talloc_free(cfg->trunk.audio_name);
1283 cfg->trunk.audio_name = "GSM/8000";
1284 cfg->trunk.no_audio_transcoding = 1;
1285 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001286 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1287 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001288 msgb_free(inp);
1289 msgb_free(resp);
1290
1291 OSMO_ASSERT(last_endpoint == 5);
1292 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001293 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001294 OSMO_ASSERT(conn);
1295 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1296 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001297
Philipp Maierffd75e42017-11-22 11:44:50 +01001298 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001299 last_endpoint = -1;
1300 resp = mgcp_handle_message(cfg, inp);
1301 msgb_free(inp);
1302 msgb_free(resp);
1303 OSMO_ASSERT(last_endpoint == 5);
1304 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001305 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001306 OSMO_ASSERT(conn);
1307 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1308 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
1309 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001310 memset(&addr, 0, sizeof(addr));
1311 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001312 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001313
1314 /* Check what happens without that flag */
1315
Philipp Maier87bd9be2017-08-22 16:35:41 +02001316 /* Free the previous endpoint and the data and
1317 * check if the connection really vanished... */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001318 mgcp_release_endp(endp);
1319 talloc_free(endp->last_response);
1320 talloc_free(endp->last_trans);
1321 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001322 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001323 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001324
1325 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001326 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001327 cfg->trunk.no_audio_transcoding = 0;
1328 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001329 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1330 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001331 msgb_free(inp);
1332 msgb_free(resp);
1333
1334 OSMO_ASSERT(last_endpoint == 5);
1335 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001336 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001337 OSMO_ASSERT(conn);
1338 OSMO_ASSERT(conn->end.codec.payload_type == 255);
1339 OSMO_ASSERT(conn->end.alt_codec.payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001340
1341 talloc_free(cfg);
1342}
1343
1344static void test_no_cycle(void)
1345{
1346 struct mgcp_config *cfg;
1347 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001348 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001349 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001350
1351 printf("Testing no sequence flow on initial packet\n");
1352
1353 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001354 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001355 mgcp_endpoints_allocate(&cfg->trunk);
1356
1357 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001358
Philipp Maierffd75e42017-11-22 11:44:50 +01001359 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1360 "test-connection");
1361 OSMO_ASSERT(_conn);
1362 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001363 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001364
Harald Welte49e3d5a2017-12-25 09:47:57 +01001365 OSMO_ASSERT(conn->state.stats.initialized == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001366
1367 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001368 OSMO_ASSERT(conn->state.stats.initialized == 1);
1369 OSMO_ASSERT(conn->state.stats.cycles == 0);
1370 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001371
1372 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001373 OSMO_ASSERT(conn->state.stats.initialized == 1);
1374 OSMO_ASSERT(conn->state.stats.cycles == 0);
1375 OSMO_ASSERT(conn->state.stats.max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001376
1377 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001378 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001379 OSMO_ASSERT(conn->state.stats.initialized == 1);
1380 OSMO_ASSERT(conn->state.stats.cycles == 0);
1381 OSMO_ASSERT(conn->state.stats.max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001382
1383 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001384 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001385 OSMO_ASSERT(conn->state.stats.initialized == 1);
1386 OSMO_ASSERT(conn->state.stats.cycles == UINT16_MAX + 1);
1387 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001388
Neels Hofmeyrb597b4f2017-11-18 21:28:35 +01001389 mgcp_release_endp(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001390 talloc_free(cfg);
1391}
1392
1393static void test_no_name(void)
1394{
1395 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001396 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001397
1398 printf("Testing no rtpmap name\n");
1399 cfg = mgcp_config_alloc();
1400
Philipp Maierfcd06552017-11-10 17:32:22 +01001401 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001402 cfg->trunk.audio_send_name = 0;
1403 mgcp_endpoints_allocate(&cfg->trunk);
1404
1405 cfg->policy_cb = mgcp_test_policy_cb;
1406
1407 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1408
Philipp Maierffd75e42017-11-22 11:44:50 +01001409 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001410 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001411
1412 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001413 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001414 (char *)msg->data);
1415 OSMO_ASSERT(false);
1416 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001417 msgb_free(inp);
1418 msgb_free(msg);
1419
1420 mgcp_release_endp(&cfg->trunk.endpoints[1]);
1421 talloc_free(cfg);
1422}
1423
1424static void test_osmux_cid(void)
1425{
1426 int id, i;
1427
1428 OSMO_ASSERT(osmux_used_cid() == 0);
1429 id = osmux_get_cid();
1430 OSMO_ASSERT(id == 0);
1431 OSMO_ASSERT(osmux_used_cid() == 1);
1432 osmux_put_cid(id);
1433 OSMO_ASSERT(osmux_used_cid() == 0);
1434
1435 for (i = 0; i < 256; ++i) {
1436 id = osmux_get_cid();
1437 OSMO_ASSERT(id == i);
1438 OSMO_ASSERT(osmux_used_cid() == i + 1);
1439 }
1440
1441 id = osmux_get_cid();
1442 OSMO_ASSERT(id == -1);
1443
1444 for (i = 0; i < 256; ++i)
1445 osmux_put_cid(i);
1446 OSMO_ASSERT(osmux_used_cid() == 0);
1447}
1448
1449static const struct log_info_cat log_categories[] = {
1450};
1451
1452const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001453 .cat = log_categories,
1454 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001455};
1456
1457int main(int argc, char **argv)
1458{
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001459 void *msgb_ctx = msgb_talloc_ctx_init(NULL, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001460 osmo_init_logging(&log_info);
1461
1462 test_strline();
1463 test_values();
1464 test_messages();
1465 test_retransmission();
1466 test_packet_loss_calc();
1467 test_rqnt_cb();
1468 test_mgcp_stats();
1469 test_packet_error_detection(1, 0);
1470 test_packet_error_detection(0, 0);
1471 test_packet_error_detection(0, 1);
1472 test_packet_error_detection(1, 1);
1473 test_multilple_codec();
1474 test_no_cycle();
1475 test_no_name();
1476 test_osmux_cid();
1477
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001478 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1479 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1480 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001481 printf("Done\n");
1482 return EXIT_SUCCESS;
1483}