END_TEST START_TEST(test_wrong_gid) { int ret; ret = check_and_open_readonly(filename, &fd, uid, gid+1, mode, CHECK_REG); fail_unless(ret == EINVAL, "check_and_open_readonly succeeded with wrong gid"); fail_unless(fd == -1, "check_and_open_readonly file descriptor not -1"); }
END_TEST START_TEST(test_not_regular_file) { int ret; ret = check_and_open_readonly("/dev/null", &fd, uid, gid, mode, CHECK_REG); fail_unless(ret == EINVAL, "check_and_open_readonly succeeded on non-regular file"); fail_unless(fd == -1, "check_and_open_readonly file descriptor not -1"); }
END_TEST START_TEST(test_ok) { int ret; ret = check_and_open_readonly(filename, &fd, uid, gid, mode, CHECK_REG); fail_unless(ret == EOK, "check_and_open_readonly failed"); fail_unless(fd >= 0, "check_and_open_readonly returned illegal file descriptor"); }
END_TEST START_TEST(test_wrong_permission) { int ret; ret = check_and_open_readonly(filename, &fd, uid, gid, (mode|S_IWOTH), CHECK_REG); fail_unless(ret == EINVAL, "check_and_open_readonly succeeded with wrong mode"); fail_unless(fd == -1, "check_and_open_readonly file descriptor not -1"); }
END_TEST START_TEST(test_simple_copy) { int ret; char origpath[PATH_MAX+1]; char *tmp; int fd = -1; errno = 0; fail_unless(getcwd(origpath, PATH_MAX) == origpath, "Cannot getcwd\n"); fail_unless(errno == 0, "Cannot getcwd\n"); /* create a file */ ret = chdir(dir_path); fail_if(ret == -1, "Cannot chdir1\n"); ret = create_simple_file("bar", "bar"); fail_if(ret == -1, "Cannot create file1\n"); /* create a subdir and file inside it */ ret = mkdir("subdir", 0700); fail_if(ret == -1, "Cannot create subdir\n"); ret = chdir("subdir"); fail_if(ret == -1, "Cannot chdir\n"); ret = create_simple_file("foo", "foo"); fail_if(ret == -1, "Cannot create file\n"); /* go back */ ret = chdir(origpath); fail_if(ret == -1, "Cannot chdir\n"); /* and finally copy.. */ DEBUG(SSSDBG_FUNC_DATA, "Will copy from '%s' to '%s'\n", dir_path, dst_path); ret = copy_tree(dir_path, dst_path, 0700, uid, gid); fail_unless(ret == EOK, "copy_tree failed\n"); /* check if really copied */ ret = access(dst_path, F_OK); fail_unless(ret == 0, "destination directory not there\n"); tmp = talloc_asprintf(test_ctx, "%s/bar", dst_path); ret = check_and_open_readonly(tmp, &fd, uid, gid, 0700, CHECK_REG); fail_unless(ret == EOK, "Cannot open %s\n"); close(fd); talloc_free(tmp); }
END_TEST START_TEST(test_copy_file) { TALLOC_CTX *tmp_ctx = talloc_new(test_ctx); int ret; char origpath[PATH_MAX+1]; char *foo_path; char *bar_path; int fd = -1; errno = 0; fail_unless(getcwd(origpath, PATH_MAX) == origpath, "Cannot getcwd\n"); fail_unless(errno == 0, "Cannot getcwd\n"); /* create a file */ ret = chdir(dir_path); fail_if(ret == -1, "Cannot chdir1\n"); ret = create_simple_file("foo", "foo"); fail_if(ret == -1, "Cannot create foo\n"); foo_path = talloc_asprintf(tmp_ctx, "%s/foo", dir_path); bar_path = talloc_asprintf(tmp_ctx, "%s/bar", dst_path); /* create a file */ ret = chdir(origpath); fail_if(ret == -1, "Cannot chdir1\n"); /* Copy this file to a new file */ DEBUG(SSSDBG_FUNC_DATA, "Will copy from 'foo' to 'bar'\n"); ret = sss_copy_file_secure(foo_path, bar_path, 0700, uid, gid, 0); fail_unless(ret == EOK, "copy_file_secure failed\n"); /* check if really copied */ ret = access(bar_path, F_OK); fail_unless(ret == 0, "destination file 'bar' not there\n"); ret = check_and_open_readonly(bar_path, &fd, uid, gid, S_IFREG|S_IRWXU, 0); fail_unless(ret == EOK, "Cannot open %s\n", bar_path); close(fd); talloc_free(tmp_ctx); }
END_TEST START_TEST(test_write) { int ret; ssize_t size; errno_t my_errno; ret = check_and_open_readonly(filename, &fd, uid, gid, mode, CHECK_REG); fail_unless(ret == EOK, "check_and_open_readonly failed"); fail_unless(fd >= 0, "check_and_open_readonly returned illegal file descriptor"); size = write(fd, "abc", 3); my_errno = errno; fail_unless(size == -1, "check_and_open_readonly file is not readonly"); fail_unless(my_errno == EBADF, "write failed for other reason than readonly"); }