Systemd/src/test/test-fs-util.c
2016-03-03 19:04:06 +01:00

92 lines
2.8 KiB
C

/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include "alloc-util.h"
#include "fileio.h"
#include "fd-util.h"
#include "fs-util.h"
#include "macro.h"
#include "mkdir.h"
#include "rm-rf.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
static void test_unlink_noerrno(void) {
char name[] = "/tmp/test-close_nointr.XXXXXX";
int fd;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0);
assert_se(close_nointr(fd) >= 0);
{
PROTECT_ERRNO;
errno = -42;
assert_se(unlink_noerrno(name) >= 0);
assert_se(errno == -42);
assert_se(unlink_noerrno(name) < 0);
assert_se(errno == -42);
}
}
static void test_readlink_and_make_absolute(void) {
char tempdir[] = "/tmp/test-readlink_and_make_absolute";
char name[] = "/tmp/test-readlink_and_make_absolute/original";
char name2[] = "test-readlink_and_make_absolute/original";
char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
char *r = NULL;
assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
assert_se(touch(name) >= 0);
assert_se(symlink(name, name_alias) >= 0);
assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
assert_se(streq(r, name));
free(r);
assert_se(unlink(name_alias) >= 0);
assert_se(chdir(tempdir) >= 0);
assert_se(symlink(name2, name_alias) >= 0);
assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
assert_se(streq(r, name));
free(r);
assert_se(unlink(name_alias) >= 0);
assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
}
static void test_get_files_in_directory(void) {
_cleanup_strv_free_ char **l = NULL, **t = NULL;
assert_se(get_files_in_directory("/tmp", &l) >= 0);
assert_se(get_files_in_directory(".", &t) >= 0);
assert_se(get_files_in_directory(".", NULL) >= 0);
}
int main(int argc, char *argv[]) {
test_unlink_noerrno();
test_readlink_and_make_absolute();
test_get_files_in_directory();
return 0;
}