aboutsummaryrefslogtreecommitdiffstats
path: root/Source/testing/sender_test.c
blob: 4559de0f482e25952671226f50841f5c1000198f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * @file sender_test.h
 *
 * @brief Tests for the sender_t class.
 *
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <string.h>

#include "sender_test.h"

#include <daemon.h>
#include <threads/sender.h>
#include <network/packet.h>
#include <network/socket.h>
#include <queues/send_queue.h>
#include <queues/job_queue.h>

/**
 * Number of packets to send by sender-thread
 */
#define NUMBER_OF_PACKETS_TO_SEND 50

/**
 * Port to send the packets to
 */
#define PORT_TO_SEND 4600

/**
 * Destination IP Address
 */
#define DESTINATION_IP "127.0.0.1"

void test_sender(protected_tester_t *tester)
{
	int i;
	sender_t *sender;
	packet_t *packet;
	packet_t *received_packet;
	chunk_t packet_data;
	sender = sender_create();

	for (i = 0; i < NUMBER_OF_PACKETS_TO_SEND; i++)
	{
		packet = packet_create(AF_INET);
		packet->set_destination(packet, host_create(AF_INET,DESTINATION_IP,PORT_TO_SEND));
		packet_data.len = ( sizeof(int));
		packet_data.ptr = malloc(packet_data.len);
		*((int *) (packet_data.ptr)) = i;
		packet->set_data(packet, packet_data);
		charon->send_queue->add(charon->send_queue,packet);
	}

	for (i = 0; i < NUMBER_OF_PACKETS_TO_SEND; i++)
	{
		charon->socket->receive(charon->socket,&received_packet);
		packet_data = received_packet->get_data(received_packet);
		tester->assert_true(tester, (packet_data.len == (sizeof(int))), "received data length check");
		tester->assert_true(tester, (i == *((int *)(packet_data.ptr))), "received data value check");
		received_packet->destroy(received_packet);
	}

	sender->destroy(sender);
}