1 #include <glib.h>
2 #include <stdarg.h>
3 #include <string.h>
4 #include <stddef.h>
5 #include <setjmp.h>
6 #include <cmocka.h>
7 #include <stdlib.h>
8
9 #include "xmpp/contact.h"
10
11 void
contact_in_group(void ** state)12 contact_in_group(void** state)
13 {
14 GSList* groups = NULL;
15 groups = g_slist_append(groups, strdup("somegroup"));
16 PContact contact = p_contact_new("bob@server.com", "bob", groups, "both",
17 "is offline", FALSE);
18
19 gboolean result = p_contact_in_group(contact, "somegroup");
20
21 assert_true(result);
22
23 p_contact_free(contact);
24 // g_slist_free(groups);
25 }
26
27 void
contact_not_in_group(void ** state)28 contact_not_in_group(void** state)
29 {
30 GSList* groups = NULL;
31 groups = g_slist_append(groups, strdup("somegroup"));
32 PContact contact = p_contact_new("bob@server.com", "bob", groups, "both",
33 "is offline", FALSE);
34
35 gboolean result = p_contact_in_group(contact, "othergroup");
36
37 assert_false(result);
38
39 p_contact_free(contact);
40 // g_slist_free(groups);
41 }
42
43 void
contact_name_when_name_exists(void ** state)44 contact_name_when_name_exists(void** state)
45 {
46 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
47 "is offline", FALSE);
48
49 const char* name = p_contact_name_or_jid(contact);
50
51 assert_string_equal("bob", name);
52
53 p_contact_free(contact);
54 }
55
56 void
contact_jid_when_name_not_exists(void ** state)57 contact_jid_when_name_not_exists(void** state)
58 {
59 PContact contact = p_contact_new("bob@server.com", NULL, NULL, "both",
60 "is offline", FALSE);
61
62 const char* jid = p_contact_name_or_jid(contact);
63
64 assert_string_equal("bob@server.com", jid);
65
66 p_contact_free(contact);
67 }
68
69 void
contact_string_when_name_exists(void ** state)70 contact_string_when_name_exists(void** state)
71 {
72 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
73 "is offline", FALSE);
74
75 char* str = p_contact_create_display_string(contact, "laptop");
76
77 assert_string_equal("bob (laptop)", str);
78
79 p_contact_free(contact);
80 free(str);
81 }
82
83 void
contact_string_when_name_not_exists(void ** state)84 contact_string_when_name_not_exists(void** state)
85 {
86 PContact contact = p_contact_new("bob@server.com", NULL, NULL, "both",
87 "is offline", FALSE);
88
89 char* str = p_contact_create_display_string(contact, "laptop");
90
91 assert_string_equal("bob@server.com (laptop)", str);
92
93 p_contact_free(contact);
94 free(str);
95 }
96
97 void
contact_string_when_default_resource(void ** state)98 contact_string_when_default_resource(void** state)
99 {
100 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
101 "is offline", FALSE);
102
103 char* str = p_contact_create_display_string(contact, "__prof_default");
104
105 assert_string_equal("bob", str);
106
107 p_contact_free(contact);
108 free(str);
109 }
110
111 void
contact_presence_offline(void ** state)112 contact_presence_offline(void** state)
113 {
114 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
115 "is offline", FALSE);
116
117 const char* presence = p_contact_presence(contact);
118
119 assert_string_equal("offline", presence);
120
121 p_contact_free(contact);
122 }
123
124 void
contact_presence_uses_highest_priority(void ** state)125 contact_presence_uses_highest_priority(void** state)
126 {
127 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
128 "is offline", FALSE);
129
130 Resource* resource10 = resource_new("resource10", RESOURCE_ONLINE, NULL, 10);
131 Resource* resource20 = resource_new("resource20", RESOURCE_CHAT, NULL, 20);
132 Resource* resource30 = resource_new("resource30", RESOURCE_AWAY, NULL, 30);
133 Resource* resource1 = resource_new("resource1", RESOURCE_XA, NULL, 1);
134 Resource* resource2 = resource_new("resource2", RESOURCE_DND, NULL, 2);
135 p_contact_set_presence(contact, resource10);
136 p_contact_set_presence(contact, resource20);
137 p_contact_set_presence(contact, resource30);
138 p_contact_set_presence(contact, resource1);
139 p_contact_set_presence(contact, resource2);
140
141 const char* presence = p_contact_presence(contact);
142
143 assert_string_equal("away", presence);
144
145 p_contact_free(contact);
146 }
147
148 void
contact_presence_chat_when_same_prioroty(void ** state)149 contact_presence_chat_when_same_prioroty(void** state)
150 {
151 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
152 "is offline", FALSE);
153
154 Resource* resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
155 Resource* resource_chat = resource_new("resource_chat", RESOURCE_CHAT, NULL, 10);
156 Resource* resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
157 Resource* resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
158 Resource* resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
159 p_contact_set_presence(contact, resource_online);
160 p_contact_set_presence(contact, resource_chat);
161 p_contact_set_presence(contact, resource_away);
162 p_contact_set_presence(contact, resource_xa);
163 p_contact_set_presence(contact, resource_dnd);
164
165 const char* presence = p_contact_presence(contact);
166
167 assert_string_equal("chat", presence);
168
169 p_contact_free(contact);
170 }
171
172 void
contact_presence_online_when_same_prioroty(void ** state)173 contact_presence_online_when_same_prioroty(void** state)
174 {
175 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
176 "is offline", FALSE);
177
178 Resource* resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
179 Resource* resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
180 Resource* resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
181 Resource* resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
182 p_contact_set_presence(contact, resource_online);
183 p_contact_set_presence(contact, resource_away);
184 p_contact_set_presence(contact, resource_xa);
185 p_contact_set_presence(contact, resource_dnd);
186
187 const char* presence = p_contact_presence(contact);
188
189 assert_string_equal("online", presence);
190
191 p_contact_free(contact);
192 }
193
194 void
contact_presence_away_when_same_prioroty(void ** state)195 contact_presence_away_when_same_prioroty(void** state)
196 {
197 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
198 "is offline", FALSE);
199
200 Resource* resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
201 Resource* resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
202 Resource* resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
203 p_contact_set_presence(contact, resource_away);
204 p_contact_set_presence(contact, resource_xa);
205 p_contact_set_presence(contact, resource_dnd);
206
207 const char* presence = p_contact_presence(contact);
208
209 assert_string_equal("away", presence);
210
211 p_contact_free(contact);
212 }
213
214 void
contact_presence_xa_when_same_prioroty(void ** state)215 contact_presence_xa_when_same_prioroty(void** state)
216 {
217 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
218 "is offline", FALSE);
219
220 Resource* resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
221 Resource* resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
222 p_contact_set_presence(contact, resource_xa);
223 p_contact_set_presence(contact, resource_dnd);
224
225 const char* presence = p_contact_presence(contact);
226
227 assert_string_equal("xa", presence);
228
229 p_contact_free(contact);
230 }
231
232 void
contact_presence_dnd(void ** state)233 contact_presence_dnd(void** state)
234 {
235 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
236 "is offline", FALSE);
237
238 Resource* resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
239 p_contact_set_presence(contact, resource_dnd);
240
241 const char* presence = p_contact_presence(contact);
242
243 assert_string_equal("dnd", presence);
244
245 p_contact_free(contact);
246 }
247
248 void
contact_subscribed_when_to(void ** state)249 contact_subscribed_when_to(void** state)
250 {
251 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "to",
252 "is offline", FALSE);
253
254 gboolean result = p_contact_subscribed(contact);
255
256 assert_true(result);
257
258 p_contact_free(contact);
259 }
260
261 void
contact_subscribed_when_both(void ** state)262 contact_subscribed_when_both(void** state)
263 {
264 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
265 "is offline", FALSE);
266
267 gboolean result = p_contact_subscribed(contact);
268
269 assert_true(result);
270
271 p_contact_free(contact);
272 }
273
274 void
contact_not_subscribed_when_from(void ** state)275 contact_not_subscribed_when_from(void** state)
276 {
277 PContact contact = p_contact_new("bob@server.com", "bob", NULL, "from",
278 "is offline", FALSE);
279
280 gboolean result = p_contact_subscribed(contact);
281
282 assert_false(result);
283
284 p_contact_free(contact);
285 }
286
287 void
contact_not_subscribed_when_no_subscription_value(void ** state)288 contact_not_subscribed_when_no_subscription_value(void** state)
289 {
290 PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
291 "is offline", FALSE);
292
293 gboolean result = p_contact_subscribed(contact);
294
295 assert_false(result);
296
297 p_contact_free(contact);
298 }
299
300 void
contact_not_available(void ** state)301 contact_not_available(void** state)
302 {
303 PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
304 "is offline", FALSE);
305
306 gboolean result = p_contact_is_available(contact);
307
308 assert_false(result);
309
310 p_contact_free(contact);
311 }
312
313 void
contact_not_available_when_highest_priority_away(void ** state)314 contact_not_available_when_highest_priority_away(void** state)
315 {
316 PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
317 "is offline", FALSE);
318
319 Resource* resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
320 Resource* resource_chat = resource_new("resource_chat", RESOURCE_CHAT, NULL, 10);
321 Resource* resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 20);
322 Resource* resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
323 Resource* resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
324 p_contact_set_presence(contact, resource_online);
325 p_contact_set_presence(contact, resource_chat);
326 p_contact_set_presence(contact, resource_away);
327 p_contact_set_presence(contact, resource_xa);
328 p_contact_set_presence(contact, resource_dnd);
329
330 gboolean result = p_contact_is_available(contact);
331
332 assert_false(result);
333
334 p_contact_free(contact);
335 }
336
337 void
contact_not_available_when_highest_priority_xa(void ** state)338 contact_not_available_when_highest_priority_xa(void** state)
339 {
340 PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
341 "is offline", FALSE);
342
343 Resource* resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
344 Resource* resource_chat = resource_new("resource_chat", RESOURCE_CHAT, NULL, 10);
345 Resource* resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
346 Resource* resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 20);
347 Resource* resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
348 p_contact_set_presence(contact, resource_online);
349 p_contact_set_presence(contact, resource_chat);
350 p_contact_set_presence(contact, resource_away);
351 p_contact_set_presence(contact, resource_xa);
352 p_contact_set_presence(contact, resource_dnd);
353
354 gboolean result = p_contact_is_available(contact);
355
356 assert_false(result);
357
358 p_contact_free(contact);
359 }
360
361 void
contact_not_available_when_highest_priority_dnd(void ** state)362 contact_not_available_when_highest_priority_dnd(void** state)
363 {
364 PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
365 "is offline", FALSE);
366
367 Resource* resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
368 Resource* resource_chat = resource_new("resource_chat", RESOURCE_CHAT, NULL, 10);
369 Resource* resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
370 Resource* resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
371 Resource* resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 20);
372 p_contact_set_presence(contact, resource_online);
373 p_contact_set_presence(contact, resource_chat);
374 p_contact_set_presence(contact, resource_away);
375 p_contact_set_presence(contact, resource_xa);
376 p_contact_set_presence(contact, resource_dnd);
377
378 gboolean result = p_contact_is_available(contact);
379
380 assert_false(result);
381
382 p_contact_free(contact);
383 }
384
385 void
contact_available_when_highest_priority_online(void ** state)386 contact_available_when_highest_priority_online(void** state)
387 {
388 PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
389 "is offline", FALSE);
390
391 Resource* resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 20);
392 Resource* resource_chat = resource_new("resource_chat", RESOURCE_CHAT, NULL, 10);
393 Resource* resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
394 Resource* resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
395 Resource* resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
396 p_contact_set_presence(contact, resource_online);
397 p_contact_set_presence(contact, resource_chat);
398 p_contact_set_presence(contact, resource_away);
399 p_contact_set_presence(contact, resource_xa);
400 p_contact_set_presence(contact, resource_dnd);
401
402 gboolean result = p_contact_is_available(contact);
403
404 assert_true(result);
405
406 p_contact_free(contact);
407 }
408
409 void
contact_available_when_highest_priority_chat(void ** state)410 contact_available_when_highest_priority_chat(void** state)
411 {
412 PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
413 "is offline", FALSE);
414
415 Resource* resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
416 Resource* resource_chat = resource_new("resource_chat", RESOURCE_CHAT, NULL, 20);
417 Resource* resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
418 Resource* resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
419 Resource* resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
420 p_contact_set_presence(contact, resource_online);
421 p_contact_set_presence(contact, resource_chat);
422 p_contact_set_presence(contact, resource_away);
423 p_contact_set_presence(contact, resource_xa);
424 p_contact_set_presence(contact, resource_dnd);
425
426 gboolean result = p_contact_is_available(contact);
427
428 assert_true(result);
429
430 p_contact_free(contact);
431 }
432