Merge lp:~jamesodhunt/upstart/environ-remove-tests into lp:upstart

Proposed by James Hunt on 2013-10-24
Status: Merged
Merged at revision: 1551
Proposed branch: lp:~jamesodhunt/upstart/environ-remove-tests
Merge into: lp:upstart
Diff against target: 549 lines (+490/-1)
5 files modified
ChangeLog (+18/-0)
init/environ.c (+4/-0)
init/job_process.c (+1/-1)
init/tests/test_environ.c (+457/-0)
test/test_util_common.c (+10/-0)
To merge this branch: bzr merge lp:~jamesodhunt/upstart/environ-remove-tests
Reviewer Review Type Date Requested Status
Dimitri John Ledkov 2013-10-24 Approve on 2013-11-03
Review via email: mp+192506@code.launchpad.net
To post a comment you must log in.
Dimitri John Ledkov (xnox) wrote :

lgtm

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ChangeLog'
2--- ChangeLog 2013-10-23 22:44:54 +0000
3+++ ChangeLog 2013-10-24 13:34:13 +0000
4@@ -1,3 +1,21 @@
5+2013-10-24 James Hunt <james.hunt@ubuntu.com>
6+
7+ * init/environ.c: Comment.
8+ * init/job_process.c: Formatting.
9+ * init/test_environ.c:
10+ - test_add(): New test:
11+ - "using bare word with no corresponding variable set in environment"
12+ - test_remove(): New function ("the missing test") containing 8 new tests:
13+ - "remove name=value pair with empty table"
14+ - "remove bare name with empty table"
15+ - "remove name=value from table of size 1"
16+ - "remove bare name from table of size 1"
17+ - "remove first name=value entry from table of size 2"
18+ - "remove first bare name entry from table of size 2"
19+ - "remove last name=value entry from table of size 2"
20+ - "remove last bare name entry from table of size 2"
21+ * test/test_util_common.c: get_initctl(): Added environment checks.
22+
23 2013-10-23 Dmitrijs Ledkovs <xnox@ubuntu.com>
24
25 * extra/upstart-socket-bridge.c: use SOCKET_PATH in our event
26
27=== modified file 'init/environ.c'
28--- init/environ.c 2013-01-08 15:57:31 +0000
29+++ init/environ.c 2013-10-24 13:34:13 +0000
30@@ -202,6 +202,10 @@
31 for (e = *env; e && *e; e++) {
32 keylen = strcspn (*e, "=");
33
34+ /* Found @str in the existing environment (either as a
35+ * name=value pair, or a bare name), so don't copy it to
36+ * the new environment.
37+ */
38 if (! strncmp (str, *e, keylen))
39 continue;
40
41
42=== modified file 'init/job_process.c'
43--- init/job_process.c 2013-10-03 14:43:24 +0000
44+++ init/job_process.c 2013-10-24 13:34:13 +0000
45@@ -278,7 +278,7 @@
46 env = NIH_MUST (nih_str_array_new (NULL));
47
48 if (job->env)
49- NIH_MUST(environ_append (&env, NULL, &envc, TRUE, job->env));
50+ NIH_MUST (environ_append (&env, NULL, &envc, TRUE, job->env));
51
52 if (job->stop_env
53 && ((process == PROCESS_PRE_STOP)
54
55=== modified file 'init/tests/test_environ.c'
56--- init/tests/test_environ.c 2011-06-06 17:05:11 +0000
57+++ init/tests/test_environ.c 2013-10-24 13:34:13 +0000
58@@ -572,6 +572,462 @@
59 }
60
61 unsetenv ("BAR");
62+
63+ /* Check that attempting to add a variable by name fails if
64+ * there is no corresponding environment variable set.
65+ */
66+ TEST_FEATURE ("using bare word with no corresponding variable set in environment");
67+
68+ /* Ensure variable not set initially */
69+ TEST_EQ_P (getenv ("UPSTART_TEST_VARIABLE"), NULL);
70+
71+ TEST_ALLOC_FAIL {
72+ TEST_ALLOC_SAFE {
73+ len = 0;
74+ env = nih_str_array_new (NULL);
75+ assert (nih_str_array_add (&env, NULL, &len,
76+ "FOO=BAR"));
77+ }
78+
79+ ret = environ_add (&env, NULL, &len, FALSE, "UPSTART_TEST_VARIABLE");
80+
81+ if (test_alloc_failed) {
82+ TEST_EQ_P (ret, NULL);
83+
84+ TEST_EQ (len, 1);
85+ TEST_EQ_STR (env[0], "FOO=BAR");
86+ TEST_EQ_P (env[1], NULL);
87+
88+ nih_free (env);
89+ continue;
90+ }
91+
92+ /* XXX: Attempting to add an unset variable results in
93+ * no change to the table (it is not an error!)
94+ */
95+ TEST_EQ_P (ret, env);
96+
97+ TEST_EQ (len, 1);
98+ TEST_EQ_STR (env[0], "FOO=BAR");
99+ TEST_EQ_P (env[1], NULL);
100+
101+ nih_free (env);
102+ }
103+}
104+
105+void
106+test_remove (void)
107+{
108+ char **env = NULL, **ret;
109+ size_t len = 0;
110+
111+ TEST_FUNCTION ("environ_remove");
112+
113+ TEST_FEATURE ("remove name=value pair with empty table");
114+ TEST_ALLOC_FAIL {
115+ TEST_ALLOC_SAFE {
116+ len = 0;
117+ env = nih_str_array_new (NULL);
118+ }
119+
120+ ret = environ_remove (&env, NULL, &len, "FOO=BAR");
121+
122+ if (test_alloc_failed) {
123+ TEST_EQ_P (ret, NULL);
124+
125+ TEST_EQ (len, 0);
126+ TEST_EQ_P (env[0], NULL);
127+
128+ nih_free (env);
129+ continue;
130+ }
131+
132+ TEST_EQ_P (ret, NULL);
133+ TEST_EQ (len, 0);
134+ TEST_EQ_P (env[0], NULL);
135+
136+ nih_free (env);
137+ }
138+
139+ TEST_FEATURE ("remove bare name with empty table");
140+ TEST_ALLOC_FAIL {
141+ TEST_ALLOC_SAFE {
142+ len = 0;
143+ env = nih_str_array_new (NULL);
144+ }
145+
146+ ret = environ_remove (&env, NULL, &len, "FOO");
147+
148+ if (test_alloc_failed) {
149+ TEST_EQ_P (ret, NULL);
150+
151+ TEST_EQ (len, 0);
152+ TEST_EQ_P (env[0], NULL);
153+
154+ nih_free (env);
155+ continue;
156+ }
157+
158+ TEST_EQ_P (ret, NULL);
159+ TEST_EQ (len, 0);
160+ TEST_EQ_P (env[0], NULL);
161+
162+ nih_free (env);
163+ }
164+
165+ TEST_FEATURE ("remove name=value from table of size 1");
166+ TEST_ALLOC_FAIL {
167+ TEST_ALLOC_SAFE {
168+ len = 0;
169+ env = nih_str_array_new (NULL);
170+
171+ ret = environ_add (&env, NULL, &len, TRUE, "FOO=BAR");
172+ TEST_NE_P (ret, NULL);
173+
174+ TEST_EQ (len, 1);
175+ TEST_ALLOC_PARENT (env[0], env);
176+ TEST_ALLOC_SIZE (env[0], 8);
177+ TEST_EQ_STR (env[0], "FOO=BAR");
178+ TEST_EQ_P (env[1], NULL);
179+ }
180+
181+ ret = environ_remove (&env, NULL, &len, "FOO=BAR");
182+
183+ if (test_alloc_failed) {
184+ TEST_EQ_P (ret, NULL);
185+
186+ TEST_EQ (len, 1);
187+
188+ TEST_ALLOC_PARENT (env[0], env);
189+ TEST_ALLOC_SIZE (env[0], 8);
190+
191+ TEST_NE_P (env[0], NULL);
192+ TEST_EQ_STR (env[0], "FOO=BAR");
193+
194+ TEST_EQ_P (env[1], NULL);
195+
196+ nih_free (env);
197+ continue;
198+ }
199+
200+ TEST_NE_P (ret, NULL);
201+ TEST_EQ (len, 0);
202+ TEST_EQ_P (env[0], NULL);
203+
204+ nih_free (env);
205+ }
206+
207+ TEST_FEATURE ("remove bare name from table of size 1");
208+ TEST_ALLOC_FAIL {
209+ TEST_ALLOC_SAFE {
210+ len = 0;
211+ env = nih_str_array_new (NULL);
212+
213+ ret = environ_add (&env, NULL, &len, TRUE, "FOO=BAR");
214+ TEST_NE_P (ret, NULL);
215+
216+ TEST_EQ (len, 1);
217+ TEST_ALLOC_PARENT (env[0], env);
218+ TEST_ALLOC_SIZE (env[0], 8);
219+ TEST_EQ_STR (env[0], "FOO=BAR");
220+ TEST_EQ_P (env[1], NULL);
221+ }
222+
223+ ret = environ_remove (&env, NULL, &len, "FOO");
224+
225+ if (test_alloc_failed) {
226+ TEST_EQ_P (ret, NULL);
227+
228+ TEST_EQ (len, 1);
229+
230+ TEST_ALLOC_PARENT (env[0], env);
231+ TEST_ALLOC_SIZE (env[0], 8);
232+
233+ TEST_NE_P (env[0], NULL);
234+ TEST_EQ_STR (env[0], "FOO=BAR");
235+
236+ TEST_EQ_P (env[1], NULL);
237+
238+ nih_free (env);
239+ continue;
240+ }
241+
242+ TEST_NE_P (ret, NULL);
243+ TEST_EQ (len, 0);
244+ TEST_EQ_P (env[0], NULL);
245+
246+ nih_free (env);
247+ }
248+
249+ TEST_FEATURE ("remove first name=value entry from table of size 2");
250+ TEST_ALLOC_FAIL {
251+ TEST_ALLOC_SAFE {
252+ len = 0;
253+ env = nih_str_array_new (NULL);
254+
255+ ret = environ_add (&env, NULL, &len, TRUE, "FOO=BAR");
256+ TEST_NE_P (ret, NULL);
257+
258+ TEST_EQ (len, 1);
259+ TEST_ALLOC_PARENT (env[0], env);
260+ TEST_ALLOC_SIZE (env[0], 8);
261+ TEST_EQ_STR (env[0], "FOO=BAR");
262+ TEST_EQ_P (env[1], NULL);
263+
264+ ret = environ_add (&env, NULL, &len, TRUE, "BAZ=QUX");
265+ TEST_NE_P (ret, NULL);
266+
267+ TEST_EQ (len, 2);
268+ TEST_ALLOC_PARENT (env[0], env);
269+ TEST_ALLOC_SIZE (env[0], 8);
270+ TEST_EQ_STR (env[0], "FOO=BAR");
271+
272+ TEST_ALLOC_PARENT (env[1], env);
273+ TEST_ALLOC_SIZE (env[1], 8);
274+ TEST_EQ_STR (env[1], "BAZ=QUX");
275+
276+ TEST_EQ_P (env[2], NULL);
277+ }
278+
279+ /* Remove first entry added */
280+ ret = environ_remove (&env, NULL, &len, "FOO=BAR");
281+
282+ if (test_alloc_failed) {
283+ TEST_EQ_P (ret, NULL);
284+
285+ TEST_EQ (len, 2);
286+ TEST_ALLOC_PARENT (env[0], env);
287+ TEST_ALLOC_SIZE (env[0], 8);
288+ TEST_EQ_STR (env[0], "FOO=BAR");
289+
290+ TEST_ALLOC_PARENT (env[1], env);
291+ TEST_ALLOC_SIZE (env[1], 8);
292+ TEST_EQ_STR (env[1], "BAZ=QUX");
293+
294+ TEST_EQ_P (env[2], NULL);
295+
296+ nih_free (env);
297+ continue;
298+ }
299+
300+ TEST_NE_P (ret, NULL);
301+ TEST_EQ (len, 1);
302+
303+ TEST_ALLOC_PARENT (env[0], env);
304+ TEST_ALLOC_SIZE (env[0], 8);
305+ TEST_EQ_STR (env[0], "BAZ=QUX");
306+
307+ TEST_EQ_P (env[1], NULL);
308+
309+ nih_free (env);
310+ }
311+
312+ TEST_FEATURE ("remove first bare name entry from table of size 2");
313+
314+ /* Set a variable to allow the bare name to be expanded */
315+ assert0 (setenv ("UPSTART_TEST_VARIABLE", "foo", 1));
316+
317+ TEST_ALLOC_FAIL {
318+ TEST_ALLOC_SAFE {
319+ len = 0;
320+ env = nih_str_array_new (NULL);
321+
322+ ret = environ_add (&env, NULL, &len, TRUE, "UPSTART_TEST_VARIABLE");
323+ TEST_NE_P (ret, NULL);
324+
325+ TEST_EQ (len, 1);
326+ TEST_ALLOC_PARENT (env[0], env);
327+ TEST_ALLOC_SIZE (env[0], 8);
328+
329+ /* Should have been expanded */
330+ TEST_EQ_STR (env[0], "UPSTART_TEST_VARIABLE=foo");
331+
332+ TEST_EQ_P (env[1], NULL);
333+
334+ ret = environ_add (&env, NULL, &len, TRUE, "BAZ=QUX");
335+ TEST_NE_P (ret, NULL);
336+
337+ TEST_EQ (len, 2);
338+
339+ TEST_ALLOC_PARENT (env[0], env);
340+ TEST_ALLOC_SIZE (env[0], 8);
341+ TEST_EQ_STR (env[0], "UPSTART_TEST_VARIABLE=foo");
342+
343+ TEST_ALLOC_PARENT (env[1], env);
344+ TEST_ALLOC_SIZE (env[1], 8);
345+ TEST_EQ_STR (env[1], "BAZ=QUX");
346+
347+ TEST_EQ_P (env[2], NULL);
348+ }
349+
350+ /* Remove first entry added */
351+ ret = environ_remove (&env, NULL, &len, "UPSTART_TEST_VARIABLE");
352+
353+ if (test_alloc_failed) {
354+ TEST_EQ_P (ret, NULL);
355+
356+ TEST_EQ (len, 2);
357+ TEST_ALLOC_PARENT (env[0], env);
358+ TEST_ALLOC_SIZE (env[0], 8);
359+ TEST_EQ_STR (env[0], "UPSTART_TEST_VARIABLE=foo");
360+
361+ TEST_ALLOC_PARENT (env[1], env);
362+ TEST_ALLOC_SIZE (env[1], 8);
363+ TEST_EQ_STR (env[1], "BAZ=QUX");
364+
365+ TEST_EQ_P (env[2], NULL);
366+
367+ nih_free (env);
368+ continue;
369+ }
370+
371+ TEST_NE_P (ret, NULL);
372+ TEST_EQ (len, 1);
373+
374+ TEST_ALLOC_PARENT (env[0], env);
375+ TEST_ALLOC_SIZE (env[0], 8);
376+ TEST_EQ_STR (env[0], "BAZ=QUX");
377+
378+ TEST_EQ_P (env[1], NULL);
379+
380+ nih_free (env);
381+ }
382+
383+ assert0 (unsetenv ("UPSTART_TEST_VARIABLE"));
384+
385+ TEST_FEATURE ("remove last name=value entry from table of size 2");
386+ TEST_ALLOC_FAIL {
387+ TEST_ALLOC_SAFE {
388+ len = 0;
389+ env = nih_str_array_new (NULL);
390+
391+ ret = environ_add (&env, NULL, &len, TRUE, "FOO=BAR");
392+ TEST_NE_P (ret, NULL);
393+
394+ TEST_EQ (len, 1);
395+ TEST_ALLOC_PARENT (env[0], env);
396+ TEST_ALLOC_SIZE (env[0], 8);
397+ TEST_EQ_STR (env[0], "FOO=BAR");
398+ TEST_EQ_P (env[1], NULL);
399+
400+ ret = environ_add (&env, NULL, &len, TRUE, "BAZ=QUX");
401+ TEST_NE_P (ret, NULL);
402+
403+ TEST_EQ (len, 2);
404+ TEST_ALLOC_PARENT (env[0], env);
405+ TEST_ALLOC_SIZE (env[0], 8);
406+ TEST_EQ_STR (env[0], "FOO=BAR");
407+
408+ TEST_ALLOC_PARENT (env[1], env);
409+ TEST_ALLOC_SIZE (env[1], 8);
410+ TEST_EQ_STR (env[1], "BAZ=QUX");
411+
412+ TEST_EQ_P (env[2], NULL);
413+ }
414+
415+ /* Remove last entry added */
416+ ret = environ_remove (&env, NULL, &len, "BAZ=QUX");
417+
418+ if (test_alloc_failed) {
419+ TEST_EQ_P (ret, NULL);
420+
421+ TEST_EQ (len, 2);
422+ TEST_ALLOC_PARENT (env[0], env);
423+ TEST_ALLOC_SIZE (env[0], 8);
424+ TEST_EQ_STR (env[0], "FOO=BAR");
425+
426+ TEST_ALLOC_PARENT (env[1], env);
427+ TEST_ALLOC_SIZE (env[1], 8);
428+ TEST_EQ_STR (env[1], "BAZ=QUX");
429+
430+ TEST_EQ_P (env[2], NULL);
431+
432+ nih_free (env);
433+ continue;
434+ }
435+
436+ TEST_NE_P (ret, NULL);
437+ TEST_EQ (len, 1);
438+
439+ TEST_ALLOC_PARENT (env[0], env);
440+ TEST_ALLOC_SIZE (env[0], 8);
441+ TEST_EQ_STR (env[0], "FOO=BAR");
442+
443+ TEST_EQ_P (env[1], NULL);
444+
445+ nih_free (env);
446+ }
447+
448+ TEST_FEATURE ("remove last bare name entry from table of size 2");
449+
450+ /* Set a variable to allow the bare name to be expanded */
451+ assert0 (setenv ("UPSTART_TEST_VARIABLE", "foo", 1));
452+
453+ TEST_ALLOC_FAIL {
454+ TEST_ALLOC_SAFE {
455+ len = 0;
456+ env = nih_str_array_new (NULL);
457+
458+ ret = environ_add (&env, NULL, &len, TRUE, "FOO=BAR");
459+ TEST_NE_P (ret, NULL);
460+
461+ TEST_EQ (len, 1);
462+ TEST_ALLOC_PARENT (env[0], env);
463+ TEST_ALLOC_SIZE (env[0], 8);
464+ TEST_EQ_STR (env[0], "FOO=BAR");
465+ TEST_EQ_P (env[1], NULL);
466+
467+ ret = environ_add (&env, NULL, &len, TRUE, "UPSTART_TEST_VARIABLE");
468+ TEST_NE_P (ret, NULL);
469+
470+ TEST_EQ (len, 2);
471+ TEST_ALLOC_PARENT (env[0], env);
472+ TEST_ALLOC_SIZE (env[0], 8);
473+ TEST_EQ_STR (env[0], "FOO=BAR");
474+
475+ TEST_ALLOC_PARENT (env[1], env);
476+ TEST_ALLOC_SIZE (env[1], 8);
477+
478+ /* Should have been expanded */
479+ TEST_EQ_STR (env[1], "UPSTART_TEST_VARIABLE=foo");
480+
481+ TEST_EQ_P (env[2], NULL);
482+ }
483+
484+ /* Remove last entry added */
485+ ret = environ_remove (&env, NULL, &len, "UPSTART_TEST_VARIABLE");
486+
487+ if (test_alloc_failed) {
488+ TEST_EQ_P (ret, NULL);
489+
490+ TEST_EQ (len, 2);
491+ TEST_ALLOC_PARENT (env[0], env);
492+ TEST_ALLOC_SIZE (env[0], 8);
493+ TEST_EQ_STR (env[0], "FOO=BAR");
494+
495+ TEST_ALLOC_PARENT (env[1], env);
496+ TEST_ALLOC_SIZE (env[1], 8);
497+ TEST_EQ_STR (env[1], "UPSTART_TEST_VARIABLE=foo");
498+
499+ TEST_EQ_P (env[2], NULL);
500+
501+ nih_free (env);
502+ continue;
503+ }
504+
505+ TEST_NE_P (ret, NULL);
506+ TEST_EQ (len, 1);
507+
508+ TEST_ALLOC_PARENT (env[0], env);
509+ TEST_ALLOC_SIZE (env[0], 8);
510+ TEST_EQ_STR (env[0], "FOO=BAR");
511+
512+ TEST_EQ_P (env[1], NULL);
513+
514+ nih_free (env);
515+ }
516+
517+ assert0 (unsetenv ("UPSTART_TEST_VARIABLE"));
518 }
519
520 void
521@@ -1590,6 +2046,7 @@
522 setenv ("UPSTART_NO_SESSIONS", "1", 1);
523
524 test_add ();
525+ test_remove ();
526 test_append ();
527 test_set ();
528 test_lookup ();
529
530=== modified file 'test/test_util_common.c'
531--- test/test_util_common.c 2013-10-02 08:59:20 +0000
532+++ test/test_util_common.c 2013-10-24 13:34:13 +0000
533@@ -349,6 +349,16 @@
534 {
535 static char path[PATH_MAX + 1024] = { 0 };
536 int ret;
537+ int env_valid;
538+
539+ /* Sanity check calling environment */
540+ if (test_user_mode) {
541+ env_valid = getenv ("UPSTART_SESSION") ? TRUE : FALSE;
542+ } else {
543+ env_valid = getenv ("DBUS_SESSION_BUS_ADDRESS") ? TRUE : FALSE;
544+ }
545+
546+ nih_assert (env_valid);
547
548 ret = sprintf (path, "%s %s",
549 get_initctl_binary (),

Subscribers

People subscribed via source and target branches