diff --git a/Knot.files b/Knot.files index 50c9fd64b7f16a71b19bfe943891e1fb37a045d1..ef2d19e6c43b9fd8de79fac31b75b7d17bd809d4 100644 --- a/Knot.files +++ b/Knot.files @@ -182,6 +182,8 @@ src/tests/common/skiplist_tests.c src/tests/common/skiplist_tests.h src/tests/common/slab_tests.c src/tests/common/slab_tests.h +src/tests/common/os_fdset_tests.c +src/tests/common/os_fdset_tests.h src/tests/knot/dthreads_tests.c src/tests/knot/dthreads_tests.h src/tests/knot/conf_tests.c diff --git a/src/Makefile.am b/src/Makefile.am index 34065ef0f27a3963a79d230fbe91f03cd0260f6a..4a1a754e1ce0e3753fb035c60ab2f613a93e460d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -58,6 +58,8 @@ unittests_SOURCES = \ tests/common/skiplist_tests.h \ tests/common/slab_tests.c \ tests/common/slab_tests.h \ + tests/common/os_fdset_tests.c \ + tests/common/os_fdset_tests.h \ tests/knot/conf_tests.c \ tests/knot/conf_tests.h \ tests/knot/dthreads_tests.c \ diff --git a/src/tests/common/os_fdset_tests.c b/src/tests/common/os_fdset_tests.c new file mode 100644 index 0000000000000000000000000000000000000000..79072a0c5131a01e883391b814e8776862433c14 --- /dev/null +++ b/src/tests/common/os_fdset_tests.c @@ -0,0 +1,87 @@ +#include <stdlib.h> +#include <stdint.h> + +#include "tests/common/os_fdset_tests.h" +#include "common/os_fdset.h" + +static int os_fdset_tests_count(int argc, char *argv[]); +static int os_fdset_tests_run(int argc, char *argv[]); + +/*! Exported unit API. + */ +unit_api os_fdset_tests_api = { + "Native fdset poll wrapper", //! Unit name + &os_fdset_tests_count, //! Count scheduled tests + &os_fdset_tests_run //! Run scheduled tests +}; + +static int os_fdset_tests_count(int argc, char *argv[]) +{ + return 11; +} + +static int os_fdset_tests_run(int argc, char *argv[]) +{ + diag("os_fdset: implements '%s'", os_fdset_method()); + + /* 1. Create fdset. */ + os_fdset_t *set = os_fdset_new(); + ok(set != 0, "os_fdset: new"); + + /* 2. Create pipe. */ + int fds[2]; + int ret = pipe(fds); + ok(ret >= 0, "os_fdset: pipe() works"); + + /* 3. Add fd to set. */ + ret = os_fdset_add(set, fds[0], OS_EV_READ); + ok(ret == 0, "os_fdset: add to set works"); + + /* 4. Watch fdset. */ + const char pattern = 0xde; + ret = write(fds[1], &pattern, 1); + ret = os_fdset_poll(set); + ok(ret > 0, "os_fdset: poll returned events"); + + /* 5. Prepare event set. */ + os_fdset_it it; + ret = os_fdset_begin(set, &it); + ok(ret == 0, "os_fdset: begin is valid"); + + /* 6. Receive data. */ + char buf; + ret = read(it.fd, &buf, 1); + ok(ret >= 0 && buf == pattern, "os_fdset: contains valid data"); + + /* 7. Iterate event set. */ + ret = os_fdset_next(set, &it); + ok(ret < 0, "os_fdset: boundary check works"); + + /* 8. Remove from event set. */ + ret = os_fdset_remove(set, fds[0]); + ok(ret == 0, "os_fdset: remove from fdset works"); + close(fds[0]); + close(fds[1]); + + /* 9. Poll empty fdset. */ + ret = os_fdset_poll(set); + ok(ret <= 0, "os_fdset: polling empty fdset returns"); + + /* 10. Crash test. */ + lives_ok({ + os_fdset_destroy(0); + os_fdset_add(0, -1, 0); + os_fdset_remove(0, -1); + os_fdset_poll(0); + os_fdset_begin(0, 0); + os_fdset_end(0, 0); + os_fdset_next(0, 0); + os_fdset_method(); + }, "os_fdset: crash test successful"); + + /* 11. Destroy fdset. */ + ret = os_fdset_destroy(set); + ok(ret == 0, "os_fdset: destroyed"); + + return 0; +} diff --git a/src/tests/common/os_fdset_tests.h b/src/tests/common/os_fdset_tests.h new file mode 100644 index 0000000000000000000000000000000000000000..087fc9278cdf099eb0d0a7f43b2a35baa05f7a8b --- /dev/null +++ b/src/tests/common/os_fdset_tests.h @@ -0,0 +1,9 @@ +#ifndef _KNOTD_OS_FDSET_TESTS_H_ +#define _KNOTD_OS_FDSET_TESTS_H_ + +#include "common/libtap/tap_unit.h" + +/* Unit API. */ +unit_api os_fdset_tests_api; + +#endif /* _KNOTD_OS_FDSET_TESTS_H_ */ diff --git a/src/tests/unittests_main.c b/src/tests/unittests_main.c index 3b2b7a440c8b845c86be14689cb8ca090d32e05d..7d023977c7c387e2a08116950059970a6b63f30d 100644 --- a/src/tests/unittests_main.c +++ b/src/tests/unittests_main.c @@ -8,6 +8,7 @@ #include "tests/common/events_tests.h" #include "tests/common/da_tests.h" #include "tests/common/acl_tests.h" +#include "tests/common/os_fdset_tests.h" #include "tests/knot/dthreads_tests.h" #include "tests/knot/journal_tests.h" #include "tests/knot/server_tests.h" @@ -30,6 +31,7 @@ int main(int argc, char *argv[]) &events_tests_api, //! Events testing unit &da_tests_api, //! Dynamic array unit &acl_tests_api, //! ACLs + &os_fdset_tests_api, //! FDSET polling wrapper /* Server parts. */ &conf_tests_api, //! Configuration parser tests