blob: fb99911b8e96021c867d667719e63f253b85267a [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];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200639
640 cfg = mgcp_config_alloc();
641
Philipp Maierfcd06552017-11-10 17:32:22 +0100642 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200643 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200644 cfg->policy_cb = mgcp_test_policy_cb;
645
Philipp Maierffd75e42017-11-22 11:44:50 +0100646 memset(last_conn_id, 0, sizeof(last_conn_id));
647
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200648 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
649
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650 for (i = 0; i < ARRAY_SIZE(tests); i++) {
651 const struct mgcp_test *t = &tests[i];
652 struct msgb *inp;
653 struct msgb *msg;
654
Philipp Maierffd75e42017-11-22 11:44:50 +0100655 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200656 printf("Testing %s\n", t->name);
657
658 last_endpoint = -1;
659 dummy_packets = 0;
660
Philipp Maier87bd9be2017-08-22 16:35:41 +0200661 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
662 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200663
Philipp Maierffd75e42017-11-22 11:44:50 +0100664 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200665 msg = mgcp_handle_message(cfg, inp);
666 msgb_free(inp);
667 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200668 if (msg) {
669 printf("%s failed '%s'\n", t->name,
670 (char *)msg->data);
671 OSMO_ASSERT(false);
672 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100673 } else if (check_response(msg->data, t->exp_resp) != 0) {
674 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200675 OSMO_ASSERT(false);
676 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100677
678 if (msg)
679 get_conn_id_from_response(msg->data, last_conn_id,
680 sizeof(last_conn_id));
681
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200682 msgb_free(msg);
683
684 if (dummy_packets)
685 printf("Dummy packets: %d\n", dummy_packets);
686
687 if (last_endpoint != -1) {
688 endp = &cfg->trunk.endpoints[last_endpoint];
689
Philipp Maier01d24a32017-11-21 17:26:09 +0100690 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200691 if (conn) {
692 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200693
Philipp Maier87bd9be2017-08-22 16:35:41 +0200694 if (conn->end.packet_duration_ms != -1)
695 printf("Detected packet duration: %d\n",
696 conn->end.packet_duration_ms);
697 else
698 printf("Packet duration not set\n");
699 if (endp->local_options.pkt_period_min ||
700 endp->local_options.pkt_period_max)
701 printf
702 ("Requested packetetization period: "
703 "%d-%d\n",
704 endp->local_options.pkt_period_min,
705 endp->
706 local_options.pkt_period_max);
707 else
708 printf
709 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200710
Philipp Maier87bd9be2017-08-22 16:35:41 +0200711 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
712 printf("Connection mode: %d:%s%s%s%s\n",
713 conn->conn->mode,
714 !conn->conn->mode ? " NONE" : "",
715 conn->conn->mode & MGCP_CONN_SEND_ONLY
716 ? " SEND" : "",
717 conn->conn->mode & MGCP_CONN_RECV_ONLY
718 ? " RECV" : "",
719 conn->conn->mode & MGCP_CONN_LOOPBACK
720 & ~MGCP_CONN_RECV_SEND
721 ? " LOOP" : "");
722 fprintf(stderr,
723 "RTP output %sabled, NET output %sabled\n",
724 conn->end.output_enabled
725 ? "en" : "dis",
726 conn->end.output_enabled
727 ? "en" : "dis");
728 } else
729 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200730
Philipp Maier87bd9be2017-08-22 16:35:41 +0200731 OSMO_ASSERT(conn->end.output_enabled
732 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
733
734 conn->conn->mode |= CONN_UNMODIFIED;
735
736 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200737 endp->local_options.pkt_period_min = 0;
738 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200739 }
740
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200741 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100742 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200743 OSMO_ASSERT(last_endpoint != -1);
744 endp = &cfg->trunk.endpoints[last_endpoint];
745
746 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200747 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200748 last_endpoint,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200749 conn->end.codec.payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200750
Philipp Maier87bd9be2017-08-22 16:35:41 +0200751 if (t->ptype != PTYPE_IGNORE)
752 OSMO_ASSERT(conn->end.codec.payload_type ==
753 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200754
755 /* Reset them again for next test */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200756 conn->end.codec.payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 }
758 }
759
760 talloc_free(cfg);
761}
762
763static void test_retransmission(void)
764{
765 struct mgcp_config *cfg;
766 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100767 char last_conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200768
769 cfg = mgcp_config_alloc();
770
Philipp Maierfcd06552017-11-10 17:32:22 +0100771 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200772 mgcp_endpoints_allocate(&cfg->trunk);
773
Philipp Maierffd75e42017-11-22 11:44:50 +0100774 memset(last_conn_id, 0, sizeof(last_conn_id));
775
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200776 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
777
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200778 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
779 const struct mgcp_test *t = &retransmit[i];
780 struct msgb *inp;
781 struct msgb *msg;
782
Philipp Maierffd75e42017-11-22 11:44:50 +0100783 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200784 printf("Testing %s\n", t->name);
785
Philipp Maierffd75e42017-11-22 11:44:50 +0100786 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200787 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200788
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200789 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100790 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200791 printf("%s failed '%s'\n", t->name, (char *)msg->data);
792 OSMO_ASSERT(false);
793 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100794
795 if (msg && strcmp(t->name, CRCX))
796 get_conn_id_from_response(msg->data, last_conn_id,
797 sizeof(last_conn_id));
798
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200799 msgb_free(msg);
800
801 /* Retransmit... */
802 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100803 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200804 msg = mgcp_handle_message(cfg, inp);
805 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100806 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200807 printf("%s failed '%s'\n", t->name, (char *)msg->data);
808 OSMO_ASSERT(false);
809 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200810 msgb_free(msg);
811 }
812
813 talloc_free(cfg);
814}
815
816static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
817{
818 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200819 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200820 return 0;
821}
822
823static void test_rqnt_cb(void)
824{
825 struct mgcp_config *cfg;
826 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100827 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200828
829 cfg = mgcp_config_alloc();
830 cfg->rqnt_cb = rqnt_cb;
831
Philipp Maierfcd06552017-11-10 17:32:22 +0100832 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200833 mgcp_endpoints_allocate(&cfg->trunk);
834
835 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
836
Philipp Maierffd75e42017-11-22 11:44:50 +0100837 inp = create_msg(CRCX, NULL);
838 msg = mgcp_handle_message(cfg, inp);
839 OSMO_ASSERT(msg);
840 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
841 sizeof(conn_id)) == 0);
842 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200843 msgb_free(inp);
844
845 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100846 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200847 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200848 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200849 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
850 abort();
851 }
852
Philipp Maier87bd9be2017-08-22 16:35:41 +0200853 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200854 printf("FAILED: callback not called: %p\n", cfg->data);
855 abort();
856 }
857
858 msgb_free(msg);
859 msgb_free(inp);
860
Philipp Maierffd75e42017-11-22 11:44:50 +0100861 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200862 msgb_free(mgcp_handle_message(cfg, inp));
863 msgb_free(inp);
864 talloc_free(cfg);
865}
866
867struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200868 int cycles;
869 uint16_t base_seq;
870 uint16_t max_seq;
871 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 uint32_t expected;
874 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200875};
876
877static const struct pl_test pl_test_dat[] = {
878 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200879 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
880 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200881 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200882 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
883 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200885 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
886 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200887 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200888 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
889 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200890 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200891 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
892 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200893};
894
895static void test_packet_loss_calc(void)
896{
897 int i;
898 printf("Testing packet loss calculation.\n");
899
900 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
901 uint32_t expected;
902 int loss;
903 struct mgcp_rtp_state state;
904 struct mgcp_rtp_end rtp;
905 memset(&state, 0, sizeof(state));
906 memset(&rtp, 0, sizeof(rtp));
907
908 state.stats_initialized = 1;
909 state.stats_base_seq = pl_test_dat[i].base_seq;
910 state.stats_max_seq = pl_test_dat[i].max_seq;
911 state.stats_cycles = pl_test_dat[i].cycles;
912
Philipp Maier87bd9be2017-08-22 16:35:41 +0200913 rtp.packets_rx = pl_test_dat[i].packets;
914 calc_loss(&state, &rtp, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200915
Philipp Maier87bd9be2017-08-22 16:35:41 +0200916 if (loss != pl_test_dat[i].loss
917 || expected != pl_test_dat[i].expected) {
918 printf
919 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
920 i, loss, pl_test_dat[i].loss, expected,
921 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200922 }
923 }
924}
925
Philipp Maier87bd9be2017-08-22 16:35:41 +0200926int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
927 uint32_t *pr, uint32_t *_or, int *loss,
928 uint32_t *jitter)
929{
930 char *line, *save;
931 int rc;
932
933 /* initialize with bad values */
934 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
935 *loss = INT_MAX;
936
937 line = strtok_r((char *)msg->l2h, "\r\n", &save);
938 if (!line)
939 return -1;
940
941 /* this can only parse the message that is created above... */
942 for_each_non_empty_line(line, save) {
943 switch (line[0]) {
944 case 'P':
945 rc = sscanf(line,
946 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
947 ps, os, pr, _or, loss, jitter);
948 return rc == 6 ? 0 : -1;
949 }
950 }
951
952 return -1;
953}
954
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200955static void test_mgcp_stats(void)
956{
957 printf("Testing stat parsing\n");
958
959 uint32_t bps, bos, pr, _or, jitter;
960 struct msgb *msg;
961 int loss;
962 int rc;
963
Philipp Maierffd75e42017-11-22 11:44:50 +0100964 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200965 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
966 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200967 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
968 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200969 printf("FAIL: Parsing failed1.\n");
970 msgb_free(msg);
971
Philipp Maier87bd9be2017-08-22 16:35:41 +0200972 msg =
973 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +0100974 ("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 +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 != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
978 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200979 printf("FAIL: Parsing failed2.\n");
980 msgb_free(msg);
981}
982
983struct rtp_packet_info {
984 float txtime;
985 int len;
986 char *data;
987};
988
989struct rtp_packet_info test_rtp_packets1[] = {
990 /* RTP: SeqNo=0, TS=0 */
991 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200992 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200993 /* RTP: SeqNo=1, TS=160 */
994 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200995 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200996 /* RTP: SeqNo=2, TS=320 */
997 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200998 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200999 /* Repeat RTP timestamp: */
1000 /* RTP: SeqNo=3, TS=320 */
1001 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\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=4, TS=480 */
1004 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\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=5, TS=640 */
1007 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\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 /* Double skip RTP timestamp (delta = 2*160): */
1010 /* RTP: SeqNo=6, TS=960 */
1011 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\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=7, TS=1120 */
1014 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\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=8, TS=1280 */
1017 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\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 /* Non 20ms RTP timestamp (delta = 120): */
1020 /* RTP: SeqNo=9, TS=1400 */
1021 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\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=10, TS=1560 */
1024 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\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=11, TS=1720 */
1027 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\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 /* SSRC changed to 0x10203040, RTP timestamp jump */
1030 /* RTP: SeqNo=12, TS=34688 */
1031 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
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=13, TS=34848 */
1034 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
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=14, TS=35008 */
1037 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001038 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001039 /* Non 20ms RTP timestamp (delta = 120): */
1040 /* RTP: SeqNo=15, TS=35128 */
1041 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\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=16, TS=35288 */
1044 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\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=17, TS=35448 */
1047 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001048 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001049 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1050 /* RTP: SeqNo=19, TS=35768 */
1051 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\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=20, TS=35928 */
1054 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\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=21, TS=36088 */
1057 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001058 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001059 /* Repeat last packet */
1060 /* RTP: SeqNo=21, TS=36088 */
1061 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\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=22, TS=36248 */
1064 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\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=23, TS=36408 */
1067 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\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 /* Don't increment SeqNo but increment timestamp by 160 */
1070 /* RTP: SeqNo=23, TS=36568 */
1071 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\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=24, TS=36728 */
1074 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\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=25, TS=36888 */
1077 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\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 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1080 * SeqNo jump */
1081 /* RTP: SeqNo=1000, TS=160000 */
1082 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
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=1001, TS=160160 */
1085 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
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=1002, TS=160320 */
1088 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001089 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001090};
1091
Philipp Maier87bd9be2017-08-22 16:35:41 +02001092void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1093 struct mgcp_rtp_state *state,
1094 struct mgcp_rtp_end *rtp_end,
1095 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001096
1097static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1098{
1099 int i;
1100
1101 struct mgcp_trunk_config trunk;
1102 struct mgcp_endpoint endp;
1103 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001104 struct mgcp_rtp_end *rtp;
1105 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106 char buffer[4096];
1107 uint32_t last_ssrc = 0;
1108 uint32_t last_timestamp = 0;
1109 uint32_t last_seqno = 0;
1110 int last_in_ts_err_cnt = 0;
1111 int last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001112 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001113 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001114
1115 printf("Testing packet error detection%s%s.\n",
1116 patch_ssrc ? ", patch SSRC" : "",
1117 patch_ts ? ", patch timestamps" : "");
1118
1119 memset(&trunk, 0, sizeof(trunk));
1120 memset(&endp, 0, sizeof(endp));
1121 memset(&state, 0, sizeof(state));
1122
Philipp Maier87bd9be2017-08-22 16:35:41 +02001123 endp.type = &ep_typeset.rtp;
1124
Philipp Maierfcd06552017-11-10 17:32:22 +01001125 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001126 trunk.endpoints = &endp;
1127 trunk.force_constant_ssrc = patch_ssrc;
1128 trunk.force_aligned_timing = patch_ts;
1129
1130 endp.tcfg = &trunk;
1131
Philipp Maier87bd9be2017-08-22 16:35:41 +02001132 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001133 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1134 "test-connection");
1135 OSMO_ASSERT(_conn);
1136 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001137 OSMO_ASSERT(conn);
1138
1139 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001140
1141 rtp->codec.payload_type = 98;
1142
1143 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1144 struct rtp_packet_info *info = test_rtp_packets1 + i;
1145
1146 force_monotonic_time_us = round(1000000.0 * info->txtime);
1147
1148 OSMO_ASSERT(info->len <= sizeof(buffer));
1149 OSMO_ASSERT(info->len >= 0);
1150 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001151 mgcp_rtp_end_config(&endp, 1, rtp);
1152
1153 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1154 buffer, info->len);
1155
1156 if (state.out_stream.ssrc != last_ssrc) {
1157 printf("Output SSRC changed to %08x\n",
1158 state.out_stream.ssrc);
1159 last_ssrc = state.out_stream.ssrc;
1160 }
1161
1162 printf("In TS: %d, dTS: %d, Seq: %d\n",
1163 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001164 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001165
1166 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
1167 "TS Err change: in %+d, out %+d\n",
1168 state.out_stream.last_timestamp - last_timestamp,
1169 state.out_stream.last_tsdelta,
1170 state.out_stream.last_seq - last_seqno,
1171 state.in_stream.err_ts_counter - last_in_ts_err_cnt,
1172 state.out_stream.err_ts_counter - last_out_ts_err_cnt);
1173
1174 printf("Stats: Jitter = %u, Transit = %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001175 calc_jitter(&state), state.stats_transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001176
1177 last_in_ts_err_cnt = state.in_stream.err_ts_counter;
1178 last_out_ts_err_cnt = state.out_stream.err_ts_counter;
1179 last_timestamp = state.out_stream.last_timestamp;
1180 last_seqno = state.out_stream.last_seq;
1181 }
1182
1183 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001184 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185}
1186
1187static void test_multilple_codec(void)
1188{
1189 struct mgcp_config *cfg;
1190 struct mgcp_endpoint *endp;
1191 struct msgb *inp, *resp;
1192 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001193 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001194 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001195
1196 printf("Testing multiple payload types\n");
1197
1198 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001199 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001200 mgcp_endpoints_allocate(&cfg->trunk);
1201 cfg->policy_cb = mgcp_test_policy_cb;
1202 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1203
1204 /* Allocate endpoint 1@mgw with two codecs */
1205 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001206 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001207 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001208 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1209 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001210 msgb_free(inp);
1211 msgb_free(resp);
1212
1213 OSMO_ASSERT(last_endpoint == 1);
1214 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001215 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001216 OSMO_ASSERT(conn);
1217 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1218 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001219
1220 /* Allocate 2@mgw with three codecs, last one ignored */
1221 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001222 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001223 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001224 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1225 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001226 msgb_free(inp);
1227 msgb_free(resp);
1228
1229 OSMO_ASSERT(last_endpoint == 2);
1230 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001231 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001232 OSMO_ASSERT(conn);
1233 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1234 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001235
1236 /* Allocate 3@mgw with no codecs, check for PT == -1 */
1237 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001238 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001239 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001240 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1241 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001242 msgb_free(inp);
1243 msgb_free(resp);
1244
1245 OSMO_ASSERT(last_endpoint == 3);
1246 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001247 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001248 OSMO_ASSERT(conn);
1249 OSMO_ASSERT(conn->end.codec.payload_type == -1);
1250 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001251
1252 /* Allocate 4@mgw with a single codec */
1253 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001254 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001255 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001256 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1257 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001258 msgb_free(inp);
1259 msgb_free(resp);
1260
1261 OSMO_ASSERT(last_endpoint == 4);
1262 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001263 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001264 OSMO_ASSERT(conn);
1265 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1266 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001267
1268 /* Allocate 5@mgw at select GSM.. */
1269 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001270 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001271 talloc_free(cfg->trunk.audio_name);
1272 cfg->trunk.audio_name = "GSM/8000";
1273 cfg->trunk.no_audio_transcoding = 1;
1274 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001275 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1276 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001277 msgb_free(inp);
1278 msgb_free(resp);
1279
1280 OSMO_ASSERT(last_endpoint == 5);
1281 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001282 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001283 OSMO_ASSERT(conn);
1284 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1285 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001286
Philipp Maierffd75e42017-11-22 11:44:50 +01001287 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001288 last_endpoint = -1;
1289 resp = mgcp_handle_message(cfg, inp);
1290 msgb_free(inp);
1291 msgb_free(resp);
1292 OSMO_ASSERT(last_endpoint == 5);
1293 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001294 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001295 OSMO_ASSERT(conn);
1296 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1297 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
1298 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001299 memset(&addr, 0, sizeof(addr));
1300 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001301 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001302
1303 /* Check what happens without that flag */
1304
Philipp Maier87bd9be2017-08-22 16:35:41 +02001305 /* Free the previous endpoint and the data and
1306 * check if the connection really vanished... */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001307 mgcp_release_endp(endp);
1308 talloc_free(endp->last_response);
1309 talloc_free(endp->last_trans);
1310 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001311 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001312 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001313
1314 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001315 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001316 cfg->trunk.no_audio_transcoding = 0;
1317 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001318 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1319 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001320 msgb_free(inp);
1321 msgb_free(resp);
1322
1323 OSMO_ASSERT(last_endpoint == 5);
1324 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001325 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001326 OSMO_ASSERT(conn);
1327 OSMO_ASSERT(conn->end.codec.payload_type == 255);
1328 OSMO_ASSERT(conn->end.alt_codec.payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001329
1330 talloc_free(cfg);
1331}
1332
1333static void test_no_cycle(void)
1334{
1335 struct mgcp_config *cfg;
1336 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001337 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001338 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001339
1340 printf("Testing no sequence flow on initial packet\n");
1341
1342 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001343 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001344 mgcp_endpoints_allocate(&cfg->trunk);
1345
1346 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001347
Philipp Maierffd75e42017-11-22 11:44:50 +01001348 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1349 "test-connection");
1350 OSMO_ASSERT(_conn);
1351 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001352 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001353
Philipp Maier87bd9be2017-08-22 16:35:41 +02001354 OSMO_ASSERT(conn->state.stats_initialized == 0);
1355
1356 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
1357 OSMO_ASSERT(conn->state.stats_initialized == 1);
1358 OSMO_ASSERT(conn->state.stats_cycles == 0);
1359 OSMO_ASSERT(conn->state.stats_max_seq == 0);
1360
1361 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
1362 OSMO_ASSERT(conn->state.stats_initialized == 1);
1363 OSMO_ASSERT(conn->state.stats_cycles == 0);
1364 OSMO_ASSERT(conn->state.stats_max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001365
1366 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001367 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
1368 OSMO_ASSERT(conn->state.stats_initialized == 1);
1369 OSMO_ASSERT(conn->state.stats_cycles == 0);
1370 OSMO_ASSERT(conn->state.stats_max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001371
1372 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001373 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
1374 OSMO_ASSERT(conn->state.stats_initialized == 1);
1375 OSMO_ASSERT(conn->state.stats_cycles == UINT16_MAX + 1);
1376 OSMO_ASSERT(conn->state.stats_max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001377
Neels Hofmeyrb597b4f2017-11-18 21:28:35 +01001378 mgcp_release_endp(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001379 talloc_free(cfg);
1380}
1381
1382static void test_no_name(void)
1383{
1384 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001385 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001386
1387 printf("Testing no rtpmap name\n");
1388 cfg = mgcp_config_alloc();
1389
Philipp Maierfcd06552017-11-10 17:32:22 +01001390 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001391 cfg->trunk.audio_send_name = 0;
1392 mgcp_endpoints_allocate(&cfg->trunk);
1393
1394 cfg->policy_cb = mgcp_test_policy_cb;
1395
1396 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1397
Philipp Maierffd75e42017-11-22 11:44:50 +01001398 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001399 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001400
1401 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001402 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001403 (char *)msg->data);
1404 OSMO_ASSERT(false);
1405 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001406 msgb_free(inp);
1407 msgb_free(msg);
1408
1409 mgcp_release_endp(&cfg->trunk.endpoints[1]);
1410 talloc_free(cfg);
1411}
1412
1413static void test_osmux_cid(void)
1414{
1415 int id, i;
1416
1417 OSMO_ASSERT(osmux_used_cid() == 0);
1418 id = osmux_get_cid();
1419 OSMO_ASSERT(id == 0);
1420 OSMO_ASSERT(osmux_used_cid() == 1);
1421 osmux_put_cid(id);
1422 OSMO_ASSERT(osmux_used_cid() == 0);
1423
1424 for (i = 0; i < 256; ++i) {
1425 id = osmux_get_cid();
1426 OSMO_ASSERT(id == i);
1427 OSMO_ASSERT(osmux_used_cid() == i + 1);
1428 }
1429
1430 id = osmux_get_cid();
1431 OSMO_ASSERT(id == -1);
1432
1433 for (i = 0; i < 256; ++i)
1434 osmux_put_cid(i);
1435 OSMO_ASSERT(osmux_used_cid() == 0);
1436}
1437
1438static const struct log_info_cat log_categories[] = {
1439};
1440
1441const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001442 .cat = log_categories,
1443 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001444};
1445
1446int main(int argc, char **argv)
1447{
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001448 void *msgb_ctx = msgb_talloc_ctx_init(NULL, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001449 osmo_init_logging(&log_info);
1450
1451 test_strline();
1452 test_values();
1453 test_messages();
1454 test_retransmission();
1455 test_packet_loss_calc();
1456 test_rqnt_cb();
1457 test_mgcp_stats();
1458 test_packet_error_detection(1, 0);
1459 test_packet_error_detection(0, 0);
1460 test_packet_error_detection(0, 1);
1461 test_packet_error_detection(1, 1);
1462 test_multilple_codec();
1463 test_no_cycle();
1464 test_no_name();
1465 test_osmux_cid();
1466
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001467 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1468 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1469 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001470 printf("Done\n");
1471 return EXIT_SUCCESS;
1472}