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 #include "xmpp/roster_list.h"
11
12 void
empty_list_when_none_added(void ** state)13 empty_list_when_none_added(void** state)
14 {
15 roster_create();
16 GSList* list = roster_get_contacts(ROSTER_ORD_NAME);
17 assert_null(list);
18
19 g_slist_free(list);
20 roster_destroy();
21 }
22
23 void
contains_one_element(void ** state)24 contains_one_element(void** state)
25 {
26 roster_create();
27 roster_add("James", NULL, NULL, NULL, FALSE);
28 GSList* list = roster_get_contacts(ROSTER_ORD_NAME);
29 assert_int_equal(1, g_slist_length(list));
30
31 g_slist_free(list);
32 roster_destroy();
33 }
34
35 void
first_element_correct(void ** state)36 first_element_correct(void** state)
37 {
38 roster_create();
39 roster_add("James", NULL, NULL, NULL, FALSE);
40 GSList* list = roster_get_contacts(ROSTER_ORD_NAME);
41 PContact james = list->data;
42
43 assert_string_equal("James", p_contact_barejid(james));
44
45 g_slist_free(list);
46 roster_destroy();
47 }
48
49 void
contains_two_elements(void ** state)50 contains_two_elements(void** state)
51 {
52 roster_create();
53 roster_add("James", NULL, NULL, NULL, FALSE);
54 roster_add("Dave", NULL, NULL, NULL, FALSE);
55 GSList* list = roster_get_contacts(ROSTER_ORD_NAME);
56
57 assert_int_equal(2, g_slist_length(list));
58
59 g_slist_free(list);
60 roster_destroy();
61 }
62
63 void
first_and_second_elements_correct(void ** state)64 first_and_second_elements_correct(void** state)
65 {
66 roster_create();
67 roster_add("James", NULL, NULL, NULL, FALSE);
68 roster_add("Dave", NULL, NULL, NULL, FALSE);
69 GSList* list = roster_get_contacts(ROSTER_ORD_NAME);
70
71 PContact first = list->data;
72 PContact second = (g_slist_next(list))->data;
73
74 assert_string_equal("Dave", p_contact_barejid(first));
75 assert_string_equal("James", p_contact_barejid(second));
76
77 g_slist_free(list);
78 roster_destroy();
79 }
80
81 void
contains_three_elements(void ** state)82 contains_three_elements(void** state)
83 {
84 roster_create();
85 roster_add("James", NULL, NULL, NULL, FALSE);
86 roster_add("Bob", NULL, NULL, NULL, FALSE);
87 roster_add("Dave", NULL, NULL, NULL, FALSE);
88 GSList* list = roster_get_contacts(ROSTER_ORD_NAME);
89
90 assert_int_equal(3, g_slist_length(list));
91
92 g_slist_free(list);
93 roster_destroy();
94 }
95
96 void
first_three_elements_correct(void ** state)97 first_three_elements_correct(void** state)
98 {
99 roster_create();
100 roster_add("Bob", NULL, NULL, NULL, FALSE);
101 roster_add("Dave", NULL, NULL, NULL, FALSE);
102 roster_add("James", NULL, NULL, NULL, FALSE);
103 GSList* list = roster_get_contacts(ROSTER_ORD_NAME);
104 PContact bob = list->data;
105 PContact dave = (g_slist_next(list))->data;
106 PContact james = (g_slist_next(g_slist_next(list)))->data;
107
108 assert_string_equal("James", p_contact_barejid(james));
109 assert_string_equal("Dave", p_contact_barejid(dave));
110 assert_string_equal("Bob", p_contact_barejid(bob));
111
112 g_slist_free(list);
113 roster_destroy();
114 }
115
116 void
add_twice_at_beginning_adds_once(void ** state)117 add_twice_at_beginning_adds_once(void** state)
118 {
119 roster_create();
120 roster_add("James", NULL, NULL, NULL, FALSE);
121 roster_add("James", NULL, NULL, NULL, FALSE);
122 roster_add("Dave", NULL, NULL, NULL, FALSE);
123 roster_add("Bob", NULL, NULL, NULL, FALSE);
124 GSList* list = roster_get_contacts(ROSTER_ORD_NAME);
125 PContact first = list->data;
126 PContact second = (g_slist_next(list))->data;
127 PContact third = (g_slist_next(g_slist_next(list)))->data;
128
129 assert_int_equal(3, g_slist_length(list));
130 assert_string_equal("Bob", p_contact_barejid(first));
131 assert_string_equal("Dave", p_contact_barejid(second));
132 assert_string_equal("James", p_contact_barejid(third));
133
134 g_slist_free(list);
135 roster_destroy();
136 }
137
138 void
add_twice_in_middle_adds_once(void ** state)139 add_twice_in_middle_adds_once(void** state)
140 {
141 roster_create();
142 roster_add("James", NULL, NULL, NULL, FALSE);
143 roster_add("Dave", NULL, NULL, NULL, FALSE);
144 roster_add("James", NULL, NULL, NULL, FALSE);
145 roster_add("Bob", NULL, NULL, NULL, FALSE);
146 GSList* list = roster_get_contacts(ROSTER_ORD_NAME);
147 PContact first = list->data;
148 PContact second = (g_slist_next(list))->data;
149 PContact third = (g_slist_next(g_slist_next(list)))->data;
150
151 assert_int_equal(3, g_slist_length(list));
152 assert_string_equal("Bob", p_contact_barejid(first));
153 assert_string_equal("Dave", p_contact_barejid(second));
154 assert_string_equal("James", p_contact_barejid(third));
155
156 g_slist_free(list);
157 roster_destroy();
158 }
159
160 void
add_twice_at_end_adds_once(void ** state)161 add_twice_at_end_adds_once(void** state)
162 {
163 roster_create();
164 roster_add("James", NULL, NULL, NULL, FALSE);
165 roster_add("Dave", NULL, NULL, NULL, FALSE);
166 roster_add("Bob", NULL, NULL, NULL, FALSE);
167 roster_add("James", NULL, NULL, NULL, FALSE);
168 GSList* list = roster_get_contacts(ROSTER_ORD_NAME);
169 PContact first = list->data;
170 PContact second = (g_slist_next(list))->data;
171 PContact third = (g_slist_next(g_slist_next(list)))->data;
172
173 assert_int_equal(3, g_slist_length(list));
174 assert_string_equal("Bob", p_contact_barejid(first));
175 assert_string_equal("Dave", p_contact_barejid(second));
176 assert_string_equal("James", p_contact_barejid(third));
177
178 g_slist_free(list);
179 roster_destroy();
180 }
181
182 void
find_first_exists(void ** state)183 find_first_exists(void** state)
184 {
185 roster_create();
186 roster_add("James", NULL, NULL, NULL, FALSE);
187 roster_add("Dave", NULL, NULL, NULL, FALSE);
188 roster_add("Bob", NULL, NULL, NULL, FALSE);
189
190 char* search = strdup("B");
191
192 char* result = roster_contact_autocomplete(search, FALSE, NULL);
193 assert_string_equal("Bob", result);
194 free(result);
195 free(search);
196 roster_destroy();
197 }
198
199 void
find_second_exists(void ** state)200 find_second_exists(void** state)
201 {
202 roster_create();
203 roster_add("James", NULL, NULL, NULL, FALSE);
204 roster_add("Dave", NULL, NULL, NULL, FALSE);
205 roster_add("Bob", NULL, NULL, NULL, FALSE);
206
207 char* result = roster_contact_autocomplete("Dav", FALSE, NULL);
208 assert_string_equal("Dave", result);
209 free(result);
210 roster_destroy();
211 }
212
213 void
find_third_exists(void ** state)214 find_third_exists(void** state)
215 {
216 roster_create();
217 roster_add("James", NULL, NULL, NULL, FALSE);
218 roster_add("Dave", NULL, NULL, NULL, FALSE);
219 roster_add("Bob", NULL, NULL, NULL, FALSE);
220
221 char* result = roster_contact_autocomplete("Ja", FALSE, NULL);
222 assert_string_equal("James", result);
223 free(result);
224 roster_destroy();
225 }
226
227 void
find_returns_null(void ** state)228 find_returns_null(void** state)
229 {
230 roster_create();
231 roster_add("James", NULL, NULL, NULL, FALSE);
232 roster_add("Dave", NULL, NULL, NULL, FALSE);
233 roster_add("Bob", NULL, NULL, NULL, FALSE);
234
235 char* result = roster_contact_autocomplete("Mike", FALSE, NULL);
236 assert_null(result);
237 roster_destroy();
238 }
239
240 void
find_on_empty_returns_null(void ** state)241 find_on_empty_returns_null(void** state)
242 {
243 roster_create();
244 char* result = roster_contact_autocomplete("James", FALSE, NULL);
245 assert_null(result);
246 roster_destroy();
247 }
248
249 void
find_twice_returns_second_when_two_match(void ** state)250 find_twice_returns_second_when_two_match(void** state)
251 {
252 roster_create();
253 roster_add("James", NULL, NULL, NULL, FALSE);
254 roster_add("Jamie", NULL, NULL, NULL, FALSE);
255 roster_add("Bob", NULL, NULL, NULL, FALSE);
256
257 char* result1 = roster_contact_autocomplete("Jam", FALSE, NULL);
258 char* result2 = roster_contact_autocomplete(result1, FALSE, NULL);
259 assert_string_equal("Jamie", result2);
260 free(result1);
261 free(result2);
262 roster_destroy();
263 }
264
265 void
find_five_times_finds_fifth(void ** state)266 find_five_times_finds_fifth(void** state)
267 {
268 roster_create();
269 roster_add("Jama", NULL, NULL, NULL, FALSE);
270 roster_add("Jamb", NULL, NULL, NULL, FALSE);
271 roster_add("Mike", NULL, NULL, NULL, FALSE);
272 roster_add("Dave", NULL, NULL, NULL, FALSE);
273 roster_add("Jamm", NULL, NULL, NULL, FALSE);
274 roster_add("Jamn", NULL, NULL, NULL, FALSE);
275 roster_add("Matt", NULL, NULL, NULL, FALSE);
276 roster_add("Jamo", NULL, NULL, NULL, FALSE);
277 roster_add("Jamy", NULL, NULL, NULL, FALSE);
278 roster_add("Jamz", NULL, NULL, NULL, FALSE);
279
280 char* result1 = roster_contact_autocomplete("Jam", FALSE, NULL);
281 char* result2 = roster_contact_autocomplete(result1, FALSE, NULL);
282 char* result3 = roster_contact_autocomplete(result2, FALSE, NULL);
283 char* result4 = roster_contact_autocomplete(result3, FALSE, NULL);
284 char* result5 = roster_contact_autocomplete(result4, FALSE, NULL);
285 assert_string_equal("Jamo", result5);
286 free(result1);
287 free(result2);
288 free(result3);
289 free(result4);
290 free(result5);
291 roster_destroy();
292 }
293
294 void
find_twice_returns_first_when_two_match_and_reset(void ** state)295 find_twice_returns_first_when_two_match_and_reset(void** state)
296 {
297 roster_create();
298 roster_add("James", NULL, NULL, NULL, FALSE);
299 roster_add("Jamie", NULL, NULL, NULL, FALSE);
300 roster_add("Bob", NULL, NULL, NULL, FALSE);
301
302 char* result1 = roster_contact_autocomplete("Jam", FALSE, NULL);
303 roster_reset_search_attempts();
304 char* result2 = roster_contact_autocomplete(result1, FALSE, NULL);
305 assert_string_equal("James", result2);
306 free(result1);
307 free(result2);
308 roster_destroy();
309 }
310
311 void
add_contact_with_no_group(void ** state)312 add_contact_with_no_group(void** state)
313 {
314 roster_create();
315 roster_add("person@server.org", NULL, NULL, NULL, FALSE);
316
317 GList* groups_res = roster_get_groups();
318 assert_int_equal(g_list_length(groups_res), 0);
319
320 g_list_free_full(groups_res, free);
321 roster_destroy();
322 }
323
324 void
add_contact_with_group(void ** state)325 add_contact_with_group(void** state)
326 {
327 roster_create();
328
329 GSList* groups = NULL;
330 groups = g_slist_append(groups, strdup("friends"));
331 roster_add("person@server.org", NULL, groups, NULL, FALSE);
332
333 GList* groups_res = roster_get_groups();
334 assert_int_equal(g_list_length(groups_res), 1);
335
336 GList* found = g_list_find_custom(groups_res, "friends", (GCompareFunc)g_strcmp0);
337 assert_true(found != NULL);
338 assert_string_equal(found->data, "friends");
339
340 g_list_free_full(groups_res, free);
341 roster_destroy();
342 }
343
344 void
add_contact_with_two_groups(void ** state)345 add_contact_with_two_groups(void** state)
346 {
347 roster_create();
348
349 GSList* groups = NULL;
350 groups = g_slist_append(groups, strdup("friends"));
351 groups = g_slist_append(groups, strdup("work"));
352 roster_add("person@server.org", NULL, groups, NULL, FALSE);
353
354 GList* groups_res = roster_get_groups();
355 assert_int_equal(g_list_length(groups_res), 2);
356
357 GList* found = g_list_find_custom(groups_res, "friends", (GCompareFunc)g_strcmp0);
358 assert_true(found != NULL);
359 assert_string_equal(found->data, "friends");
360 found = g_list_find_custom(groups_res, "work", (GCompareFunc)g_strcmp0);
361 assert_true(found != NULL);
362 assert_string_equal(found->data, "work");
363
364 g_list_free_full(groups_res, free);
365 roster_destroy();
366 }
367
368 void
add_contact_with_three_groups(void ** state)369 add_contact_with_three_groups(void** state)
370 {
371 roster_create();
372
373 GSList* groups = NULL;
374 groups = g_slist_append(groups, strdup("friends"));
375 groups = g_slist_append(groups, strdup("work"));
376 groups = g_slist_append(groups, strdup("stuff"));
377 roster_add("person@server.org", NULL, groups, NULL, FALSE);
378
379 GList* groups_res = roster_get_groups();
380 assert_int_equal(g_list_length(groups_res), 3);
381
382 GList* found = g_list_find_custom(groups_res, "friends", (GCompareFunc)g_strcmp0);
383 assert_true(found != NULL);
384 assert_string_equal(found->data, "friends");
385 found = g_list_find_custom(groups_res, "work", (GCompareFunc)g_strcmp0);
386 assert_true(found != NULL);
387 assert_string_equal(found->data, "work");
388 found = g_list_find_custom(groups_res, "stuff", (GCompareFunc)g_strcmp0);
389 assert_true(found != NULL);
390 assert_string_equal(found->data, "stuff");
391
392 g_list_free_full(groups_res, free);
393 roster_destroy();
394 }
395
396 void
add_contact_with_three_groups_update_adding_two(void ** state)397 add_contact_with_three_groups_update_adding_two(void** state)
398 {
399 roster_create();
400
401 GSList* groups1 = NULL;
402 groups1 = g_slist_append(groups1, strdup("friends"));
403 groups1 = g_slist_append(groups1, strdup("work"));
404 groups1 = g_slist_append(groups1, strdup("stuff"));
405 roster_add("person@server.org", NULL, groups1, NULL, FALSE);
406
407 GSList* groups2 = NULL;
408 groups2 = g_slist_append(groups2, strdup("friends"));
409 groups2 = g_slist_append(groups2, strdup("work"));
410 groups2 = g_slist_append(groups2, strdup("stuff"));
411 groups2 = g_slist_append(groups2, strdup("things"));
412 groups2 = g_slist_append(groups2, strdup("people"));
413 roster_update("person@server.org", NULL, groups2, NULL, FALSE);
414
415 GList* groups_res = roster_get_groups();
416 assert_int_equal(g_list_length(groups_res), 5);
417
418 GList* found = g_list_find_custom(groups_res, "friends", (GCompareFunc)g_strcmp0);
419 assert_true(found != NULL);
420 assert_string_equal(found->data, "friends");
421 found = g_list_find_custom(groups_res, "work", (GCompareFunc)g_strcmp0);
422 assert_true(found != NULL);
423 assert_string_equal(found->data, "work");
424 found = g_list_find_custom(groups_res, "stuff", (GCompareFunc)g_strcmp0);
425 assert_true(found != NULL);
426 assert_string_equal(found->data, "stuff");
427 found = g_list_find_custom(groups_res, "things", (GCompareFunc)g_strcmp0);
428 assert_true(found != NULL);
429 assert_string_equal(found->data, "things");
430 found = g_list_find_custom(groups_res, "people", (GCompareFunc)g_strcmp0);
431 assert_true(found != NULL);
432 assert_string_equal(found->data, "people");
433
434 g_list_free_full(groups_res, free);
435 roster_destroy();
436 }
437
438 void
add_contact_with_three_groups_update_removing_one(void ** state)439 add_contact_with_three_groups_update_removing_one(void** state)
440 {
441 roster_create();
442
443 GSList* groups1 = NULL;
444 groups1 = g_slist_append(groups1, strdup("friends"));
445 groups1 = g_slist_append(groups1, strdup("work"));
446 groups1 = g_slist_append(groups1, strdup("stuff"));
447 roster_add("person@server.org", NULL, groups1, NULL, FALSE);
448
449 GSList* groups2 = NULL;
450 groups2 = g_slist_append(groups2, strdup("friends"));
451 groups2 = g_slist_append(groups2, strdup("stuff"));
452 roster_update("person@server.org", NULL, groups2, NULL, FALSE);
453
454 GList* groups_res = roster_get_groups();
455 assert_int_equal(g_list_length(groups_res), 2);
456
457 GList* found = g_list_find_custom(groups_res, "friends", (GCompareFunc)g_strcmp0);
458 assert_true(found != NULL);
459 assert_string_equal(found->data, "friends");
460 found = g_list_find_custom(groups_res, "stuff", (GCompareFunc)g_strcmp0);
461 assert_true(found != NULL);
462 assert_string_equal(found->data, "stuff");
463
464 g_list_free_full(groups_res, free);
465 roster_destroy();
466 }
467
468 void
add_contact_with_three_groups_update_removing_two(void ** state)469 add_contact_with_three_groups_update_removing_two(void** state)
470 {
471 roster_create();
472
473 GSList* groups1 = NULL;
474 groups1 = g_slist_append(groups1, strdup("friends"));
475 groups1 = g_slist_append(groups1, strdup("work"));
476 groups1 = g_slist_append(groups1, strdup("stuff"));
477 roster_add("person@server.org", NULL, groups1, NULL, FALSE);
478
479 GSList* groups2 = NULL;
480 groups2 = g_slist_append(groups2, strdup("stuff"));
481 roster_update("person@server.org", NULL, groups2, NULL, FALSE);
482
483 GList* groups_res = roster_get_groups();
484 assert_int_equal(g_list_length(groups_res), 1);
485
486 GList* found = g_list_find_custom(groups_res, "stuff", (GCompareFunc)g_strcmp0);
487 assert_true(found != NULL);
488 assert_string_equal(found->data, "stuff");
489
490 g_list_free_full(groups_res, free);
491 roster_destroy();
492 }
493
494 void
add_contact_with_three_groups_update_removing_three(void ** state)495 add_contact_with_three_groups_update_removing_three(void** state)
496 {
497 roster_create();
498
499 GSList* groups1 = NULL;
500 groups1 = g_slist_append(groups1, strdup("friends"));
501 groups1 = g_slist_append(groups1, strdup("work"));
502 groups1 = g_slist_append(groups1, strdup("stuff"));
503 roster_add("person@server.org", NULL, groups1, NULL, FALSE);
504
505 roster_update("person@server.org", NULL, NULL, NULL, FALSE);
506
507 GList* groups_res = roster_get_groups();
508 assert_int_equal(g_list_length(groups_res), 0);
509
510 g_list_free_full(groups_res, free);
511 roster_destroy();
512 }
513
514 void
add_contact_with_three_groups_update_two_new(void ** state)515 add_contact_with_three_groups_update_two_new(void** state)
516 {
517 roster_create();
518
519 GSList* groups1 = NULL;
520 groups1 = g_slist_append(groups1, strdup("friends"));
521 groups1 = g_slist_append(groups1, strdup("work"));
522 groups1 = g_slist_append(groups1, strdup("stuff"));
523 roster_add("person@server.org", NULL, groups1, NULL, FALSE);
524
525 GSList* groups2 = NULL;
526 groups2 = g_slist_append(groups2, strdup("newfriends"));
527 groups2 = g_slist_append(groups2, strdup("somepeople"));
528 roster_update("person@server.org", NULL, groups2, NULL, FALSE);
529
530 GList* groups_res = roster_get_groups();
531 assert_int_equal(g_list_length(groups_res), 2);
532
533 GList* found = g_list_find_custom(groups_res, "newfriends", (GCompareFunc)g_strcmp0);
534 assert_true(found != NULL);
535 found = g_list_find_custom(groups_res, "somepeople", (GCompareFunc)g_strcmp0);
536 assert_true(found != NULL);
537
538 g_list_free_full(groups_res, free);
539 roster_destroy();
540 }
541
542 void
add_remove_contact_groups(void ** state)543 add_remove_contact_groups(void** state)
544 {
545 roster_create();
546
547 GSList* groups1 = NULL;
548 groups1 = g_slist_append(groups1, strdup("friends"));
549 groups1 = g_slist_append(groups1, strdup("work"));
550 groups1 = g_slist_append(groups1, strdup("stuff"));
551 roster_add("person@server.org", NULL, groups1, NULL, FALSE);
552
553 roster_remove("person@server.org", "person@server.org");
554
555 GList* groups_res = roster_get_groups();
556 assert_int_equal(g_list_length(groups_res), 0);
557
558 g_list_free_full(groups_res, free);
559 roster_destroy();
560 }
561
562 void
add_contacts_with_different_groups(void ** state)563 add_contacts_with_different_groups(void** state)
564 {
565 roster_create();
566
567 GSList* groups1 = NULL;
568 groups1 = g_slist_append(groups1, strdup("friends"));
569 groups1 = g_slist_append(groups1, strdup("work"));
570 groups1 = g_slist_append(groups1, strdup("stuff"));
571 roster_add("person@server.org", NULL, groups1, NULL, FALSE);
572
573 GSList* groups2 = NULL;
574 groups2 = g_slist_append(groups2, strdup("newfriends"));
575 groups2 = g_slist_append(groups2, strdup("somepeople"));
576 roster_add("bob@server.org", NULL, groups2, NULL, FALSE);
577
578 GList* groups_res = roster_get_groups();
579 assert_int_equal(g_list_length(groups_res), 5);
580
581 GList* found = g_list_find_custom(groups_res, "friends", (GCompareFunc)g_strcmp0);
582 assert_true(found != NULL);
583 found = g_list_find_custom(groups_res, "work", (GCompareFunc)g_strcmp0);
584 assert_true(found != NULL);
585 found = g_list_find_custom(groups_res, "stuff", (GCompareFunc)g_strcmp0);
586 assert_true(found != NULL);
587 found = g_list_find_custom(groups_res, "newfriends", (GCompareFunc)g_strcmp0);
588 assert_true(found != NULL);
589 found = g_list_find_custom(groups_res, "somepeople", (GCompareFunc)g_strcmp0);
590 assert_true(found != NULL);
591
592 g_list_free_full(groups_res, free);
593 roster_destroy();
594 }
595
596 void
add_contacts_with_same_groups(void ** state)597 add_contacts_with_same_groups(void** state)
598 {
599 roster_create();
600
601 GSList* groups1 = NULL;
602 groups1 = g_slist_append(groups1, strdup("friends"));
603 groups1 = g_slist_append(groups1, strdup("work"));
604 groups1 = g_slist_append(groups1, strdup("stuff"));
605 roster_add("person@server.org", NULL, groups1, NULL, FALSE);
606
607 GSList* groups2 = NULL;
608 groups2 = g_slist_append(groups2, strdup("friends"));
609 groups2 = g_slist_append(groups2, strdup("work"));
610 groups2 = g_slist_append(groups2, strdup("stuff"));
611 roster_add("bob@server.org", NULL, groups2, NULL, FALSE);
612
613 GList* groups_res = roster_get_groups();
614 assert_int_equal(g_list_length(groups_res), 3);
615
616 GList* found = g_list_find_custom(groups_res, "friends", (GCompareFunc)g_strcmp0);
617 assert_true(found != NULL);
618 found = g_list_find_custom(groups_res, "work", (GCompareFunc)g_strcmp0);
619 assert_true(found != NULL);
620 found = g_list_find_custom(groups_res, "stuff", (GCompareFunc)g_strcmp0);
621 assert_true(found != NULL);
622
623 g_list_free_full(groups_res, free);
624 roster_destroy();
625 }
626
627 void
add_contacts_with_overlapping_groups(void ** state)628 add_contacts_with_overlapping_groups(void** state)
629 {
630 roster_create();
631
632 GSList* groups1 = NULL;
633 groups1 = g_slist_append(groups1, strdup("friends"));
634 groups1 = g_slist_append(groups1, strdup("work"));
635 groups1 = g_slist_append(groups1, strdup("stuff"));
636 roster_add("person@server.org", NULL, groups1, NULL, FALSE);
637
638 GSList* groups2 = NULL;
639 groups2 = g_slist_append(groups2, strdup("friends"));
640 groups2 = g_slist_append(groups2, strdup("work"));
641 groups2 = g_slist_append(groups2, strdup("different"));
642 roster_add("bob@server.org", NULL, groups2, NULL, FALSE);
643
644 GList* groups_res = roster_get_groups();
645 assert_int_equal(g_list_length(groups_res), 4);
646
647 GList* found = g_list_find_custom(groups_res, "friends", (GCompareFunc)g_strcmp0);
648 assert_true(found != NULL);
649 found = g_list_find_custom(groups_res, "work", (GCompareFunc)g_strcmp0);
650 assert_true(found != NULL);
651 found = g_list_find_custom(groups_res, "stuff", (GCompareFunc)g_strcmp0);
652 assert_true(found != NULL);
653 found = g_list_find_custom(groups_res, "different", (GCompareFunc)g_strcmp0);
654 assert_true(found != NULL);
655
656 g_list_free_full(groups_res, free);
657 roster_destroy();
658 }
659
660 void
remove_contact_with_remaining_in_group(void ** state)661 remove_contact_with_remaining_in_group(void** state)
662 {
663 roster_create();
664
665 GSList* groups1 = NULL;
666 groups1 = g_slist_append(groups1, strdup("friends"));
667 groups1 = g_slist_append(groups1, strdup("work"));
668 groups1 = g_slist_append(groups1, strdup("stuff"));
669 roster_add("person@server.org", NULL, groups1, NULL, FALSE);
670
671 GSList* groups2 = NULL;
672 groups2 = g_slist_append(groups2, strdup("friends"));
673 groups2 = g_slist_append(groups2, strdup("work"));
674 groups2 = g_slist_append(groups2, strdup("different"));
675 roster_add("bob@server.org", NULL, groups2, NULL, FALSE);
676
677 roster_remove("bob@server.org", "bob@server.org");
678
679 GList* groups_res = roster_get_groups();
680 assert_int_equal(g_list_length(groups_res), 3);
681
682 GList* found = g_list_find_custom(groups_res, "friends", (GCompareFunc)g_strcmp0);
683 assert_true(found != NULL);
684 found = g_list_find_custom(groups_res, "work", (GCompareFunc)g_strcmp0);
685 assert_true(found != NULL);
686 found = g_list_find_custom(groups_res, "stuff", (GCompareFunc)g_strcmp0);
687 assert_true(found != NULL);
688
689 g_list_free_full(groups_res, free);
690 roster_destroy();
691 }
692
693 void
get_contact_display_name(void ** state)694 get_contact_display_name(void** state)
695 {
696 roster_create();
697 roster_add("person@server.org", "nickname", NULL, NULL, FALSE);
698
699 assert_string_equal("nickname", roster_get_display_name("person@server.org"));
700
701 roster_destroy();
702 }
703
704 void
get_contact_display_name_is_barejid_if_name_is_empty(void ** state)705 get_contact_display_name_is_barejid_if_name_is_empty(void** state)
706 {
707 roster_create();
708 roster_add("person@server.org", NULL, NULL, NULL, FALSE);
709
710 assert_string_equal("person@server.org", roster_get_display_name("person@server.org"));
711
712 roster_destroy();
713 }
714
715 void
get_contact_display_name_is_passed_barejid_if_contact_does_not_exist(void ** state)716 get_contact_display_name_is_passed_barejid_if_contact_does_not_exist(void** state)
717 {
718 roster_create();
719
720 assert_string_equal("person@server.org", roster_get_display_name("person@server.org"));
721
722 roster_destroy();
723 }
724