Skip to content

Commit

Permalink
Libc: add dirent tests
Browse files Browse the repository at this point in the history
JIRA: CI-359
  • Loading branch information
KArkadiusz committed Nov 28, 2023
1 parent 4b9a002 commit ecf1ffa
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 317 deletions.
94 changes: 92 additions & 2 deletions libc/dirent/closedir.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

#include <unity_fixture.h>
#include "common.h"
#include "dirent_helper_functions.h"

#define MAIN_DIR "test_closedir"

Expand All @@ -36,8 +38,9 @@ TEST_GROUP(dirent_closedir);
TEST_SETUP(dirent_closedir)
{
errno = 0;
TEST_ASSERT_TRUE(mkdir(MAIN_DIR, 0777) != -1 || errno == EEXIST);
TEST_ASSERT_TRUE(mkdir(MAIN_DIR "/dir1", 0777) != -1 || errno == EEXIST);
TEST_MKDIR_ASSERTED(MAIN_DIR, S_IRWXU);
errno = 0;
TEST_MKDIR_ASSERTED(MAIN_DIR "/dir1", S_IRUSR);
}


Expand Down Expand Up @@ -66,8 +69,95 @@ TEST(dirent_closedir, closing_non_empty_dir)
}


TEST(dirent_closedir, preserving_content_after_closedir)
{
char dirNames[7][10];
ino_t inodes[7];
struct dirent *info;
DIR *dp1;
int result = 0;
errno = 0;


TEST_MKDIR_ASSERTED(MAIN_DIR "/test_preserve", S_IRWXU);
TEST_MKDIR_ASSERTED(MAIN_DIR "/test_preserve/B", S_IRUSR);
TEST_MKDIR_ASSERTED(MAIN_DIR "/test_preserve/CC", S_IRUSR);
TEST_MKDIR_ASSERTED(MAIN_DIR "/test_preserve/DDDD", S_IRUSR);
TEST_MKDIR_ASSERTED(MAIN_DIR "/test_preserve/EEEEEE", S_IRUSR);


dp1 = opendir(MAIN_DIR "/test_preserve");
TEST_ASSERT_NOT_NULL(dp1);

/*
*Create an array with names of dirs.
*Indexes will be used to associate name of each directory with a bit
*/
{
int i = 0;
while ((info = readdir(dp1)) != NULL) {
inodes[i] = info->d_ino;
strcpy(dirNames[i++], info->d_name);
}
}


closedir(dp1);
DIR *dp2 = opendir(MAIN_DIR "/test_preserve");
TEST_ASSERT_NOT_NULL(dp2);
rewinddir(dp2);

/*
* Map each directory to a bit.
* In case of fail set most left bit to 1.
* Assert every bit is high except the first two.
*/

/* Go through each entry */

while ((info = readdir(dp2)) != NULL) {

int found = 0;
char name[10];
strcpy(name, info->d_name);

/* determine the index of given name */
for (int i = 0; i < 7; ++i) {

if (!strcmp(name, dirNames[i])) {

TEST_ASSERT_EQUAL_INT64(inodes[i], info->d_ino);

/* Set the index bit of found name */
result |= 1 << i;

found = 1;
}
}

/* Name found, time to search for another name */
if (found)
continue;

TEST_FAIL();
}

/* result variable should be 0b0011 1111, which is 63, or 3f */
TEST_ASSERT_EQUAL_INT(0x3f, result);

closedir(dp2);

rmdir(MAIN_DIR "/test_preserve/B");
rmdir(MAIN_DIR "/test_preserve/CC");
rmdir(MAIN_DIR "/test_preserve/DDDD");
rmdir(MAIN_DIR "/test_preserve/EEEEEE");
rmdir(MAIN_DIR "/test_preserve");
}


TEST_GROUP_RUNNER(dirent_closedir)
{
RUN_TEST_CASE(dirent_closedir, closing_empty_dir);
RUN_TEST_CASE(dirent_closedir, closing_non_empty_dir);
RUN_TEST_CASE(dirent_closedir, preserving_content_after_closedir);
}
81 changes: 0 additions & 81 deletions libc/dirent/dirent_helper_functions.c

This file was deleted.

17 changes: 8 additions & 9 deletions libc/dirent/dirent_helper_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
#include <sys/stat.h>
#include <string.h>

#define INO_T_TEST_MAX_DIRS 10

#define TEST_MKDIR_ASSERTED(path, mode) TEST_ASSERT_TRUE_MESSAGE(mkdir(path, mode) != -1 || errno == EEXIST, strerror(errno))

void test_mkdir_asserted(char *path, mode_t mode);
#define TEST_OPENDIR_ASSERTED(path) \
({ \
DIR *dp = opendir(path); \
TEST_ASSERT_NOT_NULL(dp); \
dp; \
})

int test_create_directories(int num_of_dirs);

int d_ino_in(ino_t arg, ino_t *arr);

DIR *test_opendir_asserted(const char *path);


#endif
#endif
Loading

0 comments on commit ecf1ffa

Please sign in to comment.