diff --git a/.gitignore b/.gitignore index 0ef0814..9fba556 100644 --- a/.gitignore +++ b/.gitignore @@ -65,5 +65,10 @@ compile_commands.json CTestTestfile.cmake _deps CMakeUserPresets.json - build + +# clangd +.cache/ + +# generated test data +tests/data/*.bin \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 83aacc5..bd6b4d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,10 +11,11 @@ target_include_directories(cosms-core PUBLIC ${PROJECT_SOURCE_DIR}/include) if (MSVC) target_compile_options(cosms-core PRIVATE /W4) else() - target_compile_options(cosms-core PRIVATE -wall -Wextra -pedantic) + target_compile_options(cosms-core PRIVATE -Wall -Wextra -pedantic) endif() -add_executable(cosms-core-test ${PROJECT_SOURCE_DIR}/tests/main.c) +file(GLOB_RECURSE SRC_TEST_FILES "${PROJECT_SOURCE_DIR}/tests/*.c") +add_executable(cosms-core-test ${SRC_TEST_FILES}) target_include_directories(cosms-core-test PRIVATE ${PROJECT_SOURCE_DIR}/tests) target_link_libraries(cosms-core-test cosms-core) diff --git a/include/cosms-core/file.h b/include/cosms-core/file.h index d29be8b..d91ba9e 100644 --- a/include/cosms-core/file.h +++ b/include/cosms-core/file.h @@ -1,7 +1,130 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ #ifndef COSMS_CORE_FILE #define COSMS_CORE_FILE -void cosms_core_file_read(char *path); -void cosms_core_file_write(char *path); - +#if defined(_WIN32) +#include #endif + +typedef enum { + COSMS_CORE_FILE_OK = 0, + COSMS_CORE_FILE_NOT_FOUND = -1, + COSMS_CORE_FILE_NO_ACCESS = -2, + COSMS_CORE_FILE_LIMIT_REACHED = -3, + COSMS_CORE_FILE_COULD_NOT_READ_SIZE = -4, + COSMS_CORE_FILE_UNKOWN_ERROR = -5, + COSMS_CORE_FILE_FAILED_TO_ALLOCATE = -6, + COSMS_CORE_FILE_FAILED_TO_READ = -7, + COSMS_CORE_FILE_FAILED_TO_WRITE = -8, + COSMS_CORE_FILE_STILL_OPEN = -9, + COSMS_CORE_FILE_INVALID_FILE = -10, + COSMS_CORE_FILE_INVALID_OPERATION = -11 +} CosmsCoreFileError; + +#define COSMS_CORE_FILE_LARGE 0x7FFFFFFF + +#define COSMS_CORE_FILE_MODE_READ (1u << 0) +#define COSMS_CORE_FILE_MODE_WRITE (1u << 1) +#define COSMS_CORE_FILE_MODE_APPEND (1u << 2) +#define COSMS_CORE_FILE_MODE_CREATE (1u << 3) + +struct cosms_core_file { + int mode; + + #if defined(__GNUC__) + int native_file; + #elif defined(_WIN32) + HANDLE native_file; + #endif +}; + +/** + * @brief Opens file in specified mode. + * + * @param file Pointer to file struct (must not be NULL) + * @param path to the file to open + * @param mode to open the file in (read, write, apped, create or a combination of these) + * @return COSMS_CORE_FILE_OK on success, or a negative error code + */ +CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode); +/** + * @brief Closes an open file. + * + * @param file Pointer to file struct that has been opened (must not be NULL) + * @return COSMS_CORE_FILE_OK on success, or a negative error code + */ +CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file); + +/** + * @brief Gets the size of an opened file. + * + * @param file Pointer to file struct that has been opned (must not be NULL) + * @param size Pointer to the variable to store the size in (must not be NULL) + * @return COSMS_CORE_FILE_OK on success, or a negative error code + */ +CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size); + +/** + * @brief Try's to reads specified number of bytes. + * + * @param file Pointer to file struct that has been opened in read mode (must not be NULL) + * @param buffer to store the file content in (has to be preallocated stack or heap memory) + * @param bytes_to_read from the file. + * @param bytes_read Pointer to the variable to store the amount of bytes that where actually read (can be NULL) + * @return COSMS_CORE_FILE_OK on success, or a negative error code + */ +CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read); +/** + * @brief Reads the whole file with support for reading large files. + * + * @param file Pointer to file struct that has been opened in read mode (must not be NULL) + * @param buffer Pointer to buffer to store the file content in (needs to be empty) + * @param bytes_read Pointer to the variable to store the amount of bytes that where actually read (can be NULL) + * @return COSMS_CORE_FILE_OK on success, or a negative error code + */ +CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read); + +/** + * @brief Try's to write specified number of bytes. + * + * @param file Pointer to file struct that has been opened in write mode (must not be NULL) + * @param buffer that contains the bytes to write (must not be NULL) + * @param bytes_to_write to the file + * @param bytes_written Pointer to the variable to store the amount of bytes that where actually written (can be NULL) + * @return COSMS_CORE_FILE_OK on success, or a negative error code + */ +CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written); +/** + * @brief Writes the full buffer to the file with support for writting large files. + * + * @param file Pointer to file struct that has been opened in write mode (must not be NULL) + * @param buffer that contains the bytes to write (must not be NULL) + * @param bytes_to_write to the file + * @return COSMS_CORE_FILE_OK on success, or a negative error code + */ +CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write); + +/** + * @brief Deletes specified file from system. + * + * @param path to the file to delete + * @return COSMS_CORE_FILE_OK on success, or a negative error code + */ +CosmsCoreFileError cosms_core_file_delete(const char *path); + +/** + * @brief Converts error code to a string that can be used for printing/logging errors. + * + * @param error The error code to convert to a string message. + * @return The error as a string on success, or NULL + */ +const char *cosms_core_file_error_string(CosmsCoreFileError error); + +#endif /* COSMS_CORE_FILE */ diff --git a/src/file.c b/src/file.c index bf321da..6e77ec9 100644 --- a/src/file.c +++ b/src/file.c @@ -1,11 +1,458 @@ -#include +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#include "cosms-core/file.h" -#include +#include +#include +#include -void cosms_core_file_read(char *path) { - printf("Hello, World!"); +#if defined(__GNUC__) +#define _FILE_OFFSET_BITS_ 64 + +#include +#include +#include +#include +#endif + +/** + * @brief Opens file in specified mode. + */ +CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode) { + file->mode = mode; + + #if defined(__GNUC__) + int flags = 0; + if (mode & (COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE)) { + flags |= O_RDWR; + } else if (mode & COSMS_CORE_FILE_MODE_READ) { + flags |= O_RDONLY; + } else if (mode & COSMS_CORE_FILE_MODE_WRITE) { + flags |= O_WRONLY; + } + + if (mode & COSMS_CORE_FILE_MODE_APPEND) { + flags |= O_APPEND; + } else if (mode & COSMS_CORE_FILE_MODE_CREATE) { + flags |= (O_TRUNC | O_CREAT); + } + + #ifdef O_LARGEFILE + struct stat file_stat; + if (stat(path, &file_stat) != 0) { + if (errno == ENOENT) { + return COSMS_CORE_FILE_NOT_FOUND; + } + + return COSMS_CORE_FILE_COULD_NOT_READ_SIZE; + } + + if (file_stat.st_size > COSMS_CORE_FILE_LARGE) { + flags |= O_LARGEFILE; + } + #endif + + file->native_file = open(path, flags, 0644); + if (file->native_file == -1) { + switch(errno) { + case ENOENT: + return COSMS_CORE_FILE_NOT_FOUND; + + case EACCES: + return COSMS_CORE_FILE_NO_ACCESS; + + case EMFILE: + case ENFILE: + return COSMS_CORE_FILE_LIMIT_REACHED; + + default: + return COSMS_CORE_FILE_UNKOWN_ERROR; + } + } + #elif defined(_WIN32) + DWORD access_flags = 0; + DWORD create_flag = OPEN_EXISTING; + + if (mode & COSMS_CORE_FILE_MODE_READ) { + access_flags |= GENERIC_READ; + } + + if (mode & COSMS_CORE_FILE_MODE_WRITE) { + access_flags |= GENERIC_WRITE; + } + + if (mode & COSMS_CORE_FILE_MODE_APPEND) { + create_flag = OPEN_ALWAYS; + } else if (mode & COSMS_CORE_FILE_MODE_CREATE) { + create_flag = CREATE_ALWAYS; + } + + file->native_file = CreateFile(path, access_flags, 0, NULL, create_flag, FILE_ATTRIBUTE_NORMAL, NULL); + if (file->native_file == INVALID_HANDLE_VALUE) { + switch(GetLastError()) { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + return COSMS_CORE_FILE_NOT_FOUND; + + case ERROR_ACCESS_DENIED: + case ERROR_SHARING_VIOLATION: + case ERROR_LOCK_VIOLATION: + return COSMS_CORE_FILE_NO_ACCESS; + + case ERROR_TOO_MANY_OPEN_FILES: + return COSMS_CORE_FILE_LIMIT_REACHED; + + default: + return COSMS_CORE_FILE_UNKOWN_ERROR; + } + } + #endif + + return COSMS_CORE_FILE_OK; } -void cosms_core_file_write(char *path) { - printf("Hello, World"); +/** + * @brief Closes an open file. + */ +CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) { + #if defined(__GNUC__) + int error = close(file->native_file); + if (error == -1) { + if (errno == EBADF) { + return COSMS_CORE_FILE_INVALID_FILE; + } + + return COSMS_CORE_FILE_UNKOWN_ERROR; + } + #elif defined(_WIN32) + BOOL error = CloseHandle(file->native_file); + if (error == 0) { + if (GetLastError() == ERROR_INVALID_HANDLE) { + return COSMS_CORE_FILE_INVALID_FILE; + } + + return COSMS_CORE_FILE_UNKOWN_ERROR; + } + #endif + + return COSMS_CORE_FILE_OK; +} + +/** + * @brief Gets the size of an opened file. + */ +CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size) { + #if defined(__GNUC__) + struct stat file_stat; + if (fstat(file->native_file, &file_stat) != 0) { + if (errno == EBADF) { + return COSMS_CORE_FILE_INVALID_FILE; + } + + return COSMS_CORE_FILE_COULD_NOT_READ_SIZE; + } + + (*size) = file_stat.st_size; + #elif defined(_WIN32) + DWORD high_size; + DWORD low_size = GetFileSize(file->native_file, &high_size); + if (low_size == INVALID_FILE_SIZE) { + if (GetLastError() == ERROR_INVALID_HANDLE) { + return COSMS_CORE_FILE_INVALID_FILE; + } + + return COSMS_CORE_FILE_COULD_NOT_READ_SIZE; + } + + (*size) = ((unsigned long long)high_size << 32) | low_size; + #endif + + return COSMS_CORE_FILE_OK; +} + +/** + * @brief Try's to reads specified number of bytes. + */ +CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read) { + if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) { + return COSMS_CORE_FILE_INVALID_OPERATION; + } + + #if defined(__GNUC__) + int read_bytes = read(file->native_file, buffer, bytes_to_read); + if (read_bytes == -1) { + return COSMS_CORE_FILE_FAILED_TO_READ; + } + + if (bytes_read != NULL) { + (*bytes_read) = read_bytes; + } + #elif defined(_WIN32) + if (ReadFile(file->native_file, buffer, bytes_to_read, (LPDWORD)bytes_read, NULL) == 0) { + return COSMS_CORE_FILE_FAILED_TO_READ; + } + #endif + + buffer[bytes_to_read] = '\0'; + return COSMS_CORE_FILE_OK; +} + +/** + * @brief Reads the whole file with support for reading large files. + */ +CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read) { + if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) { + return COSMS_CORE_FILE_INVALID_OPERATION; + } + + #if defined(__GNUC__) + struct stat file_stat; + if (fstat(file->native_file, &file_stat) != 0) { + return COSMS_CORE_FILE_COULD_NOT_READ_SIZE; + } + + if (bytes_read != NULL) { + (*bytes_read) = file_stat.st_size; + } + + (*buffer) = (char*)malloc((file_stat.st_size + 1) * sizeof(char)); + if ((*buffer) == NULL) { + return COSMS_CORE_FILE_FAILED_TO_ALLOCATE; + } + + unsigned long long remaining_bytes = file_stat.st_size; + while (remaining_bytes != 0) { + unsigned int current_bytes_to_read; + if (remaining_bytes > UINT_MAX) { + current_bytes_to_read = UINT_MAX; + } else { + current_bytes_to_read = (unsigned int)remaining_bytes; + } + + int read_bytes = read(file->native_file, (*buffer) + (file_stat.st_size - remaining_bytes), current_bytes_to_read); + if (read_bytes == -1) { + free(*buffer); + return COSMS_CORE_FILE_FAILED_TO_READ; + } + + remaining_bytes -= read_bytes; + } + + (*buffer)[file_stat.st_size] = '\0'; + #elif defined(_WIN32) + DWORD high_size; + DWORD low_size = GetFileSize(file->native_file, &high_size); + if (low_size == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) { + return COSMS_CORE_FILE_COULD_NOT_READ_SIZE; + } + + unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size; + if (bytes_read != NULL) { + (*bytes_read) = file_size; + } + + (*buffer) = (char*)malloc((file_size + 1) * sizeof(char)); + if ((*buffer) == NULL) { + return COSMS_CORE_FILE_FAILED_TO_ALLOCATE; + } + + unsigned long long remaining_bytes = file_size; + while (remaining_bytes != 0) { + DWORD current_bytes_to_read, read_bytes; + if (remaining_bytes > UINT_MAX) { + current_bytes_to_read = UINT_MAX; + } else { + current_bytes_to_read = (DWORD)remaining_bytes; + } + + if (ReadFile(file->native_file, (*buffer) + (file_size - remaining_bytes), current_bytes_to_read, &read_bytes, NULL) == 0) { + free(*buffer); + return COSMS_CORE_FILE_FAILED_TO_READ; + } + + remaining_bytes -= read_bytes; + } + + (*buffer)[file_size] = '\0'; + #endif + + return COSMS_CORE_FILE_OK; +} + +/** + * @brief Try's to write specified number of bytes. + */ +CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written) { + if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) { + return COSMS_CORE_FILE_INVALID_OPERATION; + } + + #if defined(__GNUC__) + int written_bytes = write(file->native_file, buffer, bytes_to_write); + if (written_bytes == -1) { + return COSMS_CORE_FILE_FAILED_TO_WRITE; + } + + if (bytes_written != NULL) { + (*bytes_written) = written_bytes; + } + #elif defined(_WIN32) + if (file->mode & COSMS_CORE_FILE_MODE_APPEND) { + SetFilePointer(file->native_file, 0, NULL, FILE_END); + } + + if (WriteFile(file->native_file, buffer, bytes_to_write, (LPDWORD)bytes_written, NULL) == 0) { + return COSMS_CORE_FILE_FAILED_TO_WRITE; + } + #endif + + return COSMS_CORE_FILE_OK; +} + +/** + * @brief Writes the full buffer to the file with support for writting large files. + */ +CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write) { + if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) { + return COSMS_CORE_FILE_INVALID_OPERATION; + } + + #if defined(__GNUC__) + unsigned long long remaining_bytes = bytes_to_write; + while (remaining_bytes != 0) { + unsigned int current_bytes_to_write; + if (remaining_bytes > UINT_MAX) { + current_bytes_to_write = UINT_MAX; + } else { + current_bytes_to_write = (unsigned int)remaining_bytes; + } + + int written_bytes = write(file->native_file, buffer + (bytes_to_write - remaining_bytes), current_bytes_to_write); + if (written_bytes == -1) { + return COSMS_CORE_FILE_FAILED_TO_WRITE; + } + + remaining_bytes -= written_bytes; + } + #elif defined(_WIN32) + if (file->mode & COSMS_CORE_FILE_MODE_APPEND) { + LARGE_INTEGER zero; + zero.QuadPart = 0; + + SetFilePointerEx(file->native_file, zero, NULL, FILE_END); + } + + unsigned long long remaining_bytes = bytes_to_write; + while (remaining_bytes != 0) { + DWORD current_bytes_to_write, written_bytes; + if (remaining_bytes > UINT_MAX) { + current_bytes_to_write = UINT_MAX; + } else { + current_bytes_to_write = (DWORD)remaining_bytes; + } + + if (WriteFile(file->native_file, buffer + (bytes_to_write - remaining_bytes), current_bytes_to_write, &written_bytes, NULL) == 0) { + return COSMS_CORE_FILE_FAILED_TO_WRITE; + } + + remaining_bytes -= written_bytes; + } + #endif + + return COSMS_CORE_FILE_OK; +} + +/** + * @brief Deletes specified file from system. + */ +CosmsCoreFileError cosms_core_file_delete(const char *path) { + #if defined(__GNUC__) + if (unlink(path) == -1) { + switch (errno) { + case ENOENT: + return COSMS_CORE_FILE_NOT_FOUND; + + case EACCES: + return COSMS_CORE_FILE_NO_ACCESS; + + case EBUSY: + return COSMS_CORE_FILE_STILL_OPEN; + + default: + return COSMS_CORE_FILE_UNKOWN_ERROR; + } + } + #elif defined(_WIN32) + if (!DeleteFile(path)) { + switch(GetLastError()) { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + return COSMS_CORE_FILE_NOT_FOUND; + + case ERROR_ACCESS_DENIED: + return COSMS_CORE_FILE_NO_ACCESS; + + case ERROR_LOCK_VIOLATION: + case ERROR_SHARING_VIOLATION: + return COSMS_CORE_FILE_STILL_OPEN; + + default: + return COSMS_CORE_FILE_UNKOWN_ERROR; + } + } + #endif + + return COSMS_CORE_FILE_OK; +} + +/** + * @brief Converts error code to a string that can be used for printing/logging errors. + */ +const char *cosms_core_file_error_string(CosmsCoreFileError error) { + switch(error) { + case COSMS_CORE_FILE_OK: + return "cosms-file everything ok"; + + case COSMS_CORE_FILE_NOT_FOUND: + return "cosms-file path not found"; + + case COSMS_CORE_FILE_NO_ACCESS: + return "cosms-file no access"; + + case COSMS_CORE_FILE_LIMIT_REACHED: + return "cosms-file to many open files"; + + case COSMS_CORE_FILE_COULD_NOT_READ_SIZE: + return "cosms-file failed to read size"; + + case COSMS_CORE_FILE_UNKOWN_ERROR: + return "cosms-file unkown error occured"; + + case COSMS_CORE_FILE_FAILED_TO_ALLOCATE: + return "cosms-file failed to allocate memory"; + + case COSMS_CORE_FILE_FAILED_TO_READ: + return "cosms-file failed to read content of file"; + + case COSMS_CORE_FILE_FAILED_TO_WRITE: + return "cosms-file failed to write content to file"; + + case COSMS_CORE_FILE_STILL_OPEN: + return "cosms-file still open"; + + case COSMS_CORE_FILE_INVALID_FILE: + return "cosms-file invalid native file given"; + + case COSMS_CORE_FILE_INVALID_OPERATION: + return "cosms-file invalid operation for current file"; + + default: + return NULL; + } } diff --git a/tests/data/small-file.txt b/tests/data/small-file.txt new file mode 100644 index 0000000..b45ef6f --- /dev/null +++ b/tests/data/small-file.txt @@ -0,0 +1 @@ +Hello, World! \ No newline at end of file diff --git a/tests/main.c b/tests/main.c index e6e6793..402ef39 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,6 +1,19 @@ -#include +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#include "tools/tool.h" +#include "unit/unit.h" int main(void) { - cosms_core_file_read("test.txt"); + if (cosms_core_tool_generate_test_data() != 0) { + return -1; + } + + COSMS_CORE_TEST_RUN() return 0; } diff --git a/tests/test.h b/tests/test.h new file mode 100644 index 0000000..427c1e4 --- /dev/null +++ b/tests/test.h @@ -0,0 +1,54 @@ +#ifndef COSMS_CORE_TEST +#define COSMS_CORE_TEST + +#include + +#define COSMS_CORE_TEST_EXPORT_TEST(name) { #name, cosms_core_##name##_test } +#define COSMS_CORE_TEST_DEFINE(name) const char *cosms_core_##name##_test(void) +#define COSMS_CORE_TEST_TEST(name, implementation) const char *cosms_core_##name##_test(void) { \ + implementation \ +} + +/** + * @brief Creates a list to export so it can be imported into the main test list. + */ +#define COSMS_CORE_TEST_EXPORT(name, ...) static const struct cosms_core_test cosms_core_test_##name[] = { \ + __VA_ARGS__, \ + 0 \ + } + +/** + * @brief Creates a list of lists from exported tests so all tests can be easly executed. + */ +#define COSMS_CORE_TEST_START static const struct cosms_core_test *cosms_core_test_tests[] = { +#define COSMS_CORE_TEST_IMPORT(name) cosms_core_test_##name +#define COSMS_CORE_TEST_END }; + +/** + * @brief Runs all tests specified in the cosms_core_test_tests array; + */ +#define COSMS_CORE_TEST_RUN() \ + for (unsigned long long index = 0; index < sizeof(cosms_core_test_tests) / sizeof(cosms_core_test_tests[0]); index += 1) { \ + unsigned long long test_index = 0; \ + struct cosms_core_test current_test = cosms_core_test_tests[index][test_index]; \ + while (current_test.name != 0) { \ + printf("[*] running test %s...", current_test.name); \ + fflush(stdout); \ + const char *result = current_test.function(); \ + if (result == NULL) { \ + printf("\r[PASS] test %s completed successfully!\n", current_test.name); \ + } else { \ + printf("\r[FAIL] test %s failed with error:\n\t%s\n", current_test.name, result); \ + } \ + \ + test_index += 1; \ + current_test = cosms_core_test_tests[index][test_index]; \ + } \ + } + +struct cosms_core_test { + const char *name; + const char *(*function)(void); +}; + +#endif /* COSMS_CORE_TEST */ diff --git a/tests/tools/tool-file.c b/tests/tools/tool-file.c new file mode 100644 index 0000000..5d84a0d --- /dev/null +++ b/tests/tools/tool-file.c @@ -0,0 +1,89 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#include "tool-file.h" + +#include +#include +#include + +#define COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE 5242880000ULL +#define CHUNK 4096 + +#define LARGE_FILE_PATH "tests/data/large-file.bin" + +#define SMALL_DELETE_FILE_PATH "tests/data/small-delete-file.bin" +#define LARGE_DELETE_FILE_PATH "tests/data/large-delete-file.bin" + +/** + * @brief Generates a large nearly 5GB file for testing. + */ +int cosms_core_tool_file_generate_large_file(void) { + FILE *file = fopen(LARGE_FILE_PATH, "wb"); + if (file == NULL) { + return -1; + } + + static char buffer[CHUNK]; + for (unsigned int index = 0; index < CHUNK; index += 1) { + buffer[index] = 'a'; + } + + unsigned long long written_bytes = 0; + while (written_bytes < COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE) { + unsigned int bytes_to_write = CHUNK; + if (written_bytes + CHUNK > COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE) { + bytes_to_write = (unsigned int)(COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE - written_bytes); + } + + fwrite(buffer, 1, bytes_to_write, file); + written_bytes += bytes_to_write; + } + + fclose(file); + return 0; +} + +/** + * @brief Generates files to be deleted. + */ +int cosms_core_tool_file_generate_delete_file(void) { + FILE *file = fopen(SMALL_DELETE_FILE_PATH, "wb"); + if (file == NULL) { + return -1; + } + + char *small_buffer = "Hello, World!"; + fwrite(small_buffer, 1, strlen(small_buffer), file); + + fclose(file); + + file = fopen(LARGE_DELETE_FILE_PATH, "wb"); + if (file == NULL) { + return -1; + } + + static char large_buffer[CHUNK]; + for (unsigned int index = 0; index < CHUNK; index += 1) { + large_buffer[index] = 'a'; + } + + unsigned long long written_bytes = 0; + while (written_bytes < COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE) { + unsigned int bytes_to_write = CHUNK; + if (written_bytes + CHUNK > COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE) { + bytes_to_write = (unsigned int)(COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE - written_bytes); + } + + fwrite(large_buffer, 1, bytes_to_write, file); + written_bytes += bytes_to_write; + } + + fclose(file); + return 0; +} \ No newline at end of file diff --git a/tests/tools/tool-file.h b/tests/tools/tool-file.h new file mode 100644 index 0000000..72cae10 --- /dev/null +++ b/tests/tools/tool-file.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#ifndef COSMS_CORE_TOOL_FILE +#define COSMS_CORE_TOOL_FILE + +/** + * @brief Generates a large nearly 5GB file for testing. + */ +int cosms_core_tool_file_generate_large_file(void); +/** + * @brief Generates files to be deleted. + */ +int cosms_core_tool_file_generate_delete_file(void); + +#endif /* COSMS_CORE_TOOL_FILE */ \ No newline at end of file diff --git a/tests/tools/tool.c b/tests/tools/tool.c new file mode 100644 index 0000000..0532bef --- /dev/null +++ b/tests/tools/tool.c @@ -0,0 +1,30 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#include "tool.h" +#include "tool-file.h" + +#include + +int cosms_core_tool_generate_test_data(void) { + printf("[*] Generating test data..."); + fflush(stdout); + + if (cosms_core_tool_file_generate_large_file() != 0) { + printf("\r[FAILED] could not generate large file!\n"); + return -1; + } + + if (cosms_core_tool_file_generate_delete_file() != 0) { + printf("\r[FAILED] could not generate delete files!\n"); + return -1; + } + + printf("\r[COMPLETED] finished generating test data!\n"); + return 0; +} \ No newline at end of file diff --git a/tests/tools/tool.h b/tests/tools/tool.h new file mode 100644 index 0000000..fb0b296 --- /dev/null +++ b/tests/tools/tool.h @@ -0,0 +1,14 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#ifndef COSMS_CORE_TOOL +#define COSMS_CORE_TOOL + +int cosms_core_tool_generate_test_data(void); + +#endif /* COSMS_CORE_TOOL */ \ No newline at end of file diff --git a/tests/unit/file.c b/tests/unit/file.c new file mode 100644 index 0000000..a523139 --- /dev/null +++ b/tests/unit/file.c @@ -0,0 +1,738 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#include "unit/file.h" + +#include + +#include +#include + +#if defined (__GNUC__) +#define COSMS_CORE_FILE_TEST_INVALID_NATIVE_FILE -1 +#elif defined(_WIN32) +#define COSMS_CORE_FILE_TEST_INVALID_NATIVE_FILE NULL +#endif + +#define COSMS_CORE_FILE_TEST_LARGE_FILE_SIZE 5242880000ULL + +COSMS_CORE_TEST_TEST(file_open_small_file_read, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_open_large_file_read, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.bin", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_open_non_existing_file_read, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "non-existing-file.cosms", COSMS_CORE_FILE_MODE_READ); + + if (error == COSMS_CORE_FILE_OK) { + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return cosms_core_file_error_string(error); + } + + if (error != COSMS_CORE_FILE_NOT_FOUND) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_open_small_file_write, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_WRITE); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_open_large_file_write, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.bin", COSMS_CORE_FILE_MODE_WRITE); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_open_small_file_append, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_APPEND); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_open_large_file_append, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.bin", COSMS_CORE_FILE_MODE_APPEND); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_open_small_file_read_write, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_open_large_file_read_write, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.bin", COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_close, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_close_non_existing_file, + struct cosms_core_file file; + file.native_file = COSMS_CORE_FILE_TEST_INVALID_NATIVE_FILE; + + CosmsCoreFileError error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_INVALID_FILE) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_size_small_file, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned long long size = 0; + error = cosms_core_file_get_size(&file, &size); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + if (size != 13) { + cosms_core_file_close(&file); + return "result does not match expected result"; + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_size_large, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.bin", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned long long size = 0; + error = cosms_core_file_get_size(&file, &size); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + if (size != COSMS_CORE_FILE_TEST_LARGE_FILE_SIZE) { + cosms_core_file_close(&file); + return "result does not match expected result"; + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_size_non_existing_file, + struct cosms_core_file file; + file.native_file = COSMS_CORE_FILE_TEST_INVALID_NATIVE_FILE; + + unsigned long long size = 0; + CosmsCoreFileError error = cosms_core_file_get_size(&file, &size); + if (error != COSMS_CORE_FILE_INVALID_FILE) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_small_file, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned int bytes_to_read = 13; + unsigned int bytes_read = 0; + + char buffer[14]; + error = cosms_core_file_read(&file, buffer, bytes_to_read, &bytes_read); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + if (bytes_to_read != bytes_read || strcmp(buffer, "Hello, World!") != 0) { + cosms_core_file_close(&file); + return "result does not match expected result"; + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_large_file, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.bin", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned long long bytes_to_read = COSMS_CORE_FILE_TEST_LARGE_FILE_SIZE; + unsigned long long bytes_read = 0; + + char *buffer = NULL; + error = cosms_core_file_read_all(&file, &buffer, &bytes_read); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + char *expected_result = (char*)malloc((bytes_to_read + 1) * sizeof(char)); + if (expected_result == NULL) { + cosms_core_file_close(&file); + free(buffer); + + return "Failed to allocate memory"; + } + + for (unsigned long long index = 0; index < bytes_to_read; index += 1) { + expected_result[index] = 'a'; + } + + expected_result[bytes_to_read] = '\0'; + if (bytes_to_read != bytes_read || strcmp(buffer, expected_result) != 0) { + cosms_core_file_close(&file); + + free(expected_result); + free(buffer); + + return "result does not match expected result"; + } + + free(expected_result); + free(buffer); + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_non_existing_file, + struct cosms_core_file file; + file.mode = COSMS_CORE_FILE_MODE_READ; + file.native_file = COSMS_CORE_FILE_TEST_INVALID_NATIVE_FILE; + + unsigned int bytes_to_read = 13; + unsigned int bytes_read = 0; + + char buffer[14]; + CosmsCoreFileError error = cosms_core_file_read(&file, buffer, bytes_to_read, &bytes_read); + if (error != COSMS_CORE_FILE_FAILED_TO_READ) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_full_non_existing_file, + struct cosms_core_file file; + file.mode = COSMS_CORE_FILE_MODE_READ; + file.native_file = COSMS_CORE_FILE_TEST_INVALID_NATIVE_FILE; + + unsigned long long bytes_read = 0; + char *buffer = NULL; + CosmsCoreFileError error = cosms_core_file_read_all(&file, &buffer, &bytes_read); + if (error != COSMS_CORE_FILE_COULD_NOT_READ_SIZE) { + if (error == COSMS_CORE_FILE_OK) { + free(buffer); + } + + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_file_using_wrong_mode, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_APPEND); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned int bytes_to_read = 13; + unsigned int bytes_read = 0; + + char buffer[14]; + error = cosms_core_file_read(&file, buffer, bytes_to_read, &bytes_read); + if (error != COSMS_CORE_FILE_INVALID_OPERATION) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_full_file_using_wrong_mode, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_APPEND); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned long long bytes_read = 0; + char *buffer = NULL; + error = cosms_core_file_read_all(&file, &buffer, &bytes_read); + if (error != COSMS_CORE_FILE_INVALID_OPERATION) { + cosms_core_file_close(&file); + if (error == COSMS_CORE_FILE_OK) { + free(buffer); + } + + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_write_small_file, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-write-file.bin", COSMS_CORE_FILE_MODE_WRITE | COSMS_CORE_FILE_MODE_CREATE); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned int bytes_to_write = 13; + unsigned int bytes_written = 0; + + char *write_buffer = "Hello, World!"; + error = cosms_core_file_write(&file, write_buffer, bytes_to_write, &bytes_written); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + if (bytes_to_write != bytes_written) { + cosms_core_file_close(&file); + return "did not write all bytes to file"; + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_open(&file, "tests/data/small-write-file.bin", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + char read_buffer[14] = { 0 }; + error = cosms_core_file_read(&file, read_buffer, bytes_to_write, NULL); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + if (strcmp(read_buffer, write_buffer) != 0) { + cosms_core_file_close(&file); + return "result does not match expected result"; + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_write_large_file, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-write-file.bin", COSMS_CORE_FILE_MODE_WRITE | COSMS_CORE_FILE_MODE_CREATE); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned long long bytes_to_write = COSMS_CORE_FILE_TEST_LARGE_FILE_SIZE; + char *write_buffer = (char*)malloc((bytes_to_write + 1) * sizeof(char)); + for (unsigned long long index = 0; index < bytes_to_write; index += 1) { + write_buffer[index] = 'a'; + } + + write_buffer[bytes_to_write] = '\0'; + error = cosms_core_file_write_all(&file, write_buffer, bytes_to_write); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + free(write_buffer); + + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + free(write_buffer); + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_open(&file, "tests/data/large-write-file.bin", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + free(write_buffer); + return cosms_core_file_error_string(error); + } + + char *read_buffer = NULL; + error = cosms_core_file_read_all(&file, &read_buffer, NULL); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + free(write_buffer); + + return cosms_core_file_error_string(error); + } + + if (strcmp(read_buffer, write_buffer) != 0) { + cosms_core_file_close(&file); + + free(read_buffer); + free(write_buffer); + + return "result does not match expected result"; + } + + free(read_buffer); + free(write_buffer); + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_write_append_small_file, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-write-file.bin", COSMS_CORE_FILE_MODE_WRITE | COSMS_CORE_FILE_MODE_APPEND); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned int bytes_to_write = 13; + unsigned int bytes_written = 0; + + char *write_buffer = "Hello, World!"; + error = cosms_core_file_write(&file, write_buffer, bytes_to_write, &bytes_written); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + if (bytes_to_write != bytes_written) { + cosms_core_file_close(&file); + return "did not write all bytes to file"; + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_open(&file, "tests/data/small-write-file.bin", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned int bytes_to_read = 26; + char read_buffer[27] = { 0 }; + error = cosms_core_file_read(&file, read_buffer, bytes_to_read, NULL); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + if (strcmp(read_buffer, "Hello, World!Hello, World!") != 0) { + cosms_core_file_close(&file); + return "result does not match expected result"; + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_write_append_large_file, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-write-file.bin", COSMS_CORE_FILE_MODE_WRITE | COSMS_CORE_FILE_MODE_APPEND); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned long long bytes_to_write = 2; + char *write_buffer = "bb"; + error = cosms_core_file_write_all(&file, write_buffer, bytes_to_write); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_open(&file, "tests/data/large-write-file.bin", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned long long bytes_to_read = COSMS_CORE_FILE_TEST_LARGE_FILE_SIZE + 2; + unsigned long long bytes_read = 0; + char *read_buffer = NULL; + error = cosms_core_file_read_all(&file, &read_buffer, &bytes_read); + if (error != COSMS_CORE_FILE_OK) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + char *expected_result = (char*)malloc((bytes_to_read + 1) * sizeof(char)); + for (unsigned long long index = 0; index < bytes_to_read; index += 1) { + expected_result[index] = 'a'; + } + + expected_result[bytes_to_read - 2] = 'b'; + expected_result[bytes_to_read - 1] = 'b'; + expected_result[bytes_to_read] = '\0'; + + if (strcmp(read_buffer, expected_result) != 0) { + cosms_core_file_close(&file); + + free(expected_result); + free(read_buffer); + + return "result does not match expected result"; + } + + free(expected_result); + free(read_buffer); + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_write_using_wrong_mode, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-write-file.bin", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned int bytes_to_write = 13; + unsigned int bytes_written = 0; + + char *write_buffer = "Hello, World!"; + error = cosms_core_file_write(&file, write_buffer, bytes_to_write, &bytes_written); + if (error != COSMS_CORE_FILE_INVALID_OPERATION) { + cosms_core_file_close(&file); + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_write_full_file_using_wrong_mode, + struct cosms_core_file file; + CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-write-file.bin", COSMS_CORE_FILE_MODE_READ); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + unsigned long long bytes_to_write = COSMS_CORE_FILE_TEST_LARGE_FILE_SIZE; + char *write_buffer = (char*)malloc((bytes_to_write + 1) * sizeof(char)); + for (unsigned long long index = 0; index < bytes_to_write; index += 1) { + write_buffer[index] = 'a'; + } + + write_buffer[bytes_to_write] = '\0'; + error = cosms_core_file_write_all(&file, write_buffer, bytes_to_write); + if (error != COSMS_CORE_FILE_INVALID_OPERATION) { + cosms_core_file_close(&file); + free(write_buffer); + + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + free(write_buffer); + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_delete_small_file, + CosmsCoreFileError error = cosms_core_file_delete("tests/data/small-delete-file.bin"); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_delete_large_file, + CosmsCoreFileError error = cosms_core_file_delete("tests/data/large-delete-file.bin"); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_delete_non_existing_file, + CosmsCoreFileError error = cosms_core_file_delete("non-existing-file.cosms"); + if (error != COSMS_CORE_FILE_NOT_FOUND) { + return cosms_core_file_error_string(error); + } + + return NULL; +) diff --git a/tests/unit/file.h b/tests/unit/file.h new file mode 100644 index 0000000..3d54417 --- /dev/null +++ b/tests/unit/file.h @@ -0,0 +1,86 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#ifndef COSMS_CORE_FILE_TEST +#define COSMS_CORE_FILE_TEST + +#include "test.h" + +COSMS_CORE_TEST_DEFINE(file_open_small_file_read); +COSMS_CORE_TEST_DEFINE(file_open_large_file_read); +COSMS_CORE_TEST_DEFINE(file_open_non_existing_file_read); +COSMS_CORE_TEST_DEFINE(file_open_small_file_write); +COSMS_CORE_TEST_DEFINE(file_open_large_file_write); +COSMS_CORE_TEST_DEFINE(file_open_small_file_append); +COSMS_CORE_TEST_DEFINE(file_open_large_file_append); +COSMS_CORE_TEST_DEFINE(file_open_small_file_read_write); +COSMS_CORE_TEST_DEFINE(file_open_large_file_read_write); + +COSMS_CORE_TEST_DEFINE(file_close); +COSMS_CORE_TEST_DEFINE(file_close_non_existing_file); + +COSMS_CORE_TEST_DEFINE(file_size_small_file); +COSMS_CORE_TEST_DEFINE(file_size_large); +COSMS_CORE_TEST_DEFINE(file_size_non_existing_file); + +COSMS_CORE_TEST_DEFINE(file_read_small_file); +COSMS_CORE_TEST_DEFINE(file_read_large_file); +COSMS_CORE_TEST_DEFINE(file_read_non_existing_file); +COSMS_CORE_TEST_DEFINE(file_read_full_non_existing_file); +COSMS_CORE_TEST_DEFINE(file_read_file_using_wrong_mode); +COSMS_CORE_TEST_DEFINE(file_read_full_file_using_wrong_mode); + +COSMS_CORE_TEST_DEFINE(file_write_small_file); +COSMS_CORE_TEST_DEFINE(file_write_large_file); +COSMS_CORE_TEST_DEFINE(file_write_append_small_file); +COSMS_CORE_TEST_DEFINE(file_write_append_large_file); +COSMS_CORE_TEST_DEFINE(file_write_using_wrong_mode); +COSMS_CORE_TEST_DEFINE(file_write_full_file_using_wrong_mode); + +COSMS_CORE_TEST_DEFINE(file_delete_small_file); +COSMS_CORE_TEST_DEFINE(file_delete_large_file); +COSMS_CORE_TEST_DEFINE(file_delete_non_existing_file); + +COSMS_CORE_TEST_EXPORT(file, + COSMS_CORE_TEST_EXPORT_TEST(file_open_small_file_read), + COSMS_CORE_TEST_EXPORT_TEST(file_open_large_file_read), + COSMS_CORE_TEST_EXPORT_TEST(file_open_non_existing_file_read), + COSMS_CORE_TEST_EXPORT_TEST(file_open_small_file_write), + COSMS_CORE_TEST_EXPORT_TEST(file_open_large_file_write), + COSMS_CORE_TEST_EXPORT_TEST(file_open_small_file_append), + COSMS_CORE_TEST_EXPORT_TEST(file_open_large_file_append), + COSMS_CORE_TEST_EXPORT_TEST(file_open_small_file_read_write), + COSMS_CORE_TEST_EXPORT_TEST(file_open_large_file_read_write), + + COSMS_CORE_TEST_EXPORT_TEST(file_close), + COSMS_CORE_TEST_EXPORT_TEST(file_close_non_existing_file), + + COSMS_CORE_TEST_EXPORT_TEST(file_size_small_file), + COSMS_CORE_TEST_EXPORT_TEST(file_size_large), + COSMS_CORE_TEST_EXPORT_TEST(file_size_non_existing_file), + + COSMS_CORE_TEST_EXPORT_TEST(file_read_small_file), + COSMS_CORE_TEST_EXPORT_TEST(file_read_large_file), + COSMS_CORE_TEST_EXPORT_TEST(file_read_non_existing_file), + COSMS_CORE_TEST_EXPORT_TEST(file_read_full_non_existing_file), + COSMS_CORE_TEST_EXPORT_TEST(file_read_file_using_wrong_mode), + COSMS_CORE_TEST_EXPORT_TEST(file_read_full_file_using_wrong_mode), + + COSMS_CORE_TEST_EXPORT_TEST(file_write_small_file), + COSMS_CORE_TEST_EXPORT_TEST(file_write_large_file), + COSMS_CORE_TEST_EXPORT_TEST(file_write_append_small_file), + COSMS_CORE_TEST_EXPORT_TEST(file_write_append_large_file), + COSMS_CORE_TEST_EXPORT_TEST(file_write_using_wrong_mode), + COSMS_CORE_TEST_EXPORT_TEST(file_write_full_file_using_wrong_mode), + + COSMS_CORE_TEST_EXPORT_TEST(file_delete_small_file), + COSMS_CORE_TEST_EXPORT_TEST(file_delete_large_file), + COSMS_CORE_TEST_EXPORT_TEST(file_delete_non_existing_file) +); + +#endif \ No newline at end of file diff --git a/tests/unit/unit.h b/tests/unit/unit.h new file mode 100644 index 0000000..fbd3a3e --- /dev/null +++ b/tests/unit/unit.h @@ -0,0 +1,19 @@ +/* + * Copyright (C) Tristan Franssen, . + * + * This software is licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License in the file LICENSE or at + * http://www.apache.org/licenses/LICENSE-2.0 + */ +#ifndef COSMS_UNIT +#define COSMS_UNIT + +#include "test.h" +#include "file.h" + +COSMS_CORE_TEST_START + COSMS_CORE_TEST_IMPORT(file) +COSMS_CORE_TEST_END + +#endif \ No newline at end of file