diff options
author | Thomas Haller <thaller@redhat.com> | 2020-06-22 19:21:17 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-06-22 23:09:05 +0200 |
commit | 2a6ecf21285d2a7c0c806b6add360437493ff3ba (patch) | |
tree | e603fb255fbe51dd38170bb44bfee12efbdb289b | |
parent | a2142e884b9090f5b5f42b55381a1f848c2426bb (diff) | |
download | NetworkManager-2a6ecf21285d2a7c0c806b6add360437493ff3ba.tar.gz |
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.
-rw-r--r-- | shared/systemd/nm-sd-utils-shared.c | 40 | ||||
-rw-r--r-- | shared/systemd/nm-sd-utils-shared.h | 4 |
2 files changed, 44 insertions, 0 deletions
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__ */ |