Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tronkko committed Sep 3, 2023
1 parent ab35ddf commit 9c4650d
Show file tree
Hide file tree
Showing 12 changed files with 926 additions and 515 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@
/*.filters
/*.vcxproj
/*.dir

# Some filesystems do not support symlinks, therefore
# it is a bad idea to include symlinks in a git source tree
/tests/1/link_*
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ if(DIRENT_BUILD_TESTS)
add_executable(${TEST_NAME} EXCLUDE_FROM_ALL ${ARGN})
add_test(NAME ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND $<TARGET_FILE:${TEST_NAME}>)
add_dependencies(check ${TEST_NAME})
set_property(TEST ${TEST_NAME} PROPERTY SKIP_RETURN_CODE 77)
endfunction(add_test_executable)

add_test_executable(t-compile tests/t-compile.c)
Expand Down
46 changes: 26 additions & 20 deletions include/dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,12 @@
# define S_IFBLK 0
#endif

/* Link
* S_IFLNK is not defined in Windows's `stat.h`, so we define it here.
* In Windows's `stat.h`, file type macros (S_IFDIR, S_IFREG...) are defined
* bitmasks, except that they are NOT on Linux. So long as S_* & ~S_IFMT == 0,
* as is the case in Linux, the S_IS*(mode) macros works fine. */
/*
* Symbolic link. Be ware that S_IFLNK value and S_ISLNK() macro are only
* usable with dirent - they do not work with stat() function call!
*/
#if !defined(S_IFLNK)
# define S_IFLNK (S_IFMT & 0XCCCCCCCC)
# define S_IFLNK (_S_IFDIR | _S_IFREG)
#endif

/* Socket */
Expand All @@ -128,6 +127,11 @@
# define S_IXUSR 0
#endif

/* User full permissions */
#if !defined(S_IRWXU)
# define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
#endif

/* Read group permission */
#if !defined(S_IRGRP)
# define S_IRGRP 0
Expand All @@ -143,6 +147,11 @@
# define S_IXGRP 0
#endif

/* Group full permissions */
#if !defined(S_IRWXG)
# define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
#endif

/* Read others permission */
#if !defined(S_IROTH)
# define S_IROTH 0
Expand All @@ -158,6 +167,11 @@
# define S_IXOTH 0
#endif

/* Other full permissions */
#if !defined(S_IRWXO)
# define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
#endif

/* Maximum length of file name */
#if !defined(PATH_MAX)
# define PATH_MAX MAX_PATH
Expand All @@ -184,10 +198,10 @@
#define DTTOIF(type) (type)

/*
* File type macros. Note that block devices, sockets and links cannot be
* distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
* only defined for compatibility. These macros should always return false
* on Windows.
* File type macros. Note that block devices and sockets cannot be
* distinguished on Windows, and the macros S_ISBLK and S_ISSOCK are only
* defined for compatibility. These macros should always return false on
* Windows.
*/
#if !defined(S_ISFIFO)
# define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
Expand Down Expand Up @@ -531,12 +545,8 @@ _wreaddir_r(
DWORD attr = datap->dwFileAttributes;
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0)
entry->d_type = DT_CHR;
#ifdef FILE_ATTRIBUTE_REPARSE_POINT
/* A Windows link to directory is both symlink (reparse point) and
* directory. Symlink takes precedence, just as Linux does. */
else if ((attr & FILE_ATTRIBUTE_REPARSE_POINT))
else if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
entry->d_type = DT_LNK;
#endif
else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
entry->d_type = DT_DIR;
else
Expand Down Expand Up @@ -797,12 +807,8 @@ readdir_r(
DWORD attr = datap->dwFileAttributes;
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0)
entry->d_type = DT_CHR;
#ifdef FILE_ATTRIBUTE_REPARSE_POINT
/* A Windows link to directory is both symlink (reparse point) and
* directory. Symlink takes precedence, just as Linux does. */
else if ((attr & FILE_ATTRIBUTE_REPARSE_POINT))
else if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
entry->d_type = DT_LNK;
#endif
else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
entry->d_type = DT_DIR;
else
Expand Down
37 changes: 32 additions & 5 deletions tests/t-compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@
#include <stdlib.h>
#include <string.h>

static void test_properties(void);
static void test_length(void);
static void initialize(void);
static void cleanup(void);

int
main(int argc, char *argv[])
main(void)
{
struct dirent *dirp = NULL;
initialize();

(void) argc;
(void) argv;
test_properties();
test_length();

cleanup();
return EXIT_SUCCESS;
}

static void
test_properties(void)
{
#ifdef _DIRENT_HAVE_D_TYPE
printf("Has d_type\n");
#endif
Expand All @@ -38,9 +50,24 @@ main(int argc, char *argv[])
#ifdef _D_ALLOC_NAMLEN
printf("Has _D_ALLOC_NAMLEN\n");
#endif
}

static void
test_length(void)
{
struct dirent *dirp = NULL;
printf("Length of d_name with terminator: %d\n",
(int) sizeof(dirp->d_name));
}

static void
initialize(void)
{
/*NOP*/;
}

static void
cleanup(void)
{
printf("OK\n");
return EXIT_SUCCESS;
}
29 changes: 19 additions & 10 deletions tests/t-cplusplus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ using namespace std;
static int only_readme(const struct dirent *entry);
static void test_retrieval(void);
static void test_scan(void);
static void initialize(void);
static void cleanup(void);

int
main(int argc, char *argv[])
main(void)
{
(void) argc;
(void) argv;
initialize();

test_retrieval();
test_scan();

cout << "OK" << endl;
cleanup();
return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -106,12 +107,8 @@ test_retrieval(void)
assert(_D_ALLOC_NAMLEN(ent) > 3);
#endif
found += 8;
#ifdef _DIRENT_HAVE_D_TYPE
} else if (ent->d_type != DT_LNK || ent->d_name[0] != 'l') {
#else
} else if (ent->d_name[0] != 'l') {
#endif
/* Other file. Symlinks created by t-symlink are ignored. */
} else {
/* Other file */
cerr << "Unexpected file " << ent->d_name << endl;
abort();
}
Expand Down Expand Up @@ -157,3 +154,15 @@ only_readme(const struct dirent *entry)

return pass;
}

static void
initialize(void)
{
/*NOP*/;
}

static void
cleanup(void)
{
cout << "OK" << endl;
}
90 changes: 37 additions & 53 deletions tests/t-dirent.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ static void test_chdir(void);
static void test_filename(void);
static void test_readdir(void);
static void test_wreaddir(void);
static void initialize(void);
static void cleanup(void);

int
main(int argc, char *argv[])
main(void)
{
(void) argc;
(void) argv;
initialize();

/* Execute tests */
test_macros();
test_retrieval();
test_nonexistent();
Expand All @@ -51,7 +51,7 @@ main(int argc, char *argv[])
test_readdir();
test_wreaddir();

printf("OK\n");
cleanup();
return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -150,12 +150,8 @@ test_retrieval(void)
assert(_D_ALLOC_NAMLEN(ent) > 3);
#endif
found += 8;
#ifdef _DIRENT_HAVE_D_TYPE
} else if (ent->d_type != DT_LNK || ent->d_name[0] != 'l') {
#else
} else if (ent->d_name[0] != 'l') {
#endif
/* Other file. Symlinks created by t-symlink are ignored. */
} else {
/* Other file */
fprintf(stderr, "Unexpected file %s\n", ent->d_name);
abort();
}
Expand Down Expand Up @@ -219,12 +215,8 @@ test_rewind(void)
} else if (strcmp(ent->d_name, "dir") == 0) {
/* Just a directory */
found += 8;
#ifdef _DIRENT_HAVE_D_TYPE
} else if (ent->d_type != DT_LNK || ent->d_name[0] != 'l') {
#else
} else if (ent->d_name[0] != 'l') {
#endif
/* Other file. Symlinks created by t-symlink are ignored. */
} else {
/* Other file */
fprintf(stderr, "Unexpected file %s\n", ent->d_name);
abort();
}
Expand Down Expand Up @@ -252,12 +244,8 @@ test_rewind(void)
} else if (strcmp(ent->d_name, "dir") == 0) {
/* Just a directory */
found += 8;
#ifdef _DIRENT_HAVE_D_TYPE
} else if (ent->d_type != DT_LNK || ent->d_name[0] != 'l') {
#else
} else if (ent->d_name[0] != 'l') {
#endif
/* Other file. Symlinks created by t-symlink are ignored. */
} else {
/* Other file */
fprintf(stderr, "Unexpected file %s\n", ent->d_name);
abort();
}
Expand Down Expand Up @@ -294,12 +282,8 @@ test_chdir(void)
} else if (strcmp(ent->d_name, "dir") == 0) {
/* Just a directory */
found += 8;
#ifdef _DIRENT_HAVE_D_TYPE
} else if (ent->d_type != DT_LNK || ent->d_name[0] != 'l') {
#else
} else if (ent->d_name[0] != 'l') {
#endif
/* Other file. Symlinks created by t-symlink are ignored. */
} else {
/* Other file */
fprintf(stderr, "Unexpected file %s\n", ent->d_name);
abort();
}
Expand Down Expand Up @@ -332,12 +316,8 @@ test_chdir(void)
} else if (strcmp(ent->d_name, "dir") == 0) {
/* Just a directory */
found += 8;
#ifdef _DIRENT_HAVE_D_TYPE
} else if (ent->d_type != DT_LNK || ent->d_name[0] != 'l') {
#else
} else if (ent->d_name[0] != 'l') {
#endif
/* Other file. Symlinks created by t-symlink are ignored. */
} else {
/* Other file */
fprintf(stderr, "Unexpected file %s\n", ent->d_name);
abort();
}
Expand Down Expand Up @@ -436,9 +416,9 @@ test_readdir(void)
size_t n = 0;
while (n < 10 && readdir_r(dir, &ent[n], &entry) == /*OK*/0 && entry != NULL)
n++;
/* Make sure that we got all the files from directory,
* with or without the two symlinks created by `t-symlink` */
assert(n >= 4 && n <= 6);

/* Make sure that we got all the files from directory */
assert(n == 4);

/* Check entries in memory */
int found = 0;
Expand Down Expand Up @@ -506,12 +486,8 @@ test_readdir(void)
assert(_D_ALLOC_NAMLEN(entry) > 3);
#endif
found += 8;
#ifdef _DIRENT_HAVE_D_TYPE
} else if (entry->d_type != DT_LNK || entry->d_name[0] != 'l') {
#else
} else if (entry->d_name[0] != 'l') {
#endif
/* Other file. Symlinks created by t-symlink are ignored. */
} else {
/* Other file */
fprintf(stderr, "Unexpected file %s\n", entry->d_name);
abort();
}
Expand Down Expand Up @@ -543,9 +519,9 @@ test_wreaddir(void)
size_t n = 0;
while (n < 10 && _wreaddir_r(dir, &ent[n], &entry) == /*OK*/0 && entry != NULL)
n++;
/* Make sure that we got all the files from directory,
* with or without the two symlinks created by `t-symlink` */
assert(n >= 4 && n <= 6);

/* Make sure that we got all the files from directory */
assert(n == 4);

/* Check entries in memory */
int found = 0;
Expand Down Expand Up @@ -613,12 +589,8 @@ test_wreaddir(void)
assert(_D_ALLOC_NAMLEN(entry) > 3);
#endif
found += 8;
#ifdef _DIRENT_HAVE_D_TYPE
} else if (entry->d_type != DT_LNK || entry->d_name[0] != 'l') {
#else
} else if (entry->d_name[0] != 'l') {
#endif
/* Other file. Symlinks created by t-symlink are ignored. */
} else {
/* Other file */
fprintf(stderr, "Unexpected file\n");
abort();
}
Expand All @@ -630,3 +602,15 @@ test_wreaddir(void)
_wclosedir(dir);
#endif
}

static void
initialize(void)
{
/*NOP*/;
}

static void
cleanup(void)
{
printf("OK\n");
}
Loading

0 comments on commit 9c4650d

Please sign in to comment.