unit-tests: Provide a wrapper around bus_t::add_listener and unregister them during cleanup

In case listeners on the stack are triggered while cleaning up after a
test failed (e.g. via ike_sa_manager_t::flush) remaining listeners defined on
the stack would cause a segmentation fault.
This commit is contained in:
Tobias Brunner
2016-06-17 18:48:04 +02:00
parent b7fac1d96e
commit 5d10ef316d
2 changed files with 32 additions and 0 deletions
@@ -18,6 +18,7 @@
#include "mock_ipsec.h"
#include "mock_nonce_gen.h"
#include <collections/array.h>
#include <credentials/sets/mem_cred.h>
typedef struct private_exchange_test_helper_t private_exchange_test_helper_t;
@@ -42,6 +43,11 @@ struct private_exchange_test_helper_t {
* IKE_SA SPI counter
*/
refcount_t ike_spi;
/**
* List of registered listeners
*/
array_t *listeners;
};
/**
@@ -240,6 +246,13 @@ METHOD(exchange_test_helper_t, establish_sa, void,
DESTROY_IF(backend.ike_cfg);
}
METHOD(exchange_test_helper_t, add_listener, void,
private_exchange_test_helper_t *this, listener_t *listener)
{
array_insert_create(&this->listeners, ARRAY_TAIL, listener);
charon->bus->add_listener(charon->bus, listener);
}
/**
* Enable logging in charon as requested
*/
@@ -291,6 +304,7 @@ void exchange_test_helper_init(char *plugins)
.sender = mock_sender_create(),
.establish_sa = _establish_sa,
.process_message = _process_message,
.add_listener = _add_listener,
},
.creds = mem_cred_create(),
);
@@ -327,13 +341,19 @@ void exchange_test_helper_init(char *plugins)
void exchange_test_helper_deinit()
{
private_exchange_test_helper_t *this;
listener_t *listener;
this = (private_exchange_test_helper_t*)exchange_test_helper;
while (array_remove(this->listeners, ARRAY_HEAD, &listener))
{
charon->bus->remove_listener(charon->bus, listener);
}
lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
this->creds->destroy(this->creds);
/* can't let charon do it as it happens too late */
charon->sender->destroy(charon->sender);
charon->sender = NULL;
array_destroy(this->listeners);
free(this);
}
@@ -67,6 +67,18 @@ struct exchange_test_helper_t {
*/
void (*process_message)(exchange_test_helper_t *this, ike_sa_t *sa,
message_t *message);
/**
* Register a listener with the bus.
*
* Don't use bus_t::add_listener() directly for listeners on the stack
* as that could lead to invalid listeners registered when hooks are
* triggered during cleanup if a test case fails. All of the listeners
* added this way are unregistered with the bus before cleaning up.
*
* @param listener listener to add to the bus
*/
void (*add_listener)(exchange_test_helper_t *this, listener_t *listener);
};
struct exchange_test_sa_conf_t {