chiark / gitweb /
tests: add test-fdset
authorRonny Chevalier <chevalier.ronny@gmail.com>
Sat, 14 Jun 2014 20:10:45 +0000 (22:10 +0200)
committerTom Gundersen <teg@jklm.no>
Mon, 16 Jun 2014 18:38:22 +0000 (20:38 +0200)
.gitignore
Makefile.am
src/test/test-fdset.c [new file with mode: 0644]

index f7127bde9b5f13b593f47ae5486aeb3cb6d83de7..97cefb514adaef0a36d477ce03a7792acab2bdc6 100644 (file)
 /test-engine
 /test-env-replace
 /test-event
 /test-engine
 /test-env-replace
 /test-event
+/test-fdset
 /test-fileio
 /test-hashmap
 /test-hostname
 /test-fileio
 /test-hashmap
 /test-hostname
index cb43ddb081b4a31862beac49fed09f7f790c08fc..a9e07b455f81dc8a2f222a101929ad8373c44b07 100644 (file)
@@ -1234,7 +1234,8 @@ tests += \
        test-device-nodes \
        test-xml \
        test-architecture \
        test-device-nodes \
        test-xml \
        test-architecture \
-       test-socket-util
+       test-socket-util \
+       test-fdset
 
 EXTRA_DIST += \
        test/sched_idle_bad.service \
 
 EXTRA_DIST += \
        test/sched_idle_bad.service \
@@ -1338,6 +1339,12 @@ test_utf8_SOURCES = \
 test_utf8_LDADD = \
        libsystemd-shared.la
 
 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
 
 test_util_SOURCES = \
        src/test/test-util.c
 
diff --git a/src/test/test-fdset.c b/src/test/test-fdset.c
new file mode 100644 (file)
index 0000000..ed83ee1
--- /dev/null
@@ -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();
+}