Annotation of embedaddon/strongswan/src/libcharon/tests/utils/exchange_test_asserts.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2016-2017 Tobias Brunner
                      3:  * HSR Hochschule fuer Technik Rapperswil
                      4:  *
                      5:  * This program is free software; you can redistribute it and/or modify it
                      6:  * under the terms of the GNU General Public License as published by the
                      7:  * Free Software Foundation; either version 2 of the License, or (at your
                      8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                      9:  *
                     10:  * This program is distributed in the hope that it will be useful, but
                     11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     13:  * for more details.
                     14:  */
                     15: 
                     16: #include <inttypes.h>
                     17: 
                     18: #include <test_suite.h>
                     19: 
                     20: #include "exchange_test_asserts.h"
                     21: #include "mock_ipsec.h"
                     22: 
                     23: /*
                     24:  * Described in header
                     25:  */
                     26: bool exchange_test_asserts_hook(listener_t *listener)
                     27: {
                     28:        listener_hook_assert_t *this = (listener_hook_assert_t*)listener;
                     29: 
                     30:        this->count++;
                     31:        return TRUE;
                     32: }
                     33: 
                     34: /*
                     35:  * Described in header
                     36:  */
                     37: bool exchange_test_asserts_ike_updown(listener_t *listener, ike_sa_t *ike_sa,
                     38:                                                                          bool up)
                     39: {
                     40:        listener_hook_assert_t *this = (listener_hook_assert_t*)listener;
                     41: 
                     42:        this->count++;
                     43:        assert_listener_msg(this->up == up, this, "IKE_SA not '%s'",
                     44:                                                this->up ? "up" : "down");
                     45:        return TRUE;
                     46: }
                     47: 
                     48: /*
                     49:  * Described in header
                     50:  */
                     51: bool exchange_test_asserts_child_updown(listener_t *listener, ike_sa_t *ike_sa,
                     52:                                                                                child_sa_t *child_sa, bool up)
                     53: {
                     54:        listener_hook_assert_t *this = (listener_hook_assert_t*)listener;
                     55: 
                     56:        this->count++;
                     57:        assert_listener_msg(this->up == up, this, "CHILD_SA not '%s'",
                     58:                                                this->up ? "up" : "down");
                     59:        return TRUE;
                     60: }
                     61: 
                     62: /*
                     63:  * Described in header
                     64:  */
                     65: bool exchange_test_asserts_ike_rekey(listener_t *listener, ike_sa_t *old,
                     66:                                                                         ike_sa_t *new)
                     67: {
                     68:        listener_hook_assert_t *this = (listener_hook_assert_t*)listener;
                     69:        ike_sa_id_t *id;
                     70:        uint64_t spi;
                     71: 
                     72:        this->count++;
                     73:        id = old->get_id(old);
                     74:        spi = id->get_initiator_spi(id);
                     75:        assert_listener_msg(this->spi_old == spi, this, "unexpected old IKE_SA "
                     76:                                                "%.16"PRIx64"_i instead of %.16"PRIx64"_i",
                     77:                                                be64toh(spi), be64toh(this->spi_old));
                     78:        id = new->get_id(new);
                     79:        spi = id->get_initiator_spi(id);
                     80:        assert_listener_msg(this->spi_new == spi, this, "unexpected new IKE_SA "
                     81:                                                "%.16"PRIx64"_i instead of %.16"PRIx64"_i",
                     82:                                                be64toh(spi), be64toh(this->spi_new));
                     83:        return TRUE;
                     84: }
                     85: 
                     86: /*
                     87:  * Described in header
                     88:  */
                     89: bool exchange_test_asserts_child_rekey(listener_t *listener, ike_sa_t *ike_sa,
                     90:                                                                           child_sa_t *old, child_sa_t *new)
                     91: {
                     92:        listener_hook_assert_t *this = (listener_hook_assert_t*)listener;
                     93:        uint32_t spi, expected;
                     94: 
                     95:        this->count++;
                     96:        spi = old->get_spi(old, TRUE);
                     97:        expected = this->spi_old;
                     98:        assert_listener_msg(expected == spi, this, "unexpected old CHILD_SA %.8x "
                     99:                                                "instead of %.8x", spi, expected);
                    100:        spi = new->get_spi(new, TRUE);
                    101:        expected = this->spi_new;
                    102:        assert_listener_msg(expected == spi, this, "unexpected new CHILD_SA %.8x "
                    103:                                                "instead of %.8x", spi, expected);
                    104:        return TRUE;
                    105: }
                    106: 
                    107: /**
                    108:  * Assert a given message rule
                    109:  */
                    110: static void assert_message_rule(listener_message_assert_t *this, message_t *msg,
                    111:                                                                listener_message_rule_t *rule)
                    112: {
                    113:        if (rule->expected)
                    114:        {
                    115:                if (rule->payload)
                    116:                {
                    117:                        assert_listener_msg(msg->get_payload(msg, rule->payload),
                    118:                                                                this, "expected payload (%N) not found",
                    119:                                                                payload_type_names, rule->payload);
                    120: 
                    121:                }
                    122:                if (rule->notify)
                    123:                {
                    124:                        assert_listener_msg(msg->get_notify(msg, rule->notify),
                    125:                                                                this, "expected notify payload (%N) not found",
                    126:                                                                notify_type_names, rule->notify);
                    127:                }
                    128:        }
                    129:        else
                    130:        {
                    131:                if (rule->payload)
                    132:                {
                    133:                        assert_listener_msg(!msg->get_payload(msg, rule->payload),
                    134:                                                                this, "unexpected payload (%N) found",
                    135:                                                                payload_type_names, rule->payload);
                    136: 
                    137:                }
                    138:                if (rule->notify)
                    139:                {
                    140:                        assert_listener_msg(!msg->get_notify(msg, rule->notify),
                    141:                                                                this, "unexpected notify payload (%N) found",
                    142:                                                                notify_type_names, rule->notify);
                    143:                }
                    144:        }
                    145: }
                    146: 
                    147: /*
                    148:  * Described in header
                    149:  */
                    150: bool exchange_test_asserts_message(listener_t *listener, ike_sa_t *ike_sa,
                    151:                                                                message_t *message, bool incoming, bool plain)
                    152: {
                    153:        listener_message_assert_t *this = (listener_message_assert_t*)listener;
                    154: 
                    155:        if (plain && this->incoming == incoming)
                    156:        {
                    157:                if (this->count >= 0)
                    158:                {
                    159:                        enumerator_t *enumerator;
                    160:                        int count = 0;
                    161:                        enumerator = message->create_payload_enumerator(message);
                    162:                        while (enumerator->enumerate(enumerator, NULL))
                    163:                        {
                    164:                                count++;
                    165:                        }
                    166:                        enumerator->destroy(enumerator);
                    167:                        assert_listener_msg(this->count == count, this, "unexpected payload "
                    168:                                                                "count in message (%d != %d)", this->count,
                    169:                                                                count);
                    170:                }
                    171:                if (this->num_rules)
                    172:                {
                    173:                        int i;
                    174: 
                    175:                        for (i = 0; i < this->num_rules; i++)
                    176:                        {
                    177:                                assert_message_rule(this, message, &this->rules[i]);
                    178:                        }
                    179:                }
                    180:                return FALSE;
                    181:        }
                    182:        return TRUE;
                    183: }
                    184: 
                    185: /**
                    186:  * Compare two SPIs
                    187:  */
                    188: static int spis_cmp(const void *a, const void *b)
                    189: {
                    190:        return *(const uint32_t*)a - *(const uint32_t*)b;
                    191: }
                    192: 
                    193: /**
                    194:  * Compare two SPIs to sort them
                    195:  */
                    196: static int spis_sort(const void *a, const void *b, void *data)
                    197: {
                    198:        return spis_cmp(a, b);
                    199: }
                    200: 
                    201: 
                    202: /*
                    203:  * Described in header
                    204:  */
                    205: void exchange_test_asserts_ipsec_sas(ipsec_sas_assert_t *sas)
                    206: {
                    207:        enumerator_t *enumerator;
                    208:        array_t *spis;
                    209:        ike_sa_t *ike_sa;
                    210:        uint32_t spi;
                    211:        int i;
                    212: 
                    213:        spis = array_create(sizeof(uint32_t), 0);
                    214:        for (i = 0; i < sas->count; i++)
                    215:        {
                    216:                array_insert(spis, ARRAY_TAIL, &sas->spis[i]);
                    217:        }
                    218:        array_sort(spis, spis_sort, NULL);
                    219: 
                    220:        enumerator = mock_ipsec_create_sa_enumerator();
                    221:        while (enumerator->enumerate(enumerator, &ike_sa, &spi))
                    222:        {
                    223:                if (ike_sa == sas->ike_sa)
                    224:                {
                    225:                        i = array_bsearch(spis, &spi, spis_cmp, NULL);
                    226:                        assert_listener_msg(i != -1, sas, "unexpected IPsec SA %.8x", spi);
                    227:                        array_remove(spis, i, NULL);
                    228:                }
                    229:        }
                    230:        enumerator->destroy(enumerator);
                    231:        for (i = 0; i < array_count(spis); i++)
                    232:        {
                    233:                array_get(spis, i, &spi);
                    234:                assert_listener_msg(!spi, sas, "expected IPsec SA %.8x not found", spi);
                    235:        }
                    236:        array_destroy(spis);
                    237: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>