From 6b125b5b68e2657eae9b36f23d7f569fb9e49f8d Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 13 Jun 2020 00:41:47 +0200 Subject: [PATCH] Fix warnings and missing mutex/cond initialization in the test suite. --- test/basicpp.cpp | 2 +- test/blacklist.c | 4 ++++ test/channels-aio-cornercases.c | 20 +++++++++++--------- test/channels-aio-fd.c | 10 ++++++---- test/channels-aio.c | 10 +++++++--- test/channels-cornercases.c | 3 +++ test/channels-failure.c | 3 +++ test/channels-fork.c | 8 ++++++-- test/channels-udp.c | 5 ++++- test/channels.c | 2 ++ test/duplicate.c | 2 ++ test/echo-fork.c | 4 ++++ test/get-all-nodes.c | 2 ++ test/import-export.c | 2 ++ test/invite-join.c | 2 ++ test/stream.c | 16 ++++++++-------- test/trio.c | 4 ++++ test/trio2.c | 4 ++++ test/utils.c | 20 +++++++++++++------- test/utils.h | 3 ++- 20 files changed, 90 insertions(+), 36 deletions(-) diff --git a/test/basicpp.cpp b/test/basicpp.cpp index 2f4e5c84..c0550ecf 100644 --- a/test/basicpp.cpp +++ b/test/basicpp.cpp @@ -9,7 +9,7 @@ using namespace std; -int main() { +int main(void) { assert(meshlink::destroy("basicpp_conf")); // Open a new meshlink instance. diff --git a/test/blacklist.c b/test/blacklist.c index 8b081963..1f5cd605 100644 --- a/test/blacklist.c +++ b/test/blacklist.c @@ -45,6 +45,10 @@ static void baz_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool r } int main(void) { + init_sync_flag(&bar_connected); + init_sync_flag(&bar_disconnected); + init_sync_flag(&baz_connected); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); // Create three instances. diff --git a/test/channels-aio-cornercases.c b/test/channels-aio-cornercases.c index 0640c7df..dba4de51 100644 --- a/test/channels-aio-cornercases.c +++ b/test/channels-aio-cornercases.c @@ -27,8 +27,6 @@ struct channel_info { struct aio_info aio_infos[2]; }; -static size_t b_received_len; -static struct timeval b_received_tv; static struct sync_flag b_received_flag; static void aio_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, void *priv) { @@ -66,14 +64,14 @@ static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint switch(port) { case 1: case 3: - meshlink_channel_aio_receive(mesh, channel, info->data, size / 4, aio_cb, &info->aio_infos[0]); - meshlink_channel_aio_receive(mesh, channel, info->data + size / 4, size - size / 4, aio_cb_close, &info->aio_infos[1]); + assert(meshlink_channel_aio_receive(mesh, channel, info->data, size / 4, aio_cb, &info->aio_infos[0])); + assert(meshlink_channel_aio_receive(mesh, channel, info->data + size / 4, size - size / 4, aio_cb_close, &info->aio_infos[1])); break; case 2: case 4: - meshlink_channel_aio_receive(mesh, channel, info->data, size / 4, aio_cb_close, &info->aio_infos[0]); - meshlink_channel_aio_receive(mesh, channel, info->data + size / 4, size - size / 4, aio_cb, &info->aio_infos[1]); + assert(meshlink_channel_aio_receive(mesh, channel, info->data, size / 4, aio_cb_close, &info->aio_infos[0])); + assert(meshlink_channel_aio_receive(mesh, channel, info->data + size / 4, size - size / 4, aio_cb, &info->aio_infos[1])); set_sync_flag(&info->aio_infos[1].flag, true); break; @@ -84,9 +82,8 @@ static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint return true; } -int main(int argc, char *argv[]) { - (void)argc; - (void)argv; +int main(void) { + init_sync_flag(&b_received_flag); meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb); @@ -107,6 +104,11 @@ int main(int argc, char *argv[]) { memset(out_infos, 0, sizeof(out_infos)); for(size_t i = 0; i < nchannels; i++) { + init_sync_flag(&in_infos[i].aio_infos[0].flag); + init_sync_flag(&in_infos[i].aio_infos[1].flag); + init_sync_flag(&out_infos[i].aio_infos[0].flag); + init_sync_flag(&out_infos[i].aio_infos[1].flag); + in_infos[i].data = malloc(size); assert(in_infos[i].data); out_infos[i].data = outdata; diff --git a/test/channels-aio-fd.c b/test/channels-aio-fd.c index 40fcf7fc..2a32c38a 100644 --- a/test/channels-aio-fd.c +++ b/test/channels-aio-fd.c @@ -65,10 +65,7 @@ static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint return true; } -int main(int argc, char *argv[]) { - (void)argc; - (void)argv; - +int main(void) { meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb); // Prepare file @@ -93,6 +90,11 @@ int main(int argc, char *argv[]) { memset(out_infos, 0, sizeof(out_infos)); for(size_t i = 0; i < nchannels; i++) { + init_sync_flag(&in_infos[i].aio_infos[0].flag); + init_sync_flag(&in_infos[i].aio_infos[1].flag); + init_sync_flag(&out_infos[i].aio_infos[0].flag); + init_sync_flag(&out_infos[i].aio_infos[1].flag); + char filename[PATH_MAX]; snprintf(filename, sizeof(filename), "channels_aio_fd.out%d", (int)i); in_infos[i].file = fopen(filename, "w"); diff --git a/test/channels-aio.c b/test/channels-aio.c index a0ce5715..c43f1da1 100644 --- a/test/channels-aio.c +++ b/test/channels-aio.c @@ -86,9 +86,8 @@ static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint return true; } -int main(int argc, char *argv[]) { - (void)argc; - (void)argv; +int main(void) { + init_sync_flag(&b_received_flag); meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb); @@ -110,6 +109,11 @@ int main(int argc, char *argv[]) { memset(out_infos, 0, sizeof(out_infos)); for(size_t i = 0; i < nchannels; i++) { + init_sync_flag(&in_infos[i].aio_infos[0].flag); + init_sync_flag(&in_infos[i].aio_infos[1].flag); + init_sync_flag(&out_infos[i].aio_infos[0].flag); + init_sync_flag(&out_infos[i].aio_infos[1].flag); + in_infos[i].data = malloc(size); assert(in_infos[i].data); out_infos[i].data = outdata; diff --git a/test/channels-cornercases.c b/test/channels-cornercases.c index 3b605bb5..b8534030 100644 --- a/test/channels-cornercases.c +++ b/test/channels-cornercases.c @@ -96,6 +96,7 @@ int main(void) { assert(nb); struct sync_flag channel_opened = {.flag = false}; + init_sync_flag(&channel_opened); meshlink_channel_t *channel = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_opened, 0); assert(channel); @@ -136,6 +137,7 @@ int main(void) { // Send a message to b struct sync_flag channel_closed = {.flag = false}; + init_sync_flag(&channel_closed); channel->priv = &channel_closed; assert(meshlink_channel_send(a, channel, "Hello", 5) == 5); @@ -146,6 +148,7 @@ int main(void) { // Try to create a second channel struct sync_flag channel_polled = {.flag = false}; + init_sync_flag(&channel_polled); meshlink_channel_t *channel2 = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_polled, 0); assert(channel2); diff --git a/test/channels-failure.c b/test/channels-failure.c index e9b4c7ba..defdca96 100644 --- a/test/channels-failure.c +++ b/test/channels-failure.c @@ -44,6 +44,9 @@ static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, con } int main(void) { + init_sync_flag(&poll_flag); + init_sync_flag(&receive_flag); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); // Open two meshlink instances. diff --git a/test/channels-fork.c b/test/channels-fork.c index 483f430b..38ae9b19 100644 --- a/test/channels-fork.c +++ b/test/channels-fork.c @@ -187,10 +187,14 @@ static void alarm_handler(int sig) { } int main(void) { + init_sync_flag(&bar_responded); + init_sync_flag(&foo_connected); + init_sync_flag(&foo_gone); + int fda[2], fdb[2]; - assert(pipe2(fda, 0) != -1); - assert(pipe2(fdb, 0) != -1); + assert(pipe(fda) != -1); + assert(pipe(fdb) != -1); if(!fork()) { return main2(fdb[0], fda[1]); diff --git a/test/channels-udp.c b/test/channels-udp.c index 4e628c01..40d5605d 100644 --- a/test/channels-udp.c +++ b/test/channels-udp.c @@ -101,6 +101,9 @@ static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint } int main(void) { + init_sync_flag(&accept_flag); + init_sync_flag(&close_flag); + meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb); // Open four new meshlink instance, the server and three peers. @@ -164,7 +167,7 @@ int main(void) { } const struct timespec req = {0, 2000000}; - clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL); + nanosleep(&req, NULL); } // Let the clients close the channels diff --git a/test/channels.c b/test/channels.c index 3dbbaa7b..fb8cdb0a 100644 --- a/test/channels.c +++ b/test/channels.c @@ -76,6 +76,8 @@ static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t } int main(void) { + init_sync_flag(&b_responded); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); // Open two new meshlink instance. diff --git a/test/duplicate.c b/test/duplicate.c index ed3e7568..df9f86be 100644 --- a/test/duplicate.c +++ b/test/duplicate.c @@ -19,6 +19,8 @@ static void handle_duplicate(meshlink_handle_t *mesh, meshlink_node_t *node) { } int main(void) { + init_sync_flag(&duplicate_detected); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); // Open meshlink instances diff --git a/test/echo-fork.c b/test/echo-fork.c index 0096c9e0..7fe0ab84 100644 --- a/test/echo-fork.c +++ b/test/echo-fork.c @@ -175,6 +175,10 @@ static int main2(void) { int main(void) { + init_sync_flag(&a_started); + init_sync_flag(&a_stopped); + init_sync_flag(&b_responded); + meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb); // Initialize and exchange configuration. diff --git a/test/get-all-nodes.c b/test/get-all-nodes.c index 815dc7df..152184c6 100644 --- a/test/get-all-nodes.c +++ b/test/get-all-nodes.c @@ -24,6 +24,8 @@ static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reach } int main(void) { + init_sync_flag(&bar_reachable); + struct meshlink_node **nodes = NULL; size_t nnodes = 0; diff --git a/test/import-export.c b/test/import-export.c index 599e4c14..15e21344 100644 --- a/test/import-export.c +++ b/test/import-export.c @@ -23,6 +23,8 @@ static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reach } int main(void) { + init_sync_flag(&bar_reachable); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); // Open two new meshlink instance. diff --git a/test/invite-join.c b/test/invite-join.c index 85fb4931..cb1d6aa6 100644 --- a/test/invite-join.c +++ b/test/invite-join.c @@ -23,6 +23,8 @@ static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reach } int main(void) { + init_sync_flag(&baz_reachable); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); assert(meshlink_destroy("invite_join_conf.1")); diff --git a/test/stream.c b/test/stream.c index 378809cc..25aaf2f5 100644 --- a/test/stream.c +++ b/test/stream.c @@ -73,6 +73,7 @@ int main(int argc, char *argv[]) { char *p = buf; memcpy(buf, &now, sizeof(now)); + clock_gettime(CLOCK_REALTIME, &now); for(uint64_t *q = (uint64_t *)(buf + sizeof(now)); (char *)q < buf + framesize; q++) { *q = counter++; @@ -82,23 +83,22 @@ int main(int argc, char *argv[]) { ssize_t sent = write(1, p, tosend); if(sent <= 0) { - err(1, "write(1, %p, %zu)", p, tosend); + err(1, "write(1, %p, %zu)", (void *)p, tosend); } tosend -= sent; p += sent; } - next = now; - next.tv_nsec += interval; + next.tv_sec = 0; + next.tv_nsec = interval; while(next.tv_nsec >= 1000000000) { next.tv_nsec -= 1000000000; next.tv_sec++; } - clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next, NULL); - now = next; + nanosleep(&next, NULL); total -= framesize; } else { struct timespec *ts = (struct timespec *)buf; @@ -109,7 +109,7 @@ int main(int argc, char *argv[]) { ssize_t result = read(0, p, toread); if(result <= 0) { - err(1, "read(1, %p, %zu)", p, toread); + err(1, "read(1, %p, %zu)", (void *)p, toread); } toread -= result; @@ -124,7 +124,7 @@ int main(int argc, char *argv[]) { ssize_t result = read(0, p, toread); if(result <= 0) { - err(1, "read(1, %p, %zu)", p, toread); + err(1, "read(1, %p, %zu)", (void *)p, toread); } toread -= result; @@ -137,7 +137,7 @@ int main(int argc, char *argv[]) { if(*q != counter++) { uint64_t offset = (counter - 1) * 8; offset += ((counter * 8) / (framesize - sizeof(now))) * sizeof(now); - err(1, "verification failed at offset %lu", offset); + err(1, "verification failed at offset %lu", (unsigned long)offset); } } diff --git a/test/trio.c b/test/trio.c index 478dd28e..bdb3904e 100644 --- a/test/trio.c +++ b/test/trio.c @@ -48,6 +48,10 @@ static void baz_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool r } int main(void) { + init_sync_flag(&received); + init_sync_flag(&bar_learned_baz); + init_sync_flag(&baz_learned_bar); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); // Create three instances. diff --git a/test/trio2.c b/test/trio2.c index 328c6a95..dc57679e 100644 --- a/test/trio2.c +++ b/test/trio2.c @@ -48,6 +48,10 @@ static void baz_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool r } int main(void) { + init_sync_flag(&received); + init_sync_flag(&bar_learned_baz); + init_sync_flag(&baz_learned_bar); + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); // Create three instances. diff --git a/test/utils.c b/test/utils.c index d4ba6b6d..c526ff0d 100644 --- a/test/utils.c +++ b/test/utils.c @@ -13,18 +13,23 @@ #include "utils.h" +void init_sync_flag(struct sync_flag *s) { + assert(pthread_mutex_init(&s->mutex, NULL) == 0); + assert(pthread_cond_init(&s->cond, NULL) == 0); +} + void set_sync_flag(struct sync_flag *s, bool value) { - pthread_mutex_lock(&s->mutex); + assert(pthread_mutex_lock(&s->mutex) == 0); s->flag = value; - pthread_cond_broadcast(&s->cond); - pthread_mutex_unlock(&s->mutex); + assert(pthread_cond_broadcast(&s->cond) == 0); + assert(pthread_mutex_unlock(&s->mutex) == 0); } bool check_sync_flag(struct sync_flag *s) { bool flag; - pthread_mutex_lock(&s->mutex); + assert(pthread_mutex_lock(&s->mutex) == 0); flag = s->flag; - pthread_mutex_unlock(&s->mutex); + assert(pthread_mutex_unlock(&s->mutex) == 0); return flag; } @@ -33,7 +38,7 @@ bool wait_sync_flag(struct sync_flag *s, int seconds) { clock_gettime(CLOCK_REALTIME, &timeout); timeout.tv_sec += seconds; - pthread_mutex_lock(&s->mutex); + assert(pthread_mutex_lock(&s->mutex) == 0); while(!s->flag) { if(!pthread_cond_timedwait(&s->cond, &s->mutex, &timeout) || errno != EINTR) { @@ -41,7 +46,7 @@ bool wait_sync_flag(struct sync_flag *s, int seconds) { } } - pthread_mutex_unlock(&s->mutex); + assert(pthread_mutex_unlock(&s->mutex) == 0); return s->flag; } @@ -108,6 +113,7 @@ static void pair_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool void start_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) { struct sync_flag pair_status = {.flag = false}; + init_sync_flag(&pair_status); a->priv = &pair_status; meshlink_set_node_status_cb(a, pair_status_cb); diff --git a/test/utils.h b/test/utils.h index 3576df59..883b9154 100644 --- a/test/utils.h +++ b/test/utils.h @@ -13,6 +13,7 @@ struct sync_flag { bool flag; }; +extern void init_sync_flag(struct sync_flag *s); extern void set_sync_flag(struct sync_flag *s, bool value); extern bool check_sync_flag(struct sync_flag *s); extern bool wait_sync_flag(struct sync_flag *s, int seconds); @@ -49,7 +50,7 @@ extern void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const ch #endif /// Compare two timespec values. -static bool timespec_lt(const struct timespec *a, const struct timespec *b) { +static inline bool timespec_lt(const struct timespec *a, const struct timespec *b) { if(a->tv_sec == b->tv_sec) { return a->tv_nsec < b->tv_nsec; } else { -- 2.39.5