Merge lp:~jamesodhunt/upstart/fix-job_find into lp:upstart
- fix-job_find
- Merge into trunk
Proposed by
James Hunt
Status: | Merged |
---|---|
Merged at revision: | 1599 |
Proposed branch: | lp:~jamesodhunt/upstart/fix-job_find |
Merge into: | lp:upstart |
Diff against target: |
877 lines (+708/-20) 6 files modified
ChangeLog (+24/-0) init/control.c (+29/-13) init/control.h (+6/-5) init/job.c (+3/-0) init/tests/test_control.c (+527/-2) init/tests/test_job.c (+119/-0) |
To merge this branch: | bzr merge lp:~jamesodhunt/upstart/fix-job_find |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Upstart Reviewers | Pending | ||
Review via email: mp+195632@code.launchpad.net |
Commit message
Description of the change
Fixes a bug that could cause a Session Init to crash.
To post a comment you must log in.
- 1574. By James Hunt
-
* Sync with lp:upstart.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'ChangeLog' | |||
2 | --- ChangeLog 2013-11-18 10:31:21 +0000 | |||
3 | +++ ChangeLog 2013-11-18 16:45:10 +0000 | |||
4 | @@ -1,3 +1,27 @@ | |||
5 | 1 | 2013-11-18 James Hunt <james.hunt@ubuntu.com> | ||
6 | 2 | |||
7 | 3 | * init/control.c: | ||
8 | 4 | - control_set_env(): | ||
9 | 5 | - Check permissions before anything else. | ||
10 | 6 | - Explicit check on @var rather than an assert. | ||
11 | 7 | - control_unset_env(): Explicit check on @name rather than an assert. | ||
12 | 8 | - control_get_env(): Explicit check on @name. | ||
13 | 9 | - control_reset_env(): Check permissions before anything else. | ||
14 | 10 | * init/control.h: control_get_job(): | ||
15 | 11 | - Pass @job_name to job_find() rather than hard-coded NULL. | ||
16 | 12 | - Handle instance being NULL when raising NIH D-Bus error. | ||
17 | 13 | * init/job.c: job_find(): Handle NULL job_name. | ||
18 | 14 | * init/tests/test_control.c: | ||
19 | 15 | - Typo. | ||
20 | 16 | - New functions: | ||
21 | 17 | - test_list_env(). | ||
22 | 18 | - test_list_env(). | ||
23 | 19 | - test_get_env(). | ||
24 | 20 | - test_set_env(). | ||
25 | 21 | - test_unset_env(). | ||
26 | 22 | - test_reset_env(). | ||
27 | 23 | * init/tests/test_job.c: test_job_find(): New function. | ||
28 | 24 | |||
29 | 1 | 2013-11-16 Dmitrijs Ledkovs <xnox@ubuntu.com> | 25 | 2013-11-16 Dmitrijs Ledkovs <xnox@ubuntu.com> |
30 | 2 | 26 | ||
31 | 3 | * init/xdg.c, util/Makefile.am, test/Makefile.am, init/conf.c: | 27 | * init/xdg.c, util/Makefile.am, test/Makefile.am, init/conf.c: |
32 | 4 | 28 | ||
33 | === modified file 'init/control.c' | |||
34 | --- init/control.c 2013-11-05 16:15:19 +0000 | |||
35 | +++ init/control.c 2013-11-18 16:45:10 +0000 | |||
36 | @@ -1288,7 +1288,13 @@ | |||
37 | 1288 | 1288 | ||
38 | 1289 | nih_assert (message); | 1289 | nih_assert (message); |
39 | 1290 | nih_assert (job_details); | 1290 | nih_assert (job_details); |
41 | 1291 | nih_assert (var); | 1291 | |
42 | 1292 | if (! control_check_permission (message)) { | ||
43 | 1293 | nih_dbus_error_raise_printf ( | ||
44 | 1294 | DBUS_INTERFACE_UPSTART ".Error.PermissionDenied", | ||
45 | 1295 | _("You do not have permission to modify job environment")); | ||
46 | 1296 | return -1; | ||
47 | 1297 | } | ||
48 | 1292 | 1298 | ||
49 | 1293 | if (job_details[0]) { | 1299 | if (job_details[0]) { |
50 | 1294 | job_name = job_details[0]; | 1300 | job_name = job_details[0]; |
51 | @@ -1302,10 +1308,9 @@ | |||
52 | 1302 | return -1; | 1308 | return -1; |
53 | 1303 | } | 1309 | } |
54 | 1304 | 1310 | ||
59 | 1305 | if (! control_check_permission (message)) { | 1311 | if (! var || ! *var) { |
60 | 1306 | nih_dbus_error_raise_printf ( | 1312 | nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS, |
61 | 1307 | DBUS_INTERFACE_UPSTART ".Error.PermissionDenied", | 1313 | _("Variable may not be empty string")); |
58 | 1308 | _("You do not have permission to modify job environment")); | ||
62 | 1309 | return -1; | 1314 | return -1; |
63 | 1310 | } | 1315 | } |
64 | 1311 | 1316 | ||
65 | @@ -1386,7 +1391,6 @@ | |||
66 | 1386 | 1391 | ||
67 | 1387 | nih_assert (message); | 1392 | nih_assert (message); |
68 | 1388 | nih_assert (job_details); | 1393 | nih_assert (job_details); |
69 | 1389 | nih_assert (name); | ||
70 | 1390 | 1394 | ||
71 | 1391 | if (! control_check_permission (message)) { | 1395 | if (! control_check_permission (message)) { |
72 | 1392 | nih_dbus_error_raise_printf ( | 1396 | nih_dbus_error_raise_printf ( |
73 | @@ -1395,6 +1399,12 @@ | |||
74 | 1395 | return -1; | 1399 | return -1; |
75 | 1396 | } | 1400 | } |
76 | 1397 | 1401 | ||
77 | 1402 | if (! name || ! *name) { | ||
78 | 1403 | nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS, | ||
79 | 1404 | _("Variable may not be empty string")); | ||
80 | 1405 | return -1; | ||
81 | 1406 | } | ||
82 | 1407 | |||
83 | 1398 | if (job_details[0]) { | 1408 | if (job_details[0]) { |
84 | 1399 | job_name = job_details[0]; | 1409 | job_name = job_details[0]; |
85 | 1400 | 1410 | ||
86 | @@ -1490,6 +1500,12 @@ | |||
87 | 1490 | return -1; | 1500 | return -1; |
88 | 1491 | } | 1501 | } |
89 | 1492 | 1502 | ||
90 | 1503 | if (! name || ! *name) { | ||
91 | 1504 | nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS, | ||
92 | 1505 | _("Variable may not be empty string")); | ||
93 | 1506 | return -1; | ||
94 | 1507 | } | ||
95 | 1508 | |||
96 | 1493 | if (job_details[0]) { | 1509 | if (job_details[0]) { |
97 | 1494 | job_name = job_details[0]; | 1510 | job_name = job_details[0]; |
98 | 1495 | 1511 | ||
99 | @@ -1649,6 +1665,13 @@ | |||
100 | 1649 | nih_assert (message); | 1665 | nih_assert (message); |
101 | 1650 | nih_assert (job_details); | 1666 | nih_assert (job_details); |
102 | 1651 | 1667 | ||
103 | 1668 | if (! control_check_permission (message)) { | ||
104 | 1669 | nih_dbus_error_raise_printf ( | ||
105 | 1670 | DBUS_INTERFACE_UPSTART ".Error.PermissionDenied", | ||
106 | 1671 | _("You do not have permission to modify job environment")); | ||
107 | 1672 | return -1; | ||
108 | 1673 | } | ||
109 | 1674 | |||
110 | 1652 | if (job_details[0]) { | 1675 | if (job_details[0]) { |
111 | 1653 | job_name = job_details[0]; | 1676 | job_name = job_details[0]; |
112 | 1654 | 1677 | ||
113 | @@ -1661,13 +1684,6 @@ | |||
114 | 1661 | return -1; | 1684 | return -1; |
115 | 1662 | } | 1685 | } |
116 | 1663 | 1686 | ||
117 | 1664 | if (! control_check_permission (message)) { | ||
118 | 1665 | nih_dbus_error_raise_printf ( | ||
119 | 1666 | DBUS_INTERFACE_UPSTART ".Error.PermissionDenied", | ||
120 | 1667 | _("You do not have permission to modify job environment")); | ||
121 | 1668 | return -1; | ||
122 | 1669 | } | ||
123 | 1670 | |||
124 | 1671 | /* Verify that job name is valid */ | 1687 | /* Verify that job name is valid */ |
125 | 1672 | if (job_name && ! strlen (job_name)) { | 1688 | if (job_name && ! strlen (job_name)) { |
126 | 1673 | nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS, | 1689 | nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS, |
127 | 1674 | 1690 | ||
128 | === modified file 'init/control.h' | |||
129 | --- init/control.h 2013-10-25 13:49:49 +0000 | |||
130 | +++ init/control.h 2013-11-18 16:45:10 +0000 | |||
131 | @@ -1,6 +1,6 @@ | |||
132 | 1 | /* upstart | 1 | /* upstart |
133 | 2 | * | 2 | * |
135 | 3 | * Copyright 2009-2011 Canonical Ltd. | 3 | * Copyright © 2009-2011 Canonical Ltd. |
136 | 4 | * Author: Scott James Remnant <scott@netsplit.com>. | 4 | * Author: Scott James Remnant <scott@netsplit.com>. |
137 | 5 | * | 5 | * |
138 | 6 | * This program is free software; you can redistribute it and/or modify | 6 | * This program is free software; you can redistribute it and/or modify |
139 | @@ -60,7 +60,7 @@ | |||
140 | 60 | **/ | 60 | **/ |
141 | 61 | #define control_get_job(session, job, job_name, instance) \ | 61 | #define control_get_job(session, job, job_name, instance) \ |
142 | 62 | { \ | 62 | { \ |
144 | 63 | if (job_name != NULL ) { \ | 63 | if (job_name) { \ |
145 | 64 | JobClass *class; \ | 64 | JobClass *class; \ |
146 | 65 | \ | 65 | \ |
147 | 66 | class = job_class_get_registered (job_name, session); \ | 66 | class = job_class_get_registered (job_name, session); \ |
148 | @@ -73,13 +73,14 @@ | |||
149 | 73 | return -1; \ | 73 | return -1; \ |
150 | 74 | } \ | 74 | } \ |
151 | 75 | \ | 75 | \ |
154 | 76 | job = job_find (session, class, NULL, instance); \ | 76 | job = job_find (session, class, job_name, instance); \ |
155 | 77 | if (job == NULL) { \ | 77 | if (! job) { \ |
156 | 78 | nih_dbus_error_raise_printf ( \ | 78 | nih_dbus_error_raise_printf ( \ |
157 | 79 | DBUS_INTERFACE_UPSTART \ | 79 | DBUS_INTERFACE_UPSTART \ |
158 | 80 | ".Error.UnknownJobInstance", \ | 80 | ".Error.UnknownJobInstance", \ |
159 | 81 | _("Unknown instance: %s of job %s"), \ | 81 | _("Unknown instance: %s of job %s"), \ |
161 | 82 | instance, job_name); \ | 82 | instance ? instance : "(null)", \ |
162 | 83 | job_name); \ | ||
163 | 83 | return -1; \ | 84 | return -1; \ |
164 | 84 | } \ | 85 | } \ |
165 | 85 | } \ | 86 | } \ |
166 | 86 | 87 | ||
167 | === modified file 'init/job.c' | |||
168 | --- init/job.c 2013-08-21 11:07:36 +0000 | |||
169 | +++ init/job.c 2013-11-18 16:45:10 +0000 | |||
170 | @@ -2350,6 +2350,9 @@ | |||
171 | 2350 | nih_assert (class || job_class); | 2350 | nih_assert (class || job_class); |
172 | 2351 | nih_assert (job_classes); | 2351 | nih_assert (job_classes); |
173 | 2352 | 2352 | ||
174 | 2353 | if (! job_name) | ||
175 | 2354 | goto error; | ||
176 | 2355 | |||
177 | 2353 | if (! class) | 2356 | if (! class) |
178 | 2354 | class = job_class_get_registered (job_class, session); | 2357 | class = job_class_get_registered (job_class, session); |
179 | 2355 | 2358 | ||
180 | 2356 | 2359 | ||
181 | === modified file 'init/tests/test_control.c' | |||
182 | --- init/tests/test_control.c 2013-10-25 13:49:49 +0000 | |||
183 | +++ init/tests/test_control.c 2013-11-18 16:45:10 +0000 | |||
184 | @@ -61,9 +61,10 @@ | |||
185 | 61 | #include "control.h" | 61 | #include "control.h" |
186 | 62 | #include "errors.h" | 62 | #include "errors.h" |
187 | 63 | 63 | ||
188 | 64 | #include "test_util_common.h" | ||
189 | 64 | 65 | ||
190 | 65 | extern const char *control_server_address; | 66 | extern const char *control_server_address; |
192 | 66 | 67 | extern int no_inherit_env; | |
193 | 67 | 68 | ||
194 | 68 | void | 69 | void |
195 | 69 | test_server_open (void) | 70 | test_server_open (void) |
196 | @@ -933,7 +934,7 @@ | |||
197 | 933 | strcat (filename, "/baz"); | 934 | strcat (filename, "/baz"); |
198 | 934 | 935 | ||
199 | 935 | /* XXX: note that this will generate an error message when this | 936 | /* XXX: note that this will generate an error message when this |
201 | 936 | * test runs sine "/baz" does not exist as a directory. | 937 | * test runs since "/baz" does not exist as a directory. |
202 | 937 | */ | 938 | */ |
203 | 938 | source3 = conf_source_new (NULL, filename, CONF_DIR); | 939 | source3 = conf_source_new (NULL, filename, CONF_DIR); |
204 | 939 | 940 | ||
205 | @@ -2176,6 +2177,524 @@ | |||
206 | 2176 | nih_log_priority = NIH_LOG_UNKNOWN; | 2177 | nih_log_priority = NIH_LOG_UNKNOWN; |
207 | 2177 | } | 2178 | } |
208 | 2178 | 2179 | ||
209 | 2180 | void | ||
210 | 2181 | test_list_env (void) | ||
211 | 2182 | { | ||
212 | 2183 | NihDBusMessage *message = NULL; | ||
213 | 2184 | int ret; | ||
214 | 2185 | nih_local char **env = NULL; | ||
215 | 2186 | nih_local char **job_details = NULL; | ||
216 | 2187 | nih_local char **job_details2 = NULL; | ||
217 | 2188 | JobClass *class; | ||
218 | 2189 | Job *job; | ||
219 | 2190 | NihError *error; | ||
220 | 2191 | |||
221 | 2192 | TEST_FUNCTION ("control_list_env"); | ||
222 | 2193 | |||
223 | 2194 | nih_error_init (); | ||
224 | 2195 | job_class_init (); | ||
225 | 2196 | job_class_environment_init (); | ||
226 | 2197 | |||
227 | 2198 | no_inherit_env = TRUE; | ||
228 | 2199 | |||
229 | 2200 | message = nih_new (NULL, NihDBusMessage); | ||
230 | 2201 | TEST_NE_P (message, NULL); | ||
231 | 2202 | message->connection = NULL; | ||
232 | 2203 | message->message = NULL; | ||
233 | 2204 | |||
234 | 2205 | job_details = nih_str_array_new (NULL); | ||
235 | 2206 | nih_assert (job_details); | ||
236 | 2207 | |||
237 | 2208 | class = job_class_new (NULL, "foo", NULL); | ||
238 | 2209 | TEST_NE_P (class, NULL); | ||
239 | 2210 | |||
240 | 2211 | job = job_new (class, ""); | ||
241 | 2212 | TEST_NE_P (job, NULL); | ||
242 | 2213 | |||
243 | 2214 | nih_hash_add (job_classes, &class->entry); | ||
244 | 2215 | |||
245 | 2216 | assert0 (job_class_environment_set ("hello=world", TRUE)); | ||
246 | 2217 | |||
247 | 2218 | /************************************************************/ | ||
248 | 2219 | TEST_FEATURE ("with empty array"); | ||
249 | 2220 | |||
250 | 2221 | ret = control_list_env (NULL, message, job_details, &env); | ||
251 | 2222 | TEST_EQ (ret, 0); | ||
252 | 2223 | |||
253 | 2224 | /* Default variables that Upstart adds */ | ||
254 | 2225 | TEST_STR_MATCH (env[0], "PATH=*"); | ||
255 | 2226 | TEST_STR_MATCH (env[1], "TERM=*"); | ||
256 | 2227 | |||
257 | 2228 | TEST_EQ_STR (env[2], "hello=world"); | ||
258 | 2229 | TEST_EQ_P (env[3], NULL); | ||
259 | 2230 | |||
260 | 2231 | /************************************************************/ | ||
261 | 2232 | TEST_FEATURE ("with empty job class name"); | ||
262 | 2233 | |||
263 | 2234 | job_details2 = nih_str_array_copy (NULL, NULL, job_details); | ||
264 | 2235 | TEST_NE_P (job_details2, NULL); | ||
265 | 2236 | |||
266 | 2237 | nih_assert (nih_str_array_add (&job_details2, | ||
267 | 2238 | NULL, | ||
268 | 2239 | NULL, | ||
269 | 2240 | "")); | ||
270 | 2241 | |||
271 | 2242 | ret = control_list_env (NULL, message, job_details2, &env); | ||
272 | 2243 | TEST_LT (ret, 0); | ||
273 | 2244 | |||
274 | 2245 | error = nih_error_get (); | ||
275 | 2246 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
276 | 2247 | nih_free (error); | ||
277 | 2248 | |||
278 | 2249 | /************************************************************/ | ||
279 | 2250 | TEST_FEATURE ("with missing instance name"); | ||
280 | 2251 | |||
281 | 2252 | /* add name */ | ||
282 | 2253 | nih_assert (nih_str_array_add (&job_details, | ||
283 | 2254 | NULL, | ||
284 | 2255 | NULL, | ||
285 | 2256 | "foo")); | ||
286 | 2257 | |||
287 | 2258 | ret = control_list_env (NULL, message, job_details, &env); | ||
288 | 2259 | TEST_LT (ret, 0); | ||
289 | 2260 | |||
290 | 2261 | error = nih_error_get (); | ||
291 | 2262 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
292 | 2263 | nih_free (error); | ||
293 | 2264 | |||
294 | 2265 | /************************************************************/ | ||
295 | 2266 | /* We can't wrap this in a TEST_ALLOC_FAIL() block since the | ||
296 | 2267 | * nih_dbus_*() calls such as nih_dbus_error_raise_printf() use | ||
297 | 2268 | * NIH_MUST() which defeats the fail macro | ||
298 | 2269 | */ | ||
299 | 2270 | TEST_FEATURE ("with correct job details"); | ||
300 | 2271 | |||
301 | 2272 | /* add instance */ | ||
302 | 2273 | nih_assert (nih_str_array_add (&job_details, | ||
303 | 2274 | NULL, | ||
304 | 2275 | NULL, | ||
305 | 2276 | "")); | ||
306 | 2277 | |||
307 | 2278 | ret = control_list_env (NULL, message, job_details, &env); | ||
308 | 2279 | TEST_EQ (ret, 0); | ||
309 | 2280 | |||
310 | 2281 | nih_free (message); | ||
311 | 2282 | } | ||
312 | 2283 | |||
313 | 2284 | void | ||
314 | 2285 | test_get_env (void) | ||
315 | 2286 | { | ||
316 | 2287 | NihDBusMessage *message = NULL; | ||
317 | 2288 | int ret; | ||
318 | 2289 | char *value = NULL; | ||
319 | 2290 | nih_local char **job_details = NULL; | ||
320 | 2291 | nih_local char **job_details2 = NULL; | ||
321 | 2292 | JobClass *class; | ||
322 | 2293 | Job *job; | ||
323 | 2294 | NihError *error; | ||
324 | 2295 | |||
325 | 2296 | TEST_FUNCTION ("control_get_env"); | ||
326 | 2297 | |||
327 | 2298 | nih_error_init (); | ||
328 | 2299 | job_class_init (); | ||
329 | 2300 | job_class_environment_init (); | ||
330 | 2301 | |||
331 | 2302 | no_inherit_env = TRUE; | ||
332 | 2303 | |||
333 | 2304 | message = nih_new (NULL, NihDBusMessage); | ||
334 | 2305 | TEST_NE_P (message, NULL); | ||
335 | 2306 | message->connection = NULL; | ||
336 | 2307 | message->message = NULL; | ||
337 | 2308 | |||
338 | 2309 | job_details = nih_str_array_new (NULL); | ||
339 | 2310 | nih_assert (job_details); | ||
340 | 2311 | |||
341 | 2312 | job_details2 = nih_str_array_copy (NULL, NULL, job_details); | ||
342 | 2313 | TEST_NE_P (job_details2, NULL); | ||
343 | 2314 | |||
344 | 2315 | nih_assert (nih_str_array_add (&job_details2, | ||
345 | 2316 | NULL, | ||
346 | 2317 | NULL, | ||
347 | 2318 | "")); | ||
348 | 2319 | |||
349 | 2320 | class = job_class_new (NULL, "foo", NULL); | ||
350 | 2321 | TEST_NE_P (class, NULL); | ||
351 | 2322 | |||
352 | 2323 | job = job_new (class, ""); | ||
353 | 2324 | TEST_NE_P (job, NULL); | ||
354 | 2325 | |||
355 | 2326 | nih_hash_add (job_classes, &class->entry); | ||
356 | 2327 | |||
357 | 2328 | assert0 (job_class_environment_set ("hello=world", TRUE)); | ||
358 | 2329 | |||
359 | 2330 | /************************************************************/ | ||
360 | 2331 | TEST_FEATURE ("with NULL variable name"); | ||
361 | 2332 | |||
362 | 2333 | ret = control_get_env (NULL, message, job_details2, NULL, &value); | ||
363 | 2334 | TEST_LT (ret, 0); | ||
364 | 2335 | |||
365 | 2336 | error = nih_error_get (); | ||
366 | 2337 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
367 | 2338 | nih_free (error); | ||
368 | 2339 | |||
369 | 2340 | /************************************************************/ | ||
370 | 2341 | TEST_FEATURE ("with empty variable name"); | ||
371 | 2342 | |||
372 | 2343 | ret = control_get_env (NULL, message, job_details2, "", &value); | ||
373 | 2344 | TEST_LT (ret, 0); | ||
374 | 2345 | |||
375 | 2346 | error = nih_error_get (); | ||
376 | 2347 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
377 | 2348 | nih_free (error); | ||
378 | 2349 | |||
379 | 2350 | /************************************************************/ | ||
380 | 2351 | TEST_FEATURE ("with empty job class name"); | ||
381 | 2352 | |||
382 | 2353 | ret = control_get_env (NULL, message, job_details2, "hello", &value); | ||
383 | 2354 | TEST_LT (ret, 0); | ||
384 | 2355 | |||
385 | 2356 | error = nih_error_get (); | ||
386 | 2357 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
387 | 2358 | nih_free (error); | ||
388 | 2359 | |||
389 | 2360 | /************************************************************/ | ||
390 | 2361 | TEST_FEATURE ("with missing instance name"); | ||
391 | 2362 | |||
392 | 2363 | /* add name */ | ||
393 | 2364 | nih_assert (nih_str_array_add (&job_details, | ||
394 | 2365 | NULL, | ||
395 | 2366 | NULL, | ||
396 | 2367 | "foo")); | ||
397 | 2368 | |||
398 | 2369 | ret = control_get_env (NULL, message, job_details, "hello", &value); | ||
399 | 2370 | TEST_LT (ret, 0); | ||
400 | 2371 | |||
401 | 2372 | error = nih_error_get (); | ||
402 | 2373 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
403 | 2374 | nih_free (error); | ||
404 | 2375 | |||
405 | 2376 | /************************************************************/ | ||
406 | 2377 | TEST_FEATURE ("with correct job details"); | ||
407 | 2378 | |||
408 | 2379 | /* add instance */ | ||
409 | 2380 | nih_assert (nih_str_array_add (&job_details, | ||
410 | 2381 | NULL, | ||
411 | 2382 | NULL, | ||
412 | 2383 | "")); | ||
413 | 2384 | |||
414 | 2385 | ret = control_get_env (NULL, message, job_details, "hello", &value); | ||
415 | 2386 | TEST_EQ (ret, 0); | ||
416 | 2387 | TEST_EQ_STR (value, "world"); | ||
417 | 2388 | |||
418 | 2389 | /************************************************************/ | ||
419 | 2390 | /* tidy up */ | ||
420 | 2391 | |||
421 | 2392 | nih_free (value); | ||
422 | 2393 | nih_free (message); | ||
423 | 2394 | } | ||
424 | 2395 | |||
425 | 2396 | void | ||
426 | 2397 | test_set_env (void) | ||
427 | 2398 | { | ||
428 | 2399 | NihDBusMessage *message = NULL; | ||
429 | 2400 | int ret; | ||
430 | 2401 | nih_local char **job_details = NULL; | ||
431 | 2402 | nih_local char **job_details2 = NULL; | ||
432 | 2403 | JobClass *class; | ||
433 | 2404 | Job *job; | ||
434 | 2405 | NihError *error; | ||
435 | 2406 | |||
436 | 2407 | TEST_FUNCTION ("control_set_env"); | ||
437 | 2408 | |||
438 | 2409 | nih_error_init (); | ||
439 | 2410 | job_class_init (); | ||
440 | 2411 | job_class_environment_init (); | ||
441 | 2412 | |||
442 | 2413 | no_inherit_env = TRUE; | ||
443 | 2414 | |||
444 | 2415 | message = nih_new (NULL, NihDBusMessage); | ||
445 | 2416 | TEST_NE_P (message, NULL); | ||
446 | 2417 | message->connection = NULL; | ||
447 | 2418 | message->message = NULL; | ||
448 | 2419 | |||
449 | 2420 | job_details = nih_str_array_new (NULL); | ||
450 | 2421 | nih_assert (job_details); | ||
451 | 2422 | |||
452 | 2423 | job_details2 = nih_str_array_copy (NULL, NULL, job_details); | ||
453 | 2424 | TEST_NE_P (job_details2, NULL); | ||
454 | 2425 | |||
455 | 2426 | nih_assert (nih_str_array_add (&job_details2, | ||
456 | 2427 | NULL, | ||
457 | 2428 | NULL, | ||
458 | 2429 | "")); | ||
459 | 2430 | |||
460 | 2431 | class = job_class_new (NULL, "foo", NULL); | ||
461 | 2432 | TEST_NE_P (class, NULL); | ||
462 | 2433 | |||
463 | 2434 | job = job_new (class, ""); | ||
464 | 2435 | TEST_NE_P (job, NULL); | ||
465 | 2436 | |||
466 | 2437 | nih_hash_add (job_classes, &class->entry); | ||
467 | 2438 | |||
468 | 2439 | /************************************************************/ | ||
469 | 2440 | TEST_FEATURE ("with NULL variable name"); | ||
470 | 2441 | |||
471 | 2442 | ret = control_set_env (NULL, message, job_details2, NULL, TRUE); | ||
472 | 2443 | TEST_LT (ret, 0); | ||
473 | 2444 | |||
474 | 2445 | error = nih_error_get (); | ||
475 | 2446 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
476 | 2447 | nih_free (error); | ||
477 | 2448 | |||
478 | 2449 | /************************************************************/ | ||
479 | 2450 | TEST_FEATURE ("with empty variable name"); | ||
480 | 2451 | |||
481 | 2452 | ret = control_set_env (NULL, message, job_details2, "", TRUE); | ||
482 | 2453 | TEST_LT (ret, 0); | ||
483 | 2454 | |||
484 | 2455 | error = nih_error_get (); | ||
485 | 2456 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
486 | 2457 | nih_free (error); | ||
487 | 2458 | |||
488 | 2459 | /************************************************************/ | ||
489 | 2460 | TEST_FEATURE ("with empty job class name"); | ||
490 | 2461 | |||
491 | 2462 | ret = control_set_env (NULL, message, job_details2, "hello=world", TRUE); | ||
492 | 2463 | TEST_LT (ret, 0); | ||
493 | 2464 | |||
494 | 2465 | error = nih_error_get (); | ||
495 | 2466 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
496 | 2467 | nih_free (error); | ||
497 | 2468 | |||
498 | 2469 | /************************************************************/ | ||
499 | 2470 | TEST_FEATURE ("with missing instance name"); | ||
500 | 2471 | |||
501 | 2472 | /* add name */ | ||
502 | 2473 | nih_assert (nih_str_array_add (&job_details, | ||
503 | 2474 | NULL, | ||
504 | 2475 | NULL, | ||
505 | 2476 | "foo")); | ||
506 | 2477 | |||
507 | 2478 | ret = control_set_env (NULL, message, job_details, "hello=world", TRUE); | ||
508 | 2479 | TEST_LT (ret, 0); | ||
509 | 2480 | |||
510 | 2481 | error = nih_error_get (); | ||
511 | 2482 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
512 | 2483 | nih_free (error); | ||
513 | 2484 | |||
514 | 2485 | /************************************************************/ | ||
515 | 2486 | TEST_FEATURE ("with correct job details"); | ||
516 | 2487 | |||
517 | 2488 | /* add instance */ | ||
518 | 2489 | nih_assert (nih_str_array_add (&job_details, | ||
519 | 2490 | NULL, | ||
520 | 2491 | NULL, | ||
521 | 2492 | "")); | ||
522 | 2493 | |||
523 | 2494 | ret = control_set_env (NULL, message, job_details, "hello=world", TRUE); | ||
524 | 2495 | TEST_EQ (ret, 0); | ||
525 | 2496 | |||
526 | 2497 | /************************************************************/ | ||
527 | 2498 | /* tidy up */ | ||
528 | 2499 | |||
529 | 2500 | nih_free (message); | ||
530 | 2501 | } | ||
531 | 2502 | |||
532 | 2503 | void | ||
533 | 2504 | test_unset_env (void) | ||
534 | 2505 | { | ||
535 | 2506 | NihDBusMessage *message = NULL; | ||
536 | 2507 | int ret; | ||
537 | 2508 | nih_local char **job_details = NULL; | ||
538 | 2509 | nih_local char **job_details2 = NULL; | ||
539 | 2510 | JobClass *class; | ||
540 | 2511 | Job *job; | ||
541 | 2512 | NihError *error; | ||
542 | 2513 | |||
543 | 2514 | TEST_FUNCTION ("control_unset_env"); | ||
544 | 2515 | |||
545 | 2516 | nih_error_init (); | ||
546 | 2517 | job_class_init (); | ||
547 | 2518 | job_class_environment_init (); | ||
548 | 2519 | |||
549 | 2520 | no_inherit_env = TRUE; | ||
550 | 2521 | |||
551 | 2522 | message = nih_new (NULL, NihDBusMessage); | ||
552 | 2523 | TEST_NE_P (message, NULL); | ||
553 | 2524 | message->connection = NULL; | ||
554 | 2525 | message->message = NULL; | ||
555 | 2526 | |||
556 | 2527 | job_details = nih_str_array_new (NULL); | ||
557 | 2528 | nih_assert (job_details); | ||
558 | 2529 | |||
559 | 2530 | job_details2 = nih_str_array_copy (NULL, NULL, job_details); | ||
560 | 2531 | TEST_NE_P (job_details2, NULL); | ||
561 | 2532 | |||
562 | 2533 | nih_assert (nih_str_array_add (&job_details2, | ||
563 | 2534 | NULL, | ||
564 | 2535 | NULL, | ||
565 | 2536 | "")); | ||
566 | 2537 | |||
567 | 2538 | class = job_class_new (NULL, "foo", NULL); | ||
568 | 2539 | TEST_NE_P (class, NULL); | ||
569 | 2540 | |||
570 | 2541 | job = job_new (class, ""); | ||
571 | 2542 | TEST_NE_P (job, NULL); | ||
572 | 2543 | |||
573 | 2544 | nih_hash_add (job_classes, &class->entry); | ||
574 | 2545 | |||
575 | 2546 | assert0 (job_class_environment_set ("hello=world", TRUE)); | ||
576 | 2547 | |||
577 | 2548 | /************************************************************/ | ||
578 | 2549 | TEST_FEATURE ("with NULL variable name"); | ||
579 | 2550 | |||
580 | 2551 | ret = control_unset_env (NULL, message, job_details2, NULL); | ||
581 | 2552 | TEST_LT (ret, 0); | ||
582 | 2553 | |||
583 | 2554 | error = nih_error_get (); | ||
584 | 2555 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
585 | 2556 | nih_free (error); | ||
586 | 2557 | |||
587 | 2558 | /************************************************************/ | ||
588 | 2559 | TEST_FEATURE ("with empty variable name"); | ||
589 | 2560 | |||
590 | 2561 | ret = control_unset_env (NULL, message, job_details2, ""); | ||
591 | 2562 | TEST_LT (ret, 0); | ||
592 | 2563 | |||
593 | 2564 | error = nih_error_get (); | ||
594 | 2565 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
595 | 2566 | nih_free (error); | ||
596 | 2567 | |||
597 | 2568 | /************************************************************/ | ||
598 | 2569 | TEST_FEATURE ("with empty job class name"); | ||
599 | 2570 | |||
600 | 2571 | ret = control_unset_env (NULL, message, job_details2, "hello"); | ||
601 | 2572 | TEST_LT (ret, 0); | ||
602 | 2573 | |||
603 | 2574 | error = nih_error_get (); | ||
604 | 2575 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
605 | 2576 | nih_free (error); | ||
606 | 2577 | |||
607 | 2578 | /************************************************************/ | ||
608 | 2579 | TEST_FEATURE ("with missing instance name"); | ||
609 | 2580 | |||
610 | 2581 | /* add name */ | ||
611 | 2582 | nih_assert (nih_str_array_add (&job_details, | ||
612 | 2583 | NULL, | ||
613 | 2584 | NULL, | ||
614 | 2585 | "foo")); | ||
615 | 2586 | |||
616 | 2587 | ret = control_unset_env (NULL, message, job_details, "hello"); | ||
617 | 2588 | TEST_LT (ret, 0); | ||
618 | 2589 | |||
619 | 2590 | error = nih_error_get (); | ||
620 | 2591 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
621 | 2592 | nih_free (error); | ||
622 | 2593 | |||
623 | 2594 | /************************************************************/ | ||
624 | 2595 | TEST_FEATURE ("with correct job details"); | ||
625 | 2596 | |||
626 | 2597 | /* add instance */ | ||
627 | 2598 | nih_assert (nih_str_array_add (&job_details, | ||
628 | 2599 | NULL, | ||
629 | 2600 | NULL, | ||
630 | 2601 | "")); | ||
631 | 2602 | |||
632 | 2603 | ret = control_unset_env (NULL, message, job_details, "hello"); | ||
633 | 2604 | TEST_EQ (ret, 0); | ||
634 | 2605 | |||
635 | 2606 | /************************************************************/ | ||
636 | 2607 | /* tidy up */ | ||
637 | 2608 | |||
638 | 2609 | nih_free (message); | ||
639 | 2610 | } | ||
640 | 2611 | |||
641 | 2612 | void | ||
642 | 2613 | test_reset_env (void) | ||
643 | 2614 | { | ||
644 | 2615 | NihDBusMessage *message = NULL; | ||
645 | 2616 | int ret; | ||
646 | 2617 | nih_local char **job_details = NULL; | ||
647 | 2618 | nih_local char **job_details2 = NULL; | ||
648 | 2619 | JobClass *class; | ||
649 | 2620 | Job *job; | ||
650 | 2621 | NihError *error; | ||
651 | 2622 | |||
652 | 2623 | TEST_FUNCTION ("control_reset_env"); | ||
653 | 2624 | |||
654 | 2625 | nih_error_init (); | ||
655 | 2626 | job_class_init (); | ||
656 | 2627 | job_class_environment_init (); | ||
657 | 2628 | |||
658 | 2629 | no_inherit_env = TRUE; | ||
659 | 2630 | |||
660 | 2631 | message = nih_new (NULL, NihDBusMessage); | ||
661 | 2632 | TEST_NE_P (message, NULL); | ||
662 | 2633 | message->connection = NULL; | ||
663 | 2634 | message->message = NULL; | ||
664 | 2635 | |||
665 | 2636 | job_details = nih_str_array_new (NULL); | ||
666 | 2637 | nih_assert (job_details); | ||
667 | 2638 | |||
668 | 2639 | class = job_class_new (NULL, "foo", NULL); | ||
669 | 2640 | TEST_NE_P (class, NULL); | ||
670 | 2641 | |||
671 | 2642 | job = job_new (class, ""); | ||
672 | 2643 | TEST_NE_P (job, NULL); | ||
673 | 2644 | |||
674 | 2645 | nih_hash_add (job_classes, &class->entry); | ||
675 | 2646 | |||
676 | 2647 | /************************************************************/ | ||
677 | 2648 | TEST_FEATURE ("with empty job class name"); | ||
678 | 2649 | |||
679 | 2650 | job_details2 = nih_str_array_copy (NULL, NULL, job_details); | ||
680 | 2651 | TEST_NE_P (job_details2, NULL); | ||
681 | 2652 | |||
682 | 2653 | nih_assert (nih_str_array_add (&job_details2, | ||
683 | 2654 | NULL, | ||
684 | 2655 | NULL, | ||
685 | 2656 | "")); | ||
686 | 2657 | |||
687 | 2658 | ret = control_reset_env (NULL, message, job_details2); | ||
688 | 2659 | TEST_LT (ret, 0); | ||
689 | 2660 | |||
690 | 2661 | error = nih_error_get (); | ||
691 | 2662 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
692 | 2663 | nih_free (error); | ||
693 | 2664 | |||
694 | 2665 | /************************************************************/ | ||
695 | 2666 | TEST_FEATURE ("with missing instance name"); | ||
696 | 2667 | |||
697 | 2668 | /* add name */ | ||
698 | 2669 | nih_assert (nih_str_array_add (&job_details, | ||
699 | 2670 | NULL, | ||
700 | 2671 | NULL, | ||
701 | 2672 | "foo")); | ||
702 | 2673 | |||
703 | 2674 | ret = control_reset_env (NULL, message, job_details); | ||
704 | 2675 | TEST_LT (ret, 0); | ||
705 | 2676 | |||
706 | 2677 | error = nih_error_get (); | ||
707 | 2678 | TEST_EQ (error->number, NIH_DBUS_ERROR); | ||
708 | 2679 | nih_free (error); | ||
709 | 2680 | |||
710 | 2681 | /************************************************************/ | ||
711 | 2682 | TEST_FEATURE ("with correct job details"); | ||
712 | 2683 | |||
713 | 2684 | /* add instance */ | ||
714 | 2685 | nih_assert (nih_str_array_add (&job_details, | ||
715 | 2686 | NULL, | ||
716 | 2687 | NULL, | ||
717 | 2688 | "")); | ||
718 | 2689 | |||
719 | 2690 | ret = control_reset_env (NULL, message, job_details); | ||
720 | 2691 | TEST_EQ (ret, 0); | ||
721 | 2692 | |||
722 | 2693 | /************************************************************/ | ||
723 | 2694 | /* tidy up */ | ||
724 | 2695 | |||
725 | 2696 | nih_free (message); | ||
726 | 2697 | } | ||
727 | 2179 | 2698 | ||
728 | 2180 | int | 2699 | int |
729 | 2181 | main (int argc, | 2700 | main (int argc, |
730 | @@ -2205,5 +2724,11 @@ | |||
731 | 2205 | test_get_log_priority (); | 2724 | test_get_log_priority (); |
732 | 2206 | test_set_log_priority (); | 2725 | test_set_log_priority (); |
733 | 2207 | 2726 | ||
734 | 2727 | test_list_env (); | ||
735 | 2728 | test_get_env (); | ||
736 | 2729 | test_set_env (); | ||
737 | 2730 | test_unset_env (); | ||
738 | 2731 | test_reset_env (); | ||
739 | 2732 | |||
740 | 2208 | return 0; | 2733 | return 0; |
741 | 2209 | } | 2734 | } |
742 | 2210 | 2735 | ||
743 | === modified file 'init/tests/test_job.c' | |||
744 | --- init/tests/test_job.c 2013-08-23 12:46:42 +0000 | |||
745 | +++ init/tests/test_job.c 2013-11-18 16:45:10 +0000 | |||
746 | @@ -7493,6 +7493,123 @@ | |||
747 | 7493 | } | 7493 | } |
748 | 7494 | } | 7494 | } |
749 | 7495 | 7495 | ||
750 | 7496 | void | ||
751 | 7497 | test_job_find (void) | ||
752 | 7498 | { | ||
753 | 7499 | ConfFile *file; | ||
754 | 7500 | ConfSource *source; | ||
755 | 7501 | Job *job, *ret; | ||
756 | 7502 | JobClass *class; | ||
757 | 7503 | Session *session; | ||
758 | 7504 | |||
759 | 7505 | TEST_FUNCTION ("job_find"); | ||
760 | 7506 | nih_error_init (); | ||
761 | 7507 | conf_init (); | ||
762 | 7508 | job_class_init (); | ||
763 | 7509 | |||
764 | 7510 | TEST_HASH_EMPTY (job_classes); | ||
765 | 7511 | |||
766 | 7512 | source = conf_source_new (NULL, "/tmp", CONF_JOB_DIR); | ||
767 | 7513 | TEST_NE_P (source, NULL); | ||
768 | 7514 | |||
769 | 7515 | file = conf_file_new (source, "/tmp/test"); | ||
770 | 7516 | TEST_NE_P (file, NULL); | ||
771 | 7517 | |||
772 | 7518 | class = file->job = job_class_new (NULL, "test", NULL); | ||
773 | 7519 | TEST_NE_P (class, NULL); | ||
774 | 7520 | |||
775 | 7521 | job = job_new (class, ""); | ||
776 | 7522 | TEST_NE_P (job, NULL); | ||
777 | 7523 | |||
778 | 7524 | TEST_HASH_EMPTY (job_classes); | ||
779 | 7525 | TEST_TRUE (job_class_consider (class)); | ||
780 | 7526 | TEST_HASH_NOT_EMPTY (job_classes); | ||
781 | 7527 | |||
782 | 7528 | /***********************************************************/ | ||
783 | 7529 | TEST_FEATURE ("JobClass, no job name or Session"); | ||
784 | 7530 | |||
785 | 7531 | ret = job_find (NULL, class, NULL, NULL); | ||
786 | 7532 | TEST_EQ_P (ret, NULL); | ||
787 | 7533 | |||
788 | 7534 | /***********************************************************/ | ||
789 | 7535 | TEST_FEATURE ("job class name, no job name or Session"); | ||
790 | 7536 | |||
791 | 7537 | ret = job_find (NULL, NULL, "test", NULL); | ||
792 | 7538 | TEST_EQ_P (ret, NULL); | ||
793 | 7539 | |||
794 | 7540 | /***********************************************************/ | ||
795 | 7541 | TEST_FEATURE ("JobClass+job name, no Session"); | ||
796 | 7542 | |||
797 | 7543 | ret = job_find (NULL, class, NULL, ""); | ||
798 | 7544 | TEST_EQ (ret, job); | ||
799 | 7545 | |||
800 | 7546 | /***********************************************************/ | ||
801 | 7547 | TEST_FEATURE ("job class name+job name, no Session"); | ||
802 | 7548 | |||
803 | 7549 | ret = job_find (NULL, NULL, "test", ""); | ||
804 | 7550 | TEST_EQ (ret, job); | ||
805 | 7551 | |||
806 | 7552 | /***********************************************************/ | ||
807 | 7553 | /* recreate env */ | ||
808 | 7554 | |||
809 | 7555 | nih_free (conf_sources); | ||
810 | 7556 | nih_free (job_classes); | ||
811 | 7557 | |||
812 | 7558 | conf_sources = NULL; | ||
813 | 7559 | job_classes = NULL; | ||
814 | 7560 | |||
815 | 7561 | conf_init (); | ||
816 | 7562 | job_class_init (); | ||
817 | 7563 | |||
818 | 7564 | TEST_HASH_EMPTY (job_classes); | ||
819 | 7565 | |||
820 | 7566 | source = conf_source_new (NULL, "/tmp", CONF_JOB_DIR); | ||
821 | 7567 | TEST_NE_P (source, NULL); | ||
822 | 7568 | |||
823 | 7569 | session = session_new (NULL, "/abc"); | ||
824 | 7570 | TEST_NE_P (session, NULL); | ||
825 | 7571 | session->conf_path = NIH_MUST (nih_strdup (session, "/def/ghi")); | ||
826 | 7572 | |||
827 | 7573 | source->session = session; | ||
828 | 7574 | |||
829 | 7575 | file = conf_file_new (source, "/tmp/test"); | ||
830 | 7576 | TEST_NE_P (file, NULL); | ||
831 | 7577 | |||
832 | 7578 | class = file->job = job_class_new (NULL, "test", session); | ||
833 | 7579 | TEST_NE_P (class, NULL); | ||
834 | 7580 | |||
835 | 7581 | job = job_new (class, ""); | ||
836 | 7582 | TEST_NE_P (job, NULL); | ||
837 | 7583 | |||
838 | 7584 | TEST_TRUE (job_class_consider (class)); | ||
839 | 7585 | TEST_HASH_NOT_EMPTY (job_classes); | ||
840 | 7586 | |||
841 | 7587 | /***********************************************************/ | ||
842 | 7588 | TEST_FEATURE ("JobClass+Session, no job name"); | ||
843 | 7589 | |||
844 | 7590 | ret = job_find (session, class, NULL, NULL); | ||
845 | 7591 | TEST_EQ_P (ret, NULL); | ||
846 | 7592 | |||
847 | 7593 | /***********************************************************/ | ||
848 | 7594 | TEST_FEATURE ("job class name+Session, no job name"); | ||
849 | 7595 | |||
850 | 7596 | ret = job_find (session, NULL, "test", NULL); | ||
851 | 7597 | TEST_EQ_P (ret, NULL); | ||
852 | 7598 | |||
853 | 7599 | /***********************************************************/ | ||
854 | 7600 | /* clean up */ | ||
855 | 7601 | |||
856 | 7602 | nih_free (conf_sources); | ||
857 | 7603 | nih_free (job_classes); | ||
858 | 7604 | nih_free (session); | ||
859 | 7605 | |||
860 | 7606 | conf_sources = NULL; | ||
861 | 7607 | job_classes = NULL; | ||
862 | 7608 | |||
863 | 7609 | conf_init (); | ||
864 | 7610 | job_class_init (); | ||
865 | 7611 | } | ||
866 | 7612 | |||
867 | 7496 | 7613 | ||
868 | 7497 | void | 7614 | void |
869 | 7498 | deserialise_ptrace_next (void) | 7615 | deserialise_ptrace_next (void) |
870 | @@ -7608,6 +7725,8 @@ | |||
871 | 7608 | 7725 | ||
872 | 7609 | test_deserialise_ptrace (); | 7726 | test_deserialise_ptrace (); |
873 | 7610 | 7727 | ||
874 | 7728 | test_job_find (); | ||
875 | 7729 | |||
876 | 7611 | return 0; | 7730 | return 0; |
877 | 7612 | } | 7731 | } |
878 | 7613 | 7732 |