/work/auto_tests/scenarios/framework/framework.h
Line | Count | Source |
1 | | #ifndef TOX_TEST_FRAMEWORK_H |
2 | | #define TOX_TEST_FRAMEWORK_H |
3 | | |
4 | | #include <stdbool.h> |
5 | | #include <stdint.h> |
6 | | #include <stdio.h> |
7 | | #include <stdlib.h> |
8 | | |
9 | | #include "../../check_compat.h" |
10 | | #include "../../../toxcore/attributes.h" |
11 | | #include "../../../toxcore/ccompat.h" |
12 | | #include "../../../toxcore/tox.h" |
13 | | #include "../../../toxcore/tox_dispatch.h" |
14 | | |
15 | | // --- Constants --- |
16 | 19.9k | #define TOX_SCENARIO_TICK_MS 50 |
17 | | |
18 | | // --- Opaque Types --- |
19 | | typedef struct ToxNode ToxNode; |
20 | | typedef struct ToxScenario ToxScenario; |
21 | | |
22 | | typedef enum { |
23 | | TOX_SCENARIO_OK, |
24 | | TOX_SCENARIO_RUNNING, |
25 | | TOX_SCENARIO_DONE, |
26 | | TOX_SCENARIO_ERROR, |
27 | | TOX_SCENARIO_TIMEOUT |
28 | | } ToxScenarioStatus; |
29 | | |
30 | | /** |
31 | | * A script function that defines the behavior of a node. |
32 | | */ |
33 | | typedef void tox_node_script_cb(ToxNode *self, void *ctx); |
34 | | |
35 | | // --- Core API --- |
36 | | |
37 | | ToxScenario *tox_scenario_new(int argc, char *const argv[], uint64_t timeout_ms); |
38 | | void tox_scenario_free(ToxScenario *s); |
39 | | |
40 | | /** |
41 | | * Adds a node to the scenario and assigns it a script. |
42 | | * @param ctx_size The size of the context struct to be mirrored (snapshot) at each tick. |
43 | | */ |
44 | | ToxNode *tox_scenario_add_node(ToxScenario *s, const char *alias, tox_node_script_cb *script, void *ctx, size_t ctx_size); |
45 | | |
46 | | /** |
47 | | * Extended version of add_node that accepts custom Tox_Options. |
48 | | */ |
49 | | ToxNode *tox_scenario_add_node_ex(ToxScenario *s, const char *alias, tox_node_script_cb *script, void *ctx, size_t ctx_size, const Tox_Options *options); |
50 | | |
51 | | /** |
52 | | * Returns a node by its index in the scenario. |
53 | | */ |
54 | | ToxNode *tox_scenario_get_node(ToxScenario *s, uint32_t index); |
55 | | |
56 | | /** |
57 | | * Runs the scenario until all nodes complete their scripts or timeout occurs. |
58 | | */ |
59 | | ToxScenarioStatus tox_scenario_run(ToxScenario *s); |
60 | | |
61 | | // --- Logging API --- |
62 | | |
63 | | /** |
64 | | * Logs a message with the node's alias/index and current virtual time. |
65 | | */ |
66 | | void tox_node_log(ToxNode *node, const char *format, ...) GNU_PRINTF(2, 3); |
67 | | |
68 | | /** |
69 | | * Logs a message from the scenario runner with current virtual time. |
70 | | */ |
71 | | void tox_scenario_log(const ToxScenario *s, const char *format, ...) GNU_PRINTF(2, 3); |
72 | | |
73 | | // --- Script API (Callable from within scripts) --- |
74 | | |
75 | | /** |
76 | | * Returns the underlying Tox instance. |
77 | | */ |
78 | | Tox *tox_node_get_tox(const ToxNode *node); |
79 | | |
80 | | /** |
81 | | * Returns the mirrored address of the node. |
82 | | */ |
83 | | void tox_node_get_address(const ToxNode *node, uint8_t *address); |
84 | | |
85 | | /** |
86 | | * Returns the scenario this node belongs to. |
87 | | */ |
88 | | ToxScenario *tox_node_get_scenario(const ToxNode *node); |
89 | | |
90 | | /** |
91 | | * Returns the alias of the node. |
92 | | */ |
93 | | const char *tox_node_get_alias(const ToxNode *node); |
94 | | |
95 | | /** |
96 | | * Returns the index of the node in the scenario. |
97 | | */ |
98 | | uint32_t tox_node_get_index(const ToxNode *node); |
99 | | |
100 | | /** |
101 | | * Returns the script context associated with this node. |
102 | | */ |
103 | | void *tox_node_get_script_ctx(const ToxNode *node); |
104 | | |
105 | | /** |
106 | | * Returns a read-only snapshot of a peer's context. |
107 | | * The snapshot is updated at every tick. |
108 | | */ |
109 | | const void *tox_node_get_peer_ctx(const ToxNode *node); |
110 | | |
111 | | /** |
112 | | * Returns the event dispatcher associated with this node. |
113 | | */ |
114 | | Tox_Dispatch *tox_node_get_dispatch(const ToxNode *node); |
115 | | |
116 | | /** |
117 | | * Predicate helpers for common wait conditions. |
118 | | */ |
119 | | bool tox_node_is_self_connected(const ToxNode *node); |
120 | | bool tox_node_is_friend_connected(const ToxNode *node, uint32_t friend_number); |
121 | | |
122 | | Tox_Connection tox_node_get_connection_status(const ToxNode *node); |
123 | | Tox_Connection tox_node_get_friend_connection_status(const ToxNode *node, uint32_t friend_number); |
124 | | |
125 | | /** |
126 | | * Conference helpers. |
127 | | */ |
128 | | uint32_t tox_node_get_conference_peer_count(const ToxNode *node, uint32_t conference_number); |
129 | | |
130 | | /** |
131 | | * Network simulation. |
132 | | */ |
133 | | void tox_node_set_offline(ToxNode *node, bool offline); |
134 | | bool tox_node_is_offline(const ToxNode *node); |
135 | | |
136 | | /** |
137 | | * Returns true if the node has finished its script. |
138 | | */ |
139 | | bool tox_node_is_finished(const ToxNode *node); |
140 | | |
141 | | /** |
142 | | * High-level predicates for specific state changes. |
143 | | */ |
144 | | bool tox_node_friend_name_is(const ToxNode *node, uint32_t friend_number, const uint8_t *name, size_t length); |
145 | | bool tox_node_friend_status_message_is(const ToxNode *node, uint32_t friend_number, const uint8_t *msg, size_t length); |
146 | | bool tox_node_friend_typing_is(const ToxNode *node, uint32_t friend_number, bool is_typing); |
147 | | |
148 | | /** |
149 | | * Yields execution to the next tick. |
150 | | */ |
151 | | void tox_scenario_yield(ToxNode *self); |
152 | | |
153 | | /** |
154 | | * Returns the current virtual time in milliseconds. |
155 | | */ |
156 | | uint64_t tox_scenario_get_time(ToxScenario *s); |
157 | | |
158 | | /** |
159 | | * Returns true if the scenario is still running. |
160 | | */ |
161 | | bool tox_scenario_is_running(ToxNode *self); |
162 | | |
163 | | /** |
164 | | * Blocks until a condition is true. Polls every tick. |
165 | | */ |
166 | 89.3k | #define WAIT_UNTIL(cond) do { while(!(cond) && tox_scenario_is_running(self)) { tox_scenario_yield(self); } } while(0) |
167 | | |
168 | | /** |
169 | | * Blocks until a specific event is observed. |
170 | | */ |
171 | | void tox_scenario_wait_for_event(ToxNode *self, Tox_Event_Type type); |
172 | | |
173 | 29 | #define WAIT_FOR_EVENT(type) tox_scenario_wait_for_event(self, type) |
174 | | |
175 | | /** |
176 | | * Synchronization barriers. |
177 | | * Blocks until all active nodes have reached this barrier. |
178 | | */ |
179 | | void tox_scenario_barrier_wait(ToxNode *self); |
180 | | |
181 | | /** |
182 | | * High-level wait helpers. |
183 | | */ |
184 | | void tox_node_wait_for_self_connected(ToxNode *self); |
185 | | void tox_node_wait_for_friend_connected(ToxNode *self, uint32_t friend_number); |
186 | | |
187 | | // --- High-Level Helpers --- |
188 | | |
189 | | void tox_node_bootstrap(ToxNode *a, ToxNode *b); |
190 | | void tox_node_friend_add(ToxNode *a, ToxNode *b); |
191 | | |
192 | | /** |
193 | | * Reloads a node from its own savedata. |
194 | | * This simulates a client restart. |
195 | | */ |
196 | | void tox_node_reload(ToxNode *node); |
197 | | |
198 | | #endif // TOX_TEST_FRAMEWORK_H |