2025-12-10 20:39:31 +01:00
|
|
|
#include "test.h"
|
|
|
|
|
#include "unit/file.h"
|
|
|
|
|
|
|
|
|
|
#include <cosms-core/file.h>
|
2025-12-09 22:07:16 +01:00
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2025-12-10 20:39:31 +01:00
|
|
|
void cosms_core_file_read_test() {
|
2025-12-09 22:07:16 +01:00
|
|
|
COSMS_TEST_HEADER("file read");
|
|
|
|
|
|
|
|
|
|
const char *current_test_name = "reading small file";
|
|
|
|
|
COSMS_TEST_START(current_test_name);
|
|
|
|
|
|
|
|
|
|
unsigned long long file_size;
|
|
|
|
|
char *file_buffer;
|
2025-12-10 20:43:16 +01:00
|
|
|
CosmsCoreFileError error = cosms_core_file_read("tests/data/small-file.txt", &file_buffer, &file_size);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
2025-12-09 22:07:16 +01:00
|
|
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
|
|
|
|
} else {
|
|
|
|
|
const char *expected_result = "Hello, World!";
|
|
|
|
|
if (file_size != strlen(expected_result) || strcmp(expected_result, file_buffer)) {
|
|
|
|
|
COSMS_TEST_FAIL(current_test_name, "result does not match expected result");
|
|
|
|
|
} else {
|
|
|
|
|
COSMS_TEST_SUCCESS(current_test_name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(file_buffer);
|
|
|
|
|
|
|
|
|
|
current_test_name = "reading large file";
|
|
|
|
|
COSMS_TEST_START(current_test_name);
|
|
|
|
|
|
|
|
|
|
file_size = 0;
|
|
|
|
|
file_buffer = NULL;
|
|
|
|
|
error = cosms_core_file_read("tests/data/large-file.txt", &file_buffer, &file_size);
|
2025-12-10 20:43:16 +01:00
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
2025-12-09 22:07:16 +01:00
|
|
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
|
|
|
|
} else {
|
|
|
|
|
unsigned long long expected_size = 5242880000ULL;
|
|
|
|
|
char *expected_result = (char*)malloc((expected_size + 1) * sizeof(char));
|
|
|
|
|
if (!expected_result) {
|
|
|
|
|
printf("[-] failed to allocate memory for test aborting file read tests...\n");
|
|
|
|
|
free(file_buffer);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(expected_result, 'a', expected_size);
|
|
|
|
|
expected_result[expected_size] = '\0';
|
|
|
|
|
|
|
|
|
|
if (file_size != expected_size || strcmp(expected_result, file_buffer)) {
|
|
|
|
|
COSMS_TEST_FAIL(current_test_name, "result does not match expected result");
|
|
|
|
|
} else {
|
|
|
|
|
COSMS_TEST_SUCCESS(current_test_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(expected_result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(file_buffer);
|
|
|
|
|
|
|
|
|
|
current_test_name = "reading non existing file";
|
|
|
|
|
COSMS_TEST_START(current_test_name);
|
|
|
|
|
|
|
|
|
|
file_size = 0;
|
|
|
|
|
file_buffer = NULL;
|
|
|
|
|
error = cosms_core_file_read("non-existing-file.cosms", &file_buffer, &file_size);
|
2025-12-10 20:43:16 +01:00
|
|
|
if (error == COSMS_CORE_FILE_NOT_FOUND) {
|
2025-12-09 22:07:16 +01:00
|
|
|
COSMS_TEST_SUCCESS(current_test_name);
|
|
|
|
|
} else {
|
|
|
|
|
COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(file_buffer);
|
2025-12-10 20:39:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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!";
|
2025-12-10 20:43:16 +01:00
|
|
|
CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer) - 1, true);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
2025-12-10 20:39:31 +01:00
|
|
|
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);
|
2025-12-10 20:43:16 +01:00
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
2025-12-10 20:39:31 +01:00
|
|
|
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);
|
2025-12-10 20:43:16 +01:00
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
2025-12-10 20:39:31 +01:00
|
|
|
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);
|
2025-12-10 20:43:16 +01:00
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
2025-12-10 20:39:31 +01:00
|
|
|
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);
|
2025-12-10 20:43:16 +01:00
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
2025-12-10 20:39:31 +01:00
|
|
|
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);
|
2025-12-10 20:43:16 +01:00
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
2025-12-10 20:39:31 +01:00
|
|
|
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);
|
2025-12-10 20:43:16 +01:00
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
2025-12-10 20:39:31 +01:00
|
|
|
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);
|
2025-12-10 20:43:16 +01:00
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
2025-12-10 20:39:31 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2025-12-09 22:07:16 +01:00
|
|
|
}
|