Merge lp:~jamesodhunt/upstart/fix-job_find into lp:upstart

Proposed by James Hunt on 2013-11-18
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
Reviewer Review Type Date Requested Status
Upstart Reviewers 2013-11-18 Pending
Review via email: mp+195632@code.launchpad.net

Description of the change

Fixes a bug that could cause a Session Init to crash.

To post a comment you must log in.
lp:~jamesodhunt/upstart/fix-job_find updated on 2013-11-18
1574. By James Hunt on 2013-11-18

* 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+2013-11-18 James Hunt <james.hunt@ubuntu.com>
6+
7+ * init/control.c:
8+ - control_set_env():
9+ - Check permissions before anything else.
10+ - Explicit check on @var rather than an assert.
11+ - control_unset_env(): Explicit check on @name rather than an assert.
12+ - control_get_env(): Explicit check on @name.
13+ - control_reset_env(): Check permissions before anything else.
14+ * init/control.h: control_get_job():
15+ - Pass @job_name to job_find() rather than hard-coded NULL.
16+ - Handle instance being NULL when raising NIH D-Bus error.
17+ * init/job.c: job_find(): Handle NULL job_name.
18+ * init/tests/test_control.c:
19+ - Typo.
20+ - New functions:
21+ - test_list_env().
22+ - test_list_env().
23+ - test_get_env().
24+ - test_set_env().
25+ - test_unset_env().
26+ - test_reset_env().
27+ * init/tests/test_job.c: test_job_find(): New function.
28+
29 2013-11-16 Dmitrijs Ledkovs <xnox@ubuntu.com>
30
31 * init/xdg.c, util/Makefile.am, test/Makefile.am, init/conf.c:
32
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
38 nih_assert (message);
39 nih_assert (job_details);
40- nih_assert (var);
41+
42+ if (! control_check_permission (message)) {
43+ nih_dbus_error_raise_printf (
44+ DBUS_INTERFACE_UPSTART ".Error.PermissionDenied",
45+ _("You do not have permission to modify job environment"));
46+ return -1;
47+ }
48
49 if (job_details[0]) {
50 job_name = job_details[0];
51@@ -1302,10 +1308,9 @@
52 return -1;
53 }
54
55- if (! control_check_permission (message)) {
56- nih_dbus_error_raise_printf (
57- DBUS_INTERFACE_UPSTART ".Error.PermissionDenied",
58- _("You do not have permission to modify job environment"));
59+ if (! var || ! *var) {
60+ nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
61+ _("Variable may not be empty string"));
62 return -1;
63 }
64
65@@ -1386,7 +1391,6 @@
66
67 nih_assert (message);
68 nih_assert (job_details);
69- nih_assert (name);
70
71 if (! control_check_permission (message)) {
72 nih_dbus_error_raise_printf (
73@@ -1395,6 +1399,12 @@
74 return -1;
75 }
76
77+ if (! name || ! *name) {
78+ nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
79+ _("Variable may not be empty string"));
80+ return -1;
81+ }
82+
83 if (job_details[0]) {
84 job_name = job_details[0];
85
86@@ -1490,6 +1500,12 @@
87 return -1;
88 }
89
90+ if (! name || ! *name) {
91+ nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
92+ _("Variable may not be empty string"));
93+ return -1;
94+ }
95+
96 if (job_details[0]) {
97 job_name = job_details[0];
98
99@@ -1649,6 +1665,13 @@
100 nih_assert (message);
101 nih_assert (job_details);
102
103+ if (! control_check_permission (message)) {
104+ nih_dbus_error_raise_printf (
105+ DBUS_INTERFACE_UPSTART ".Error.PermissionDenied",
106+ _("You do not have permission to modify job environment"));
107+ return -1;
108+ }
109+
110 if (job_details[0]) {
111 job_name = job_details[0];
112
113@@ -1661,13 +1684,6 @@
114 return -1;
115 }
116
117- if (! control_check_permission (message)) {
118- nih_dbus_error_raise_printf (
119- DBUS_INTERFACE_UPSTART ".Error.PermissionDenied",
120- _("You do not have permission to modify job environment"));
121- return -1;
122- }
123-
124 /* Verify that job name is valid */
125 if (job_name && ! strlen (job_name)) {
126 nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
127
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 /* upstart
133 *
134- * Copyright 2009-2011 Canonical Ltd.
135+ * Copyright © 2009-2011 Canonical Ltd.
136 * Author: Scott James Remnant <scott@netsplit.com>.
137 *
138 * This program is free software; you can redistribute it and/or modify
139@@ -60,7 +60,7 @@
140 **/
141 #define control_get_job(session, job, job_name, instance) \
142 { \
143- if (job_name != NULL ) { \
144+ if (job_name) { \
145 JobClass *class; \
146 \
147 class = job_class_get_registered (job_name, session); \
148@@ -73,13 +73,14 @@
149 return -1; \
150 } \
151 \
152- job = job_find (session, class, NULL, instance); \
153- if (job == NULL) { \
154+ job = job_find (session, class, job_name, instance); \
155+ if (! job) { \
156 nih_dbus_error_raise_printf ( \
157 DBUS_INTERFACE_UPSTART \
158 ".Error.UnknownJobInstance", \
159 _("Unknown instance: %s of job %s"), \
160- instance, job_name); \
161+ instance ? instance : "(null)", \
162+ job_name); \
163 return -1; \
164 } \
165 } \
166
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 nih_assert (class || job_class);
172 nih_assert (job_classes);
173
174+ if (! job_name)
175+ goto error;
176+
177 if (! class)
178 class = job_class_get_registered (job_class, session);
179
180
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 #include "control.h"
186 #include "errors.h"
187
188+#include "test_util_common.h"
189
190 extern const char *control_server_address;
191-
192+extern int no_inherit_env;
193
194 void
195 test_server_open (void)
196@@ -933,7 +934,7 @@
197 strcat (filename, "/baz");
198
199 /* XXX: note that this will generate an error message when this
200- * test runs sine "/baz" does not exist as a directory.
201+ * test runs since "/baz" does not exist as a directory.
202 */
203 source3 = conf_source_new (NULL, filename, CONF_DIR);
204
205@@ -2176,6 +2177,524 @@
206 nih_log_priority = NIH_LOG_UNKNOWN;
207 }
208
209+void
210+test_list_env (void)
211+{
212+ NihDBusMessage *message = NULL;
213+ int ret;
214+ nih_local char **env = NULL;
215+ nih_local char **job_details = NULL;
216+ nih_local char **job_details2 = NULL;
217+ JobClass *class;
218+ Job *job;
219+ NihError *error;
220+
221+ TEST_FUNCTION ("control_list_env");
222+
223+ nih_error_init ();
224+ job_class_init ();
225+ job_class_environment_init ();
226+
227+ no_inherit_env = TRUE;
228+
229+ message = nih_new (NULL, NihDBusMessage);
230+ TEST_NE_P (message, NULL);
231+ message->connection = NULL;
232+ message->message = NULL;
233+
234+ job_details = nih_str_array_new (NULL);
235+ nih_assert (job_details);
236+
237+ class = job_class_new (NULL, "foo", NULL);
238+ TEST_NE_P (class, NULL);
239+
240+ job = job_new (class, "");
241+ TEST_NE_P (job, NULL);
242+
243+ nih_hash_add (job_classes, &class->entry);
244+
245+ assert0 (job_class_environment_set ("hello=world", TRUE));
246+
247+ /************************************************************/
248+ TEST_FEATURE ("with empty array");
249+
250+ ret = control_list_env (NULL, message, job_details, &env);
251+ TEST_EQ (ret, 0);
252+
253+ /* Default variables that Upstart adds */
254+ TEST_STR_MATCH (env[0], "PATH=*");
255+ TEST_STR_MATCH (env[1], "TERM=*");
256+
257+ TEST_EQ_STR (env[2], "hello=world");
258+ TEST_EQ_P (env[3], NULL);
259+
260+ /************************************************************/
261+ TEST_FEATURE ("with empty job class name");
262+
263+ job_details2 = nih_str_array_copy (NULL, NULL, job_details);
264+ TEST_NE_P (job_details2, NULL);
265+
266+ nih_assert (nih_str_array_add (&job_details2,
267+ NULL,
268+ NULL,
269+ ""));
270+
271+ ret = control_list_env (NULL, message, job_details2, &env);
272+ TEST_LT (ret, 0);
273+
274+ error = nih_error_get ();
275+ TEST_EQ (error->number, NIH_DBUS_ERROR);
276+ nih_free (error);
277+
278+ /************************************************************/
279+ TEST_FEATURE ("with missing instance name");
280+
281+ /* add name */
282+ nih_assert (nih_str_array_add (&job_details,
283+ NULL,
284+ NULL,
285+ "foo"));
286+
287+ ret = control_list_env (NULL, message, job_details, &env);
288+ TEST_LT (ret, 0);
289+
290+ error = nih_error_get ();
291+ TEST_EQ (error->number, NIH_DBUS_ERROR);
292+ nih_free (error);
293+
294+ /************************************************************/
295+ /* We can't wrap this in a TEST_ALLOC_FAIL() block since the
296+ * nih_dbus_*() calls such as nih_dbus_error_raise_printf() use
297+ * NIH_MUST() which defeats the fail macro
298+ */
299+ TEST_FEATURE ("with correct job details");
300+
301+ /* add instance */
302+ nih_assert (nih_str_array_add (&job_details,
303+ NULL,
304+ NULL,
305+ ""));
306+
307+ ret = control_list_env (NULL, message, job_details, &env);
308+ TEST_EQ (ret, 0);
309+
310+ nih_free (message);
311+}
312+
313+void
314+test_get_env (void)
315+{
316+ NihDBusMessage *message = NULL;
317+ int ret;
318+ char *value = NULL;
319+ nih_local char **job_details = NULL;
320+ nih_local char **job_details2 = NULL;
321+ JobClass *class;
322+ Job *job;
323+ NihError *error;
324+
325+ TEST_FUNCTION ("control_get_env");
326+
327+ nih_error_init ();
328+ job_class_init ();
329+ job_class_environment_init ();
330+
331+ no_inherit_env = TRUE;
332+
333+ message = nih_new (NULL, NihDBusMessage);
334+ TEST_NE_P (message, NULL);
335+ message->connection = NULL;
336+ message->message = NULL;
337+
338+ job_details = nih_str_array_new (NULL);
339+ nih_assert (job_details);
340+
341+ job_details2 = nih_str_array_copy (NULL, NULL, job_details);
342+ TEST_NE_P (job_details2, NULL);
343+
344+ nih_assert (nih_str_array_add (&job_details2,
345+ NULL,
346+ NULL,
347+ ""));
348+
349+ class = job_class_new (NULL, "foo", NULL);
350+ TEST_NE_P (class, NULL);
351+
352+ job = job_new (class, "");
353+ TEST_NE_P (job, NULL);
354+
355+ nih_hash_add (job_classes, &class->entry);
356+
357+ assert0 (job_class_environment_set ("hello=world", TRUE));
358+
359+ /************************************************************/
360+ TEST_FEATURE ("with NULL variable name");
361+
362+ ret = control_get_env (NULL, message, job_details2, NULL, &value);
363+ TEST_LT (ret, 0);
364+
365+ error = nih_error_get ();
366+ TEST_EQ (error->number, NIH_DBUS_ERROR);
367+ nih_free (error);
368+
369+ /************************************************************/
370+ TEST_FEATURE ("with empty variable name");
371+
372+ ret = control_get_env (NULL, message, job_details2, "", &value);
373+ TEST_LT (ret, 0);
374+
375+ error = nih_error_get ();
376+ TEST_EQ (error->number, NIH_DBUS_ERROR);
377+ nih_free (error);
378+
379+ /************************************************************/
380+ TEST_FEATURE ("with empty job class name");
381+
382+ ret = control_get_env (NULL, message, job_details2, "hello", &value);
383+ TEST_LT (ret, 0);
384+
385+ error = nih_error_get ();
386+ TEST_EQ (error->number, NIH_DBUS_ERROR);
387+ nih_free (error);
388+
389+ /************************************************************/
390+ TEST_FEATURE ("with missing instance name");
391+
392+ /* add name */
393+ nih_assert (nih_str_array_add (&job_details,
394+ NULL,
395+ NULL,
396+ "foo"));
397+
398+ ret = control_get_env (NULL, message, job_details, "hello", &value);
399+ TEST_LT (ret, 0);
400+
401+ error = nih_error_get ();
402+ TEST_EQ (error->number, NIH_DBUS_ERROR);
403+ nih_free (error);
404+
405+ /************************************************************/
406+ TEST_FEATURE ("with correct job details");
407+
408+ /* add instance */
409+ nih_assert (nih_str_array_add (&job_details,
410+ NULL,
411+ NULL,
412+ ""));
413+
414+ ret = control_get_env (NULL, message, job_details, "hello", &value);
415+ TEST_EQ (ret, 0);
416+ TEST_EQ_STR (value, "world");
417+
418+ /************************************************************/
419+ /* tidy up */
420+
421+ nih_free (value);
422+ nih_free (message);
423+}
424+
425+void
426+test_set_env (void)
427+{
428+ NihDBusMessage *message = NULL;
429+ int ret;
430+ nih_local char **job_details = NULL;
431+ nih_local char **job_details2 = NULL;
432+ JobClass *class;
433+ Job *job;
434+ NihError *error;
435+
436+ TEST_FUNCTION ("control_set_env");
437+
438+ nih_error_init ();
439+ job_class_init ();
440+ job_class_environment_init ();
441+
442+ no_inherit_env = TRUE;
443+
444+ message = nih_new (NULL, NihDBusMessage);
445+ TEST_NE_P (message, NULL);
446+ message->connection = NULL;
447+ message->message = NULL;
448+
449+ job_details = nih_str_array_new (NULL);
450+ nih_assert (job_details);
451+
452+ job_details2 = nih_str_array_copy (NULL, NULL, job_details);
453+ TEST_NE_P (job_details2, NULL);
454+
455+ nih_assert (nih_str_array_add (&job_details2,
456+ NULL,
457+ NULL,
458+ ""));
459+
460+ class = job_class_new (NULL, "foo", NULL);
461+ TEST_NE_P (class, NULL);
462+
463+ job = job_new (class, "");
464+ TEST_NE_P (job, NULL);
465+
466+ nih_hash_add (job_classes, &class->entry);
467+
468+ /************************************************************/
469+ TEST_FEATURE ("with NULL variable name");
470+
471+ ret = control_set_env (NULL, message, job_details2, NULL, TRUE);
472+ TEST_LT (ret, 0);
473+
474+ error = nih_error_get ();
475+ TEST_EQ (error->number, NIH_DBUS_ERROR);
476+ nih_free (error);
477+
478+ /************************************************************/
479+ TEST_FEATURE ("with empty variable name");
480+
481+ ret = control_set_env (NULL, message, job_details2, "", TRUE);
482+ TEST_LT (ret, 0);
483+
484+ error = nih_error_get ();
485+ TEST_EQ (error->number, NIH_DBUS_ERROR);
486+ nih_free (error);
487+
488+ /************************************************************/
489+ TEST_FEATURE ("with empty job class name");
490+
491+ ret = control_set_env (NULL, message, job_details2, "hello=world", TRUE);
492+ TEST_LT (ret, 0);
493+
494+ error = nih_error_get ();
495+ TEST_EQ (error->number, NIH_DBUS_ERROR);
496+ nih_free (error);
497+
498+ /************************************************************/
499+ TEST_FEATURE ("with missing instance name");
500+
501+ /* add name */
502+ nih_assert (nih_str_array_add (&job_details,
503+ NULL,
504+ NULL,
505+ "foo"));
506+
507+ ret = control_set_env (NULL, message, job_details, "hello=world", TRUE);
508+ TEST_LT (ret, 0);
509+
510+ error = nih_error_get ();
511+ TEST_EQ (error->number, NIH_DBUS_ERROR);
512+ nih_free (error);
513+
514+ /************************************************************/
515+ TEST_FEATURE ("with correct job details");
516+
517+ /* add instance */
518+ nih_assert (nih_str_array_add (&job_details,
519+ NULL,
520+ NULL,
521+ ""));
522+
523+ ret = control_set_env (NULL, message, job_details, "hello=world", TRUE);
524+ TEST_EQ (ret, 0);
525+
526+ /************************************************************/
527+ /* tidy up */
528+
529+ nih_free (message);
530+}
531+
532+void
533+test_unset_env (void)
534+{
535+ NihDBusMessage *message = NULL;
536+ int ret;
537+ nih_local char **job_details = NULL;
538+ nih_local char **job_details2 = NULL;
539+ JobClass *class;
540+ Job *job;
541+ NihError *error;
542+
543+ TEST_FUNCTION ("control_unset_env");
544+
545+ nih_error_init ();
546+ job_class_init ();
547+ job_class_environment_init ();
548+
549+ no_inherit_env = TRUE;
550+
551+ message = nih_new (NULL, NihDBusMessage);
552+ TEST_NE_P (message, NULL);
553+ message->connection = NULL;
554+ message->message = NULL;
555+
556+ job_details = nih_str_array_new (NULL);
557+ nih_assert (job_details);
558+
559+ job_details2 = nih_str_array_copy (NULL, NULL, job_details);
560+ TEST_NE_P (job_details2, NULL);
561+
562+ nih_assert (nih_str_array_add (&job_details2,
563+ NULL,
564+ NULL,
565+ ""));
566+
567+ class = job_class_new (NULL, "foo", NULL);
568+ TEST_NE_P (class, NULL);
569+
570+ job = job_new (class, "");
571+ TEST_NE_P (job, NULL);
572+
573+ nih_hash_add (job_classes, &class->entry);
574+
575+ assert0 (job_class_environment_set ("hello=world", TRUE));
576+
577+ /************************************************************/
578+ TEST_FEATURE ("with NULL variable name");
579+
580+ ret = control_unset_env (NULL, message, job_details2, NULL);
581+ TEST_LT (ret, 0);
582+
583+ error = nih_error_get ();
584+ TEST_EQ (error->number, NIH_DBUS_ERROR);
585+ nih_free (error);
586+
587+ /************************************************************/
588+ TEST_FEATURE ("with empty variable name");
589+
590+ ret = control_unset_env (NULL, message, job_details2, "");
591+ TEST_LT (ret, 0);
592+
593+ error = nih_error_get ();
594+ TEST_EQ (error->number, NIH_DBUS_ERROR);
595+ nih_free (error);
596+
597+ /************************************************************/
598+ TEST_FEATURE ("with empty job class name");
599+
600+ ret = control_unset_env (NULL, message, job_details2, "hello");
601+ TEST_LT (ret, 0);
602+
603+ error = nih_error_get ();
604+ TEST_EQ (error->number, NIH_DBUS_ERROR);
605+ nih_free (error);
606+
607+ /************************************************************/
608+ TEST_FEATURE ("with missing instance name");
609+
610+ /* add name */
611+ nih_assert (nih_str_array_add (&job_details,
612+ NULL,
613+ NULL,
614+ "foo"));
615+
616+ ret = control_unset_env (NULL, message, job_details, "hello");
617+ TEST_LT (ret, 0);
618+
619+ error = nih_error_get ();
620+ TEST_EQ (error->number, NIH_DBUS_ERROR);
621+ nih_free (error);
622+
623+ /************************************************************/
624+ TEST_FEATURE ("with correct job details");
625+
626+ /* add instance */
627+ nih_assert (nih_str_array_add (&job_details,
628+ NULL,
629+ NULL,
630+ ""));
631+
632+ ret = control_unset_env (NULL, message, job_details, "hello");
633+ TEST_EQ (ret, 0);
634+
635+ /************************************************************/
636+ /* tidy up */
637+
638+ nih_free (message);
639+}
640+
641+void
642+test_reset_env (void)
643+{
644+ NihDBusMessage *message = NULL;
645+ int ret;
646+ nih_local char **job_details = NULL;
647+ nih_local char **job_details2 = NULL;
648+ JobClass *class;
649+ Job *job;
650+ NihError *error;
651+
652+ TEST_FUNCTION ("control_reset_env");
653+
654+ nih_error_init ();
655+ job_class_init ();
656+ job_class_environment_init ();
657+
658+ no_inherit_env = TRUE;
659+
660+ message = nih_new (NULL, NihDBusMessage);
661+ TEST_NE_P (message, NULL);
662+ message->connection = NULL;
663+ message->message = NULL;
664+
665+ job_details = nih_str_array_new (NULL);
666+ nih_assert (job_details);
667+
668+ class = job_class_new (NULL, "foo", NULL);
669+ TEST_NE_P (class, NULL);
670+
671+ job = job_new (class, "");
672+ TEST_NE_P (job, NULL);
673+
674+ nih_hash_add (job_classes, &class->entry);
675+
676+ /************************************************************/
677+ TEST_FEATURE ("with empty job class name");
678+
679+ job_details2 = nih_str_array_copy (NULL, NULL, job_details);
680+ TEST_NE_P (job_details2, NULL);
681+
682+ nih_assert (nih_str_array_add (&job_details2,
683+ NULL,
684+ NULL,
685+ ""));
686+
687+ ret = control_reset_env (NULL, message, job_details2);
688+ TEST_LT (ret, 0);
689+
690+ error = nih_error_get ();
691+ TEST_EQ (error->number, NIH_DBUS_ERROR);
692+ nih_free (error);
693+
694+ /************************************************************/
695+ TEST_FEATURE ("with missing instance name");
696+
697+ /* add name */
698+ nih_assert (nih_str_array_add (&job_details,
699+ NULL,
700+ NULL,
701+ "foo"));
702+
703+ ret = control_reset_env (NULL, message, job_details);
704+ TEST_LT (ret, 0);
705+
706+ error = nih_error_get ();
707+ TEST_EQ (error->number, NIH_DBUS_ERROR);
708+ nih_free (error);
709+
710+ /************************************************************/
711+ TEST_FEATURE ("with correct job details");
712+
713+ /* add instance */
714+ nih_assert (nih_str_array_add (&job_details,
715+ NULL,
716+ NULL,
717+ ""));
718+
719+ ret = control_reset_env (NULL, message, job_details);
720+ TEST_EQ (ret, 0);
721+
722+ /************************************************************/
723+ /* tidy up */
724+
725+ nih_free (message);
726+}
727
728 int
729 main (int argc,
730@@ -2205,5 +2724,11 @@
731 test_get_log_priority ();
732 test_set_log_priority ();
733
734+ test_list_env ();
735+ test_get_env ();
736+ test_set_env ();
737+ test_unset_env ();
738+ test_reset_env ();
739+
740 return 0;
741 }
742
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 }
748 }
749
750+void
751+test_job_find (void)
752+{
753+ ConfFile *file;
754+ ConfSource *source;
755+ Job *job, *ret;
756+ JobClass *class;
757+ Session *session;
758+
759+ TEST_FUNCTION ("job_find");
760+ nih_error_init ();
761+ conf_init ();
762+ job_class_init ();
763+
764+ TEST_HASH_EMPTY (job_classes);
765+
766+ source = conf_source_new (NULL, "/tmp", CONF_JOB_DIR);
767+ TEST_NE_P (source, NULL);
768+
769+ file = conf_file_new (source, "/tmp/test");
770+ TEST_NE_P (file, NULL);
771+
772+ class = file->job = job_class_new (NULL, "test", NULL);
773+ TEST_NE_P (class, NULL);
774+
775+ job = job_new (class, "");
776+ TEST_NE_P (job, NULL);
777+
778+ TEST_HASH_EMPTY (job_classes);
779+ TEST_TRUE (job_class_consider (class));
780+ TEST_HASH_NOT_EMPTY (job_classes);
781+
782+ /***********************************************************/
783+ TEST_FEATURE ("JobClass, no job name or Session");
784+
785+ ret = job_find (NULL, class, NULL, NULL);
786+ TEST_EQ_P (ret, NULL);
787+
788+ /***********************************************************/
789+ TEST_FEATURE ("job class name, no job name or Session");
790+
791+ ret = job_find (NULL, NULL, "test", NULL);
792+ TEST_EQ_P (ret, NULL);
793+
794+ /***********************************************************/
795+ TEST_FEATURE ("JobClass+job name, no Session");
796+
797+ ret = job_find (NULL, class, NULL, "");
798+ TEST_EQ (ret, job);
799+
800+ /***********************************************************/
801+ TEST_FEATURE ("job class name+job name, no Session");
802+
803+ ret = job_find (NULL, NULL, "test", "");
804+ TEST_EQ (ret, job);
805+
806+ /***********************************************************/
807+ /* recreate env */
808+
809+ nih_free (conf_sources);
810+ nih_free (job_classes);
811+
812+ conf_sources = NULL;
813+ job_classes = NULL;
814+
815+ conf_init ();
816+ job_class_init ();
817+
818+ TEST_HASH_EMPTY (job_classes);
819+
820+ source = conf_source_new (NULL, "/tmp", CONF_JOB_DIR);
821+ TEST_NE_P (source, NULL);
822+
823+ session = session_new (NULL, "/abc");
824+ TEST_NE_P (session, NULL);
825+ session->conf_path = NIH_MUST (nih_strdup (session, "/def/ghi"));
826+
827+ source->session = session;
828+
829+ file = conf_file_new (source, "/tmp/test");
830+ TEST_NE_P (file, NULL);
831+
832+ class = file->job = job_class_new (NULL, "test", session);
833+ TEST_NE_P (class, NULL);
834+
835+ job = job_new (class, "");
836+ TEST_NE_P (job, NULL);
837+
838+ TEST_TRUE (job_class_consider (class));
839+ TEST_HASH_NOT_EMPTY (job_classes);
840+
841+ /***********************************************************/
842+ TEST_FEATURE ("JobClass+Session, no job name");
843+
844+ ret = job_find (session, class, NULL, NULL);
845+ TEST_EQ_P (ret, NULL);
846+
847+ /***********************************************************/
848+ TEST_FEATURE ("job class name+Session, no job name");
849+
850+ ret = job_find (session, NULL, "test", NULL);
851+ TEST_EQ_P (ret, NULL);
852+
853+ /***********************************************************/
854+ /* clean up */
855+
856+ nih_free (conf_sources);
857+ nih_free (job_classes);
858+ nih_free (session);
859+
860+ conf_sources = NULL;
861+ job_classes = NULL;
862+
863+ conf_init ();
864+ job_class_init ();
865+}
866+
867
868 void
869 deserialise_ptrace_next (void)
870@@ -7608,6 +7725,8 @@
871
872 test_deserialise_ptrace ();
873
874+ test_job_find ();
875+
876 return 0;
877 }
878

Subscribers

People subscribed via source and target branches