From 2a6ecf21285d2a7c0c806b6add360437493ff3ba Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 22 Jun 2020 19:21:17 +0200 Subject: [PATCH] tests: add nmtst_extract_first_word_all() for testing We usually don't want to use internal API of systemd for our own purposes. Here, we will use it to check our implementation against systemd's. Add an accessor to extract_first_word() for testing. --- shared/systemd/nm-sd-utils-shared.c | 40 +++++++++++++++++++++++++++++ shared/systemd/nm-sd-utils-shared.h | 4 +++ 2 files changed, 44 insertions(+) diff --git a/shared/systemd/nm-sd-utils-shared.c b/shared/systemd/nm-sd-utils-shared.c index 4444e6c7f6..4fe82ca053 100644 --- a/shared/systemd/nm-sd-utils-shared.c +++ b/shared/systemd/nm-sd-utils-shared.c @@ -137,3 +137,43 @@ nm_sd_http_url_is_valid_https (const char *url) nm_assert (_http_url_is_valid (url, FALSE) == http_url_is_valid (url)); return _http_url_is_valid (url, TRUE); } + +/*****************************************************************************/ + +int +nmtst_systemd_extract_first_word_all (const char *str, char ***out_strv) +{ + gs_unref_ptrarray GPtrArray *arr = NULL; + + /* we implement a str split function to parse `/proc/cmdline`. This + * code should behave like systemd, which uses extract_first_word() + * for that. + * + * As we want to unit-test our implementation to match systemd, + * expose this function for testing. */ + + g_assert (out_strv); + g_assert (!*out_strv); + + if (!str) + return 0; + + arr = g_ptr_array_new_with_free_func (g_free); + + for (;;) { + gs_free char *word = NULL; + int r; + + r = extract_first_word (&str, &word, NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX); + if (r < 0) + return r; + if (r == 0) + break; + g_ptr_array_add (arr, g_steal_pointer (&word)); + } + + g_ptr_array_add (arr, NULL); + + *out_strv = (char **) g_ptr_array_free (g_steal_pointer (&arr), FALSE); + return 1; +} diff --git a/shared/systemd/nm-sd-utils-shared.h b/shared/systemd/nm-sd-utils-shared.h index a3ca1edc03..75e38b8422 100644 --- a/shared/systemd/nm-sd-utils-shared.h +++ b/shared/systemd/nm-sd-utils-shared.h @@ -38,4 +38,8 @@ gboolean nm_sd_hostname_is_valid(const char *s, bool allow_trailing_dot); gboolean nm_sd_http_url_is_valid_https (const char *url); +/*****************************************************************************/ + +int nmtst_systemd_extract_first_word_all (const char *str, char ***out_strv); + #endif /* __NM_SD_UTILS_SHARED_H__ */