summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorFabiano Fidêncio <fidencio@redhat.com>2017-08-02 14:00:03 +0200
committerJakub Hrozek <jhrozek@redhat.com>2017-08-28 20:40:53 +0200
commit6f466e0a3d950d21bd750ef53cb93b75dc023f9e (patch)
treef5329bc033702fc13a185901cb8631e842cd9f60 /src/tests
parent5b93634c7f0e34f69b4cf8fb9b2e77b9179024a7 (diff)
downloadsssd-6f466e0a3d950d21bd750ef53cb93b75dc023f9e.tar.gz
sssd-6f466e0a3d950d21bd750ef53cb93b75dc023f9e.tar.xz
sssd-6f466e0a3d950d21bd750ef53cb93b75dc023f9e.zip
UTIL: Add sss_create_dir()
The newly added function helps us to create a new dir avoiding a possible TUCTOU issue. It's going to be used by the new session provider code. A simple test for this new function has also been provided. Related: https://pagure.io/SSSD/sssd/issue/2995 Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com> Reviewed-by: Pavel Březina <pbrezina@redhat.com> Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/files-tests.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/tests/files-tests.c b/src/tests/files-tests.c
index 9feb9274a..1ccf404b9 100644
--- a/src/tests/files-tests.c
+++ b/src/tests/files-tests.c
@@ -378,6 +378,42 @@ START_TEST(test_copy_node)
}
END_TEST
+START_TEST(test_create_dir)
+{
+ int ret;
+ char origpath[PATH_MAX+1];
+ char *new_dir;
+ struct stat info;
+
+ errno = 0;
+
+ fail_unless(getcwd(origpath, PATH_MAX) == origpath, "Cannot getcwd\n");
+ fail_unless(errno == 0, "Cannot getcwd\n");
+
+ /* create a dir */
+ ret = sss_create_dir(dir_path, "testdir", S_IRUSR | S_IXUSR, uid, gid);
+ fail_unless(ret == EOK, "cannot create dir: %s", strerror(ret));
+
+ new_dir = talloc_asprintf(NULL, "%s/testdir", dir_path);
+ ret = stat(new_dir, &info);
+ fail_unless(ret == EOK, "failed to stat '%s'\n", new_dir);
+
+ /* check the dir has been created */
+ fail_unless(S_ISDIR(info.st_mode) != 0, "'%s' is not a dir.\n", new_dir);
+
+ /* check the permissions are okay */
+ fail_unless((info.st_mode & S_IRUSR) != 0, "Read permission is not set\n");
+ fail_unless((info.st_mode & S_IWUSR) == 0, "Write permission is set\n");
+ fail_unless((info.st_mode & S_IXUSR) != 0, "Exec permission is not set\n");
+
+ /* check the owner is okay */
+ fail_unless(info.st_uid == uid, "Dir created with the wrong uid\n");
+ fail_unless(info.st_gid == gid, "Dir created with the wrong gid\n");
+
+ talloc_free(new_dir);
+}
+END_TEST
+
static Suite *files_suite(void)
{
Suite *s = suite_create("files_suite");
@@ -393,6 +429,7 @@ static Suite *files_suite(void)
tcase_add_test(tc_files, test_copy_file);
tcase_add_test(tc_files, test_copy_symlink);
tcase_add_test(tc_files, test_copy_node);
+ tcase_add_test(tc_files, test_create_dir);
suite_add_tcase(s, tc_files);
return s;