summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-07-04 14:42:02 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-05 23:41:27 +0000
commit597f23bdd99e0962a6912231938543b42e6f43ab (patch)
tree52dad44ceced79bd333f538739b7169fca3a9f1e
parent8739f7fccb7cafc54ca0f5fa074c9a740f7048ba (diff)
Use run_auto_test.h test fixture for some auto-tests.
Most of the auto-tests should use this fixture, but I've only done a few to set an example.
-rwxr-xr-x.travis/cmake-linux3
-rwxr-xr-x.travis/cmake-osx3
-rw-r--r--auto_tests/conference_double_invite_test.c142
-rw-r--r--auto_tests/friend_connection_test.c4
-rw-r--r--auto_tests/overflow_recvq_test.c4
-rw-r--r--auto_tests/overflow_sendq_test.c4
-rw-r--r--auto_tests/run_auto_test.h3
-rw-r--r--auto_tests/send_message_test.c96
8 files changed, 69 insertions, 190 deletions
diff --git a/.travis/cmake-linux b/.travis/cmake-linux
index e3517a09..e5874c19 100755
--- a/.travis/cmake-linux
+++ b/.travis/cmake-linux
@@ -62,7 +62,8 @@ travis_script() {
62 62
63 cd _build # pushd 63 cd _build # pushd
64 make "-j$NPROC" -k install 64 make "-j$NPROC" -k install
65 make "-j$NPROC" test ARGS="-j50" CTEST_OUTPUT_ON_FAILURE=1 65 make "-j$NPROC" test ARGS="-j50" || \
66 make "-j$NPROC" test ARGS="-j50 --rerun-failed" CTEST_OUTPUT_ON_FAILURE=1
66 cd - # popd 67 cd - # popd
67 68
68 other/astyle/format-source . "$ASTYLE" 69 other/astyle/format-source . "$ASTYLE"
diff --git a/.travis/cmake-osx b/.travis/cmake-osx
index 529a2309..524f7116 100755
--- a/.travis/cmake-osx
+++ b/.travis/cmake-osx
@@ -35,7 +35,8 @@ travis_script() {
35 35
36 cd _build # pushd 36 cd _build # pushd
37 make "-j$NPROC" -k install 37 make "-j$NPROC" -k install
38 make "-j$NPROC" test ARGS="-j50" CTEST_OUTPUT_ON_FAILURE=1 38 make "-j$NPROC" test ARGS="-j50" || \
39 make "-j$NPROC" test ARGS="-j50 --rerun-failed" CTEST_OUTPUT_ON_FAILURE=1
39 cd - # popd 40 cd - # popd
40} 41}
41 42
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
12typedef struct State { 8typedef 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
21static 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
29static 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
38static void handle_conference_invite(Tox *tox, uint32_t friend_number, TOX_CONFERENCE_TYPE type, const uint8_t *cookie, 19static 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
56int main(void) 39static 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); 82int 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}
diff --git a/auto_tests/friend_connection_test.c b/auto_tests/friend_connection_test.c
index 7af1b575..13d08b65 100644
--- a/auto_tests/friend_connection_test.c
+++ b/auto_tests/friend_connection_test.c
@@ -9,9 +9,7 @@
9#define _XOPEN_SOURCE 600 9#define _XOPEN_SOURCE 600
10#endif 10#endif
11 11
12#include "check_compat.h" 12#include <stdint.h>
13
14#include "../toxcore/tox.h"
15 13
16typedef struct State { 14typedef struct State {
17 uint32_t index; 15 uint32_t index;
diff --git a/auto_tests/overflow_recvq_test.c b/auto_tests/overflow_recvq_test.c
index 3227c5f4..cdad2442 100644
--- a/auto_tests/overflow_recvq_test.c
+++ b/auto_tests/overflow_recvq_test.c
@@ -5,9 +5,7 @@
5#define _XOPEN_SOURCE 600 5#define _XOPEN_SOURCE 600
6#endif 6#endif
7 7
8#include "check_compat.h" 8#include <stdint.h>
9
10#include "../toxcore/tox.h"
11 9
12typedef struct State { 10typedef struct State {
13 uint32_t index; 11 uint32_t index;
diff --git a/auto_tests/overflow_sendq_test.c b/auto_tests/overflow_sendq_test.c
index 94f5c12a..cc6eeda9 100644
--- a/auto_tests/overflow_sendq_test.c
+++ b/auto_tests/overflow_sendq_test.c
@@ -5,9 +5,7 @@
5#define _XOPEN_SOURCE 600 5#define _XOPEN_SOURCE 600
6#endif 6#endif
7 7
8#include "check_compat.h" 8#include <stdint.h>
9
10#include "../toxcore/tox.h"
11 9
12typedef struct State { 10typedef struct State {
13 uint32_t index; 11 uint32_t index;
diff --git a/auto_tests/run_auto_test.h b/auto_tests/run_auto_test.h
index 6ae8ad6a..a4c638ff 100644
--- a/auto_tests/run_auto_test.h
+++ b/auto_tests/run_auto_test.h
@@ -1,3 +1,6 @@
1#include <stdlib.h> // calloc, free
2
3#include "check_compat.h"
1#include "helpers.h" 4#include "helpers.h"
2 5
3static bool all_connected(uint32_t tox_count, Tox **toxes) 6static bool all_connected(uint32_t tox_count, Tox **toxes)
diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c
index 35ef5a62..822ea64f 100644
--- a/auto_tests/send_message_test.c
+++ b/auto_tests/send_message_test.c
@@ -5,27 +5,24 @@
5#define _XOPEN_SOURCE 600 5#define _XOPEN_SOURCE 600
6#endif 6#endif
7 7
8#ifdef HAVE_CONFIG_H 8#include <stdbool.h>
9#include "config.h" 9#include <stdint.h>
10#endif
11
12#include "check_compat.h"
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <time.h>
17 10
18#include "../toxcore/ccompat.h" 11typedef struct State {
19#include "../toxcore/tox.h" 12 uint32_t index;
20#include "../toxcore/util.h" 13 bool message_received;
14} State;
21 15
22#include "helpers.h" 16#include "run_auto_test.h"
23 17
24#define MESSAGE_FILLER 'G' 18#define MESSAGE_FILLER 'G'
25 19
26static void message_callback(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type, const uint8_t *string, size_t length, 20static void message_callback(
27 void *userdata) 21 Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type,
22 const uint8_t *string, size_t length, void *userdata)
28{ 23{
24 State *state = (State *)userdata;
25
29 if (type != TOX_MESSAGE_TYPE_NORMAL) { 26 if (type != TOX_MESSAGE_TYPE_NORMAL) {
30 ck_abort_msg("Bad type"); 27 ck_abort_msg("Bad type");
31 } 28 }
@@ -34,87 +31,36 @@ static void message_callback(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE typ
34 memset(cmp_msg, MESSAGE_FILLER, sizeof(cmp_msg)); 31 memset(cmp_msg, MESSAGE_FILLER, sizeof(cmp_msg));
35 32
36 if (length == TOX_MAX_MESSAGE_LENGTH && memcmp(string, cmp_msg, sizeof(cmp_msg)) == 0) { 33 if (length == TOX_MAX_MESSAGE_LENGTH && memcmp(string, cmp_msg, sizeof(cmp_msg)) == 0) {
37 bool *message_received = (bool *)userdata; 34 state->message_received = true;
38 *message_received = true;
39 } 35 }
40} 36}
41 37
42static void test_send_message(void) 38static void send_message_test(Tox **toxes, State *state)
43{ 39{
44 printf("initialising 2 toxes\n"); 40 tox_callback_friend_message(toxes[1], &message_callback);
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 41
89 uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1]; 42 uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1];
90 memset(msgs, MESSAGE_FILLER, sizeof(msgs)); 43 memset(msgs, MESSAGE_FILLER, sizeof(msgs));
91 44
92 TOX_ERR_FRIEND_SEND_MESSAGE errm; 45 TOX_ERR_FRIEND_SEND_MESSAGE errm;
93 tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm); 46 tox_friend_send_message(toxes[0], 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); 47 ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG, "TOX_MAX_MESSAGE_LENGTH is too small? error=%d", errm);
95 48
96 tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm); 49 tox_friend_send_message(toxes[0], 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); 50 ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm);
98 51
99 bool message_received = false; 52 while (!state[1].message_received) {
100 53 tox_iterate(toxes[0], &state[0]);
101 while (!message_received) { 54 tox_iterate(toxes[1], &state[1]);
102 tox_iterate(tox1, nullptr);
103 tox_iterate(tox2, &message_received);
104 55
105 c_sleep(ITERATION_INTERVAL); 56 c_sleep(ITERATION_INTERVAL);
106 } 57 }
107
108 printf("tox clients messaging succeeded\n");
109
110 tox_kill(tox1);
111 tox_kill(tox2);
112} 58}
113 59
114int main(void) 60int main(void)
115{ 61{
116 setvbuf(stdout, nullptr, _IONBF, 0); 62 setvbuf(stdout, nullptr, _IONBF, 0);
117 63
118 test_send_message(); 64 run_auto_test(2, send_message_test);
119 return 0; 65 return 0;
120} 66}