object_tester.cc 5.31 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
	belle-sip - SIP (RFC3261) library.
	Copyright (C) 2019  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
	the Free Software Foundation, either version 2 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/>.
*/
Simon Morlat's avatar
Simon Morlat committed
18

Simon Morlat's avatar
Simon Morlat committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include "belle-sip/belle-sip.h"
#include "belle_sip_tester.h"

#include "belle-sip/object++.hh"
#include "bctoolbox/exception.hh"

using namespace bellesip;

static void on_object_destroyed(void *userpointer, belle_sip_object_t *obj_being_destroyed){
	int *value = static_cast<int*>(userpointer);
	*value = TRUE;
}

static void basic_test(void){
	int object_destroyed = FALSE;
	Object *obj = new Object();
36

Simon Morlat's avatar
Simon Morlat committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	belle_sip_object_t *c_obj = obj->getCObject();
	BC_ASSERT_PTR_NOT_NULL(c_obj);
	if (c_obj){
		belle_sip_object_weak_ref(c_obj, on_object_destroyed, &object_destroyed);
	}
	/*we put a weak ref to this object in order to know when it is destroyed*/
	obj->ref();
	Object *clone = obj->clone();

	obj->unref();
	BC_ASSERT_FALSE(object_destroyed);
	obj->unref(); /*this unref will destroy the object*/
	BC_ASSERT_TRUE(object_destroyed);
	object_destroyed = false;

	c_obj = clone->getCObject();
	BC_ASSERT_PTR_NOT_NULL(c_obj);
	if (c_obj){
		belle_sip_object_weak_ref(c_obj, on_object_destroyed, &object_destroyed);
	}
	clone->unref();
58
	BC_ASSERT_TRUE(object_destroyed);
Simon Morlat's avatar
Simon Morlat committed
59 60
};

61 62


Simon Morlat's avatar
Simon Morlat committed
63 64 65 66 67 68 69 70 71
typedef struct _LinphoneEvent LinphoneEvent;

typedef enum _LinphoneEventState{
	LinphoneEventIdle,
	LinphoneEventSubscribed
}LinphoneEventState;

namespace Linphone{

72
class Event : public HybridObject<LinphoneEvent, Event> {
Simon Morlat's avatar
Simon Morlat committed
73
	public:
74 75

		enum State {
Simon Morlat's avatar
Simon Morlat committed
76 77 78 79 80 81 82 83 84 85 86 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
			Idle,
			Subscribed
		};
		Event() : mState(Idle){
		}
		void sendSubscribe(const std::string& dest){
			mState = Subscribed;
		}
		State getState()const{
			return mState;
		}
		void doSomething(){
			throw BctbxException("Unimplemented");
		}
	protected:
		//~Event() = default; //we shouls have the destructor private but in order to test the delete exception
		//we'll make it public.
	private:
		State mState;
};

}//end of namespace

extern "C"{

using namespace Linphone;

LinphoneEvent *linphone_event_new(void){
	return (new Event())->toC();
}

void linphone_event_send_subscribe(LinphoneEvent *obj, const char *dest){
	Event::toCpp(obj)->sendSubscribe(dest);
}

LinphoneEventState linphone_event_get_state(const LinphoneEvent *obj){
	return (LinphoneEventState)Event::toCpp(obj)->getState(); /*enum conversion should be performed better*/
}

void linphone_event_ref(LinphoneEvent *obj){
	Event::toCpp(obj)->ref();
}

void linphone_event_unref(LinphoneEvent *obj){
	Event::toCpp(obj)->unref();
}

}//end of extern "C"


static void dual_object(void){
	int object_destroyed = 0;
	/*in this test we use the C Api */
	LinphoneEvent *ev = linphone_event_new();
	BC_ASSERT_TRUE(linphone_event_get_state(ev) == LinphoneEventIdle);
	linphone_event_send_subscribe(ev, "sip:1234@sip.linphone.org");
	BC_ASSERT_TRUE(linphone_event_get_state(ev) == LinphoneEventSubscribed);

	belle_sip_object_t *c_obj = Linphone::Event::toCpp(ev)->getCObject();
	BC_ASSERT_PTR_NOT_NULL(c_obj);
	if (c_obj){
		belle_sip_object_weak_ref(c_obj, on_object_destroyed, &object_destroyed);
	}
	linphone_event_unref(ev);
	BC_ASSERT_TRUE(object_destroyed);
}

static void dual_object_shared_ptr(void){
	int object_destroyed = 0;
	std::shared_ptr<Event> ev = bellesip::make_shared<Event>();
	belle_sip_object_t *c_obj = ev->getCObject();
	BC_ASSERT_PTR_NOT_NULL(c_obj);
	if (c_obj){
		belle_sip_object_weak_ref(c_obj, on_object_destroyed, &object_destroyed);
	}
	ev.reset();
152
 	BC_ASSERT_TRUE(object_destroyed);
Simon Morlat's avatar
Simon Morlat committed
153 154 155 156 157 158 159 160 161 162 163
}

static void dual_object_shared_from_this(void){
	int object_destroyed = 0;
	std::shared_ptr<Event> ev = bellesip::make_shared<Event>();
	std::shared_ptr<Event> otherptr;
	belle_sip_object_t *c_obj = ev->getCObject();
	BC_ASSERT_PTR_NOT_NULL(c_obj);
	if (c_obj){
		belle_sip_object_weak_ref(c_obj, on_object_destroyed, &object_destroyed);
	}
164
	otherptr = ev->getSharedFromThis();
Simon Morlat's avatar
Simon Morlat committed
165 166 167 168 169 170 171 172 173
	ev.reset();
	BC_ASSERT_FALSE(object_destroyed);
	otherptr.reset();
	BC_ASSERT_TRUE(object_destroyed);
}

static void main_loop_cpp_do_later(void){
	int test = 0;
	belle_sip_main_loop_t *ml = belle_sip_main_loop_new();
174

Simon Morlat's avatar
Simon Morlat committed
175 176 177 178 179 180 181 182 183 184 185 186
	belle_sip_main_loop_cpp_do_later(ml, [&test](){ test = 44; });
	BC_ASSERT_TRUE(test == 0);
	belle_sip_main_loop_sleep(ml, 10);
	BC_ASSERT_TRUE(test == 44);
	belle_sip_object_unref(ml);
}

static test_t object_tests[] = {
        TEST_NO_TAG("Basic test", basic_test),
	TEST_NO_TAG("Hybrid C/C++ object", dual_object),
	TEST_NO_TAG("Hybrid C/C++ object with shared_ptr", dual_object_shared_ptr),
	TEST_NO_TAG("Hybrid C/C++ object with shared_from_this", dual_object_shared_from_this),
187
	TEST_NO_TAG("Mainloop's do_later in c++", main_loop_cpp_do_later)
Simon Morlat's avatar
Simon Morlat committed
188 189 190 191 192 193
};

test_suite_t object_test_suite = {"Object", NULL, NULL, belle_sip_tester_before_each, belle_sip_tester_after_each,
                                                                sizeof(object_tests) / sizeof(object_tests[0]), object_tests};