summaryrefslogtreecommitdiff
path: root/auto_tests/set_status_message_test.c
diff options
context:
space:
mode:
authorRobin Lindén <dev@robinlinden.eu>2018-02-18 01:57:45 +0100
committeriphydf <iphydf@users.noreply.github.com>2018-02-18 14:50:18 +0000
commitc3515c49e012f76c5d2ca104476e372d924a79f9 (patch)
tree56512d94a79ad08d768ea51afa735c56833338b1 /auto_tests/set_status_message_test.c
parent13706de14bce8f8bfd1cb7db226d12cbd927b1b4 (diff)
Split up tox_test into multiple smaller tests
Diffstat (limited to 'auto_tests/set_status_message_test.c')
-rw-r--r--auto_tests/set_status_message_test.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/auto_tests/set_status_message_test.c b/auto_tests/set_status_message_test.c
new file mode 100644
index 00000000..f1508720
--- /dev/null
+++ b/auto_tests/set_status_message_test.c
@@ -0,0 +1,113 @@
1/* Tests that we can set our status message
2 */
3
4#ifndef _XOPEN_SOURCE
5#define _XOPEN_SOURCE 600
6#endif
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "check_compat.h"
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <time.h>
17
18#include "../toxcore/ccompat.h"
19#include "../toxcore/tox.h"
20#include "../toxcore/util.h"
21
22#include "helpers.h"
23
24#define STATUS_MESSAGE "Installing Gentoo"
25
26static void status_callback(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length, void *user_data)
27{
28 ck_assert_msg(length == sizeof(STATUS_MESSAGE) &&
29 memcmp(message, STATUS_MESSAGE, sizeof(STATUS_MESSAGE)) == 0,
30 "incorrect data in status callback");
31 bool *status_updated = (bool *)user_data;
32 *status_updated = true;
33}
34
35static void test_set_status_message(void)
36{
37 printf("initialising 2 toxes\n");
38 uint32_t index[] = { 1, 2 };
39 const time_t cur_time = time(nullptr);
40 Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
41 Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);
42
43 ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");
44
45 printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
46 uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
47 tox_self_get_public_key(tox2, public_key);
48 tox_friend_add_norequest(tox1, public_key, nullptr);
49 tox_self_get_public_key(tox1, public_key);
50 tox_friend_add_norequest(tox2, public_key, nullptr);
51
52 printf("bootstrapping tox2 off tox1\n");
53 uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
54 tox_self_get_dht_id(tox1, dht_key);
55 const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
56
57 tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
58
59 while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
60 tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) {
61 tox_iterate(tox1, nullptr);
62 tox_iterate(tox2, nullptr);
63
64 c_sleep(ITERATION_INTERVAL);
65 }
66
67 printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
68 const time_t con_time = time(nullptr);
69
70 while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
71 tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
72 tox_iterate(tox1, nullptr);
73 tox_iterate(tox2, nullptr);
74
75 c_sleep(ITERATION_INTERVAL);
76 }
77
78 printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);
79
80 TOX_ERR_SET_INFO err_n;
81 tox_callback_friend_status_message(tox2, status_callback);
82 bool ret = tox_self_set_status_message(tox1, (const uint8_t *)STATUS_MESSAGE, sizeof(STATUS_MESSAGE),
83 &err_n);
84 ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_status_message failed because %u\n", err_n);
85
86 bool status_updated = false;
87
88 while (!status_updated) {
89 tox_iterate(tox1, nullptr);
90 tox_iterate(tox2, &status_updated);
91 c_sleep(ITERATION_INTERVAL);
92 }
93
94 ck_assert_msg(tox_friend_get_status_message_size(tox2, 0, nullptr) == sizeof(STATUS_MESSAGE),
95 "status message length not correct");
96 uint8_t cmp_status[sizeof(STATUS_MESSAGE)];
97 tox_friend_get_status_message(tox2, 0, cmp_status, nullptr);
98 ck_assert_msg(memcmp(cmp_status, STATUS_MESSAGE, sizeof(STATUS_MESSAGE)) == 0,
99 "status message not correct");
100
101 printf("test_set_status_message succeeded, took %ld seconds\n", time(nullptr) - cur_time);
102
103 tox_kill(tox1);
104 tox_kill(tox2);
105}
106
107int main(int argc, char *argv[])
108{
109 setvbuf(stdout, nullptr, _IONBF, 0);
110
111 test_set_status_message();
112 return 0;
113}