- memory allocation tests removed
[strongswan.git] / Source / charon / testcases / 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 "receiver_test.h"
27
28 #include <globals.h>
29 #include <threads/receiver.h>
30 #include <network/packet.h>
31 #include <network/socket.h>
32 #include <queues/send_queue.h>
33 #include <queues/job_queue.h>
34 #include <queues/jobs/incoming_packet_job.h>
35 #include <encoding/payloads/encodings.h>
36 #include <utils/allocator.h>
37
38 /**
39 * Number of packets to send by sender-thread
40 */
41 #define NUMBER_OF_PACKETS_TO_SEND 100
42
43 /**
44 * Port to send the packets to
45 */
46 #define PORT_TO_SEND 4600
47
48 /**
49 * Destination IP Address
50 */
51 #define DESTINATION_IP "127.0.0.1"
52
53 void test_receiver(tester_t *tester)
54 {
55 int i;
56 receiver_t *receiver;
57 packet_t *packet;
58 job_t *job;
59 packet_t *received_packet;
60 receiver = receiver_create();
61
62 for (i = 0; i < NUMBER_OF_PACKETS_TO_SEND; i++)
63 {
64 packet = packet_create();
65 packet->destination = host_create(AF_INET,DESTINATION_IP,PORT_TO_SEND);
66 packet->data.ptr = allocator_alloc_thing(int);
67 packet->data.len = ( sizeof(int));
68 *((int *) (packet->data.ptr)) = i;
69 global_socket->send(global_socket,packet);
70 packet->destroy(packet);
71 }
72
73 for (i = 0; i < NUMBER_OF_PACKETS_TO_SEND; i++)
74 {
75 job = global_job_queue->get(global_job_queue);
76 tester->assert_true(tester, (job->get_type(job) == INCOMING_PACKET), "job type check");
77
78 ((incoming_packet_job_t *)(job))->get_packet((incoming_packet_job_t *)(job),&received_packet);
79 tester->assert_true(tester, (received_packet->data.len == (sizeof(int))), "received data length check");
80 tester->assert_true(tester, (i == *((int *)(received_packet->data.ptr))), "received data value check");
81 received_packet->destroy(received_packet);
82
83 job->destroy(job);
84 }
85
86 receiver->destroy(receiver);
87 }