belle_sip_core_tester.c 13.1 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
	(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 "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"

27
#ifndef _WIN32
28 29
#include <sys/types.h>
#include <inttypes.h>
30
#endif
31 32 33 34

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

35
static int foreach_called = 0;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
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;
}

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

60 61 62 63 64 65 66 67 68 69
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
70
	BC_ASSERT_EQUAL(belle_sip_object_data_set(obj, "test_i", INT_TO_VOIDPTR(i), NULL), 0, int, "%d");
71
	// should return the value we put in it
72
	BC_ASSERT_EQUAL( VOIDPTR_TO_INT(belle_sip_object_data_get(obj, "test_i")), i, int, "%d");
73 74 75 76 77 78 79 80 81


	/*
	 * Overwriting insertion
	 */


	// overwrite data: should return 1 when set()
	i = 124;
82
	BC_ASSERT_EQUAL(belle_sip_object_data_set(obj, "test_i", INT_TO_VOIDPTR(i), NULL), 1 , int, "%d");
83
	// should return the new value we put in it
84
	BC_ASSERT_EQUAL( VOIDPTR_TO_INT(belle_sip_object_data_get(obj, "test_i")), i, int, "%d");
85 86 87 88 89 90


	/*
	 * normal insertion with destroy callback
	 */

91
	BC_ASSERT_EQUAL(belle_sip_object_data_set(obj, "test_str",
92 93
											  (void*)belle_sip_strdup(str),
											  test_object_data_string_destroy),
94
					0, int, "%d");
95 96

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

100
	BC_ASSERT_EQUAL(belle_sip_object_data_remove(obj, "test_str"),0, int, "%d");
101
	// we expect the destroy() function to be called on removal
102
	BC_ASSERT_EQUAL(destroy_called, 1, int, "%d");
103 104 105 106 107 108 109 110 111 112 113
	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);
114
	BC_ASSERT_EQUAL(destroy_called, 1, int, "%d"); // we expect the dtor to have been called to free the first string
115 116 117 118 119


	/*
	 * Get non-existent key
	 */
120
	BC_ASSERT_PTR_NULL(belle_sip_object_data_get(obj, "non-exist"));
121 122 123 124 125

	/*
	 * test cloning the dictionary
	 */
	belle_sip_object_data_clone(obj, cloned, test_object_data_string_clone);
126
	BC_ASSERT_EQUAL(clone_called,2,int,"%d"); // we expect the clone function to be called for "test_i" and "test_st, int, "%d"r"
127 128

	// the values should be equal
129 130 131
	BC_ASSERT_EQUAL( VOIDPTR_TO_INT(belle_sip_object_data_get(obj, "test_i")),
					 VOIDPTR_TO_INT(belle_sip_object_data_get(cloned, "test_i"))
					 , int, "%d");
132

133
	BC_ASSERT_STRING_EQUAL( (const char*)belle_sip_object_data_get(obj, "test_str"),
134
							(const char*)belle_sip_object_data_get(cloned, "test_str"));
135
	// but the pointers should be different
136 137
	BC_ASSERT_PTR_NOT_EQUAL( belle_sip_object_data_get(obj, "test_str"),
							belle_sip_object_data_get(cloned, "test_str"));
138

139

140 141 142 143
	/*
	 * Foreach test
	 */
	belle_sip_object_data_foreach(obj, test_object_data_foreach_cb, NULL);
144
	BC_ASSERT_EQUAL( foreach_called, 2 , int, "%d");
145

146 147 148 149 150 151 152 153 154 155
	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;
156
	int64_t i64 = 0xF2345678 << 1; // gcc doesn't like 0x1234567890 as a 64 bit literal..
157 158

	belle_sip_dict_set_int(obj, "test_i", i);
159
	BC_ASSERT_EQUAL(belle_sip_dict_get_int(obj,"test_i",-1),i, int, "%d");
160 161

	// return default int value
162
	BC_ASSERT_EQUAL(belle_sip_dict_get_int(obj,"unexistent",-1),-1, int, "%d");
163 164

	// remove existing entry
165
	BC_ASSERT_EQUAL(belle_sip_dict_remove(obj, "test_i"),0, int, "%d");
166 167

	// test_i should't be present anymore
168
	BC_ASSERT_EQUAL(belle_sip_dict_get_int(obj,"test_i",-1),-1, int, "%d");
169 170

	// remove unknown entry
171
	BC_ASSERT_NOT_EQUAL(belle_sip_dict_remove(obj, "unexistent"),0,int,"%d");
172 173

	belle_sip_dict_set_string(obj, "test_str", str);
174
	BC_ASSERT_STRING_EQUAL( (const char*)belle_sip_dict_get_string(obj, "test_str", ""),str);
175 176

	// unknown string value
177
	BC_ASSERT_STRING_EQUAL( (const char*)belle_sip_dict_get_string(obj, "unexistent", "toto"),"toto");
178 179

	belle_sip_dict_set_int64(obj, "test_i64", i64);
Simon Morlat's avatar
Simon Morlat committed
180
	BC_ASSERT_EQUAL(belle_sip_dict_get_int64(obj,"test_i64",-1),i64, long long, "%lld");
181 182 183

	belle_sip_dict_clear(obj);
	// test_str shouldn't exist anymore
184
	BC_ASSERT_STRING_EQUAL(belle_sip_dict_get_string(obj,"test_str","toto"),"toto");
185 186 187 188

	belle_sip_object_unref(obj);
}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static const char *parts_id[] = {
	"6h-Yqv3@sip.linphonecyberattack.fr",
	"Azgwg6Q@sip.linphonecyberattack.fr",
	"0NKY7am@sip.linphonecyberattack.fr",
	"p8AxOjL@sip.linphonecyberattack.fr",
	"HqquRnL@sip.linphonecyberattack.fr",
	"ftVeEXj@sip.linphonecyberattack.fr",
	"3OQQ~t4@sip.linphonecyberattack.fr"
};
static const char *parts_content[] = {
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
	"<list xmlns=\"urn:ietf:params:xml:ns:rlmi\" fullState=\"false\" uri=\"sip:rls@sip.linphonecyberattack.fr\" version=\"1\">\n"
	"  <resource uri=\"sip:francois@sip.linphonecyberattack.fr\">\n"
	"    <instance cid=\"Azgwg6Q@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
	"  </resource>\n"
	"  <resource uri=\"sip:simon@sip.linphonecyberattack.fr\">\n"
	"    <instance cid=\"0NKY7am@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
	"  </resource>\n"
	"  <resource uri=\"sip:gautier@sip.linphonecyberattack.fr\">\n"
	"    <instance cid=\"p8AxOjL@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
	"  </resource>\n"
	"  <resource uri=\"sip:margaux@sip.linphonecyberattack.fr\">\n"
	"    <instance cid=\"HqquRnL@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
	"  </resource>\n"
	"  <resource uri=\"sip:marielle@sip.linphonecyberattack.fr\">\n"
	"    <instance cid=\"ftVeEXj@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
	"  </resource>\n"
	"  <resource uri=\"sip:sylvain@sip.linphonecyberattack.fr\">\n"
	"    <instance cid=\"3OQQ~t4@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
	"  </resource>\n"
	"</list>\n",
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
	"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:francois@sip.linphonecyberattack.fr\">\n"
	"  <tuple id=\"pfaa79\">\n"
	"    <status>\n"
	"      <basic>open</basic>\n"
	"    </status>\n"
	"    <contact>sip:francois@sip.linphonecyberattack.fr</contact>\n"
	"    <timestamp>2016-06-13T16:12:48</timestamp>\n"
	"  </tuple>\n"
	"</presence>",
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
	"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:simon@sip.linphonecyberattack.fr\">\n"
	"  <tuple id=\"d-0p-1\">\n"
	"    <status>\n"
	"      <basic>open</basic>\n"
	"    </status>\n"
	"    <contact>sip:simon@sip.linphonecyberattack.fr</contact>\n"
	"    <timestamp>2016-06-13T16:12:48</timestamp>\n"
	"  </tuple>\n"
	"</presence>",
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
	"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:gautier@sip.linphonecyberattack.fr\">\n"
	"  <tuple id=\"fz3ywt\">\n"
	"    <status>\n"
	"      <basic>open</basic>\n"
	"    </status>\n"
	"    <contact>sip:gautier@sip.linphonecyberattack.fr</contact>\n"
	"    <timestamp>2016-06-13T16:12:48</timestamp>\n"
	"  </tuple>\n"
	"</presence>",
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
	"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:margaux@sip.linphonecyberattack.fr\">\n"
	"  <tuple id=\"_p7ttd\">\n"
	"    <status>\n"
	"      <basic>open</basic>\n"
	"    </status>\n"
	"    <contact>sip:margaux@sip.linphonecyberattack.fr</contact>\n"
	"    <timestamp>2016-06-13T16:12:48</timestamp>\n"
	"  </tuple>\n"
	"</presence>",
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
	"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:marielle@sip.linphonecyberattack.fr\">\n"
	"  <tuple id=\"bro6yr\">\n"
	"    <status>\n"
	"      <basic>open</basic>\n"
	"    </status>\n"
	"    <contact>sip:marielle@sip.linphonecyberattack.fr</contact>\n"
	"    <timestamp>2016-06-13T16:12:48</timestamp>\n"
	"  </tuple>\n"
	"</presence>",
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
	"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:sylvain@sip.linphonecyberattack.fr\">\n"
	"  <tuple id=\"bhpk9z\">\n"
	"    <status>\n"
	"      <basic>open</basic>\n"
	"    </status>\n"
	"    <contact>sip:sylvain@sip.linphonecyberattack.fr</contact>\n"
	"    <timestamp>2016-06-13T16:12:48</timestamp>\n"
	"  </tuple>\n"
	"</presence>"
};

static void test_presence_marshal(void) {
	char *desc;
	unsigned int i;

	belle_sip_memory_body_handler_t *mbh;
	belle_sip_multipart_body_handler_t *mpbh = belle_sip_multipart_body_handler_new(NULL, NULL, NULL, MULTIPART_BOUNDARY);
	belle_sip_object_ref(mpbh);
	for (i = 0; i < 7; i++) {
		belle_sip_header_t *content_transfer_encoding = belle_sip_header_create("Content-Transfer-Encoding", "binary");
		belle_sip_header_t *content_id = belle_sip_header_create("Content-Id", parts_id[i]);
		belle_sip_header_t *content_type;
			if (i == 0) {
				content_type = BELLE_SIP_HEADER(belle_sip_header_content_type_create("application", "rlmi+xml; charset=\"UTF-8\""));
			} else {
				content_type = BELLE_SIP_HEADER(belle_sip_header_content_type_create("application", "pidf+xml; charset=\"UTF-8\""));
			}
		mbh = belle_sip_memory_body_handler_new_copy_from_buffer((void *)parts_content[i], strlen(parts_content[i]), NULL, NULL);
		belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), content_transfer_encoding);
		belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), content_id);
		belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), content_type);
		belle_sip_multipart_body_handler_add_part(mpbh, BELLE_SIP_BODY_HANDLER(mbh));
	}
	desc = belle_sip_object_to_string(mpbh);
	BC_ASSERT_PTR_NOT_NULL(desc);
306
	
307
	if (desc != NULL) {
308 309
		BC_ASSERT_EQUAL((unsigned int)strlen(desc), 4688, unsigned int, "%u");
		belle_sip_message("Body is [%s]",desc);
310
	}
311 312 313 314 315 316 317 318 319
	belle_sip_multipart_body_handler_t *memory_mpbh = belle_sip_multipart_body_handler_new_from_buffer(desc, strlen(desc), belle_sip_multipart_body_handler_get_boundary(mpbh));
	
	const bctbx_list_t * parts = belle_sip_multipart_body_handler_get_parts(memory_mpbh);
	for (i = 0; i < 7 ; i++) {
		belle_sip_body_handler_t *part = (belle_sip_body_handler_t *)parts->data;
		BC_ASSERT_EQUAL(belle_sip_body_handler_get_size(part), strlen(parts_content[i]), int, "%i");
		parts = parts->next;
	}
	
320
	belle_sip_object_unref(mpbh);
321
	belle_sip_object_unref(memory_mpbh);
322 323
}

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
static void test_compressed_body(void) {
	belle_sip_memory_body_handler_t *mbh = belle_sip_memory_body_handler_new_copy_from_buffer((void *)parts_content[0], strlen(parts_content[0]), NULL, NULL);
	belle_sip_memory_body_handler_apply_encoding(mbh, "deflate");
	BC_ASSERT_EQUAL(belle_sip_memory_body_handler_unapply_encoding(mbh, "deflate"), 0, int, "%i");
	belle_sip_object_unref(mbh);
}

static void test_truncated_compressed_body(void) {
	belle_sip_memory_body_handler_t *mbh = belle_sip_memory_body_handler_new_copy_from_buffer((void *)parts_content[0], strlen(parts_content[0]), NULL, NULL);
	belle_sip_memory_body_handler_apply_encoding(mbh, "deflate");
	/* Truncate the body */
	belle_sip_body_handler_set_size(BELLE_SIP_BODY_HANDLER(mbh), belle_sip_body_handler_get_size(BELLE_SIP_BODY_HANDLER(mbh)) - 5);
	BC_ASSERT_EQUAL(belle_sip_memory_body_handler_unapply_encoding(mbh, "deflate"), -1, int, "%i");
	belle_sip_object_unref(mbh);
}

340 341

test_t core_tests[] = {
342 343
	TEST_NO_TAG("Object Data", test_object_data),
	TEST_NO_TAG("Dictionary", test_dictionary),
344 345 346
	TEST_NO_TAG("Presence marshal", test_presence_marshal),
	TEST_NO_TAG("Compressed body", test_compressed_body),
	TEST_NO_TAG("Truncated compressed body", test_truncated_compressed_body)
347 348
};

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