From 865b1756d3de1724f112b90c70a7bdf48d2e97cb Mon Sep 17 00:00:00 2001 From: "Fabio M. Di Nitto" Date: Fri, 11 Aug 2017 07:20:20 +0200 Subject: [PATCH] [testing] add knet_send with all crypto modules test Signed-off-by: Fabio M. Di Nitto --- libknet/compress.c | 8 + libknet/compress.h | 5 + libknet/tests/api-check.mk | 8 + libknet/tests/api_knet_send_compress.c | 255 +++++++++++++++++++++++++ 4 files changed, 276 insertions(+) create mode 100644 libknet/tests/api_knet_send_compress.c diff --git a/libknet/compress.c b/libknet/compress.c index 80a3068..41f6434 100644 --- a/libknet/compress.c +++ b/libknet/compress.c @@ -37,6 +37,14 @@ compress_model_t compress_modules_cmds[] = { { NULL, NULL, NULL, NULL }, }; +/* + * used exclusively by the test suite (see api_knet_send_compress) + */ +const char *get_model_by_idx(int idx) +{ + return compress_modules_cmds[idx].model_name; +} + static int get_model(const char *model) { int idx = 0; diff --git a/libknet/compress.h b/libknet/compress.h index cb83539..4d220ff 100644 --- a/libknet/compress.h +++ b/libknet/compress.h @@ -46,4 +46,9 @@ int decompress( unsigned char *buf_out, ssize_t *buf_out_len); +/* + * used exclusively by the test suite (see api_knet_send_compress) + */ +const char *get_model_by_idx(int idx); + #endif diff --git a/libknet/tests/api-check.mk b/libknet/tests/api-check.mk index f1cc8ff..2a7e2ad 100644 --- a/libknet/tests/api-check.mk +++ b/libknet/tests/api-check.mk @@ -25,6 +25,7 @@ api_checks = \ api_knet_handle_get_transport_reconnect_interval_test \ api_knet_recv_test \ api_knet_send_test \ + api_knet_send_compress_test \ api_knet_send_sync_test \ api_knet_send_loopback_test \ api_knet_handle_pmtud_setfreq_test \ @@ -117,6 +118,13 @@ api_knet_recv_test_SOURCES = api_knet_recv.c \ api_knet_send_test_SOURCES = api_knet_send.c \ test-common.c +api_knet_send_compress_test_SOURCES = api_knet_send_compress.c \ + ../compress.c \ + ../logging.c \ + ../compress_zlib.c \ + ../compress_lz4.c \ + test-common.c + api_knet_send_loopback_test_SOURCES = api_knet_send_loopback.c \ test-common.c diff --git a/libknet/tests/api_knet_send_compress.c b/libknet/tests/api_knet_send_compress.c new file mode 100644 index 0000000..12400dd --- /dev/null +++ b/libknet/tests/api_knet_send_compress.c @@ -0,0 +1,255 @@ +/* + * Copyright (C) 2016 Red Hat, Inc. All rights reserved. + * + * Authors: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-2.0+ + */ + +#include "config.h" + +#include +#include +#include +#include +#include + +#include "libknet.h" + +#include "compress.h" +#include "internals.h" +#include "netutils.h" +#include "test-common.h" + +static int private_data; + +static void sock_notify(void *pvt_data, + int datafd, + int8_t channel, + uint8_t tx_rx, + int error, + int errorno) +{ + return; +} + +static void test(const char *model) +{ + knet_handle_t knet_h; + int logfds[2]; + int datafd = 0; + int8_t channel = 0; + //struct knet_link_status link_status; + char send_buff[KNET_MAX_PACKET_SIZE]; + char recv_buff[KNET_MAX_PACKET_SIZE]; + ssize_t send_len = 0; + int recv_len = 0; + int savederrno; + struct sockaddr_storage lo; + struct knet_handle_compress_cfg knet_handle_compress_cfg; + + memset(&lo, 0, sizeof(struct sockaddr_storage)); + + if (knet_strtoaddr("127.0.0.1", "50000", &lo, sizeof(struct sockaddr_storage)) < 0) { + printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); + exit(FAIL); + } + + memset(send_buff, 0, sizeof(send_buff)); + + setup_logpipes(logfds); + + knet_h = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG); + + if (!knet_h) { + printf("knet_handle_new failed: %s\n", strerror(errno)); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + flush_logs(logfds[0], stdout); + + printf("Test knet_send with %s and valid data\n", model); + + memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); + strncpy(knet_handle_compress_cfg.compress_model, model, sizeof(knet_handle_compress_cfg.compress_model) - 1); + knet_handle_compress_cfg.compress_level = 4; + knet_handle_compress_cfg.compress_threshold = 0; + + if (knet_handle_compress(knet_h, &knet_handle_compress_cfg) < 0) { + printf("knet_handle_compress did not accept zlib compress mode with compress level 1 cfg\n"); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { + printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + datafd = 0; + channel = -1; + + if (knet_handle_add_datafd(knet_h, &datafd, &channel) < 0) { + printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + if (knet_host_add(knet_h, 1) < 0) { + printf("knet_host_add failed: %s\n", strerror(errno)); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + if (knet_link_set_config(knet_h, 1, 0, KNET_TRANSPORT_UDP, &lo, &lo, 0) < 0) { + printf("Unable to configure link: %s\n", strerror(errno)); + knet_host_remove(knet_h, 1); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + if (knet_link_set_enable(knet_h, 1, 0, 1) < 0) { + printf("knet_link_set_enable failed: %s\n", strerror(errno)); + knet_link_clear_config(knet_h, 1, 0); + knet_host_remove(knet_h, 1); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + if (knet_handle_setfwd(knet_h, 1) < 0) { + printf("knet_handle_setfwd failed: %s\n", strerror(errno)); + knet_link_set_enable(knet_h, 1, 0, 0); + knet_link_clear_config(knet_h, 1, 0); + knet_host_remove(knet_h, 1); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + while(knet_h->host_index[1]->status.reachable != 1) { + printf("waiting host to be reachable\n"); + sleep(1); + } + + send_len = knet_send(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel); + if (send_len <= 0) { + printf("knet_send failed: %s\n", strerror(errno)); + knet_link_set_enable(knet_h, 1, 0, 0); + knet_link_clear_config(knet_h, 1, 0); + knet_host_remove(knet_h, 1); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + if (send_len != sizeof(send_buff)) { + printf("knet_send sent only %zd bytes: %s\n", send_len, strerror(errno)); + knet_link_set_enable(knet_h, 1, 0, 0); + knet_link_clear_config(knet_h, 1, 0); + knet_host_remove(knet_h, 1); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + flush_logs(logfds[0], stdout); + + sleep(1); + + recv_len = knet_recv(knet_h, recv_buff, KNET_MAX_PACKET_SIZE, channel); + savederrno = errno; + if (recv_len != send_len) { + printf("knet_recv received only %d bytes: %s (errno: %d)\n", recv_len, strerror(errno), errno); + knet_link_set_enable(knet_h, 1, 0, 0); + knet_link_clear_config(knet_h, 1, 0); + knet_host_remove(knet_h, 1); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + if ((is_helgrind()) && (recv_len == -1) && (savederrno == EAGAIN)) { + printf("helgrind exception. this is normal due to possible timeouts\n"); + exit(PASS); + } + exit(FAIL); + } + + if (memcmp(recv_buff, send_buff, KNET_MAX_PACKET_SIZE)) { + printf("recv and send buffers are different!\n"); + knet_link_set_enable(knet_h, 1, 0, 0); + knet_link_clear_config(knet_h, 1, 0); + knet_host_remove(knet_h, 1); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + +#if 0 + /* A sanity check on the stats */ + if (knet_link_get_status(knet_h, 1, 0, &link_status, sizeof(link_status)) < 0) { + printf("knet_link_get_status failed: %s\n", strerror(errno)); + knet_link_set_enable(knet_h, 1, 0, 0); + knet_link_clear_config(knet_h, 1, 0); + knet_host_remove(knet_h, 1); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); + exit(FAIL); + } + + if (link_status.stats.tx_data_packets != 2 || + link_status.stats.rx_data_packets != 2 || + link_status.stats.tx_data_bytes < KNET_MAX_PACKET_SIZE || + link_status.stats.rx_data_bytes < KNET_MAX_PACKET_SIZE || + link_status.stats.tx_data_bytes > KNET_MAX_PACKET_SIZE*2 || + link_status.stats.rx_data_bytes > KNET_MAX_PACKET_SIZE*2) { + printf("stats look wrong: tx_packets: %lu (%lu bytes), rx_packets: %lu (%lu bytes)\n", + link_status.stats.tx_data_packets, + link_status.stats.tx_data_bytes, + link_status.stats.rx_data_packets, + link_status.stats.rx_data_bytes); + } + + flush_logs(logfds[0], stdout); +#endif + + knet_link_set_enable(knet_h, 1, 0, 0); + knet_link_clear_config(knet_h, 1, 0); + knet_host_remove(knet_h, 1); + knet_handle_free(knet_h); + flush_logs(logfds[0], stdout); + close_logpipes(logfds); +} + +int main(int argc, char *argv[]) +{ + int idx = 0; + const char *model = NULL; + + need_root(); + + while ((model = get_model_by_idx(idx)) != NULL) { + test(model); + idx++; + } + + return PASS; +} -- 2.39.5