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 :(
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-09-01 15:55:45 +02:00
parent 51636f3c39
commit 98dbda7a2c
4 changed files with 53 additions and 1 deletions
+1 -1
View File
@@ -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;
+1
View File
@@ -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);
+4
View File
@@ -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',
+47
View File
@@ -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);