diff options
| author | Michael Biebl <biebl@debian.org> | 2022-05-04 15:35:24 +0200 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2022-05-04 15:35:24 +0200 |
| commit | 9959fdb2e8ddd06f2161798ca0a39c77d67c652d (patch) | |
| tree | 2ce24a336d2b1c5fd5dec3090db312eded6c78ba /src/core/tests | |
| parent | 8c623dddbdebe354cb94bfc559a5371a14865317 (diff) | |
New upstream version 1.37.92 upstream/1.37.92
Diffstat (limited to 'src/core/tests')
| -rw-r--r-- | src/core/tests/test-core.c | 29 | ||||
| -rw-r--r-- | src/core/tests/test-l3cfg.c | 14 | ||||
| -rw-r--r-- | src/core/tests/test-utils.c | 47 |
3 files changed, 78 insertions, 12 deletions
diff --git a/src/core/tests/test-core.c b/src/core/tests/test-core.c index f61d66d9..b4e1c4d5 100644 --- a/src/core/tests/test-core.c +++ b/src/core/tests/test-core.c @@ -2319,10 +2319,25 @@ test_machine_id_read(void) char machine_id_str[33]; gpointer logstate; + /* This unit test checks our functions to read /etc/machine-id. As + * the path name is not configurable (and the test does not setup + * a chroot/mountns), we read the actual file from the system. That + * is ugly, as the test depends on the system where it's running. + * + * Still, better a bad test, than no test. Patch welcome to fix this + * shortcoming. + * + * Also, if you have a sufficiently broken system, the unit test fails. + * In particular, if the machine-id file exists but does not contain + * a valid ID. Just don't have that. Fix your system. */ + logstate = nmtst_logging_disable(FALSE); /* If you run this test as root, without a valid /etc/machine-id, * the code will try to get the secret-key. That is a bit ugly, - * but no real problem. */ + * but no real problem. + * + * The real answer is: don't run our unit tests as root. That's + * not the way to do it. */ machine_id = nm_utils_machine_id_bin(); nmtst_logging_reenable(logstate); @@ -2338,9 +2353,15 @@ test_machine_id_read(void) * is invalid. Our machine-id is fake, and we have nothing to * compare against. */ - /* NOTE: this test will fail, if you don't have /etc/machine-id, - * but a valid "LOCALSTATEDIR/lib/dbus/machine-id" file. - * Just don't do that. */ + if (g_file_test(LOCALSTATEDIR "/lib/dbus/machine-id", G_FILE_TEST_EXISTS)) { + /* Hm. So systemd failed to read /etc/machine-id, but we may have the one from D-Bus. + * With LOCALSTATEDIR"/lib/dbus/machine-id", we don't really know whether we + * parsed that file. Assume we don't know and skip the test on this system. */ + g_assert(!nm_utils_machine_id_is_fake()); + return; + } + + /* OK, in this case, our function should have generated a random machine ID. */ g_assert(nm_utils_machine_id_is_fake()); } else { g_assert(!nm_utils_machine_id_is_fake()); diff --git a/src/core/tests/test-l3cfg.c b/src/core/tests/test-l3cfg.c index 5501079e..924d98f1 100644 --- a/src/core/tests/test-l3cfg.c +++ b/src/core/tests/test-l3cfg.c @@ -382,13 +382,11 @@ test_l3cfg(gconstpointer test_data) nm_l3cfg_commit_type_register(l3cfg0, NM_L3_CFG_COMMIT_TYPE_UPDATE, NULL, "test1"); if (!nmtst_get_rand_one_case_in(4)) { - commit_type_2 = - nm_l3cfg_commit_type_register(l3cfg0, - nmtst_rand_select(NM_L3_CFG_COMMIT_TYPE_NONE, - NM_L3_CFG_COMMIT_TYPE_ASSUME, - NM_L3_CFG_COMMIT_TYPE_UPDATE), - NULL, - "test2"); + commit_type_2 = nm_l3cfg_commit_type_register( + l3cfg0, + nmtst_rand_select(NM_L3_CFG_COMMIT_TYPE_NONE, NM_L3_CFG_COMMIT_TYPE_UPDATE), + NULL, + "test2"); } else commit_type_2 = NULL; @@ -956,7 +954,7 @@ test_l3_ipv6ll(gconstpointer test_data) .steps_done = FALSE, }; TestL3IPv6LLData *const tdata = &tdata_stack; - char sbuf1[sizeof(_nm_utils_to_string_buffer)]; + char sbuf1[NM_UTILS_TO_STRING_BUFFER_SIZE]; int r; _LOGD("test start (/l3-ipv6ll/%d)", TEST_IDX); diff --git a/src/core/tests/test-utils.c b/src/core/tests/test-utils.c index 2d2b7340..ad9950dd 100644 --- a/src/core/tests/test-utils.c +++ b/src/core/tests/test-utils.c @@ -212,6 +212,52 @@ test_hw_addr_gen_stable_eth(void) "04:0D:CD:0C:9E:2C"); } +static void +test_shorten_hostname(void) +{ + gs_free char *maxhost = NULL; + char *hostname; + +#define do_test_shorten_hostname(_host, _exp_res, _exp_short) \ + G_STMT_START \ + { \ + gboolean _res; \ + gs_free char *_short = NULL; \ + \ + _res = nm_utils_shorten_hostname((_host), &_short); \ + g_assert_cmpint((_res), ==, (_exp_res)); \ + g_assert_cmpstr(_short, ==, (_exp_short)); \ + } \ + G_STMT_END + + /* 'maxhost' is the longest allowed hostname according to + * system configuration (`getconf HOST_NAME_MAX`). On Linux + * it's typically 64 characters, but POSIX allows up to + * 255 characters. + */ + maxhost = g_strnfill(HOST_NAME_MAX, 'a'); + + do_test_shorten_hostname("name1", TRUE, NULL); + + do_test_shorten_hostname("name1.example.com", TRUE, NULL); + + do_test_shorten_hostname(maxhost, TRUE, NULL); + + hostname = g_strdup_printf("%sbbb", maxhost); + do_test_shorten_hostname(hostname, TRUE, maxhost); + nm_clear_g_free(&hostname); + + hostname = g_strdup_printf("%s.com", maxhost); + do_test_shorten_hostname(hostname, TRUE, maxhost); + nm_clear_g_free(&hostname); + + hostname = g_strdup_printf("name1.%s.com", maxhost); + do_test_shorten_hostname(hostname, TRUE, "name1"); + nm_clear_g_free(&hostname); + + do_test_shorten_hostname(".name1", FALSE, NULL); +} + /*****************************************************************************/ NMTST_DEFINE(); @@ -223,6 +269,7 @@ main(int argc, char **argv) g_test_add_func("/utils/stable_privacy", test_stable_privacy); g_test_add_func("/utils/hw_addr_gen_stable_eth", test_hw_addr_gen_stable_eth); + g_test_add_func("/utils/shorten-hostname", test_shorten_hostname); return g_test_run(); } |