From 6fb788ca2ec44837ea10a36be7fd0030fb08955b Mon Sep 17 00:00:00 2001 From: t3sserakt Date: Thu, 28 Oct 2021 16:13:47 +0200 Subject: - fixed coverity issues --- src/testing/testing.c | 140 ++++++++++++++++----- .../testing_api_cmd_netjail_start_testsystem.c | 109 +++++++++++----- .../testing_api_cmd_netjail_stop_testsystem.c | 24 ++-- 3 files changed, 197 insertions(+), 76 deletions(-) (limited to 'src/testing') diff --git a/src/testing/testing.c b/src/testing/testing.c index 4d9b7c0cb..02cedc744 100644 --- a/src/testing/testing.c +++ b/src/testing/testing.c @@ -1798,7 +1798,7 @@ get_first_value (char *line) memcpy (copy, line, slen); token = strtok_r (copy, ":", &rest); token = strtok_r (NULL, ":", &rest); - sscanf (token, "%u", &ret); + GNUNET_assert (1 == sscanf (token, "%u", &ret)); GNUNET_free (copy); return ret; } @@ -1864,7 +1864,7 @@ get_second_value (char *line) token = strtok_r (copy, ":", &rest); token = strtok_r (NULL, ":", &rest); token = strtok_r (NULL, ":", &rest); - sscanf (token, "%u", &ret); + GNUNET_assert (1 == sscanf (token, "%u", &ret)); GNUNET_free (copy); return ret; } @@ -1923,7 +1923,7 @@ get_connect_value (char *line, struct GNUNET_TESTING_NetjailNode *node) { node_connection->node_type = GNUNET_TESTING_GLOBAL_NODE; token = strtok_r (NULL, ":", &rest); - sscanf (token, "%u", &node_n); + GNUNET_assert (1 == sscanf (token, "%u", &node_n)); LOG (GNUNET_ERROR_TYPE_ERROR, "node_n %u\n", node_n); @@ -1934,15 +1934,17 @@ get_connect_value (char *line, struct GNUNET_TESTING_NetjailNode *node) { node_connection->node_type = GNUNET_TESTING_SUBNET_NODE; token = strtok_r (NULL, ":", &rest); - sscanf (token, "%u", &node_n); - node_connection->node_n = node_n; - token = strtok_r (NULL, ":", &rest); sscanf (token, "%u", &namespace_n); node_connection->namespace_n = namespace_n; + token = strtok_r (NULL, ":", &rest); + sscanf (token, "%u", &node_n); + node_connection->node_n = node_n; LOG (GNUNET_ERROR_TYPE_ERROR, - "node_n %u namespace_n %u\n", + "node_n %u namespace_n %u node->node_n %u node->namespace_n %u\n", node_n, - namespace_n); + namespace_n, + node->node_n, + node->namespace_n); } while (NULL != (token = strtok_r (NULL, ":", &rest))) { @@ -1994,13 +1996,16 @@ node_connections (char *line, struct GNUNET_TESTING_NetjailNode *node) if (NULL != temp) { slen = strlen (temp) + 1; - copy = malloc (slen); + copy = GNUNET_malloc (slen); memcpy (copy, temp, slen); strtok_r (copy, ":", &rest); value = strtok_r (rest, "|", &rest2); while (NULL != value) { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "node_connections value %s\n", + value); node_connection = get_connect_value (value, node); GNUNET_CONTAINER_DLL_insert (node->node_connections_head, node->node_connections_tail, @@ -2011,6 +2016,7 @@ node_connections (char *line, struct GNUNET_TESTING_NetjailNode *node) value = strtok_r (NULL, "|", &rest2); } + GNUNET_free (copy); } } @@ -2056,13 +2062,7 @@ static int log_namespaces (void *cls, const struct GNUNET_ShortHashCode *id, void *value) { struct GNUNET_TESTING_NetjailNamespace *namespace = value; - struct GNUNET_TESTING_NetjailRouter *router = namespace->router; - LOG (GNUNET_ERROR_TYPE_ERROR, - "router_tcp: %u router_udp: %u spaces: %u\n", - router->tcp_port, - router->udp_port, - namespace->namespace_n); GNUNET_CONTAINER_multishortmap_iterate (namespace->nodes, &log_nodes, NULL); return GNUNET_YES; } @@ -2103,9 +2103,15 @@ GNUNET_TESTING_get_connections (unsigned int num, struct struct GNUNET_TESTING_NetjailNamespace *namespace; unsigned int namespace_n, node_m; + LOG (GNUNET_ERROR_TYPE_DEBUG, + "gaga 1\n"); log_topo (topology); - + LOG (GNUNET_ERROR_TYPE_DEBUG, + "gaga 2\n"); hkey = GNUNET_new (struct GNUNET_ShortHashCode); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "num: %u \n", + num); if (topology->nodes_x >= num) { @@ -2118,10 +2124,10 @@ GNUNET_TESTING_get_connections (unsigned int num, struct } else { - namespace_n = (unsigned int) floor ((num - topology->nodes_x) - / topology->nodes_m); + namespace_n = (unsigned int) ceil ((double) (num - topology->nodes_x) + / topology->nodes_m); LOG (GNUNET_ERROR_TYPE_ERROR, - "num: %u nodes_x: %u nodes_m: %u namespace_n: %u\n", + "ceil num: %u nodes_x: %u nodes_m: %u namespace_n: %u\n", num, topology->nodes_x, topology->nodes_m, @@ -2143,7 +2149,7 @@ GNUNET_TESTING_get_connections (unsigned int num, struct hkey); } - + GNUNET_free (hkey); return node->node_connections_head; } @@ -2172,10 +2178,74 @@ GNUNET_TESTING_get_pub_key (unsigned int num, struct GNUNET_CRYPTO_eddsa_key_get_public (priv_key, pub_key); peer->public_key = *pub_key; + GNUNET_free (priv_key); + GNUNET_free (pub_key); return peer; } +int +free_nodes_cb (void *cls, + const struct GNUNET_ShortHashCode *key, + void *value) +{ + (void) cls; + struct GNUNET_TESTING_NetjailNode *node = value; + struct GNUNET_TESTING_NodeConnection *pos_connection; + struct GNUNET_TESTING_NodeConnection *tmp_connection; + struct GNUNET_TESTING_AddressPrefix *pos_prefix; + struct GNUNET_TESTING_AddressPrefix *tmp_prefix; + + pos_connection = node->node_connections_head; + + while (NULL != pos_connection->next) + { + pos_prefix = pos_connection->address_prefixes_head; + while (NULL != pos_prefix->next) + { + tmp_prefix = pos_prefix->next; + GNUNET_free (pos_prefix); + pos_prefix = tmp_prefix; + } + tmp_connection = pos_connection->next; + GNUNET_free (pos_connection); + pos_connection = tmp_connection; + } + return GNUNET_OK; +} + + +int +free_namespaces_cb (void *cls, + const struct GNUNET_ShortHashCode *key, + void *value) +{ + (void) cls; + struct GNUNET_TESTING_NetjailNamespace *namespace = value; + + GNUNET_free (namespace->router); + GNUNET_CONTAINER_multishortmap_iterate (namespace->nodes, free_nodes_cb, + NULL); + return GNUNET_OK; + +} + + +/** + * Deallocate memory of the struct GNUNET_TESTING_NetjailTopology. + * + * @param topology The GNUNET_TESTING_NetjailTopology to be deallocated. + */ +void +GNUNET_TESTING_free_topology (struct GNUNET_TESTING_NetjailTopology *topology) +{ + GNUNET_CONTAINER_multishortmap_iterate (topology->map_namespaces, + free_namespaces_cb, NULL); + GNUNET_CONTAINER_multishortmap_iterate (topology->map_globals, free_nodes_cb, + NULL); + GNUNET_free (topology); +} + /** * Calculate the unique id identifying a node from a given connction. * @@ -2252,7 +2322,7 @@ GNUNET_TESTING_get_address (struct GNUNET_TESTING_NodeConnection *connection, } else { - GNUNET_break (0); + GNUNET_assert (0); } return addr; @@ -2296,24 +2366,19 @@ GNUNET_TESTING_get_topo_from_file (const char *filename) uint64_t fs; char *data; char *token; - char *key; + char *key = NULL; unsigned int out; char *rest = NULL; char *value; + char *value2; int ret; - struct GNUNET_TESTING_NetjailTopology *topo = GNUNET_new (struct - GNUNET_TESTING_NetjailTopology); + struct GNUNET_TESTING_NetjailTopology *topo; struct GNUNET_TESTING_NetjailNode *node; struct GNUNET_TESTING_NetjailRouter *router; struct GNUNET_TESTING_NetjailNamespace *namespace; struct GNUNET_ShortHashCode *hkey; struct GNUNET_HashCode hc; - topo->map_namespaces = - GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO); - topo->map_globals = - GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO); - if (GNUNET_YES != GNUNET_DISK_file_test (filename)) { LOG (GNUNET_ERROR_TYPE_ERROR, @@ -2339,14 +2404,22 @@ GNUNET_TESTING_get_topo_from_file (const char *filename) return NULL; } - LOG (GNUNET_ERROR_TYPE_ERROR, + LOG (GNUNET_ERROR_TYPE_DEBUG, "data: %s\n", data); + data[fs] = '\0'; token = strtok_r (data, "\n", &rest); + topo = GNUNET_new (struct GNUNET_TESTING_NetjailTopology); + topo->map_namespaces = + GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO); + topo->map_globals = + GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO); while (NULL != token) { + if (NULL != key) + free (key); key = get_key (token); LOG (GNUNET_ERROR_TYPE_ERROR, "In the loop with token: %s beginning with %s\n", @@ -2457,12 +2530,12 @@ GNUNET_TESTING_get_topo_from_file (const char *filename) LOG (GNUNET_ERROR_TYPE_ERROR, "Get value for key udp_port on R.\n"); - value = get_value ("udp_port", token); - ret = sscanf (value, "%u", &(router->udp_port)); + value2 = get_value ("udp_port", token); + ret = sscanf (value2, "%u", &(router->udp_port)); GNUNET_break (0 != ret && 1 >= router->udp_port); LOG (GNUNET_ERROR_TYPE_ERROR, "udp_port: %s\n", - value); + value2); if (GNUNET_YES == GNUNET_CONTAINER_multishortmap_contains ( topo->map_namespaces, @@ -2554,6 +2627,7 @@ GNUNET_TESTING_get_topo_from_file (const char *filename) } token = strtok_r (NULL, "\n", &rest); } + GNUNET_free (data); return topo; } diff --git a/src/testing/testing_api_cmd_netjail_start_testsystem.c b/src/testing/testing_api_cmd_netjail_start_testsystem.c index 9b567a01f..c3598d174 100644 --- a/src/testing/testing_api_cmd_netjail_start_testsystem.c +++ b/src/testing/testing_api_cmd_netjail_start_testsystem.c @@ -66,6 +66,12 @@ struct HelperMessage */ struct NetJailState { + /** + * Global state of the interpreter, used by a command + * to access information about other commands. + */ + struct GNUNET_TESTING_Interpreter *is; + /** * Context for our asynchronous completion. */ @@ -76,12 +82,6 @@ struct NetJailState */ struct GNUNET_TESTING_NetjailTopology *topology; - /** - * Pointer to the return value of the test. - * - */ - unsigned int *rv; - /** * Head of the DLL which stores messages received by the helper. * @@ -254,6 +254,7 @@ netjail_exec_cleanup (void *cls) tbc_pos); GNUNET_free (tbc_pos); } + GNUNET_TESTING_free_topology (ns->topology); GNUNET_free (ns); } @@ -429,18 +430,63 @@ helper_mst (void *cls, const struct GNUNET_MessageHeader *message) struct NetJailState *ns = cls;// tbc->ns; struct HelperMessage *hp_msg; unsigned int total_number = ns->local_m * ns->global_n + ns->known; + // uint16_t message_type = ntohs (message->type); - LOG (GNUNET_ERROR_TYPE_DEBUG, - "total %u sysstarted %u peersstarted %u prep %u finished %u %u %u %u\n", - total_number, - ns->number_of_testsystems_started, - ns->number_of_peers_started, - ns->number_of_local_tests_prepared, - ns->number_of_local_tests_finished, - ns->local_m, - ns->global_n, - ns->known); + /*switch (message_type) + { + case GNUNET_MESSAGE_TYPE_CMDS_HELPER_REPLY: + ns->number_of_testsystems_started++; + break; + case GNUNET_MESSAGE_TYPE_CMDS_HELPER_PEER_STARTED: + ns->number_of_peers_started++; + if (ns->number_of_peers_started == total_number) + { + for (int i = 1; i <= ns->known; i++) + { + send_all_peers_started (0,i, ns); + } + for (int i = 1; i <= ns->global_n; i++) + { + for (int j = 1; j <= ns->local_m; j++) + { + send_all_peers_started (i,j, ns); + } + } + ns->number_of_peers_started = 0; + } + break; + case GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_TEST_PREPARED: + ns->number_of_local_tests_prepared++; + if (ns->number_of_local_tests_prepared == total_number) + { + for (int i = 1; i <= ns->known; i++) + { + send_all_local_tests_prepared (0,i, ns); + } + for (int i = 1; i <= ns->global_n; i++) + { + for (int j = 1; j <= ns->local_m; j++) + { + send_all_local_tests_prepared (i,j, ns); + } + } + } + break; + case GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_FINISHED: + ns->number_of_local_tests_finished++; + if (ns->number_of_local_tests_finished == total_number) + { + GNUNET_TESTING_async_finish (&ns->ac); + } + break; + default: + hp_msg = GNUNET_new (struct HelperMessage); + hp_msg->bytes_msg = message->size; + memcpy (&hp_msg[1], message, message->size); + GNUNET_CONTAINER_DLL_insert (ns->hp_messages_head, ns->hp_messages_tail, + hp_msg); + }*/ if (GNUNET_MESSAGE_TYPE_CMDS_HELPER_REPLY == ntohs (message->type)) { ns->number_of_testsystems_started++; @@ -505,7 +551,16 @@ helper_mst (void *cls, const struct GNUNET_MessageHeader *message) } - + LOG (GNUNET_ERROR_TYPE_DEBUG, + "total %u sysstarted %u peersstarted %u prep %u finished %u %u %u %u\n", + total_number, + ns->number_of_testsystems_started, + ns->number_of_peers_started, + ns->number_of_local_tests_prepared, + ns->number_of_local_tests_finished, + ns->local_m, + ns->global_n, + ns->known); @@ -524,7 +579,7 @@ exp_cb (void *cls) struct TestingSystemCount *tbc = cls; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called exp_cb.\n"); - GNUNET_TESTING_interpreter_fail (tbc->ns->ac.is); + GNUNET_TESTING_async_fail (&(tbc->ns->ac)); } @@ -560,8 +615,7 @@ create_helper_init_msg_ (const char *plugin_name) * */ static void -start_helper (struct NetJailState *ns, struct - GNUNET_CONFIGURATION_Handle *config, +start_helper (struct NetJailState *ns, unsigned int m, unsigned int n) { @@ -582,7 +636,6 @@ start_helper (struct NetJailState *ns, struct struct GNUNET_TESTING_NetjailTopology *topology = ns->topology; struct GNUNET_TESTING_NetjailNode *node; struct GNUNET_TESTING_NetjailNamespace *namespace; - unsigned int *rv = ns->rv; if (0 == n) @@ -633,14 +686,14 @@ start_helper (struct NetJailState *ns, struct GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No SUID for %s!\n", NETJAIL_EXEC_SCRIPT); - *rv = 1; + GNUNET_TESTING_interpreter_fail (ns->is); } else if (GNUNET_NO == helper_check) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s not found!\n", NETJAIL_EXEC_SCRIPT); - *rv = 1; + GNUNET_TESTING_interpreter_fail (ns->is); } LOG (GNUNET_ERROR_TYPE_DEBUG, @@ -729,8 +782,9 @@ start_helper (struct NetJailState *ns, struct GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Send handle is NULL!\n"); GNUNET_free (msg); - *rv = 1; + GNUNET_TESTING_interpreter_fail (ns->is); } + GNUNET_free (hkey); } @@ -746,12 +800,11 @@ netjail_exec_run (void *cls, struct GNUNET_TESTING_Interpreter *is) { struct NetJailState *ns = cls; - struct GNUNET_CONFIGURATION_Handle *config = - GNUNET_CONFIGURATION_create (); + ns->is = is; for (int i = 1; i <= ns->known; i++) { - start_helper (ns, config, + start_helper (ns, i, 0); } @@ -760,7 +813,7 @@ netjail_exec_run (void *cls, { for (int j = 1; j <= ns->local_m; j++) { - start_helper (ns, config, + start_helper (ns, j, i); } diff --git a/src/testing/testing_api_cmd_netjail_stop_testsystem.c b/src/testing/testing_api_cmd_netjail_stop_testsystem.c index 33792f1b0..e37e955be 100644 --- a/src/testing/testing_api_cmd_netjail_stop_testsystem.c +++ b/src/testing/testing_api_cmd_netjail_stop_testsystem.c @@ -35,6 +35,11 @@ struct StopHelperState { + /** + * The complete topology information. + */ + struct GNUNET_TESTING_NetjailTopology *topology; + const char *helper_start_label; /** @@ -63,21 +68,10 @@ struct StopHelperState static void stop_testing_system_cleanup (void *cls) { + struct StopHelperState *shs = cls; -} - - -/** - * Trait function of this cmd does nothing. - * - */ -static int -stop_testing_system_traits (void *cls, - const void **ret, - const char *trait, - unsigned int index) -{ - return GNUNET_OK; + GNUNET_TESTING_free_topology (shs->topology); + GNUNET_free (shs); } @@ -148,13 +142,13 @@ GNUNET_TESTING_cmd_stop_testing_system (const char *label, shs->local_m = topology->nodes_m; shs->global_n = topology->namespaces_n; shs->known = topology->nodes_x; + shs->topology = topology; struct GNUNET_TESTING_Command cmd = { .cls = shs, .label = label, .run = &stop_testing_system_run, .cleanup = &stop_testing_system_cleanup, - .traits = &stop_testing_system_traits }; return cmd; -- cgit v1.2.3