tests: move fd-util related tests to test-fd-util.c

This commit is contained in:
Ronny Chevalier 2016-03-03 00:06:17 +01:00
parent d5b29bb5d9
commit 0999c8ade8
4 changed files with 111 additions and 72 deletions

1
.gitignore vendored
View File

@ -187,6 +187,7 @@
/test-event
/test-execute
/test-extract-word
/test-fd-util
/test-fdset
/test-fileio
/test-firewall-util

View File

@ -1432,6 +1432,7 @@ tests += \
test-alloc-util \
test-web-util \
test-stat-util \
test-fd-util \
test-string-util \
test-extract-word \
test-parse-util \
@ -1773,6 +1774,12 @@ test_alloc_util_SOURCES = \
test_alloc_util_LDADD = \
libbasic.la
test_fd_util_SOURCES = \
src/test/test-fd-util.c
test_fd_util_LDADD = \
libbasic.la
test_web_util_SOURCES = \
src/test/test-web-util.c

103
src/test/test-fd-util.c Normal file
View File

@ -0,0 +1,103 @@
/***
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 <fcntl.h>
#include <unistd.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "macro.h"
static void test_close_many(void) {
int fds[3];
char name0[] = "/tmp/test-close-many.XXXXXX";
char name1[] = "/tmp/test-close-many.XXXXXX";
char name2[] = "/tmp/test-close-many.XXXXXX";
fds[0] = mkostemp_safe(name0, O_RDWR|O_CLOEXEC);
fds[1] = mkostemp_safe(name1, O_RDWR|O_CLOEXEC);
fds[2] = mkostemp_safe(name2, O_RDWR|O_CLOEXEC);
close_many(fds, 2);
assert_se(fcntl(fds[0], F_GETFD) == -1);
assert_se(fcntl(fds[1], F_GETFD) == -1);
assert_se(fcntl(fds[2], F_GETFD) >= 0);
safe_close(fds[2]);
unlink(name0);
unlink(name1);
unlink(name2);
}
static void test_close_nointr(void) {
char name[] = "/tmp/test-test-close_nointr.XXXXXX";
int fd;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0);
assert_se(close_nointr(fd) >= 0);
assert_se(close_nointr(fd) < 0);
unlink(name);
}
static void test_same_fd(void) {
_cleanup_close_pair_ int p[2] = { -1, -1 };
_cleanup_close_ int a = -1, b = -1, c = -1;
assert_se(pipe2(p, O_CLOEXEC) >= 0);
assert_se((a = dup(p[0])) >= 0);
assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
assert_se((c = dup(a)) >= 0);
assert_se(same_fd(p[0], p[0]) > 0);
assert_se(same_fd(p[1], p[1]) > 0);
assert_se(same_fd(a, a) > 0);
assert_se(same_fd(b, b) > 0);
assert_se(same_fd(a, p[0]) > 0);
assert_se(same_fd(p[0], a) > 0);
assert_se(same_fd(c, p[0]) > 0);
assert_se(same_fd(p[0], c) > 0);
assert_se(same_fd(a, c) > 0);
assert_se(same_fd(c, a) > 0);
assert_se(same_fd(p[0], p[1]) == 0);
assert_se(same_fd(p[1], p[0]) == 0);
assert_se(same_fd(p[0], b) == 0);
assert_se(same_fd(b, p[0]) == 0);
assert_se(same_fd(p[1], a) == 0);
assert_se(same_fd(a, p[1]) == 0);
assert_se(same_fd(p[1], b) == 0);
assert_se(same_fd(b, p[1]) == 0);
assert_se(same_fd(a, b) == 0);
assert_se(same_fd(b, a) == 0);
}
int main(int argc, char *argv[]) {
test_close_many();
test_close_nointr();
test_same_fd();
return 0;
}

View File

@ -171,29 +171,6 @@ static void test_div_round_up(void) {
assert_se(0xfffffffdU / 10U + !!(0xfffffffdU % 10U) == 429496730U);
}
static void test_close_many(void) {
int fds[3];
char name0[] = "/tmp/test-close-many.XXXXXX";
char name1[] = "/tmp/test-close-many.XXXXXX";
char name2[] = "/tmp/test-close-many.XXXXXX";
fds[0] = mkostemp_safe(name0, O_RDWR|O_CLOEXEC);
fds[1] = mkostemp_safe(name1, O_RDWR|O_CLOEXEC);
fds[2] = mkostemp_safe(name2, O_RDWR|O_CLOEXEC);
close_many(fds, 2);
assert_se(fcntl(fds[0], F_GETFD) == -1);
assert_se(fcntl(fds[1], F_GETFD) == -1);
assert_se(fcntl(fds[2], F_GETFD) >= 0);
safe_close(fds[2]);
unlink(name0);
unlink(name1);
unlink(name2);
}
static void test_u64log2(void) {
assert_se(u64log2(0) == 0);
assert_se(u64log2(8) == 3);
@ -284,18 +261,6 @@ static void test_log2i(void) {
assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
}
static void test_close_nointr(void) {
char name[] = "/tmp/test-test-close_nointr.XXXXXX";
int fd;
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0);
assert_se(close_nointr(fd) >= 0);
assert_se(close_nointr(fd) < 0);
unlink(name);
}
static void test_unlink_noerrno(void) {
char name[] = "/tmp/test-close_nointr.XXXXXX";
int fd;
@ -438,40 +403,6 @@ static void test_raw_clone(void) {
}
}
static void test_same_fd(void) {
_cleanup_close_pair_ int p[2] = { -1, -1 };
_cleanup_close_ int a = -1, b = -1, c = -1;
assert_se(pipe2(p, O_CLOEXEC) >= 0);
assert_se((a = dup(p[0])) >= 0);
assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
assert_se((c = dup(a)) >= 0);
assert_se(same_fd(p[0], p[0]) > 0);
assert_se(same_fd(p[1], p[1]) > 0);
assert_se(same_fd(a, a) > 0);
assert_se(same_fd(b, b) > 0);
assert_se(same_fd(a, p[0]) > 0);
assert_se(same_fd(p[0], a) > 0);
assert_se(same_fd(c, p[0]) > 0);
assert_se(same_fd(p[0], c) > 0);
assert_se(same_fd(a, c) > 0);
assert_se(same_fd(c, a) > 0);
assert_se(same_fd(p[0], p[1]) == 0);
assert_se(same_fd(p[1], p[0]) == 0);
assert_se(same_fd(p[0], b) == 0);
assert_se(same_fd(b, p[0]) == 0);
assert_se(same_fd(p[1], a) == 0);
assert_se(same_fd(a, p[1]) == 0);
assert_se(same_fd(p[1], b) == 0);
assert_se(same_fd(b, p[1]) == 0);
assert_se(same_fd(a, b) == 0);
assert_se(same_fd(b, a) == 0);
}
static void test_sparse_write_one(int fd, const char *buffer, size_t n) {
char check[n];
@ -554,7 +485,6 @@ int main(int argc, char *argv[]) {
test_max();
test_container_of();
test_div_round_up();
test_close_many();
test_u64log2();
test_protect_errno();
test_config_parse_iec_uint64();
@ -562,14 +492,12 @@ int main(int argc, char *argv[]) {
test_get_files_in_directory();
test_in_set();
test_log2i();
test_close_nointr();
test_unlink_noerrno();
test_readlink_and_make_absolute();
test_glob_exists();
test_execute_directory();
test_parse_proc_cmdline();
test_raw_clone();
test_same_fd();
test_sparse_write();
test_fgetxattrat_fake();
test_runlevel_to_target();