Line data Source code
1 : /*
2 : Authors:
3 : Pavel Reichl <preichl@redhat.com>
4 :
5 : Copyright (C) 2015 Red Hat
6 :
7 : SSSD tests - common code for password expiration tests
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include <stdarg.h>
24 : #include <stdlib.h>
25 : #include <stddef.h>
26 : #include <setjmp.h>
27 : #include <unistd.h>
28 : #include <sys/types.h>
29 : #include <cmocka.h>
30 : #include <time.h>
31 :
32 : #include "tests/common_check.h"
33 : #include "tests/cmocka/test_expire_common.h"
34 :
35 : #define MAX 100
36 :
37 8 : static char *now_str(TALLOC_CTX *mem_ctx, const char* format, int s)
38 : {
39 8 : time_t t = time(NULL) + s;
40 : struct tm *tm;
41 : size_t len;
42 : char *timestr;
43 :
44 8 : timestr = talloc_array(mem_ctx, char, MAX);
45 :
46 8 : tm = gmtime(&t);
47 8 : len = strftime(timestr, MAX, format, tm);
48 8 : if (len == 0) {
49 0 : return NULL;
50 : }
51 :
52 8 : return timestr;
53 : }
54 :
55 2 : int expire_test_setup(void **state)
56 : {
57 : struct expire_test_ctx *exp_state;
58 : TALLOC_CTX *mem_ctx;
59 : char *past_time;
60 : char *future_time;
61 : char *invalid_format;
62 : char *invalid_longer_format;
63 :
64 2 : mem_ctx = talloc_new(NULL);
65 2 : assert_non_null(mem_ctx);
66 :
67 2 : exp_state = talloc(mem_ctx, struct expire_test_ctx);
68 2 : assert_non_null(exp_state);
69 :
70 2 : *state = exp_state;
71 :
72 : /* testing data */
73 2 : invalid_format = now_str(exp_state, "%Y%m%d%H%M%S", -20);
74 2 : assert_non_null(invalid_format);
75 :
76 2 : invalid_longer_format = (void*)now_str(exp_state, "%Y%m%d%H%M%SZA", -20);
77 2 : assert_non_null(invalid_longer_format);
78 :
79 2 : past_time = (void*)now_str(exp_state, "%Y%m%d%H%M%SZ", -20);
80 2 : assert_non_null(past_time);
81 :
82 2 : future_time = (void*)now_str(exp_state, "%Y%m%d%H%M%SZ", 20);
83 2 : assert_non_null(future_time);
84 :
85 2 : exp_state->past_time = past_time;
86 2 : exp_state->future_time = future_time;
87 2 : exp_state->invalid_format = invalid_format;
88 2 : exp_state->invalid_longer_format = invalid_longer_format;
89 :
90 2 : return 0;
91 : }
92 :
93 2 : int expire_test_teardown(void **state)
94 : {
95 : struct expire_test_ctx *test_ctx;
96 :
97 2 : test_ctx = talloc_get_type(*state, struct expire_test_ctx);
98 2 : assert_non_null(test_ctx);
99 :
100 2 : talloc_free(test_ctx);
101 :
102 2 : return 0;
103 : }
104 :
105 4 : void expire_test_tz(const char* tz,
106 : void (*test_func)(void*, void*),
107 : void *test_in,
108 : void *_test_out)
109 : {
110 : errno_t ret;
111 4 : const char *orig_tz = NULL;
112 :
113 4 : orig_tz = getenv("TZ");
114 4 : if (orig_tz == NULL) {
115 2 : orig_tz = "";
116 : }
117 :
118 4 : if (tz) {
119 4 : ret = setenv("TZ", tz, 1);
120 :
121 4 : assert_return_code(ret, errno);
122 : }
123 :
124 4 : test_func(test_in, _test_out);
125 :
126 : /* restore */
127 4 : if (orig_tz != NULL) {
128 4 : ret = setenv("TZ", orig_tz, 1);
129 4 : assert_return_code(ret, errno);
130 : }
131 4 : }
|