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 Jan 26, 2024
1 parent f3ac205 commit ad63326
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 353 deletions.
95 changes: 88 additions & 7 deletions libc/dirent/closedir.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,27 @@
* %LICENSE%
*/


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>

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

#define MAIN_DIR "test_closedir"


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 All @@ -53,7 +51,7 @@ TEST(dirent_closedir, closing_empty_dir)
DIR *dp = opendir(MAIN_DIR "/dir1");

TEST_ASSERT_NOT_NULL(dp);
TEST_ASSERT_EQUAL(0, closedir(dp));
TEST_ASSERT_EQUAL_INT(0, closedir(dp));
}


Expand All @@ -66,8 +64,91 @@ 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);
}
}

TEST_ASSERT_EQUAL_INT(0, 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 ad63326

Please sign in to comment.