blob: 0c2adc83fa338ad21626b826afa45949b02d1237 [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
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100357void sccp_write_cb(struct msgb *data, void *ctx)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200358{
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");
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100364 goto exit;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200365 } 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);
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100377 goto exit;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200378 }
379 }
380
381 write_called = 1;
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100382
383exit:
384 msgb_free(data);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200385}
386
387void sccp_c_read(struct sccp_connection *connection, struct msgb *msgb, unsigned int len)
388{
389 sccp_read_cb(msgb, len, connection->data_ctx);
390}
391
392void sccp_c_state(struct sccp_connection *connection, int old_state)
393{
394 if (connection->connection_state >= SCCP_CONNECTION_STATE_RELEASE_COMPLETE)
395 sccp_connection_free(connection);
396}
397
398int sccp_accept_cb(struct sccp_connection *connection, void *user_data)
399{
400 called = 1;
401 unsigned int ref = 0;
402 ref |= connection->destination_local_reference.octet1 << 24;
403 ref |= connection->destination_local_reference.octet2 << 16;
404 ref |= connection->destination_local_reference.octet3 << 8;
405 ref = ntohl(ref);
406
407 connection->data_cb = sccp_c_read;
408 connection->state_cb = sccp_c_state;
409
410 /* accept this */
411 return 0;
412}
413
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100414static void sccp_udt_write_cb(struct msgb *data, void *context)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200415{
416 const u_int8_t *got, *wanted;
417 int i;
418
419 write_called = 1;
420
421 if (send_data[current_test].length != msgb_l2len(data)) {
422 FAIL("Size does not match. Got: %d Wanted: %d\n",
423 msgb_l2len(data), send_data[current_test].length);
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100424 goto exit;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200425 }
426
427 got = &data->l2h[0];
428 wanted = send_data[current_test].data;
429
430 for (i = 0; i < msgb_l2len(data); ++i) {
431 if (got[i] != wanted[i]) {
432 FAIL("Failed to compare byte. Got: 0x%x Wanted: 0x%x at %d\n",
433 got[i], wanted[i], i);
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100434 goto exit;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200435 }
436 }
437
438 matched = 1;
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100439
440exit:
441 msgb_free(data);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200442}
443
444static void test_sccp_system(void)
445{
446 sccp_system_init(sccp_write_cb, NULL);
447 sccp_set_read(&sccp_ssn_bssap, sccp_read_cb, NULL);
448 sccp_connection_set_incoming(&sccp_ssn_bssap, sccp_accept_cb, NULL);
449
450 for (current_test = 0; current_test < ARRAY_SIZE(test_data); ++current_test) {
451 unsigned int length = test_data[current_test].length;
452 struct msgb *msg = msgb_alloc_headroom(length + 2, 2, __func__);
453 msg->l2h = msgb_put(msg, length);
454 memcpy(msg->l2h, test_data[current_test].data, length);
455
456 called = matched = write_called = 0;
457 printf("Testing packet: %d\n", current_test);
458 sccp_system_incoming(msg);
459
460 if (!called || !matched || (test_data[current_test].write != write_called))
461 FAIL("current test: %d called: %d matched: %d write: %d\n",
462 current_test, called, matched, write_called);
463
464 msgb_free(msg);
465 }
466}
467
468/* test sending of udt */
469static void test_sccp_send_udt(void)
470{
471 sccp_system_init(sccp_udt_write_cb, NULL);
472 sccp_set_read(NULL, NULL, NULL);
473 sccp_connection_set_incoming(NULL, NULL, NULL);
474
475 for (current_test = 0; current_test < ARRAY_SIZE(send_data); ++current_test) {
476 const struct test_data *test = &send_data[current_test];
477
478 struct msgb *msg = msgb_alloc(test->payload_length, __func__);
479 msg->l3h = msgb_put(msg, test->payload_length);
480 memcpy(msg->l3h, test->data + test->payload_start, test->payload_length);
481
482 matched = write_called = 0;
483 printf("Testing packet: %d\n", current_test);
484 sccp_write(msg, &sccp_ssn_bssap, &sccp_ssn_bssap, 0);
485
486 if (!matched || !write_called)
487 FAIL("current test: %d matched: %d write: %d\n",
488 current_test, matched, write_called);
489
490 msgb_free(msg);
491 }
492}
493
494/* send udt from one end to another */
495static unsigned int test_value = 0x2442;
496static int sccp_udt_read(struct msgb *data, unsigned int len, void *context)
497{
498 unsigned int *val;
499
500 if (len != 4) {
501 FAIL("Wrong size: %d\n", msgb_l3len(data));
502 return -1;
503 }
504
505 val = (unsigned int*)data->l3h;
506 matched = test_value == *val;
507
508 return 0;
509}
510
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100511static void sccp_write_loop(struct msgb *data, void *context)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200512{
513 /* send it back to us */
514 sccp_system_incoming(data);
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100515 msgb_free(data);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200516}
517
518static void test_sccp_udt_communication(void)
519{
520 struct msgb *data;
521 unsigned int *val;
522
523 sccp_system_init(sccp_write_loop, NULL);
524 sccp_set_read(&sccp_ssn_bssap, sccp_udt_read, NULL);
525 sccp_connection_set_incoming(NULL, NULL, NULL);
526
527
528 data = msgb_alloc(4, "test data");
529 data->l3h = &data->data[0];
530 val = (unsigned int *)msgb_put(data, 4);
531 *val = test_value;
532
533 matched = 0;
534 sccp_write(data, &sccp_ssn_bssap, &sccp_ssn_bssap, 0);
535
536 if (!matched)
537 FAIL("Talking with us didn't work\n");
538
539 msgb_free(data);
540}
541
542
543/* connection testing... open, send, close */
544static const struct connection_test *current_con_test;
545static struct sccp_connection *outgoing_con;
546static struct sccp_connection *incoming_con;
547static int outgoing_data, incoming_data, incoming_state, outgoing_state;
548
549static struct msgb *test_data1, *test_data2, *test_data3;
550
551static void sccp_conn_in_state(struct sccp_connection *conn, int old_state)
552{
553 printf("\tincome: %d -> %d\n", old_state, conn->connection_state);
554 if (conn->connection_state >= SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
555 if (conn == incoming_con) {
556 sccp_connection_free(conn);
557 incoming_con = NULL;
558 }
559 }
560}
561
562static void sccp_conn_in_data(struct sccp_connection *conn, struct msgb *msg, unsigned int len)
563{
564 /* compare the data */
565 ++incoming_data;
566 printf("\tincoming data: %d\n", len);
567
568 /* compare the data */
569 if (len != 4) {
570 FAIL("Length of packet is wrong: %u %u\n", msgb_l3len(msg), len);
571 return;
572 }
573
574 if (incoming_data == 1) {
575 if (memcmp(msg->l3h, test_data1->l3h, len) != 0) {
576 FAIL("Comparing the data failed: %d\n", incoming_data);
577 incoming_state = 0;
578 printf("Got: %s\n", hexdump(msg->l3h, len));
579 printf("Wanted: %s\n", hexdump(test_data1->l3h, len));
580
581 }
582 } else if (incoming_data == 2) {
583 if (memcmp(msg->l3h, test_data2->l3h, len) != 0) {
584 FAIL("Comparing the data failed: %d\n", incoming_data);
585 incoming_state = 0;
586 printf("Got: %s\n", hexdump(msg->l3h, len));
587 printf("Wanted: %s\n", hexdump(test_data2->l3h, len));
588 }
589 }
590
591 /* sending out data */
592 if (incoming_data == 2) {
593 printf("\tReturning data3\n");
594 sccp_connection_write(conn, test_data3);
595 }
596}
597
598static int sccp_conn_accept(struct sccp_connection *conn, void *ctx)
599{
600 printf("\taccept: %p\n", conn);
601 conn->state_cb = sccp_conn_in_state;
602 conn->data_cb = sccp_conn_in_data;
603
604 if (current_con_test->refuse)
605 return -1;
606
607 incoming_con = conn;
608 return 0;
609}
610
611/* callbacks for the outgoing side */
612static void sccp_conn_out_state(struct sccp_connection *conn, int old_state)
613{
614 printf("\toutgoing: %p %d -> %d\n", conn, old_state, conn->connection_state);
615
616 if (conn->connection_state >= SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
617 if (conn == outgoing_con) {
618 sccp_connection_free(conn);
619 outgoing_con = NULL;
620 }
621 }
622}
623
624static void sccp_conn_out_data(struct sccp_connection *conn, struct msgb *msg, unsigned int len)
625{
626 ++outgoing_data;
627 printf("\toutgoing data: %p %d\n", conn, len);
628
629 if (len != 4)
630 FAIL("Length of packet is wrong: %u %u\n", msgb_l3len(msg), len);
631
632 if (outgoing_data == 1) {
633 if (memcmp(msg->l3h, test_data3->l3h, len) != 0) {
634 FAIL("Comparing the data failed\n");
635 outgoing_state = 0;
636 }
637 }
638}
639
640static void do_test_sccp_connection(const struct connection_test *test)
641{
642 int ret;
643
644 current_con_test = test;
645 outgoing_con = incoming_con = 0;
646
647 outgoing_con = sccp_connection_socket();
648 if (!outgoing_con) {
649 FAIL("Connection is NULL\n");
650 return;
651 }
652
653 outgoing_con->state_cb = sccp_conn_out_state;
654 outgoing_con->data_cb = sccp_conn_out_data;
655 outgoing_data = incoming_data = 0;
656 incoming_state = outgoing_state = 1;
657
658 /* start testing */
659 if (test->with_data) {
660 if (sccp_connection_connect(outgoing_con, &sccp_ssn_bssap, test_data1) != 0)
661 FAIL("Binding failed\n");
662 } else {
663 ++incoming_data;
664 if (sccp_connection_connect(outgoing_con, &sccp_ssn_bssap, NULL) != 0)
665 FAIL("Binding failed\n");
666 }
667
668 if (test->refuse) {
669 if (outgoing_con)
670 FAIL("Outgoing connection should have been refused.\n");
671 } else {
672 if (!incoming_con)
673 FAIL("Creating incoming didn't work.\n");
674
675 printf("\tWriting test data2\n");
676 sccp_connection_write(outgoing_con, test_data2);
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100677 sccp_connection_send_it(outgoing_con);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200678
679 /* closing connection */
680 if (test->close_side == 0)
681 ret = sccp_connection_close(outgoing_con, 0);
682 else
683 ret = sccp_connection_close(incoming_con, 0);
684
685 if (ret != 0)
686 FAIL("Closing the connection failed\n");
687 }
688
689 /* outgoing should be gone now */
690 if (outgoing_con)
691 FAIL("Outgoing connection was not properly closed\n");
692
693 if (incoming_con)
694 FAIL("Incoming connection was not propery closed.\n");
695
696 if (test->refuse == 0) {
697 if (outgoing_data != 1 || incoming_data != 2) {
698 FAIL("Data sending failed: %d/%d %d/%d\n",
699 outgoing_data, 1,
700 incoming_data, 2);
701 }
702 }
703
704 if (!incoming_state || !outgoing_state)
705 FAIL("Failure with the state transition. %d %d\n",
706 outgoing_state, incoming_state);
707}
708
709static void test_sccp_connection(void)
710{
711 sccp_system_init(sccp_write_loop, NULL);
712 sccp_set_read(NULL, NULL, NULL);
713 sccp_connection_set_incoming(&sccp_ssn_bssap, sccp_conn_accept, NULL);
714
715 test_data1 = msgb_alloc(4, "data1");
716 test_data1->l3h = msgb_put(test_data1, 4);
717 *((unsigned int*)test_data1->l3h) = 0x23421122;
718
719 test_data2 = msgb_alloc(4, "data2");
720 test_data2->l3h = msgb_put(test_data2, 4);
721 *((unsigned int*)test_data2->l3h) = 0x42232211;
722
723 test_data3 = msgb_alloc(4, "data3");
724 test_data3->l3h = msgb_put(test_data3, 4);
725 *((unsigned int*)test_data3->l3h) = 0x2323ff55;
726
727
728 for (current_test = 0; current_test < ARRAY_SIZE(connection_tests); ++current_test) {
729 printf("Testing %d refuse: %d with_data: %d\n",
730 current_test, connection_tests[current_test].refuse,
731 connection_tests[current_test].with_data);
732 do_test_sccp_connection(&connection_tests[current_test]);
733 }
734
735 msgb_free(test_data1);
736 msgb_free(test_data2);
737 msgb_free(test_data3);
738}
739
740/* invalid input */
741static void test_sccp_system_crash(void)
742{
743 printf("trying to provoke a crash with invalid input\n");
744 sccp_set_read(&sccp_ssn_bssap, sccp_read_cb, NULL);
745 sccp_connection_set_incoming(&sccp_ssn_bssap, sccp_accept_cb, NULL);
746
747 for (current_test = 0; current_test < ARRAY_SIZE(test_data); ++current_test) {
748 int original_length = test_data[current_test].length;
749 int length = original_length + 2;
750 int i;
751
752 printf("Testing packet: %d\n", current_test);
753
754 for (i = length; i >= 0; --i) {
755 unsigned int length = MIN(test_data[current_test].length, i);
756 struct msgb *msg = msgb_alloc_headroom(length + 2, 2, __func__);
757 msg->l2h = msgb_put(msg, length);
758 memcpy(msg->l2h, test_data[current_test].data, length);
759 sccp_system_incoming(msg);
760 msgb_free(msg);
761 }
762 }
763
764 printf("survived\n");
765}
766
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +0100767static void test_sccp_parsing(void)
768{
769 for (current_test = 0; current_test < ARRAY_SIZE(parse_result); ++current_test) {
770 struct msgb *msg;
771 struct sccp_parse_result result;
772
773 msg = msgb_alloc_headroom(1024, 128, "parse-test");
774 msgb_put(msg, 1);
775 msg->l2h = msgb_put(msg, parse_result[current_test].input_len);
776 memcpy(msg->l2h, parse_result[current_test].input, msgb_l2len(msg));
777
778 memset(&result, 0, sizeof(result));
779 if (sccp_parse_header(msg, &result) != 0) {
780 fprintf(stderr, "Failed to parse test: %d\n", current_test);
781 } else {
782 if (parse_result[current_test].wanted_len != result.data_len) {
783 fprintf(stderr, "Unexpected data length.\n");
784 abort();
785 }
786
787 if (parse_result[current_test].has_src_ref) {
788 if (memcmp(result.source_local_reference,
789 &parse_result[current_test].src_ref,
790 sizeof(struct sccp_source_reference)) != 0) {
791 fprintf(stderr, "SRC REF did not match\n");
792 abort();
793 }
794 }
795
796 if (parse_result[current_test].has_dst_ref) {
797 if (memcmp(result.destination_local_reference,
798 &parse_result[current_test].dst_ref,
799 sizeof(struct sccp_source_reference)) != 0) {
800 fprintf(stderr, "DST REF did not match\n");
801 abort();
802 }
803 }
804
805 if (parse_result[current_test].src_ssn != -1) {
806 fprintf(stderr, "Not implemented.\n");
807 abort();
808 }
809
810 if (parse_result[current_test].dst_ssn != -1) {
811 fprintf(stderr, "Not implemented.\n");
812 abort();
813 }
814 }
815
816 msgb_free(msg);
817 }
818}
819
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200820
821int main(int argc, char **argv)
822{
823 test_sccp_system();
824 test_sccp_send_udt();
825 test_sccp_udt_communication();
826 test_sccp_connection();
827 test_sccp_system_crash();
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +0100828 test_sccp_parsing();
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200829 return 0;
830}
Holger Hans Peter Freyther5dbd71e2009-12-23 05:13:48 +0100831
832void db_store_counter() {}