Annotation of embedaddon/strongswan/src/libstrongswan/utils/test.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2013 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 "test.h"
        !            17: 
        !            18: #include <library.h>
        !            19: 
        !            20: /**
        !            21:  * A collection of testable functions
        !            22:  */
        !            23: static hashtable_t *functions = NULL;
        !            24: 
        !            25: #ifndef WIN32
        !            26: bool test_runner_available __attribute__((weak));
        !            27: #endif
        !            28: 
        !            29: /**
        !            30:  * Check if we have libtest linkage and need testable functions
        !            31:  */
        !            32: static bool has_libtest_linkage()
        !            33: {
        !            34: #ifdef WIN32
        !            35:        return dlsym(RTLD_DEFAULT, "test_runner_available");
        !            36: #else
        !            37:        return test_runner_available;
        !            38: #endif
        !            39: }
        !            40: 
        !            41: /*
        !            42:  * Described in header.
        !            43:  */
        !            44: void testable_function_register(char *name, void *fn)
        !            45: {
        !            46:        bool old = FALSE;
        !            47: 
        !            48:        if (lib && lib->leak_detective)
        !            49:        {
        !            50:                old = lib->leak_detective->set_state(lib->leak_detective, FALSE);
        !            51:        }
        !            52: 
        !            53:        if (has_libtest_linkage())
        !            54:        {
        !            55:                if (!functions)
        !            56:                {
        !            57:                        chunk_hash_seed();
        !            58:                        functions = hashtable_create(hashtable_hash_str,
        !            59:                                                                                 hashtable_equals_str, 8);
        !            60:                }
        !            61:                if (fn)
        !            62:                {
        !            63:                        functions->put(functions, name, fn);
        !            64:                }
        !            65:                else
        !            66:                {
        !            67:                        functions->remove(functions, name);
        !            68:                        if (functions->get_count(functions) == 0)
        !            69:                        {
        !            70:                                functions->destroy(functions);
        !            71:                                functions = NULL;
        !            72:                        }
        !            73:                }
        !            74:        }
        !            75: 
        !            76:        if (lib && lib->leak_detective)
        !            77:        {
        !            78:                lib->leak_detective->set_state(lib->leak_detective, old);
        !            79:        }
        !            80: }
        !            81: 
        !            82: /*
        !            83:  * Described in header.
        !            84:  */
        !            85: void* testable_function_get(char *name)
        !            86: {
        !            87:        if (functions)
        !            88:        {
        !            89:                return functions->get(functions, name);
        !            90:        }
        !            91:        return NULL;
        !            92: }

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