belle_generic_uri_tester.c 4.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2013  Belledonne Communications SARL, Grenoble, France

    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
    the Free Software Foundation, either version 3 of the License, or
    (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_tester.h"
#include <stdio.h>
#include "CUnit/Basic.h"


static void test_basic_uri() {
	belle_generic_uri_t* source_uri = belle_generic_uri_parse("http://www.linphone.org/index.html");
	char* source_uri_raw = belle_sip_object_to_string(source_uri);
	belle_generic_uri_t* first_uri = belle_generic_uri_parse(source_uri_raw);
	belle_generic_uri_t* uri=BELLE_GENERIC_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(first_uri)));
	belle_sip_free(source_uri_raw);
	belle_sip_object_unref(source_uri);
	belle_sip_object_unref(first_uri);

	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_scheme(uri),"http");
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_host(uri),"www.linphone.org");
jehan's avatar
jehan committed
36
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_path(uri),"/index.html");
37 38 39

	belle_sip_object_unref(uri);

40 41 42 43 44 45 46 47 48
	source_uri = belle_generic_uri_parse("http://www.linphone.org/");
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_path(source_uri),"/");
	belle_sip_object_unref(source_uri);

	source_uri = belle_generic_uri_parse("http://www.linphone.org/a/b/c");
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_path(source_uri),"/a/b/c");
	CU_ASSERT_STRING_EQUAL("http://www.linphone.org/a/b/c",source_uri_raw = belle_sip_object_to_string(source_uri));
	belle_sip_free(source_uri_raw);
	belle_sip_object_unref(source_uri);
49
}
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
static void test_complex_uri() {

	belle_generic_uri_t* source_uri = belle_generic_uri_parse("ftp://toto:secret@ftp.linphone.fr:1234/url?sa=t&rct=j&url=http%3A%2F%2Ftranslate.google.fr");
	char* source_uri_raw = belle_generic_uri_to_string(source_uri);
	belle_generic_uri_t* first_uri = belle_generic_uri_parse(source_uri_raw);
	belle_generic_uri_t* uri=BELLE_GENERIC_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(first_uri)));
	belle_sip_free(source_uri_raw);
	belle_sip_object_unref(source_uri);
	belle_sip_object_unref(first_uri);

	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_scheme(uri),"ftp");
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_host(uri),"ftp.linphone.fr");
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_user(uri),"toto");
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_user_password(uri),"secret");
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_host(uri),"ftp.linphone.fr");
jehan's avatar
jehan committed
66
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_path(uri),"/url");
67 68 69 70 71 72
	CU_ASSERT_EQUAL(belle_generic_uri_get_port(uri),1234);
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_query(uri),"sa=t&rct=j&url=http://translate.google.fr");

	belle_sip_object_unref(uri);
}

73
static void test_file_path() {
jehan's avatar
jehan committed
74 75 76 77 78 79 80 81 82 83
	belle_generic_uri_t* source_uri = belle_generic_uri_parse("/index.html");
	char* source_uri_raw = belle_sip_object_to_string(source_uri);
	belle_generic_uri_t* first_uri = belle_generic_uri_parse(source_uri_raw);
	belle_generic_uri_t* uri=BELLE_GENERIC_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(first_uri)));
	belle_sip_free(source_uri_raw);
	belle_sip_object_unref(source_uri);
	belle_sip_object_unref(first_uri);

	CU_ASSERT_PTR_NULL(belle_generic_uri_get_scheme(uri));
	CU_ASSERT_PTR_NULL(belle_generic_uri_get_host(uri));
jehan's avatar
jehan committed
84
	CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_path(uri),"/index.html");
jehan's avatar
jehan committed
85

86 87 88
	belle_sip_object_unref(uri);

	source_uri = belle_generic_uri_parse("file:///tmp/absolute-file");
89 90 91 92 93 94 95
	CU_ASSERT_PTR_NOT_NULL(source_uri);
	if (source_uri!=NULL){
		CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_scheme(source_uri),"file");
		CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_host(source_uri),"localhost");
		CU_ASSERT_STRING_EQUAL(belle_generic_uri_get_path(source_uri),"/tmp/absolute-file");
		belle_sip_object_unref(source_uri);
	}
96 97 98 99

	//this is INVALID
	source_uri = belle_generic_uri_parse("file:///./relative-file");
	CU_ASSERT_PTR_NULL(source_uri);
100 101 102 103 104 105 106

	// instead, this should be VALID
	source_uri = belle_generic_uri_parse("./relative-file");
	CU_ASSERT_PTR_NOT_NULL(source_uri);
	if (source_uri!=NULL){
		belle_sip_object_unref(source_uri);
	}
jehan's avatar
jehan committed
107
}
108 109 110 111


static test_t tests[] = {
	{ "Simple uri", test_basic_uri },
jehan's avatar
jehan committed
112
	{ "Complex uri", test_complex_uri },
113
	{ "File path", test_file_path }
114 115 116 117 118 119 120 121 122
};

test_suite_t generic_uri_test_suite = {
	"Generic uri",
	NULL,
	NULL,
	sizeof(tests) / sizeof(tests[0]),
	tests
};