Cross Reference: /sssd-io/src/tests/cmocka/test_child_common.c
test_child_common.c revision 16cb0969f0a9ea71524d852077d6a480740d4f12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
4714N/A/*
4714N/A Authors:
4714N/A Jakub Hrozek <jhrozek@redhat.com>
4714N/A
4714N/A Copyright (C) 2014 Red Hat
4714N/A
4714N/A SSSD tests: Child handlers
4714N/A
6982N/A This program is free software; you can redistribute it and/or modify
6982N/A it under the terms of the GNU General Public License as published by
4714N/A the Free Software Foundation; either version 3 of the License, or
4714N/A (at your option) any later version.
4714N/A
4714N/A This program is distributed in the hope that it will be useful,
6982N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
6982N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6982N/A GNU General Public License for more details.
6982N/A
4714N/A You should have received a copy of the GNU General Public License
4714N/A along with this program. If not, see <http://www.gnu.org/licenses/>.
4714N/A*/
4714N/A
4714N/A#include <talloc.h>
4714N/A#include <tevent.h>
4714N/A#include <errno.h>
4714N/A#include <popt.h>
4714N/A
4714N/A#include "util/child_common.h"
4714N/A#include "tests/cmocka/common_mock.h"
4714N/A
4714N/A#define TEST_BIN "dummy-child"
4714N/A#define ECHO_STR "Hello child"
4714N/A
4714N/Astruct child_test_ctx {
4714N/A int pipefd_to_child[2];
4714N/A int pipefd_from_child[2];
4714N/A
4714N/A struct sss_test_ctx *test_ctx;
4714N/A};
void child_test_setup(void **state)
{
struct child_test_ctx *child_tctx;
errno_t ret;
check_leaks_push(global_talloc_context);
child_tctx = talloc(global_talloc_context, struct child_test_ctx);
assert_non_null(child_tctx);
child_tctx->test_ctx = create_ev_test_ctx(child_tctx);
assert_non_null(child_tctx->test_ctx);
ret = pipe(child_tctx->pipefd_from_child);
assert_int_not_equal(ret, -1);
DEBUG(SSSDBG_TRACE_LIBS, "from_child: %d:%d\n",
child_tctx->pipefd_from_child[0],
child_tctx->pipefd_from_child[1]);
ret = pipe(child_tctx->pipefd_to_child);
assert_int_not_equal(ret, -1);
DEBUG(SSSDBG_TRACE_LIBS, "to_child: %d:%d\n",
child_tctx->pipefd_to_child[0],
child_tctx->pipefd_to_child[1]);
*state = child_tctx;
}
void child_test_teardown(void **state)
{
struct child_test_ctx *child_tctx = talloc_get_type(*state,
struct child_test_ctx);
talloc_free(child_tctx);
check_leaks_pop(global_talloc_context);
}
/* Just make sure the exec works. The child does nothing but exits */
void test_exec_child(void **state)
{
errno_t ret;
pid_t child_pid;
int status;
struct child_test_ctx *child_tctx = talloc_get_type(*state,
struct child_test_ctx);
child_pid = fork();
assert_int_not_equal(child_pid, -1);
if (child_pid == 0) {
ret = exec_child(child_tctx,
child_tctx->pipefd_to_child,
child_tctx->pipefd_from_child,
CHILD_DIR"/"TEST_BIN, 2);
assert_int_equal(ret, EOK);
} else {
do {
errno = 0;
ret = waitpid(child_pid, &status, 0);
} while (ret == -1 && errno == EINTR);
if (ret > 0) {
ret = EIO;
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
assert_int_equal(ret, 0);
}
} else {
DEBUG(SSSDBG_FUNC_DATA,
"Failed to wait for children %d\n", child_pid);
ret = EIO;
}
}
}
/* Just make sure the exec works. The child does nothing but exits */
void test_exec_child_extra_args(void **state)
{
errno_t ret;
pid_t child_pid;
int status;
struct child_test_ctx *child_tctx = talloc_get_type(*state,
struct child_test_ctx);
const char *extra_args[] = { "--guitar=george",
"--drums=ringo",
NULL };
setenv("TEST_CHILD_ACTION", "check_extra_args", 1);
child_pid = fork();
assert_int_not_equal(child_pid, -1);
if (child_pid == 0) {
ret = exec_child_ex(child_tctx,
child_tctx->pipefd_to_child,
child_tctx->pipefd_from_child,
CHILD_DIR"/"TEST_BIN, 2, extra_args,
STDIN_FILENO, STDOUT_FILENO);
assert_int_equal(ret, EOK);
} else {
do {
errno = 0;
ret = waitpid(child_pid, &status, 0);
} while (ret == -1 && errno == EINTR);
if (ret > 0) {
ret = EIO;
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
assert_int_equal(ret, 0);
}
} else {
DEBUG(SSSDBG_FUNC_DATA,
"Failed to wait for children %d\n", child_pid);
ret = EIO;
}
}
}
int main(int argc, const char *argv[])
{
int rv;
poptContext pc;
int opt;
struct poptOption long_options[] = {
POPT_AUTOHELP
SSSD_DEBUG_OPTS
POPT_TABLEEND
};
const UnitTest tests[] = {
unit_test_setup_teardown(test_exec_child,
child_test_setup,
child_test_teardown),
unit_test_setup_teardown(test_exec_child_extra_args,
child_test_setup,
child_test_teardown),
};
/* Set debug level to invalid value so we can deside if -d 0 was used. */
debug_level = SSSDBG_INVALID;
pc = poptGetContext(argv[0], argc, argv, long_options, 0);
while((opt = poptGetNextOpt(pc)) != -1) {
switch(opt) {
default:
fprintf(stderr, "\nInvalid option %s: %s\n\n",
poptBadOption(pc, 0), poptStrerror(opt));
poptPrintUsage(pc, stderr, 0);
return 1;
}
}
poptFreeContext(pc);
DEBUG_CLI_INIT(debug_level);
rv = run_tests(tests);
return rv;
}