diff --git a/src/shared/file.c b/src/shared/file.c index 8134a6c..7f41fe2 100644 --- a/src/shared/file.c +++ b/src/shared/file.c @@ -296,17 +296,21 @@ static bool receive_and_assign_metadata(int fd, const Config* config, File* file static File* receive_delta_file(int fd, const Config* config, const char* check_path, void* old_data, unsigned long long old_size) { - if (!old_data) + if (!old_data) { + send_status(fd, STATUS_ERROR); return NULL; + } DeltaSignature* sig = delta_signature_create(old_data, old_size, config->delta_block_size); if (!sig) { + send_status(fd, STATUS_ERROR); free(old_data); return NULL; } Data* sig_data = delta_signature_serialize(sig); if (!sig_data) { + send_status(fd, STATUS_ERROR); delta_signature_destroy(sig); free(old_data); return NULL; @@ -518,7 +522,6 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) { if (!receive_and_assign_metadata(fd, config, file)) return NULL; - // Read file type indicator int file_type; if (!receive_int(fd, &file_type)) { file_destroy(file); @@ -605,7 +608,6 @@ bool file_send_sendfile(File* file, int file_descriptor, bool use_metadata, int if (use_metadata && !metadata_send(file_descriptor, file->metadata)) return false; - // Send file type indicator int ft = (int)file->type; if (!send_int(file_descriptor, ft)) return false; diff --git a/tests/test_protocol.c b/tests/test_protocol.c index bece803..f60d45f 100644 --- a/tests/test_protocol.c +++ b/tests/test_protocol.c @@ -46,6 +46,7 @@ static void test_send_receive_str() { EXPECT_TRUE(send_str(0, "")); + /* cppcheck-suppress constVariablePointer */ char* received = receive_str(0); EXPECT_NOT_NULL(received); EXPECT_EQ_STR(received, ""); @@ -63,6 +64,7 @@ static void test_send_receive_str_normal() { EXPECT_TRUE(send_str(0, "Hello, Protocol!")); + /* cppcheck-suppress constVariablePointer */ char* received = receive_str(0); EXPECT_NOT_NULL(received); EXPECT_EQ_STR(received, "Hello, Protocol!"); @@ -78,6 +80,7 @@ static void test_send_receive_data() { io_set_fds(p[0], p[1]); io_set_bwlimit(0); + /* cppcheck-suppress constVariablePointer */ unsigned char bin[] = {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0xFF}; void* buf = malloc(sizeof(bin)); EXPECT_NOT_NULL(buf); @@ -128,6 +131,7 @@ static void test_send_receive_status() { io_set_fds(p[0], p[1]); io_set_bwlimit(0); + /* cppcheck-suppress constVariablePointer */ Status statuses[] = {STATUS_OK, STATUS_ERROR, STATUS_FINISHED, STATUS_NEXT, STATUS_CHUNK, STATUS_CHECK, STATUS_DELTA_SIGNATURE, STATUS_DELTA_DATA}; int count = sizeof(statuses) / sizeof(statuses[0]); diff --git a/tests/test_scanner.c b/tests/test_scanner.c index 4c6048c..e9bd87f 100644 --- a/tests/test_scanner.c +++ b/tests/test_scanner.c @@ -15,7 +15,7 @@ static void test_scanner_single_file() { const char* file1 = "test_scan_dir_single/file1.txt"; const char* content1 = "hello scanner"; - mkdir(dir, 0755); + EXPECT_EQ_INT(mkdir(dir, 0755), 0); create_test_file(file1, content1); DirectoryScanner* scanner = @@ -43,7 +43,7 @@ static void test_scanner_multiple_files() { const char* content1 = "alpha"; const char* content2 = "beta"; - mkdir(dir, 0755); + EXPECT_EQ_INT(mkdir(dir, 0755), 0); create_test_file(file1, content1); create_test_file(file2, content2); @@ -82,8 +82,8 @@ static void test_scanner_subdirectory() { const char* sub_file = "test_scan_sub/sub/sub_file.txt"; const char* content = "nested content"; - mkdir(root, 0755); - mkdir(sub, 0755); + EXPECT_EQ_INT(mkdir(root, 0755), 0); + EXPECT_EQ_INT(mkdir(sub, 0755), 0); create_test_file(root_file, content); create_test_file(sub_file, content); @@ -109,7 +109,7 @@ static void test_scanner_subdirectory() { static void test_scanner_empty_directory() { const char* dir = "test_scan_empty"; - mkdir(dir, 0755); + EXPECT_EQ_INT(mkdir(dir, 0755), 0); DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0); @@ -130,7 +130,7 @@ static void test_scanner_exclude_pattern() { const char* f_tmp = "test_scan_excl/remove.tmp"; const char* content = "data"; - mkdir(dir, 0755); + EXPECT_EQ_INT(mkdir(dir, 0755), 0); create_test_file(f_txt, content); create_test_file(f_tmp, content); @@ -154,8 +154,6 @@ static void test_scanner_exclude_pattern() { } static void test_scanner_exclude_subdirectory() { - /* Exclude patterns match filenames only (via entry->d_name). - * Files inside subdirectories are also matched by filename. */ const char* root = "test_scan_excl_sub"; const char* sub = "test_scan_excl_sub/sub"; const char* root_txt = "test_scan_excl_sub/root.txt"; @@ -163,13 +161,12 @@ static void test_scanner_exclude_subdirectory() { const char* sub_tmp = "test_scan_excl_sub/sub/temp.tmp"; const char* content = "data"; - mkdir(root, 0755); - mkdir(sub, 0755); + EXPECT_EQ_INT(mkdir(root, 0755), 0); + EXPECT_EQ_INT(mkdir(sub, 0755), 0); create_test_file(root_txt, content); create_test_file(sub_txt, content); create_test_file(sub_tmp, content); - /* Exclude *.tmp — should exclude sub/temp.tmp but keep root.txt and sub/data.txt */ char* exclude[] = {"*.tmp"}; DirectoryScanner* scanner = directory_scanner_create((char*)root, false, 0, exclude, 1, NULL, 0, 0, 0); @@ -180,7 +177,6 @@ static void test_scanner_exclude_subdirectory() { while ((chunk = directory_scanner_next(scanner)) != NULL) { total += chunk->element_count; for (int i = 0; i < chunk->element_count; i++) { - /* No path should end in .tmp */ size_t len = strlen(chunk->items[i]->path); EXPECT_TRUE(len < 4 || strcmp(chunk->items[i]->path + len - 4, ".tmp") != 0); } @@ -197,22 +193,17 @@ static void test_scanner_exclude_subdirectory() { } static void test_scanner_include_and_exclude() { - /* In the scanner, exclude is checked first and takes precedence. - * Include patterns act as an additional filter: if include_count > 0, - * the file must match one of the include patterns (after not being excluded). - * This test uses non-overlapping exclude and include patterns. */ const char* dir = "test_scan_inc_exc"; const char* f_txt = "test_scan_inc_exc/a.txt"; const char* f_log = "test_scan_inc_exc/b.log"; const char* f_bak = "test_scan_inc_exc/c.bak"; const char* content = "filter"; - mkdir(dir, 0755); + EXPECT_EQ_INT(mkdir(dir, 0755), 0); create_test_file(f_txt, content); create_test_file(f_log, content); create_test_file(f_bak, content); - /* Exclude *.bak. Include *.txt and *.log. */ char* exclude[] = {"*.bak"}; char* include[] = {"*.txt", "*.log"}; DirectoryScanner* scanner = @@ -230,7 +221,6 @@ static void test_scanner_include_and_exclude() { if (strstr(chunk->items[i]->path, "b.log")) found_log = 1; } - /* a.txt included by *.txt, b.log included by *.log, c.bak excluded by *.bak */ EXPECT_TRUE(found_txt); EXPECT_TRUE(found_log); @@ -248,8 +238,7 @@ static void test_scanner_max_size() { const char* dir = "test_scan_max"; const char* small = "test_scan_max/small.txt"; const char* large = "test_scan_max/large.txt"; - - mkdir(dir, 0755); + EXPECT_EQ_INT(mkdir(dir, 0755), 0); create_test_file(small, "tiny"); create_test_file(large, "this_content_is_longer_than_ten_chars"); @@ -277,7 +266,7 @@ static void test_scanner_min_size() { const char* empty_f = "test_scan_min/empty.txt"; const char* data_f = "test_scan_min/data.txt"; - mkdir(dir, 0755); + EXPECT_EQ_INT(mkdir(dir, 0755), 0); create_test_file(empty_f, ""); create_test_file(data_f, "some content here"); @@ -306,7 +295,7 @@ static void test_scanner_size_range() { const char* medium = "test_scan_range/med.txt"; const char* huge = "test_scan_range/huge.txt"; - mkdir(dir, 0755); + EXPECT_EQ_INT(mkdir(dir, 0755), 0); create_test_file(tiny, "ab"); create_test_file(medium, "hello world"); create_test_file(huge, "this is a much larger file for testing size filters"); @@ -339,7 +328,7 @@ static void test_scanner_mixed_patterns() { const char* c_txt = "test_scan_mixed/c.txt"; /* size ~= 5 */ const char* d_bak = "test_scan_mixed/d.bak"; /* size ~= 42 */ - mkdir(dir, 0755); + EXPECT_EQ_INT(mkdir(dir, 0755), 0); create_test_file(a_txt, "aaaaa"); create_test_file(b_bin, "bbbbbbbbbbbbb"); create_test_file(c_txt, "ccccc"); @@ -375,7 +364,7 @@ static void test_scanner_no_patterns() { const char* f1 = "test_scan_none/f1.txt"; const char* f2 = "test_scan_none/f2.txt"; - mkdir(dir, 0755); + EXPECT_EQ_INT(mkdir(dir, 0755), 0); create_test_file(f1, "first"); create_test_file(f2, "second");