diff options
author | Robin Lindén <dev@robinlinden.eu> | 2018-02-18 01:57:45 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-02-18 14:50:18 +0000 |
commit | c3515c49e012f76c5d2ca104476e372d924a79f9 (patch) | |
tree | 56512d94a79ad08d768ea51afa735c56833338b1 | |
parent | 13706de14bce8f8bfd1cb7db226d12cbd927b1b4 (diff) |
Split up tox_test into multiple smaller tests
-rw-r--r-- | CMakeLists.txt | 9 | ||||
-rw-r--r-- | auto_tests/friend_request_test.c | 92 | ||||
-rw-r--r-- | auto_tests/helpers.h | 3 | ||||
-rw-r--r-- | auto_tests/lossless_packet_test.c | 112 | ||||
-rw-r--r-- | auto_tests/lossy_packet_test.c | 109 | ||||
-rw-r--r-- | auto_tests/monolith_test.cpp | 32 | ||||
-rw-r--r-- | auto_tests/save_load_test.c | 149 | ||||
-rw-r--r-- | auto_tests/send_message_test.c | 120 | ||||
-rw-r--r-- | auto_tests/set_name_test.c | 106 | ||||
-rw-r--r-- | auto_tests/set_status_message_test.c | 113 | ||||
-rw-r--r-- | auto_tests/tox_test.c | 445 | ||||
-rw-r--r-- | auto_tests/typing_test.c | 111 |
12 files changed, 953 insertions, 448 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a16c1220..9425b35b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -442,19 +442,26 @@ auto_test(crypto MSVC_DONT_BUILD) | |||
442 | auto_test(dht MSVC_DONT_BUILD) | 442 | auto_test(dht MSVC_DONT_BUILD) |
443 | auto_test(encryptsave) | 443 | auto_test(encryptsave) |
444 | auto_test(file_transfer) | 444 | auto_test(file_transfer) |
445 | auto_test(friend_request) | ||
445 | auto_test(lan_discovery) | 446 | auto_test(lan_discovery) |
447 | auto_test(lossless_packet) | ||
448 | auto_test(lossy_packet) | ||
446 | auto_test(messenger MSVC_DONT_BUILD) | 449 | auto_test(messenger MSVC_DONT_BUILD) |
447 | auto_test(network) | 450 | auto_test(network) |
448 | auto_test(onion) | 451 | auto_test(onion) |
449 | auto_test(resource_leak) | 452 | auto_test(resource_leak) |
450 | auto_test(save_friend) | 453 | auto_test(save_friend) |
454 | auto_test(save_load) | ||
455 | auto_test(send_message) | ||
456 | auto_test(set_name) | ||
457 | auto_test(set_status_message) | ||
451 | auto_test(simple_conference) | 458 | auto_test(simple_conference) |
452 | auto_test(skeleton) | 459 | auto_test(skeleton) |
453 | auto_test(tox) | ||
454 | auto_test(tox_many) | 460 | auto_test(tox_many) |
455 | auto_test(tox_many_tcp) | 461 | auto_test(tox_many_tcp) |
456 | auto_test(tox_one) | 462 | auto_test(tox_one) |
457 | auto_test(tox_strncasecmp) | 463 | auto_test(tox_strncasecmp) |
464 | auto_test(typing) | ||
458 | auto_test(version) | 465 | auto_test(version) |
459 | # TODO(iphydf): These tests are broken. The code needs to be fixed, as the | 466 | # TODO(iphydf): These tests are broken. The code needs to be fixed, as the |
460 | # tests themselves are correct. | 467 | # tests themselves are correct. |
diff --git a/auto_tests/friend_request_test.c b/auto_tests/friend_request_test.c new file mode 100644 index 00000000..d9987cfc --- /dev/null +++ b/auto_tests/friend_request_test.c | |||
@@ -0,0 +1,92 @@ | |||
1 | /* Tests that we can add friends. | ||
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 FR_MESSAGE "Gentoo" | ||
25 | |||
26 | static void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, size_t length, | ||
27 | void *userdata) | ||
28 | { | ||
29 | ck_assert_msg(length == sizeof(FR_MESSAGE) && memcmp(FR_MESSAGE, data, sizeof(FR_MESSAGE)) == 0, | ||
30 | "unexpected friend request message"); | ||
31 | tox_friend_add_norequest(tox, public_key, nullptr); | ||
32 | } | ||
33 | |||
34 | static void test_friend_request(void) | ||
35 | { | ||
36 | printf("initialising 2 toxes\n"); | ||
37 | uint32_t index[] = { 1, 2 }; | ||
38 | const time_t cur_time = time(nullptr); | ||
39 | Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]); | ||
40 | Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]); | ||
41 | |||
42 | ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances"); | ||
43 | |||
44 | printf("bootstrapping tox2 off tox1\n"); | ||
45 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
46 | tox_self_get_dht_id(tox1, dht_key); | ||
47 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
48 | |||
49 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
50 | |||
51 | while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || | ||
52 | tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) { | ||
53 | tox_iterate(tox1, nullptr); | ||
54 | tox_iterate(tox2, nullptr); | ||
55 | |||
56 | c_sleep(ITERATION_INTERVAL); | ||
57 | } | ||
58 | |||
59 | printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time); | ||
60 | const time_t con_time = time(nullptr); | ||
61 | |||
62 | printf("tox1 adds tox2 as friend, tox2 accepts\n"); | ||
63 | tox_callback_friend_request(tox2, accept_friend_request); | ||
64 | |||
65 | uint8_t address[TOX_ADDRESS_SIZE]; | ||
66 | tox_self_get_address(tox2, address); | ||
67 | |||
68 | const uint32_t test = tox_friend_add(tox1, address, (const uint8_t *)FR_MESSAGE, sizeof(FR_MESSAGE), nullptr); | ||
69 | ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); | ||
70 | |||
71 | while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP || | ||
72 | tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) { | ||
73 | tox_iterate(tox1, nullptr); | ||
74 | tox_iterate(tox2, nullptr); | ||
75 | |||
76 | c_sleep(ITERATION_INTERVAL); | ||
77 | } | ||
78 | |||
79 | printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); | ||
80 | printf("test_friend_request succeeded, took %ld seconds\n", time(nullptr) - cur_time); | ||
81 | |||
82 | tox_kill(tox1); | ||
83 | tox_kill(tox2); | ||
84 | } | ||
85 | |||
86 | int main(int argc, char *argv[]) | ||
87 | { | ||
88 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
89 | |||
90 | test_friend_request(); | ||
91 | return 0; | ||
92 | } | ||
diff --git a/auto_tests/helpers.h b/auto_tests/helpers.h index de106340..72f57ead 100644 --- a/auto_tests/helpers.h +++ b/auto_tests/helpers.h | |||
@@ -17,6 +17,8 @@ | |||
17 | #define c_sleep(x) usleep(1000 * (x)) | 17 | #define c_sleep(x) usleep(1000 * (x)) |
18 | #endif | 18 | #endif |
19 | 19 | ||
20 | #define ITERATION_INTERVAL 200 | ||
21 | |||
20 | static const char *tox_log_level_name(TOX_LOG_LEVEL level) | 22 | static const char *tox_log_level_name(TOX_LOG_LEVEL level) |
21 | { | 23 | { |
22 | switch (level) { | 24 | switch (level) { |
@@ -58,6 +60,7 @@ Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_d | |||
58 | 60 | ||
59 | if (log_options == nullptr) { | 61 | if (log_options == nullptr) { |
60 | log_options = tox_options_new(nullptr); | 62 | log_options = tox_options_new(nullptr); |
63 | // tox_options_set_local_discovery_enabled(log_options, false); | ||
61 | } | 64 | } |
62 | 65 | ||
63 | assert(log_options != nullptr); | 66 | assert(log_options != nullptr); |
diff --git a/auto_tests/lossless_packet_test.c b/auto_tests/lossless_packet_test.c new file mode 100644 index 00000000..b5185757 --- /dev/null +++ b/auto_tests/lossless_packet_test.c | |||
@@ -0,0 +1,112 @@ | |||
1 | /* Tests that we can send lossless packets. | ||
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 LOSSLESS_PACKET_FILLER 160 | ||
25 | |||
26 | static void handle_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, | ||
27 | void *user_data) | ||
28 | { | ||
29 | uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE]; | ||
30 | memset(cmp_packet, LOSSLESS_PACKET_FILLER, sizeof(cmp_packet)); | ||
31 | |||
32 | if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) { | ||
33 | bool *custom_packet_received = (bool *)user_data; | ||
34 | *custom_packet_received = true; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | static void test_lossless_packet(void) | ||
39 | { | ||
40 | printf("initialising 2 toxes\n"); | ||
41 | uint32_t index[] = { 1, 2 }; | ||
42 | const time_t cur_time = time(nullptr); | ||
43 | Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]); | ||
44 | Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]); | ||
45 | |||
46 | ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances"); | ||
47 | |||
48 | printf("tox1 adds tox2 as friend, tox2 adds tox1\n"); | ||
49 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; | ||
50 | tox_self_get_public_key(tox2, public_key); | ||
51 | tox_friend_add_norequest(tox1, public_key, nullptr); | ||
52 | tox_self_get_public_key(tox1, public_key); | ||
53 | tox_friend_add_norequest(tox2, public_key, nullptr); | ||
54 | |||
55 | printf("bootstrapping tox2 off tox1\n"); | ||
56 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
57 | tox_self_get_dht_id(tox1, dht_key); | ||
58 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
59 | |||
60 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
61 | |||
62 | while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || | ||
63 | tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) { | ||
64 | tox_iterate(tox1, nullptr); | ||
65 | tox_iterate(tox2, nullptr); | ||
66 | |||
67 | c_sleep(200); | ||
68 | } | ||
69 | |||
70 | printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time); | ||
71 | const time_t con_time = time(nullptr); | ||
72 | |||
73 | while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP || | ||
74 | tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) { | ||
75 | tox_iterate(tox1, nullptr); | ||
76 | tox_iterate(tox2, nullptr); | ||
77 | |||
78 | c_sleep(200); | ||
79 | } | ||
80 | |||
81 | printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); | ||
82 | |||
83 | tox_callback_friend_lossless_packet(tox2, &handle_lossless_packet); | ||
84 | uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1]; | ||
85 | memset(packet, LOSSLESS_PACKET_FILLER, sizeof(packet)); | ||
86 | bool ret = tox_friend_send_lossless_packet(tox1, 0, packet, sizeof(packet), nullptr); | ||
87 | ck_assert_msg(ret == false, "tox_friend_send_lossless_packet bigger fail %i", ret); | ||
88 | ret = tox_friend_send_lossless_packet(tox1, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr); | ||
89 | ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret); | ||
90 | |||
91 | bool received_lossless_packet = false; | ||
92 | |||
93 | while (!received_lossless_packet) { | ||
94 | tox_iterate(tox1, nullptr); | ||
95 | tox_iterate(tox2, &received_lossless_packet); | ||
96 | |||
97 | c_sleep(200); | ||
98 | } | ||
99 | |||
100 | printf("test_lossless_packet succeeded, took %ld seconds\n", time(nullptr) - cur_time); | ||
101 | |||
102 | tox_kill(tox1); | ||
103 | tox_kill(tox2); | ||
104 | } | ||
105 | |||
106 | int main(int argc, char *argv[]) | ||
107 | { | ||
108 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
109 | |||
110 | test_lossless_packet(); | ||
111 | return 0; | ||
112 | } | ||
diff --git a/auto_tests/lossy_packet_test.c b/auto_tests/lossy_packet_test.c new file mode 100644 index 00000000..d225a62b --- /dev/null +++ b/auto_tests/lossy_packet_test.c | |||
@@ -0,0 +1,109 @@ | |||
1 | /* Tests that we can send lossy packets. | ||
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 LOSSY_PACKET_FILLER 200 | ||
25 | |||
26 | static void handle_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) | ||
27 | { | ||
28 | uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE]; | ||
29 | memset(cmp_packet, LOSSY_PACKET_FILLER, sizeof(cmp_packet)); | ||
30 | |||
31 | if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) { | ||
32 | bool *custom_packet_received = (bool *)user_data; | ||
33 | *custom_packet_received = true; | ||
34 | } | ||
35 | } | ||
36 | |||
37 | static void test_lossy_packet(void) | ||
38 | { | ||
39 | printf("initialising 2 toxes\n"); | ||
40 | uint32_t index[] = { 1, 2 }; | ||
41 | const time_t cur_time = time(nullptr); | ||
42 | Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]); | ||
43 | Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]); | ||
44 | |||
45 | ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances"); | ||
46 | |||
47 | printf("tox1 adds tox2 as friend, tox2 adds tox1\n"); | ||
48 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; | ||
49 | tox_self_get_public_key(tox2, public_key); | ||
50 | tox_friend_add_norequest(tox1, public_key, nullptr); | ||
51 | tox_self_get_public_key(tox1, public_key); | ||
52 | tox_friend_add_norequest(tox2, public_key, nullptr); | ||
53 | |||
54 | printf("bootstrapping tox2 off tox1\n"); | ||
55 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
56 | tox_self_get_dht_id(tox1, dht_key); | ||
57 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
58 | |||
59 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
60 | |||
61 | while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || | ||
62 | tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) { | ||
63 | tox_iterate(tox1, nullptr); | ||
64 | tox_iterate(tox2, nullptr); | ||
65 | |||
66 | c_sleep(200); | ||
67 | } | ||
68 | |||
69 | printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time); | ||
70 | const time_t con_time = time(nullptr); | ||
71 | |||
72 | while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP || | ||
73 | tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) { | ||
74 | tox_iterate(tox1, nullptr); | ||
75 | tox_iterate(tox2, nullptr); | ||
76 | c_sleep(200); | ||
77 | } | ||
78 | |||
79 | printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); | ||
80 | |||
81 | tox_callback_friend_lossy_packet(tox2, &handle_lossy_packet); | ||
82 | uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1]; | ||
83 | memset(packet, LOSSY_PACKET_FILLER, sizeof(packet)); | ||
84 | bool ret = tox_friend_send_lossy_packet(tox1, 0, packet, sizeof(packet), nullptr); | ||
85 | ck_assert_msg(ret == false, "tox_friend_send_lossy_packet bigger fail %i", ret); | ||
86 | ret = tox_friend_send_lossy_packet(tox1, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr); | ||
87 | ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret); | ||
88 | |||
89 | bool received_lossy_packet = false; | ||
90 | |||
91 | while (!received_lossy_packet) { | ||
92 | tox_iterate(tox1, nullptr); | ||
93 | tox_iterate(tox2, &received_lossy_packet); | ||
94 | c_sleep(200); | ||
95 | } | ||
96 | |||
97 | printf("test_lossy_packet succeeded, took %ld seconds\n", time(nullptr) - cur_time); | ||
98 | |||
99 | tox_kill(tox1); | ||
100 | tox_kill(tox2); | ||
101 | } | ||
102 | |||
103 | int main(int argc, char *argv[]) | ||
104 | { | ||
105 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
106 | |||
107 | test_lossy_packet(); | ||
108 | return 0; | ||
109 | } | ||
diff --git a/auto_tests/monolith_test.cpp b/auto_tests/monolith_test.cpp index b09d8985..23055bb0 100644 --- a/auto_tests/monolith_test.cpp +++ b/auto_tests/monolith_test.cpp | |||
@@ -52,10 +52,22 @@ namespace file_saving_test | |||
52 | { | 52 | { |
53 | #include "file_saving_test.c" | 53 | #include "file_saving_test.c" |
54 | } | 54 | } |
55 | namespace friend_request_test | ||
56 | { | ||
57 | #include "friend_request_test.c" | ||
58 | } | ||
55 | namespace lan_discovery_test | 59 | namespace lan_discovery_test |
56 | { | 60 | { |
57 | #include "lan_discovery_test.c" | 61 | #include "lan_discovery_test.c" |
58 | } | 62 | } |
63 | namespace lossless_packet_test | ||
64 | { | ||
65 | #include "lossless_packet_test.c" | ||
66 | } | ||
67 | namespace lossy_packet_test | ||
68 | { | ||
69 | #include "lossy_packet_test.c" | ||
70 | } | ||
59 | namespace messenger_test | 71 | namespace messenger_test |
60 | { | 72 | { |
61 | #include "messenger_test.c" | 73 | #include "messenger_test.c" |
@@ -76,6 +88,10 @@ namespace save_friend_test | |||
76 | { | 88 | { |
77 | #include "save_friend_test.c" | 89 | #include "save_friend_test.c" |
78 | } | 90 | } |
91 | namespace save_load_test | ||
92 | { | ||
93 | #include "save_load_test.c" | ||
94 | } | ||
79 | namespace selfname_change_conference_test | 95 | namespace selfname_change_conference_test |
80 | { | 96 | { |
81 | #include "selfname_change_conference_test.c" | 97 | #include "selfname_change_conference_test.c" |
@@ -84,6 +100,18 @@ namespace self_conference_title_change_test | |||
84 | { | 100 | { |
85 | #include "self_conference_title_change_test.c" | 101 | #include "self_conference_title_change_test.c" |
86 | } | 102 | } |
103 | namespace send_message_test | ||
104 | { | ||
105 | #include "send_message_test.c" | ||
106 | } | ||
107 | namespace set_name_test | ||
108 | { | ||
109 | #include "set_name_test.c" | ||
110 | } | ||
111 | namespace set_status_message_test | ||
112 | { | ||
113 | #include "set_status_message_test.c" | ||
114 | } | ||
87 | namespace simple_conference_test | 115 | namespace simple_conference_test |
88 | { | 116 | { |
89 | #include "simple_conference_test.c" | 117 | #include "simple_conference_test.c" |
@@ -116,9 +144,9 @@ namespace tox_strncasecmp_test | |||
116 | { | 144 | { |
117 | #include "tox_strncasecmp_test.c" | 145 | #include "tox_strncasecmp_test.c" |
118 | } | 146 | } |
119 | namespace tox_test | 147 | namespace typing_test |
120 | { | 148 | { |
121 | #include "tox_test.c" | 149 | #include "typing_test.c" |
122 | } | 150 | } |
123 | namespace version_test | 151 | namespace version_test |
124 | { | 152 | { |
diff --git a/auto_tests/save_load_test.c b/auto_tests/save_load_test.c new file mode 100644 index 00000000..ab4e0a8c --- /dev/null +++ b/auto_tests/save_load_test.c | |||
@@ -0,0 +1,149 @@ | |||
1 | /* Tests that we can save and load Tox data. | ||
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 | static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) | ||
25 | { | ||
26 | if (length == 7 && memcmp("Gentoo", data, 7) == 0) { | ||
27 | tox_friend_add_norequest(m, public_key, nullptr); | ||
28 | } | ||
29 | } | ||
30 | |||
31 | static unsigned int connected_t1; | ||
32 | static void tox_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data) | ||
33 | { | ||
34 | if (connected_t1 && !connection_status) { | ||
35 | ck_abort_msg("Tox went offline"); | ||
36 | } | ||
37 | |||
38 | ck_assert_msg(connection_status == TOX_CONNECTION_UDP, "wrong status %u", connection_status); | ||
39 | |||
40 | connected_t1 = connection_status; | ||
41 | } | ||
42 | |||
43 | static void test_few_clients(void) | ||
44 | { | ||
45 | uint32_t index[] = { 1, 2, 3 }; | ||
46 | time_t con_time = 0, cur_time = time(nullptr); | ||
47 | Tox *tox1 = tox_new_log(nullptr, nullptr, &index[0]); | ||
48 | Tox *tox2 = tox_new_log(nullptr, nullptr, &index[1]); | ||
49 | Tox *tox3 = tox_new_log(nullptr, nullptr, &index[2]); | ||
50 | |||
51 | ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances"); | ||
52 | |||
53 | printf("bootstrapping tox2 and tox3 off tox1\n"); | ||
54 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
55 | tox_self_get_dht_id(tox1, dht_key); | ||
56 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
57 | |||
58 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
59 | tox_bootstrap(tox3, "localhost", dht_port, dht_key, nullptr); | ||
60 | |||
61 | connected_t1 = 0; | ||
62 | tox_callback_self_connection_status(tox1, tox_connection_status); | ||
63 | tox_callback_friend_request(tox2, accept_friend_request); | ||
64 | uint8_t address[TOX_ADDRESS_SIZE]; | ||
65 | tox_self_get_address(tox2, address); | ||
66 | uint32_t test = tox_friend_add(tox3, address, (const uint8_t *)"Gentoo", 7, nullptr); | ||
67 | ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); | ||
68 | |||
69 | uint8_t off = 1; | ||
70 | |||
71 | while (1) { | ||
72 | tox_iterate(tox1, nullptr); | ||
73 | tox_iterate(tox2, nullptr); | ||
74 | tox_iterate(tox3, nullptr); | ||
75 | |||
76 | if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2) | ||
77 | && tox_self_get_connection_status(tox3)) { | ||
78 | if (off) { | ||
79 | printf("Toxes are online, took %ld seconds\n", time(nullptr) - cur_time); | ||
80 | con_time = time(nullptr); | ||
81 | off = 0; | ||
82 | } | ||
83 | |||
84 | if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP | ||
85 | && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) { | ||
86 | break; | ||
87 | } | ||
88 | } | ||
89 | |||
90 | c_sleep(ITERATION_INTERVAL); | ||
91 | } | ||
92 | |||
93 | ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1); | ||
94 | printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); | ||
95 | |||
96 | const size_t save_size1 = tox_get_savedata_size(tox2); | ||
97 | ck_assert_msg(save_size1 != 0, "save is invalid size %u", (unsigned)save_size1); | ||
98 | printf("%u\n", (unsigned)save_size1); | ||
99 | VLA(uint8_t, save1, save_size1); | ||
100 | tox_get_savedata(tox2, save1); | ||
101 | tox_kill(tox2); | ||
102 | |||
103 | struct Tox_Options *const options = tox_options_new(nullptr); | ||
104 | tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); | ||
105 | tox_options_set_savedata_data(options, save1, save_size1); | ||
106 | tox_options_set_local_discovery_enabled(options, false); | ||
107 | tox2 = tox_new_log(options, nullptr, &index[1]); | ||
108 | cur_time = time(nullptr); | ||
109 | off = 1; | ||
110 | |||
111 | while (1) { | ||
112 | tox_iterate(tox1, nullptr); | ||
113 | tox_iterate(tox2, nullptr); | ||
114 | tox_iterate(tox3, nullptr); | ||
115 | |||
116 | if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2) | ||
117 | && tox_self_get_connection_status(tox3)) { | ||
118 | if (off) { | ||
119 | printf("Toxes are online again after reloading, took %ld seconds\n", time(nullptr) - cur_time); | ||
120 | con_time = time(nullptr); | ||
121 | off = 0; | ||
122 | } | ||
123 | |||
124 | if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP | ||
125 | && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) { | ||
126 | break; | ||
127 | } | ||
128 | } | ||
129 | |||
130 | c_sleep(ITERATION_INTERVAL); | ||
131 | } | ||
132 | |||
133 | printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); | ||
134 | |||
135 | printf("test_few_clients succeeded, took %ld seconds\n", time(nullptr) - cur_time); | ||
136 | |||
137 | tox_options_free(options); | ||
138 | tox_kill(tox1); | ||
139 | tox_kill(tox2); | ||
140 | tox_kill(tox3); | ||
141 | } | ||
142 | |||
143 | int main(int argc, char *argv[]) | ||
144 | { | ||
145 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
146 | |||
147 | test_few_clients(); | ||
148 | return 0; | ||
149 | } | ||
diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c new file mode 100644 index 00000000..3a5088f3 --- /dev/null +++ b/auto_tests/send_message_test.c | |||
@@ -0,0 +1,120 @@ | |||
1 | /* Tests that we can send messages to friends. | ||
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 MESSAGE_FILLER 'G' | ||
25 | |||
26 | static void message_callback(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type, const uint8_t *string, size_t length, | ||
27 | void *userdata) | ||
28 | { | ||
29 | if (type != TOX_MESSAGE_TYPE_NORMAL) { | ||
30 | ck_abort_msg("Bad type"); | ||
31 | } | ||
32 | |||
33 | uint8_t cmp_msg[TOX_MAX_MESSAGE_LENGTH]; | ||
34 | memset(cmp_msg, MESSAGE_FILLER, sizeof(cmp_msg)); | ||
35 | |||
36 | if (length == TOX_MAX_MESSAGE_LENGTH && memcmp(string, cmp_msg, sizeof(cmp_msg)) == 0) { | ||
37 | bool *message_received = (bool *)userdata; | ||
38 | *message_received = true; | ||
39 | } | ||
40 | } | ||
41 | |||
42 | static void test_send_message(void) | ||
43 | { | ||
44 | printf("initialising 2 toxes\n"); | ||
45 | uint32_t index[] = { 1, 2 }; | ||
46 | const time_t cur_time = time(nullptr); | ||
47 | Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]); | ||
48 | Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]); | ||
49 | |||
50 | ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances"); | ||
51 | |||
52 | printf("tox1 adds tox2 as friend, tox2 adds tox1\n"); | ||
53 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; | ||
54 | tox_self_get_public_key(tox2, public_key); | ||
55 | tox_friend_add_norequest(tox1, public_key, nullptr); | ||
56 | tox_self_get_public_key(tox1, public_key); | ||
57 | tox_friend_add_norequest(tox2, public_key, nullptr); | ||
58 | |||
59 | printf("bootstrapping tox2 off tox1\n"); | ||
60 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
61 | tox_self_get_dht_id(tox1, dht_key); | ||
62 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
63 | |||
64 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
65 | |||
66 | while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || | ||
67 | tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) { | ||
68 | tox_iterate(tox1, nullptr); | ||
69 | tox_iterate(tox2, nullptr); | ||
70 | |||
71 | c_sleep(ITERATION_INTERVAL); | ||
72 | } | ||
73 | |||
74 | printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time); | ||
75 | const time_t con_time = time(nullptr); | ||
76 | |||
77 | while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP || | ||
78 | tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) { | ||
79 | tox_iterate(tox1, nullptr); | ||
80 | tox_iterate(tox2, nullptr); | ||
81 | |||
82 | c_sleep(ITERATION_INTERVAL); | ||
83 | } | ||
84 | |||
85 | printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); | ||
86 | |||
87 | tox_callback_friend_message(tox2, &message_callback); | ||
88 | |||
89 | uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1]; | ||
90 | memset(msgs, MESSAGE_FILLER, sizeof(msgs)); | ||
91 | |||
92 | TOX_ERR_FRIEND_SEND_MESSAGE errm; | ||
93 | tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm); | ||
94 | ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG, "TOX_MAX_MESSAGE_LENGTH is too small? error=%d", errm); | ||
95 | |||
96 | tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm); | ||
97 | ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm); | ||
98 | |||
99 | bool message_received = false; | ||
100 | |||
101 | while (!message_received) { | ||
102 | tox_iterate(tox1, nullptr); | ||
103 | tox_iterate(tox2, &message_received); | ||
104 | |||
105 | c_sleep(ITERATION_INTERVAL); | ||
106 | } | ||
107 | |||
108 | printf("tox clients messaging succeeded\n"); | ||
109 | |||
110 | tox_kill(tox1); | ||
111 | tox_kill(tox2); | ||
112 | } | ||
113 | |||
114 | int main(int argc, char *argv[]) | ||
115 | { | ||
116 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
117 | |||
118 | test_send_message(); | ||
119 | return 0; | ||
120 | } | ||
diff --git a/auto_tests/set_name_test.c b/auto_tests/set_name_test.c new file mode 100644 index 00000000..a7f01ec7 --- /dev/null +++ b/auto_tests/set_name_test.c | |||
@@ -0,0 +1,106 @@ | |||
1 | /* Tests that we can set our name. | ||
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 NICKNAME "Gentoo" | ||
25 | |||
26 | static void nickchange_callback(Tox *tox, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata) | ||
27 | { | ||
28 | ck_assert_msg(length == sizeof(NICKNAME) && memcmp(string, NICKNAME, sizeof(NICKNAME)) == 0, "Name not correct"); | ||
29 | bool *nickname_updated = (bool *)userdata; | ||
30 | *nickname_updated = true; | ||
31 | } | ||
32 | |||
33 | static void test_set_name(void) | ||
34 | { | ||
35 | printf("initialising 2 toxes\n"); | ||
36 | uint32_t index[] = { 1, 2 }; | ||
37 | const time_t cur_time = time(nullptr); | ||
38 | Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]); | ||
39 | Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]); | ||
40 | |||
41 | ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances"); | ||
42 | |||
43 | printf("tox1 adds tox2 as friend, tox2 adds tox1\n"); | ||
44 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; | ||
45 | tox_self_get_public_key(tox2, public_key); | ||
46 | tox_friend_add_norequest(tox1, public_key, nullptr); | ||
47 | tox_self_get_public_key(tox1, public_key); | ||
48 | tox_friend_add_norequest(tox2, public_key, nullptr); | ||
49 | |||
50 | printf("bootstrapping tox2 off tox1\n"); | ||
51 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
52 | tox_self_get_dht_id(tox1, dht_key); | ||
53 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
54 | |||
55 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
56 | |||
57 | while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || | ||
58 | tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) { | ||
59 | tox_iterate(tox1, nullptr); | ||
60 | tox_iterate(tox2, nullptr); | ||
61 | c_sleep(ITERATION_INTERVAL); | ||
62 | } | ||
63 | |||
64 | printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time); | ||
65 | const time_t con_time = time(nullptr); | ||
66 | |||
67 | while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP || | ||
68 | tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) { | ||
69 | tox_iterate(tox1, nullptr); | ||
70 | tox_iterate(tox2, nullptr); | ||
71 | c_sleep(ITERATION_INTERVAL); | ||
72 | } | ||
73 | |||
74 | printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); | ||
75 | |||
76 | tox_callback_friend_name(tox2, nickchange_callback); | ||
77 | TOX_ERR_SET_INFO err_n; | ||
78 | bool ret = tox_self_set_name(tox1, (const uint8_t *)NICKNAME, sizeof(NICKNAME), &err_n); | ||
79 | ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_name failed because %u\n", err_n); | ||
80 | |||
81 | bool nickname_updated = false; | ||
82 | |||
83 | while (!nickname_updated) { | ||
84 | tox_iterate(tox1, nullptr); | ||
85 | tox_iterate(tox2, &nickname_updated); | ||
86 | c_sleep(ITERATION_INTERVAL); | ||
87 | } | ||
88 | |||
89 | ck_assert_msg(tox_friend_get_name_size(tox2, 0, nullptr) == sizeof(NICKNAME), "Name length not correct"); | ||
90 | uint8_t temp_name[sizeof(NICKNAME)]; | ||
91 | tox_friend_get_name(tox2, 0, temp_name, nullptr); | ||
92 | ck_assert_msg(memcmp(temp_name, NICKNAME, sizeof(NICKNAME)) == 0, "Name not correct"); | ||
93 | |||
94 | printf("test_set_name succeeded, took %ld seconds\n", time(nullptr) - cur_time); | ||
95 | |||
96 | tox_kill(tox1); | ||
97 | tox_kill(tox2); | ||
98 | } | ||
99 | |||
100 | int main(int argc, char *argv[]) | ||
101 | { | ||
102 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
103 | |||
104 | test_set_name(); | ||
105 | return 0; | ||
106 | } | ||
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 | |||
26 | static 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 | |||
35 | static 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 | |||
107 | int main(int argc, char *argv[]) | ||
108 | { | ||
109 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
110 | |||
111 | test_set_status_message(); | ||
112 | return 0; | ||
113 | } | ||
diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c deleted file mode 100644 index d6613a9a..00000000 --- a/auto_tests/tox_test.c +++ /dev/null | |||
@@ -1,445 +0,0 @@ | |||
1 | /* Auto Tests | ||
2 | * | ||
3 | * Tox Tests | ||
4 | * | ||
5 | * The following tests were written with a small Tox network in mind. Therefore, | ||
6 | * each test timeout was set to one for a small Tox Network. If connected to the | ||
7 | * 'Global' Tox Network, traversing the DHT would take MUCH longer than the | ||
8 | * timeouts allow. Because of this running these tests require NO other Tox | ||
9 | * clients running or accessible on/to localhost. | ||
10 | * | ||
11 | */ | ||
12 | |||
13 | #ifndef _XOPEN_SOURCE | ||
14 | #define _XOPEN_SOURCE 600 | ||
15 | #endif | ||
16 | |||
17 | #ifdef HAVE_CONFIG_H | ||
18 | #include "config.h" | ||
19 | #endif | ||
20 | |||
21 | #include "check_compat.h" | ||
22 | |||
23 | #include <stdio.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <time.h> | ||
26 | |||
27 | #include "../toxcore/ccompat.h" | ||
28 | #include "../toxcore/tox.h" | ||
29 | #include "../toxcore/util.h" | ||
30 | |||
31 | #include "helpers.h" | ||
32 | |||
33 | /* The Travis-CI container responds poorly to ::1 as a localhost address | ||
34 | * You're encouraged to -D FORCE_TESTS_IPV6 on a local test */ | ||
35 | #ifdef FORCE_TESTS_IPV6 | ||
36 | #define TOX_LOCALHOST "::1" | ||
37 | #else | ||
38 | #define TOX_LOCALHOST "127.0.0.1" | ||
39 | #endif | ||
40 | |||
41 | static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) | ||
42 | { | ||
43 | if (*((uint32_t *)userdata) != 974536) { | ||
44 | return; | ||
45 | } | ||
46 | |||
47 | if (length == 7 && memcmp("Gentoo", data, 7) == 0) { | ||
48 | tox_friend_add_norequest(m, public_key, nullptr); | ||
49 | } | ||
50 | } | ||
51 | static uint32_t messages_received; | ||
52 | |||
53 | static void print_message(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type, const uint8_t *string, size_t length, | ||
54 | void *userdata) | ||
55 | { | ||
56 | if (*((uint32_t *)userdata) != 974536) { | ||
57 | return; | ||
58 | } | ||
59 | |||
60 | if (type != TOX_MESSAGE_TYPE_NORMAL) { | ||
61 | ck_abort_msg("Bad type"); | ||
62 | } | ||
63 | |||
64 | uint8_t cmp_msg[TOX_MAX_MESSAGE_LENGTH]; | ||
65 | memset(cmp_msg, 'G', sizeof(cmp_msg)); | ||
66 | |||
67 | if (length == TOX_MAX_MESSAGE_LENGTH && memcmp(string, cmp_msg, sizeof(cmp_msg)) == 0) { | ||
68 | ++messages_received; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | static uint32_t name_changes; | ||
73 | |||
74 | static void print_nickchange(Tox *m, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata) | ||
75 | { | ||
76 | if (*((uint32_t *)userdata) != 974536) { | ||
77 | return; | ||
78 | } | ||
79 | |||
80 | if (length == sizeof("Gentoo") && memcmp(string, "Gentoo", sizeof("Gentoo")) == 0) { | ||
81 | ++name_changes; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | static uint32_t status_m_changes; | ||
86 | static void print_status_m_change(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length, | ||
87 | void *user_data) | ||
88 | { | ||
89 | if (*((uint32_t *)user_data) != 974536) { | ||
90 | return; | ||
91 | } | ||
92 | |||
93 | if (length == sizeof("Installing Gentoo") && | ||
94 | memcmp(message, "Installing Gentoo", sizeof("Installing Gentoo")) == 0) { | ||
95 | ++status_m_changes; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | static uint32_t typing_changes; | ||
100 | |||
101 | static void print_typingchange(Tox *m, uint32_t friendnumber, bool typing, void *userdata) | ||
102 | { | ||
103 | if (*((uint32_t *)userdata) != 974536) { | ||
104 | return; | ||
105 | } | ||
106 | |||
107 | if (!typing) { | ||
108 | typing_changes = 1; | ||
109 | } else { | ||
110 | typing_changes = 2; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | static uint32_t custom_packet; | ||
115 | |||
116 | static void handle_custom_packet(Tox *m, uint32_t friend_num, const uint8_t *data, size_t len, void *object) | ||
117 | { | ||
118 | uint8_t number = *((uint32_t *)object); | ||
119 | |||
120 | if (len != TOX_MAX_CUSTOM_PACKET_SIZE) { | ||
121 | return; | ||
122 | } | ||
123 | |||
124 | VLA(uint8_t, f_data, len); | ||
125 | memset(f_data, number, len); | ||
126 | |||
127 | if (memcmp(f_data, data, len) == 0) { | ||
128 | ++custom_packet; | ||
129 | } else { | ||
130 | ck_abort_msg("Custom packet fail. %u", number); | ||
131 | } | ||
132 | |||
133 | return; | ||
134 | } | ||
135 | |||
136 | static unsigned int connected_t1; | ||
137 | static void tox_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data) | ||
138 | { | ||
139 | if (*((uint32_t *)user_data) != 974536) { | ||
140 | return; | ||
141 | } | ||
142 | |||
143 | if (connected_t1 && !connection_status) { | ||
144 | ck_abort_msg("Tox went offline"); | ||
145 | } | ||
146 | |||
147 | ck_assert_msg(connection_status == TOX_CONNECTION_UDP, "wrong status %u", connection_status); | ||
148 | |||
149 | connected_t1 = connection_status; | ||
150 | } | ||
151 | |||
152 | START_TEST(test_few_clients) | ||
153 | { | ||
154 | uint32_t index[] = { 1, 2, 3 }; | ||
155 | long long unsigned int con_time = 0, cur_time = time(nullptr); | ||
156 | TOX_ERR_NEW t_n_error; | ||
157 | Tox *tox1 = tox_new_log(nullptr, &t_n_error, &index[0]); | ||
158 | ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); | ||
159 | Tox *tox2 = tox_new_log(nullptr, &t_n_error, &index[1]); | ||
160 | ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); | ||
161 | Tox *tox3 = tox_new_log(nullptr, &t_n_error, &index[2]); | ||
162 | ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); | ||
163 | |||
164 | ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances"); | ||
165 | |||
166 | { | ||
167 | TOX_ERR_GET_PORT error; | ||
168 | uint16_t first_port = tox_self_get_udp_port(tox1, &error); | ||
169 | ck_assert_msg(33445 <= first_port && first_port <= 33545 - 2, | ||
170 | "First Tox instance did not bind to udp port inside [33445, 33543].\n"); | ||
171 | ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); | ||
172 | |||
173 | ck_assert_msg(tox_self_get_udp_port(tox2, &error) == first_port + 1, | ||
174 | "Second Tox instance did not bind to udp port %d.\n", first_port + 1); | ||
175 | ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); | ||
176 | |||
177 | ck_assert_msg(tox_self_get_udp_port(tox3, &error) == first_port + 2, | ||
178 | "Third Tox instance did not bind to udp port %d.\n", first_port + 2); | ||
179 | ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); | ||
180 | } | ||
181 | |||
182 | uint32_t to_compare = 974536; | ||
183 | connected_t1 = 0; | ||
184 | tox_callback_self_connection_status(tox1, tox_connection_status); | ||
185 | tox_callback_friend_request(tox2, accept_friend_request); | ||
186 | uint8_t address[TOX_ADDRESS_SIZE]; | ||
187 | tox_self_get_address(tox2, address); | ||
188 | uint32_t test = tox_friend_add(tox3, address, (const uint8_t *)"Gentoo", 7, nullptr); | ||
189 | ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); | ||
190 | |||
191 | uint8_t off = 1; | ||
192 | |||
193 | while (1) { | ||
194 | tox_iterate(tox1, &to_compare); | ||
195 | tox_iterate(tox2, &to_compare); | ||
196 | tox_iterate(tox3, &to_compare); | ||
197 | |||
198 | if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2) | ||
199 | && tox_self_get_connection_status(tox3)) { | ||
200 | if (off) { | ||
201 | printf("Toxes are online, took %llu seconds\n", time(nullptr) - cur_time); | ||
202 | con_time = time(nullptr); | ||
203 | off = 0; | ||
204 | } | ||
205 | |||
206 | if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP | ||
207 | && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) { | ||
208 | break; | ||
209 | } | ||
210 | } | ||
211 | |||
212 | c_sleep(50); | ||
213 | } | ||
214 | |||
215 | ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1); | ||
216 | printf("tox clients connected took %llu seconds\n", time(nullptr) - con_time); | ||
217 | to_compare = 974536; | ||
218 | tox_callback_friend_message(tox3, print_message); | ||
219 | uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1]; | ||
220 | memset(msgs, 'G', sizeof(msgs)); | ||
221 | TOX_ERR_FRIEND_SEND_MESSAGE errm; | ||
222 | tox_friend_send_message(tox2, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm); | ||
223 | ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG, "TOX_MAX_MESSAGE_LENGTH is too small\n"); | ||
224 | tox_friend_send_message(tox2, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm); | ||
225 | ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big\n"); | ||
226 | |||
227 | while (1) { | ||
228 | messages_received = 0; | ||
229 | tox_iterate(tox1, &to_compare); | ||
230 | tox_iterate(tox2, &to_compare); | ||
231 | tox_iterate(tox3, &to_compare); | ||
232 | |||
233 | if (messages_received) { | ||
234 | break; | ||
235 | } | ||
236 | |||
237 | c_sleep(50); | ||
238 | } | ||
239 | |||
240 | printf("tox clients messaging succeeded\n"); | ||
241 | |||
242 | unsigned int save_size1 = tox_get_savedata_size(tox2); | ||
243 | ck_assert_msg(save_size1 != 0, "save is invalid size %u", save_size1); | ||
244 | printf("%u\n", save_size1); | ||
245 | VLA(uint8_t, save1, save_size1); | ||
246 | tox_get_savedata(tox2, save1); | ||
247 | tox_kill(tox2); | ||
248 | |||
249 | struct Tox_Options *options = tox_options_new(nullptr); | ||
250 | tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); | ||
251 | tox_options_set_savedata_data(options, save1, save_size1); | ||
252 | tox2 = tox_new_log(options, nullptr, &index[1]); | ||
253 | cur_time = time(nullptr); | ||
254 | off = 1; | ||
255 | |||
256 | while (1) { | ||
257 | tox_iterate(tox1, &to_compare); | ||
258 | tox_iterate(tox2, &to_compare); | ||
259 | tox_iterate(tox3, &to_compare); | ||
260 | |||
261 | if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2) | ||
262 | && tox_self_get_connection_status(tox3)) { | ||
263 | if (off) { | ||
264 | printf("Toxes are online again after reloading, took %llu seconds\n", time(nullptr) - cur_time); | ||
265 | con_time = time(nullptr); | ||
266 | off = 0; | ||
267 | } | ||
268 | |||
269 | if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP | ||
270 | && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) { | ||
271 | break; | ||
272 | } | ||
273 | } | ||
274 | |||
275 | c_sleep(50); | ||
276 | } | ||
277 | |||
278 | printf("tox clients connected took %llu seconds\n", time(nullptr) - con_time); | ||
279 | tox_callback_friend_name(tox3, print_nickchange); | ||
280 | TOX_ERR_SET_INFO err_n; | ||
281 | bool succ = tox_self_set_name(tox2, (const uint8_t *)"Gentoo", sizeof("Gentoo"), &err_n); | ||
282 | ck_assert_msg(succ && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_name failed because %u\n", err_n); | ||
283 | |||
284 | while (1) { | ||
285 | name_changes = 0; | ||
286 | tox_iterate(tox1, &to_compare); | ||
287 | tox_iterate(tox2, &to_compare); | ||
288 | tox_iterate(tox3, &to_compare); | ||
289 | |||
290 | if (name_changes) { | ||
291 | break; | ||
292 | } | ||
293 | |||
294 | c_sleep(50); | ||
295 | } | ||
296 | |||
297 | ck_assert_msg(tox_friend_get_name_size(tox3, 0, nullptr) == sizeof("Gentoo"), "Name length not correct"); | ||
298 | uint8_t temp_name[sizeof("Gentoo")]; | ||
299 | tox_friend_get_name(tox3, 0, temp_name, nullptr); | ||
300 | ck_assert_msg(memcmp(temp_name, "Gentoo", sizeof("Gentoo")) == 0, "Name not correct"); | ||
301 | |||
302 | tox_callback_friend_status_message(tox3, print_status_m_change); | ||
303 | succ = tox_self_set_status_message(tox2, (const uint8_t *)"Installing Gentoo", sizeof("Installing Gentoo"), &err_n); | ||
304 | ck_assert_msg(succ && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_status_message failed because %u\n", err_n); | ||
305 | |||
306 | while (1) { | ||
307 | status_m_changes = 0; | ||
308 | tox_iterate(tox1, &to_compare); | ||
309 | tox_iterate(tox2, &to_compare); | ||
310 | tox_iterate(tox3, &to_compare); | ||
311 | |||
312 | if (status_m_changes) { | ||
313 | break; | ||
314 | } | ||
315 | |||
316 | c_sleep(50); | ||
317 | } | ||
318 | |||
319 | ck_assert_msg(tox_friend_get_status_message_size(tox3, 0, nullptr) == sizeof("Installing Gentoo"), | ||
320 | "status message length not correct"); | ||
321 | uint8_t temp_status_m[sizeof("Installing Gentoo")]; | ||
322 | tox_friend_get_status_message(tox3, 0, temp_status_m, nullptr); | ||
323 | ck_assert_msg(memcmp(temp_status_m, "Installing Gentoo", sizeof("Installing Gentoo")) == 0, | ||
324 | "status message not correct"); | ||
325 | |||
326 | tox_callback_friend_typing(tox2, &print_typingchange); | ||
327 | tox_self_set_typing(tox3, 0, 1, nullptr); | ||
328 | |||
329 | while (1) { | ||
330 | typing_changes = 0; | ||
331 | tox_iterate(tox1, &to_compare); | ||
332 | tox_iterate(tox2, &to_compare); | ||
333 | tox_iterate(tox3, &to_compare); | ||
334 | |||
335 | if (typing_changes == 2) { | ||
336 | break; | ||
337 | } | ||
338 | |||
339 | ck_assert_msg(typing_changes == 0, "Typing fail"); | ||
340 | |||
341 | c_sleep(50); | ||
342 | } | ||
343 | |||
344 | ck_assert_msg(tox_friend_get_typing(tox2, 0, nullptr) == 1, "Typing fail"); | ||
345 | tox_self_set_typing(tox3, 0, 0, nullptr); | ||
346 | |||
347 | while (1) { | ||
348 | typing_changes = 0; | ||
349 | tox_iterate(tox1, &to_compare); | ||
350 | tox_iterate(tox2, &to_compare); | ||
351 | tox_iterate(tox3, &to_compare); | ||
352 | |||
353 | if (typing_changes == 1) { | ||
354 | break; | ||
355 | } | ||
356 | |||
357 | ck_assert_msg(typing_changes == 0, "Typing fail"); | ||
358 | |||
359 | c_sleep(50); | ||
360 | } | ||
361 | |||
362 | TOX_ERR_FRIEND_QUERY err_t; | ||
363 | ck_assert_msg(tox_friend_get_typing(tox2, 0, &err_t) == 0, "Typing fail"); | ||
364 | ck_assert_msg(err_t == TOX_ERR_FRIEND_QUERY_OK, "Typing fail"); | ||
365 | |||
366 | uint32_t packet_number = 160; | ||
367 | tox_callback_friend_lossless_packet(tox3, &handle_custom_packet); | ||
368 | uint8_t data_c[TOX_MAX_CUSTOM_PACKET_SIZE + 1]; | ||
369 | memset(data_c, ((uint8_t)packet_number), sizeof(data_c)); | ||
370 | int ret = tox_friend_send_lossless_packet(tox2, 0, data_c, sizeof(data_c), nullptr); | ||
371 | ck_assert_msg(ret == 0, "tox_friend_send_lossless_packet bigger fail %i", ret); | ||
372 | ret = tox_friend_send_lossless_packet(tox2, 0, data_c, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr); | ||
373 | ck_assert_msg(ret == 1, "tox_friend_send_lossless_packet fail %i", ret); | ||
374 | |||
375 | while (1) { | ||
376 | custom_packet = 0; | ||
377 | tox_iterate(tox1, &to_compare); | ||
378 | tox_iterate(tox2, &to_compare); | ||
379 | tox_iterate(tox3, &packet_number); | ||
380 | |||
381 | if (custom_packet == 1) { | ||
382 | break; | ||
383 | } | ||
384 | |||
385 | ck_assert_msg(custom_packet == 0, "Lossless packet fail"); | ||
386 | |||
387 | c_sleep(50); | ||
388 | } | ||
389 | |||
390 | packet_number = 200; | ||
391 | tox_callback_friend_lossy_packet(tox3, &handle_custom_packet); | ||
392 | memset(data_c, ((uint8_t)packet_number), sizeof(data_c)); | ||
393 | ret = tox_friend_send_lossy_packet(tox2, 0, data_c, sizeof(data_c), nullptr); | ||
394 | ck_assert_msg(ret == 0, "tox_friend_send_lossy_packet bigger fail %i", ret); | ||
395 | ret = tox_friend_send_lossy_packet(tox2, 0, data_c, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr); | ||
396 | ck_assert_msg(ret == 1, "tox_friend_send_lossy_packet fail %i", ret); | ||
397 | |||
398 | while (1) { | ||
399 | custom_packet = 0; | ||
400 | tox_iterate(tox1, &to_compare); | ||
401 | tox_iterate(tox2, &to_compare); | ||
402 | tox_iterate(tox3, &packet_number); | ||
403 | |||
404 | if (custom_packet == 1) { | ||
405 | break; | ||
406 | } | ||
407 | |||
408 | ck_assert_msg(custom_packet == 0, "lossy packet fail"); | ||
409 | |||
410 | c_sleep(50); | ||
411 | } | ||
412 | |||
413 | printf("test_few_clients succeeded, took %llu seconds\n", time(nullptr) - cur_time); | ||
414 | |||
415 | tox_options_free(options); | ||
416 | tox_kill(tox1); | ||
417 | tox_kill(tox2); | ||
418 | tox_kill(tox3); | ||
419 | } | ||
420 | END_TEST | ||
421 | |||
422 | static Suite *tox_suite(void) | ||
423 | { | ||
424 | Suite *s = suite_create("Tox few clients"); | ||
425 | |||
426 | DEFTESTCASE(few_clients); | ||
427 | |||
428 | return s; | ||
429 | } | ||
430 | |||
431 | int main(int argc, char *argv[]) | ||
432 | { | ||
433 | srand((unsigned int) time(nullptr)); | ||
434 | |||
435 | Suite *tox = tox_suite(); | ||
436 | SRunner *test_runner = srunner_create(tox); | ||
437 | |||
438 | int number_failed = 0; | ||
439 | srunner_run_all(test_runner, CK_NORMAL); | ||
440 | number_failed = srunner_ntests_failed(test_runner); | ||
441 | |||
442 | srunner_free(test_runner); | ||
443 | |||
444 | return number_failed; | ||
445 | } | ||
diff --git a/auto_tests/typing_test.c b/auto_tests/typing_test.c new file mode 100644 index 00000000..837ed757 --- /dev/null +++ b/auto_tests/typing_test.c | |||
@@ -0,0 +1,111 @@ | |||
1 | /* Tests that our typing notifications work. | ||
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 | static void typing_callback(Tox *m, uint32_t friendnumber, bool typing, void *userdata) | ||
25 | { | ||
26 | bool *is_typing = (bool *)userdata; | ||
27 | *is_typing = typing; | ||
28 | } | ||
29 | |||
30 | static void test_typing(void) | ||
31 | { | ||
32 | printf("initialising 2 toxes\n"); | ||
33 | uint32_t index[] = { 1, 2 }; | ||
34 | const time_t cur_time = time(nullptr); | ||
35 | Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]); | ||
36 | Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]); | ||
37 | |||
38 | ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances"); | ||
39 | |||
40 | printf("tox1 adds tox2 as friend, tox2 adds tox1\n"); | ||
41 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; | ||
42 | tox_self_get_public_key(tox2, public_key); | ||
43 | tox_friend_add_norequest(tox1, public_key, nullptr); | ||
44 | tox_self_get_public_key(tox1, public_key); | ||
45 | tox_friend_add_norequest(tox2, public_key, nullptr); | ||
46 | |||
47 | printf("bootstrapping tox2 off tox1\n"); | ||
48 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
49 | tox_self_get_dht_id(tox1, dht_key); | ||
50 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
51 | |||
52 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
53 | |||
54 | while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || | ||
55 | tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) { | ||
56 | tox_iterate(tox1, nullptr); | ||
57 | tox_iterate(tox2, nullptr); | ||
58 | |||
59 | c_sleep(200); | ||
60 | } | ||
61 | |||
62 | printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time); | ||
63 | const time_t con_time = time(nullptr); | ||
64 | |||
65 | while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP || | ||
66 | tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) { | ||
67 | tox_iterate(tox1, nullptr); | ||
68 | tox_iterate(tox2, nullptr); | ||
69 | |||
70 | c_sleep(200); | ||
71 | } | ||
72 | |||
73 | printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); | ||
74 | |||
75 | tox_callback_friend_typing(tox2, &typing_callback); | ||
76 | tox_self_set_typing(tox1, 0, true, nullptr); | ||
77 | |||
78 | bool is_typing = false; | ||
79 | |||
80 | while (!is_typing) { | ||
81 | tox_iterate(tox1, nullptr); | ||
82 | tox_iterate(tox2, &is_typing); | ||
83 | c_sleep(200); | ||
84 | } | ||
85 | |||
86 | ck_assert_msg(tox_friend_get_typing(tox2, 0, nullptr) == 1, "Typing failure"); | ||
87 | tox_self_set_typing(tox1, 0, false, nullptr); | ||
88 | |||
89 | while (is_typing) { | ||
90 | tox_iterate(tox1, nullptr); | ||
91 | tox_iterate(tox2, &is_typing); | ||
92 | c_sleep(200); | ||
93 | } | ||
94 | |||
95 | TOX_ERR_FRIEND_QUERY err_t; | ||
96 | ck_assert_msg(tox_friend_get_typing(tox2, 0, &err_t) == 0, "Typing failure"); | ||
97 | ck_assert_msg(err_t == TOX_ERR_FRIEND_QUERY_OK, "Typing failure"); | ||
98 | |||
99 | printf("test_typing succeeded, took %ld seconds\n", time(nullptr) - cur_time); | ||
100 | |||
101 | tox_kill(tox1); | ||
102 | tox_kill(tox2); | ||
103 | } | ||
104 | |||
105 | int main(int argc, char *argv[]) | ||
106 | { | ||
107 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
108 | |||
109 | test_typing(); | ||
110 | return 0; | ||
111 | } | ||