aboutsummaryrefslogtreecommitdiffstats
path: root/libexec_setenvf_append.c
blob: 9c89a0eef12bd0a6a1a01d8986e1ea8a1cbccb0c (plain) (blame)
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
198
199
200
201
202
203
204
205
206
207
208
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


extern inline int libexec_setenvf_append(struct libexec_command *, const char *, const char *, ...);


#else


int
main(void)
{
	struct libexec_command cmd, ref;
	char **env;

	errno = 0;
	ASSERT_EQ_INT(libexec_setenvf_append(NULL, "X", "Y"), -1);
	ASSERT_EQ_INT(errno, EINVAL);

	cmd = LIBEXEC_COMMAND_INIT;
	memcpy(&ref, &cmd, sizeof(cmd));

	errno = 0;
	ASSERT_EQ_INT(libexec_setenvf_append(&cmd, "A=B", "C"), -1);
	ASSERT_EQ_INT(errno, EINVAL);
	ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));

	errno = 0;
	ASSERT_EQ_INT(libexec_setenvf_append(&cmd, "A", NULL), -1);
	ASSERT_EQ_INT(errno, EINVAL);
	ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));

	errno = 0;
	ASSERT_EQ_INT(libexec_setenvf_append(&cmd, NULL, "B"), -1);
	ASSERT_EQ_INT(errno, EINVAL);
	ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));

	ASSERT_ZERO(libexec_clear_environ(&cmd));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_IS_NULL(cmd.environ[0]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "A", "%s=%s", "B", "C"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_IS_NULL(cmd.environ[1]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "A", "%s", "D"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=D");
	ASSERT_IS_NULL(cmd.environ[2]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "A", "%c", 'D'));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=D");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "A=D");
	ASSERT_IS_NULL(cmd.environ[3]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "AA", "E"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=D");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "A=D");
	ASSERT_NOT_NULL(cmd.environ[3]);
	ASSERT_EQ_STR(cmd.environ[3], "AA=E");
	ASSERT_IS_NULL(cmd.environ[4]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "BB", "F"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=D");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "A=D");
	ASSERT_NOT_NULL(cmd.environ[3]);
	ASSERT_EQ_STR(cmd.environ[3], "AA=E");
	ASSERT_NOT_NULL(cmd.environ[4]);
	ASSERT_EQ_STR(cmd.environ[4], "BB=F");
	ASSERT_IS_NULL(cmd.environ[5]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "B", "F"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=D");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "A=D");
	ASSERT_NOT_NULL(cmd.environ[3]);
	ASSERT_EQ_STR(cmd.environ[3], "AA=E");
	ASSERT_NOT_NULL(cmd.environ[4]);
	ASSERT_EQ_STR(cmd.environ[4], "BB=F");
	ASSERT_NOT_NULL(cmd.environ[5]);
	ASSERT_EQ_STR(cmd.environ[5], "B=F");
	ASSERT_IS_NULL(cmd.environ[6]);

	env = cmd.environ;
	cmd.environ = NULL;
	ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));
	cmd.environ = env;

	libexec_destroy_command(&cmd);

	cmd = LIBEXEC_COMMAND_INIT;
	env = environ;
	environ = calloc(2, sizeof(*environ));
	ASSERT_NOT_NULL(environ);
	environ[0] = strdup("X=0");
	ASSERT_NOT_NULL(environ[0]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "A", "%s", "B=C"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_IS_NULL(cmd.environ[2]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "A", "D"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "A=D");
	ASSERT_IS_NULL(cmd.environ[3]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "A", "D"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "A=D");
	ASSERT_NOT_NULL(cmd.environ[3]);
	ASSERT_EQ_STR(cmd.environ[3], "A=D");
	ASSERT_IS_NULL(cmd.environ[4]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "AA", "E"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "A=D");
	ASSERT_NOT_NULL(cmd.environ[3]);
	ASSERT_EQ_STR(cmd.environ[3], "A=D");
	ASSERT_NOT_NULL(cmd.environ[4]);
	ASSERT_EQ_STR(cmd.environ[4], "AA=E");
	ASSERT_IS_NULL(cmd.environ[5]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "BB", "F"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "A=D");
	ASSERT_NOT_NULL(cmd.environ[3]);
	ASSERT_EQ_STR(cmd.environ[3], "A=D");
	ASSERT_NOT_NULL(cmd.environ[4]);
	ASSERT_EQ_STR(cmd.environ[4], "AA=E");
	ASSERT_NOT_NULL(cmd.environ[5]);
	ASSERT_EQ_STR(cmd.environ[5], "BB=F");
	ASSERT_IS_NULL(cmd.environ[6]);

	ASSERT_ZERO(libexec_setenvf_append(&cmd, "B", "F"));
	ASSERT_NOT_NULL(cmd.environ);
	ASSERT_NOT_NULL(cmd.environ[0]);
	ASSERT_EQ_STR(cmd.environ[0], "X=0");
	ASSERT_NOT_NULL(cmd.environ[1]);
	ASSERT_EQ_STR(cmd.environ[1], "A=B=C");
	ASSERT_NOT_NULL(cmd.environ[2]);
	ASSERT_EQ_STR(cmd.environ[2], "A=D");
	ASSERT_NOT_NULL(cmd.environ[3]);
	ASSERT_EQ_STR(cmd.environ[3], "A=D");
	ASSERT_NOT_NULL(cmd.environ[4]);
	ASSERT_EQ_STR(cmd.environ[4], "AA=E");
	ASSERT_NOT_NULL(cmd.environ[5]);
	ASSERT_EQ_STR(cmd.environ[5], "BB=F");
	ASSERT_NOT_NULL(cmd.environ[6]);
	ASSERT_EQ_STR(cmd.environ[6], "B=F");
	ASSERT_IS_NULL(cmd.environ[7]);

	libexec_destroy_command(&cmd);
	environ = env;
	return 0;
}


#endif