From: Thomas Weißschuh Date: Thu, 16 May 2024 17:05:01 +0000 (+0200) Subject: libuuid: test time-based UUID generation X-Git-Tag: v2.42-start~329^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=244dc7998dac929d89bdedc6de421b7b7c59cdca;p=thirdparty%2Futil-linux.git libuuid: test time-based UUID generation Signed-off-by: Thomas Weißschuh --- diff --git a/libuuid/meson.build b/libuuid/meson.build index b67b3d5e7..7046da930 100644 --- a/libuuid/meson.build +++ b/libuuid/meson.build @@ -14,6 +14,7 @@ lib_uuid_sources = ''' '''.split() predefined_c = files('src/predefined.c') +pack_c = files('src/pack.c') unpack_c = files('src/unpack.c') unparse_c = files('src/unparse.c') @@ -57,3 +58,11 @@ if build_libuuid meson.override_dependency('uuid', uuid_dep) endif endif + +test_uuid_time_sources = files('src/gen_uuid.c') + [ + pack_c, + unpack_c, + randutils_c, + md5_c, + sha1_c, +] diff --git a/libuuid/src/Makemodule.am b/libuuid/src/Makemodule.am index be2781123..e36363697 100644 --- a/libuuid/src/Makemodule.am +++ b/libuuid/src/Makemodule.am @@ -4,6 +4,12 @@ test_uuid_parser_SOURCES = libuuid/src/test_uuid.c test_uuid_parser_LDADD = libuuid.la $(SOCKET_LIBS) $(LDADD) test_uuid_parser_CFLAGS = $(AM_CFLAGS) -I$(ul_libuuid_incdir) +check_PROGRAMS += test_uuid_time +test_uuid_time_SOURCES = libuuid/src/gen_uuid.c \ + libuuid/src/pack.c libuuid/src/unpack.c lib/randutils.c lib/md5.c lib/sha1.c +test_uuid_time_LDADD = libuuid.la $(SOCKET_LIBS) $(LDADD) +test_uuid_time_CFLAGS = $(AM_CFLAGS) -I$(ul_libuuid_incdir) -DTEST_PROGRAM + # includes uuidincdir = $(includedir)/uuid uuidinc_HEADERS = libuuid/src/uuid.h diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c index e80e371c4..806454448 100644 --- a/libuuid/src/gen_uuid.c +++ b/libuuid/src/gen_uuid.c @@ -121,6 +121,17 @@ static int getuid (void) } #endif +#ifdef TEST_PROGRAM +#define gettimeofday gettimeofday_fixed + +static int gettimeofday_fixed(struct timeval *tv, void *tz __attribute__((unused))) +{ + tv->tv_sec = 1645557742; + tv->tv_usec = 123456; + return 0; +} +#endif + /* * Get the ethernet hardware address, if we can find it... * @@ -454,7 +465,7 @@ error: return -1; } -#if defined(HAVE_UUIDD) && defined(HAVE_SYS_UN_H) +#if defined(HAVE_UUIDD) && defined(HAVE_SYS_UN_H) && !defined(TEST_PROGRAM) /* * Try using the uuidd daemon to generate the UUID @@ -800,3 +811,25 @@ void uuid_generate_sha1(uuid_t out, const uuid_t ns, const char *name, size_t le uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x5000; uuid_pack(&uu, out); } + +#ifdef TEST_PROGRAM +int main(void) +{ + char buf[UUID_STR_LEN]; + uuid_t uuid; + + uuid_generate_time(uuid); + uuid_unparse(uuid, buf); + printf("%s\n", buf); + + uuid_generate_time_v6(uuid); + uuid_unparse(uuid, buf); + printf("%s\n", buf); + + uuid_generate_time_v7(uuid); + uuid_unparse(uuid, buf); + printf("%s\n", buf); + + return 0; +} +#endif diff --git a/meson.build b/meson.build index 8b3149102..3df8918c4 100644 --- a/meson.build +++ b/meson.build @@ -3468,6 +3468,17 @@ if not is_disabler(exe) exes += exe endif +exe = executable( + 'test_uuid_time', + test_uuid_time_sources, + include_directories : [dir_include], + link_with : lib_uuid, + c_args : '-DTEST_PROGRAM', + build_by_default : program_tests) +if not is_disabler(exe) + exes += exe +endif + ############################################################ libfdisk_tests_cflags = ['-DTEST_PROGRAM'] diff --git a/tests/commands.sh b/tests/commands.sh index 9eef92ccb..3af675962 100644 --- a/tests/commands.sh +++ b/tests/commands.sh @@ -53,6 +53,7 @@ TS_HELPER_SYSINFO="${ts_helpersdir}test_sysinfo" TS_HELPER_TIOCSTI="${ts_helpersdir}test_tiocsti" TS_HELPER_UUID_PARSER="${ts_helpersdir}test_uuid_parser" TS_HELPER_UUID_NAMESPACE="${ts_helpersdir}test_uuid_namespace" +TS_HELPER_UUID_TIME="${ts_helpersdir}test_uuid_time" TS_HELPER_MBSENCODE="${ts_helpersdir}test_mbsencode" TS_HELPER_CAL="${ts_helpersdir}test_cal" TS_HELPER_LAST_FUZZ="${ts_helpersdir}test_last_fuzz" diff --git a/tests/expected/uuid/time b/tests/expected/uuid/time new file mode 100644 index 000000000..e850d680a --- /dev/null +++ b/tests/expected/uuid/time @@ -0,0 +1,4 @@ +VARIANT TYPE TIME +DCE time-based 2022-02-22 19:22:22,123456+00:00 +DCE time-v6 2022-02-22 19:22:22,123456+00:00 +DCE time-v7 2022-02-22 19:22:22,123000+00:00 diff --git a/tests/ts/uuid/time b/tests/ts/uuid/time new file mode 100755 index 000000000..f8d283d81 --- /dev/null +++ b/tests/ts/uuid/time @@ -0,0 +1,27 @@ +#!/bin/bash + +# This file is part of util-linux. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +TS_TOPDIR="${0%/*}/../.." +TS_DESC="uuid_time" + +. "$TS_TOPDIR"/functions.sh +ts_init "$*" + +ts_check_test_command "$TS_HELPER_UUID_TIME" +ts_check_test_command "$TS_CMD_UUIDPARSE" + +$TS_HELPER_UUID_TIME | TZ=UTC $TS_CMD_UUIDPARSE -o VARIANT,TYPE,TIME \ + >> $TS_OUTPUT 2>> $TS_ERRLOG + +ts_finalize