diff options
Diffstat (limited to 'auto_tests/conference_double_invite_test.c')
-rw-r--r-- | auto_tests/conference_double_invite_test.c | 142 |
1 files changed, 38 insertions, 104 deletions
diff --git a/auto_tests/conference_double_invite_test.c b/auto_tests/conference_double_invite_test.c index ea85de10..7b646726 100644 --- a/auto_tests/conference_double_invite_test.c +++ b/auto_tests/conference_double_invite_test.c | |||
@@ -2,15 +2,11 @@ | |||
2 | #define _XOPEN_SOURCE 600 | 2 | #define _XOPEN_SOURCE 600 |
3 | #endif | 3 | #endif |
4 | 4 | ||
5 | #include "../toxcore/tox.h" | 5 | #include <stdbool.h> |
6 | 6 | #include <stdint.h> | |
7 | #include <assert.h> | ||
8 | #include <stdlib.h> | ||
9 | |||
10 | #include "helpers.h" | ||
11 | 7 | ||
12 | typedef struct State { | 8 | typedef struct State { |
13 | uint32_t id; | 9 | uint32_t index; |
14 | bool self_online; | 10 | bool self_online; |
15 | bool friend_online; | 11 | bool friend_online; |
16 | 12 | ||
@@ -18,137 +14,75 @@ typedef struct State { | |||
18 | uint32_t conference; | 14 | uint32_t conference; |
19 | } State; | 15 | } State; |
20 | 16 | ||
21 | static void handle_self_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data) | 17 | #include "run_auto_test.h" |
22 | { | ||
23 | State *state = (State *)user_data; | ||
24 | |||
25 | fprintf(stderr, "self_connection_status(#%u, %d, _)\n", state->id, connection_status); | ||
26 | state->self_online = connection_status != TOX_CONNECTION_NONE; | ||
27 | } | ||
28 | |||
29 | static void handle_friend_connection_status(Tox *tox, uint32_t friend_number, TOX_CONNECTION connection_status, | ||
30 | void *user_data) | ||
31 | { | ||
32 | State *state = (State *)user_data; | ||
33 | |||
34 | fprintf(stderr, "handle_friend_connection_status(#%u, %u, %d, _)\n", state->id, friend_number, connection_status); | ||
35 | state->friend_online = connection_status != TOX_CONNECTION_NONE; | ||
36 | } | ||
37 | 18 | ||
38 | static void handle_conference_invite(Tox *tox, uint32_t friend_number, TOX_CONFERENCE_TYPE type, const uint8_t *cookie, | 19 | static void handle_conference_invite( |
39 | size_t length, void *user_data) | 20 | Tox *tox, uint32_t friend_number, TOX_CONFERENCE_TYPE type, |
21 | const uint8_t *cookie, size_t length, void *user_data) | ||
40 | { | 22 | { |
41 | State *state = (State *)user_data; | 23 | State *state = (State *)user_data; |
42 | 24 | ||
43 | fprintf(stderr, "handle_conference_invite(#%u, %u, %d, uint8_t[%u], _)\n", | 25 | fprintf(stderr, "handle_conference_invite(#%u, %u, %d, uint8_t[%u], _)\n", |
44 | state->id, friend_number, type, (unsigned)length); | 26 | state->index, friend_number, type, (unsigned)length); |
45 | fprintf(stderr, "tox%u joining conference\n", state->id); | 27 | fprintf(stderr, "tox%u joining conference\n", state->index); |
46 | 28 | ||
47 | if (friend_number != -1) { | 29 | if (friend_number != -1) { |
48 | TOX_ERR_CONFERENCE_JOIN err; | 30 | TOX_ERR_CONFERENCE_JOIN err; |
49 | state->conference = tox_conference_join(tox, friend_number, cookie, length, &err); | 31 | state->conference = tox_conference_join(tox, friend_number, cookie, length, &err); |
50 | assert(err == TOX_ERR_CONFERENCE_JOIN_OK); | 32 | ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, |
51 | fprintf(stderr, "tox%u Joined conference %u\n", state->id, state->conference); | 33 | "attempting to join the conference returned with an error: %d", err); |
34 | fprintf(stderr, "tox%u joined conference %u\n", state->index, state->conference); | ||
52 | state->joined = true; | 35 | state->joined = true; |
53 | } | 36 | } |
54 | } | 37 | } |
55 | 38 | ||
56 | int main(void) | 39 | static void conference_double_invite_test(Tox **toxes, State *state) |
57 | { | 40 | { |
58 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
59 | |||
60 | State state1 = {1}; | ||
61 | State state2 = {2}; | ||
62 | |||
63 | // Create toxes. | ||
64 | Tox *tox1 = tox_new_log(nullptr, nullptr, &state1.id); | ||
65 | Tox *tox2 = tox_new_log(nullptr, nullptr, &state2.id); | ||
66 | |||
67 | // tox1 <-> tox2 | ||
68 | uint8_t key[TOX_PUBLIC_KEY_SIZE]; | ||
69 | tox_self_get_public_key(tox2, key); | ||
70 | tox_friend_add_norequest(tox1, key, nullptr); // tox1 -> tox2 | ||
71 | tox_self_get_public_key(tox1, key); | ||
72 | tox_friend_add_norequest(tox2, key, nullptr); // tox2 -> tox1 | ||
73 | |||
74 | printf("bootstrapping tox2 off tox1\n"); | ||
75 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
76 | tox_self_get_dht_id(tox1, dht_key); | ||
77 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
78 | |||
79 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
80 | |||
81 | // Connection callbacks. | ||
82 | tox_callback_self_connection_status(tox1, handle_self_connection_status); | ||
83 | tox_callback_self_connection_status(tox2, handle_self_connection_status); | ||
84 | |||
85 | tox_callback_friend_connection_status(tox1, handle_friend_connection_status); | ||
86 | tox_callback_friend_connection_status(tox2, handle_friend_connection_status); | ||
87 | |||
88 | // Conference callbacks. | 41 | // Conference callbacks. |
89 | tox_callback_conference_invite(tox1, handle_conference_invite); | 42 | tox_callback_conference_invite(toxes[0], handle_conference_invite); |
90 | tox_callback_conference_invite(tox2, handle_conference_invite); | 43 | tox_callback_conference_invite(toxes[1], handle_conference_invite); |
91 | |||
92 | // Wait for self connection. | ||
93 | fprintf(stderr, "Waiting for toxes to come online\n"); | ||
94 | |||
95 | while (!state1.self_online || !state2.self_online) { | ||
96 | tox_iterate(tox1, &state1); | ||
97 | tox_iterate(tox2, &state2); | ||
98 | |||
99 | c_sleep(100); | ||
100 | } | ||
101 | |||
102 | fprintf(stderr, "Toxes are online\n"); | ||
103 | |||
104 | // Wait for friend connection. | ||
105 | fprintf(stderr, "Waiting for friends to connect\n"); | ||
106 | |||
107 | while (!state1.friend_online || !state2.friend_online) { | ||
108 | tox_iterate(tox1, &state1); | ||
109 | tox_iterate(tox2, &state2); | ||
110 | |||
111 | c_sleep(100); | ||
112 | } | ||
113 | |||
114 | fprintf(stderr, "Friends are connected\n"); | ||
115 | 44 | ||
116 | { | 45 | { |
117 | // Create new conference, tox1 is the founder. | 46 | // Create new conference, tox0 is the founder. |
118 | TOX_ERR_CONFERENCE_NEW err; | 47 | TOX_ERR_CONFERENCE_NEW err; |
119 | state1.conference = tox_conference_new(tox1, &err); | 48 | state[0].conference = tox_conference_new(toxes[0], &err); |
120 | state1.joined = true; | 49 | state[0].joined = true; |
121 | assert(err == TOX_ERR_CONFERENCE_NEW_OK); | 50 | ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, |
122 | fprintf(stderr, "Created conference: id=%u\n", state1.conference); | 51 | "attempting to create a new conference returned with an error: %d", err); |
52 | fprintf(stderr, "Created conference: index=%u\n", state[0].conference); | ||
123 | } | 53 | } |
124 | 54 | ||
125 | { | 55 | { |
126 | // Invite friend. | 56 | // Invite friend. |
127 | TOX_ERR_CONFERENCE_INVITE err; | 57 | TOX_ERR_CONFERENCE_INVITE err; |
128 | tox_conference_invite(tox1, 0, state1.conference, &err); | 58 | tox_conference_invite(toxes[0], 0, state[0].conference, &err); |
129 | assert(err == TOX_ERR_CONFERENCE_INVITE_OK); | 59 | ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, |
130 | fprintf(stderr, "tox1 invited tox2\n"); | 60 | "attempting to invite a friend returned with an error: %d", err); |
61 | fprintf(stderr, "tox0 invited tox1\n"); | ||
131 | } | 62 | } |
132 | 63 | ||
133 | fprintf(stderr, "Waiting for invitation to arrive\n"); | 64 | fprintf(stderr, "Waiting for invitation to arrive\n"); |
134 | 65 | ||
135 | while (!state1.joined || !state2.joined) { | 66 | while (!state[0].joined || !state[1].joined) { |
136 | tox_iterate(tox1, &state1); | 67 | tox_iterate(toxes[0], &state[0]); |
137 | tox_iterate(tox2, &state2); | 68 | tox_iterate(toxes[1], &state[1]); |
138 | 69 | ||
139 | c_sleep(100); | 70 | c_sleep(ITERATION_INTERVAL); |
140 | } | 71 | } |
141 | 72 | ||
142 | fprintf(stderr, "Invitations accepted\n"); | 73 | fprintf(stderr, "Invitations accepted\n"); |
143 | 74 | ||
144 | // Invite one more time, resulting in friend -1 inviting tox2. | 75 | // Invite one more time, resulting in friend -1 inviting tox1 (toxes[1]). |
145 | tox_conference_invite(tox1, 0, state1.conference, 0); | 76 | tox_conference_invite(toxes[0], 0, state[0].conference, 0); |
146 | 77 | ||
147 | tox_iterate(tox1, &state1); | 78 | tox_iterate(toxes[0], &state[0]); |
148 | tox_iterate(tox2, &state2); | 79 | tox_iterate(toxes[1], &state[1]); |
80 | } | ||
149 | 81 | ||
150 | tox_kill(tox2); | 82 | int main(void) |
151 | tox_kill(tox1); | 83 | { |
84 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
152 | 85 | ||
86 | run_auto_test(2, conference_double_invite_test); | ||
153 | return 0; | 87 | return 0; |
154 | } | 88 | } |