#include "unit/file.h" #include #include #include COSMS_CORE_TEST_TEST(file_read, COSMS_CORE_TEST_SUB_TEST(reading small file, unsigned long long file_size = 0; char *file_buffer = NULL; CosmsCoreFileError error = cosms_core_file_read("tests/data/small-file.txt", &file_buffer, &file_size); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { const char *expected_result = "Hello, World!"; if (cosms_core_test_assert(cosms_core_sub_test_name, file_size == strlen(expected_result) && strcmp(expected_result, file_buffer) == 0, "result does not match expected result")) { COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } } if (file_buffer != NULL) { free(file_buffer); } ) COSMS_CORE_TEST_SUB_TEST(reading large file, unsigned long long file_size = 0; char *file_buffer = NULL; CosmsCoreFileError error = cosms_core_file_read("tests/data/large-file.txt", &file_buffer, &file_size); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { 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 (cosms_core_test_assert(cosms_core_sub_test_name, file_size == expected_size && strcmp(expected_result, file_buffer) == 0, "result does not match expected result")) { COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } free(expected_result); } if (file_buffer != NULL) { free(file_buffer); } ) COSMS_CORE_TEST_SUB_TEST(reading non existing file, unsigned long long file_size = 0; char *file_buffer = NULL; CosmsCoreFileError error = cosms_core_file_read("non-existing-file.cosms", &file_buffer, &file_size); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } if (file_buffer != NULL) { free(file_buffer); } ) ) COSMS_CORE_TEST_TEST(file_write, COSMS_CORE_TEST_SUB_TEST(writting small file, char *file_buffer = "Hello, World!"; CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), true); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { char *file_content = NULL; error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { if (cosms_core_test_assert(cosms_core_sub_test_name, strcmp(file_buffer, file_content) == 0, "file content not what was written")) { COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } } if (file_content != NULL) { free(file_content); } } ) COSMS_CORE_TEST_SUB_TEST(writting big file, 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'; CosmsCoreFileError error = cosms_core_file_write("tests/data/large-write-file.txt", large_file_buffer, large_file_size, true); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { char *file_content = NULL; error = cosms_core_file_read("tests/data/large-write-file.txt", &file_content, NULL); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { if (cosms_core_test_assert(cosms_core_sub_test_name, strcmp(large_file_buffer, file_content) == 0, "file content not what was written")) { COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } } if (file_content != NULL) { free(file_content); } } free(large_file_buffer); ) COSMS_CORE_TEST_SUB_TEST(appending file, char *file_buffer = "Hello, World!"; CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), false); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { char *file_content = NULL; error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { const char *expected_result = "Hello, World!Hello, World!"; if (cosms_core_test_assert(cosms_core_sub_test_name, strcmp(expected_result, file_content) == 0, "file content not what was written")) { COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } } if (file_content != NULL) { free(file_content); } } ) COSMS_CORE_TEST_SUB_TEST(appending to non existing file, char *file_buffer = "Hello, World!"; CosmsCoreFileError error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer), false); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { char *file_content = NULL; error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { if (cosms_core_test_assert(cosms_core_sub_test_name, strcmp(file_buffer, file_content) == 0, "file content not what was written")) { COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } } if (file_content != NULL) { free(file_content); } } ) ) COSMS_CORE_TEST_TEST(file_delete, COSMS_CORE_TEST_SUB_TEST(deleting small file, CosmsCoreFileError error = cosms_core_file_delete("tests/data/small-write-file.txt"); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { char *file_content = NULL; error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } if (file_content != NULL) { free(file_content); } } ) COSMS_CORE_TEST_SUB_TEST(deleting large file, CosmsCoreFileError error = cosms_core_file_delete("tests/data/append-write-file.txt"); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { char *file_content = NULL; error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } if (file_content != NULL) { free(file_content); } } ) COSMS_CORE_TEST_SUB_TEST(deleting non existing file, CosmsCoreFileError error = cosms_core_file_delete("non-existing-file.cosms"); if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } ) )