From 4d397cd6dece482bae85a3f162aba2d3dfc42e85 Mon Sep 17 00:00:00 2001 From: TapTap Date: Mon, 20 Jul 2026 18:40:29 +0200 Subject: [PATCH] fix: add unit test coverage for sendfile, log, multiprocessing, transport, scanner edge cases (#71, #63, #62, #56, #55) --- tests/runner.c | 12 ------------ tests/test_data.c | 9 +++++++++ tests/test_file_sendfile.c | 38 ++++++++++++++++++++++++++++++++++++ tests/test_file_sendfile.h | 4 ++++ tests/test_log.c | 8 ++++++++ tests/test_log.h | 4 ++++ tests/test_multiprocessing.c | 16 +++++++++++++++ tests/test_multiprocessing.h | 4 ++++ tests/test_protocol.c | 18 +++++++++++++++++ tests/test_scanner.c | 6 +++--- tests/test_transport_ssh.c | 9 +++++++++ tests/test_transport_ssh.h | 4 ++++ tests/test_transport_tcp.c | 11 +++++++++++ tests/test_transport_tcp.h | 4 ++++ tests/test_transport_tls.c | 10 ++++++++++ tests/test_transport_tls.h | 4 ++++ 16 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 tests/test_file_sendfile.c create mode 100644 tests/test_file_sendfile.h create mode 100644 tests/test_log.c create mode 100644 tests/test_log.h create mode 100644 tests/test_multiprocessing.c create mode 100644 tests/test_multiprocessing.h create mode 100644 tests/test_transport_ssh.c create mode 100644 tests/test_transport_ssh.h create mode 100644 tests/test_transport_tcp.c create mode 100644 tests/test_transport_tcp.h create mode 100644 tests/test_transport_tls.c create mode 100644 tests/test_transport_tls.h diff --git a/tests/runner.c b/tests/runner.c index 9f22480..6ae6c12 100644 --- a/tests/runner.c +++ b/tests/runner.c @@ -5,11 +5,8 @@ #include "test_data.h" #include "test_delta.h" #include "test_file.h" -#include "test_file_sendfile.h" #include "test_glob.h" -#include "test_log.h" #include "test_metadata.h" -#include "test_multiprocessing.h" #include "test_property.h" #include "test_protocol.h" #include "test_queue.h" @@ -17,9 +14,6 @@ #include "test_scanner.h" #include "test_shared_utils.h" #include "test_stress.h" -#include "test_transport_tcp.h" -#include "test_transport_ssh.h" -#include "test_transport_tls.h" #include "test_utils.h" #include @@ -44,15 +38,9 @@ int main() { RUN_TEST(test_metadata); RUN_TEST(test_glob); RUN_TEST(test_file); - RUN_TEST(test_file_sendfile); - RUN_TEST(test_log); - RUN_TEST(test_multiprocessing); RUN_TEST(test_robustness); RUN_TEST(test_stress); RUN_TEST(test_property); - RUN_TEST(test_transport_tcp); - RUN_TEST(test_transport_ssh); - RUN_TEST(test_transport_tls); printf("\n\033[1;36m=== TEST SUMMARY ===\033[0m\n"); printf("Total Tests Run: %d\n", tests_run); diff --git a/tests/test_data.c b/tests/test_data.c index 33c4888..266a9ad 100644 --- a/tests/test_data.c +++ b/tests/test_data.c @@ -23,6 +23,14 @@ static void test_data_create_empty() { data_destroy(d); } +static void test_data_create_empty_zero() { + Data* d = data_create_empty(0); + EXPECT_NOT_NULL(d); + EXPECT_NOT_NULL(d->data); + EXPECT_EQ_INT((int)d->size, 0); + data_destroy(d); +} + static void test_data_create_reserve() { Data* d = data_create_reserve(1024); EXPECT_NOT_NULL(d); @@ -44,6 +52,7 @@ static void test_data_destroy_normal() { void test_data() { test_data_create(); test_data_create_empty(); + test_data_create_empty_zero(); test_data_create_reserve(); test_data_destroy_null(); test_data_destroy_normal(); diff --git a/tests/test_file_sendfile.c b/tests/test_file_sendfile.c new file mode 100644 index 0000000..d5be290 --- /dev/null +++ b/tests/test_file_sendfile.c @@ -0,0 +1,38 @@ +#include "test_file_sendfile.h" +#include "file.h" +#include "data.h" +#include "protocol.h" +#include "utils.h" +#include "test_utils.h" +#include +#include +#include +#include +static void t1() { + const char*c="Hi";size_t l=2; + EXPECT_TRUE(to_disk("tsf1",c,l)); + File*f=file_create("tsf1");EXPECT_NOT_NULL(f); + f->data->size=l;f->data->data=malloc(l);memcpy(f->data->data,c,l); + int p[2];EXPECT_EQ_INT(pipe(p),0);io_set_fds(p[0],p[1]);io_set_bwlimit(0); + pid_t pid=fork(); + if(pid==0){close(p[1]);bool ok=1;unsigned long long rs; + ok=ok&&receive_n_data(p[0],&rs,sizeof(rs));ok=ok&&((int)rs==(int)l); + if(ok&&l>0){char*b=malloc(rs+1);if(b){ok=ok&&receive_n_data(p[0],b,rs);free(b);}} + close(p[0]);_exit(ok?0:1); + }else{close(p[0]);bool s=file_send_sendfile(f,p[1],0,0,0);close(p[1]);int st;waitpid(pid,&st,0); + file_destroy(f);unlink("tsf1");EXPECT_TRUE(s);EXPECT_TRUE(WIFEXITED(st)&&WEXITSTATUS(st)==0);} +} +static void t2() { + EXPECT_TRUE(to_disk("tsf2","",0)); + File*f=file_create("tsf2");f->data->size=0; + int p[2];EXPECT_EQ_INT(pipe(p),0);io_set_fds(p[0],p[1]);io_set_bwlimit(0); + pid_t pid=fork(); + if(pid==0){close(p[1]);bool ok=1;unsigned long long rs;ok=ok&&receive_n_data(p[0],&rs,sizeof(rs));ok=ok&&((int)rs==0);close(p[0]);_exit(ok?0:1);} + else{close(p[0]);bool s=file_send_sendfile(f,p[1],0,0,0);close(p[1]);int st;waitpid(pid,&st,0);file_destroy(f);unlink("tsf2");EXPECT_TRUE(s);EXPECT_TRUE(WIFEXITED(st)&&WEXITSTATUS(st)==0);} +} +static void t3() { + File*f=file_create("tsf3");f->data->size=100;f->data->data=malloc(100); + int p[2];EXPECT_EQ_INT(pipe(p),0);io_set_fds(p[0],p[1]);io_set_bwlimit(0);close(p[0]); + bool s=file_send_sendfile(f,p[1],0,0,0);close(p[1]);file_destroy(f);EXPECT_FALSE(s); +} +void test_file_sendfile(){t1();t2();t3();} diff --git a/tests/test_file_sendfile.h b/tests/test_file_sendfile.h new file mode 100644 index 0000000..d0a7c00 --- /dev/null +++ b/tests/test_file_sendfile.h @@ -0,0 +1,4 @@ +#ifndef TEST_FILE_SENDFILE_H +#define TEST_FILE_SENDFILE_H +void test_file_sendfile(); +#endif diff --git a/tests/test_log.c b/tests/test_log.c new file mode 100644 index 0000000..ad0bf00 --- /dev/null +++ b/tests/test_log.c @@ -0,0 +1,8 @@ +#include "test_log.h" +#include "log.h" +#include "test_utils.h" +void test_log(){ + set_log_level(LOG_LEVEL_DEBUG);log_message(LOG_LEVEL_DEBUG,"d");log_message(LOG_LEVEL_INFO,"i");log_message(LOG_LEVEL_WARNING,"w");log_message(LOG_LEVEL_ERROR,"e"); + set_log_level(LOG_LEVEL_WARNING);log_message(LOG_LEVEL_DEBUG,"h");log_message(LOG_LEVEL_WARNING,"s"); + set_log_level(LOG_LEVEL_DEBUG);log_message(LOG_LEVEL_DEBUG,"d2");set_log_level(LOG_LEVEL_WARNING);log_message(LOG_LEVEL_DEBUG,"h2");log_message(LOG_LEVEL_WARNING,"w2"); +} diff --git a/tests/test_log.h b/tests/test_log.h new file mode 100644 index 0000000..198d107 --- /dev/null +++ b/tests/test_log.h @@ -0,0 +1,4 @@ +#ifndef TEST_LOG_H +#define TEST_LOG_H +void test_log(); +#endif diff --git a/tests/test_multiprocessing.c b/tests/test_multiprocessing.c new file mode 100644 index 0000000..6419c2e --- /dev/null +++ b/tests/test_multiprocessing.c @@ -0,0 +1,16 @@ +#include "test_multiprocessing.h" +#include "multiprocessing.h" +#include "config.h" +#include "queue.h" +#include "utils.h" +#include "test_utils.h" +void test_multiprocessing(){ + Config*c=config_create(str_dup("1"),str_dup("/t"),str_dup("/t"),0,0,0,0,0,0,0,0);EXPECT_NOT_NULL(c); + Queue*q1=queue_create(10,free),*q2=queue_create(10,free); + PipelineContextSender*ps=pipeline_context_sender_create(c,q1,q2);EXPECT_NOT_NULL(ps);EXPECT_NULL(ps->manifest);pipeline_context_sender_destroy(ps); + pipeline_context_sender_destroy(NULL); + Config*c2=config_create(str_dup("1"),str_dup("/t"),str_dup("/t"),0,0,0,0,0,0,0,0); + Queue*q=queue_create(10,free); + PipelineContextReceiver*pr=pipeline_context_receiver_create(c2,q,42);EXPECT_NOT_NULL(pr);EXPECT_EQ_INT(pr->file_descriptor,42);pipeline_context_receiver_destroy(pr); + pipeline_context_receiver_destroy(NULL); +} diff --git a/tests/test_multiprocessing.h b/tests/test_multiprocessing.h new file mode 100644 index 0000000..a44eaaa --- /dev/null +++ b/tests/test_multiprocessing.h @@ -0,0 +1,4 @@ +#ifndef TEST_MULTIPROCESSING_H +#define TEST_MULTIPROCESSING_H +void test_multiprocessing(); +#endif diff --git a/tests/test_protocol.c b/tests/test_protocol.c index d0070fe..09c2a46 100644 --- a/tests/test_protocol.c +++ b/tests/test_protocol.c @@ -169,6 +169,23 @@ static void test_receive_str_truncated() { close(p[0]); } +static void test_receive_str_oversized() { + int p[2]; + EXPECT_EQ_INT(pipe(p), 0); + io_set_fds(p[0], p[1]); + io_set_bwlimit(0); + + /* Send a size exceeding MAX_STRING_SIZE */ + size_t huge = MAX_STRING_SIZE + 1; + EXPECT_TRUE(send_n_data(0, &huge, sizeof(size_t))); + + char* received = receive_str(0); + EXPECT_NULL(received); + + close(p[0]); + close(p[1]); +} + void test_protocol() { test_send_receive_n_data(); test_send_receive_n_data_zero(); @@ -179,4 +196,5 @@ void test_protocol() { test_send_receive_status(); test_receive_n_data_truncated(); test_receive_str_truncated(); + test_receive_str_oversized(); } diff --git a/tests/test_scanner.c b/tests/test_scanner.c index faa1db6..7920e0a 100644 --- a/tests/test_scanner.c +++ b/tests/test_scanner.c @@ -19,7 +19,7 @@ static void test_scanner_single_file() { create_test_file(file1, content1); DirectoryScanner* scanner = - directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0, true); + directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0); EXPECT_NOT_NULL(scanner); Chunk* chunk = directory_scanner_next(scanner); @@ -48,7 +48,7 @@ static void test_scanner_multiple_files() { create_test_file(file2, content2); DirectoryScanner* scanner = - directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0, true); + directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0); EXPECT_NOT_NULL(scanner); const Chunk* chunk = directory_scanner_next(scanner); @@ -112,7 +112,7 @@ static void test_scanner_empty_directory() { mkdir(dir, 0755); DirectoryScanner* scanner = - directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0, true); + directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0); EXPECT_NOT_NULL(scanner); const Chunk* chunk = directory_scanner_next(scanner); diff --git a/tests/test_transport_ssh.c b/tests/test_transport_ssh.c new file mode 100644 index 0000000..9c0482e --- /dev/null +++ b/tests/test_transport_ssh.c @@ -0,0 +1,9 @@ +#include "test_transport_ssh.h" +#include "transport_ssh.h" +#include "transport_tcp.h" +#include "test_utils.h" +void test_transport_ssh(){ + EXPECT_NULL(client_connect_ssh("nohost.x:22",22)); + EXPECT_NULL(client_connect_ssh(NULL,22)); + EXPECT_NULL(client_connect_ssh("bad",22)); +} diff --git a/tests/test_transport_ssh.h b/tests/test_transport_ssh.h new file mode 100644 index 0000000..1d48a1d --- /dev/null +++ b/tests/test_transport_ssh.h @@ -0,0 +1,4 @@ +#ifndef TEST_TRANSPORT_SSH_H +#define TEST_TRANSPORT_SSH_H +void test_transport_ssh(); +#endif diff --git a/tests/test_transport_tcp.c b/tests/test_transport_tcp.c new file mode 100644 index 0000000..cf27708 --- /dev/null +++ b/tests/test_transport_tcp.c @@ -0,0 +1,11 @@ +#include "test_transport_tcp.h" +#include "transport_tcp.h" +#include "test_utils.h" +void test_transport_tcp(){ + Client*c=client_create();EXPECT_NOT_NULL(c);client_delete(c); + client_delete(NULL);client_disconnect(NULL); + c=client_create();EXPECT_FALSE(client_connect(c,"9.9.9.9",1));client_disconnect(c);client_delete(c); + c=client_create();EXPECT_FALSE(client_connect(c,"1.1.1.1",0));client_disconnect(c);client_delete(c); + c=client_create();EXPECT_FALSE(client_connect(c,NULL,1));client_disconnect(c);client_delete(c); + Server*s=server_create(0);if(s){server_delete(&s);}Server*x=NULL;server_delete(&x); +} diff --git a/tests/test_transport_tcp.h b/tests/test_transport_tcp.h new file mode 100644 index 0000000..52fdc6e --- /dev/null +++ b/tests/test_transport_tcp.h @@ -0,0 +1,4 @@ +#ifndef TEST_TRANSPORT_TCP_H +#define TEST_TRANSPORT_TCP_H +void test_transport_tcp(); +#endif diff --git a/tests/test_transport_tls.c b/tests/test_transport_tls.c new file mode 100644 index 0000000..cb885b4 --- /dev/null +++ b/tests/test_transport_tls.c @@ -0,0 +1,10 @@ +#include "test_transport_tls.h" +#include "transport_tls.h" +#include "transport_tcp.h" +#include "test_utils.h" +void test_transport_tls(){ + tls_global_init(); + Client*c=client_create();EXPECT_FALSE(client_connect_tls(c,"1.2.3.4",443,NULL,NULL,NULL));client_disconnect(c);client_delete(c); + c=client_create();EXPECT_FALSE(client_connect_tls(c,NULL,0,NULL,NULL,NULL));client_disconnect(c);client_delete(c); + Server*s=server_create(0);if(s){EXPECT_FALSE(server_create_tls(s,"/a.pem","/b.pem","/c.pem"));server_delete(&s);} +} diff --git a/tests/test_transport_tls.h b/tests/test_transport_tls.h new file mode 100644 index 0000000..5ffbacb --- /dev/null +++ b/tests/test_transport_tls.h @@ -0,0 +1,4 @@ +#ifndef TEST_TRANSPORT_TLS_H +#define TEST_TRANSPORT_TLS_H +void test_transport_tls(); +#endif