/work/auto_tests/scenarios/scenario_save_load_test.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "framework/framework.h" |
2 | | #include <stdio.h> |
3 | | #include <string.h> |
4 | | #include <stdlib.h> |
5 | | |
6 | 137 | #define FRIEND_REQUEST_MSG "Gentoo" |
7 | | |
8 | | static void on_friend_request(const Tox_Event_Friend_Request *event, void *user_data) |
9 | 136 | { |
10 | 136 | ToxNode *self = (ToxNode *)user_data; |
11 | 136 | const uint8_t *msg = tox_event_friend_request_get_message(event); |
12 | 136 | size_t len = tox_event_friend_request_get_message_length(event); |
13 | | |
14 | 136 | if (len == 7 && memcmp(msg, FRIEND_REQUEST_MSG, 7) == 0) { |
15 | 136 | tox_friend_add_norequest(tox_node_get_tox(self), tox_event_friend_request_get_public_key(event), nullptr); |
16 | 136 | } |
17 | 136 | } |
18 | | |
19 | | static void relay_script(ToxNode *self, void *ctx) |
20 | 1 | { |
21 | 1 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
22 | | |
23 | 1 | ToxScenario *s = tox_node_get_scenario(self); |
24 | 1 | bool peers_done = false; |
25 | 859 | while (!peers_done && tox_scenario_is_running(self)) { |
26 | 858 | peers_done = true; |
27 | 860 | for (uint32_t i = 1; i < 3; ++i) { |
28 | 859 | if (!tox_node_is_finished(tox_scenario_get_node(s, i))) { |
29 | 857 | peers_done = false; |
30 | 857 | break; |
31 | 857 | } |
32 | 859 | } |
33 | 858 | if (!peers_done) { |
34 | 857 | tox_scenario_yield(self); |
35 | 857 | } |
36 | 858 | } |
37 | 1 | } |
38 | | |
39 | | static void peer_script(ToxNode *self, void *ctx) |
40 | 2 | { |
41 | 2 | Tox *tox = tox_node_get_tox(self); |
42 | 2 | uint32_t my_index = tox_node_get_index(self); |
43 | | |
44 | 2 | if (my_index == 1) { |
45 | 1 | tox_events_callback_friend_request(tox_node_get_dispatch(self), on_friend_request); |
46 | 1 | } |
47 | | |
48 | 2 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
49 | | |
50 | 2 | if (my_index == 2) { |
51 | 1 | ToxNode *peer1 = tox_scenario_get_node(tox_node_get_scenario(self), 1); |
52 | 1 | uint8_t addr[TOX_ADDRESS_SIZE]; |
53 | 1 | tox_node_get_address(peer1, addr); |
54 | 1 | tox_friend_add(tox, addr, (const uint8_t *)FRIEND_REQUEST_MSG, 7, nullptr); |
55 | 1 | } |
56 | | |
57 | 2 | WAIT_UNTIL(tox_node_is_friend_connected(self, 0)); |
58 | 2 | tox_node_log(self, "Connected to friend"); |
59 | | |
60 | | // Reload test |
61 | 2 | tox_node_log(self, "Reloading..."); |
62 | 2 | tox_node_reload(self); |
63 | 2 | tox_node_log(self, "Reloaded"); |
64 | | |
65 | 2 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
66 | 2 | WAIT_UNTIL(tox_node_is_friend_connected(self, 0)); |
67 | 2 | tox_node_log(self, "Connected to friend again after reload"); |
68 | 2 | } |
69 | | |
70 | | int main(int argc, char *argv[]) |
71 | 31 | { |
72 | 31 | ToxScenario *s = tox_scenario_new(argc, argv, 300000); |
73 | | |
74 | 31 | ToxNode *relay = tox_scenario_add_node(s, "Relay", relay_script, nullptr, 0); |
75 | 31 | ToxNode *peer1 = tox_scenario_add_node(s, "Peer1", peer_script, nullptr, 0); |
76 | 31 | ToxNode *peer2 = tox_scenario_add_node(s, "Peer2", peer_script, nullptr, 0); |
77 | | |
78 | | // All peers bootstrap from relay |
79 | 31 | tox_node_bootstrap(peer1, relay); |
80 | 31 | tox_node_bootstrap(peer2, relay); |
81 | | |
82 | 31 | ToxScenarioStatus res = tox_scenario_run(s); |
83 | 31 | if (res != TOX_SCENARIO_DONE) { |
84 | 0 | tox_scenario_log(s, "Test failed with status %u", res); |
85 | 0 | return 1; |
86 | 0 | } |
87 | | |
88 | 31 | tox_scenario_free(s); |
89 | 31 | return 0; |
90 | 31 | } |