/work/auto_tests/set_name_test.c
Line | Count | Source |
1 | | /* Tests that we can set our name. |
2 | | */ |
3 | | |
4 | | #include <stdio.h> |
5 | | #include <stdlib.h> |
6 | | #include <string.h> |
7 | | #include <time.h> |
8 | | |
9 | | #include "../testing/misc_tools.h" |
10 | | #include "../toxcore/ccompat.h" |
11 | | #include "../toxcore/tox.h" |
12 | | #include "../toxcore/util.h" |
13 | | #include "auto_test_support.h" |
14 | | #include "check_compat.h" |
15 | | |
16 | 2 | #define NICKNAME "Gentoo" |
17 | | |
18 | | static void nickchange_callback(Tox *tox, const Tox_Event_Friend_Name *event, void *user_data) |
19 | 1 | { |
20 | | //const uint32_t friend_number = tox_event_friend_name_get_friend_number(event); |
21 | 1 | const uint8_t* name = tox_event_friend_name_get_name(event); |
22 | 1 | const uint32_t name_length = tox_event_friend_name_get_name_length(event); |
23 | | |
24 | 1 | ck_assert_msg(name_length == sizeof(NICKNAME), "Name length not correct: %d != %d", (uint16_t)name_length, |
25 | 1 | (uint16_t)sizeof(NICKNAME)); |
26 | 1 | ck_assert_msg(memcmp(name, NICKNAME, sizeof(NICKNAME)) == 0, "Name not correct: %s", (const char *)name); |
27 | 1 | bool *nickname_updated = (bool *)user_data; |
28 | 1 | *nickname_updated = true; |
29 | 1 | } |
30 | | |
31 | | static void test_set_name(void) |
32 | 1 | { |
33 | 1 | printf("initialising 2 toxes\n"); |
34 | 1 | uint32_t index[] = { 1, 2 }; |
35 | 1 | const time_t cur_time = time(nullptr); |
36 | 1 | Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]); |
37 | 1 | Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]); |
38 | | |
39 | 1 | ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances"); |
40 | | |
41 | | // we only run events on tox2 in this test case |
42 | 1 | tox_events_init(tox2); |
43 | | |
44 | 1 | Tox_Dispatch *dispatch2 = tox_dispatch_new(nullptr); |
45 | 1 | ck_assert(dispatch2 != nullptr); |
46 | | |
47 | 1 | printf("tox1 adds tox2 as friend, tox2 adds tox1\n"); |
48 | 1 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; |
49 | 1 | tox_self_get_public_key(tox2, public_key); |
50 | 1 | tox_friend_add_norequest(tox1, public_key, nullptr); |
51 | 1 | tox_self_get_public_key(tox1, public_key); |
52 | 1 | tox_friend_add_norequest(tox2, public_key, nullptr); |
53 | | |
54 | 1 | printf("bootstrapping tox2 off tox1\n"); |
55 | 1 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; |
56 | 1 | tox_self_get_dht_id(tox1, dht_key); |
57 | 1 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); |
58 | | |
59 | 1 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); |
60 | | |
61 | 41 | do { |
62 | 41 | tox_iterate(tox1, nullptr); |
63 | | |
64 | 41 | Tox_Err_Events_Iterate err = TOX_ERR_EVENTS_ITERATE_OK; |
65 | 41 | Tox_Events *events = tox_events_iterate(tox2, true, &err); |
66 | 41 | ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK); |
67 | | //tox_dispatch_invoke(dispatch2, events, tox2, nullptr); |
68 | 41 | tox_events_free(events); |
69 | | |
70 | 41 | c_sleep(ITERATION_INTERVAL); |
71 | 41 | } while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || |
72 | 41 | tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE); |
73 | | |
74 | 1 | printf("toxes are online, took %lu seconds\n", (unsigned long)(time(nullptr) - cur_time)); |
75 | 1 | const time_t con_time = time(nullptr); |
76 | | |
77 | 14 | do { |
78 | 14 | tox_iterate(tox1, nullptr); |
79 | | |
80 | 14 | Tox_Err_Events_Iterate err = TOX_ERR_EVENTS_ITERATE_OK; |
81 | 14 | Tox_Events *events = tox_events_iterate(tox2, true, &err); |
82 | 14 | ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK); |
83 | | //tox_dispatch_invoke(dispatch2, events, tox2, nullptr); |
84 | 14 | tox_events_free(events); |
85 | | |
86 | 14 | c_sleep(ITERATION_INTERVAL); |
87 | 14 | } while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP || |
88 | 14 | tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP); |
89 | | |
90 | 1 | printf("tox clients connected took %lu seconds\n", (unsigned long)(time(nullptr) - con_time)); |
91 | | |
92 | 1 | tox_events_callback_friend_name(dispatch2, nickchange_callback); |
93 | 1 | Tox_Err_Set_Info err_n; |
94 | 1 | bool ret = tox_self_set_name(tox1, (const uint8_t *)NICKNAME, sizeof(NICKNAME), &err_n); |
95 | 1 | ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_name failed because %d\n", err_n); |
96 | | |
97 | 1 | bool nickname_updated = false; |
98 | | |
99 | 1 | do { |
100 | 1 | tox_iterate(tox1, nullptr); |
101 | | |
102 | 1 | Tox_Err_Events_Iterate err = TOX_ERR_EVENTS_ITERATE_OK; |
103 | 1 | Tox_Events *events = tox_events_iterate(tox2, true, &err); |
104 | 1 | ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK); |
105 | 1 | tox_dispatch_invoke(dispatch2, events, tox2, &nickname_updated); |
106 | 1 | tox_events_free(events); |
107 | | |
108 | 1 | c_sleep(ITERATION_INTERVAL); |
109 | 1 | } while (!nickname_updated); |
110 | | |
111 | 1 | ck_assert_msg(tox_friend_get_name_size(tox2, 0, nullptr) == sizeof(NICKNAME), "Name length not correct"); |
112 | 1 | uint8_t temp_name[sizeof(NICKNAME)]; |
113 | 1 | tox_friend_get_name(tox2, 0, temp_name, nullptr); |
114 | 1 | ck_assert_msg(memcmp(temp_name, NICKNAME, sizeof(NICKNAME)) == 0, "Name not correct"); |
115 | | |
116 | 1 | printf("test_set_name succeeded, took %lu seconds\n", (unsigned long)(time(nullptr) - cur_time)); |
117 | | |
118 | 1 | tox_dispatch_free(dispatch2); |
119 | | |
120 | 1 | tox_kill(tox1); |
121 | 1 | tox_kill(tox2); |
122 | 1 | } |
123 | | |
124 | | int main(void) |
125 | 721 | { |
126 | 721 | setvbuf(stdout, nullptr, _IONBF, 0); |
127 | | |
128 | 721 | test_set_name(); |
129 | 721 | return 0; |
130 | 721 | } |