blob: f0b58ef02a5e250a66ae6bbccecd4b486d26be55 [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 */
19
20#include <openbsc/mgcp.h>
Holger Hans Peter Freyther1f0c5b42011-02-28 14:37:03 +010021#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +010022
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020023#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010024#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +010025#include <string.h>
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +020026#include <limits.h>
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +010027
Harald Welte8819d822012-01-27 00:00:44 +010028#define AUEP1 "AUEP 158663169 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n"
Harald Weltef74da142012-01-27 00:36:56 +010029#define AUEP1_RET "200 158663169 OK\r\n"
Harald Welte8819d822012-01-27 00:00:44 +010030#define AUEP2 "AUEP 18983213 ds/e1-2/1@172.16.6.66 MGCP 1.0\r\n"
Harald Weltef74da142012-01-27 00:36:56 +010031#define AUEP2_RET "500 18983213 FAIL\r\n"
Holger Hans Peter Freytherd77023b2012-03-16 09:53:49 +010032#define EMPTY "\r\n"
33#define EMPTY_RET NULL
34#define SHORT "CRCX \r\n"
35#define SHORT_RET "510 000000 FAIL\r\n"
36
Holger Hans Peter Freythercf2f1582012-12-10 12:54:57 +010037#define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@172.16.6.66 MGCP 1.0\r\n"
38#define MDCX_ERR_RET "510 18983213 FAIL\r\n"
39#define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n"
40#define MDCX_RET "400 18983214 FAIL\r\n"
41#define MDCX3 "MDCX 18983215 1@mgw MGCP 1.0\r\n"
42#define MDCX3_RET "200 18983215 OK\r\n" \
43 "I: 1\n" \
44 "\n" \
45 "v=0\r\n" \
46 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
47 "c=IN IP4 0.0.0.0\r\n" \
48 "t=0 0\r\n" \
49 "m=audio 0 RTP/AVP 126\r\n" \
50 "a=rtpmap:126 AMR/8000\r\n"
51
Holger Hans Peter Freytherd77023b2012-03-16 09:53:49 +010052#define SHORT2 "CRCX 1"
53#define SHORT2_RET "510 000000 FAIL\r\n"
54#define SHORT3 "CRCX 1 1@mgw"
55#define SHORT4 "CRCX 1 1@mgw MGCP"
56#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
Harald Weltef74da142012-01-27 00:36:56 +010057
Harald Welte27fa7902012-01-27 00:37:23 +010058#define CRCX "CRCX 2 1@mgw MGCP 1.0\r\n" \
59 "M: sendrecv\r\n" \
60 "C: 2\r\n" \
61 "\r\n" \
62 "v=0\r\n" \
63 "c=IN IP4 123.12.12.123\r\n" \
64 "m=audio 5904 RTP/AVP 97\r\n" \
65 "a=rtpmap:97 GSM-EFR/8000\r\n"
66
67#define CRCX_RET "200 2 OK\r\n" \
68 "I: 1\n" \
69 "\n" \
70 "v=0\r\n" \
71 "o=- 1 23 IN IP4 0.0.0.0\r\n" \
72 "c=IN IP4 0.0.0.0\r\n" \
73 "t=0 0\r\n" \
74 "m=audio 0 RTP/AVP 126\r\n" \
75 "a=rtpmap:126 AMR/8000\r\n"
76
77
78#define CRCX_ZYN "CRCX 2 1@mgw MGCP 1.0\r" \
79 "M: sendrecv\r" \
80 "C: 2\r\r" \
81 "v=0\r" \
82 "c=IN IP4 123.12.12.123\r" \
83 "m=audio 5904 RTP/AVP 97\r" \
84 "a=rtpmap:97 GSM-EFR/8000\r"
Harald Weltef74da142012-01-27 00:36:56 +010085
Holger Hans Peter Freytherd267f4d2012-12-07 14:42:03 +010086#define CRCX_ZYN_RET "200 2 OK\r\n" \
87 "I: 2\n" \
88 "\n" \
89 "v=0\r\n" \
90 "o=- 2 23 IN IP4 0.0.0.0\r\n" \
91 "c=IN IP4 0.0.0.0\r\n" \
92 "t=0 0\r\n" \
93 "m=audio 0 RTP/AVP 126\r\n" \
94 "a=rtpmap:126 AMR/8000\r\n"
95
Holger Hans Peter Freyther0bf15a82012-09-14 17:18:12 +020096#define DLCX "DLCX 7 1@mgw MGCP 1.0\r\n" \
97 "C: 2\r\n"
98
99#define DLCX_RET "250 7 OK\r\n" \
Holger Hans Peter Freythercb306a62012-10-24 21:22:47 +0200100 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n"
Holger Hans Peter Freyther0bf15a82012-09-14 17:18:12 +0200101
Holger Hans Peter Freyther8d0be252012-11-29 12:54:22 +0100102#define RQNT "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
103 "X: B244F267488\r\n" \
104 "S: D/9\r\n"
105
106#define RQNT2 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
107 "X: ADD4F26746F\r\n" \
108 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
109
110#define RQNT_RET "200 186908780 OK\r\n"
Holger Hans Peter Freyther0bf15a82012-09-14 17:18:12 +0200111
Harald Weltef74da142012-01-27 00:36:56 +0100112struct mgcp_test {
113 const char *name;
114 const char *req;
115 const char *exp_resp;
116};
117
Holger Hans Peter Freytherd267f4d2012-12-07 14:42:03 +0100118static const struct mgcp_test tests[] = {
Harald Weltef74da142012-01-27 00:36:56 +0100119 { "AUEP1", AUEP1, AUEP1_RET },
120 { "AUEP2", AUEP2, AUEP2_RET },
Holger Hans Peter Freythercf2f1582012-12-10 12:54:57 +0100121 { "MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET },
122 { "MDCX2", MDCX_UNALLOCATED, MDCX_RET },
Harald Welte27fa7902012-01-27 00:37:23 +0100123 { "CRCX", CRCX, CRCX_RET },
Holger Hans Peter Freythercf2f1582012-12-10 12:54:57 +0100124 { "MDCX3", MDCX3, MDCX3_RET },
Holger Hans Peter Freytherd267f4d2012-12-07 14:42:03 +0100125 { "DLCX", DLCX, DLCX_RET },
126 { "CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET },
Holger Hans Peter Freytherd77023b2012-03-16 09:53:49 +0100127 { "EMPTY", EMPTY, EMPTY_RET },
128 { "SHORT1", SHORT, SHORT_RET },
129 { "SHORT2", SHORT2, SHORT2_RET },
130 { "SHORT3", SHORT3, SHORT2_RET },
131 { "SHORT4", SHORT4, SHORT2_RET },
Holger Hans Peter Freyther8d0be252012-11-29 12:54:22 +0100132 { "RQNT1", RQNT, RQNT_RET },
133 { "RQNT2", RQNT2, RQNT_RET },
Holger Hans Peter Freyther0bf15a82012-09-14 17:18:12 +0200134 { "DLCX", DLCX, DLCX_RET },
Harald Weltef74da142012-01-27 00:36:56 +0100135};
Harald Welte8819d822012-01-27 00:00:44 +0100136
137static struct msgb *create_msg(const char *str)
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100138{
139 struct msgb *msg;
140
141 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Harald Welte8819d822012-01-27 00:00:44 +0100142 int len = sprintf((char *)msg->data, str);
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100143 msg->l2h = msgb_put(msg, len);
144 return msg;
145}
146
Holger Hans Peter Freyther5563e6c2012-03-16 09:45:13 +0100147static void test_messages(void)
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100148{
Holger Hans Peter Freytherd5b3ca62012-01-06 15:04:43 +0100149 struct mgcp_config *cfg;
Harald Weltef74da142012-01-27 00:36:56 +0100150 int i;
Holger Hans Peter Freytherd5b3ca62012-01-06 15:04:43 +0100151
152 cfg = mgcp_config_alloc();
Harald Weltef74da142012-01-27 00:36:56 +0100153
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100154 cfg->trunk.number_endpoints = 64;
Holger Hans Peter Freyther1f0c5b42011-02-28 14:37:03 +0100155 mgcp_endpoints_allocate(&cfg->trunk);
156
157 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100158
Harald Weltef74da142012-01-27 00:36:56 +0100159 for (i = 0; i < ARRAY_SIZE(tests); i++) {
160 const struct mgcp_test *t = &tests[i];
161 struct msgb *inp;
162 struct msgb *msg;
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100163
Harald Weltef74da142012-01-27 00:36:56 +0100164 printf("Testing %s\n", t->name);
165
166 inp = create_msg(t->req);
167 msg = mgcp_handle_message(cfg, inp);
168 msgb_free(inp);
Holger Hans Peter Freytherd77023b2012-03-16 09:53:49 +0100169 if (!t->exp_resp) {
170 if (msg)
171 printf("%s failed '%s'\n", t->name, (char *) msg->data);
172 } else if (strcmp((char *) msg->data, t->exp_resp) != 0)
Harald Weltef74da142012-01-27 00:36:56 +0100173 printf("%s failed '%s'\n", t->name, (char *) msg->data);
174 msgb_free(msg);
175 }
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100176
177 talloc_free(cfg);
178}
179
Holger Hans Peter Freyther8d0be252012-11-29 12:54:22 +0100180static int rqnt_cb(struct mgcp_endpoint *endp, char _tone, const char *data)
181{
182 ptrdiff_t tone = _tone;
183 endp->cfg->data = (void *) tone;
184 return 0;
185}
186
187static void test_rqnt_cb(void)
188{
189 struct mgcp_config *cfg;
190 struct msgb *inp, *msg;
191
192 cfg = mgcp_config_alloc();
193 cfg->rqnt_cb = rqnt_cb;
194
195 cfg->trunk.number_endpoints = 64;
196 mgcp_endpoints_allocate(&cfg->trunk);
197
198 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
199
200 inp = create_msg(CRCX);
201 msgb_free(mgcp_handle_message(cfg, inp));
202 msgb_free(inp);
203
204 /* send the RQNT and check for the CB */
205 inp = create_msg(RQNT);
206 msg = mgcp_handle_message(cfg, inp);
207 if (strncmp((const char *) msg->l2h, "200", 3) != 0) {
208 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
209 abort();
210 }
211
212 if (cfg->data != (void *) '9') {
213 printf("FAILED: callback not called: %p\n", cfg->data);
214 abort();
215 }
216
217 msgb_free(msg);
218 msgb_free(inp);
219
220 inp = create_msg(DLCX);
221 msgb_free(mgcp_handle_message(cfg, inp));
222 msgb_free(inp);
223 talloc_free(cfg);
224}
225
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200226struct pl_test {
227 int cycles;
228 uint16_t base_seq;
229 uint16_t max_seq;
230 uint32_t packets;
231
232 uint32_t expected;
233 int loss;
234};
235
236static const struct pl_test pl_test_dat[] = {
237 /* basic.. just one package */
238 { .cycles = 0, .base_seq = 0, .max_seq = 0, .packets = 1, .expected = 1, .loss = 0},
239 /* some packages and a bit of loss */
240 { .cycles = 0, .base_seq = 0, .max_seq = 100, .packets = 100, .expected = 101, .loss = 1},
241 /* wrap around */
242 { .cycles = 1<<16, .base_seq = 0xffff, .max_seq = 2, .packets = 4, .expected = 4, .loss = 0},
243 /* min loss */
244 { .cycles = 0, .base_seq = 0, .max_seq = 0, .packets = UINT_MAX, .expected = 1, .loss = INT_MIN },
245 /* max loss, with wrap around on expected max */
246 { .cycles = INT_MAX, .base_seq = 0, .max_seq = UINT16_MAX, .packets = 0, .expected = ((uint32_t)(INT_MAX) + UINT16_MAX + 1), .loss = INT_MAX },
247};
248
249static void test_packet_loss_calc(void)
250{
251 int i;
252 printf("Testing packet loss calculation.\n");
253
254 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
255 uint32_t expected;
256 int loss;
257 struct mgcp_rtp_state state;
258 struct mgcp_rtp_end rtp;
259 memset(&state, 0, sizeof(state));
260 memset(&rtp, 0, sizeof(rtp));
261
262 state.initialized = 1;
263 state.base_seq = pl_test_dat[i].base_seq;
264 state.max_seq = pl_test_dat[i].max_seq;
265 state.cycles = pl_test_dat[i].cycles;
266
267 rtp.packets = pl_test_dat[i].packets;
268 mgcp_state_calc_loss(&state, &rtp, &expected, &loss);
269
270 if (loss != pl_test_dat[i].loss || expected != pl_test_dat[i].expected) {
271 printf("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
272 i, loss, pl_test_dat[i].loss,
273 expected, pl_test_dat[i].expected);
274 }
275 }
276}
277
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100278int main(int argc, char **argv)
279{
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200280 osmo_init_logging(&log_info);
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100281
Holger Hans Peter Freyther5563e6c2012-03-16 09:45:13 +0100282 test_messages();
Holger Hans Peter Freyther38e02c52012-10-22 18:09:35 +0200283 test_packet_loss_calc();
Holger Hans Peter Freyther8d0be252012-11-29 12:54:22 +0100284 test_rqnt_cb();
Holger Hans Peter Freytherd5b3ca62012-01-06 15:04:43 +0100285
286 printf("Done\n");
287 return EXIT_SUCCESS;
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100288}