naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
8d419f
From fb51d78042fec1a2df2a7da1f9a759875bd7e07e Mon Sep 17 00:00:00 2001
8d419f
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
8d419f
Date: Tue, 8 Mar 2022 11:38:46 +0100
8d419f
Subject: [PATCH] shared/specifier: clarify and add test for missing data
8d419f
8d419f
In systemd.unit we document that unset fields resolve to "". But we didn't
8d419f
directly test this, so let's do that. Also, we return -ENOENT if the file
8d419f
is missing, which we didn't document or test.
8d419f
8d419f
(cherry picked from commit 7962116fc8a2572c5c89904ac50fe99c8101f28f)
8d419f
8d419f
Related: #2082131
8d419f
---
8d419f
 src/shared/specifier.c    |  4 +++-
8d419f
 src/test/test-specifier.c | 14 ++++++++++++++
8d419f
 2 files changed, 17 insertions(+), 1 deletion(-)
8d419f
8d419f
diff --git a/src/shared/specifier.c b/src/shared/specifier.c
8d419f
index f8ab98541f..c26628975c 100644
8d419f
--- a/src/shared/specifier.c
8d419f
+++ b/src/shared/specifier.c
8d419f
@@ -213,7 +213,9 @@ int specifier_architecture(char specifier, const void *data, const char *root, c
8d419f
 }
8d419f
 
8d419f
 /* Note: fields in /etc/os-release might quite possibly be missing, even if everything is entirely valid
8d419f
- * otherwise. We'll return an empty value or NULL in that case from the functions below. */
8d419f
+ * otherwise. We'll return an empty value or NULL in that case from the functions below. But if the
8d419f
+ * os-release file is missing, we'll return -ENOENT. This means that something is seriously wrong with the
8d419f
+ * installation. */
8d419f
 
8d419f
 int specifier_os_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
8d419f
         return parse_os_release(root, "ID", ret);
8d419f
diff --git a/src/test/test-specifier.c b/src/test/test-specifier.c
8d419f
index dda993ce9d..790f0252d7 100644
8d419f
--- a/src/test/test-specifier.c
8d419f
+++ b/src/test/test-specifier.c
8d419f
@@ -96,4 +96,18 @@ TEST(specifiers) {
8d419f
         }
8d419f
 }
8d419f
 
8d419f
+TEST(specifiers_missing_data_ok) {
8d419f
+        _cleanup_free_ char *resolved = NULL;
8d419f
+
8d419f
+        assert_se(setenv("SYSTEMD_OS_RELEASE", "/dev/null", 1) == 0);
8d419f
+        assert_se(specifier_printf("%A-%B-%M-%o-%w-%W", SIZE_MAX, specifier_table, NULL, NULL, &resolved) >= 0);
8d419f
+        assert_se(streq(resolved, "-----"));
8d419f
+
8d419f
+        assert_se(setenv("SYSTEMD_OS_RELEASE", "/nosuchfileordirectory", 1) == 0);
8d419f
+        assert_se(specifier_printf("%A-%B-%M-%o-%w-%W", SIZE_MAX, specifier_table, NULL, NULL, &resolved) == -ENOENT);
8d419f
+        assert_se(streq(resolved, "-----"));
8d419f
+
8d419f
+        assert_se(unsetenv("SYSTEMD_OS_RELEASE") == 0);
8d419f
+}
8d419f
+
8d419f
 DEFINE_TEST_MAIN(LOG_DEBUG);