diff options
Diffstat (limited to 'auto_tests/tox_many_test.c')
-rw-r--r-- | auto_tests/tox_many_test.c | 76 |
1 files changed, 25 insertions, 51 deletions
diff --git a/auto_tests/tox_many_test.c b/auto_tests/tox_many_test.c index bacaaff8..a797d99c 100644 --- a/auto_tests/tox_many_test.c +++ b/auto_tests/tox_many_test.c | |||
@@ -22,10 +22,6 @@ | |||
22 | 22 | ||
23 | static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) | 23 | static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) |
24 | { | 24 | { |
25 | if (*((uint32_t *)userdata) != 974536) { | ||
26 | return; | ||
27 | } | ||
28 | |||
29 | if (length == 7 && memcmp("Gentoo", data, 7) == 0) { | 25 | if (length == 7 && memcmp("Gentoo", data, 7) == 0) { |
30 | tox_friend_add_norequest(m, public_key, nullptr); | 26 | tox_friend_add_norequest(m, public_key, nullptr); |
31 | } | 27 | } |
@@ -35,29 +31,19 @@ static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8 | |||
35 | #define NUM_TOXES 90 | 31 | #define NUM_TOXES 90 |
36 | #define NUM_FRIENDS 50 | 32 | #define NUM_FRIENDS 50 |
37 | 33 | ||
38 | START_TEST(test_many_clients) | 34 | static void test_many_clients(void) |
39 | { | 35 | { |
40 | long long unsigned int cur_time = time(nullptr); | 36 | time_t cur_time = time(nullptr); |
41 | Tox *toxes[NUM_TOXES]; | 37 | Tox *toxes[NUM_TOXES]; |
42 | uint32_t index[NUM_TOXES]; | 38 | uint32_t index[NUM_TOXES]; |
43 | uint32_t i, j; | ||
44 | uint32_t to_comp = 974536; | ||
45 | 39 | ||
46 | for (i = 0; i < NUM_TOXES; ++i) { | 40 | for (uint32_t i = 0; i < NUM_TOXES; ++i) { |
47 | index[i] = i + 1; | 41 | index[i] = i + 1; |
48 | toxes[i] = tox_new_log(nullptr, nullptr, &index[i]); | 42 | toxes[i] = tox_new_log(nullptr, nullptr, &index[i]); |
49 | ck_assert_msg(toxes[i] != nullptr, "Failed to create tox instances %u", i); | 43 | ck_assert_msg(toxes[i] != nullptr, "failed to create tox instances %u", i); |
50 | tox_callback_friend_request(toxes[i], accept_friend_request); | 44 | tox_callback_friend_request(toxes[i], accept_friend_request); |
51 | } | 45 | } |
52 | 46 | ||
53 | { | ||
54 | TOX_ERR_GET_PORT error; | ||
55 | uint16_t port = tox_self_get_udp_port(toxes[0], &error); | ||
56 | ck_assert_msg(33445 <= port && port <= 33545, | ||
57 | "First Tox instance did not bind to udp port inside [33445, 33545].\n"); | ||
58 | ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); | ||
59 | } | ||
60 | |||
61 | struct { | 47 | struct { |
62 | uint16_t tox1; | 48 | uint16_t tox1; |
63 | uint16_t tox2; | 49 | uint16_t tox2; |
@@ -65,20 +51,20 @@ START_TEST(test_many_clients) | |||
65 | 51 | ||
66 | uint8_t address[TOX_ADDRESS_SIZE]; | 52 | uint8_t address[TOX_ADDRESS_SIZE]; |
67 | 53 | ||
68 | unsigned int num_f = 0; | 54 | uint32_t num_f = 0; |
69 | 55 | ||
70 | for (i = 0; i < NUM_TOXES; ++i) { | 56 | for (uint32_t i = 0; i < NUM_TOXES; ++i) { |
71 | num_f += tox_self_get_friend_list_size(toxes[i]); | 57 | num_f += tox_self_get_friend_list_size(toxes[i]); |
72 | } | 58 | } |
73 | 59 | ||
74 | ck_assert_msg(num_f == 0, "bad num friends: %u", num_f); | 60 | ck_assert_msg(num_f == 0, "bad num friends: %u", num_f); |
75 | 61 | ||
76 | for (i = 0; i < NUM_FRIENDS; ++i) { | 62 | for (uint32_t i = 0; i < NUM_FRIENDS; ++i) { |
77 | loop_top: | 63 | loop_top: |
78 | pairs[i].tox1 = rand() % NUM_TOXES; | 64 | pairs[i].tox1 = rand() % NUM_TOXES; |
79 | pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES; | 65 | pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES; |
80 | 66 | ||
81 | for (j = 0; j < i; ++j) { | 67 | for (uint32_t j = 0; j < i; ++j) { |
82 | if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { | 68 | if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { |
83 | goto loop_top; | 69 | goto loop_top; |
84 | } | 70 | } |
@@ -93,10 +79,16 @@ loop_top: | |||
93 | goto loop_top; | 79 | goto loop_top; |
94 | } | 80 | } |
95 | 81 | ||
96 | ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend error code: %i", test); | 82 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; |
83 | tox_self_get_dht_id(toxes[pairs[i].tox1], dht_key); | ||
84 | const uint16_t dht_port = tox_self_get_udp_port(toxes[pairs[i].tox1], nullptr); | ||
85 | |||
86 | tox_bootstrap(toxes[pairs[i].tox2], "localhost", dht_port, dht_key, nullptr); | ||
87 | |||
88 | ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "failed to add friend error code: %i", test); | ||
97 | } | 89 | } |
98 | 90 | ||
99 | for (i = 0; i < NUM_TOXES; ++i) { | 91 | for (uint32_t i = 0; i < NUM_TOXES; ++i) { |
100 | num_f += tox_self_get_friend_list_size(toxes[i]); | 92 | num_f += tox_self_get_friend_list_size(toxes[i]); |
101 | } | 93 | } |
102 | 94 | ||
@@ -107,8 +99,8 @@ loop_top: | |||
107 | while (1) { | 99 | while (1) { |
108 | uint16_t counter = 0; | 100 | uint16_t counter = 0; |
109 | 101 | ||
110 | for (i = 0; i < NUM_TOXES; ++i) { | 102 | for (uint32_t i = 0; i < NUM_TOXES; ++i) { |
111 | for (j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) { | 103 | for (uint32_t j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) { |
112 | if (tox_friend_get_connection_status(toxes[i], j, nullptr) == TOX_CONNECTION_UDP) { | 104 | if (tox_friend_get_connection_status(toxes[i], j, nullptr) == TOX_CONNECTION_UDP) { |
113 | ++counter; | 105 | ++counter; |
114 | } | 106 | } |
@@ -124,42 +116,24 @@ loop_top: | |||
124 | break; | 116 | break; |
125 | } | 117 | } |
126 | 118 | ||
127 | for (i = 0; i < NUM_TOXES; ++i) { | 119 | for (uint32_t i = 0; i < NUM_TOXES; ++i) { |
128 | tox_iterate(toxes[i], &to_comp); | 120 | tox_iterate(toxes[i], nullptr); |
129 | } | 121 | } |
130 | 122 | ||
131 | c_sleep(50); | 123 | c_sleep(50); |
132 | } | 124 | } |
133 | 125 | ||
134 | for (i = 0; i < NUM_TOXES; ++i) { | 126 | for (uint32_t i = 0; i < NUM_TOXES; ++i) { |
135 | tox_kill(toxes[i]); | 127 | tox_kill(toxes[i]); |
136 | } | 128 | } |
137 | 129 | ||
138 | printf("test_many_clients succeeded, took %llu seconds\n", time(nullptr) - cur_time); | 130 | printf("test_many_clients succeeded, took %ld seconds\n", time(nullptr) - cur_time); |
139 | } | ||
140 | END_TEST | ||
141 | |||
142 | static Suite *tox_suite(void) | ||
143 | { | ||
144 | Suite *s = suite_create("Tox"); | ||
145 | |||
146 | DEFTESTCASE(many_clients); | ||
147 | |||
148 | return s; | ||
149 | } | 131 | } |
150 | 132 | ||
151 | int main(int argc, char *argv[]) | 133 | int main(int argc, char *argv[]) |
152 | { | 134 | { |
153 | srand((unsigned int) time(nullptr)); | 135 | setvbuf(stdout, nullptr, _IONBF, 0); |
154 | |||
155 | Suite *tox = tox_suite(); | ||
156 | SRunner *test_runner = srunner_create(tox); | ||
157 | |||
158 | int number_failed = 0; | ||
159 | srunner_run_all(test_runner, CK_NORMAL); | ||
160 | number_failed = srunner_ntests_failed(test_runner); | ||
161 | |||
162 | srunner_free(test_runner); | ||
163 | 136 | ||
164 | return number_failed; | 137 | test_many_clients(); |
138 | return 0; | ||
165 | } | 139 | } |