summaryrefslogtreecommitdiff
path: root/auto_tests/messenger_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'auto_tests/messenger_test.c')
-rw-r--r--auto_tests/messenger_test.c279
1 files changed, 279 insertions, 0 deletions
diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c
new file mode 100644
index 00000000..deed498f
--- /dev/null
+++ b/auto_tests/messenger_test.c
@@ -0,0 +1,279 @@
1/* unit tests for /core/Messenger.c
2 * Design:
3 * Just call every non-static function in Messenger.c, checking that
4 * they return as they should with check calls. "Bad" calls of the type
5 * function(bad_data, good_length) are _not_ checked for, this type
6 * of call is the fault of the client code.
7 *
8 * Note:
9 * None of the functions here test things that rely on the network, i.e.
10 * checking that status changes are recieved, messages can be sent, etc.
11 * All of that is done in a separate test, with two local clients running. */
12
13#include "../core/Messenger.h"
14#include <sys/types.h>
15#include <stdint.h>
16#include <string.h>
17#include <check.h>
18
19#define REALLY_BIG_NUMBER ((1) << (sizeof(uint16_t) * 7))
20#define STRINGS_EQUAL(X, Y) (strcmp(X, Y) == 0)
21
22char *friend_id_str = "1145e295b0fbdc9330d5d74ec204a8bf23c315106040b4035d0d358d07ee3f7d";
23
24/* in case we need more than one ID for a test */
25char *good_id_a_str = "DB9B569D14850ED8364C3744CAC2C8FF78985D213E980C7C508D0E91E8E45441";
26char *good_id_b_str = "d3f14b6d384d8f5f2a66cff637e69f28f539c5de61bc29744785291fa4ef4d64";
27
28char *bad_id_str = "9B569D14ff637e69f2";
29
30unsigned char *friend_id = NULL;
31unsigned char *good_id_a = NULL;
32unsigned char *good_id_b = NULL;
33unsigned char *bad_id = NULL;
34
35int friend_id_num = 0;
36
37unsigned char * hex_string_to_bin(char hex_string[])
38{
39 size_t len = strlen(hex_string);
40 unsigned char *val = calloc(1, len);
41 char *pos = hex_string;
42 int i = 0;
43 for(i = 0; i < len; ++i, pos+=2)
44 sscanf(pos,"%2hhx",&val[i]);
45 return val;
46}
47
48START_TEST(test_m_sendmesage)
49{
50 char *message = "h-hi :3";
51 int good_len = strlen(message);
52 int bad_len = MAX_DATA_SIZE;
53
54
55 ck_assert(m_sendmessage(-1, (uint8_t *)message, good_len) == 0);
56 ck_assert(m_sendmessage(REALLY_BIG_NUMBER, (uint8_t *)message, good_len) == 0);
57 ck_assert(m_sendmessage(17, (uint8_t *)message, good_len) == 0);
58 ck_assert(m_sendmessage(friend_id_num, (uint8_t *)message, bad_len) == 0);
59}
60END_TEST
61
62START_TEST(test_m_get_userstatus_size)
63{
64 int rc = 0;
65 ck_assert_msg((m_get_userstatus_size(-1) == -1),
66 "m_get_userstatus_size did NOT catch an argument of -1");
67 ck_assert_msg((m_get_userstatus_size(REALLY_BIG_NUMBER) == -1),
68 "m_get_userstatus_size did NOT catch the following argument: %d\n",
69 REALLY_BIG_NUMBER);
70 rc = m_get_userstatus_size(friend_id_num);
71
72 /* this WILL error if the original m_addfriend_norequest() failed */
73 ck_assert_msg((rc > 0 && rc <= MAX_USERSTATUS_LENGTH),
74 "m_get_userstatus_size is returning out of range values!\n"
75 "(this can be caused by the error of m_addfriend_norequest"
76 " in the beginning of the suite)\n");
77}
78END_TEST
79
80START_TEST(test_m_set_userstatus)
81{
82 char *status = "online!";
83 uint16_t good_length = strlen(status);
84 uint16_t bad_length = REALLY_BIG_NUMBER;
85
86 if(m_set_userstatus(USERSTATUS_KIND_ONLINE,
87 (uint8_t *)status, bad_length) != -1)
88 ck_abort_msg("m_set_userstatus did NOT catch the following length: %d\n",
89 REALLY_BIG_NUMBER);
90
91 if((m_set_userstatus(USERSTATUS_KIND_RETAIN,
92 (uint8_t *)status, good_length)) != 0)
93 ck_abort_msg("m_set_userstatus did NOT return 0 on the following length: %d\n"
94 "MAX_USERSTATUS_LENGTH: %d\n", good_length, MAX_USERSTATUS_LENGTH);
95}
96END_TEST
97
98START_TEST(test_m_friendstatus)
99{
100 ck_assert_msg((m_friendstatus(-1) == NOFRIEND),
101 "m_friendstatus did NOT catch an argument of -1.\n");
102 ck_assert_msg((m_friendstatus(REALLY_BIG_NUMBER) == NOFRIEND),
103 "m_friendstatus did NOT catch an argument of %d.\n",
104 REALLY_BIG_NUMBER);
105}
106END_TEST
107
108START_TEST(test_m_delfriend)
109{
110 ck_assert_msg((m_delfriend(-1) == -1),
111 "m_delfriend did NOT catch an argument of -1\n");
112 ck_assert_msg((m_delfriend(REALLY_BIG_NUMBER) == -1),
113 "m_delfriend did NOT catch the following number: %d\n",
114 REALLY_BIG_NUMBER);
115}
116END_TEST
117
118START_TEST(test_m_addfriend)
119{
120 char *good_data = "test";
121 char *bad_data = "";
122
123 int good_len = strlen(good_data);
124 int bad_len = strlen(bad_data);
125 int really_bad_len = (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
126 - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
127 + crypto_box_ZEROBYTES + 100);
128
129 if(m_addfriend((uint8_t *)friend_id, (uint8_t *)good_data, really_bad_len) != FAERR_TOOLONG)
130 ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", really_bad_len);
131
132 /* this will error if the original m_addfriend_norequest() failed */
133 if(m_addfriend((uint8_t *)friend_id, (uint8_t *)good_data, good_len) != FAERR_ALREADYSENT)
134 ck_abort_msg("m_addfriend did NOT catch adding a friend we already have.\n"
135 "(this can be caused by the error of m_addfriend_norequest in"
136 " the beginning of the suite)\n");
137
138 if(m_addfriend((uint8_t *)good_id_b, (uint8_t *)bad_data, bad_len) != FAERR_NOMESSAGE)
139 ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", bad_len);
140
141 /* this should REALLY error */
142 if(m_addfriend((uint8_t *)bad_id, (uint8_t *)good_data, good_len) >= 0)
143 ck_abort_msg("The following ID passed through "
144 "m_addfriend without an error:\n'%s'\n", bad_id_str);
145}
146END_TEST
147
148START_TEST(test_setname)
149{
150 char *good_name = "consensualCorn";
151 int good_length = strlen(good_name);
152 int bad_length = REALLY_BIG_NUMBER;
153
154 if(setname((uint8_t *)good_name, bad_length) != -1)
155 ck_abort_msg("setname() did NOT error on %d as a length argument!\n",
156 bad_length);
157 if(setname((uint8_t *)good_name, good_length) != 0)
158 ck_abort_msg("setname() did NOT return 0 on good arguments!\n");
159}
160END_TEST
161
162START_TEST(test_getself_name)
163{
164 char *nickname = "testGallop";
165 int len = strlen(nickname);
166 char nick_check[len];
167
168 setname((uint8_t *)nickname, len);
169 getself_name((uint8_t *)nick_check);
170
171 ck_assert_msg((!STRINGS_EQUAL(nickname, nick_check)),
172 "getself_name failed to return the known name!\n"
173 "known name: %s\nreturned: %s\n", nickname, nick_check);
174}
175END_TEST
176
177/* this test is excluded for now, due to lack of a way
178 * to set a friend's status for now.
179 * ideas:
180 * if we have access to the friends list, we could
181 * just add a status manually ourselves. */
182/*
183START_TEST(test_m_copy_userstatus)
184{
185 assert(m_copy_userstatus(-1, buf, MAX_USERSTATUS_LENGTH) == -1);
186 assert(m_copy_userstatus(REALLY_BIG_NUMBER, buf, MAX_USERSTATUS_LENGTH) == -1);
187 m_copy_userstatus(friend_id_num, buf, MAX_USERSTATUS_LENGTH + 6);
188
189 assert(STRINGS_EQUAL(name_buf, friend_id_status));
190}
191END_TEST
192*/
193
194/* this test is excluded for now, due to lack of a way
195 * to set a friend's nickname for now.
196 * ideas:
197 * if we have access to the friends list, we could
198 * just add a name manually ourselves. */
199/*
200START_TEST(test_getname)
201{
202 uint8_t name_buf[MAX_NAME_LENGTH];
203
204 assert(getname(-1, name_buf) == -1);
205 assert(getname(REALLY_BIG_NUMBER, name_buf) == -1);
206
207 getname(friend_id_num, name_buf);
208 assert(name_buf[MAX_NAME_LENGTH] == '\0'); // something like this
209}
210END_TEST
211*/
212
213Suite *messenger_suite(void)
214{
215 Suite *s = suite_create("Messenger");
216
217 TCase *userstatus_size = tcase_create("userstatus_size");
218 TCase *set_userstatus = tcase_create("set_userstatus");
219 TCase *send_message = tcase_create("send_message");
220 TCase *friendstatus = tcase_create("friendstatus");
221 TCase *getself_name = tcase_create("getself_name");
222 TCase *delfriend = tcase_create("delfriend");
223 TCase *addfriend = tcase_create("addfriend");
224 TCase *setname = tcase_create("setname");
225
226 tcase_add_test(userstatus_size, test_m_get_userstatus_size);
227 tcase_add_test(set_userstatus, test_m_set_userstatus);
228 tcase_add_test(friendstatus, test_m_friendstatus);
229 tcase_add_test(getself_name, test_getself_name);
230 tcase_add_test(send_message, test_m_sendmesage);
231 tcase_add_test(delfriend, test_m_delfriend);
232 tcase_add_test(addfriend, test_m_addfriend);
233 tcase_add_test(setname, test_setname);
234
235 suite_add_tcase(s, userstatus_size);
236 suite_add_tcase(s, set_userstatus);
237 suite_add_tcase(s, friendstatus);
238 suite_add_tcase(s, send_message);
239 suite_add_tcase(s, getself_name);
240 suite_add_tcase(s, delfriend);
241 suite_add_tcase(s, addfriend);
242 suite_add_tcase(s, setname);
243
244 return s;
245}
246
247int main(int argc, char *argv[])
248{
249 Suite *messenger = messenger_suite();
250 SRunner *test_runner = srunner_create(messenger);
251 int number_failed = 0;
252
253 friend_id = hex_string_to_bin(friend_id_str);
254 good_id_a = hex_string_to_bin(good_id_a_str);
255 good_id_b = hex_string_to_bin(good_id_b_str);
256 bad_id = hex_string_to_bin(bad_id_str);
257
258 /* setup a default friend and friendnum */
259 if(m_addfriend_norequest((uint8_t *)friend_id) < 0)
260 fputs("m_addfriend_norequest() failed on a valid ID!\n"
261 "this was CRITICAL to the test, and the build WILL fail.\n"
262 "the tests will continue now...\n\n", stderr);
263
264 if((friend_id_num = getfriend_id((uint8_t *)friend_id)) < 0)
265 fputs("getfriend_id() failed on a valid ID!\n"
266 "this was CRITICAL to the test, and the build WILL fail.\n"
267 "the tests will continue now...\n\n", stderr);
268
269 srunner_run_all(test_runner, CK_NORMAL);
270 number_failed = srunner_ntests_failed(test_runner);
271
272 srunner_free(test_runner);
273 free(friend_id);
274 free(good_id_a);
275 free(good_id_b);
276 free(bad_id);
277
278 return number_failed;
279}