belle_sip_core_tester.c 5.8 KB
Newer Older
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
	Copyright (C) 2010  Belledonne Communications SARL

	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
7
	the Free Software Foundation, either version 2 of the License, or
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
	(at your option) any later version.

	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.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include "CUnit/Basic.h"
#include "belle-sip/belle-sip.h"
#include "belle-sip/object.h"
#include "belle-sip/dict.h"
#include "belle_sip_tester.h"
#include "belle_sip_internal.h"

#include "register_tester.h"

29 30 31 32 33 34 35 36
#ifndef WIN32
#include <sys/types.h>
#endif
#include <inttypes.h>

#define INT_TO_VOIDPTR(i) ((void*)(intptr_t)(i))
#define VOIDPTR_TO_INT(p) ((int)(intptr_t)(p))

37
static int foreach_called = 0;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static int destroy_called = 0;
static int clone_called = 0;

static void test_object_data_string_destroy(void* data){
	belle_sip_free(data);
	destroy_called++;
}

static void* test_object_data_string_clone(const char*name, void* data){
	clone_called++;
	if( strcmp(name, "test_str") == 0)
		return belle_sip_strdup(data);
	else
		return data;
}

54 55 56 57 58 59 60 61
static void test_object_data_foreach_cb(const char*name, void*data, void*udata)
{
	(void)name;
	(void)data;
	(void)udata;
	foreach_called++;
}

62 63 64 65 66 67 68 69 70 71
static void test_object_data(void)
{
	belle_sip_object_t*    obj = belle_sip_object_new(belle_sip_object_t);
	belle_sip_object_t* cloned = belle_sip_object_new(belle_sip_object_t);
	int i = 123;
	const char* str = "toto", *str2 = "titi";

	/* normal insertion with no destroy callback */

	// should return 0
72
	CU_ASSERT_EQUAL(belle_sip_object_data_set(obj, "test_i", INT_TO_VOIDPTR(i), NULL), 0);
73
	// should return the value we put in it
74
	CU_ASSERT_EQUAL( VOIDPTR_TO_INT(belle_sip_object_data_get(obj, "test_i")), i);
75 76 77 78 79 80 81 82 83


	/*
	 * Overwriting insertion
	 */


	// overwrite data: should return 1 when set()
	i = 124;
84 85 86
	CU_ASSERT_EQUAL(belle_sip_object_data_set(obj, "test_i", INT_TO_VOIDPTR(i), NULL), 1 );
	// should return the new value we put in it
	CU_ASSERT_EQUAL( VOIDPTR_TO_INT(belle_sip_object_data_get(obj, "test_i")), i);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130


	/*
	 * normal insertion with destroy callback
	 */

	CU_ASSERT_EQUAL(belle_sip_object_data_set(obj, "test_str",
											  (void*)belle_sip_strdup(str),
											  test_object_data_string_destroy),
					0);

	// we should get back the same string
	CU_ASSERT_STRING_EQUAL( (const char*)belle_sip_object_data_get(obj, "test_str"),
							str );

	CU_ASSERT_EQUAL(belle_sip_object_data_remove(obj, "test_str"),0);
	// we expect the destroy() function to be called on removal
	CU_ASSERT_EQUAL(destroy_called, 1);
	destroy_called = 0;

	/*
	 * string insertion and replace
	 */
	belle_sip_object_data_set(obj, "test_str",
							  (void*)belle_sip_strdup(str),
							  test_object_data_string_destroy);
	belle_sip_object_data_set(obj, "test_str",
							  (void*)belle_sip_strdup(str2),
							  test_object_data_string_destroy);
	CU_ASSERT_EQUAL(destroy_called, 1); // we expect the dtor to have been called to free the first string


	/*
	 * Get non-existent key
	 */
	CU_ASSERT_EQUAL(belle_sip_object_data_get(obj, "non-exist"),NULL);

	/*
	 * test cloning the dictionary
	 */
	belle_sip_object_data_clone(obj, cloned, test_object_data_string_clone);
	CU_ASSERT_EQUAL(clone_called,2); // we expect the clone function to be called for "test_i" and "test_str"

	// the values should be equal
131 132
	CU_ASSERT_EQUAL( VOIDPTR_TO_INT(belle_sip_object_data_get(obj, "test_i")),
					 VOIDPTR_TO_INT(belle_sip_object_data_get(cloned, "test_i")) );
133 134 135

	CU_ASSERT_STRING_EQUAL( (const char*)belle_sip_object_data_get(obj, "test_str"),
							(const char*)belle_sip_object_data_get(cloned, "test_str"));
136
	// but the pointers should be different
137 138 139
	CU_ASSERT_NOT_EQUAL( (const char*)belle_sip_object_data_get(obj, "test_str"),
							(const char*)belle_sip_object_data_get(cloned, "test_str"));

140

141 142 143 144 145 146
	/*
	 * Foreach test
	 */
	belle_sip_object_data_foreach(obj, test_object_data_foreach_cb, NULL);
	CU_ASSERT_EQUAL( foreach_called, 2 );

147 148 149 150 151 152 153 154 155 156
	belle_sip_object_unref(obj);
	belle_sip_object_unref(cloned);

}

static void test_dictionary(void)
{
	belle_sip_dict_t* obj = belle_sip_object_new(belle_sip_dict_t);
	const char* str = "";
	int i = 5;
157
	int64_t i64 = 0xF2345678 << 1; // gcc doesn't like 0x1234567890 as a 64 bit literal..
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

	belle_sip_dict_set_int(obj, "test_i", i);
	CU_ASSERT_EQUAL(belle_sip_dict_get_int(obj,"test_i",-1),i);

	// return default int value
	CU_ASSERT_EQUAL(belle_sip_dict_get_int(obj,"unexistent",-1),-1);

	// remove existing entry
	CU_ASSERT_EQUAL(belle_sip_dict_remove(obj, "test_i"),0);

	// test_i should't be present anymore
	CU_ASSERT_EQUAL(belle_sip_dict_get_int(obj,"test_i",-1),-1);

	// remove unknown entry
	CU_ASSERT_NOT_EQUAL(belle_sip_dict_remove(obj, "unexistent"),0);

	belle_sip_dict_set_string(obj, "test_str", str);
	CU_ASSERT_STRING_EQUAL( (const char*)belle_sip_dict_get_string(obj, "test_str", ""),str);

	// unknown string value
	CU_ASSERT_STRING_EQUAL( (const char*)belle_sip_dict_get_string(obj, "unexistent", "toto"),"toto");

	belle_sip_dict_set_int64(obj, "test_i64", i64);
	CU_ASSERT_EQUAL(belle_sip_dict_get_int64(obj,"test_i64",-1),i64);

	belle_sip_dict_clear(obj);
	// test_str shouldn't exist anymore
185
	CU_ASSERT_STRING_EQUAL(belle_sip_dict_get_string(obj,"test_str","toto"),"toto");
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

	belle_sip_object_unref(obj);
}


test_t core_tests[] = {
	{ "Object Data", test_object_data },
	{ "Dictionary", test_dictionary }
};

test_suite_t core_test_suite = {
	"Core",
	NULL,
	NULL,
	sizeof(core_tests) / sizeof(core_tests[0]),
	core_tests
};