From 98dbda7a2c0cf995b346ecd1f49c3ed996f7c993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 31 Aug 2026 19:18:44 +0200 Subject: [PATCH] test-hostname-pick-word: add a simple test to check word selection This is useful to check if how fast the code is (painfully slow) and whether worlds indeed are selected with a flat distribution probability (they are). The code is slow because the hashing and because chaseat is exteremely inefficient :( --- src/shared/hostname-setup.c | 2 +- src/shared/hostname-setup.h | 1 + src/test/meson.build | 4 +++ src/test/test-hostname-pick-word.c | 47 ++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/test/test-hostname-pick-word.c diff --git a/src/shared/hostname-setup.c b/src/shared/hostname-setup.c index 2512292b0c..cd728539f3 100644 --- a/src/shared/hostname-setup.c +++ b/src/shared/hostname-setup.c @@ -369,7 +369,7 @@ static int pick_word_linear_scan(FILE *f, off_t offset, char **ret) { } } -static int hostname_pick_word(sd_id128_t mid, size_t pos, char **ret) { +int hostname_pick_word(sd_id128_t mid, size_t pos, char **ret) { static const sd_id128_t word_key = SD_ID128_MAKE(2d,9f,1c,7a,4b,8e,43,11,9a,6d,5f,02,c8,77,e3,14); _cleanup_fclose_ FILE *f = NULL; struct stat st; diff --git a/src/shared/hostname-setup.h b/src/shared/hostname-setup.h index 6e68b6fa79..c9f1e46eb8 100644 --- a/src/shared/hostname-setup.h +++ b/src/shared/hostname-setup.h @@ -22,6 +22,7 @@ int read_etc_hostname(const char *path, bool substitute_wildcards, char **ret); void hostname_update_source_hint(const char *hostname, HostnameSource source); int hostname_setup(bool really); +int hostname_pick_word(sd_id128_t mid, size_t pos, char **ret); int hostname_substitute_wildcards(const char *name, char **ret); char* get_default_hostname(void); diff --git a/src/test/meson.build b/src/test/meson.build index da3148d955..63535a90d3 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -395,6 +395,10 @@ executables += [ ], 'timeout' : 180, }, + test_template + { + 'sources': files('test-hostname-pick-word.c'), + 'type' : 'manual', + }, test_template + { 'sources' : files('test-ipcrm.c'), 'type' : 'unsafe', diff --git a/src/test/test-hostname-pick-word.c b/src/test/test-hostname-pick-word.c new file mode 100644 index 0000000000..3021c6e6ef --- /dev/null +++ b/src/test/test-hostname-pick-word.c @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "sd-id128.h" + +#include "alloc-util.h" +#include "hostname-setup.h" +#include "log.h" +#include "main-func.h" +#include "parse-util.h" +#include "tests.h" + +static int run(int argc, char **argv) { + int r; + + log_parse_environment(); + + unsigned c = 100, pos = 1; + + if (argc > 1) { + r = safe_atou(argv[1], &pos); + if (r < 0) + return log_error_errno(r, "Cannot parse uint %s: %m", argv[1]); + } + + if (argc > 2) { + r = safe_atou(argv[2], &c); + if (r < 0) + return log_error_errno(r, "Cannot parse uint %s: %m", argv[2]); + } + + sd_id128_t mid = SD_ID128_ARRAY(aa,bb,cc,ee,33,21,42,31,89,ba,a4,91,33,22,11,00); + + while (c-- > 0) { + _cleanup_free_ char *w = NULL; + + ASSERT_OK(hostname_pick_word(mid, pos, &w)); + ASSERT_PTR(w); + + printf("%s\n", w); + + mid.qwords[0] += 3; + } + + return 0; +} + +DEFINE_MAIN_FUNCTION(run);