tests: add test-fdset

This commit is contained in:
Ronny Chevalier 2014-06-14 22:10:45 +02:00 committed by Tom Gundersen
parent c182135d3a
commit 106ecd769d
3 changed files with 123 additions and 1 deletions

1
.gitignore vendored
View file

@ -148,6 +148,7 @@
/test-engine
/test-env-replace
/test-event
/test-fdset
/test-fileio
/test-hashmap
/test-hostname

View file

@ -1234,7 +1234,8 @@ tests += \
test-device-nodes \
test-xml \
test-architecture \
test-socket-util
test-socket-util \
test-fdset
EXTRA_DIST += \
test/sched_idle_bad.service \
@ -1338,6 +1339,12 @@ test_utf8_SOURCES = \
test_utf8_LDADD = \
libsystemd-shared.la
test_fdset_SOURCES = \
src/test/test-fdset.c
test_fdset_LDADD = \
libsystemd-core.la
test_util_SOURCES = \
src/test/test-util.c

114
src/test/test-fdset.c Normal file
View file

@ -0,0 +1,114 @@
/***
This file is part of systemd
Copyright 2014 Ronny Chevalier
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 "fdset.h"
#include "util.h"
#include "macro.h"
static void test_fdset_new_fill(void) {
int fd = -1;
_cleanup_fdset_free_ FDSet *fdset = NULL;
char name[] = "/tmp/test-fdset_new_fill.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0);
assert_se(fdset_new_fill(&fdset) >= 0);
assert_se(fdset_contains(fdset, fd));
unlink(name);
}
static void test_fdset_put_dup(void) {
_cleanup_close_ int fd = -1;
int copyfd = -1;
_cleanup_fdset_free_ FDSet *fdset = NULL;
char name[] = "/tmp/test-fdset_put_dup.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0);
fdset = fdset_new();
assert_se(fdset);
copyfd = fdset_put_dup(fdset, fd);
assert_se(copyfd >= 0 && copyfd != fd);
assert_se(fdset_contains(fdset, copyfd));
assert_se(!fdset_contains(fdset, fd));
unlink(name);
}
static void test_fdset_cloexec(void) {
int fd = -1;
_cleanup_fdset_free_ FDSet *fdset = NULL;
int flags = -1;
char name[] = "/tmp/test-fdset_cloexec.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0);
fdset = fdset_new();
assert_se(fdset);
assert_se(fdset_put(fdset, fd));
assert_se(fdset_cloexec(fdset, false) >= 0);
flags = fcntl(fd, F_GETFD);
assert_se(flags >= 0);
assert_se(!(flags & FD_CLOEXEC));
assert_se(fdset_cloexec(fdset, true) >= 0);
flags = fcntl(fd, F_GETFD);
assert_se(flags >= 0);
assert_se(flags & FD_CLOEXEC);
unlink(name);
}
static void test_fdset_close_others(void) {
int fd = -1;
int copyfd = -1;
_cleanup_fdset_free_ FDSet *fdset = NULL;
int flags = -1;
char name[] = "/tmp/test-fdset_close_others.XXXXXX";
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0);
fdset = fdset_new();
assert_se(fdset);
copyfd = fdset_put_dup(fdset, fd);
assert_se(copyfd >= 0);
assert_se(fdset_close_others(fdset) >= 0);
flags = fcntl(fd, F_GETFD);
assert_se(flags < 0);
flags = fcntl(copyfd, F_GETFD);
assert_se(flags >= 0);
unlink(name);
}
int main(int argc, char *argv[]) {
test_fdset_new_fill();
test_fdset_put_dup();
test_fdset_cloexec();
test_fdset_close_others();
}