]> git.meshlink.io Git - meshlink/blob - test/api_set_node_status_cb.c
7e5da192a418da966d90d34731601848985e1398
[meshlink] / test / api_set_node_status_cb.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <assert.h>
6 #include <string.h>
7
8 #include "meshlink.h"
9 #include "utils.h"
10
11 static struct sync_flag a_reachable;
12 static struct sync_flag b_reachable;
13
14 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
15         (void)mesh;
16
17         if(!reachable) {
18                 return;
19         }
20
21         if(!strcmp(node->name, "a")) {
22                 set_sync_flag(&a_reachable, true);
23         } else if(!strcmp(node->name, "b")) {
24                 set_sync_flag(&b_reachable, true);
25         }
26 }
27
28 int main(void) {
29         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
30
31         init_sync_flag(&a_reachable);
32         init_sync_flag(&b_reachable);
33
34         meshlink_handle_t *mesh1, *mesh2;
35         open_meshlink_pair_ephemeral(&mesh1, &mesh2, "api_set_node_status_cb");
36
37         // Test case #1: check that setting a valid status callback will cause it to be called
38         //               when the node itself is started or stopped
39
40         meshlink_set_node_status_cb(mesh1, status_cb);
41         assert(meshlink_start(mesh1));
42         assert(wait_sync_flag(&a_reachable, 5));
43
44         // Test case #2: check that the status callback will be called when another peer is started
45
46         assert(meshlink_start(mesh2));
47         assert(wait_sync_flag(&b_reachable, 5));
48
49         // Test case #3: check that passing a NULL pointer for the mesh returns an error
50
51         meshlink_errno = MESHLINK_OK;
52         meshlink_set_node_status_cb(NULL, status_cb);
53         assert(meshlink_errno == MESHLINK_EINVAL);
54
55         // Done.
56
57         close_meshlink_pair(mesh1, mesh2);
58 }