From e276388f7bb4792fd7277868646a409690da5576 Mon Sep 17 00:00:00 2001 From: Mineplay5780 Date: Wed, 10 Dec 2025 20:39:31 +0100 Subject: [PATCH] feat(file): implemented write file tests --- src/file.c | 10 +++- tests/main.c | 3 +- tests/unit/file.c | 117 ++++++++++++++++++++++++++++++++++++++++++++-- tests/unit/file.h | 7 +-- 4 files changed, 126 insertions(+), 11 deletions(-) diff --git a/src/file.c b/src/file.c index cf6c189..b0cc00f 100644 --- a/src/file.c +++ b/src/file.c @@ -46,7 +46,10 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l return COSMS_FILE_COULD_NOT_READ_SIZE; } - *size = file_stat.st_size; + if (size != NULL) { + *size = file_stat.st_size; + } + *content = (char*)malloc((file_stat.st_size + 1) * sizeof(char)); if (content == NULL) { close(file); @@ -104,7 +107,10 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size; - *size = file_size; + if (size != NULL) { + *size = file_size; + } + *content = (char*)malloc((file_size + 1) * sizeof(char)); if (content == NULL) { CloseHandle(file); diff --git a/tests/main.c b/tests/main.c index b348f4f..a5695b7 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,6 +1,7 @@ #include "unit/unit.h" int main(void) { - cosms_file_read_test(); + cosms_core_file_read_test(); + cosms_core_file_write_test(); return 0; } diff --git a/tests/unit/file.c b/tests/unit/file.c index 1052bb1..89e1de8 100644 --- a/tests/unit/file.c +++ b/tests/unit/file.c @@ -1,12 +1,12 @@ -#include "file.h" -#include "../test.h" -//#include -#include "../../include/cosms-core/file.h" +#include "test.h" +#include "unit/file.h" + +#include #include #include -void cosms_file_read_test() { +void cosms_core_file_read_test() { COSMS_TEST_HEADER("file read"); const char *current_test_name = "reading small file"; @@ -72,4 +72,111 @@ void cosms_file_read_test() { } free(file_buffer); +} + +void cosms_core_file_write_test() { + COSMS_TEST_HEADER("file write"); + + const char *current_test_name = "writting small file"; + COSMS_TEST_START(current_test_name); + + char *file_buffer = "Hello, World!"; + CosmsFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer) - 1, true); + if (error != COSMS_FILE_OK) { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } else { + char *file_content; + error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); + if (error != COSMS_FILE_OK) { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } else { + if (strcmp(file_buffer, file_content)) { + COSMS_TEST_SUCCESS(current_test_name); + } else { + COSMS_TEST_FAIL(current_test_name, "file content not what was written"); + } + } + + free(file_content); + } + + current_test_name = "writting big file"; + COSMS_TEST_START(current_test_name); + + unsigned long long large_file_size = 5242880000ULL; + char *large_file_buffer = (char*)malloc((large_file_size + 1) * sizeof(char)); + if (!large_file_buffer) { + printf("[-] failed to allocate memory for test aborting file read tests...\n"); + free(large_file_buffer); + return; + } + + memset(large_file_buffer, 'a', large_file_size); + large_file_buffer[large_file_size] = '\0'; + + error = cosms_core_file_write("tests/data/large-write-file.txt", large_file_buffer, large_file_size, true); + if (error != COSMS_FILE_OK) { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } else { + char *file_content; + error = cosms_core_file_read("tests/data/large-write-file.txt", &file_content, NULL); + if (error != COSMS_FILE_OK) { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } else { + if (strcmp(file_buffer, file_content)) { + COSMS_TEST_SUCCESS(current_test_name); + } else { + COSMS_TEST_FAIL(current_test_name, "file content not what was written"); + } + } + + free(file_content); + } + + free(large_file_buffer); + + current_test_name = "appending file"; + COSMS_TEST_START(current_test_name); + + error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer) - 1, false); + if (error != COSMS_FILE_OK) { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } else { + char *file_content; + error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); + if (error != COSMS_FILE_OK) { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } else { + const char *expected_result = "Hello, World!Hello, World!"; + if (strcmp(expected_result, file_content)) { + COSMS_TEST_SUCCESS(current_test_name); + } else { + COSMS_TEST_FAIL(current_test_name, "file content not what was written"); + } + } + + free(file_content); + } + + current_test_name = "appending to non existing file"; + COSMS_TEST_START(current_test_name); + + error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer) - 1, false); + if (error != COSMS_FILE_OK) { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } else { + char *file_content; + error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); + if (error != COSMS_FILE_OK) { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } else { + if (strcmp(file_buffer, file_content)) { + COSMS_TEST_SUCCESS(current_test_name); + } else { + COSMS_TEST_FAIL(current_test_name, "file content not what was written"); + } + } + + free(file_content); + } } \ No newline at end of file diff --git a/tests/unit/file.h b/tests/unit/file.h index da53018..c789f04 100644 --- a/tests/unit/file.h +++ b/tests/unit/file.h @@ -1,8 +1,9 @@ -#ifndef COSMS_FILE_TEST -#define COSMS_FILE_TEST +#ifndef COSMS_CORE_FILE_TEST +#define COSMS_CORE_FILE_TEST #include -void cosms_file_read_test(); +void cosms_core_file_read_test(); +void cosms_core_file_write_test(); #endif \ No newline at end of file