Compare commits

...

4 Commits

Author SHA1 Message Date
EyitopeIO d5b1d72a46 Organize and cleanup 2024-03-09 20:57:10 +00:00
EyitopeIO 059d910245 Merge branch 'cleanup' 2024-03-09 20:23:05 +00:00
EyitopeIO 1643ec80a4 Merge branch 'unittests' 2024-03-09 20:22:56 +00:00
EyitopeIO 8ab2752d17 Better organisation 2024-03-09 20:20:21 +00:00
2 changed files with 22 additions and 23 deletions

View File

@ -100,6 +100,7 @@ Apple's command-line build tools are usually installed as part of setting up
Homebrew. HTTPDirFS will be installed in ``/usr/local``. Homebrew. HTTPDirFS will be installed in ``/usr/local``.
## Running Tests ## Running Tests
GoogleTest is the utilized unit test framework. Google recommends you live GoogleTest is the utilized unit test framework. Google recommends you live
at head, so it is also recommended that you compile and install the latest from at head, so it is also recommended that you compile and install the latest from
[source](https://github.com/google/googletest.git), since what you install from [source](https://github.com/google/googletest.git), since what you install from
@ -107,9 +108,11 @@ a repository may be outdated. Below are the package names if installing from a
repository repository
### Ubuntu and Debian ### Ubuntu and Debian
libgtest-dev libgtest-dev
### FreeBSD and macOS ### FreeBSD and macOS
googletest googletest
Build and run tests Build and run tests

View File

@ -7,57 +7,53 @@ extern "C" {
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace { namespace {
#define TEST_FILE_NAME_LEN 6 const char test_file_name[] = "id_rsa";
const char *test_file_name = "id_rsa";
#define TEST_FOLDER_NAME_LEN 6
const char test_folder_name[] = "/abcdefg";
const char *path_root = "/";
const char *path_sample1 = "/www";
const char *path_sample2 = "/www/folder1/";
const char *path_sample3 = "/www/folder1/folder2";
TEST(PathAppendTest, PathLengthLessThanMaxPathLen) { TEST(PathAppendTest, PathLengthLessThanMaxPathLen) {
const char *path_root = "/";
const char *path_sample1 = "/www";
const char *path_sample2 = "/www/folder1/";
const char *path_sample3 = "/www/folder1/folder2";
char *path = path_append(path_root, test_file_name); char *path = path_append(path_root, test_file_name);
ASSERT_STREQ("/id_rsa", path); ASSERT_STREQ("/id_rsa", path);
free(path); FREE(path);
path = path_append(path_sample1, test_file_name); path = path_append(path_sample1, test_file_name);
ASSERT_STREQ("/www/id_rsa", path); ASSERT_STREQ("/www/id_rsa", path);
free(path); FREE(path);
path = path_append(path_sample2, test_file_name); path = path_append(path_sample2, test_file_name);
ASSERT_STREQ("/www/folder1/id_rsa", path); ASSERT_STREQ("/www/folder1/id_rsa", path);
free(path); FREE(path);
path = path_append(path_sample3, test_file_name); path = path_append(path_sample3, test_file_name);
ASSERT_STREQ("/www/folder1/folder2/id_rsa", path); ASSERT_STREQ("/www/folder1/folder2/id_rsa", path);
free(path); FREE(path);
} }
TEST(PathAppendTest, PathLengthGreaterThanMaxPathLen) { TEST(PathAppendTest, PathLengthGreaterThanMaxPathLen) {
#define PATH_LEN_4098 4098 const char test_folder_name[] = "/abcdefg";
const int path_len_4098 = 4098;
char very_long_path[PATH_LEN_4098] = { 0 }; const int test_folder_name_len = strlen(test_folder_name);
char very_long_path[path_len_4098] = { 0 };
char *p = very_long_path; char *p = very_long_path;
/* /*
* MAX_PATH_LEN is a perfect multiple of the length of test_folder_name, * MAX_PATH_LEN is an integer multiple of test_folder_name_len,
* so it would fit perfectly in the 4096 bytes of very_long_path. * so it would fit perfectly in the 4096 bytes of very_long_path.
*/ */
for (int i = 0; i < PATH_LEN_4098; i += TEST_FOLDER_NAME_LEN) { for (int i = 0; i < path_len_4098; i += test_folder_name_len) {
memcpy(p, test_folder_name, TEST_FOLDER_NAME_LEN); memcpy(p, test_folder_name, test_folder_name_len);
p += TEST_FOLDER_NAME_LEN; p += test_folder_name_len;
} }
char *path = path_append(very_long_path, test_file_name); char *path = path_append(very_long_path, test_file_name);
ASSERT_NE(nullptr, path); ASSERT_NE(nullptr, path);
ASSERT_EQ(path[MAX_PATH_LEN - 1], 'c'); ASSERT_EQ(path[MAX_PATH_LEN - 1], 'g');
ASSERT_EQ(path[MAX_PATH_LEN + 0], '/'); ASSERT_EQ(path[MAX_PATH_LEN + 0], '/');
ASSERT_EQ(path[MAX_PATH_LEN + 1], 'i'); ASSERT_EQ(path[MAX_PATH_LEN + 1], 'i');
ASSERT_EQ(path[MAX_PATH_LEN + 6], 'a'); ASSERT_EQ(path[MAX_PATH_LEN + 6], 'a');
free(path); FREE(path);
} }
} // namespace } // namespace