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