~upstart-devel/upstart/trunk

« back to all changes in this revision

Viewing changes to init/tests/test_xdg.c

* Merge of lp:~jamesodhunt/upstart/remove-basic-user-sessions.
  (Note: won't compile due to dependency of commit about to be applied).

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#include <nih/string.h>
23
23
#include <nih/test.h>
24
24
 
 
25
#include <limits.h>
25
26
#include <stdlib.h>
26
 
#include <limits.h>
 
27
#include <sys/types.h>
 
28
#include <sys/stat.h>
 
29
#include <unistd.h>
27
30
 
28
31
#include "xdg.h"
29
32
 
30
33
void
 
34
_test_dir_created (char * dirname) {
 
35
        struct stat statbuf;
 
36
 
 
37
        TEST_EQ (lstat (dirname, &statbuf), 0);
 
38
        TEST_TRUE (S_ISDIR (statbuf.st_mode));
 
39
        
 
40
        /* Check that the created directory has 0700 permissions, as per XDG spec */
 
41
        TEST_EQ (0700, statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
 
42
}
 
43
 
 
44
void
31
45
test_get_home_subdir (void)
32
46
{
33
47
        char    dirname[PATH_MAX];
40
54
        TEST_EQ (unsetenv ("HOME"), 0);
41
55
 
42
56
        TEST_ALLOC_FAIL {
43
 
                dir = get_home_subdir ("test");
 
57
                dir = get_home_subdir ("test", FALSE);
44
58
                TEST_EQ_P (dir, NULL);
45
59
        }
46
60
 
54
68
                        expected = NIH_MUST (nih_sprintf (NULL, "%s/test", dirname));
55
69
                }
56
70
 
57
 
                dir = get_home_subdir ("test");
 
71
                dir = get_home_subdir ("test", FALSE);
58
72
 
59
73
                if (test_alloc_failed) {
60
74
                        TEST_EQ_P (dir, NULL);
69
83
}
70
84
 
71
85
void
72
 
test_get_config_home (void)
 
86
_test_get_home (char * env_var_name, char * dir_name, char * (*function)(void))
73
87
{
74
88
        char   dirname[PATH_MAX];
 
89
        char   onemore[PATH_MAX];
75
90
        char * outname;
76
91
        char * expected;
77
92
 
78
 
        TEST_FUNCTION ("xdg_get_config_home");
79
 
 
80
93
        TEST_FEATURE ("with HOME set and without environment override");
81
94
        TEST_FILENAME (dirname);
82
95
        TEST_EQ (setenv ("HOME", dirname, 1), 0);
83
 
        TEST_EQ (unsetenv ("XDG_CONFIG_HOME"), 0);
 
96
        TEST_EQ (mkdir (dirname, 0755), 0);
 
97
        TEST_EQ (unsetenv (env_var_name), 0);
84
98
        TEST_ALLOC_FAIL {
85
99
                TEST_ALLOC_SAFE {
86
 
                        expected = NIH_MUST (nih_sprintf (NULL, "%s/.config", dirname));
 
100
                        expected = NIH_MUST (nih_sprintf (NULL, "%s/%s", dirname, dir_name));
87
101
                }
88
102
 
89
103
                outname = NULL;
90
 
                outname = xdg_get_config_home ();
 
104
                outname = function ();
91
105
 
92
106
                if (! test_alloc_failed) {
93
107
                        TEST_EQ_STR (outname, expected);
 
108
                        _test_dir_created (expected);
94
109
                } else {
95
110
                        TEST_EQ_P (outname, NULL);
96
111
                }
98
113
                if (outname)
99
114
                        nih_free (outname);
100
115
 
101
 
                nih_free(expected);
 
116
                rmdir (expected);
 
117
                nih_free (expected);
102
118
        }
103
119
 
104
120
        TEST_FEATURE ("with HOME set and with empty environment override");
105
 
        TEST_EQ (setenv ("XDG_CONFIG_HOME", "", 1), 0);
106
 
 
107
 
 
108
 
        TEST_ALLOC_FAIL {
109
 
                TEST_ALLOC_SAFE {
110
 
                        expected = NIH_MUST (nih_sprintf (NULL, "%s/.config", dirname));
111
 
                }
112
 
                outname = NULL;
113
 
                outname = xdg_get_config_home();
114
 
 
115
 
                if (test_alloc_failed) {
116
 
                        TEST_EQ_P (outname, NULL);
117
 
                } else {
118
 
                        TEST_EQ_STR (outname, expected);
119
 
                }
120
 
                if (outname)
121
 
                        nih_free (outname);
122
 
                nih_free(expected);
 
121
        TEST_EQ (setenv (env_var_name, "", 1), 0);
 
122
 
 
123
 
 
124
        TEST_ALLOC_FAIL {
 
125
                TEST_ALLOC_SAFE {
 
126
                        expected = NIH_MUST (nih_sprintf (NULL, "%s/%s", dirname, dir_name));
 
127
                }
 
128
                outname = NULL;
 
129
                outname = function ();
 
130
 
 
131
                if (test_alloc_failed) {
 
132
                        TEST_EQ_P (outname, NULL);
 
133
                } else {
 
134
                        TEST_EQ_STR (outname, expected);
 
135
                        _test_dir_created (expected);
 
136
                }
 
137
                if (outname)
 
138
                        nih_free (outname);
 
139
                rmdir (expected);
 
140
                nih_free (expected);
 
141
        }
 
142
 
 
143
        TEST_FEATURE ("with HOME set and with relative environment override");
 
144
        TEST_EQ (setenv (env_var_name, "../", 1), 0);
 
145
 
 
146
 
 
147
        TEST_ALLOC_FAIL {
 
148
                TEST_ALLOC_SAFE {
 
149
                        expected = NIH_MUST (nih_sprintf (NULL, "%s/%s", dirname, dir_name));
 
150
                }
 
151
                outname = NULL;
 
152
                outname = function ();
 
153
 
 
154
                if (test_alloc_failed) {
 
155
                        TEST_EQ_P (outname, NULL);
 
156
                } else {
 
157
                        TEST_EQ_STR (outname, expected);
 
158
                        _test_dir_created (expected);
 
159
                }
 
160
                if (outname)
 
161
                        nih_free (outname);
 
162
                rmdir (expected);
 
163
                nih_free (expected);
123
164
        }
124
165
 
125
166
        TEST_FEATURE ("with HOME set and with environment override");
126
 
        expected = NIH_MUST (nih_strdup (NULL, "/home/me/.config-test"));
127
 
        TEST_EQ (setenv ("XDG_CONFIG_HOME", expected, 1), 0);
 
167
        TEST_FILENAME (onemore);
 
168
        expected = NIH_MUST (nih_sprintf (NULL, "%s", onemore));
 
169
        TEST_EQ (setenv (env_var_name, expected, 1), 0);
128
170
 
129
171
        TEST_ALLOC_FAIL {
130
172
                outname = NULL;
131
 
                outname = xdg_get_config_home();
 
173
                outname = function ();
132
174
 
133
175
                if (test_alloc_failed) {
134
176
                        TEST_EQ_P (outname, NULL);
135
177
                } else {
136
178
                        TEST_EQ_STR (outname, expected);
 
179
                        _test_dir_created (expected);
137
180
                }
138
181
                if (outname)
139
182
                        nih_free (outname);
 
183
                rmdir (expected);
140
184
        }
141
185
 
 
186
        TEST_EQ (rmdir (dirname), 0);
 
187
 
142
188
        TEST_FEATURE ("without HOME set and with environment override");
143
189
        TEST_EQ (unsetenv ("HOME"), 0);
144
190
 
145
191
        TEST_ALLOC_FAIL {
146
192
                outname = NULL;
147
 
                outname = xdg_get_config_home();
 
193
                outname = function ();
148
194
 
149
195
                if (test_alloc_failed) {
150
196
                        TEST_EQ_P (outname, NULL);
151
197
                } else {
152
198
                        TEST_EQ_STR (outname, expected);
 
199
                        _test_dir_created (expected);
153
200
                }
154
201
                if (outname)
155
202
                        nih_free (outname);
 
203
                rmdir (expected);
156
204
        }
157
205
        nih_free(expected);
158
206
 
159
207
        TEST_FEATURE ("without HOME set and with empty environment override");
160
 
        TEST_EQ (setenv ("XDG_CONFIG_HOME", "", 1), 0);
 
208
        TEST_EQ (setenv (env_var_name, "", 1), 0);
161
209
 
162
210
        TEST_ALLOC_FAIL {
163
211
                outname = NULL;
164
 
                outname = xdg_get_config_home();
 
212
                outname = function ();
165
213
                TEST_EQ_P (outname, NULL);
166
214
        }
167
215
 
168
216
        TEST_FEATURE ("without HOME set and without environment override");
169
 
        TEST_EQ (unsetenv ("XDG_CONFIG_HOME"), 0);
 
217
        TEST_EQ (unsetenv (env_var_name), 0);
170
218
        TEST_ALLOC_FAIL {
171
219
                outname = NULL;
172
 
                outname = xdg_get_config_home();
 
220
                outname = function ();
173
221
                TEST_EQ_P (outname, NULL);
174
222
        }
175
223
}
176
224
 
177
225
void
 
226
test_get_config_home (void)
 
227
{
 
228
        TEST_FUNCTION ("xdg_get_config_home");
 
229
        _test_get_home ("XDG_CONFIG_HOME", ".config", &xdg_get_config_home);
 
230
 
 
231
}
 
232
 
 
233
void
 
234
test_get_cache_home (void)
 
235
{
 
236
        TEST_FUNCTION ("xdg_get_cache_home");
 
237
        _test_get_home ("XDG_CACHE_HOME", ".cache", &xdg_get_cache_home);
 
238
}
 
239
 
 
240
void
178
241
test_get_config_dirs (void)
179
242
{
180
243
        char   **dirs = NULL;
260
323
        TEST_FEATURE ("with HOME set");
261
324
        TEST_FILENAME (dirname);
262
325
        TEST_EQ (setenv ("HOME", dirname, 1), 0);
 
326
        TEST_EQ (mkdir (dirname, 0755), 0);
263
327
        TEST_EQ (unsetenv ("XDG_CONFIG_HOME"), 0);
264
328
        TEST_EQ (unsetenv ("XDG_CONFIG_DIRS"), 0);
265
329
 
266
330
        TEST_ALLOC_FAIL {
267
331
                TEST_ALLOC_SAFE {
268
 
                        dirs = NULL;
269
332
                        expected = nih_str_array_new (NULL);
270
333
                        path = NIH_MUST (nih_sprintf (NULL, "%s/.config/upstart", dirname));
271
334
                        assert (nih_str_array_add (&expected, NULL, NULL, path));
282
345
                        TEST_EQ_P (dirs, NULL);
283
346
                } else {
284
347
                        TEST_EQ_STR (dirs[0], expected[0]);
 
348
                        //_test_dir_created (expected[0]);
285
349
                        TEST_EQ_STR (dirs[1], expected[1]);
286
350
                        TEST_EQ_STR (dirs[2], "/etc/xdg/upstart");
287
351
                        TEST_EQ_STR (dirs[3], SYSTEM_USERCONFDIR);
288
352
                        TEST_EQ (dirs[4], NULL);
289
353
                        nih_free (dirs);
290
354
                }
291
 
                nih_free(expected);
292
 
        }
293
 
 
 
355
                TEST_ALLOC_SAFE {
 
356
                        rmdir (expected[0]);
 
357
                        path = nih_sprintf (NULL, "%s/.config", dirname);
 
358
                        rmdir (path);
 
359
                        nih_free (path);
 
360
                        nih_free (expected);
 
361
                }
 
362
        }
 
363
 
 
364
        TEST_FEATURE ("with some invalid XDG_CONFIG_DIRS");
 
365
        TEST_EQ (setenv ("XDG_CONFIG_DIRS", "/etc/xdg/xdg-subdir:../haha:/etc/xdg", 1), 0);
 
366
 
 
367
        TEST_ALLOC_FAIL {
 
368
                TEST_ALLOC_SAFE {
 
369
                        expected = nih_str_array_new (NULL);
 
370
                        path = NIH_MUST (nih_sprintf (NULL, "%s/.config/upstart", dirname));
 
371
                        assert (nih_str_array_add (&expected, NULL, NULL, path));
 
372
                        nih_free(path);
 
373
                        path = NIH_MUST (nih_sprintf (NULL, "%s/.init", dirname));
 
374
                        assert (nih_str_array_add (&expected, NULL, NULL, path));
 
375
                        nih_free(path);
 
376
                }
 
377
 
 
378
                dirs = NULL;
 
379
                dirs = get_user_upstart_dirs ();
 
380
 
 
381
                if (test_alloc_failed) {
 
382
                        TEST_EQ_P (dirs, NULL);
 
383
                } else {
 
384
                        TEST_EQ_STR (dirs[0], expected[0]);
 
385
                        _test_dir_created (expected[0]);
 
386
                        TEST_EQ_STR (dirs[1], expected[1]);
 
387
                        TEST_EQ_STR (dirs[2], "/etc/xdg/xdg-subdir/upstart");
 
388
                        TEST_EQ_STR (dirs[3], "/etc/xdg/upstart");
 
389
                        TEST_EQ_STR (dirs[4], SYSTEM_USERCONFDIR);
 
390
                        TEST_EQ (dirs[5], NULL);
 
391
                        nih_free (dirs);
 
392
                }
 
393
                TEST_ALLOC_SAFE {
 
394
                        rmdir (expected[0]);
 
395
                        path = nih_sprintf (NULL, "%s/.config", dirname);
 
396
                        rmdir (path);
 
397
                        nih_free (path);
 
398
                        nih_free (expected);
 
399
                }
 
400
        }
 
401
        TEST_EQ (rmdir (dirname), 0);
 
402
}
 
403
 
 
404
void
 
405
test_get_user_log_dir (void)
 
406
{
 
407
        char dirname[PATH_MAX];
 
408
        char         *expected;
 
409
        char             *path;
 
410
 
 
411
        TEST_FUNCTION ("get_user_log_dir");
 
412
        TEST_FEATURE ("with HOME set");
 
413
        TEST_FILENAME (dirname);
 
414
        TEST_EQ (setenv ("HOME", dirname, 1), 0);
 
415
        TEST_EQ (mkdir (dirname, 0755), 0);
 
416
        TEST_EQ (unsetenv ("XDG_CACHE_HOME"), 0);
 
417
 
 
418
        expected = nih_sprintf (NULL, "%s/.cache/upstart", dirname);
 
419
 
 
420
        TEST_ALLOC_FAIL {
 
421
                path = get_user_log_dir ();
 
422
                if (test_alloc_failed) {
 
423
                        TEST_EQ_P (path, NULL);
 
424
                } else {
 
425
                        TEST_EQ_STR (path, expected);
 
426
                        _test_dir_created (expected);
 
427
                        nih_free (path);
 
428
                }
 
429
        }
 
430
        rmdir (expected);
 
431
        nih_free (expected);
 
432
        path = nih_sprintf (NULL, "%s/.cache", dirname);
 
433
        rmdir (path);
 
434
        nih_free (path);
 
435
        rmdir (dirname);
294
436
}
295
437
 
296
438
int
301
443
        test_get_config_home ();
302
444
        test_get_config_dirs ();
303
445
        test_get_user_upstart_dirs ();
 
446
        test_get_cache_home ();
 
447
        test_get_user_log_dir ();
304
448
 
305
449
        return 0;
306
450
}