Compare commits

...
Sign in to create a new pull request.

13 commits

11 changed files with 584 additions and 13 deletions

4
.gitignore vendored
View file

@ -65,5 +65,7 @@ compile_commands.json
CTestTestfile.cmake CTestTestfile.cmake
_deps _deps
CMakeUserPresets.json CMakeUserPresets.json
build build
# clangd
.cache/

View file

@ -11,10 +11,11 @@ target_include_directories(cosms-core PUBLIC ${PROJECT_SOURCE_DIR}/include)
if (MSVC) if (MSVC)
target_compile_options(cosms-core PRIVATE /W4) target_compile_options(cosms-core PRIVATE /W4)
else() else()
target_compile_options(cosms-core PRIVATE -wall -Wextra -pedantic) target_compile_options(cosms-core PRIVATE -Wall -Wextra -pedantic)
endif() 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_include_directories(cosms-core-test PRIVATE ${PROJECT_SOURCE_DIR}/tests)
target_link_libraries(cosms-core-test cosms-core) target_link_libraries(cosms-core-test cosms-core)

View file

@ -1,7 +1,34 @@
/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* 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 #ifndef COSMS_CORE_FILE
#define COSMS_CORE_FILE #define COSMS_CORE_FILE
void cosms_core_file_read(char *path); #include <stdbool.h>
void cosms_core_file_write(char *path);
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,
} CosmsCoreFileError;
CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size);
CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override);
CosmsCoreFileError cosms_core_file_delete(const char *path);
const char *cosms_core_file_error_string(CosmsCoreFileError error);
#endif #endif

View file

@ -1,11 +1,318 @@
#include <cosms-core/file.h> /*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* 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 <stdio.h> #include <stdlib.h>
#include <stdint.h>
#include <limits.h>
void cosms_core_file_read(char *path) { #if defined(__GNUC__)
printf("Hello, World!"); #define _FILE_OFFSET_BITS_ 64
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#elif defined(_WIN32)
#include <windows.h>
#endif
CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size) {
#if defined(__GNUC__)
int file = open(path, O_RDONLY);
if (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;
}
}
struct stat file_stat;
if (stat(path, &file_stat) != 0) {
close(file);
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
if (size != NULL) {
*size = file_stat.st_size;
}
*content = (char*)malloc((file_stat.st_size + 1) * sizeof(char));
if (content == NULL) {
close(file);
return COSMS_CORE_FILE_FAILED_TO_ALLOCATE;
}
unsigned long long remaining_bytes = file_stat.st_size;
while (remaining_bytes != 0) {
unsigned int bytes_to_read;
if (remaining_bytes > UINT_MAX) {
bytes_to_read = UINT_MAX;
} else {
bytes_to_read = (unsigned int)remaining_bytes;
}
int read_bytes = read(file, (*content) + (file_stat.st_size - remaining_bytes), bytes_to_read);
if (read_bytes == -1) {
free(*content);
close(file);
return COSMS_CORE_FILE_FAILED_TO_READ;
}
remaining_bytes -= read_bytes;
}
(*content)[file_stat.st_size] = '\0';
close(file);
#elif defined(_WIN32)
HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (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;
}
}
DWORD high_size;
DWORD low_size = GetFileSize(file, &high_size);
if (low_size == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
CloseHandle(file);
return COSMS_CORE_FILE_COULD_NOT_READ_SIZE;
}
unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size;
if (size != NULL) {
*size = file_size;
}
*content = (char*)malloc((file_size + 1) * sizeof(char));
if (content == NULL) {
CloseHandle(file);
return COSMS_CORE_FILE_FAILED_TO_ALLOCATE;
}
unsigned long long remaining_bytes = file_size;
while (remaining_bytes != 0) {
DWORD bytes_to_read, read_bytes;
if (remaining_bytes > UINT_MAX) {
bytes_to_read = UINT_MAX;
} else {
bytes_to_read = (DWORD)remaining_bytes;
}
if (!ReadFile(file, (*content) + (file_size - remaining_bytes), bytes_to_read, &read_bytes, NULL)) {
free(*content);
CloseHandle(file);
return COSMS_CORE_FILE_FAILED_TO_READ;
}
remaining_bytes -= read_bytes;
}
(*content)[file_size] = '\0';
CloseHandle(file);
#endif
return COSMS_CORE_FILE_OK;
} }
void cosms_core_file_write(char *path) { CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override) {
printf("Hello, World"); #if defined(__GNUC__)
int file;
if (override) {
file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
} else {
file = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
}
if (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;
}
}
unsigned long long remaining_bytes = size;
while (remaining_bytes != 0) {
unsigned int bytes_to_write;
if (remaining_bytes > UINT_MAX) {
bytes_to_write = UINT_MAX;
} else {
bytes_to_write = (unsigned int)remaining_bytes;
}
int written_bytes = write(file, content + (size - remaining_bytes), bytes_to_write);
if (written_bytes == -1) {
close(file);
return COSMS_CORE_FILE_FAILED_TO_WRITE;
}
remaining_bytes -= written_bytes;
}
close(file);
#elif defined(_WIN32)
HANDLE file;
if (override) {
file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
} else {
file = CreateFile(path, FILE_APPEND_DATA, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
}
if (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;
}
}
unsigned long long remaining_bytes = size;
while (remaining_bytes != 0) {
DWORD bytes_to_write, written_bytes;
if (remaining_bytes > UINT_MAX) {
bytes_to_write = UINT_MAX;
} else {
bytes_to_write = (DWORD)remaining_bytes;
}
if (!WriteFile(file, content + (size - remaining_bytes), bytes_to_write, &written_bytes, NULL)) {
CloseHandle(file);
return COSMS_CORE_FILE_FAILED_TO_WRITE;
}
remaining_bytes -= written_bytes;
}
CloseHandle(file);
#endif
return COSMS_CORE_FILE_OK;
}
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;
}
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";
default:
return NULL;
}
} }

BIN
tests/data/large-file.txt Normal file

Binary file not shown.

View file

@ -0,0 +1 @@
Hello, World!

View file

@ -1,6 +1,9 @@
#include <cosms-core/file.h> #include "unit/file.h"
#include "unit/unit.h"
int main(void) { int main(void) {
cosms_core_file_read("test.txt"); cosms_core_file_read_test();
cosms_core_file_write_test();
cosms_core_file_delete_test();
return 0; return 0;
} }

22
tests/test.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef COSMS_TEST
#define COSMS_TEST
#include <stdio.h>
#include <stdbool.h>
#define COSMS_CORE_TEST_HEADER(test_module_name) printf("----------------------------------------\n%s\n----------------------------------------\n", test_module_name);
#define COSMS_CORE_TEST_START(test_name) printf("[*] running test %s...\n", test_name)
#define COSMS_CORE_TEST_SUCCESS(test_name) printf("[+] test %s completed successfully!\n", test_name);
#define COSMS_CORE_TEST_FAIL(test_name, error) printf("[-] test %s failed with error: %s\n", test_name, error)
static inline bool cosms_core_test_assert(const char *test_name, bool expression, const char *error_message) {
if (!expression) {
printf("[-] test %s failed with error: %s\n", test_name, error_message);
return false;
}
return true;
}
#endif

192
tests/unit/file.c Normal file
View file

@ -0,0 +1,192 @@
#include "test.h"
#include "unit/file.h"
#include <cosms-core/file.h>
#include <string.h>
#include <stdlib.h>
void cosms_core_file_read_test(void) {
COSMS_CORE_TEST_HEADER("file read");
const char *current_test_name = "reading small file";
COSMS_CORE_TEST_START(current_test_name);
unsigned long long file_size;
char *file_buffer;
CosmsCoreFileError error = cosms_core_file_read("tests/data/small-file.txt", &file_buffer, &file_size);
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
const char *expected_result = "Hello, World!";
if (cosms_core_test_assert(current_test_name, file_size == strlen(expected_result) && strcmp(expected_result, file_buffer) == 0, "result does not match expected result")) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
}
free(file_buffer);
current_test_name = "reading large file";
COSMS_CORE_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);
if (cosms_core_test_assert(current_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(current_test_name, file_size == expected_size && strcmp(expected_result, file_buffer) == 0, "result does not match expected result")) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
free(expected_result);
}
free(file_buffer);
current_test_name = "reading non existing file";
COSMS_CORE_TEST_START(current_test_name);
file_size = 0;
file_buffer = NULL;
error = cosms_core_file_read("non-existing-file.cosms", &file_buffer, &file_size);
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
free(file_buffer);
}
void cosms_core_file_write_test(void) {
COSMS_CORE_TEST_HEADER("file write");
const char *current_test_name = "writting small file";
COSMS_CORE_TEST_START(current_test_name);
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(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
char *file_content;
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
if (cosms_core_test_assert(current_test_name, strcmp(file_buffer, file_content) == 0, "file content not what was written")) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
}
free(file_content);
}
current_test_name = "writting big file";
COSMS_CORE_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 (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
char *file_content;
error = cosms_core_file_read("tests/data/large-write-file.txt", &file_content, NULL);
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
if (cosms_core_test_assert(current_test_name, strcmp(large_file_buffer, file_content) == 0, "file content not what was written")) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
}
free(file_content);
}
free(large_file_buffer);
current_test_name = "appending file";
COSMS_CORE_TEST_START(current_test_name);
error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), false);
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
char *file_content;
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
if (cosms_core_test_assert(current_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(current_test_name, strcmp(expected_result, file_content) == 0, "file content not what was written")) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
}
free(file_content);
}
current_test_name = "appending to non existing file";
COSMS_CORE_TEST_START(current_test_name);
error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer), false);
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
char *file_content;
error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL);
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) {
if (cosms_core_test_assert(current_test_name, strcmp(file_buffer, file_content) == 0, "file content not what was written")) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
}
free(file_content);
}
}
void cosms_core_file_delete_test(void) {
COSMS_CORE_TEST_HEADER("file delete");
const char *current_test_name = "deleting small file";
COSMS_CORE_TEST_START(current_test_name);
CosmsCoreFileError error = cosms_core_file_delete("tests/data/small-write-file.txt");
if (cosms_core_test_assert(current_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(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
if (file_content != NULL) {
free(file_content);
}
}
current_test_name = "deleting large file";
COSMS_CORE_TEST_START(current_test_name);
error = cosms_core_file_delete("tests/data/append-write-file.txt");
if (cosms_core_test_assert(current_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(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
if (file_content != NULL) {
free(file_content);
}
}
current_test_name = "deleting non existing file";
COSMS_CORE_TEST_START(current_test_name);
error = cosms_core_file_delete("non-existing-file.cosms");
if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) {
COSMS_CORE_TEST_SUCCESS(current_test_name);
}
}

10
tests/unit/file.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef COSMS_CORE_FILE_TEST
#define COSMS_CORE_FILE_TEST
#include <stdbool.h>
void cosms_core_file_read_test();
void cosms_core_file_write_test();
void cosms_core_file_delete_test();
#endif

6
tests/unit/unit.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef COSMS_UNIT
#define COSMS_UNIT
#include "file.h"
#endif