blob: eb41d3eaf5e8de2db1fcf62b02737bca661f81ab [file] [log] [blame]
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001/*
2 * SCCP testing code
3 *
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freyther6ae65722010-02-03 18:10:07 +01005 * (C) 2009 by On-Waves
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <stdio.h>
26
27#include <arpa/inet.h>
28
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020029#include <openbsc/gsm_data.h>
30#include <openbsc/debug.h>
Harald Weltef98a4972010-02-20 17:29:27 +010031#include <osmocore/msgb.h>
Holger Hans Peter Freyther8a69cb22010-02-12 22:44:50 +010032
33#include <sccp/sccp.h>
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020034
35#define MIN(x, y) ((x) < (y) ? (x) : (y))
36
37/* BSC -> MSC */
38static const u_int8_t bssmap_reset[] = {
39 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
40 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
41 0x01, 0x20,
42};
43
44/* MSC -> BSC reset ack */
45static const u_int8_t bssmap_reset_ack[] = {
46 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
47 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
48 0x00, 0x01, 0x31,
49};
50
51/* MSC -> BSC paging, connection less */
52static const u_int8_t bssmap_paging[] = {
53 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
54 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x10,
55 0x00, 0x0e, 0x52, 0x08, 0x08, 0x29, 0x47, 0x10,
56 0x02, 0x01, 0x31, 0x97, 0x61, 0x1a, 0x01, 0x06,
57};
58
59/* MSC -> BSC paging, UDT without PC */
60static const u_int8_t bssmap_udt[] = {
61 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
62 0x02, 0x42, 0xfe, 0x10, 0x00, 0x0e, 0x52, 0x08,
63 0x08, 0x29, 0x47, 0x10, 0x02, 0x01, 0x31, 0x97,
64 0x61, 0x1a, 0x01, 0x06,
65};
66
67/* BSC -> MSC connection open */
68static const u_int8_t bssmap_cr[] = {
69 0x01, 0x01, 0x02, 0x03, 0x02, 0x02, 0x04, 0x02,
70 0x42, 0xfe, 0x0f, 0x1f, 0x00, 0x1d, 0x57, 0x05,
71 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x12, 0xc3,
72 0x50, 0x17, 0x10, 0x05, 0x24, 0x11, 0x03, 0x33,
73 0x19, 0xa2, 0x08, 0x29, 0x47, 0x10, 0x02, 0x01,
74 0x31, 0x97, 0x61, 0x00
75};
76
77/* MSC -> BSC connection confirm */
78static const u_int8_t bssmap_cc[] = {
79 0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
80};
81
82/* MSC -> BSC DTAP
83 *
84 * we fake a bit and make it BSC -> MSC... so the
85 * payload does not make any sense..
86 */
87static const u_int8_t bssmap_dtap[] = {
88 0x06, 0x00, 0x00, 0x03, 0x00, 0x01, 0x0f, 0x01, 0x00, 0x0c,
89 0x03, 0x05, 0x5c, 0x08, 0x11, 0x81, 0x33, 0x66, 0x02, 0x13,
90 0x45, 0xf4,
91};
92
93/* MSC -> BSC clear command */
94static const u_int8_t bssmap_clear[] = {
95 0x06, 0x00, 0x00, 0x03, 0x00, 0x01, 0x06, 0x00, 0x04, 0x20,
96 0x04, 0x01, 0x09,
97};
98
99/* MSC -> BSC released */
100static const u_int8_t bssmap_released[] = {
101 0x04, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x01, 0x0f,
102 0x02, 0x23, 0x42, 0x00,
103};
104
105/* BSC -> MSC released */
106static const u_int8_t bssmap_release_complete[] = {
107 0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03
108};
109
110struct test_data {
111 int length;
112 const u_int8_t *data;
113 int payload_start;
114 int payload_length;
115 u_int8_t first_byte;
116
117 /* in case it should trigger a sccp response */
118 int write;
119 const u_int8_t *response;
120 int response_length;
121};
122
123static const struct test_data test_data[] = {
124 {
125 .length = ARRAY_SIZE(bssmap_reset),
126 .data = &bssmap_reset[0],
127 .payload_start = 12,
128 .payload_length = ARRAY_SIZE(bssmap_reset) - 12,
129 .first_byte = 0x0,
130 },
131 {
132 .length = ARRAY_SIZE(bssmap_reset_ack),
133 .data = &bssmap_reset_ack[0],
134 .payload_start = 16,
135 .payload_length = ARRAY_SIZE(bssmap_reset_ack) - 16,
136 .first_byte = 0x0,
137 },
138 {
139 .length = ARRAY_SIZE(bssmap_paging),
140 .data = &bssmap_paging[0],
141 .payload_start = 16,
142 .payload_length = ARRAY_SIZE(bssmap_paging) - 16,
143 .first_byte = 0x0,
144 },
145 {
146 .length = ARRAY_SIZE(bssmap_cr),
147 .data = &bssmap_cr[0],
148 .payload_start = 12,
149 /* 0x00 is end of optional data, subtract this byte */
150 .payload_length = 31,
151 .first_byte = 0x0,
152
153 /* the connection request should trigger a connection confirm */
154 .write = 1,
155 .response = &bssmap_cc[0],
156 .response_length= ARRAY_SIZE(bssmap_cc),
157 },
158 {
159 .length = ARRAY_SIZE(bssmap_dtap),
160 .data = &bssmap_dtap[0],
161 .payload_start = 7,
162 .payload_length = 15,
163 .first_byte = 0x01,
164 },
165 {
166 .length = ARRAY_SIZE(bssmap_clear),
167 .data = &bssmap_clear[0],
168 .payload_start = 7,
169 .payload_length = 6,
170 .first_byte = 0x00,
171 },
172 {
173 .length = ARRAY_SIZE(bssmap_released),
174 .data = &bssmap_released[0],
175 .payload_length = 2,
176 .payload_start = 11,
177 .first_byte = 0x23,
178
179 .write = 1,
180 .response = &bssmap_release_complete[0],
181 .response_length= ARRAY_SIZE(bssmap_release_complete),
182 },
183};
184
185/* we will send UDTs and verify they look like this */
186static const struct test_data send_data[] = {
187 {
188 .length = ARRAY_SIZE(bssmap_udt),
189 .data = &bssmap_udt[0],
190 .payload_start = 12,
191 .payload_length = ARRAY_SIZE(bssmap_udt) - 12,
192 .first_byte = 0x0,
193 },
194 {
195 .length = ARRAY_SIZE(bssmap_reset),
196 .data = &bssmap_reset[0],
197 .payload_start = 12,
198 .payload_length = ARRAY_SIZE(bssmap_reset) - 12,
199 .first_byte = 0x0,
200 },
201};
202
203struct connection_test {
204 /* should the connection be refused? */
205 int refuse;
206
207 int with_data;
208
209 /* on which side to close the connection? */
210 int close_side;
211 int close_cause;
212};
213
214/* sccp connection handling we want to test */
215static const struct connection_test connection_tests[] = {
216 {
217 .refuse = 1,
218 },
219 {
220 .refuse = 1,
221 .with_data = 1,
222 },
223 {
224 .refuse = 0,
225 .close_side = 0,
226 .close_cause = 5,
227 },
228 {
229 .refuse = 0,
230 .close_side = 0,
231 .close_cause = 5,
232 .with_data = 1,
233 },
234 {
235 .refuse = 0,
236 .close_side = 1,
237 .close_cause = 5,
238 },
239 {
240 .refuse = 0,
241 .close_side = 1,
242 .close_cause = 5,
243 .with_data = 1,
244 },
245};
246
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +0100247struct sccp_parse_header_result {
248 /* results */
249 int msg_type;
250 int wanted_len;
251 int src_ssn;
252 int dst_ssn;
253
254 int has_src_ref, has_dst_ref;
255 struct sccp_source_reference src_ref;
256 struct sccp_source_reference dst_ref;
257
258 /* the input */
259 const u_int8_t *input;
260 int input_len;
261};
262
263static const u_int8_t it_test[] = {
2640x10, 0x01, 0x07,
2650x94, 0x01, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00 };
266
267static const struct sccp_parse_header_result parse_result[] = {
268 {
269 .msg_type = SCCP_MSG_TYPE_IT,
270 .wanted_len = 0,
271 .src_ssn = -1,
272 .dst_ssn = -1,
273 .has_src_ref = 1,
274 .has_dst_ref = 1,
275
276 .src_ref = {
277 .octet1 = 0x01,
278 .octet2 = 0x04,
279 .octet3 = 0x00
280 },
281 .dst_ref = {
282 .octet1 = 0x01,
283 .octet2 = 0x07,
284 .octet3 = 0x94,
285 },
286
287 .input = it_test,
288 .input_len = sizeof(it_test),
289 },
290};
291
292
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200293/* testing procedure:
294 * - we will use sccp_write and see what will be set in the
295 * outgoing callback
296 * - we will call sccp_system_incoming and see which calls
297 * are made. And then compare it to the ones we expect. We
298 * want the payload to arrive, or callbacks to be called.
299 * - we will use sccp_connection_socket and sccp_connection_write
300 * and verify state handling of connections
301 */
302
303static int current_test;
304
305/*
306 * test state...
307 */
308static int called = 0;
309static int matched = 0;
310static int write_called = 0;
311
312#define FAIL(x, args...) printf("FAILURE in %s:%d: " x, __FILE__, __LINE__, ## args)
313
314/*
315 * writing these packets and expecting a result
316 */
317int sccp_read_cb(struct msgb *data, unsigned len, void *context)
318{
319 u_int16_t payload_length = test_data[current_test].payload_length;
320 const u_int8_t *got, *wanted;
321 int i;
322
323 called = 1;
324
325 if (msgb_l3len(data) < len) {
326 /* this should never be reached */
327 FAIL("Something horrible happened.. invalid packet..\n");
328 exit(-1);
329 }
330
331 if (len == 0 || len != payload_length) {
332 FAIL("length mismatch: got: %d wanted: %d\n", msgb_l3len(data), payload_length);
333 return -1;
334 }
335
336 if (data->l3h[0] != test_data[current_test].first_byte) {
337 FAIL("The first bytes of l3 do not match: 0x%x 0x%x\n",
338 data->l3h[0], test_data[current_test].first_byte);
339 return -1;
340 }
341
342 got = &data->l3h[0];
343 wanted = test_data[current_test].data + test_data[current_test].payload_start;
344
345 for (i = 0; i < len; ++i) {
346 if (got[i] != wanted[i]) {
347 FAIL("Failed to compare byte. Got: 0x%x Wanted: 0x%x at %d\n",
348 got[i], wanted[i], i);
349 return -1;
350 }
351 }
352
353 matched = 1;
354 return 0;
355}
356
357int sccp_write_cb(struct msgb *data, void *ctx)
358{
359 int i = 0;
360 const u_int8_t *got, *wanted;
361
362 if (test_data[current_test].response == NULL) {
363 FAIL("Didn't expect write callback\n");
364 return -1;
365 } else if (test_data[current_test].response_length != msgb_l2len(data)) {
366 FAIL("Size does not match. Got: %d Wanted: %d\n",
367 msgb_l2len(data), test_data[current_test].response_length);
368 }
369
370 got = &data->l2h[0];
371 wanted = test_data[current_test].response;
372
373 for (i = 0; i < msgb_l2len(data); ++i) {
374 if (got[i] != wanted[i]) {
375 FAIL("Failed to compare byte. Got: 0x%x Wanted: 0x%x at %d\n",
376 got[i], wanted[i], i);
377 return -1;
378 }
379 }
380
381 write_called = 1;
382 return 0;
383}
384
385void sccp_c_read(struct sccp_connection *connection, struct msgb *msgb, unsigned int len)
386{
387 sccp_read_cb(msgb, len, connection->data_ctx);
388}
389
390void sccp_c_state(struct sccp_connection *connection, int old_state)
391{
392 if (connection->connection_state >= SCCP_CONNECTION_STATE_RELEASE_COMPLETE)
393 sccp_connection_free(connection);
394}
395
396int sccp_accept_cb(struct sccp_connection *connection, void *user_data)
397{
398 called = 1;
399 unsigned int ref = 0;
400 ref |= connection->destination_local_reference.octet1 << 24;
401 ref |= connection->destination_local_reference.octet2 << 16;
402 ref |= connection->destination_local_reference.octet3 << 8;
403 ref = ntohl(ref);
404
405 connection->data_cb = sccp_c_read;
406 connection->state_cb = sccp_c_state;
407
408 /* accept this */
409 return 0;
410}
411
412static int sccp_udt_write_cb(struct msgb *data, void *context)
413{
414 const u_int8_t *got, *wanted;
415 int i;
416
417 write_called = 1;
418
419 if (send_data[current_test].length != msgb_l2len(data)) {
420 FAIL("Size does not match. Got: %d Wanted: %d\n",
421 msgb_l2len(data), send_data[current_test].length);
422 return -1;
423 }
424
425 got = &data->l2h[0];
426 wanted = send_data[current_test].data;
427
428 for (i = 0; i < msgb_l2len(data); ++i) {
429 if (got[i] != wanted[i]) {
430 FAIL("Failed to compare byte. Got: 0x%x Wanted: 0x%x at %d\n",
431 got[i], wanted[i], i);
432 return -1;
433 }
434 }
435
436 matched = 1;
437 return 0;
438}
439
440static void test_sccp_system(void)
441{
442 sccp_system_init(sccp_write_cb, NULL);
443 sccp_set_read(&sccp_ssn_bssap, sccp_read_cb, NULL);
444 sccp_connection_set_incoming(&sccp_ssn_bssap, sccp_accept_cb, NULL);
445
446 for (current_test = 0; current_test < ARRAY_SIZE(test_data); ++current_test) {
447 unsigned int length = test_data[current_test].length;
448 struct msgb *msg = msgb_alloc_headroom(length + 2, 2, __func__);
449 msg->l2h = msgb_put(msg, length);
450 memcpy(msg->l2h, test_data[current_test].data, length);
451
452 called = matched = write_called = 0;
453 printf("Testing packet: %d\n", current_test);
454 sccp_system_incoming(msg);
455
456 if (!called || !matched || (test_data[current_test].write != write_called))
457 FAIL("current test: %d called: %d matched: %d write: %d\n",
458 current_test, called, matched, write_called);
459
460 msgb_free(msg);
461 }
462}
463
464/* test sending of udt */
465static void test_sccp_send_udt(void)
466{
467 sccp_system_init(sccp_udt_write_cb, NULL);
468 sccp_set_read(NULL, NULL, NULL);
469 sccp_connection_set_incoming(NULL, NULL, NULL);
470
471 for (current_test = 0; current_test < ARRAY_SIZE(send_data); ++current_test) {
472 const struct test_data *test = &send_data[current_test];
473
474 struct msgb *msg = msgb_alloc(test->payload_length, __func__);
475 msg->l3h = msgb_put(msg, test->payload_length);
476 memcpy(msg->l3h, test->data + test->payload_start, test->payload_length);
477
478 matched = write_called = 0;
479 printf("Testing packet: %d\n", current_test);
480 sccp_write(msg, &sccp_ssn_bssap, &sccp_ssn_bssap, 0);
481
482 if (!matched || !write_called)
483 FAIL("current test: %d matched: %d write: %d\n",
484 current_test, matched, write_called);
485
486 msgb_free(msg);
487 }
488}
489
490/* send udt from one end to another */
491static unsigned int test_value = 0x2442;
492static int sccp_udt_read(struct msgb *data, unsigned int len, void *context)
493{
494 unsigned int *val;
495
496 if (len != 4) {
497 FAIL("Wrong size: %d\n", msgb_l3len(data));
498 return -1;
499 }
500
501 val = (unsigned int*)data->l3h;
502 matched = test_value == *val;
503
504 return 0;
505}
506
507static int sccp_write_loop(struct msgb *data, void *context)
508{
509 /* send it back to us */
510 sccp_system_incoming(data);
511 return 0;
512}
513
514static void test_sccp_udt_communication(void)
515{
516 struct msgb *data;
517 unsigned int *val;
518
519 sccp_system_init(sccp_write_loop, NULL);
520 sccp_set_read(&sccp_ssn_bssap, sccp_udt_read, NULL);
521 sccp_connection_set_incoming(NULL, NULL, NULL);
522
523
524 data = msgb_alloc(4, "test data");
525 data->l3h = &data->data[0];
526 val = (unsigned int *)msgb_put(data, 4);
527 *val = test_value;
528
529 matched = 0;
530 sccp_write(data, &sccp_ssn_bssap, &sccp_ssn_bssap, 0);
531
532 if (!matched)
533 FAIL("Talking with us didn't work\n");
534
535 msgb_free(data);
536}
537
538
539/* connection testing... open, send, close */
540static const struct connection_test *current_con_test;
541static struct sccp_connection *outgoing_con;
542static struct sccp_connection *incoming_con;
543static int outgoing_data, incoming_data, incoming_state, outgoing_state;
544
545static struct msgb *test_data1, *test_data2, *test_data3;
546
547static void sccp_conn_in_state(struct sccp_connection *conn, int old_state)
548{
549 printf("\tincome: %d -> %d\n", old_state, conn->connection_state);
550 if (conn->connection_state >= SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
551 if (conn == incoming_con) {
552 sccp_connection_free(conn);
553 incoming_con = NULL;
554 }
555 }
556}
557
558static void sccp_conn_in_data(struct sccp_connection *conn, struct msgb *msg, unsigned int len)
559{
560 /* compare the data */
561 ++incoming_data;
562 printf("\tincoming data: %d\n", len);
563
564 /* compare the data */
565 if (len != 4) {
566 FAIL("Length of packet is wrong: %u %u\n", msgb_l3len(msg), len);
567 return;
568 }
569
570 if (incoming_data == 1) {
571 if (memcmp(msg->l3h, test_data1->l3h, len) != 0) {
572 FAIL("Comparing the data failed: %d\n", incoming_data);
573 incoming_state = 0;
574 printf("Got: %s\n", hexdump(msg->l3h, len));
575 printf("Wanted: %s\n", hexdump(test_data1->l3h, len));
576
577 }
578 } else if (incoming_data == 2) {
579 if (memcmp(msg->l3h, test_data2->l3h, len) != 0) {
580 FAIL("Comparing the data failed: %d\n", incoming_data);
581 incoming_state = 0;
582 printf("Got: %s\n", hexdump(msg->l3h, len));
583 printf("Wanted: %s\n", hexdump(test_data2->l3h, len));
584 }
585 }
586
587 /* sending out data */
588 if (incoming_data == 2) {
589 printf("\tReturning data3\n");
590 sccp_connection_write(conn, test_data3);
591 }
592}
593
594static int sccp_conn_accept(struct sccp_connection *conn, void *ctx)
595{
596 printf("\taccept: %p\n", conn);
597 conn->state_cb = sccp_conn_in_state;
598 conn->data_cb = sccp_conn_in_data;
599
600 if (current_con_test->refuse)
601 return -1;
602
603 incoming_con = conn;
604 return 0;
605}
606
607/* callbacks for the outgoing side */
608static void sccp_conn_out_state(struct sccp_connection *conn, int old_state)
609{
610 printf("\toutgoing: %p %d -> %d\n", conn, old_state, conn->connection_state);
611
612 if (conn->connection_state >= SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
613 if (conn == outgoing_con) {
614 sccp_connection_free(conn);
615 outgoing_con = NULL;
616 }
617 }
618}
619
620static void sccp_conn_out_data(struct sccp_connection *conn, struct msgb *msg, unsigned int len)
621{
622 ++outgoing_data;
623 printf("\toutgoing data: %p %d\n", conn, len);
624
625 if (len != 4)
626 FAIL("Length of packet is wrong: %u %u\n", msgb_l3len(msg), len);
627
628 if (outgoing_data == 1) {
629 if (memcmp(msg->l3h, test_data3->l3h, len) != 0) {
630 FAIL("Comparing the data failed\n");
631 outgoing_state = 0;
632 }
633 }
634}
635
636static void do_test_sccp_connection(const struct connection_test *test)
637{
638 int ret;
639
640 current_con_test = test;
641 outgoing_con = incoming_con = 0;
642
643 outgoing_con = sccp_connection_socket();
644 if (!outgoing_con) {
645 FAIL("Connection is NULL\n");
646 return;
647 }
648
649 outgoing_con->state_cb = sccp_conn_out_state;
650 outgoing_con->data_cb = sccp_conn_out_data;
651 outgoing_data = incoming_data = 0;
652 incoming_state = outgoing_state = 1;
653
654 /* start testing */
655 if (test->with_data) {
656 if (sccp_connection_connect(outgoing_con, &sccp_ssn_bssap, test_data1) != 0)
657 FAIL("Binding failed\n");
658 } else {
659 ++incoming_data;
660 if (sccp_connection_connect(outgoing_con, &sccp_ssn_bssap, NULL) != 0)
661 FAIL("Binding failed\n");
662 }
663
664 if (test->refuse) {
665 if (outgoing_con)
666 FAIL("Outgoing connection should have been refused.\n");
667 } else {
668 if (!incoming_con)
669 FAIL("Creating incoming didn't work.\n");
670
671 printf("\tWriting test data2\n");
672 sccp_connection_write(outgoing_con, test_data2);
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100673 sccp_connection_send_it(outgoing_con);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200674
675 /* closing connection */
676 if (test->close_side == 0)
677 ret = sccp_connection_close(outgoing_con, 0);
678 else
679 ret = sccp_connection_close(incoming_con, 0);
680
681 if (ret != 0)
682 FAIL("Closing the connection failed\n");
683 }
684
685 /* outgoing should be gone now */
686 if (outgoing_con)
687 FAIL("Outgoing connection was not properly closed\n");
688
689 if (incoming_con)
690 FAIL("Incoming connection was not propery closed.\n");
691
692 if (test->refuse == 0) {
693 if (outgoing_data != 1 || incoming_data != 2) {
694 FAIL("Data sending failed: %d/%d %d/%d\n",
695 outgoing_data, 1,
696 incoming_data, 2);
697 }
698 }
699
700 if (!incoming_state || !outgoing_state)
701 FAIL("Failure with the state transition. %d %d\n",
702 outgoing_state, incoming_state);
703}
704
705static void test_sccp_connection(void)
706{
707 sccp_system_init(sccp_write_loop, NULL);
708 sccp_set_read(NULL, NULL, NULL);
709 sccp_connection_set_incoming(&sccp_ssn_bssap, sccp_conn_accept, NULL);
710
711 test_data1 = msgb_alloc(4, "data1");
712 test_data1->l3h = msgb_put(test_data1, 4);
713 *((unsigned int*)test_data1->l3h) = 0x23421122;
714
715 test_data2 = msgb_alloc(4, "data2");
716 test_data2->l3h = msgb_put(test_data2, 4);
717 *((unsigned int*)test_data2->l3h) = 0x42232211;
718
719 test_data3 = msgb_alloc(4, "data3");
720 test_data3->l3h = msgb_put(test_data3, 4);
721 *((unsigned int*)test_data3->l3h) = 0x2323ff55;
722
723
724 for (current_test = 0; current_test < ARRAY_SIZE(connection_tests); ++current_test) {
725 printf("Testing %d refuse: %d with_data: %d\n",
726 current_test, connection_tests[current_test].refuse,
727 connection_tests[current_test].with_data);
728 do_test_sccp_connection(&connection_tests[current_test]);
729 }
730
731 msgb_free(test_data1);
732 msgb_free(test_data2);
733 msgb_free(test_data3);
734}
735
736/* invalid input */
737static void test_sccp_system_crash(void)
738{
739 printf("trying to provoke a crash with invalid input\n");
740 sccp_set_read(&sccp_ssn_bssap, sccp_read_cb, NULL);
741 sccp_connection_set_incoming(&sccp_ssn_bssap, sccp_accept_cb, NULL);
742
743 for (current_test = 0; current_test < ARRAY_SIZE(test_data); ++current_test) {
744 int original_length = test_data[current_test].length;
745 int length = original_length + 2;
746 int i;
747
748 printf("Testing packet: %d\n", current_test);
749
750 for (i = length; i >= 0; --i) {
751 unsigned int length = MIN(test_data[current_test].length, i);
752 struct msgb *msg = msgb_alloc_headroom(length + 2, 2, __func__);
753 msg->l2h = msgb_put(msg, length);
754 memcpy(msg->l2h, test_data[current_test].data, length);
755 sccp_system_incoming(msg);
756 msgb_free(msg);
757 }
758 }
759
760 printf("survived\n");
761}
762
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +0100763static void test_sccp_parsing(void)
764{
765 for (current_test = 0; current_test < ARRAY_SIZE(parse_result); ++current_test) {
766 struct msgb *msg;
767 struct sccp_parse_result result;
768
769 msg = msgb_alloc_headroom(1024, 128, "parse-test");
770 msgb_put(msg, 1);
771 msg->l2h = msgb_put(msg, parse_result[current_test].input_len);
772 memcpy(msg->l2h, parse_result[current_test].input, msgb_l2len(msg));
773
774 memset(&result, 0, sizeof(result));
775 if (sccp_parse_header(msg, &result) != 0) {
776 fprintf(stderr, "Failed to parse test: %d\n", current_test);
777 } else {
778 if (parse_result[current_test].wanted_len != result.data_len) {
779 fprintf(stderr, "Unexpected data length.\n");
780 abort();
781 }
782
783 if (parse_result[current_test].has_src_ref) {
784 if (memcmp(result.source_local_reference,
785 &parse_result[current_test].src_ref,
786 sizeof(struct sccp_source_reference)) != 0) {
787 fprintf(stderr, "SRC REF did not match\n");
788 abort();
789 }
790 }
791
792 if (parse_result[current_test].has_dst_ref) {
793 if (memcmp(result.destination_local_reference,
794 &parse_result[current_test].dst_ref,
795 sizeof(struct sccp_source_reference)) != 0) {
796 fprintf(stderr, "DST REF did not match\n");
797 abort();
798 }
799 }
800
801 if (parse_result[current_test].src_ssn != -1) {
802 fprintf(stderr, "Not implemented.\n");
803 abort();
804 }
805
806 if (parse_result[current_test].dst_ssn != -1) {
807 fprintf(stderr, "Not implemented.\n");
808 abort();
809 }
810 }
811
812 msgb_free(msg);
813 }
814}
815
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200816
817int main(int argc, char **argv)
818{
819 test_sccp_system();
820 test_sccp_send_udt();
821 test_sccp_udt_communication();
822 test_sccp_connection();
823 test_sccp_system_crash();
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +0100824 test_sccp_parsing();
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200825 return 0;
826}
Holger Hans Peter Freyther5dbd71e2009-12-23 05:13:48 +0100827
828void db_store_counter() {}