File: object_tester.cc

package info (click to toggle)
belle-sip 5.1.64%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,036 kB
  • sloc: ansic: 275,494; cpp: 2,766; cs: 418; makefile: 184; python: 86; xml: 47; sh: 41; objc: 38; ruby: 8
file content (275 lines) | stat: -rw-r--r-- 9,041 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
152
153
154
155
156
157
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
185
186
187
188
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
/*
 * 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/>.
 */


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

	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();
	BC_ASSERT_TRUE(object_destroyed);
};



typedef struct _LinphoneEvent LinphoneEvent;

typedef enum _LinphoneEventState{
	LinphoneEventIdle,
	LinphoneEventSubscribed
}LinphoneEventState;

namespace Linphone{

class Event : public HybridObject<LinphoneEvent, Event> {
	public:

		enum State {
			Idle,
			Subscribed
		};
		Event() : mState(Idle){
		}
		void sendSubscribe(const std::string& dest){
			mState = Subscribed;
		}
		State getState()const{
			return mState;
		}
		void doSomething(){
			throw BctbxException("Unimplemented");
		}
		Event *clone()const{
			return new Event(*this);
		}
	protected:
		Event(const Event& orig) : HybridObject<LinphoneEvent, Event>(orig){
			mState = orig.mState;
		}
		//~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_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.
}

static void dual_object_shared_ptr(void){
	int object_destroyed = 0;
	std::shared_ptr<Event> ev = Event::create();
	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);
	}
	//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.
	// Get a manual reference again:
	belle_sip_object_ref(c_obj);
	// Drop the shared_ptr:
	ev.reset();
	BC_ASSERT_FALSE(object_destroyed);
	// Get again a shared_ptr, this will test the re-instanciation of the internal weak_ptr (as std::enable_shared_from_this)
	ev = Event::getSharedFromThis((LinphoneEvent*)c_obj);
	// Drop the C reference:
	belle_sip_object_unref(c_obj);
	BC_ASSERT_FALSE(object_destroyed);
	
	ev.reset();
 	BC_ASSERT_TRUE(object_destroyed); // Now the object should be destroyed.
}

static void dual_object_shared_from_this(void){
	int object_destroyed = 0;
	std::shared_ptr<Event> ev = Event::create();
	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);
	}
	otherptr = ev->getSharedFromThis();
	ev.reset();
	BC_ASSERT_FALSE(object_destroyed);
	otherptr.reset();
	BC_ASSERT_TRUE(object_destroyed);
}

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

static void dual_object_in_weak_ptr(void){
	int object_destroyed = 0;
	std::shared_ptr<Event> ev = Event::create();
	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);
	}
	//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.
	std::weak_ptr<Event> ev_weak = Event::toCpp((LinphoneEvent*)c_obj)->getSharedFromThis(); /* get another shared_ptr to make the weak one */
	BC_ASSERT_TRUE(ev_weak.lock() != nullptr);
	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.
	ev.reset();
 	BC_ASSERT_TRUE(object_destroyed); // Now the object should be destroyed.
	/* the weak_ptr shall be reset too. */
	BC_ASSERT_TRUE(ev_weak.lock() == nullptr);
}

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

	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 clone", dual_object_clone),
	TEST_NO_TAG("Hybrid C/C++ object with shared_ptr", dual_object_shared_ptr),
	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),
	TEST_NO_TAG("Hybrid C/C++ object with in a weak_ptr", dual_object_in_weak_ptr),
	TEST_NO_TAG("Mainloop's do_later in c++", main_loop_cpp_do_later)
};

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