test-unit-file.c revision 79413b673b45adc98dfeaec882bbdda2343cb2f9
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
Copyright 2013 Zbigniew Jędrzejewski-Szmek
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/capability.h>
#include <unistd.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "hashmap.h"
#include "hostname-util.h"
#include "install-printf.h"
#include "install.h"
#include "load-fragment.h"
#include "macro.h"
#include "specifier.h"
#include "string-util.h"
#include "strv.h"
#include "test-helper.h"
#include "user-util.h"
#include "util.h"
static int test_unit_file_get_set(void) {
int r;
Hashmap *h;
Iterator i;
UnitFileList *p;
h = hashmap_new(&string_hash_ops);
assert_se(h);
return EXIT_TEST_SKIP;
}
"unit_file_get_list: %s", strerror(-r));
if (r < 0)
return EXIT_FAILURE;
HASHMAP_FOREACH(p, h, i)
return 0;
}
static void check_execcommand(ExecCommand *c,
const char* path,
const char* argv0,
const char* argv1,
const char* argv2,
bool ignore) {
size_t n;
assert_se(c);
log_info("expect: \"%s\" [\"%s\" \"%s\" \"%s\"]",
n = strv_length(c->argv);
log_info("actual: \"%s\" [\"%s\" \"%s\" \"%s\"]",
if (n > 0)
if (n > 1)
}
static void test_config_parse_exec(void) {
/* int config_parse_exec(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) */
int r;
const char *ccc;
log_info("/* basic test */");
"LValue", 0, "/RValue r1",
&c, NULL);
assert_se(r >= 0);
"LValue", 0, "/RValue///slashes r1///",
&c, NULL);
log_info("/* test slashes */");
assert_se(r >= 0);
c1 = c->command_next;
log_info("/* trailing slash */");
"LValue", 0, "/RValue/ argv0 r1",
&c, NULL);
assert_se(r == 0);
log_info("/* honour_argv0 */");
"LValue", 0, "@/RValue///slashes2 ///argv0 r1",
&c, NULL);
assert_se(r >= 0);
log_info("/* honour_argv0, no args */");
"LValue", 0, "@/RValue",
&c, NULL);
assert_se(r == 0);
log_info("/* no command, whitespace only, reset */");
"LValue", 0, " ",
&c, NULL);
assert_se(r == 0);
log_info("/* ignore && honour_argv0 */");
"LValue", 0, "-@/RValue///slashes3 argv0a r1",
&c, NULL);
assert_se(r >= 0);
c1 = c;
log_info("/* ignore && honour_argv0 */");
"LValue", 0, "@-/RValue///slashes4 argv0b r1",
&c, NULL);
assert_se(r >= 0);
log_info("/* ignore && ignore */");
"LValue", 0, "--/RValue argv0 r1",
&c, NULL);
assert_se(r == 0);
log_info("/* ignore && ignore (2) */");
"LValue", 0, "-@-/RValue argv0 r1",
&c, NULL);
assert_se(r == 0);
log_info("/* semicolon */");
"LValue", 0,
"-@/RValue argv0 r1 ; "
&c, NULL);
assert_se(r >= 0);
log_info("/* two semicolons in a row */");
"LValue", 0,
"-@/RValue argv0 r1 ; ; "
&c, NULL);
assert_se(r >= 0);
/* second command fails because the executable name is ";" */
log_info("/* trailing semicolon */");
"LValue", 0,
"-@/RValue argv0 r1 ; ",
&c, NULL);
assert_se(r >= 0);
log_info("/* trailing semicolon, no whitespace */");
"LValue", 0,
"-@/RValue argv0 r1 ;",
&c, NULL);
assert_se(r >= 0);
log_info("/* trailing semicolon in single quotes */");
"LValue", 0,
"-@/RValue argv0 r1 ';'",
&c, NULL);
assert_se(r >= 0);
log_info("/* escaped semicolon */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* escaped semicolon with following arg */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* escaped semicolon as part of an expression */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* encoded semicolon */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* quoted semicolon */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* quoted semicolon with following arg */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* spaces in the filename */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* spaces in the filename, no args */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* spaces in the filename, everything quoted */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* escaped spaces in the filename */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* escaped spaces in the filename (2) */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
/* \\x is an incomplete hexadecimal sequence, invalid because of the slash */
char path[] = "/path\\X";
"LValue", 0, path,
&c, NULL);
assert_se(r == 0);
}
log_info("/* valid character: \\s */");
"LValue", 0, "/path\\s",
&c, NULL);
assert_se(r >= 0);
log_info("/* quoted backslashes */");
"LValue", 0,
&c, NULL);
assert_se(r >= 0);
log_info("/* trailing backslash: \\ */");
/* backslash is invalid */
"LValue", 0, "/path\\",
&c, NULL);
assert_se(r == 0);
log_info("/* missing ending ' */");
"LValue", 0, "/path 'foo",
&c, NULL);
assert_se(r == 0);
log_info("/* missing ending ' with trailing backslash */");
"LValue", 0, "/path 'foo\\",
&c, NULL);
assert_se(r == 0);
log_info("/* invalid space between modifiers */");
"LValue", 0, "- /path",
&c, NULL);
assert_se(r == 0);
log_info("/* only modifiers, no path */");
"LValue", 0, "-",
&c, NULL);
assert_se(r == 0);
log_info("/* empty argument, reset */");
"LValue", 0, "",
&c, NULL);
assert_se(r == 0);
}
#define env_file_1 \
"a=a\n" \
"b=b\\\n" \
"c\n" \
"d=d\\\n" \
"e\\\n" \
"f\n" \
"g=g\\ \n" \
"h=h\n" \
"i=i\\"
#define env_file_2 \
"a=a\\\n"
#define env_file_3 \
"#--nouser-config \\\n" \
"normal=line"
#define env_file_4 \
"# Generated\n" \
"\n" \
"HWMON_MODULES=\"coretemp f71882fg\"\n" \
"\n" \
"# For compatibility reasons\n" \
"\n" \
"MODULE_0=coretemp\n" \
"MODULE_1=f71882fg"
#define env_file_5 \
"a=\n" \
"b="
static void test_load_env_file_1(void) {
int r;
char name[] = "/tmp/test-load-env-file.XXXXXX";
_cleanup_close_ int fd;
assert_se(r == 0);
}
static void test_load_env_file_2(void) {
int r;
char name[] = "/tmp/test-load-env-file.XXXXXX";
_cleanup_close_ int fd;
assert_se(r == 0);
}
static void test_load_env_file_3(void) {
int r;
char name[] = "/tmp/test-load-env-file.XXXXXX";
_cleanup_close_ int fd;
assert_se(r == 0);
}
static void test_load_env_file_4(void) {
char name[] = "/tmp/test-load-env-file.XXXXXX";
_cleanup_close_ int fd;
int r;
assert_se(r == 0);
}
static void test_load_env_file_5(void) {
int r;
char name[] = "/tmp/test-load-env-file.XXXXXX";
_cleanup_close_ int fd;
assert_se(r == 0);
}
static void test_install_printf(void) {
path[] = "/run/systemd/system/name.service";
char name3[] = "name@inst.service",
path3[] = "/run/systemd/system/name.service";
do { \
_cleanup_free_ char *t = NULL; \
_cleanup_free_ char \
if (result) { \
printf("%s\n", t); \
} while(false)
}
}
static void test_config_parse_bounding_set(void) {
/* int config_parse_bounding_set(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) */
int r;
"CapabilityBoundingSet", 0, "CAP_NET_RAW",
assert_se(r >= 0);
"CapabilityBoundingSet", 0, "CAP_NET_ADMIN",
assert_se(r >= 0);
"CapabilityBoundingSet", 0, "",
assert_se(r >= 0);
"CapabilityBoundingSet", 0, "~",
assert_se(r >= 0);
"CapabilityBoundingSet", 0, " 'CAP_NET_RAW' WAT_CAP??? CAP_NET_ADMIN CAP'_trailing_garbage",
assert_se(r >= 0);
}
static void test_config_parse_rlimit(void) {
assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55", rl, NULL) >= 0);
assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity", rl, NULL) >= 0);
assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "56", rl, NULL) >= 0);
assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "57s", rl, NULL) >= 0);
assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "infinity", rl, NULL) >= 0);
assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "1234ms", rl, NULL) >= 0);
assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58", rl, NULL) >= 0);
assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s", rl, NULL) >= 0);
assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity", rl, NULL) >= 0);
assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "2345ms", rl, NULL) >= 0);
}
static void test_config_parse_pass_environ(void) {
/* int config_parse_pass_environ(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) */
int r;
"PassEnvironment", 0, "A B",
assert_se(r >= 0);
"PassEnvironment", 0, "",
assert_se(r >= 0);
"PassEnvironment", 0, "'invalid name' 'normal_name' A=1 \\",
assert_se(r >= 0);
}
int r;
log_open();
r = test_unit_file_get_set();
return r;
}