/work/auto_tests/scenarios/scenario_save_friend_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 | | typedef struct { |
7 | | uint8_t *name; |
8 | | uint8_t *status_message; |
9 | | size_t name_len; |
10 | | size_t status_len; |
11 | | } ReferenceData; |
12 | | |
13 | | static void alice_script(ToxNode *self, void *ctx) |
14 | 1 | { |
15 | 1 | const ReferenceData *ref = (const ReferenceData *)ctx; |
16 | | |
17 | 1 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
18 | 1 | WAIT_UNTIL(tox_node_is_friend_connected(self, 0)); |
19 | | |
20 | | // Wait for name and status to propagate |
21 | 1 | WAIT_UNTIL(tox_node_friend_name_is(self, 0, ref->name, ref->name_len)); |
22 | 1 | WAIT_UNTIL(tox_node_friend_status_message_is(self, 0, ref->status_message, ref->status_len)); |
23 | 1 | tox_node_log(self, "Received reference name and status from Bob"); |
24 | | |
25 | | // Save and Reload |
26 | 1 | tox_node_log(self, "Reloading..."); |
27 | 1 | tox_node_reload(self); |
28 | 1 | tox_node_log(self, "Reloaded"); |
29 | | |
30 | 1 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
31 | 1 | WAIT_UNTIL(tox_node_is_friend_connected(self, 0)); |
32 | | |
33 | | // Check if Bob's info is still correct after reload |
34 | 1 | ck_assert(tox_node_friend_name_is(self, 0, ref->name, ref->name_len)); |
35 | 1 | ck_assert(tox_node_friend_status_message_is(self, 0, ref->status_message, ref->status_len)); |
36 | 1 | tox_node_log(self, "Bob's name and status are still correct after reload"); |
37 | 1 | } |
38 | | |
39 | | static void bob_script(ToxNode *self, void *ctx) |
40 | 1 | { |
41 | 1 | ReferenceData *ref = (ReferenceData *)ctx; |
42 | 1 | Tox *tox = tox_node_get_tox(self); |
43 | | |
44 | 1 | tox_self_set_name(tox, ref->name, ref->name_len, nullptr); |
45 | 1 | tox_self_set_status_message(tox, ref->status_message, ref->status_len, nullptr); |
46 | | |
47 | 1 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
48 | 1 | WAIT_UNTIL(tox_node_is_friend_connected(self, 0)); |
49 | | |
50 | | // Stay online for Alice |
51 | 1 | tox_node_log(self, "Waiting for Alice to finish..."); |
52 | 627 | while (!tox_node_is_finished(tox_scenario_get_node(tox_node_get_scenario(self), 0))) { |
53 | 626 | tox_scenario_yield(self); |
54 | 626 | } |
55 | 1 | tox_node_log(self, "Alice finished, Bob is done"); |
56 | 1 | } |
57 | | |
58 | | int main(int argc, char *argv[]) |
59 | 2 | { |
60 | 2 | ToxScenario *s = tox_scenario_new(argc, argv, 300000); |
61 | | |
62 | 2 | ReferenceData ref; |
63 | 2 | ref.name_len = tox_max_name_length(); |
64 | 2 | ref.status_len = tox_max_status_message_length(); |
65 | 2 | ref.name = (uint8_t *)malloc(ref.name_len); |
66 | 2 | ref.status_message = (uint8_t *)malloc(ref.status_len); |
67 | | |
68 | 134 | for (size_t i = 0; i < ref.name_len; ++i) { |
69 | 132 | ref.name[i] = (uint8_t)(rand() % 256); |
70 | 132 | } |
71 | 1.01k | for (size_t i = 0; i < ref.status_len; ++i) { |
72 | 1.01k | ref.status_message[i] = (uint8_t)(rand() % 256); |
73 | 1.01k | } |
74 | | |
75 | 2 | ToxNode *alice = tox_scenario_add_node(s, "Alice", alice_script, &ref, 0); |
76 | 2 | ToxNode *bob = tox_scenario_add_node(s, "Bob", bob_script, &ref, 0); |
77 | | |
78 | 2 | tox_node_friend_add(alice, bob); |
79 | 2 | tox_node_friend_add(bob, alice); |
80 | 2 | tox_node_bootstrap(alice, bob); |
81 | 2 | tox_node_bootstrap(bob, alice); |
82 | | |
83 | 2 | ToxScenarioStatus res = tox_scenario_run(s); |
84 | 2 | if (res != TOX_SCENARIO_DONE) { |
85 | 0 | tox_scenario_log(s, "Test failed with status %u", res); |
86 | 0 | return 1; |
87 | 0 | } |
88 | | |
89 | 2 | tox_scenario_free(s); |
90 | 2 | free(ref.name); |
91 | 2 | free(ref.status_message); |
92 | 2 | return 0; |
93 | 2 | } |