8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek/*
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek Copyright (C) 2016 Red Hat
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek This program is free software; you can redistribute it and/or modify
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek it under the terms of the GNU General Public License as published by
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek (at your option) any later version.
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek This program is distributed in the hope that it will be useful,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek GNU General Public License for more details.
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek You should have received a copy of the GNU General Public License
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek*/
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include <errno.h>
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include <stdlib.h>
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include <stdarg.h>
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include <string.h>
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include <stddef.h>
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include <setjmp.h>
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include <cmocka.h>
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include <talloc.h>
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include <popt.h>
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include "limits.h"
3996e391054a1c02ab62e1541ae21a8204bd5d0aAmitKumar#include "shared/io.h"
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include "util/inotify.h"
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include "util/util.h"
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek#include "tests/common.h"
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstruct inotify_test_ctx {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek char *filename;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek char *dirname;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek int ncb;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek int threshold;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek /* if the cb receives flags not in this set, test fails */
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek uint32_t exp_flags;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct sss_test_ctx *tctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *fail_te;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek};
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void test_timeout(struct tevent_context *ev,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval t,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek void *ptr)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "The test timed out!\n");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(te);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fail();
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic struct inotify_test_ctx *common_setup(TALLOC_CTX *mem_ctx)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval tv;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx = talloc_zero(mem_ctx, struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ctx == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return NULL;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx->tctx = create_ev_test_ctx(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ctx->tctx == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return NULL;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek gettimeofday(&tv, NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv.tv_sec += 5;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx->fail_te = tevent_add_timer(ctx->tctx->ev, ctx,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv, test_timeout, ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ctx->fail_te == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unable to queue fallback timer!\n");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return NULL;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic int inotify_test_setup(void **state)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek int fd;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx = common_setup(NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ctx == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx->filename = talloc_strdup(ctx, "test_inotify.XXXXXX");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ctx->filename == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fd = mkstemp(ctx->filename);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (fd == -1) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek close(fd);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek *state = ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 0;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic int inotify_test_dir_setup(void **state)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx = common_setup(NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ctx == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx->dirname = talloc_strdup(ctx, "test_inotify_dir.XXXXXX");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ctx->dirname == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx->dirname = mkdtemp(ctx->dirname);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ctx->dirname == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx->filename = talloc_asprintf(ctx, "%s/testfile", ctx->dirname);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ctx->filename == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek *state = ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 0;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic int inotify_test_teardown(void **state)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *ctx = talloc_get_type_abort(*state,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek int ret;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = unlink(ctx->filename);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ret == -1 && errno != ENOENT) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 0;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic int inotify_test_dir_teardown(void **state)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *ctx = talloc_get_type_abort(*state,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek int ret;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = unlink(ctx->filename);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ret == -1 && errno != ENOENT) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = rmdir(ctx->dirname);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ret == -1 && errno != ENOENT) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 0;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void file_mod_op(struct tevent_context *ev,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval t,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek void *ptr)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(ptr,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek FILE *f;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(te);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek f = fopen(test_ctx->filename, "w");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (f == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->error = errno;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->done = true;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fprintf(f, "%s\n", test_ctx->filename);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fflush(f);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fclose(f);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void check_and_set_threshold(struct inotify_test_ctx *test_ctx,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek uint32_t flags)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (test_ctx->exp_flags != 0 && !(test_ctx->exp_flags & flags)) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fail();
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->ncb++;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic int inotify_set_threshold_cb(const char *filename,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek uint32_t flags,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek void *pvt)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(pvt,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek check_and_set_threshold(test_ctx, flags);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return EOK;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic int inotify_threshold_cb(const char *filename,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek uint32_t flags,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek void *pvt)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(pvt,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek check_and_set_threshold(test_ctx, flags);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (test_ctx->ncb == test_ctx->threshold) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->done = true;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return EOK;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return EOK;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek/* Test that running two modifications fires the callback twice */
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void test_inotify_mod(void **state)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(*state,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct snotify_ctx *ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval tv;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek errno_t ret;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx = snotify_create(test_ctx, test_ctx->tctx->ev, SNOTIFY_WATCH_DIR,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->filename, NULL, IN_MODIFY,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_threshold_cb, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek assert_non_null(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->threshold = 2;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->exp_flags = IN_MODIFY;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek gettimeofday(&tv, NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv.tv_usec += 500;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek te = tevent_add_timer(test_ctx->tctx->ev, test_ctx,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv, file_mod_op, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (te == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unable to queue file update!\n");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek gettimeofday(&tv, NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv.tv_sec += 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek te = tevent_add_timer(test_ctx->tctx->ev, test_ctx,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv, file_mod_op, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (te == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unable to queue file update!\n");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = test_ev_loop(test_ctx->tctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek assert_int_equal(ret, EOK);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void file_mv_op(struct tevent_context *ev,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval t,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek void *ptr)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(ptr,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek FILE *f;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek int fd;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek char src_tmp_file[] = "test_inotify_src.XXXXXX";
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek int ret;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(te);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fd = mkstemp(src_tmp_file);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (fd == -1) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->error = errno;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->done = true;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek f = fdopen(fd, "w");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (f == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek close(fd);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek unlink(src_tmp_file);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->error = errno;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->done = true;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fprintf(f, "%s\n", test_ctx->filename);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fflush(f);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fclose(f);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = rename(src_tmp_file, test_ctx->filename);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ret == -1) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek unlink(src_tmp_file);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->error = errno;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->done = true;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void test_inotify_mv(void **state)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(*state,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct snotify_ctx *ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval tv;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek errno_t ret;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx = snotify_create(test_ctx, test_ctx->tctx->ev, SNOTIFY_WATCH_DIR,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->filename, NULL, IN_MOVED_TO,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_threshold_cb, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek assert_non_null(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->threshold = 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->exp_flags = IN_MOVED_TO;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek gettimeofday(&tv, NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv.tv_usec += 200;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek te = tevent_add_timer(test_ctx->tctx->ev, test_ctx,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv, file_mv_op, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (te == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unable to queue file update!\n");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = test_ev_loop(test_ctx->tctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek assert_int_equal(ret, EOK);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void file_del_add_op(struct tevent_context *ev,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval t,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek void *ptr)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(ptr,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek FILE *f;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek int ret;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(te);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = unlink(test_ctx->filename);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ret == -1) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->error = errno;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->done = true;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek f = fopen(test_ctx->filename, "w");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (f == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->error = errno;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->done = true;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fprintf(f, "%s\n", test_ctx->filename);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fflush(f);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fclose(f);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void test_inotify_del_add(void **state)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(*state,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct snotify_ctx *ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval tv;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek errno_t ret;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->threshold = 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->exp_flags = IN_CREATE;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx = snotify_create(test_ctx, test_ctx->tctx->ev, SNOTIFY_WATCH_DIR,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->filename, NULL,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek IN_CREATE,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_threshold_cb, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek assert_non_null(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek gettimeofday(&tv, NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv.tv_usec += 200;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek te = tevent_add_timer(test_ctx->tctx->ev, test_ctx,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv, file_del_add_op, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (te == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unable to queue file update!\n");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = test_ev_loop(test_ctx->tctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek assert_int_equal(ret, EOK);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void test_inotify_file_moved_in(void **state)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(*state,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct snotify_ctx *ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval tv;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek errno_t ret;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->threshold = 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->exp_flags = IN_CREATE;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx = snotify_create(test_ctx, test_ctx->tctx->ev, SNOTIFY_WATCH_DIR,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->filename, NULL,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek IN_CREATE | IN_CLOSE_WRITE,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_threshold_cb, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek assert_non_null(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek gettimeofday(&tv, NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv.tv_usec += 200;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek te = tevent_add_timer(test_ctx->tctx->ev, test_ctx,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv, file_mod_op, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (te == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unable to queue file update!\n");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = test_ev_loop(test_ctx->tctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek assert_int_equal(ret, EOK);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void file_del_op(struct tevent_context *ev,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval t,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek void *ptr)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(ptr,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek int ret;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek talloc_free(te);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = unlink(test_ctx->filename);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (ret == -1) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->error = errno;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->done = true;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void check_threshold_cb(struct tevent_context *ev,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval t,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek void *ptr)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(ptr,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek /* tests that no more callbacks were issued and exactly one
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek * was caught for both requests
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek */
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (test_ctx->ncb == test_ctx->threshold) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->tctx->done = true;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fail();
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekstatic void test_inotify_delay(void **state)
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx *test_ctx = talloc_get_type_abort(*state,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct inotify_test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct snotify_ctx *ctx;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval tv;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct tevent_timer *te;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek errno_t ret;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct timeval delay = { .tv_sec = 1, .tv_usec = 0 };
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->threshold = 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->exp_flags = IN_CREATE | IN_DELETE;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ctx = snotify_create(test_ctx, test_ctx->tctx->ev, SNOTIFY_WATCH_DIR,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek test_ctx->filename, &delay,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek IN_CREATE | IN_DELETE,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_set_threshold_cb, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek assert_non_null(ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek gettimeofday(&tv, NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv.tv_usec += 100;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek te = tevent_add_timer(test_ctx->tctx->ev, test_ctx,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv, file_mod_op, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (te == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unable to queue file update!\n");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek gettimeofday(&tv, NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv.tv_usec += 200;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek te = tevent_add_timer(test_ctx->tctx->ev, test_ctx,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv, file_del_op, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (te == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unable to queue file update!\n");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek gettimeofday(&tv, NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv.tv_sec += 2;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek te = tevent_add_timer(test_ctx->tctx->ev, test_ctx,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek tv, check_threshold_cb, test_ctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek if (te == NULL) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unable to queue file update!\n");
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek ret = test_ev_loop(test_ctx->tctx);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek assert_int_equal(ret, EOK);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozekint main(int argc, const char *argv[])
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek{
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek poptContext pc;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek int opt;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek struct poptOption long_options[] = {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek POPT_AUTOHELP
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek SSSD_DEBUG_OPTS
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek POPT_TABLEEND
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek };
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek const struct CMUnitTest tests[] = {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek cmocka_unit_test_setup_teardown(test_inotify_mv,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_test_setup,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_test_teardown),
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek cmocka_unit_test_setup_teardown(test_inotify_mod,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_test_setup,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_test_teardown),
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek cmocka_unit_test_setup_teardown(test_inotify_del_add,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_test_setup,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_test_teardown),
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek cmocka_unit_test_setup_teardown(test_inotify_file_moved_in,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_test_dir_setup,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_test_dir_teardown),
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek cmocka_unit_test_setup_teardown(test_inotify_delay,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_test_dir_setup,
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek inotify_test_dir_teardown),
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek };
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek debug_level = SSSDBG_INVALID;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek pc = poptGetContext(argv[0], argc, argv, long_options, 0);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek while((opt = poptGetNextOpt(pc)) != -1) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek switch(opt) {
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek default:
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek fprintf(stderr, "\nInvalid option %s: %s\n\n",
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek poptBadOption(pc, 0), poptStrerror(opt));
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek poptPrintUsage(pc, stderr, 0);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return 1;
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek }
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek poptFreeContext(pc);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek DEBUG_CLI_INIT(debug_level);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek return cmocka_run_group_tests(tests, NULL, NULL);
8cfb42e1985550e99585d311f68087d414932806Jakub Hrozek}