1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <glib.h>
8 
9 #include "ui/ui.h"
10 #include "ui/stub_ui.h"
11 
12 #include "xmpp/xmpp.h"
13 #include "xmpp/roster_list.h"
14 #include "command/cmd_funcs.h"
15 
16 #define CMD_ROSTER "/roster"
17 
18 static void
test_with_connection_status(jabber_conn_status_t status)19 test_with_connection_status(jabber_conn_status_t status)
20 {
21     gchar* args[] = { NULL };
22 
23     will_return(connection_get_status, status);
24 
25     expect_cons_show("You are not currently connected.");
26 
27     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
28     assert_true(result);
29 }
30 
31 void
cmd_roster_shows_message_when_disconnecting(void ** state)32 cmd_roster_shows_message_when_disconnecting(void** state)
33 {
34     test_with_connection_status(JABBER_DISCONNECTING);
35 }
36 
37 void
cmd_roster_shows_message_when_connecting(void ** state)38 cmd_roster_shows_message_when_connecting(void** state)
39 {
40     test_with_connection_status(JABBER_CONNECTING);
41 }
42 
43 void
cmd_roster_shows_message_when_disconnected(void ** state)44 cmd_roster_shows_message_when_disconnected(void** state)
45 {
46     test_with_connection_status(JABBER_DISCONNECTED);
47 }
48 
49 void
cmd_roster_shows_roster_when_no_args(void ** state)50 cmd_roster_shows_roster_when_no_args(void** state)
51 {
52     gchar* args[] = { NULL };
53 
54     will_return(connection_get_status, JABBER_CONNECTED);
55 
56     roster_create();
57     roster_add("bob@server.org", "bob", NULL, "both", FALSE);
58     GSList* roster = roster_get_contacts(ROSTER_ORD_NAME);
59 
60     expect_memory(cons_show_roster, list, roster, sizeof(roster));
61 
62     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
63     assert_true(result);
64 
65     g_slist_free(roster);
66     roster_destroy();
67 }
68 
69 void
cmd_roster_add_shows_message_when_no_jid(void ** state)70 cmd_roster_add_shows_message_when_no_jid(void** state)
71 {
72     gchar* args[] = { "add", NULL };
73 
74     will_return(connection_get_status, JABBER_CONNECTED);
75 
76     expect_string(cons_bad_cmd_usage, cmd, CMD_ROSTER);
77 
78     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
79     assert_true(result);
80 }
81 
82 void
cmd_roster_add_sends_roster_add_request(void ** state)83 cmd_roster_add_sends_roster_add_request(void** state)
84 {
85     char* jid = "bob@server.org";
86     char* nick = "bob";
87     gchar* args[] = { "add", jid, nick, NULL };
88 
89     will_return(connection_get_status, JABBER_CONNECTED);
90 
91     expect_string(roster_send_add_new, barejid, jid);
92     expect_string(roster_send_add_new, name, nick);
93 
94     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
95     assert_true(result);
96 }
97 
98 void
cmd_roster_remove_shows_message_when_no_jid(void ** state)99 cmd_roster_remove_shows_message_when_no_jid(void** state)
100 {
101     gchar* args[] = { "remove", NULL };
102 
103     will_return(connection_get_status, JABBER_CONNECTED);
104 
105     expect_string(cons_bad_cmd_usage, cmd, CMD_ROSTER);
106 
107     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
108     assert_true(result);
109 }
110 
111 void
cmd_roster_remove_sends_roster_remove_request(void ** state)112 cmd_roster_remove_sends_roster_remove_request(void** state)
113 {
114     char* jid = "bob@server.org";
115     gchar* args[] = { "remove", jid, NULL };
116 
117     will_return(connection_get_status, JABBER_CONNECTED);
118 
119     expect_string(roster_send_remove, barejid, jid);
120 
121     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
122     assert_true(result);
123 }
124 
125 void
cmd_roster_nick_shows_message_when_no_jid(void ** state)126 cmd_roster_nick_shows_message_when_no_jid(void** state)
127 {
128     gchar* args[] = { "nick", NULL };
129 
130     will_return(connection_get_status, JABBER_CONNECTED);
131 
132     expect_string(cons_bad_cmd_usage, cmd, CMD_ROSTER);
133 
134     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
135     assert_true(result);
136 }
137 
138 void
cmd_roster_nick_shows_message_when_no_nick(void ** state)139 cmd_roster_nick_shows_message_when_no_nick(void** state)
140 {
141     gchar* args[] = { "nick", "bob@server.org", NULL };
142 
143     will_return(connection_get_status, JABBER_CONNECTED);
144 
145     expect_string(cons_bad_cmd_usage, cmd, CMD_ROSTER);
146 
147     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
148     assert_true(result);
149 }
150 
151 void
cmd_roster_nick_shows_message_when_no_contact_exists(void ** state)152 cmd_roster_nick_shows_message_when_no_contact_exists(void** state)
153 {
154     gchar* args[] = { "nick", "bob@server.org", "bobster", NULL };
155 
156     roster_create();
157 
158     will_return(connection_get_status, JABBER_CONNECTED);
159 
160     expect_cons_show("Contact not found in roster: bob@server.org");
161 
162     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
163     assert_true(result);
164 
165     roster_destroy();
166 }
167 
168 void
cmd_roster_nick_sends_name_change_request(void ** state)169 cmd_roster_nick_sends_name_change_request(void** state)
170 {
171     char* jid = "bob@server.org";
172     char* nick = "bobster";
173     gchar* args[] = { "nick", jid, nick, NULL };
174 
175     roster_create();
176     GSList* groups = NULL;
177     groups = g_slist_append(groups, strdup("group1"));
178     roster_add(jid, "bob", groups, "both", FALSE);
179 
180     will_return(connection_get_status, JABBER_CONNECTED);
181 
182     expect_string(roster_send_name_change, barejid, jid);
183     expect_string(roster_send_name_change, new_name, nick);
184     expect_memory(roster_send_name_change, groups, groups, sizeof(groups));
185 
186     expect_cons_show("Nickname for bob@server.org set to: bobster.");
187 
188     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
189     assert_true(result);
190 
191     PContact contact = roster_get_contact(jid);
192     assert_string_equal(p_contact_name(contact), nick);
193 
194     roster_destroy();
195 }
196 
197 void
cmd_roster_clearnick_shows_message_when_no_jid(void ** state)198 cmd_roster_clearnick_shows_message_when_no_jid(void** state)
199 {
200     gchar* args[] = { "clearnick", NULL };
201 
202     will_return(connection_get_status, JABBER_CONNECTED);
203 
204     expect_string(cons_bad_cmd_usage, cmd, CMD_ROSTER);
205 
206     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
207     assert_true(result);
208 }
209 
210 void
cmd_roster_clearnick_shows_message_when_no_contact_exists(void ** state)211 cmd_roster_clearnick_shows_message_when_no_contact_exists(void** state)
212 {
213     gchar* args[] = { "clearnick", "bob@server.org", NULL };
214 
215     roster_create();
216 
217     will_return(connection_get_status, JABBER_CONNECTED);
218 
219     expect_cons_show("Contact not found in roster: bob@server.org");
220 
221     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
222     assert_true(result);
223 
224     roster_destroy();
225 }
226 
227 void
cmd_roster_clearnick_sends_name_change_request_with_empty_nick(void ** state)228 cmd_roster_clearnick_sends_name_change_request_with_empty_nick(void** state)
229 {
230     char* jid = "bob@server.org";
231     gchar* args[] = { "clearnick", jid, NULL };
232 
233     roster_create();
234     GSList* groups = NULL;
235     groups = g_slist_append(groups, strdup("group1"));
236     roster_add(jid, "bob", groups, "both", FALSE);
237 
238     will_return(connection_get_status, JABBER_CONNECTED);
239 
240     expect_string(roster_send_name_change, barejid, jid);
241     expect_value(roster_send_name_change, new_name, NULL);
242     expect_memory(roster_send_name_change, groups, groups, sizeof(groups));
243 
244     expect_cons_show("Nickname for bob@server.org removed.");
245 
246     gboolean result = cmd_roster(NULL, CMD_ROSTER, args);
247     assert_true(result);
248 
249     PContact contact = roster_get_contact(jid);
250     assert_null(p_contact_name(contact));
251 
252     roster_destroy();
253 }
254