405 lines
16 KiB
C
405 lines
16 KiB
C
#include "unit/file.h"
|
|
|
|
#include <cosms-core/file.h>
|
|
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
COSMS_CORE_TEST_TEST(file_open,
|
|
COSMS_CORE_TEST_SUB_TEST(opening small file for reading,
|
|
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) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening large file for reading,
|
|
struct cosms_core_file file;
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening small file for writting,
|
|
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) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening large file for writting,
|
|
struct cosms_core_file file;
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_WRITE);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening small file for appending,
|
|
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) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening large file for appending,
|
|
struct cosms_core_file file;
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_APPEND);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening small file for reading and writting,
|
|
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) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening large file for reading and writting,
|
|
struct cosms_core_file file;
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
COSMS_CORE_TEST_TEST(file_close,
|
|
COSMS_CORE_TEST_SUB_TEST(closing 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) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(closing non-existing file,
|
|
struct cosms_core_file file;
|
|
file.native_file = 0;
|
|
|
|
CosmsCoreFileError error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_INVALID_FILE) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
)
|
|
)
|
|
|
|
COSMS_CORE_TEST_TEST(file_size,
|
|
COSMS_CORE_TEST_SUB_TEST(reading small file size,
|
|
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) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
unsigned long long size = 0;
|
|
error = cosms_core_file_get_size(&file, &size);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
if (size != 13) {
|
|
cosms_core_test_sub_test_error = "result does not match expected result";
|
|
}
|
|
}
|
|
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(reading large file size,
|
|
struct cosms_core_file file;
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
unsigned long long size = 0;
|
|
error = cosms_core_file_get_size(&file, &size);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
} else {
|
|
if (size != 5242880000ULL) {
|
|
cosms_core_test_sub_test_error = "result does not match expected result";
|
|
}
|
|
}
|
|
|
|
error = cosms_core_file_close(&file);
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(reading non-existing file size,
|
|
struct cosms_core_file file;
|
|
file.native_file = 0;
|
|
|
|
unsigned long long size = 0;
|
|
CosmsCoreFileError error = cosms_core_file_get_size(&file, &size);
|
|
if (error != COSMS_CORE_FILE_INVALID_FILE) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
)
|
|
)
|
|
|
|
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 (error == COSMS_CORE_FILE_OK) {
|
|
const char *expected_result = "Hello, World!";
|
|
if (file_size != strlen(expected_result) || strcmp(expected_result, file_buffer) != 0) {
|
|
cosms_core_test_sub_test_error = "result does not match expected result";
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
|
|
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 (error == COSMS_CORE_FILE_OK) {
|
|
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) != 0) {
|
|
cosms_core_test_sub_test_error = "result does not match expected result";
|
|
}
|
|
|
|
free(expected_result);
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
|
|
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 (error != COSMS_CORE_FILE_NOT_FOUND) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
|
|
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), COSMS_CORE_FILE_MODE_WRITE);
|
|
if (error == COSMS_CORE_FILE_OK) {
|
|
char *file_content = NULL;
|
|
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
|
|
if (error == COSMS_CORE_FILE_OK) {
|
|
if (strcmp(file_buffer, file_content) != 0) {
|
|
cosms_core_test_sub_test_error = "file content not what was written";
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
|
|
if (file_content != NULL) {
|
|
free(file_content);
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
)
|
|
|
|
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, COSMS_CORE_FILE_MODE_WRITE);
|
|
if (error == COSMS_CORE_FILE_OK) {
|
|
char *file_content = NULL;
|
|
error = cosms_core_file_read("tests/data/large-write-file.txt", &file_content, NULL);
|
|
if (error == COSMS_CORE_FILE_OK) {
|
|
if (strcmp(large_file_buffer, file_content) != 0) {
|
|
cosms_core_test_sub_test_error = "file content not what was written";
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
|
|
if (file_content != NULL) {
|
|
free(file_content);
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
|
|
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), COSMS_CORE_FILE_MODE_APPEND);
|
|
if (error == COSMS_CORE_FILE_OK) {
|
|
char *file_content = NULL;
|
|
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
|
|
if (error == COSMS_CORE_FILE_OK) {
|
|
const char *expected_result = "Hello, World!Hello, World!";
|
|
if (strcmp(expected_result, file_content) != 0) {
|
|
cosms_core_test_sub_test_error = "file content not what was written";
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
|
|
if (file_content != NULL) {
|
|
free(file_content);
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
)
|
|
|
|
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), COSMS_CORE_FILE_MODE_APPEND);
|
|
if (error == COSMS_CORE_FILE_OK) {
|
|
char *file_content = NULL;
|
|
error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL);
|
|
if (error == COSMS_CORE_FILE_OK) {
|
|
if (strcmp(file_buffer, file_content) != 0) {
|
|
cosms_core_test_sub_test_error = "file content not what was written";
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
|
|
if (file_content != NULL) {
|
|
free(file_content);
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
)
|
|
)
|
|
|
|
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 (error == COSMS_CORE_FILE_OK) {
|
|
char *file_content = NULL;
|
|
error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL);
|
|
if (error != COSMS_CORE_FILE_NOT_FOUND) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
|
|
if (file_content != NULL) {
|
|
free(file_content);
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(deleting large file,
|
|
CosmsCoreFileError error = cosms_core_file_delete("tests/data/append-write-file.txt");
|
|
if (error == COSMS_CORE_FILE_OK) {
|
|
char *file_content = NULL;
|
|
error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL);
|
|
if (error != COSMS_CORE_FILE_NOT_FOUND) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
|
|
if (file_content != NULL) {
|
|
free(file_content);
|
|
}
|
|
} else {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
)
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(deleting non existing file,
|
|
CosmsCoreFileError error = cosms_core_file_delete("non-existing-file.cosms");
|
|
if (error != COSMS_CORE_FILE_NOT_FOUND) {
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
}
|
|
)
|
|
)
|