2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2017-11-21 17:52:31 +01:00
|
|
|
|
|
|
|
#include "alloc-util.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "specifier.h"
|
2020-05-07 16:25:36 +02:00
|
|
|
#include "stdio-util.h"
|
2017-11-21 17:52:31 +01:00
|
|
|
#include "string-util.h"
|
|
|
|
#include "strv.h"
|
2018-09-13 14:31:13 +02:00
|
|
|
#include "tests.h"
|
2017-11-21 17:52:31 +01:00
|
|
|
|
|
|
|
static void test_specifier_escape_one(const char *a, const char *b) {
|
|
|
|
_cleanup_free_ char *x = NULL;
|
|
|
|
|
|
|
|
x = specifier_escape(a);
|
|
|
|
assert_se(streq_ptr(x, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_specifier_escape(void) {
|
2020-05-07 16:25:36 +02:00
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
|
2017-11-21 17:52:31 +01:00
|
|
|
test_specifier_escape_one(NULL, NULL);
|
|
|
|
test_specifier_escape_one("", "");
|
|
|
|
test_specifier_escape_one("%", "%%");
|
|
|
|
test_specifier_escape_one("foo bar", "foo bar");
|
|
|
|
test_specifier_escape_one("foo%bar", "foo%%bar");
|
|
|
|
test_specifier_escape_one("%%%%%", "%%%%%%%%%%");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_specifier_escape_strv_one(char **a, char **b) {
|
|
|
|
_cleanup_strv_free_ char **x = NULL;
|
|
|
|
|
|
|
|
assert_se(specifier_escape_strv(a, &x) >= 0);
|
|
|
|
assert_se(strv_equal(x, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_specifier_escape_strv(void) {
|
2020-05-07 16:25:36 +02:00
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
|
2017-11-21 17:52:31 +01:00
|
|
|
test_specifier_escape_strv_one(NULL, NULL);
|
|
|
|
test_specifier_escape_strv_one(STRV_MAKE(NULL), STRV_MAKE(NULL));
|
|
|
|
test_specifier_escape_strv_one(STRV_MAKE(""), STRV_MAKE(""));
|
|
|
|
test_specifier_escape_strv_one(STRV_MAKE("foo"), STRV_MAKE("foo"));
|
|
|
|
test_specifier_escape_strv_one(STRV_MAKE("%"), STRV_MAKE("%%"));
|
2020-05-07 16:25:36 +02:00
|
|
|
test_specifier_escape_strv_one(STRV_MAKE("foo", "%", "foo%", "%foo", "foo%foo", "quux", "%%%"),
|
|
|
|
STRV_MAKE("foo", "%%", "foo%%", "%%foo", "foo%%foo", "quux", "%%%%%%"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Any specifier functions which don't need an argument. */
|
|
|
|
static const Specifier specifier_table[] = {
|
2020-11-20 15:15:24 +01:00
|
|
|
COMMON_SYSTEM_SPECIFIERS,
|
|
|
|
|
|
|
|
COMMON_CREDS_SPECIFIERS,
|
2020-05-07 16:25:36 +02:00
|
|
|
{ 'h', specifier_user_home, NULL },
|
|
|
|
|
2020-11-20 15:15:24 +01:00
|
|
|
COMMON_TMP_SPECIFIERS,
|
2020-05-07 16:25:36 +02:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_specifiers(void) {
|
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
|
|
|
|
for (const Specifier *s = specifier_table; s->specifier; s++) {
|
|
|
|
char spec[3];
|
|
|
|
_cleanup_free_ char *resolved = NULL;
|
|
|
|
|
|
|
|
xsprintf(spec, "%%%c", s->specifier);
|
|
|
|
|
|
|
|
assert_se(specifier_printf(spec, specifier_table, NULL, &resolved) >= 0);
|
|
|
|
|
|
|
|
log_info("%%%c → %s", s->specifier, resolved);
|
|
|
|
}
|
2017-11-21 17:52:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2018-09-13 14:31:13 +02:00
|
|
|
test_setup_logging(LOG_DEBUG);
|
2017-11-21 17:52:31 +01:00
|
|
|
|
|
|
|
test_specifier_escape();
|
|
|
|
test_specifier_escape_strv();
|
2020-05-07 16:25:36 +02:00
|
|
|
test_specifiers();
|
2017-11-21 17:52:31 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|