unit-tests: Add mock sender_t implementation for unit testing

This allows to retrieve packets sent by an IKE_SA and pass it to another
IKE_SA directly via process_message().
This commit is contained in:
Tobias Brunner
2016-06-17 18:48:01 +02:00
parent b104b2a737
commit 87539617f1
4 changed files with 153 additions and 0 deletions
+1
View File
@@ -6,6 +6,7 @@ libcharon_tests_SOURCES = \
suites/test_ike_cfg.c \
suites/test_mem_pool.c \
suites/test_message_chapoly.c \
utils/mock_sender.h utils/mock_sender.c \
libcharon_tests.h libcharon_tests.c
libcharon_tests_CFLAGS = \
+11
View File
@@ -1,4 +1,7 @@
/*
* Copyright (C) 2014-2016 Tobias Brunner
* HSR Hochschule fuer Technik Rapperswil
*
* Copyright (C) 2014 Martin Willi
* Copyright (C) 2014 revosec AG
*
@@ -13,6 +16,14 @@
* for more details.
*/
/**
* @defgroup libcharon-tests tests
* @ingroup libcharon
*
* @defgroup test_utils_c test_utils
* @ingroup libcharon-tests
*/
TEST_SUITE(ike_cfg_suite_create)
TEST_SUITE(mem_pool_suite_create)
TEST_SUITE_DEPEND(message_chapoly_suite_create, AEAD, ENCR_CHACHA20_POLY1305, 32)
+85
View File
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2016 Tobias Brunner
* HSR 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 "mock_sender.h"
#include <collections/linked_list.h>
typedef struct private_mock_sender_t private_mock_sender_t;
/**
* Private data
*/
struct private_mock_sender_t {
/**
* Public interface
*/
mock_sender_t public;
/**
* Packet queue, as message_t*
*/
linked_list_t *queue;
};
METHOD(sender_t, send_, void,
private_mock_sender_t *this, packet_t *packet)
{
message_t *message;
message = message_create_from_packet(packet);
message->parse_header(message);
this->queue->insert_last(this->queue, message);
}
METHOD(mock_sender_t, dequeue, message_t*,
private_mock_sender_t *this)
{
message_t *message = NULL;
this->queue->remove_first(this->queue, (void**)&message);
return message;
}
METHOD(sender_t, destroy, void,
private_mock_sender_t *this)
{
this->queue->destroy_offset(this->queue, offsetof(message_t, destroy));
free(this);
}
/*
* Described in header
*/
mock_sender_t *mock_sender_create()
{
private_mock_sender_t *this;
INIT(this,
.public = {
.interface = {
.send = _send_,
.send_no_marker = (void*)nop,
.flush = (void*)nop,
.destroy = _destroy,
},
.dequeue = _dequeue,
},
.queue = linked_list_create(),
);
return &this->public;
}
+56
View File
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2016 Tobias Brunner
* HSR 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.
*/
/**
* sender_t implementation that does not pass the sent packet to a socket but
* instead provides it for immediate delivery to an ike_sa_t object.
*
* @defgroup mock_sender mock_sender
* @{ @ingroup test_utils_c
*/
#ifndef MOCK_SENDER_H_
#define MOCK_SENDER_H_
#include <encoding/message.h>
#include <network/sender.h>
typedef struct mock_sender_t mock_sender_t;
struct mock_sender_t {
/**
* Implemented interface
*/
sender_t interface;
/**
* Remove the next packet in the send queue as message_t object. The IKE
* header is already parsed (which is assumed does not fail) so it can
* directly be passed to ike_sa_t::process_message().
*
* @return message or NULL if none is queued
*/
message_t *(*dequeue)(mock_sender_t *this);
};
/**
* Creates a mock_sender_t instance.
*
* @return created object
*/
mock_sender_t *mock_sender_create();
#endif /** MOCK_SENDER_H_ @} */