Debian Codegraph @

tests/t-spawn.c

raw
1 /* t-spawn.c - Check the spawn functions.
2 * Copyright (C) 2024 g10 Code GmbH
3 *
4 * This file is part of libgpg-error.
5 *
6 * libgpg-error is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * libgpg-error is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #define PGM "t-spawn"
33
34 #include "t-common.h"
35
36 #define MAXLINE 1024
37
38 static void
39 run_test (const char *progname)
40 {
41 gpg_err_code_t rc;
42 int r;
43 gpgrt_spawn_actions_t act;
44 gpgrt_process_t process;
45 gpgrt_stream_t fp_out;
46 const char *const envchange[] = {
47 "ADD=0", /* Add */
48 "REPLACE=1", /* Replace */
49 "GNUPGHOME", /* Remove */
50 NULL,
51 };
52 const char *argv1[] = {
53 "--child",
54 NULL,
55 };
56 char line[MAXLINE];
57 int ok_add, ok_replace, ok_remove;
58
59 /* Make sure ADD is not defined in the parent process. */
60 rc = gpgrt_setenv ("ADD", NULL, 1);
61 if (rc)
62 fail ("gpgrt_setenv failed at %d: %s", __LINE__, gpg_strerror (rc));
63
64 /* Set REPLACE and GNUPGHOME in the parent process. */
65 rc = gpgrt_setenv ("REPLACE", "0", 1);
66 if (rc)
67 fail ("gpgrt_setenv failed at %d: %s", __LINE__, gpg_strerror (rc));
68 rc = gpgrt_setenv ("GNUPGHOME", "/tmp/test", 1);
69 if (rc)
70 fail ("gpgrt_setenv failed at %d: %s", __LINE__, gpg_strerror (rc));
71
72 rc = gpgrt_spawn_actions_new (&act);
73 if (rc)
74 fail ("gpgrt_spawn_actions_new failed at %d: %s",
75 __LINE__, gpg_strerror (rc));
76
77 gpgrt_spawn_actions_set_env_rev (act, envchange);
78
79 rc = gpgrt_process_spawn (progname, argv1,
80 (GPGRT_PROCESS_STDIN_KEEP
81 | GPGRT_PROCESS_STDOUT_PIPE
82 | GPGRT_PROCESS_STDERR_KEEP),
83 act, &process);
84 if (rc)
85 fail ("gpgrt_process_spawn failed at %d: %s", __LINE__, gpg_strerror (rc));
86
87 gpgrt_spawn_actions_release (act);
88
89 rc = gpgrt_process_get_streams (process, 0, NULL, &fp_out, NULL);
90 if (rc)
91 fail ("gpgrt_process_get_streams failed at %d: %s",
92 __LINE__, gpg_strerror (rc));
93
94 ok_add = ok_replace = 0;
95 ok_remove = 1;
96
97 while (gpgrt_fgets (line, DIM (line), fp_out))
98 {
99 #ifdef HAVE_W32_SYSTEM
100 { /* Take care of CRLF. */
101 char *p = strchr (line, '\r');
102 if (p)
103 *p = '\n';
104 }
105 #endif
106 if (!strncmp (line, "ADD=", 3))
107 {
108 if (!strncmp (line+4, "0\n", 2))
109 ok_add = 1;
110 }
111 if (!strncmp (line, "REPLACE=", 8))
112 {
113 if (!strncmp (line+8, "1\n", 2))
114 ok_replace = 1;
115 }
116 if (!strncmp (line, "GNUPGHOME=", 10))
117 ok_remove = 0;
118 }
119
120 r = gpgrt_fclose (fp_out);
121 if (r)
122 fail ("gpgrt_fclose failed at %d: %d", __LINE__, r);
123
124 rc = gpgrt_process_wait (process, 1);
125 if (rc)
126 fail ("gpgrt_process_wait failed at %d: %s", __LINE__, gpg_strerror (rc));
127
128 gpgrt_process_release (process);
129
130 if (verbose)
131 show ("Result: add=%s, replace=%s, remove=%s\n",
132 ok_add? "OK" : "FAIL", ok_replace? "OK" : "FAIL",
133 ok_remove? "OK" : "FAIL");
134
135 if (!ok_add)
136 fail ("ADD failed");
137 if (!ok_replace)
138 fail ("REPLACE failed");
139 if (!ok_remove)
140 fail ("GNUPGHOME failed");
141 }
142
143
144 int
145 main (int argc, char **argv)
146 {
147 int last_argc = -1;
148 int child = 0;
149 const char *progname = argv[0];
150
151 if (argc)
152 {
153 argc--; argv++;
154 }
155 while (argc && last_argc != argc )
156 {
157 last_argc = argc;
158 if (!strcmp (*argv, "--help"))
159 {
160 puts (
161 "usage: ./t-spawn [options]\n"
162 "\n"
163 "Options:\n"
164 " --verbose Show what is going on\n"
165 " --debug Flyswatter\n"
166 " --child Internal use for the child process\n"
167 );
168 exit (0);
169 }
170 if (!strcmp (*argv, "--verbose"))
171 {
172 verbose = 1;
173 argc--; argv++;
174 }
175 else if (!strcmp (*argv, "--debug"))
176 {
177 verbose = debug = 1;
178 argc--; argv++;
179 }
180 else if (!strcmp (*argv, "--child"))
181 {
182 child = 1;
183 argc--; argv++;
184 }
185 }
186
187 if (!gpg_error_check_version (GPG_ERROR_VERSION))
188 {
189 die ("gpg_error_check_version returned an error");
190 errorcount++;
191 }
192
193 if (child)
194 {
195 char *e;
196
197 e = gpgrt_getenv ("ADD");
198 if (e)
199 {
200 printf ("ADD=%s\n", e);
201 gpgrt_free (e);
202 }
203 e = gpgrt_getenv ("REPLACE");
204 if (e)
205 {
206 printf ("REPLACE=%s\n", e);
207 gpgrt_free (e);
208 }
209 e = gpgrt_getenv ("GNUPGHOME");
210 if (e)
211 {
212 printf ("GNUPGHOME=%s\n", e);
213 gpgrt_free (e);
214 }
215 return 0;
216 }
217
218 run_test (progname);
219 return errorcount ? 1 : 0;
220 }