unit-tests: Add assert to check for installed IPsec SAs

This commit is contained in:
Tobias Brunner 2017-03-08 15:46:39 +01:00
parent 2b581b59f0
commit 72655fe411
2 changed files with 115 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 Tobias Brunner
* Copyright (C) 2016-2017 Tobias Brunner
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
@ -18,6 +18,7 @@
#include <test_suite.h>
#include "exchange_test_asserts.h"
#include "mock_ipsec.h"
/*
* Described in header
@ -180,3 +181,57 @@ bool exchange_test_asserts_message(listener_t *listener, ike_sa_t *ike_sa,
}
return TRUE;
}
/**
* Compare two SPIs
*/
static int spis_cmp(const void *a, const void *b)
{
return *(const uint32_t*)a - *(const uint32_t*)b;
}
/**
* Compare two SPIs to sort them
*/
static int spis_sort(const void *a, const void *b, void *data)
{
return spis_cmp(a, b);
}
/*
* Described in header
*/
void exchange_test_asserts_ipsec_sas(ipsec_sas_assert_t *sas)
{
enumerator_t *enumerator;
array_t *spis;
ike_sa_t *ike_sa;
uint32_t spi;
int i;
spis = array_create(sizeof(uint32_t), 0);
for (i = 0; i < sas->count; i++)
{
array_insert(spis, ARRAY_TAIL, &sas->spis[i]);
}
array_sort(spis, spis_sort, NULL);
enumerator = mock_ipsec_create_sa_enumerator();
while (enumerator->enumerate(enumerator, &ike_sa, &spi))
{
if (ike_sa == sas->ike_sa)
{
i = array_bsearch(spis, &spi, spis_cmp, NULL);
assert_listener_msg(i != -1, sas, "unexpected IPsec SA %.8x", spi);
array_remove(spis, i, NULL);
}
}
enumerator->destroy(enumerator);
for (i = 0; i < array_count(spis); i++)
{
array_get(spis, i, &spi);
assert_listener_msg(!spi, sas, "expected IPsec SA %.8x not found", spi);
}
array_destroy(spis);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 Tobias Brunner
* Copyright (C) 2016-2017 Tobias Brunner
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
@ -14,7 +14,7 @@
*/
/**
* Special assertions using listener_t.
* Special assertions using listener_t etc.
*
* @defgroup exchange_test_asserts exchange_test_asserts
* @{ @ingroup test_utils_c
@ -28,6 +28,7 @@
typedef struct listener_hook_assert_t listener_hook_assert_t;
typedef struct listener_message_assert_t listener_message_assert_t;
typedef struct listener_message_rule_t listener_message_rule_t;
typedef struct ipsec_sas_assert_t ipsec_sas_assert_t;
struct listener_hook_assert_t {
@ -340,4 +341,60 @@ bool exchange_test_asserts_message(listener_t *this, ike_sa_t *ike_sa,
exchange_test_helper->add_listener(exchange_test_helper, &_listener.listener); \
})
/**
* Data used to check IPsec SAs
*/
struct ipsec_sas_assert_t {
/**
* Original source file
*/
const char *file;
/**
* Source line
*/
int line;
/**
* IKE_SA that installed the IPsec SAs
*/
ike_sa_t *ike_sa;
/**
* SPIs to check
*/
uint32_t *spis;
/**
* Number of SPIs for IPsec SAs to check
*/
int count;
};
/**
* Assert that all given IPsec SAs (and only these) are installed for the given
* IKE_SA.
*/
void exchange_test_asserts_ipsec_sas(ipsec_sas_assert_t *sas);
/**
* Assert that the IPsec SAs with the given SPIs (and none other) are currently
* installed by the given IKE_SA.
*
* @param sa IKE_SA
* @param ... list of SPIs
*/
#define assert_ipsec_sas_installed(sa, ...) ({ \
uint32_t _spis[] = { __VA_ARGS__ }; \
ipsec_sas_assert_t _sas_assert = { \
.file = __FILE__, \
.line = __LINE__, \
.ike_sa = sa, \
.spis = _spis, \
.count = countof(_spis), \
}; \
exchange_test_asserts_ipsec_sas(&_sas_assert); \
})
#endif /** EXCHANGE_TEST_ASSERTS_H_ @}*/