~ubuntu-branches/ubuntu/trusty/systemd/trusty

« back to all changes in this revision

Viewing changes to src/test/test-unit-file.c

Tags: upstream-202
ImportĀ upstreamĀ versionĀ 202

Show diffs side-by-side

added added

removed removed

Lines of Context:
85
85
        ExecCommand *c = NULL, *c1;
86
86
 
87
87
        /* basic test */
88
 
        r = config_parse_exec("fake", 1, "section",
 
88
        r = config_parse_exec(NULL, "fake", 1, "section",
89
89
                              "LValue", 0, "/RValue r1",
90
90
                              &c, NULL);
91
91
        assert_se(r >= 0);
92
92
        check_execcommand(c, "/RValue", "/RValue", "r1", false);
93
93
 
94
 
        r = config_parse_exec("fake", 2, "section",
 
94
        r = config_parse_exec(NULL, "fake", 2, "section",
95
95
                              "LValue", 0, "/RValue///slashes/// r1",
96
96
                              &c, NULL);
97
97
       /* test slashes */
101
101
                          "r1", false);
102
102
 
103
103
        /* honour_argv0 */
104
 
        r = config_parse_exec("fake", 3, "section",
 
104
        r = config_parse_exec(NULL, "fake", 3, "section",
105
105
                              "LValue", 0, "@/RValue///slashes2/// argv0 r1",
106
106
                              &c, NULL);
107
107
        assert_se(r >= 0);
109
109
        check_execcommand(c1, "/RValue/slashes2", "argv0", "r1", false);
110
110
 
111
111
        /* ignore && honour_argv0 */
112
 
        r = config_parse_exec("fake", 4, "section",
 
112
        r = config_parse_exec(NULL, "fake", 4, "section",
113
113
                              "LValue", 0, "-@/RValue///slashes3/// argv0a r1",
114
114
                              &c, NULL);
115
115
        assert_se(r >= 0);
118
118
                          "/RValue/slashes3", "argv0a", "r1", true);
119
119
 
120
120
        /* ignore && honour_argv0 */
121
 
        r = config_parse_exec("fake", 4, "section",
 
121
        r = config_parse_exec(NULL, "fake", 4, "section",
122
122
                              "LValue", 0, "@-/RValue///slashes4/// argv0b r1",
123
123
                              &c, NULL);
124
124
        assert_se(r >= 0);
127
127
                          "/RValue/slashes4", "argv0b", "r1", true);
128
128
 
129
129
        /* ignore && ignore */
130
 
        r = config_parse_exec("fake", 4, "section",
 
130
        r = config_parse_exec(NULL, "fake", 4, "section",
131
131
                              "LValue", 0, "--/RValue argv0 r1",
132
132
                              &c, NULL);
133
133
        assert_se(r == 0);
134
134
        assert_se(c1->command_next == NULL);
135
135
 
136
136
        /* ignore && ignore */
137
 
        r = config_parse_exec("fake", 4, "section",
 
137
        r = config_parse_exec(NULL, "fake", 4, "section",
138
138
                              "LValue", 0, "-@-/RValue argv0 r1",
139
139
                              &c, NULL);
140
140
        assert_se(r == 0);
141
141
        assert_se(c1->command_next == NULL);
142
142
 
143
143
        /* semicolon */
144
 
        r = config_parse_exec("fake", 5, "section",
 
144
        r = config_parse_exec(NULL, "fake", 5, "section",
145
145
                              "LValue", 0,
146
146
                              "-@/RValue argv0 r1 ; "
147
147
                              "/goo/goo boo",
156
156
                          "/goo/goo", "/goo/goo", "boo", false);
157
157
 
158
158
        /* trailing semicolon */
159
 
        r = config_parse_exec("fake", 5, "section",
 
159
        r = config_parse_exec(NULL, "fake", 5, "section",
160
160
                              "LValue", 0,
161
161
                              "-@/RValue argv0 r1 ; ",
162
162
                              &c, NULL);
168
168
        assert_se(c1->command_next == NULL);
169
169
 
170
170
        /* escaped semicolon */
171
 
        r = config_parse_exec("fake", 5, "section",
 
171
        r = config_parse_exec(NULL, "fake", 5, "section",
172
172
                              "LValue", 0,
173
173
                              "/usr/bin/find \\;",
174
174
                              &c, NULL);
180
180
        exec_command_free_list(c);
181
181
}
182
182
 
183
 
#define env_file_1 \
184
 
        "a\n"      \
185
 
        "b\\\n"    \
186
 
        "c\n"      \
187
 
        "d\\\n"    \
188
 
        "e\\\n"    \
189
 
        "f\n"      \
190
 
        "g\\ \n"   \
191
 
        "h\n"      \
192
 
        "i\\"
 
183
#define env_file_1                              \
 
184
        "a=a\n"                                 \
 
185
        "b=b\\\n"                               \
 
186
        "c\n"                                   \
 
187
        "d=d\\\n"                               \
 
188
        "e\\\n"                                 \
 
189
        "f\n"                                   \
 
190
        "g=g\\ \n"                              \
 
191
        "h=h\n"                                 \
 
192
        "i=i\\"
193
193
 
194
 
#define env_file_2 \
195
 
        "a\\\n"
 
194
#define env_file_2                              \
 
195
        "a=a\\\n"
196
196
 
197
197
#define env_file_3 \
198
198
        "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \
199
199
        "#--nouser-config                                     \\\n" \
200
200
        "normal=line"
201
201
 
 
202
#define env_file_4 \
 
203
       "# Generated\n" \
 
204
       "\n" \
 
205
       "HWMON_MODULES=\"coretemp f71882fg\"\n" \
 
206
       "\n" \
 
207
       "# For compatibility reasons\n" \
 
208
       "\n" \
 
209
       "MODULE_0=coretemp\n" \
 
210
       "MODULE_1=f71882fg"
 
211
 
 
212
 
202
213
static void test_load_env_file_1(void) {
203
 
        char _cleanup_strv_free_ **data = NULL;
 
214
        _cleanup_strv_free_ char **data = NULL;
204
215
        int r;
205
216
 
206
217
        char name[] = "/tmp/test-load-env-file.XXXXXX";
207
 
        int _cleanup_close_ fd = mkstemp(name);
 
218
        _cleanup_close_ int fd = mkstemp(name);
208
219
        assert(fd >= 0);
209
220
        assert_se(write(fd, env_file_1, sizeof(env_file_1)) == sizeof(env_file_1));
210
221
 
211
 
        r = load_env_file(name, &data);
 
222
        r = load_env_file(name, NULL, &data);
212
223
        assert(r == 0);
213
 
        assert(streq(data[0], "a"));
214
 
        assert(streq(data[1], "bc"));
215
 
        assert(streq(data[2], "def"));
216
 
        assert(streq(data[3], "g\\"));
217
 
        assert(streq(data[4], "h"));
218
 
        assert(streq(data[5], "i\\"));
 
224
        assert(streq(data[0], "a=a"));
 
225
        assert(streq(data[1], "b=bc"));
 
226
        assert(streq(data[2], "d=def"));
 
227
        assert(streq(data[3], "g=g "));
 
228
        assert(streq(data[4], "h=h"));
 
229
        assert(streq(data[5], "i=i"));
219
230
        assert(data[6] == NULL);
220
231
        unlink(name);
221
232
}
222
233
 
223
234
static void test_load_env_file_2(void) {
224
 
        char _cleanup_strv_free_ **data = NULL;
 
235
        _cleanup_strv_free_ char **data = NULL;
225
236
        int r;
226
237
 
227
238
        char name[] = "/tmp/test-load-env-file.XXXXXX";
228
 
        int _cleanup_close_ fd = mkstemp(name);
 
239
        _cleanup_close_ int fd = mkstemp(name);
229
240
        assert(fd >= 0);
230
241
        assert_se(write(fd, env_file_2, sizeof(env_file_2)) == sizeof(env_file_2));
231
242
 
232
 
        r = load_env_file(name, &data);
 
243
        r = load_env_file(name, NULL, &data);
233
244
        assert(r == 0);
234
 
        assert(streq(data[0], "a"));
 
245
        assert(streq(data[0], "a=a"));
235
246
        assert(data[1] == NULL);
236
247
        unlink(name);
237
248
}
238
249
 
239
250
static void test_load_env_file_3(void) {
240
 
        char _cleanup_strv_free_ **data = NULL;
 
251
        _cleanup_strv_free_ char **data = NULL;
241
252
        int r;
242
253
 
243
254
        char name[] = "/tmp/test-load-env-file.XXXXXX";
244
 
        int _cleanup_close_ fd = mkstemp(name);
 
255
        _cleanup_close_ int fd = mkstemp(name);
245
256
        assert(fd >= 0);
246
257
        assert_se(write(fd, env_file_3, sizeof(env_file_3)) == sizeof(env_file_3));
247
258
 
248
 
        r = load_env_file(name, &data);
 
259
        r = load_env_file(name, NULL, &data);
249
260
        assert(r == 0);
250
261
        assert(data == NULL);
251
262
        unlink(name);
252
263
}
253
264
 
 
265
static void test_load_env_file_4(void) {
 
266
        _cleanup_strv_free_ char **data = NULL;
 
267
        int r;
 
268
 
 
269
        char name[] = "/tmp/test-load-env-file.XXXXXX";
 
270
        _cleanup_close_ int fd = mkstemp(name);
 
271
        assert(fd >= 0);
 
272
        assert_se(write(fd, env_file_4, sizeof(env_file_4)) == sizeof(env_file_4));
 
273
 
 
274
        r = load_env_file(name, NULL, &data);
 
275
        assert(r == 0);
 
276
        assert(streq(data[0], "HWMON_MODULES=coretemp f71882fg"));
 
277
        assert(streq(data[1], "MODULE_0=coretemp"));
 
278
        assert(streq(data[2], "MODULE_1=f71882fg"));
 
279
        assert(data[3] == NULL);
 
280
        unlink(name);
 
281
}
 
282
 
 
283
 
254
284
#pragma GCC diagnostic push
255
285
#pragma GCC diagnostic ignored "-Wnonnull"
256
286
 
257
287
static void test_install_printf(void) {
258
288
        char    name[] = "name.service",
259
 
                path[] = "/run/systemd/systemd/name.service",
 
289
                path[] = "/run/systemd/system/name.service",
260
290
                user[] = "xxxx-no-such-user";
261
291
        InstallInfo i = {name, path, user};
262
292
        InstallInfo i2 = {name, path, NULL};
263
293
        char    name3[] = "name@inst.service",
264
 
                path3[] = "/run/systemd/systemd/name.service";
 
294
                path3[] = "/run/systemd/system/name.service";
265
295
        InstallInfo i3 = {name3, path3, user};
266
296
        InstallInfo i4 = {name3, path3, NULL};
267
297
 
268
 
        char _cleanup_free_ *mid, *bid, *host;
 
298
        _cleanup_free_ char *mid, *bid, *host;
269
299
 
270
300
        assert_se((mid = specifier_machine_id('m', NULL, NULL)));
271
301
        assert_se((bid = specifier_boot_id('b', NULL, NULL)));
272
302
        assert_se((host = gethostname_malloc()));
273
303
 
274
304
#define expect(src, pattern, result)                                    \
275
 
        {                                                               \
276
 
                char _cleanup_free_ *t = install_full_printf(&src, pattern); \
277
 
                char _cleanup_free_                                     \
 
305
        do {                                                            \
 
306
                _cleanup_free_ char *t = install_full_printf(&src, pattern); \
 
307
                _cleanup_free_ char                                     \
278
308
                        *d1 = strdup(i.name),                           \
279
309
                        *d2 = strdup(i.path),                           \
280
310
                        *d3 = strdup(i.user);                           \
289
319
                strcpy(i.name, d1);                                     \
290
320
                strcpy(i.path, d2);                                     \
291
321
                strcpy(i.user, d3);                                     \
292
 
        }
 
322
        } while(false)
293
323
 
294
324
        assert_se(setenv("USER", "root", 1) == 0);
295
325
 
322
352
 
323
353
int main(int argc, char *argv[]) {
324
354
 
 
355
        log_parse_environment();
 
356
        log_open();
 
357
 
325
358
        test_unit_file_get_set();
326
359
        test_config_parse_exec();
327
360
        test_load_env_file_1();
328
361
        test_load_env_file_2();
329
362
        test_load_env_file_3();
 
363
        test_load_env_file_4();
330
364
        test_install_printf();
331
365
 
332
366
        return 0;