diff options
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | auto_tests/BUILD.bazel | 6 | ||||
-rw-r--r-- | auto_tests/conference_peer_nick_test.c | 135 |
3 files changed, 142 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 7acc5c97..85231351 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -387,8 +387,9 @@ endif() | |||
387 | auto_test(TCP) | 387 | auto_test(TCP) |
388 | auto_test(bootstrap) | 388 | auto_test(bootstrap) |
389 | auto_test(conference) | 389 | auto_test(conference) |
390 | auto_test(conference_two) | ||
391 | auto_test(conference_double_invite) | 390 | auto_test(conference_double_invite) |
391 | auto_test(conference_peer_nick) | ||
392 | auto_test(conference_two) | ||
392 | auto_test(crypto MSVC_DONT_BUILD) | 393 | auto_test(crypto MSVC_DONT_BUILD) |
393 | auto_test(dht MSVC_DONT_BUILD) | 394 | auto_test(dht MSVC_DONT_BUILD) |
394 | auto_test(encryptsave) | 395 | auto_test(encryptsave) |
diff --git a/auto_tests/BUILD.bazel b/auto_tests/BUILD.bazel index 4f1f1892..4a0275ee 100644 --- a/auto_tests/BUILD.bazel +++ b/auto_tests/BUILD.bazel | |||
@@ -8,9 +8,13 @@ cc_library( | |||
8 | ], | 8 | ], |
9 | ) | 9 | ) |
10 | 10 | ||
11 | test_sizes = { | ||
12 | "conference_peer_nick_test": "medium", | ||
13 | } | ||
14 | |||
11 | [cc_test( | 15 | [cc_test( |
12 | name = src[:-2], | 16 | name = src[:-2], |
13 | size = "small", | 17 | size = test_sizes.get(src[:-2], "small"), |
14 | srcs = [src], | 18 | srcs = [src], |
15 | copts = ["-Wno-sign-compare"], | 19 | copts = ["-Wno-sign-compare"], |
16 | deps = [ | 20 | deps = [ |
diff --git a/auto_tests/conference_peer_nick_test.c b/auto_tests/conference_peer_nick_test.c new file mode 100644 index 00000000..a1b33e9f --- /dev/null +++ b/auto_tests/conference_peer_nick_test.c | |||
@@ -0,0 +1,135 @@ | |||
1 | #ifndef _XOPEN_SOURCE | ||
2 | #define _XOPEN_SOURCE 600 | ||
3 | #endif | ||
4 | |||
5 | #include <stdbool.h> | ||
6 | #include <stdint.h> | ||
7 | |||
8 | typedef struct State { | ||
9 | uint32_t index; | ||
10 | bool self_online; | ||
11 | bool friend_online; | ||
12 | bool friend_in_group; | ||
13 | |||
14 | bool joined; | ||
15 | uint32_t conference; | ||
16 | } State; | ||
17 | |||
18 | #include "run_auto_test.h" | ||
19 | |||
20 | static void handle_conference_invite( | ||
21 | Tox *tox, uint32_t friend_number, TOX_CONFERENCE_TYPE type, | ||
22 | const uint8_t *cookie, size_t length, void *user_data) | ||
23 | { | ||
24 | State *state = (State *)user_data; | ||
25 | |||
26 | fprintf(stderr, "handle_conference_invite(#%u, %u, %d, uint8_t[%u], _)\n", | ||
27 | state->index, friend_number, type, (unsigned)length); | ||
28 | fprintf(stderr, "tox%u joining conference\n", state->index); | ||
29 | |||
30 | TOX_ERR_CONFERENCE_JOIN err; | ||
31 | state->conference = tox_conference_join(tox, friend_number, cookie, length, &err); | ||
32 | ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, | ||
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); | ||
35 | state->joined = true; | ||
36 | } | ||
37 | |||
38 | static void handle_peer_list_changed(Tox *tox, uint32_t conference_number, void *user_data) | ||
39 | { | ||
40 | State *state = (State *)user_data; | ||
41 | |||
42 | fprintf(stderr, "handle_peer_list_changed(#%u, %u, _)\n", | ||
43 | state->index, conference_number); | ||
44 | |||
45 | TOX_ERR_CONFERENCE_PEER_QUERY err; | ||
46 | uint32_t const count = tox_conference_peer_count(tox, conference_number, &err); | ||
47 | ck_assert_msg(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK, | ||
48 | "failed to get conference peer count: err = %d", err); | ||
49 | printf("tox%u has %u peers\n", state->index, count); | ||
50 | state->friend_in_group = count == 2; | ||
51 | } | ||
52 | |||
53 | static void rebuild_peer_list(Tox *tox) | ||
54 | { | ||
55 | for (uint32_t conference_number = 0; | ||
56 | conference_number < tox_conference_get_chatlist_size(tox); | ||
57 | ++conference_number) { | ||
58 | TOX_ERR_CONFERENCE_PEER_QUERY err; | ||
59 | uint32_t const count = tox_conference_peer_count(tox, conference_number, &err); | ||
60 | assert(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK); | ||
61 | |||
62 | for (uint32_t peer_number = 0; peer_number < count; peer_number++) { | ||
63 | size_t size = tox_conference_peer_get_name_size(tox, conference_number, peer_number, &err); | ||
64 | assert(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK); | ||
65 | |||
66 | uint8_t *const name = malloc(size); | ||
67 | tox_conference_peer_get_name(tox, conference_number, peer_number, name, &err); | ||
68 | assert(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK); | ||
69 | free(name); | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | |||
74 | static void conference_peer_nick_test(Tox **toxes, State *state) | ||
75 | { | ||
76 | // Conference callbacks. | ||
77 | tox_callback_conference_invite(toxes[0], handle_conference_invite); | ||
78 | tox_callback_conference_invite(toxes[1], handle_conference_invite); | ||
79 | tox_callback_conference_peer_list_changed(toxes[0], handle_peer_list_changed); | ||
80 | tox_callback_conference_peer_list_changed(toxes[1], handle_peer_list_changed); | ||
81 | |||
82 | // Set the names of the toxes. | ||
83 | tox_self_set_name(toxes[0], (const uint8_t *)"test-tox-0", 10, nullptr); | ||
84 | tox_self_set_name(toxes[1], (const uint8_t *)"test-tox-1", 10, nullptr); | ||
85 | |||
86 | { | ||
87 | // Create new conference, tox0 is the founder. | ||
88 | TOX_ERR_CONFERENCE_NEW err; | ||
89 | state[0].conference = tox_conference_new(toxes[0], &err); | ||
90 | state[0].joined = true; | ||
91 | ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, | ||
92 | "attempting to create a new conference returned with an error: %d", err); | ||
93 | fprintf(stderr, "Created conference: index=%u\n", state[0].conference); | ||
94 | } | ||
95 | |||
96 | { | ||
97 | // Invite friend. | ||
98 | TOX_ERR_CONFERENCE_INVITE err; | ||
99 | tox_conference_invite(toxes[0], 0, state[0].conference, &err); | ||
100 | ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, | ||
101 | "attempting to invite a friend returned with an error: %d", err); | ||
102 | fprintf(stderr, "tox0 invited tox1\n"); | ||
103 | } | ||
104 | |||
105 | fprintf(stderr, "Waiting for invitation to arrive and peers to be in the group\n"); | ||
106 | |||
107 | while (!state[0].joined || !state[1].joined || !state[0].friend_in_group || !state[1].friend_in_group) { | ||
108 | tox_iterate(toxes[0], &state[0]); | ||
109 | tox_iterate(toxes[1], &state[1]); | ||
110 | |||
111 | c_sleep(ITERATION_INTERVAL); | ||
112 | } | ||
113 | |||
114 | fprintf(stderr, "Running tox0, but not tox1, waiting for tox1 to drop out\n"); | ||
115 | |||
116 | while (state[0].friend_in_group) { | ||
117 | tox_iterate(toxes[0], &state[0]); | ||
118 | |||
119 | // Rebuild peer list after every iteration. | ||
120 | rebuild_peer_list(toxes[0]); | ||
121 | |||
122 | c_sleep(ITERATION_INTERVAL); | ||
123 | } | ||
124 | |||
125 | fprintf(stderr, "Invitations accepted\n"); | ||
126 | |||
127 | } | ||
128 | |||
129 | int main(void) | ||
130 | { | ||
131 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
132 | |||
133 | run_auto_test(2, conference_peer_nick_test); | ||
134 | return 0; | ||
135 | } | ||