blob: b1bbb7f3a9705550af029e46c8a59ddeb7d91e1d [file] [log] [blame]
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +01001/*
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +02002 * (C) 2011-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2011-2012 by On-Waves
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +01004 * 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 */
Jacob Erlbeck84dfba12014-01-07 11:38:22 +010019#undef _GNU_SOURCE
20#define _GNU_SOURCE
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +010021
22#include <openbsc/mgcp.h>
Holger Hans Peter Freyther1f0c5b42011-02-28 14:37:03 +010023#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +010024
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020025#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010026#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +010027#include <string.h>
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +020028#include <limits.h>
Jacob Erlbeck84dfba12014-01-07 11:38:22 +010029#include <dlfcn.h>
Jacob Erlbecke763f3e2014-01-30 21:01:33 +010030#include <time.h>
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +010031
Jacob Erlbeck2bee7f92013-11-29 13:43:44 +010032char *strline_r(char *str, char **saveptr);
33
34const char *strline_test_data =
35 "one CR\r"
36 "two CR\r"
37 "\r"
38 "one CRLF\r\n"
39 "two CRLF\r\n"
40 "\r\n"
41 "one LF\n"
42 "two LF\n"
43 "\n"
44 "mixed (4 lines)\r\r\n\n\r\n";
45
46#define EXPECTED_NUMBER_OF_LINES 13
47
48static void test_strline(void)
49{
50 char *save = NULL;
51 char *line;
52 char buf[2048];
53 int counter = 0;
54
55 strncpy(buf, strline_test_data, sizeof(buf));
56
57 for (line = strline_r(buf, &save); line;
58 line = strline_r(NULL, &save)) {
59 printf("line: '%s'\n", line);
60 counter++;
61 }
62
63 OSMO_ASSERT(counter == EXPECTED_NUMBER_OF_LINES);
64}
65
Harald Welte8819d822012-01-27 00:00:44 +010066#define AUEP1 "AUEP 158663169 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n"
Harald Weltef74da142012-01-27 00:36:56 +010067#define AUEP1_RET "200 158663169 OK\r\n"
Harald Welte8819d822012-01-27 00:00:44 +010068#define AUEP2 "AUEP 18983213 ds/e1-2/1@172.16.6.66 MGCP 1.0\r\n"
Harald Weltef74da142012-01-27 00:36:56 +010069#define AUEP2_RET "500 18983213 FAIL\r\n"
Holger Hans Peter Freytherd77023b2012-03-16 09:53:49 +010070#define EMPTY "\r\n"
71#define EMPTY_RET NULL
72#define SHORT "CRCX \r\n"
73#define SHORT_RET "510 000000 FAIL\r\n"
74
Holger Hans Peter Freythercf2f1582012-12-10 12:54:57 +010075#define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@172.16.6.66 MGCP 1.0\r\n"
76#define MDCX_ERR_RET "510 18983213 FAIL\r\n"
77#define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n"
78#define MDCX_RET "400 18983214 FAIL\r\n"
79#define MDCX3 "MDCX 18983215 1@mgw MGCP 1.0\r\n"
80#define MDCX3_RET "200 18983215 OK\r\n" \
81 "I: 1\n" \
82 "\n" \
83 "v=0\r\n" \
84 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
85 "c=IN IP4 0.0.0.0\r\n" \
86 "t=0 0\r\n" \
87 "m=audio 0 RTP/AVP 126\r\n" \
Jacob Erlbeck0a1bc562013-12-10 13:09:37 +010088 "a=rtpmap:126 AMR/8000\r\n" \
89 "a=ptime:20\r\n"
Jacob Erlbecka52ac662013-11-29 13:43:45 +010090#define MDCX4 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \
Jacob Erlbeck1b653af2013-12-19 12:20:49 +010091 "M: sendrecv\r" \
Jacob Erlbecka52ac662013-11-29 13:43:45 +010092 "C: 2\r\n" \
93 "I: 1\r\n" \
94 "L: p:20, a:AMR, nt:IN\r\n" \
95 "\n" \
96 "v=0\r\n" \
97 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
98 "c=IN IP4 0.0.0.0\r\n" \
99 "t=0 0\r\n" \
100 "m=audio 4441 RTP/AVP 99\r\n" \
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100101 "a=rtpmap:99 AMR/8000\r\n" \
102 "a=ptime:40\r\n"
103#define MDCX4_RET(Ident) "200 " Ident " OK\r\n" \
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100104 "I: 1\n" \
105 "\n" \
106 "v=0\r\n" \
107 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
108 "c=IN IP4 0.0.0.0\r\n" \
109 "t=0 0\r\n" \
110 "m=audio 0 RTP/AVP 126\r\n" \
Jacob Erlbeck0a1bc562013-12-10 13:09:37 +0100111 "a=rtpmap:126 AMR/8000\r\n" \
112 "a=ptime:20\r\n"
Holger Hans Peter Freythercf2f1582012-12-10 12:54:57 +0100113
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100114#define MDCX4_PT1 "MDCX 18983217 1@mgw MGCP 1.0\r\n" \
Jacob Erlbeck1b653af2013-12-19 12:20:49 +0100115 "M: sendrecv\r" \
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100116 "C: 2\r\n" \
117 "I: 1\r\n" \
118 "L: p:20-40, a:AMR, nt:IN\r\n" \
119 "\n" \
120 "v=0\r\n" \
121 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
122 "c=IN IP4 0.0.0.0\r\n" \
123 "t=0 0\r\n" \
124 "m=audio 4441 RTP/AVP 99\r\n" \
125 "a=rtpmap:99 AMR/8000\r\n" \
126 "a=ptime:40\r\n"
127
128#define MDCX4_PT2 "MDCX 18983218 1@mgw MGCP 1.0\r\n" \
Jacob Erlbeck1b653af2013-12-19 12:20:49 +0100129 "M: sendrecv\r" \
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100130 "C: 2\r\n" \
131 "I: 1\r\n" \
132 "L: p:20-20, a:AMR, nt:IN\r\n" \
133 "\n" \
134 "v=0\r\n" \
135 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
136 "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_PT3 "MDCX 18983219 1@mgw MGCP 1.0\r\n" \
Jacob Erlbeck1b653af2013-12-19 12:20:49 +0100143 "M: sendrecv\r" \
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100144 "C: 2\r\n" \
145 "I: 1\r\n" \
146 "L: a:AMR, nt:IN\r\n" \
147 "\n" \
148 "v=0\r\n" \
149 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
150 "c=IN IP4 0.0.0.0\r\n" \
151 "t=0 0\r\n" \
152 "m=audio 4441 RTP/AVP 99\r\n" \
153 "a=rtpmap:99 AMR/8000\r\n" \
154 "a=ptime:40\r\n"
155
Jacob Erlbeck1b653af2013-12-19 12:20:49 +0100156#define MDCX4_SO "MDCX 18983220 1@mgw MGCP 1.0\r\n" \
157 "M: sendonly\r" \
158 "C: 2\r\n" \
159 "I: 1\r\n" \
160 "L: p:20, a:AMR, nt:IN\r\n" \
161 "\n" \
162 "v=0\r\n" \
163 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
164 "c=IN IP4 0.0.0.0\r\n" \
165 "t=0 0\r\n" \
166 "m=audio 4441 RTP/AVP 99\r\n" \
167 "a=rtpmap:99 AMR/8000\r\n" \
168 "a=ptime:40\r\n"
169
Holger Hans Peter Freytherd77023b2012-03-16 09:53:49 +0100170#define SHORT2 "CRCX 1"
171#define SHORT2_RET "510 000000 FAIL\r\n"
172#define SHORT3 "CRCX 1 1@mgw"
173#define SHORT4 "CRCX 1 1@mgw MGCP"
174#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
Harald Weltef74da142012-01-27 00:36:56 +0100175
Harald Welte27fa7902012-01-27 00:37:23 +0100176#define CRCX "CRCX 2 1@mgw MGCP 1.0\r\n" \
Jacob Erlbeck1b653af2013-12-19 12:20:49 +0100177 "M: recvonly\r\n" \
Harald Welte27fa7902012-01-27 00:37:23 +0100178 "C: 2\r\n" \
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100179 "L: p:20\r\n" \
Harald Welte27fa7902012-01-27 00:37:23 +0100180 "\r\n" \
181 "v=0\r\n" \
182 "c=IN IP4 123.12.12.123\r\n" \
183 "m=audio 5904 RTP/AVP 97\r\n" \
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100184 "a=rtpmap:97 GSM-EFR/8000\r\n" \
185 "a=ptime:40\r\n"
Harald Welte27fa7902012-01-27 00:37:23 +0100186
187#define CRCX_RET "200 2 OK\r\n" \
188 "I: 1\n" \
189 "\n" \
190 "v=0\r\n" \
191 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
192 "c=IN IP4 0.0.0.0\r\n" \
193 "t=0 0\r\n" \
194 "m=audio 0 RTP/AVP 126\r\n" \
Jacob Erlbeck0a1bc562013-12-10 13:09:37 +0100195 "a=rtpmap:126 AMR/8000\r\n" \
196 "a=ptime:20\r\n"
Harald Welte27fa7902012-01-27 00:37:23 +0100197
Harald Welte27fa7902012-01-27 00:37:23 +0100198#define CRCX_ZYN "CRCX 2 1@mgw MGCP 1.0\r" \
Jacob Erlbeck1b653af2013-12-19 12:20:49 +0100199 "M: recvonly\r" \
Harald Welte27fa7902012-01-27 00:37:23 +0100200 "C: 2\r\r" \
201 "v=0\r" \
202 "c=IN IP4 123.12.12.123\r" \
203 "m=audio 5904 RTP/AVP 97\r" \
204 "a=rtpmap:97 GSM-EFR/8000\r"
Harald Weltef74da142012-01-27 00:36:56 +0100205
Holger Hans Peter Freytherd267f4d2012-12-07 14:42:03 +0100206#define CRCX_ZYN_RET "200 2 OK\r\n" \
207 "I: 2\n" \
208 "\n" \
209 "v=0\r\n" \
210 "o=- 2 23 IN IP4 0.0.0.0\r\n" \
211 "c=IN IP4 0.0.0.0\r\n" \
212 "t=0 0\r\n" \
213 "m=audio 0 RTP/AVP 126\r\n" \
Jacob Erlbeck0a1bc562013-12-10 13:09:37 +0100214 "a=rtpmap:126 AMR/8000\r\n" \
215 "a=ptime:20\r\n"
Holger Hans Peter Freytherd267f4d2012-12-07 14:42:03 +0100216
Holger Hans Peter Freyther0bf15a82012-09-14 17:18:12 +0200217#define DLCX "DLCX 7 1@mgw MGCP 1.0\r\n" \
218 "C: 2\r\n"
219
220#define DLCX_RET "250 7 OK\r\n" \
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100221 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n" \
222 "X-Osmo-CP: EC TIS=0, TOS=0, TIR=0, TOR=0\r\n"
Holger Hans Peter Freyther0bf15a82012-09-14 17:18:12 +0200223
Holger Hans Peter Freyther8d0be252012-11-29 12:54:22 +0100224#define RQNT "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
225 "X: B244F267488\r\n" \
226 "S: D/9\r\n"
227
Holger Hans Peter Freytherce553612012-12-07 15:04:07 +0100228#define RQNT2 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
Holger Hans Peter Freyther8d0be252012-11-29 12:54:22 +0100229 "X: ADD4F26746F\r\n" \
230 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
231
Holger Hans Peter Freytherce553612012-12-07 15:04:07 +0100232#define RQNT1_RET "200 186908780 OK\r\n"
233#define RQNT2_RET "200 186908781 OK\r\n"
Holger Hans Peter Freyther0bf15a82012-09-14 17:18:12 +0200234
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100235#define PTYPE_IGNORE 0 /* == default initializer */
236#define PTYPE_NONE 128
237#define PTYPE_NYI PTYPE_NONE
238
Harald Weltef74da142012-01-27 00:36:56 +0100239struct mgcp_test {
240 const char *name;
241 const char *req;
242 const char *exp_resp;
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100243 int exp_net_ptype;
244 int exp_bts_ptype;
Harald Weltef74da142012-01-27 00:36:56 +0100245};
246
Holger Hans Peter Freytherd267f4d2012-12-07 14:42:03 +0100247static const struct mgcp_test tests[] = {
Harald Weltef74da142012-01-27 00:36:56 +0100248 { "AUEP1", AUEP1, AUEP1_RET },
249 { "AUEP2", AUEP2, AUEP2_RET },
Holger Hans Peter Freythercf2f1582012-12-10 12:54:57 +0100250 { "MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET },
251 { "MDCX2", MDCX_UNALLOCATED, MDCX_RET },
Jacob Erlbeck78a95012013-11-29 13:43:49 +0100252 { "CRCX", CRCX, CRCX_RET, 97, 126 },
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100253 { "MDCX3", MDCX3, MDCX3_RET, PTYPE_NONE, 126 },
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100254 { "MDCX4", MDCX4, MDCX4_RET("18983216"), 99, 126 },
255 { "MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99, 126 },
256 { "MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99, 126 },
257 { "MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99, 126 },
Jacob Erlbeck1b653af2013-12-19 12:20:49 +0100258 { "MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99, 126 },
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100259 { "DLCX", DLCX, DLCX_RET, -1, -1 },
Jacob Erlbeck78a95012013-11-29 13:43:49 +0100260 { "CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97, 126 },
Holger Hans Peter Freytherd77023b2012-03-16 09:53:49 +0100261 { "EMPTY", EMPTY, EMPTY_RET },
262 { "SHORT1", SHORT, SHORT_RET },
263 { "SHORT2", SHORT2, SHORT2_RET },
264 { "SHORT3", SHORT3, SHORT2_RET },
265 { "SHORT4", SHORT4, SHORT2_RET },
Holger Hans Peter Freytherce553612012-12-07 15:04:07 +0100266 { "RQNT1", RQNT, RQNT1_RET },
267 { "RQNT2", RQNT2, RQNT2_RET },
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100268 { "DLCX", DLCX, DLCX_RET, -1, -1 },
Holger Hans Peter Freytherce553612012-12-07 15:04:07 +0100269};
270
271static const struct mgcp_test retransmit[] = {
272 { "CRCX", CRCX, CRCX_RET },
273 { "RQNT1", RQNT, RQNT1_RET },
274 { "RQNT2", RQNT2, RQNT2_RET },
275 { "MDCX3", MDCX3, MDCX3_RET },
Holger Hans Peter Freyther0bf15a82012-09-14 17:18:12 +0200276 { "DLCX", DLCX, DLCX_RET },
Harald Weltef74da142012-01-27 00:36:56 +0100277};
Harald Welte8819d822012-01-27 00:00:44 +0100278
279static struct msgb *create_msg(const char *str)
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100280{
281 struct msgb *msg;
282
283 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Holger Hans Peter Freythera8010062013-12-12 13:06:57 +0100284 int len = sprintf((char *)msg->data, "%s", str);
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100285 msg->l2h = msgb_put(msg, len);
286 return msg;
287}
288
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100289static int last_endpoint = -1;
290
291static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
292 int state, const char *transactio_id)
293{
294 fprintf(stderr, "Policy CB got state %d on endpoint %d\n",
295 state, endpoint);
296 last_endpoint = endpoint;
297 return MGCP_POLICY_CONT;
298}
299
Jacob Erlbeck84dfba12014-01-07 11:38:22 +0100300#define MGCP_DUMMY_LOAD 0x23
301static int dummy_packets = 0;
302/* override and forward */
303ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
304 const struct sockaddr *dest_addr, socklen_t addrlen)
305{
306 typedef ssize_t (*sendto_t)(int, const void *, size_t, int,
307 const struct sockaddr *, socklen_t);
308 static sendto_t real_sendto = NULL;
309 uint32_t dest_host = htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
310 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
311
312 if (!real_sendto)
313 real_sendto = dlsym(RTLD_NEXT, "sendto");
314
315 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD ) {
Holger Hans Peter Freyther757971a2014-02-03 09:18:51 +0100316 fprintf(stderr, "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
Jacob Erlbeck84dfba12014-01-07 11:38:22 +0100317 dest_host, dest_port,
318 len, osmo_hexdump(buf, len));
319 dummy_packets += 1;
320 }
321
322 return real_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
323}
324
Jacob Erlbecke763f3e2014-01-30 21:01:33 +0100325static int64_t force_monotonic_time_us = -1;
326/* override and forward */
327int clock_gettime(clockid_t clk_id, struct timespec *tp)
328{
329 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
330 static clock_gettime_t real_clock_gettime = NULL;
331
332 if (!real_clock_gettime)
333 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
334
335 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
336 tp->tv_sec = force_monotonic_time_us / 1000000;
337 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
338 return 0;
339 }
340
341 return real_clock_gettime(clk_id, tp);
342}
343
Jacob Erlbecke35fd132014-01-16 16:50:40 +0100344#define CONN_UNMODIFIED (0x1000)
345
346static void test_values(void)
347{
348 /* Check that NONE disables all output */
349 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0)
350
351 /* Check that LOOPBACK enables all output */
352 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
353 MGCP_CONN_RECV_SEND)
354}
355
356
Holger Hans Peter Freyther5563e6c2012-03-16 09:45:13 +0100357static void test_messages(void)
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100358{
Holger Hans Peter Freytherd5b3ca62012-01-06 15:04:43 +0100359 struct mgcp_config *cfg;
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100360 struct mgcp_endpoint *endp;
Harald Weltef74da142012-01-27 00:36:56 +0100361 int i;
Holger Hans Peter Freytherd5b3ca62012-01-06 15:04:43 +0100362
363 cfg = mgcp_config_alloc();
Harald Weltef74da142012-01-27 00:36:56 +0100364
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100365 cfg->trunk.number_endpoints = 64;
Holger Hans Peter Freyther1f0c5b42011-02-28 14:37:03 +0100366 mgcp_endpoints_allocate(&cfg->trunk);
367
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100368 cfg->policy_cb = mgcp_test_policy_cb;
369
Holger Hans Peter Freyther1f0c5b42011-02-28 14:37:03 +0100370 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100371
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100372 /* reset endpoints */
373 for (i = 0; i < cfg->trunk.number_endpoints; i++) {
374 endp = &cfg->trunk.endpoints[i];
375 endp->net_end.payload_type = PTYPE_NONE;
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100376 endp->net_end.packet_duration_ms = -1;
Jacob Erlbecke35fd132014-01-16 16:50:40 +0100377
378 OSMO_ASSERT(endp->conn_mode == MGCP_CONN_NONE);
379 endp->conn_mode |= CONN_UNMODIFIED;
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100380 }
381
Harald Weltef74da142012-01-27 00:36:56 +0100382 for (i = 0; i < ARRAY_SIZE(tests); i++) {
383 const struct mgcp_test *t = &tests[i];
384 struct msgb *inp;
385 struct msgb *msg;
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100386
Harald Weltef74da142012-01-27 00:36:56 +0100387 printf("Testing %s\n", t->name);
388
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100389 last_endpoint = -1;
Jacob Erlbeck84dfba12014-01-07 11:38:22 +0100390 dummy_packets = 0;
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100391
Harald Weltef74da142012-01-27 00:36:56 +0100392 inp = create_msg(t->req);
393 msg = mgcp_handle_message(cfg, inp);
394 msgb_free(inp);
Holger Hans Peter Freytherd77023b2012-03-16 09:53:49 +0100395 if (!t->exp_resp) {
396 if (msg)
397 printf("%s failed '%s'\n", t->name, (char *) msg->data);
398 } else if (strcmp((char *) msg->data, t->exp_resp) != 0)
Harald Weltef74da142012-01-27 00:36:56 +0100399 printf("%s failed '%s'\n", t->name, (char *) msg->data);
400 msgb_free(msg);
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100401
Jacob Erlbeck84dfba12014-01-07 11:38:22 +0100402 if (dummy_packets)
403 printf("Dummy packets: %d\n", dummy_packets);
404
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100405 if (last_endpoint != -1) {
406 endp = &cfg->trunk.endpoints[last_endpoint];
407
408 if (endp->net_end.packet_duration_ms != -1)
409 printf("Detected packet duration: %d\n",
410 endp->net_end.packet_duration_ms);
411 else
412 printf("Packet duration not set\n");
Jacob Erlbeck2c2ca4d2013-12-09 14:32:03 +0100413 if (endp->local_options.pkt_period_min ||
414 endp->local_options.pkt_period_max)
415 printf("Requested packetetization period: "
416 "%d-%d\n",
417 endp->local_options.pkt_period_min,
418 endp->local_options.pkt_period_max);
419 else
420 printf("Requested packetization period not set\n");
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100421
Jacob Erlbeckcf577c32014-01-30 21:01:32 +0100422 if ((endp->conn_mode & CONN_UNMODIFIED) == 0) {
423 printf("Connection mode: %d:%s%s%s%s\n",
Jacob Erlbeck1b653af2013-12-19 12:20:49 +0100424 endp->conn_mode,
Jacob Erlbeckcf577c32014-01-30 21:01:32 +0100425 !endp->conn_mode ? " NONE" : "",
426 endp->conn_mode & MGCP_CONN_SEND_ONLY ?
427 " SEND" : "",
428 endp->conn_mode & MGCP_CONN_RECV_ONLY ?
429 " RECV" : "",
430 endp->conn_mode & MGCP_CONN_LOOPBACK &
431 ~MGCP_CONN_RECV_SEND ?
432 " LOOP" : "");
433 fprintf(stderr,
434 "BTS output %sabled, NET output %sabled\n",
435 endp->bts_end.output_enabled ? "en" : "dis",
436 endp->net_end.output_enabled ? "en" : "dis");
437 } else
Jacob Erlbeck1b653af2013-12-19 12:20:49 +0100438 printf("Connection mode not set\n");
439
Jacob Erlbecke35fd132014-01-16 16:50:40 +0100440 OSMO_ASSERT(endp->net_end.output_enabled ==
441 (endp->conn_mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
442 OSMO_ASSERT(endp->bts_end.output_enabled ==
443 (endp->conn_mode & MGCP_CONN_RECV_ONLY ? 1 : 0));
444
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100445 endp->net_end.packet_duration_ms = -1;
Jacob Erlbeck68c74f52013-12-19 17:01:24 +0100446 endp->local_options.pkt_period_min = 0;
447 endp->local_options.pkt_period_max = 0;
Jacob Erlbeckead05292014-01-16 16:50:41 +0100448 endp->conn_mode |= CONN_UNMODIFIED;
Jacob Erlbeckba477d22013-12-10 13:09:37 +0100449 }
450
451
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100452 /* Check detected payload type */
453 if (t->exp_net_ptype != PTYPE_IGNORE ||
454 t->exp_bts_ptype != PTYPE_IGNORE) {
455 OSMO_ASSERT(last_endpoint != -1);
456 endp = &cfg->trunk.endpoints[last_endpoint];
457
458 fprintf(stderr, "endpoint %d: "
459 "payload type BTS %d (exp %d), NET %d (exp %d)\n",
460 last_endpoint,
461 endp->bts_end.payload_type, t->exp_bts_ptype,
462 endp->net_end.payload_type, t->exp_net_ptype);
463
464 if (t->exp_bts_ptype != PTYPE_IGNORE)
465 OSMO_ASSERT(endp->bts_end.payload_type ==
466 t->exp_bts_ptype);
467 if (t->exp_net_ptype != PTYPE_IGNORE)
468 OSMO_ASSERT(endp->net_end.payload_type ==
469 t->exp_net_ptype);
470
471 /* Reset them again for next test */
472 endp->net_end.payload_type = PTYPE_NONE;
473 }
Harald Weltef74da142012-01-27 00:36:56 +0100474 }
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100475
476 talloc_free(cfg);
477}
478
Holger Hans Peter Freytherce553612012-12-07 15:04:07 +0100479static void test_retransmission(void)
480{
481 struct mgcp_config *cfg;
482 int i;
483
484 cfg = mgcp_config_alloc();
485
486 cfg->trunk.number_endpoints = 64;
487 mgcp_endpoints_allocate(&cfg->trunk);
488
489 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
490
Jacob Erlbeck0a1bc562013-12-10 13:09:37 +0100491 /* reset endpoints */
492 for (i = 0; i < cfg->trunk.number_endpoints; i++) {
493 struct mgcp_endpoint *endp;
494 endp = &cfg->trunk.endpoints[i];
495 endp->bts_end.packet_duration_ms = 20;
496 }
497
Holger Hans Peter Freytherce553612012-12-07 15:04:07 +0100498 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
499 const struct mgcp_test *t = &retransmit[i];
500 struct msgb *inp;
501 struct msgb *msg;
502
503 printf("Testing %s\n", t->name);
504
505 inp = create_msg(t->req);
506 msg = mgcp_handle_message(cfg, inp);
507 msgb_free(inp);
508 if (strcmp((char *) msg->data, t->exp_resp) != 0)
509 printf("%s failed '%s'\n", t->name, (char *) msg->data);
510 msgb_free(msg);
511
512 /* Retransmit... */
513 printf("Re-transmitting %s\n", t->name);
514 inp = create_msg(t->req);
515 msg = mgcp_handle_message(cfg, inp);
516 msgb_free(inp);
517 if (strcmp((char *) msg->data, t->exp_resp) != 0)
518 printf("%s failed '%s'\n", t->name, (char *) msg->data);
519 msgb_free(msg);
520 }
521
522 talloc_free(cfg);
523}
524
525static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
Holger Hans Peter Freyther8d0be252012-11-29 12:54:22 +0100526{
527 ptrdiff_t tone = _tone;
528 endp->cfg->data = (void *) tone;
529 return 0;
530}
531
532static void test_rqnt_cb(void)
533{
534 struct mgcp_config *cfg;
535 struct msgb *inp, *msg;
536
537 cfg = mgcp_config_alloc();
538 cfg->rqnt_cb = rqnt_cb;
539
540 cfg->trunk.number_endpoints = 64;
541 mgcp_endpoints_allocate(&cfg->trunk);
542
543 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
544
545 inp = create_msg(CRCX);
546 msgb_free(mgcp_handle_message(cfg, inp));
547 msgb_free(inp);
548
549 /* send the RQNT and check for the CB */
550 inp = create_msg(RQNT);
551 msg = mgcp_handle_message(cfg, inp);
552 if (strncmp((const char *) msg->l2h, "200", 3) != 0) {
553 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
554 abort();
555 }
556
557 if (cfg->data != (void *) '9') {
558 printf("FAILED: callback not called: %p\n", cfg->data);
559 abort();
560 }
561
562 msgb_free(msg);
563 msgb_free(inp);
564
565 inp = create_msg(DLCX);
566 msgb_free(mgcp_handle_message(cfg, inp));
567 msgb_free(inp);
568 talloc_free(cfg);
569}
570
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200571struct pl_test {
572 int cycles;
573 uint16_t base_seq;
574 uint16_t max_seq;
575 uint32_t packets;
576
577 uint32_t expected;
578 int loss;
579};
580
581static const struct pl_test pl_test_dat[] = {
582 /* basic.. just one package */
583 { .cycles = 0, .base_seq = 0, .max_seq = 0, .packets = 1, .expected = 1, .loss = 0},
584 /* some packages and a bit of loss */
585 { .cycles = 0, .base_seq = 0, .max_seq = 100, .packets = 100, .expected = 101, .loss = 1},
586 /* wrap around */
587 { .cycles = 1<<16, .base_seq = 0xffff, .max_seq = 2, .packets = 4, .expected = 4, .loss = 0},
588 /* min loss */
589 { .cycles = 0, .base_seq = 0, .max_seq = 0, .packets = UINT_MAX, .expected = 1, .loss = INT_MIN },
590 /* max loss, with wrap around on expected max */
591 { .cycles = INT_MAX, .base_seq = 0, .max_seq = UINT16_MAX, .packets = 0, .expected = ((uint32_t)(INT_MAX) + UINT16_MAX + 1), .loss = INT_MAX },
592};
593
594static void test_packet_loss_calc(void)
595{
596 int i;
597 printf("Testing packet loss calculation.\n");
598
599 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
600 uint32_t expected;
601 int loss;
602 struct mgcp_rtp_state state;
603 struct mgcp_rtp_end rtp;
604 memset(&state, 0, sizeof(state));
605 memset(&rtp, 0, sizeof(rtp));
606
607 state.initialized = 1;
608 state.base_seq = pl_test_dat[i].base_seq;
Jacob Erlbeck50079a12013-11-25 12:53:28 +0100609 state.out_stream.last_seq = pl_test_dat[i].max_seq;
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200610 state.cycles = pl_test_dat[i].cycles;
611
612 rtp.packets = pl_test_dat[i].packets;
613 mgcp_state_calc_loss(&state, &rtp, &expected, &loss);
614
615 if (loss != pl_test_dat[i].loss || expected != pl_test_dat[i].expected) {
616 printf("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
617 i, loss, pl_test_dat[i].loss,
618 expected, pl_test_dat[i].expected);
619 }
620 }
621}
622
Holger Hans Peter Freyther462b7d72012-10-24 21:53:40 +0200623static void test_mgcp_stats(void)
624{
625 printf("Testing stat parsing\n");
626
627 uint32_t bps, bos, pr, _or, jitter;
628 struct msgb *msg;
629 int loss;
630 int rc;
631
632 msg = create_msg(DLCX_RET);
633 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
634 printf("Parsing result: %d\n", rc);
635 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0 || jitter != 0)
636 printf("FAIL: Parsing failed1.\n");
637 msgb_free(msg);
638
639 msg = create_msg("250 7 OK\r\nP: PS=10, OS=20, PR=30, OR=40, PL=-3, JI=40\r\n");
640 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
641 printf("Parsing result: %d\n", rc);
642 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3 || jitter != 40)
643 printf("FAIL: Parsing failed2.\n");
644 msgb_free(msg);
645}
646
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100647struct rtp_packet_info {
648 float txtime;
649 int len;
650 char *data;
651};
652
653struct rtp_packet_info test_rtp_packets1[] = {
654 /* RTP: SeqNo=0, TS=0 */
655 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
656 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
657 /* RTP: SeqNo=1, TS=160 */
658 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
659 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
660 /* RTP: SeqNo=2, TS=320 */
661 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
662 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
663 /* Repeat RTP timestamp: */
664 /* RTP: SeqNo=3, TS=320 */
665 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
666 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
667 /* RTP: SeqNo=4, TS=480 */
668 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
669 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
670 /* RTP: SeqNo=5, TS=640 */
671 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
672 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
673 /* Double skip RTP timestamp (delta = 2*160): */
674 /* RTP: SeqNo=6, TS=960 */
675 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
676 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
677 /* RTP: SeqNo=7, TS=1120 */
678 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
679 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
680 /* RTP: SeqNo=8, TS=1280 */
681 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
682 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
683 /* Non 20ms RTP timestamp (delta = 120): */
684 /* RTP: SeqNo=9, TS=1400 */
685 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
686 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
687 /* RTP: SeqNo=10, TS=1560 */
688 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
689 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
690 /* RTP: SeqNo=11, TS=1720 */
691 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
692 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
693 /* SSRC changed to 0x10203040, RTP timestamp jump */
694 /* RTP: SeqNo=12, TS=34688 */
695 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
696 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
697 /* RTP: SeqNo=13, TS=34848 */
698 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
699 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
700 /* RTP: SeqNo=14, TS=35008 */
701 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
702 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Jacob Erlbeck83c05232013-12-03 12:48:07 +0100703 /* Non 20ms RTP timestamp (delta = 120): */
704 /* RTP: SeqNo=15, TS=35128 */
705 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
706 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
707 /* RTP: SeqNo=16, TS=35288 */
708 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
709 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
710 /* RTP: SeqNo=17, TS=35448 */
711 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
712 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
713 /* SeqNo increment by 2, RTP timestamp delta = 320: */
714 /* RTP: SeqNo=19, TS=35768 */
715 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
716 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
717 /* RTP: SeqNo=20, TS=35928 */
718 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
719 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
720 /* RTP: SeqNo=21, TS=36088 */
Jacob Erlbeck93c9da32013-12-18 12:54:49 +0100721 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
722 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
723 /* Repeat last packet */
724 /* RTP: SeqNo=21, TS=36088 */
725 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
726 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
727 /* RTP: SeqNo=22, TS=36248 */
728 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
729 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
730 /* RTP: SeqNo=23, TS=36408 */
731 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
732 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
733 /* Don't increment SeqNo but increment timestamp by 160 */
734 /* RTP: SeqNo=23, TS=36568 */
735 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
736 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
737 /* RTP: SeqNo=24, TS=36728 */
738 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
739 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
740 /* RTP: SeqNo=25, TS=36888 */
741 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Jacob Erlbeck83c05232013-12-03 12:48:07 +0100742 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Jacob Erlbecke763f3e2014-01-30 21:01:33 +0100743 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
744 * SeqNo jump */
745 /* RTP: SeqNo=1000, TS=160000 */
746 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
747 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
748 /* RTP: SeqNo=1001, TS=160160 */
749 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
750 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
751 /* RTP: SeqNo=1002, TS=160320 */
752 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
753 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100754};
755
756void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
757 struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr,
758 char *data, int len);
759
Jacob Erlbeck83c05232013-12-03 12:48:07 +0100760static void test_packet_error_detection(int patch_ssrc, int patch_ts)
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100761{
762 int i;
763
764 struct mgcp_trunk_config trunk;
765 struct mgcp_endpoint endp;
766 struct mgcp_rtp_state state;
Jacob Erlbeck72c30902013-11-25 15:23:35 +0100767 struct mgcp_rtp_end *rtp = &endp.net_end;
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100768 struct sockaddr_in addr = {0};
769 char buffer[4096];
Jacob Erlbeck83c05232013-12-03 12:48:07 +0100770 uint32_t last_ssrc = 0;
Jacob Erlbeck65c7a452013-12-18 12:54:50 +0100771 uint32_t last_timestamp = 0;
772 uint32_t last_seqno = 0;
773 int last_in_ts_err_cnt = 0;
774 int last_out_ts_err_cnt = 0;
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100775
Jacob Erlbeck83c05232013-12-03 12:48:07 +0100776 printf("Testing packet error detection%s%s.\n",
777 patch_ssrc ? ", patch SSRC" : "",
778 patch_ts ? ", patch timestamps" : "");
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100779
780 memset(&trunk, 0, sizeof(trunk));
781 memset(&endp, 0, sizeof(endp));
782 memset(&state, 0, sizeof(state));
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100783
784 trunk.number_endpoints = 1;
785 trunk.endpoints = &endp;
Jacob Erlbeckdb2d4312013-12-03 14:43:34 +0100786 trunk.force_constant_ssrc = patch_ssrc;
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100787 trunk.force_aligned_timing = patch_ts;
Jacob Erlbeckdb2d4312013-12-03 14:43:34 +0100788
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100789 endp.tcfg = &trunk;
790
Jacob Erlbeck72c30902013-11-25 15:23:35 +0100791 /* This doesn't free endp but resets/frees all fields of the structure
792 * and invokes mgcp_rtp_end_reset() for each mgcp_rtp_end. OTOH, it
793 * expects valid pointer fields (either NULL or talloc'ed), so the
794 * memset is still needed. It also requires that endp.tcfg and
795 * trunk.endpoints are set up properly. */
796 mgcp_free_endp(&endp);
797
798 rtp->payload_type = 98;
Jacob Erlbeck72c30902013-11-25 15:23:35 +0100799
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100800 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
801 struct rtp_packet_info *info = test_rtp_packets1 + i;
802
Jacob Erlbecke763f3e2014-01-30 21:01:33 +0100803 force_monotonic_time_us = 1000000ULL * info->txtime;
804
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100805 OSMO_ASSERT(info->len <= sizeof(buffer));
806 OSMO_ASSERT(info->len >= 0);
807 memmove(buffer, info->data, info->len);
808
Jacob Erlbeckdb2d4312013-12-03 14:43:34 +0100809 mgcp_rtp_end_config(&endp, 1, rtp);
810
Jacob Erlbeck72c30902013-11-25 15:23:35 +0100811 mgcp_patch_and_count(&endp, &state, rtp, &addr,
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100812 buffer, info->len);
813
Jacob Erlbeck83c05232013-12-03 12:48:07 +0100814 if (state.out_stream.ssrc != last_ssrc) {
815 printf("Output SSRC changed to %08x\n",
Jacob Erlbeck3da9e4e2013-12-03 15:47:04 +0100816 state.out_stream.ssrc);
Jacob Erlbeck83c05232013-12-03 12:48:07 +0100817 last_ssrc = state.out_stream.ssrc;
818 }
819
Jacob Erlbeck65c7a452013-12-18 12:54:50 +0100820 printf("In TS: %d, dTS: %d, Seq: %d\n",
821 state.in_stream.last_timestamp,
822 state.in_stream.last_tsdelta,
823 state.in_stream.last_seq);
824
825 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
826 "TS Err change: in %+d, out %+d\n",
827 state.out_stream.last_timestamp - last_timestamp,
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100828 state.out_stream.last_tsdelta,
Jacob Erlbeck65c7a452013-12-18 12:54:50 +0100829 state.out_stream.last_seq - last_seqno,
830 state.in_stream.err_ts_counter - last_in_ts_err_cnt,
831 state.out_stream.err_ts_counter - last_out_ts_err_cnt);
832
Jacob Erlbecke763f3e2014-01-30 21:01:33 +0100833 printf("Stats: Jitter = %u, Transit = %u\n",
834 mgcp_state_calc_jitter(&state), state.transit);
835
Jacob Erlbeck65c7a452013-12-18 12:54:50 +0100836 last_in_ts_err_cnt = state.in_stream.err_ts_counter;
837 last_out_ts_err_cnt = state.out_stream.err_ts_counter;
838 last_timestamp = state.out_stream.last_timestamp;
839 last_seqno = state.out_stream.last_seq;
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100840 }
Jacob Erlbecke763f3e2014-01-30 21:01:33 +0100841
842 force_monotonic_time_us = -1;
Jacob Erlbeckd62419b2013-11-25 12:53:29 +0100843}
844
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100845int main(int argc, char **argv)
846{
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200847 osmo_init_logging(&log_info);
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100848
Jacob Erlbeck2bee7f92013-11-29 13:43:44 +0100849 test_strline();
Jacob Erlbecke35fd132014-01-16 16:50:40 +0100850 test_values();
Holger Hans Peter Freyther5563e6c2012-03-16 09:45:13 +0100851 test_messages();
Holger Hans Peter Freytherce553612012-12-07 15:04:07 +0100852 test_retransmission();
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200853 test_packet_loss_calc();
Holger Hans Peter Freyther8d0be252012-11-29 12:54:22 +0100854 test_rqnt_cb();
Holger Hans Peter Freyther462b7d72012-10-24 21:53:40 +0200855 test_mgcp_stats();
Jacob Erlbeck83c05232013-12-03 12:48:07 +0100856 test_packet_error_detection(1, 0);
857 test_packet_error_detection(0, 0);
858 test_packet_error_detection(0, 1);
859 test_packet_error_detection(1, 1);
Holger Hans Peter Freytherd5b3ca62012-01-06 15:04:43 +0100860
861 printf("Done\n");
862 return EXIT_SUCCESS;
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100863}