- receiver-test written
[strongswan.git] / Source / charon / tests / receiver_test.c
1 /**
2 * @file receiver_test.c
3 *
4 * @brief Tests to test the Receiver (type receiver_t)
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "sender_test.h"
27 #include "../globals.h"
28 #include "../receiver.h"
29 #include "../packet.h"
30 #include "../socket.h"
31 #include "../send_queue.h"
32 #include "../job_queue.h"
33
34 /**
35 * Number of packets to send by sender-thread
36 */
37 #define NUMBER_OF_PACKETS_TO_SEND 100
38
39 /**
40 * Port to send the packets to
41 */
42 #define PORT_TO_SEND 4600
43
44 /**
45 * Destination IP Address
46 */
47 #define DESTINATION_IP "127.0.0.1"
48
49 void test_receiver(tester_t *tester)
50 {
51 int i;
52 receiver_t *receiver;
53 packet_t *packet;
54 job_t *job;
55 packet_t *received_packet;
56 receiver = receiver_create();
57
58 for (i = 0; i < NUMBER_OF_PACKETS_TO_SEND; i++)
59 {
60 packet = packet_create(AF_INET);
61 packet->set_destination(packet,DESTINATION_IP,PORT_TO_SEND);
62 packet->data.ptr = alloc_thing(int, "packet data");
63 packet->data.len = ( sizeof(int));
64 *((int *) (packet->data.ptr)) = i;
65 global_socket->send(global_socket,packet);
66 packet->destroy(packet);
67 }
68
69 for (i = 0; i < NUMBER_OF_PACKETS_TO_SEND; i++)
70 {
71 global_job_queue->get(global_job_queue,&job);
72 tester->assert_true(tester, (job->type == INCOMING_PACKET), "job type check");
73 received_packet = (packet_t *) job->assigned_data;
74 tester->assert_true(tester, (received_packet->data.len == (sizeof(int))), "received data length check");
75 tester->assert_true(tester, (i == *((int *)(received_packet->data.ptr))), "received data value check");
76 received_packet->destroy(received_packet);
77
78 job->destroy(job);
79 }
80
81 tester->assert_true(tester, (receiver->destroy(receiver) == SUCCESS), "destroy call check");
82 }