test_expire_common.c revision 50b8a36b0932a510e825ed1ad8103f81ead2b7d8
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl/*
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl Authors:
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl Pavel Reichl <preichl@redhat.com>
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl Copyright (C) 2015 Red Hat
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl SSSD tests - common code for password expiration tests
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl This program is free software; you can redistribute it and/or modify
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl it under the terms of the GNU General Public License as published by
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl the Free Software Foundation; either version 3 of the License, or
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl (at your option) any later version.
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl This program is distributed in the hope that it will be useful,
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl but WITHOUT ANY WARRANTY; without even the implied warranty of
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl GNU General Public License for more details.
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl You should have received a copy of the GNU General Public License
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl along with this program. If not, see <http://www.gnu.org/licenses/>.
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl*/
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#include <stdarg.h>
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#include <stdlib.h>
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#include <stddef.h>
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#include <setjmp.h>
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#include <unistd.h>
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#include <sys/types.h>
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#include <cmocka.h>
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#include <time.h>
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#include "tests/common_check.h"
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#include "tests/cmocka/test_expire_common.h"
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl#define MAX 100
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichlstatic char *now_str(TALLOC_CTX *mem_ctx, const char* format, int s)
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl{
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl time_t t = time(NULL) + s;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl struct tm *tm;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl size_t len;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl char *timestr;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl timestr = talloc_array(mem_ctx, char, MAX);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl tm = gmtime(&t);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl len = strftime(timestr, MAX, format, tm);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl if (len == 0) {
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl return NULL;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl }
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl return timestr;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl}
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichlint expire_test_setup(void **state)
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl{
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl struct expire_test_ctx *exp_state;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl TALLOC_CTX *mem_ctx;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl char *past_time;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl char *future_time;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl char *invalid_format;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl char *invalid_longer_format;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl mem_ctx = talloc_new(NULL);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl assert_non_null(mem_ctx);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl exp_state = talloc(mem_ctx, struct expire_test_ctx);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl assert_non_null(exp_state);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl *state = exp_state;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl /* testing data */
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl invalid_format = now_str(exp_state, "%Y%m%d%H%M%S", -20);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl assert_non_null(invalid_format);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl invalid_longer_format = (void*)now_str(exp_state, "%Y%m%d%H%M%SZA", -20);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl assert_non_null(invalid_longer_format);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl past_time = (void*)now_str(exp_state, "%Y%m%d%H%M%SZ", -20);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl assert_non_null(past_time);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl future_time = (void*)now_str(exp_state, "%Y%m%d%H%M%SZ", 20);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl assert_non_null(future_time);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl exp_state->past_time = past_time;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl exp_state->future_time = future_time;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl exp_state->invalid_format = invalid_format;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl exp_state->invalid_longer_format = invalid_longer_format;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl return 0;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl}
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichlint expire_test_teardown(void **state)
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl{
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl struct expire_test_ctx *test_ctx;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl test_ctx = talloc_get_type(*state, struct expire_test_ctx);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl assert_non_null(test_ctx);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl talloc_free(test_ctx);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl return 0;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl}
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichlvoid expire_test_tz(const char* tz,
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl void (*test_func)(void*, void*),
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl void *test_in,
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl void *_test_out)
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl{
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl errno_t ret;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl const char *orig_tz = NULL;
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl orig_tz = getenv("TZ");
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl if (orig_tz == NULL) {
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl orig_tz = "";
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl }
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl if (tz) {
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl ret = setenv("TZ", tz, 1);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl assert_false(ret == -1);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl }
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl test_func(test_in, _test_out);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl /* restore */
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl if (orig_tz != NULL) {
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl ret = setenv("TZ", orig_tz, 1);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl assert_false(ret == -1);
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl }
50b8a36b0932a510e825ed1ad8103f81ead2b7d8Pavel Reichl}