summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2022-03-08 11:38:46 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2022-03-29 16:17:56 +0200
commit7962116fc8a2572c5c89904ac50fe99c8101f28f (patch)
tree494c4eafadea42753910fc753ba363398049a6c6
parent3a84a3c9dfde5182398a6d0863a2b2fe90a936b1 (diff)
downloadsystemd-7962116fc8a2572c5c89904ac50fe99c8101f28f.tar.gz
shared/specifier: clarify and add test for missing data
In systemd.unit we document that unset fields resolve to "". But we didn't directly test this, so let's do that. Also, we return -ENOENT if the file is missing, which we didn't document or test.
-rw-r--r--src/shared/specifier.c4
-rw-r--r--src/test/test-specifier.c14
2 files changed, 17 insertions, 1 deletions
diff --git a/src/shared/specifier.c b/src/shared/specifier.c
index a02012407b..df52f8cf81 100644
--- a/src/shared/specifier.c
+++ b/src/shared/specifier.c
@@ -269,7 +269,9 @@ int specifier_architecture(char specifier, const void *data, const char *root, c
}
/* Note: fields in /etc/os-release might quite possibly be missing, even if everything is entirely valid
- * otherwise. We'll return an empty value or NULL in that case from the functions below. */
+ * otherwise. We'll return an empty value or NULL in that case from the functions below. But if the
+ * os-release file is missing, we'll return -ENOENT. This means that something is seriously wrong with the
+ * installation. */
int specifier_os_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
assert(ret);
diff --git a/src/test/test-specifier.c b/src/test/test-specifier.c
index 231625de95..5ece520e9b 100644
--- a/src/test/test-specifier.c
+++ b/src/test/test-specifier.c
@@ -138,4 +138,18 @@ TEST(specifiers) {
}
}
+TEST(specifiers_missing_data_ok) {
+ _cleanup_free_ char *resolved = NULL;
+
+ assert_se(setenv("SYSTEMD_OS_RELEASE", "/dev/null", 1) == 0);
+ assert_se(specifier_printf("%A-%B-%M-%o-%w-%W", SIZE_MAX, specifier_table, NULL, NULL, &resolved) >= 0);
+ assert_se(streq(resolved, "-----"));
+
+ assert_se(setenv("SYSTEMD_OS_RELEASE", "/nosuchfileordirectory", 1) == 0);
+ assert_se(specifier_printf("%A-%B-%M-%o-%w-%W", SIZE_MAX, specifier_table, NULL, NULL, &resolved) == -ENOENT);
+ assert_se(streq(resolved, "-----"));
+
+ assert_se(unsetenv("SYSTEMD_OS_RELEASE") == 0);
+}
+
DEFINE_TEST_MAIN(LOG_DEBUG);