object_tester.cc 7.27 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 18
 * Copyright (c) 2012-2019 Belledonne Communications SARL.
 *
 * This file is part of belle-sip.
 *
 * 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/>.
 */
Simon Morlat's avatar
Simon Morlat committed
19

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

#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();
37

Simon Morlat's avatar
Simon Morlat committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	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();
59
	BC_ASSERT_TRUE(object_destroyed);
Simon Morlat's avatar
Simon Morlat committed
60 61
};

62 63


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

typedef enum _LinphoneEventState{
	LinphoneEventIdle,
	LinphoneEventSubscribed
}LinphoneEventState;

namespace Linphone{

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

		enum State {
Simon Morlat's avatar
Simon Morlat committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90
			Idle,
			Subscribed
		};
		Event() : mState(Idle){
		}
		void sendSubscribe(const std::string& dest){
			mState = Subscribed;
		}
		State getState()const{
			return mState;
		}
		void doSomething(){
			throw BctbxException("Unimplemented");
		}
91 92 93
		Event *clone()const{
			return new Event(*this);
		}
Simon Morlat's avatar
Simon Morlat committed
94
	protected:
95 96 97
		Event(const Event& orig) : HybridObject<LinphoneEvent, Event>(orig){
			mState = orig.mState;
		}
Simon Morlat's avatar
Simon Morlat committed
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
		//~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);
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
static void dual_object_clone(void){
	int object_destroyed = 0;
	std::shared_ptr<Event> ev = Event::create();
	std::shared_ptr<Event> cloned_ev = ev->clone()->toSharedPtr();
	
	belle_sip_object_t *c_obj = cloned_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();
	BC_ASSERT_FALSE(object_destroyed); // the reset() shall not cause the cloned object to be destroyed of course.
	cloned_ev.reset();
 	BC_ASSERT_TRUE(object_destroyed); // Now the object should be destroyed.
}

Simon Morlat's avatar
Simon Morlat committed
166 167
static void dual_object_shared_ptr(void){
	int object_destroyed = 0;
168
	std::shared_ptr<Event> ev = Event::create();
Simon Morlat's avatar
Simon Morlat committed
169 170 171 172 173
	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);
	}
174 175 176 177 178 179 180
	//Here we mix manual reference from C and shared_ptr.
	belle_sip_object_ref(c_obj);
	ev.reset();
	BC_ASSERT_FALSE(object_destroyed); // the reset() shall not cause the object to be destroyed.
	ev = Event::toCpp((LinphoneEvent*)c_obj)->getSharedFromThis(); // we get again a shared_ptr.
	belle_sip_object_unref(c_obj);
	BC_ASSERT_FALSE(object_destroyed); // the unref() shall not cause the object to be destroyed, since the shared_ptr has a reference to it.
Simon Morlat's avatar
Simon Morlat committed
181
	ev.reset();
182
 	BC_ASSERT_TRUE(object_destroyed); // Now the object should be destroyed.
Simon Morlat's avatar
Simon Morlat committed
183 184 185 186
}

static void dual_object_shared_from_this(void){
	int object_destroyed = 0;
187
	std::shared_ptr<Event> ev = Event::create();
Simon Morlat's avatar
Simon Morlat committed
188 189 190 191 192 193
	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);
	}
194
	otherptr = ev->getSharedFromThis();
Simon Morlat's avatar
Simon Morlat committed
195 196 197 198 199 200
	ev.reset();
	BC_ASSERT_FALSE(object_destroyed);
	otherptr.reset();
	BC_ASSERT_TRUE(object_destroyed);
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
static void dual_object_shared_from_this_from_c(void){
	int object_destroyed = 0;
	LinphoneEvent *event = Event::createCObject();
	std::shared_ptr<Event> otherptr;
	BC_ASSERT_PTR_NOT_NULL(event);
	if (event){
		belle_sip_object_weak_ref(event, on_object_destroyed, &object_destroyed);
	}
	otherptr = Event::getSharedFromThis(event);
	otherptr.reset(); // the reset() of the shared_ptr shall not cause the object to be destroyed, because we still have the C ref obtained from createCObject(). 
	BC_ASSERT_FALSE(object_destroyed);
	belle_sip_object_unref(event);
	BC_ASSERT_TRUE(object_destroyed);
}

Simon Morlat's avatar
Simon Morlat committed
216 217 218
static void main_loop_cpp_do_later(void){
	int test = 0;
	belle_sip_main_loop_t *ml = belle_sip_main_loop_new();
219

Simon Morlat's avatar
Simon Morlat committed
220 221 222 223 224 225 226 227 228 229
	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),
230
	TEST_NO_TAG("Hybrid C/C++ object clone", dual_object_clone),
Simon Morlat's avatar
Simon Morlat committed
231
	TEST_NO_TAG("Hybrid C/C++ object with shared_ptr", dual_object_shared_ptr),
232 233
	TEST_NO_TAG("Hybrid C/C++ object with sharedFromThis()", dual_object_shared_from_this),
	TEST_NO_TAG("Hybrid C/C++ object with sharedFromThis() from C object", dual_object_shared_from_this_from_c),
234
	TEST_NO_TAG("Mainloop's do_later in c++", main_loop_cpp_do_later)
Simon Morlat's avatar
Simon Morlat committed
235 236 237 238 239 240
};

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};